sharpyuv.c 21 KB

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