sharpyuv.c 19 KB

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