SkYUVMath.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright 2019 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkMatrix44.h"
  8. #include "src/core/SkYUVMath.h"
  9. // in SkColorMatrix order (row-major)
  10. // Created by running SkColorMatrix_DumpYUVMatrixTables()
  11. const float Rec709_rgb_to_yuv[] = {
  12. 0.182586f, 0.614231f, 0.062007f, 0.000000f, 0.062745f,
  13. -0.100644f, -0.338572f, 0.439216f, 0.000000f, 0.501961f,
  14. 0.439216f, -0.398942f, -0.040274f, 0.000000f, 0.501961f,
  15. 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
  16. };
  17. const float Rec709_yuv_to_rgb[] = {
  18. 1.164384f, 0.000000f, 1.792741f, 0.000000f, -0.972945f,
  19. 1.164384f, -0.213249f, -0.532909f, 0.000000f, 0.301483f,
  20. 1.164384f, 2.112402f, 0.000000f, 0.000000f, -1.133402f,
  21. 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
  22. };
  23. const float Rec601_rgb_to_yuv[] = {
  24. 0.256788f, 0.504129f, 0.097906f, 0.000000f, 0.062745f,
  25. -0.148223f, -0.290993f, 0.439216f, 0.000000f, 0.501961f,
  26. 0.439216f, -0.367788f, -0.071427f, 0.000000f, 0.501961f,
  27. 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
  28. };
  29. const float Rec601_yuv_to_rgb[] = {
  30. 1.164384f, 0.000000f, 1.596027f, 0.000000f, -0.874202f,
  31. 1.164384f, -0.391762f, -0.812968f, 0.000000f, 0.531668f,
  32. 1.164384f, 2.017232f, 0.000000f, 0.000000f, -1.085631f,
  33. 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
  34. };
  35. const float JPEG_rgb_to_yuv[] = {
  36. 0.299000f, 0.587000f, 0.114000f, 0.000000f, 0.000000f,
  37. -0.168736f, -0.331264f, 0.500000f, 0.000000f, 0.501961f,
  38. 0.500000f, -0.418688f, -0.081312f, 0.000000f, 0.501961f,
  39. 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
  40. };
  41. const float JPEG_yuv_to_rgb[] = {
  42. 1.000000f, 0.000000f, 1.402000f, 0.000000f, -0.703749f,
  43. 1.000000f, -0.344136f, -0.714136f, 0.000000f, 0.531211f,
  44. 1.000000f, 1.772000f, 0.000000f, 0.000000f, -0.889475f,
  45. 0.000000f, 0.000000f, 0.000000f, 1.000000f, 0.000000f,
  46. };
  47. static_assert(kJPEG_SkYUVColorSpace == 0, "");
  48. static_assert(kRec601_SkYUVColorSpace == 1, "");
  49. static_assert(kRec709_SkYUVColorSpace == 2, "");
  50. const float* yuv_to_rgb_array[] = {
  51. JPEG_yuv_to_rgb,
  52. Rec601_yuv_to_rgb,
  53. Rec709_yuv_to_rgb,
  54. };
  55. const float* rgb_to_yuv_array[] = {
  56. JPEG_rgb_to_yuv,
  57. Rec601_rgb_to_yuv,
  58. Rec709_rgb_to_yuv,
  59. };
  60. constexpr size_t kSizeOfColorMatrix = 20 * sizeof(float);
  61. void SkColorMatrix_RGB2YUV(SkYUVColorSpace cs, float m[20]) {
  62. if ((unsigned)cs < (unsigned)kIdentity_SkYUVColorSpace) {
  63. memcpy(m, rgb_to_yuv_array[(unsigned)cs], kSizeOfColorMatrix);
  64. } else {
  65. memset(m, 0, kSizeOfColorMatrix);
  66. m[0] = m[6] = m[12] = m[18] = 1;
  67. }
  68. }
  69. void SkColorMatrix_YUV2RGB(SkYUVColorSpace cs, float m[20]) {
  70. if ((unsigned)cs < (unsigned)kIdentity_SkYUVColorSpace) {
  71. memcpy(m, yuv_to_rgb_array[(unsigned)cs], kSizeOfColorMatrix);
  72. } else {
  73. memset(m, 0, kSizeOfColorMatrix);
  74. m[0] = m[6] = m[12] = m[18] = 1;
  75. }
  76. }
  77. ///////////////////////////////////////////////////////////////////////////////////////////////////
  78. // we just drop the alpha rol/col from the colormatrix
  79. // output is | tr |
  80. // | 3x3 tg |
  81. // | tb |
  82. // | 0 0 0 1 |
  83. static void colormatrix_to_matrix44(const float src[20], SkMatrix44* dst) {
  84. for (int r = 0; r < 3; ++r) {
  85. for (int c = 0; c < 3; ++c) {
  86. dst->set(r, c, src[r*5 + c]);
  87. }
  88. dst->set(r, 3, src[r*5 + 4]);
  89. }
  90. dst->set(3, 0, 0);
  91. dst->set(3, 1, 0);
  92. dst->set(3, 2, 0);
  93. dst->set(3, 3, 1);
  94. }
  95. // input: ignore the bottom row
  96. // output: inject identity row/column for alpha
  97. static void matrix44_to_colormatrix(const SkMatrix44& src, float dst[20]) {
  98. for (int r = 0; r < 3; ++r) {
  99. for (int c = 0; c < 3; ++c) {
  100. dst[r*5 + c] = src.get(r, c);
  101. }
  102. dst[r*5 + 3] = 0; // scale alpha
  103. dst[r*5 + 4] = src.get(r, 3); // translate
  104. }
  105. dst[15] = dst[16] = dst[17] = dst[19] = 0;
  106. dst[18] = 1;
  107. }
  108. static void scale3(float m[], float s) {
  109. for (int i = 0; i < 3; ++i) {
  110. m[i] *= s;
  111. }
  112. }
  113. namespace {
  114. struct YUVCoeff {
  115. float Kr, Kb;
  116. float Cr, Cb;
  117. float scaleY, addY;
  118. float scaleUV;
  119. };
  120. } // namespace
  121. const YUVCoeff gCoeff[] = {
  122. // kJPEG_SkYUVColorSpace
  123. { 0.299f, 0.114f, 1/1.772f, 1/1.402f, 1, 0, 1, },
  124. // kRec601_SkYUVColorSpace
  125. { 0.299f, 0.114f, 1/1.772f, 1/1.402f, 219/255.f, 16/255.f, 224/255.f, },
  126. // kRec709_SkYUVColorSpace
  127. { 0.2126f, 0.0722f, 1/1.8556f, 1/1.5748f, 219/255.f, 16/255.f, 224/255.f, },
  128. };
  129. static void make_rgb_to_yuv_matrix(float mx[20], const YUVCoeff& c) {
  130. const float Kr = c.Kr;
  131. const float Kb = c.Kb;
  132. const float Kg = 1.0f - Kr - Kb;
  133. float m[20] = {
  134. Kr, Kg, Kb, 0, c.addY,
  135. -Kr, -Kg, 1-Kb, 0, 128/255.f,
  136. 1-Kr, -Kg, -Kb, 0, 128/255.f,
  137. 0, 0, 0, 1, 0,
  138. };
  139. memcpy(mx, m, sizeof(m));
  140. scale3(mx + 0, c.scaleY);
  141. scale3(mx + 5, c.Cr * c.scaleUV);
  142. scale3(mx + 10, c.Cb * c.scaleUV);
  143. }
  144. static void dump(const float m[20], SkYUVColorSpace cs, bool rgb2yuv) {
  145. const char* names[] = {
  146. "JPEG", "Rec601", "Rec709",
  147. };
  148. const char* dirnames[] = {
  149. "yuv_to_rgb", "rgb_to_yuv",
  150. };
  151. SkDebugf("const float %s_%s[] = {\n", names[cs], dirnames[rgb2yuv]);
  152. for (int i = 0; i < 4; ++i) {
  153. SkDebugf(" ");
  154. for (int j = 0; j < 5; ++j) {
  155. SkDebugf(" %9.6ff,", m[i * 5 + j]);
  156. }
  157. SkDebugf("\n");
  158. }
  159. SkDebugf("};\n");
  160. }
  161. // Used to create the prebuilt tables for each colorspace.
  162. // Don't remove this function, in case we want to recompute those tables in the future.
  163. void SkColorMatrix_DumpYUVMatrixTables() {
  164. for (auto cs : {kRec709_SkYUVColorSpace, kRec601_SkYUVColorSpace, kJPEG_SkYUVColorSpace}) {
  165. float m[20];
  166. make_rgb_to_yuv_matrix(m, gCoeff[(unsigned)cs]);
  167. dump(m, cs, true);
  168. SkMatrix44 m44, im44;
  169. colormatrix_to_matrix44(m, &m44);
  170. float im[20];
  171. #ifdef SK_DEBUG
  172. // be sure our coversion between matrix44 and colormatrix is perfect
  173. matrix44_to_colormatrix(m44, im);
  174. SkASSERT(memcmp(m, im, sizeof(im)) == 0);
  175. #endif
  176. SkAssertResult(m44.invert(&im44));
  177. matrix44_to_colormatrix(im44, im);
  178. dump(im, cs, false);
  179. }
  180. }