sharpyuv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. // Copyright 2022 Google Inc. All Rights Reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license
  4. // that can be found in the COPYING file in the root of the source
  5. // tree. An additional intellectual property rights grant can be found
  6. // in the file PATENTS. All contributing project authors may
  7. // be found in the AUTHORS file in the root of the source tree.
  8. // -----------------------------------------------------------------------------
  9. //
  10. // Sharp RGB to YUV conversion.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include "sharpyuv/sharpyuv.h"
  14. #include <assert.h>
  15. #include <limits.h>
  16. #include <math.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include "src/webp/types.h"
  20. #include "src/dsp/cpu.h"
  21. #include "sharpyuv/sharpyuv_dsp.h"
  22. #include "sharpyuv/sharpyuv_gamma.h"
  23. //------------------------------------------------------------------------------
  24. // Sharp RGB->YUV conversion
  25. static const int kNumIterations = 4;
  26. #define YUV_FIX 16 // fixed-point precision for RGB->YUV
  27. static const int kYuvHalf = 1 << (YUV_FIX - 1);
  28. // Max bit depth so that intermediate calculations fit in 16 bits.
  29. static const int kMaxBitDepth = 14;
  30. // Returns the precision shift to use based on the input rgb_bit_depth.
  31. static int GetPrecisionShift(int rgb_bit_depth) {
  32. // Try to add 2 bits of precision if it fits in kMaxBitDepth. Otherwise remove
  33. // bits if needed.
  34. return ((rgb_bit_depth + 2) <= kMaxBitDepth) ? 2
  35. : (kMaxBitDepth - rgb_bit_depth);
  36. }
  37. typedef int16_t fixed_t; // signed type with extra precision for UV
  38. typedef uint16_t fixed_y_t; // unsigned type with extra precision for W
  39. //------------------------------------------------------------------------------
  40. static uint8_t clip_8b(fixed_t v) {
  41. return (!(v & ~0xff)) ? (uint8_t)v : (v < 0) ? 0u : 255u;
  42. }
  43. static uint16_t clip(fixed_t v, int max) {
  44. return (v < 0) ? 0 : (v > max) ? max : (uint16_t)v;
  45. }
  46. static fixed_y_t clip_bit_depth(int y, int bit_depth) {
  47. const int max = (1 << bit_depth) - 1;
  48. return (!(y & ~max)) ? (fixed_y_t)y : (y < 0) ? 0 : max;
  49. }
  50. //------------------------------------------------------------------------------
  51. static int RGBToGray(int64_t r, int64_t g, int64_t b) {
  52. const int64_t luma = 13933 * r + 46871 * g + 4732 * b + kYuvHalf;
  53. return (int)(luma >> YUV_FIX);
  54. }
  55. static uint32_t ScaleDown(uint16_t a, uint16_t b, uint16_t c, uint16_t d,
  56. int rgb_bit_depth) {
  57. const int bit_depth = rgb_bit_depth + GetPrecisionShift(rgb_bit_depth);
  58. const uint32_t A = SharpYuvGammaToLinear(a, bit_depth);
  59. const uint32_t B = SharpYuvGammaToLinear(b, bit_depth);
  60. const uint32_t C = SharpYuvGammaToLinear(c, bit_depth);
  61. const uint32_t D = SharpYuvGammaToLinear(d, bit_depth);
  62. return SharpYuvLinearToGamma((A + B + C + D + 2) >> 2, bit_depth);
  63. }
  64. static WEBP_INLINE void UpdateW(const fixed_y_t* src, fixed_y_t* dst, int w,
  65. int rgb_bit_depth) {
  66. const int bit_depth = rgb_bit_depth + GetPrecisionShift(rgb_bit_depth);
  67. int i;
  68. for (i = 0; i < w; ++i) {
  69. const uint32_t R = SharpYuvGammaToLinear(src[0 * w + i], bit_depth);
  70. const uint32_t G = SharpYuvGammaToLinear(src[1 * w + i], bit_depth);
  71. const uint32_t B = SharpYuvGammaToLinear(src[2 * w + i], bit_depth);
  72. const uint32_t Y = RGBToGray(R, G, B);
  73. dst[i] = (fixed_y_t)SharpYuvLinearToGamma(Y, bit_depth);
  74. }
  75. }
  76. static void UpdateChroma(const fixed_y_t* src1, const fixed_y_t* src2,
  77. fixed_t* dst, int uv_w, int rgb_bit_depth) {
  78. int i;
  79. for (i = 0; i < uv_w; ++i) {
  80. const int r =
  81. ScaleDown(src1[0 * uv_w + 0], src1[0 * uv_w + 1], src2[0 * uv_w + 0],
  82. src2[0 * uv_w + 1], rgb_bit_depth);
  83. const int g =
  84. ScaleDown(src1[2 * uv_w + 0], src1[2 * uv_w + 1], src2[2 * uv_w + 0],
  85. src2[2 * uv_w + 1], rgb_bit_depth);
  86. const int b =
  87. ScaleDown(src1[4 * uv_w + 0], src1[4 * uv_w + 1], src2[4 * uv_w + 0],
  88. src2[4 * uv_w + 1], rgb_bit_depth);
  89. const int W = RGBToGray(r, g, b);
  90. dst[0 * uv_w] = (fixed_t)(r - W);
  91. dst[1 * uv_w] = (fixed_t)(g - W);
  92. dst[2 * uv_w] = (fixed_t)(b - W);
  93. dst += 1;
  94. src1 += 2;
  95. src2 += 2;
  96. }
  97. }
  98. static void StoreGray(const fixed_y_t* rgb, fixed_y_t* y, int w) {
  99. int i;
  100. assert(w > 0);
  101. for (i = 0; i < w; ++i) {
  102. y[i] = RGBToGray(rgb[0 * w + i], rgb[1 * w + i], rgb[2 * w + i]);
  103. }
  104. }
  105. //------------------------------------------------------------------------------
  106. static WEBP_INLINE fixed_y_t Filter2(int A, int B, int W0, int bit_depth) {
  107. const int v0 = (A * 3 + B + 2) >> 2;
  108. return clip_bit_depth(v0 + W0, bit_depth);
  109. }
  110. //------------------------------------------------------------------------------
  111. static WEBP_INLINE int Shift(int v, int shift) {
  112. return (shift >= 0) ? (v << shift) : (v >> -shift);
  113. }
  114. static void ImportOneRow(const uint8_t* const r_ptr,
  115. const uint8_t* const g_ptr,
  116. const uint8_t* const b_ptr,
  117. int rgb_step,
  118. int rgb_bit_depth,
  119. int pic_width,
  120. fixed_y_t* const dst) {
  121. // Convert the rgb_step from a number of bytes to a number of uint8_t or
  122. // uint16_t values depending the bit depth.
  123. const int step = (rgb_bit_depth > 8) ? rgb_step / 2 : rgb_step;
  124. int i;
  125. const int w = (pic_width + 1) & ~1;
  126. for (i = 0; i < pic_width; ++i) {
  127. const int off = i * step;
  128. const int shift = GetPrecisionShift(rgb_bit_depth);
  129. if (rgb_bit_depth == 8) {
  130. dst[i + 0 * w] = Shift(r_ptr[off], shift);
  131. dst[i + 1 * w] = Shift(g_ptr[off], shift);
  132. dst[i + 2 * w] = Shift(b_ptr[off], shift);
  133. } else {
  134. dst[i + 0 * w] = Shift(((uint16_t*)r_ptr)[off], shift);
  135. dst[i + 1 * w] = Shift(((uint16_t*)g_ptr)[off], shift);
  136. dst[i + 2 * w] = Shift(((uint16_t*)b_ptr)[off], shift);
  137. }
  138. }
  139. if (pic_width & 1) { // replicate rightmost pixel
  140. dst[pic_width + 0 * w] = dst[pic_width + 0 * w - 1];
  141. dst[pic_width + 1 * w] = dst[pic_width + 1 * w - 1];
  142. dst[pic_width + 2 * w] = dst[pic_width + 2 * w - 1];
  143. }
  144. }
  145. static void InterpolateTwoRows(const fixed_y_t* const best_y,
  146. const fixed_t* prev_uv,
  147. const fixed_t* cur_uv,
  148. const fixed_t* next_uv,
  149. int w,
  150. fixed_y_t* out1,
  151. fixed_y_t* out2,
  152. int rgb_bit_depth) {
  153. const int uv_w = w >> 1;
  154. const int len = (w - 1) >> 1; // length to filter
  155. int k = 3;
  156. const int bit_depth = rgb_bit_depth + GetPrecisionShift(rgb_bit_depth);
  157. while (k-- > 0) { // process each R/G/B segments in turn
  158. // special boundary case for i==0
  159. out1[0] = Filter2(cur_uv[0], prev_uv[0], best_y[0], bit_depth);
  160. out2[0] = Filter2(cur_uv[0], next_uv[0], best_y[w], bit_depth);
  161. SharpYuvFilterRow(cur_uv, prev_uv, len, best_y + 0 + 1, out1 + 1,
  162. bit_depth);
  163. SharpYuvFilterRow(cur_uv, next_uv, len, best_y + w + 1, out2 + 1,
  164. bit_depth);
  165. // special boundary case for i == w - 1 when w is even
  166. if (!(w & 1)) {
  167. out1[w - 1] = Filter2(cur_uv[uv_w - 1], prev_uv[uv_w - 1],
  168. best_y[w - 1 + 0], bit_depth);
  169. out2[w - 1] = Filter2(cur_uv[uv_w - 1], next_uv[uv_w - 1],
  170. best_y[w - 1 + w], bit_depth);
  171. }
  172. out1 += w;
  173. out2 += w;
  174. prev_uv += uv_w;
  175. cur_uv += uv_w;
  176. next_uv += uv_w;
  177. }
  178. }
  179. static WEBP_INLINE int RGBToYUVComponent(int r, int g, int b,
  180. const int coeffs[4], int sfix) {
  181. const int srounder = 1 << (YUV_FIX + sfix - 1);
  182. const int luma = coeffs[0] * r + coeffs[1] * g + coeffs[2] * b +
  183. coeffs[3] + srounder;
  184. return (luma >> (YUV_FIX + sfix));
  185. }
  186. static int ConvertWRGBToYUV(const fixed_y_t* best_y, const fixed_t* best_uv,
  187. uint8_t* y_ptr, int y_stride, uint8_t* u_ptr,
  188. int u_stride, uint8_t* v_ptr, int v_stride,
  189. int rgb_bit_depth,
  190. int yuv_bit_depth, int width, int height,
  191. const SharpYuvConversionMatrix* yuv_matrix) {
  192. int i, j;
  193. const fixed_t* const best_uv_base = best_uv;
  194. const int w = (width + 1) & ~1;
  195. const int h = (height + 1) & ~1;
  196. const int uv_w = w >> 1;
  197. const int uv_h = h >> 1;
  198. const int sfix = GetPrecisionShift(rgb_bit_depth);
  199. const int yuv_max = (1 << yuv_bit_depth) - 1;
  200. for (best_uv = best_uv_base, j = 0; j < height; ++j) {
  201. for (i = 0; i < width; ++i) {
  202. const int off = (i >> 1);
  203. const int W = best_y[i];
  204. const int r = best_uv[off + 0 * uv_w] + W;
  205. const int g = best_uv[off + 1 * uv_w] + W;
  206. const int b = best_uv[off + 2 * uv_w] + W;
  207. const int y = RGBToYUVComponent(r, g, b, yuv_matrix->rgb_to_y, sfix);
  208. if (yuv_bit_depth <= 8) {
  209. y_ptr[i] = clip_8b(y);
  210. } else {
  211. ((uint16_t*)y_ptr)[i] = clip(y, yuv_max);
  212. }
  213. }
  214. best_y += w;
  215. best_uv += (j & 1) * 3 * uv_w;
  216. y_ptr += y_stride;
  217. }
  218. for (best_uv = best_uv_base, j = 0; j < uv_h; ++j) {
  219. for (i = 0; i < uv_w; ++i) {
  220. const int off = i;
  221. // Note r, g and b values here are off by W, but a constant offset on all
  222. // 3 components doesn't change the value of u and v with a YCbCr matrix.
  223. const int r = best_uv[off + 0 * uv_w];
  224. const int g = best_uv[off + 1 * uv_w];
  225. const int b = best_uv[off + 2 * uv_w];
  226. const int u = RGBToYUVComponent(r, g, b, yuv_matrix->rgb_to_u, sfix);
  227. const int v = RGBToYUVComponent(r, g, b, yuv_matrix->rgb_to_v, sfix);
  228. if (yuv_bit_depth <= 8) {
  229. u_ptr[i] = clip_8b(u);
  230. v_ptr[i] = clip_8b(v);
  231. } else {
  232. ((uint16_t*)u_ptr)[i] = clip(u, yuv_max);
  233. ((uint16_t*)v_ptr)[i] = clip(v, yuv_max);
  234. }
  235. }
  236. best_uv += 3 * uv_w;
  237. u_ptr += u_stride;
  238. v_ptr += v_stride;
  239. }
  240. return 1;
  241. }
  242. //------------------------------------------------------------------------------
  243. // Main function
  244. static void* SafeMalloc(uint64_t nmemb, size_t size) {
  245. const uint64_t total_size = nmemb * (uint64_t)size;
  246. if (total_size != (size_t)total_size) return NULL;
  247. return malloc((size_t)total_size);
  248. }
  249. #define SAFE_ALLOC(W, H, T) ((T*)SafeMalloc((W) * (H), sizeof(T)))
  250. static int DoSharpArgbToYuv(const uint8_t* r_ptr, const uint8_t* g_ptr,
  251. const uint8_t* b_ptr, int rgb_step, int rgb_stride,
  252. int rgb_bit_depth, uint8_t* y_ptr, int y_stride,
  253. uint8_t* u_ptr, int u_stride, uint8_t* v_ptr,
  254. int v_stride, int yuv_bit_depth, int width,
  255. int height,
  256. const SharpYuvConversionMatrix* yuv_matrix) {
  257. // we expand the right/bottom border if needed
  258. const int w = (width + 1) & ~1;
  259. const int h = (height + 1) & ~1;
  260. const int uv_w = w >> 1;
  261. const int uv_h = h >> 1;
  262. uint64_t prev_diff_y_sum = ~0;
  263. int j, iter;
  264. // TODO(skal): allocate one big memory chunk. But for now, it's easier
  265. // for valgrind debugging to have several chunks.
  266. fixed_y_t* const tmp_buffer = SAFE_ALLOC(w * 3, 2, fixed_y_t); // scratch
  267. fixed_y_t* const best_y_base = SAFE_ALLOC(w, h, fixed_y_t);
  268. fixed_y_t* const target_y_base = SAFE_ALLOC(w, h, fixed_y_t);
  269. fixed_y_t* const best_rgb_y = SAFE_ALLOC(w, 2, fixed_y_t);
  270. fixed_t* const best_uv_base = SAFE_ALLOC(uv_w * 3, uv_h, fixed_t);
  271. fixed_t* const target_uv_base = SAFE_ALLOC(uv_w * 3, uv_h, fixed_t);
  272. fixed_t* const best_rgb_uv = SAFE_ALLOC(uv_w * 3, 1, fixed_t);
  273. fixed_y_t* best_y = best_y_base;
  274. fixed_y_t* target_y = target_y_base;
  275. fixed_t* best_uv = best_uv_base;
  276. fixed_t* target_uv = target_uv_base;
  277. const uint64_t diff_y_threshold = (uint64_t)(3.0 * w * h);
  278. int ok;
  279. assert(w > 0);
  280. assert(h > 0);
  281. if (best_y_base == NULL || best_uv_base == NULL ||
  282. target_y_base == NULL || target_uv_base == NULL ||
  283. best_rgb_y == NULL || best_rgb_uv == NULL ||
  284. tmp_buffer == NULL) {
  285. ok = 0;
  286. goto End;
  287. }
  288. // Import RGB samples to W/RGB representation.
  289. for (j = 0; j < height; j += 2) {
  290. const int is_last_row = (j == height - 1);
  291. fixed_y_t* const src1 = tmp_buffer + 0 * w;
  292. fixed_y_t* const src2 = tmp_buffer + 3 * w;
  293. // prepare two rows of input
  294. ImportOneRow(r_ptr, g_ptr, b_ptr, rgb_step, rgb_bit_depth, width,
  295. src1);
  296. if (!is_last_row) {
  297. ImportOneRow(r_ptr + rgb_stride, g_ptr + rgb_stride, b_ptr + rgb_stride,
  298. rgb_step, rgb_bit_depth, width, src2);
  299. } else {
  300. memcpy(src2, src1, 3 * w * sizeof(*src2));
  301. }
  302. StoreGray(src1, best_y + 0, w);
  303. StoreGray(src2, best_y + w, w);
  304. UpdateW(src1, target_y, w, rgb_bit_depth);
  305. UpdateW(src2, target_y + w, w, rgb_bit_depth);
  306. UpdateChroma(src1, src2, target_uv, uv_w, rgb_bit_depth);
  307. memcpy(best_uv, target_uv, 3 * uv_w * sizeof(*best_uv));
  308. best_y += 2 * w;
  309. best_uv += 3 * uv_w;
  310. target_y += 2 * w;
  311. target_uv += 3 * uv_w;
  312. r_ptr += 2 * rgb_stride;
  313. g_ptr += 2 * rgb_stride;
  314. b_ptr += 2 * rgb_stride;
  315. }
  316. // Iterate and resolve clipping conflicts.
  317. for (iter = 0; iter < kNumIterations; ++iter) {
  318. const fixed_t* cur_uv = best_uv_base;
  319. const fixed_t* prev_uv = best_uv_base;
  320. uint64_t diff_y_sum = 0;
  321. best_y = best_y_base;
  322. best_uv = best_uv_base;
  323. target_y = target_y_base;
  324. target_uv = target_uv_base;
  325. for (j = 0; j < h; j += 2) {
  326. fixed_y_t* const src1 = tmp_buffer + 0 * w;
  327. fixed_y_t* const src2 = tmp_buffer + 3 * w;
  328. {
  329. const fixed_t* const next_uv = cur_uv + ((j < h - 2) ? 3 * uv_w : 0);
  330. InterpolateTwoRows(best_y, prev_uv, cur_uv, next_uv, w,
  331. src1, src2, rgb_bit_depth);
  332. prev_uv = cur_uv;
  333. cur_uv = next_uv;
  334. }
  335. UpdateW(src1, best_rgb_y + 0 * w, w, rgb_bit_depth);
  336. UpdateW(src2, best_rgb_y + 1 * w, w, rgb_bit_depth);
  337. UpdateChroma(src1, src2, best_rgb_uv, uv_w, rgb_bit_depth);
  338. // update two rows of Y and one row of RGB
  339. diff_y_sum +=
  340. SharpYuvUpdateY(target_y, best_rgb_y, best_y, 2 * w,
  341. rgb_bit_depth + GetPrecisionShift(rgb_bit_depth));
  342. SharpYuvUpdateRGB(target_uv, best_rgb_uv, best_uv, 3 * uv_w);
  343. best_y += 2 * w;
  344. best_uv += 3 * uv_w;
  345. target_y += 2 * w;
  346. target_uv += 3 * uv_w;
  347. }
  348. // test exit condition
  349. if (iter > 0) {
  350. if (diff_y_sum < diff_y_threshold) break;
  351. if (diff_y_sum > prev_diff_y_sum) break;
  352. }
  353. prev_diff_y_sum = diff_y_sum;
  354. }
  355. // final reconstruction
  356. ok = ConvertWRGBToYUV(best_y_base, best_uv_base, y_ptr, y_stride, u_ptr,
  357. u_stride, v_ptr, v_stride, rgb_bit_depth, yuv_bit_depth,
  358. width, height, yuv_matrix);
  359. End:
  360. free(best_y_base);
  361. free(best_uv_base);
  362. free(target_y_base);
  363. free(target_uv_base);
  364. free(best_rgb_y);
  365. free(best_rgb_uv);
  366. free(tmp_buffer);
  367. return ok;
  368. }
  369. #undef SAFE_ALLOC
  370. // Hidden exported init function.
  371. // By default SharpYuvConvert calls it with NULL. If needed, users can declare
  372. // it as extern and call it with a VP8CPUInfo function.
  373. extern void SharpYuvInit(VP8CPUInfo cpu_info_func);
  374. void SharpYuvInit(VP8CPUInfo cpu_info_func) {
  375. static volatile VP8CPUInfo sharpyuv_last_cpuinfo_used =
  376. (VP8CPUInfo)&sharpyuv_last_cpuinfo_used;
  377. const int initialized =
  378. (sharpyuv_last_cpuinfo_used != (VP8CPUInfo)&sharpyuv_last_cpuinfo_used);
  379. if (cpu_info_func == NULL && initialized) return;
  380. if (sharpyuv_last_cpuinfo_used == cpu_info_func) return;
  381. SharpYuvInitDsp(cpu_info_func);
  382. if (!initialized) {
  383. SharpYuvInitGammaTables();
  384. }
  385. sharpyuv_last_cpuinfo_used = cpu_info_func;
  386. }
  387. int SharpYuvConvert(const void* r_ptr, const void* g_ptr,
  388. const void* b_ptr, int rgb_step, int rgb_stride,
  389. int rgb_bit_depth, void* y_ptr, int y_stride,
  390. void* u_ptr, int u_stride, void* v_ptr,
  391. int v_stride, int yuv_bit_depth, int width,
  392. int height, const SharpYuvConversionMatrix* yuv_matrix) {
  393. SharpYuvConversionMatrix scaled_matrix;
  394. const int rgb_max = (1 << rgb_bit_depth) - 1;
  395. const int rgb_round = 1 << (rgb_bit_depth - 1);
  396. const int yuv_max = (1 << yuv_bit_depth) - 1;
  397. const int sfix = GetPrecisionShift(rgb_bit_depth);
  398. if (width < 1 || height < 1 || width == INT_MAX || height == INT_MAX ||
  399. r_ptr == NULL || g_ptr == NULL || b_ptr == NULL || y_ptr == NULL ||
  400. u_ptr == NULL || v_ptr == NULL) {
  401. return 0;
  402. }
  403. if (rgb_bit_depth != 8 && rgb_bit_depth != 10 && rgb_bit_depth != 12 &&
  404. rgb_bit_depth != 16) {
  405. return 0;
  406. }
  407. if (yuv_bit_depth != 8 && yuv_bit_depth != 10 && yuv_bit_depth != 12) {
  408. return 0;
  409. }
  410. if (rgb_bit_depth > 8 && (rgb_step % 2 != 0 || rgb_stride %2 != 0)) {
  411. // Step/stride should be even for uint16_t buffers.
  412. return 0;
  413. }
  414. if (yuv_bit_depth > 8 &&
  415. (y_stride % 2 != 0 || u_stride % 2 != 0 || v_stride % 2 != 0)) {
  416. // Stride should be even for uint16_t buffers.
  417. return 0;
  418. }
  419. SharpYuvInit(NULL);
  420. // Add scaling factor to go from rgb_bit_depth to yuv_bit_depth, to the
  421. // rgb->yuv conversion matrix.
  422. if (rgb_bit_depth == yuv_bit_depth) {
  423. memcpy(&scaled_matrix, yuv_matrix, sizeof(scaled_matrix));
  424. } else {
  425. int i;
  426. for (i = 0; i < 3; ++i) {
  427. scaled_matrix.rgb_to_y[i] =
  428. (yuv_matrix->rgb_to_y[i] * yuv_max + rgb_round) / rgb_max;
  429. scaled_matrix.rgb_to_u[i] =
  430. (yuv_matrix->rgb_to_u[i] * yuv_max + rgb_round) / rgb_max;
  431. scaled_matrix.rgb_to_v[i] =
  432. (yuv_matrix->rgb_to_v[i] * yuv_max + rgb_round) / rgb_max;
  433. }
  434. }
  435. // Also incorporate precision change scaling.
  436. scaled_matrix.rgb_to_y[3] = Shift(yuv_matrix->rgb_to_y[3], sfix);
  437. scaled_matrix.rgb_to_u[3] = Shift(yuv_matrix->rgb_to_u[3], sfix);
  438. scaled_matrix.rgb_to_v[3] = Shift(yuv_matrix->rgb_to_v[3], sfix);
  439. return DoSharpArgbToYuv(r_ptr, g_ptr, b_ptr, rgb_step, rgb_stride,
  440. rgb_bit_depth, y_ptr, y_stride, u_ptr, u_stride,
  441. v_ptr, v_stride, yuv_bit_depth, width, height,
  442. &scaled_matrix);
  443. }
  444. //------------------------------------------------------------------------------