colorspace.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Colorspace conversion defines
  3. * Copyright (c) 2001, 2002, 2003 Fabrice Bellard
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. /**
  22. * @file
  23. * Various defines for YUV<->RGB conversion
  24. */
  25. #ifndef AVUTIL_COLORSPACE_H
  26. #define AVUTIL_COLORSPACE_H
  27. #define SCALEBITS 10
  28. #define ONE_HALF (1 << (SCALEBITS - 1))
  29. #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
  30. #define YUV_TO_RGB1_CCIR(cb1, cr1)\
  31. {\
  32. cb = (cb1) - 128;\
  33. cr = (cr1) - 128;\
  34. r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
  35. g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
  36. ONE_HALF;\
  37. b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
  38. }
  39. #define YUV_TO_RGB1_CCIR_BT709(cb1, cr1) \
  40. { \
  41. cb = (cb1) - 128; \
  42. cr = (cr1) - 128; \
  43. r_add = ONE_HALF + FIX(1.5747 * 255.0 / 224.0) * cr; \
  44. g_add = ONE_HALF - FIX(0.1873 * 255.0 / 224.0) * cb - \
  45. FIX(0.4682 * 255.0 / 224.0) * cr; \
  46. b_add = ONE_HALF + FIX(1.8556 * 255.0 / 224.0) * cb; \
  47. }
  48. // To be used for the BT709 variant as well
  49. #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
  50. {\
  51. y = ((y1) - 16) * FIX(255.0/219.0);\
  52. r = cm[(y + r_add) >> SCALEBITS];\
  53. g = cm[(y + g_add) >> SCALEBITS];\
  54. b = cm[(y + b_add) >> SCALEBITS];\
  55. }
  56. #define YUV_TO_RGB1(cb1, cr1)\
  57. {\
  58. cb = (cb1) - 128;\
  59. cr = (cr1) - 128;\
  60. r_add = FIX(1.40200) * cr + ONE_HALF;\
  61. g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
  62. b_add = FIX(1.77200) * cb + ONE_HALF;\
  63. }
  64. #define YUV_TO_RGB2(r, g, b, y1)\
  65. {\
  66. y = (y1) << SCALEBITS;\
  67. r = cm[(y + r_add) >> SCALEBITS];\
  68. g = cm[(y + g_add) >> SCALEBITS];\
  69. b = cm[(y + b_add) >> SCALEBITS];\
  70. }
  71. #define Y_CCIR_TO_JPEG(y)\
  72. cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
  73. #define Y_JPEG_TO_CCIR(y)\
  74. (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  75. #define C_CCIR_TO_JPEG(y)\
  76. cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
  77. /* NOTE: the clamp is really necessary! */
  78. static inline int C_JPEG_TO_CCIR(int y) {
  79. y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
  80. if (y < 16)
  81. y = 16;
  82. return y;
  83. }
  84. #define RGB_TO_Y_CCIR(r, g, b) \
  85. ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
  86. FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  87. #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
  88. (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
  89. FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  90. #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
  91. (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
  92. FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  93. #define RGB_TO_Y_JPEG(r, g, b) \
  94. (FFMIN((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
  95. FIX(0.11400) * (b) + (ONE_HALF)) >> SCALEBITS, 255))
  96. #define RGB_TO_U_JPEG(r1, g1, b1)\
  97. (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
  98. FIX(0.50000) * b1 + (ONE_HALF) - 1) >> (SCALEBITS)) + 128)
  99. #define RGB_TO_V_JPEG(r1, g1, b1)\
  100. (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
  101. FIX(0.08131) * b1 + (ONE_HALF) - 1) >> (SCALEBITS)) + 128)
  102. #endif /* AVUTIL_COLORSPACE_H */