You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
3.5KB

  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 colorspace.h
  23. * Various defines for YUV<->RGB conversion
  24. */
  25. #define SCALEBITS 10
  26. #define ONE_HALF (1 << (SCALEBITS - 1))
  27. #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
  28. #define YUV_TO_RGB1_CCIR(cb1, cr1)\
  29. {\
  30. cb = (cb1) - 128;\
  31. cr = (cr1) - 128;\
  32. r_add = FIX(1.40200*255.0/224.0) * cr + ONE_HALF;\
  33. g_add = - FIX(0.34414*255.0/224.0) * cb - FIX(0.71414*255.0/224.0) * cr + \
  34. ONE_HALF;\
  35. b_add = FIX(1.77200*255.0/224.0) * cb + ONE_HALF;\
  36. }
  37. #define YUV_TO_RGB2_CCIR(r, g, b, y1)\
  38. {\
  39. y = ((y1) - 16) * FIX(255.0/219.0);\
  40. r = cm[(y + r_add) >> SCALEBITS];\
  41. g = cm[(y + g_add) >> SCALEBITS];\
  42. b = cm[(y + b_add) >> SCALEBITS];\
  43. }
  44. #define YUV_TO_RGB1(cb1, cr1)\
  45. {\
  46. cb = (cb1) - 128;\
  47. cr = (cr1) - 128;\
  48. r_add = FIX(1.40200) * cr + ONE_HALF;\
  49. g_add = - FIX(0.34414) * cb - FIX(0.71414) * cr + ONE_HALF;\
  50. b_add = FIX(1.77200) * cb + ONE_HALF;\
  51. }
  52. #define YUV_TO_RGB2(r, g, b, y1)\
  53. {\
  54. y = (y1) << SCALEBITS;\
  55. r = cm[(y + r_add) >> SCALEBITS];\
  56. g = cm[(y + g_add) >> SCALEBITS];\
  57. b = cm[(y + b_add) >> SCALEBITS];\
  58. }
  59. #define Y_CCIR_TO_JPEG(y)\
  60. cm[((y) * FIX(255.0/219.0) + (ONE_HALF - 16 * FIX(255.0/219.0))) >> SCALEBITS]
  61. #define Y_JPEG_TO_CCIR(y)\
  62. (((y) * FIX(219.0/255.0) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  63. #define C_CCIR_TO_JPEG(y)\
  64. cm[(((y) - 128) * FIX(127.0/112.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS]
  65. /* NOTE: the clamp is really necessary! */
  66. static inline int C_JPEG_TO_CCIR(int y) {
  67. y = (((y - 128) * FIX(112.0/127.0) + (ONE_HALF + (128 << SCALEBITS))) >> SCALEBITS);
  68. if (y < 16)
  69. y = 16;
  70. return y;
  71. }
  72. #define RGB_TO_Y(r, g, b) \
  73. ((FIX(0.29900) * (r) + FIX(0.58700) * (g) + \
  74. FIX(0.11400) * (b) + ONE_HALF) >> SCALEBITS)
  75. #define RGB_TO_U(r1, g1, b1, shift)\
  76. (((- FIX(0.16874) * r1 - FIX(0.33126) * g1 + \
  77. FIX(0.50000) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  78. #define RGB_TO_V(r1, g1, b1, shift)\
  79. (((FIX(0.50000) * r1 - FIX(0.41869) * g1 - \
  80. FIX(0.08131) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  81. #define RGB_TO_Y_CCIR(r, g, b) \
  82. ((FIX(0.29900*219.0/255.0) * (r) + FIX(0.58700*219.0/255.0) * (g) + \
  83. FIX(0.11400*219.0/255.0) * (b) + (ONE_HALF + (16 << SCALEBITS))) >> SCALEBITS)
  84. #define RGB_TO_U_CCIR(r1, g1, b1, shift)\
  85. (((- FIX(0.16874*224.0/255.0) * r1 - FIX(0.33126*224.0/255.0) * g1 + \
  86. FIX(0.50000*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)
  87. #define RGB_TO_V_CCIR(r1, g1, b1, shift)\
  88. (((FIX(0.50000*224.0/255.0) * r1 - FIX(0.41869*224.0/255.0) * g1 - \
  89. FIX(0.08131*224.0/255.0) * b1 + (ONE_HALF << shift) - 1) >> (SCALEBITS + shift)) + 128)