picture_csp.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. // Copyright 2014 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. // WebPPicture utils for colorspace conversion
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <assert.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16. #include "./vp8enci.h"
  17. #include "../utils/random.h"
  18. #include "../utils/utils.h"
  19. #include "../dsp/yuv.h"
  20. // Uncomment to disable gamma-compression during RGB->U/V averaging
  21. #define USE_GAMMA_COMPRESSION
  22. // If defined, use table to compute x / alpha.
  23. #define USE_INVERSE_ALPHA_TABLE
  24. static const union {
  25. uint32_t argb;
  26. uint8_t bytes[4];
  27. } test_endian = { 0xff000000u };
  28. #define ALPHA_IS_LAST (test_endian.bytes[3] == 0xff)
  29. static WEBP_INLINE uint32_t MakeARGB32(int a, int r, int g, int b) {
  30. return (((uint32_t)a << 24) | (r << 16) | (g << 8) | b);
  31. }
  32. //------------------------------------------------------------------------------
  33. // Detection of non-trivial transparency
  34. // Returns true if alpha[] has non-0xff values.
  35. static int CheckNonOpaque(const uint8_t* alpha, int width, int height,
  36. int x_step, int y_step) {
  37. if (alpha == NULL) return 0;
  38. while (height-- > 0) {
  39. int x;
  40. for (x = 0; x < width * x_step; x += x_step) {
  41. if (alpha[x] != 0xff) return 1; // TODO(skal): check 4/8 bytes at a time.
  42. }
  43. alpha += y_step;
  44. }
  45. return 0;
  46. }
  47. // Checking for the presence of non-opaque alpha.
  48. int WebPPictureHasTransparency(const WebPPicture* picture) {
  49. if (picture == NULL) return 0;
  50. if (!picture->use_argb) {
  51. return CheckNonOpaque(picture->a, picture->width, picture->height,
  52. 1, picture->a_stride);
  53. } else {
  54. int x, y;
  55. const uint32_t* argb = picture->argb;
  56. if (argb == NULL) return 0;
  57. for (y = 0; y < picture->height; ++y) {
  58. for (x = 0; x < picture->width; ++x) {
  59. if (argb[x] < 0xff000000u) return 1; // test any alpha values != 0xff
  60. }
  61. argb += picture->argb_stride;
  62. }
  63. }
  64. return 0;
  65. }
  66. //------------------------------------------------------------------------------
  67. // Code for gamma correction
  68. #if defined(USE_GAMMA_COMPRESSION)
  69. // gamma-compensates loss of resolution during chroma subsampling
  70. #define kGamma 0.80 // for now we use a different gamma value than kGammaF
  71. #define kGammaFix 12 // fixed-point precision for linear values
  72. #define kGammaScale ((1 << kGammaFix) - 1)
  73. #define kGammaTabFix 7 // fixed-point fractional bits precision
  74. #define kGammaTabScale (1 << kGammaTabFix)
  75. #define kGammaTabRounder (kGammaTabScale >> 1)
  76. #define kGammaTabSize (1 << (kGammaFix - kGammaTabFix))
  77. static int kLinearToGammaTab[kGammaTabSize + 1];
  78. static uint16_t kGammaToLinearTab[256];
  79. static int kGammaTablesOk = 0;
  80. static void InitGammaTables(void) {
  81. if (!kGammaTablesOk) {
  82. int v;
  83. const double scale = (double)(1 << kGammaTabFix) / kGammaScale;
  84. const double norm = 1. / 255.;
  85. for (v = 0; v <= 255; ++v) {
  86. kGammaToLinearTab[v] =
  87. (uint16_t)(pow(norm * v, kGamma) * kGammaScale + .5);
  88. }
  89. for (v = 0; v <= kGammaTabSize; ++v) {
  90. kLinearToGammaTab[v] = (int)(255. * pow(scale * v, 1. / kGamma) + .5);
  91. }
  92. kGammaTablesOk = 1;
  93. }
  94. }
  95. static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) {
  96. return kGammaToLinearTab[v];
  97. }
  98. static WEBP_INLINE int Interpolate(int v) {
  99. const int tab_pos = v >> (kGammaTabFix + 2); // integer part
  100. const int x = v & ((kGammaTabScale << 2) - 1); // fractional part
  101. const int v0 = kLinearToGammaTab[tab_pos];
  102. const int v1 = kLinearToGammaTab[tab_pos + 1];
  103. const int y = v1 * x + v0 * ((kGammaTabScale << 2) - x); // interpolate
  104. assert(tab_pos + 1 < kGammaTabSize + 1);
  105. return y;
  106. }
  107. // Convert a linear value 'v' to YUV_FIX+2 fixed-point precision
  108. // U/V value, suitable for RGBToU/V calls.
  109. static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {
  110. const int y = Interpolate(base_value << shift); // final uplifted value
  111. return (y + kGammaTabRounder) >> kGammaTabFix; // descale
  112. }
  113. #else
  114. static void InitGammaTables(void) {}
  115. static WEBP_INLINE uint32_t GammaToLinear(uint8_t v) { return v; }
  116. static WEBP_INLINE int LinearToGamma(uint32_t base_value, int shift) {
  117. return (int)(base_value << shift);
  118. }
  119. #endif // USE_GAMMA_COMPRESSION
  120. //------------------------------------------------------------------------------
  121. // RGB -> YUV conversion
  122. static int RGBToY(int r, int g, int b, VP8Random* const rg) {
  123. return (rg == NULL) ? VP8RGBToY(r, g, b, YUV_HALF)
  124. : VP8RGBToY(r, g, b, VP8RandomBits(rg, YUV_FIX));
  125. }
  126. static int RGBToU(int r, int g, int b, VP8Random* const rg) {
  127. return (rg == NULL) ? VP8RGBToU(r, g, b, YUV_HALF << 2)
  128. : VP8RGBToU(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));
  129. }
  130. static int RGBToV(int r, int g, int b, VP8Random* const rg) {
  131. return (rg == NULL) ? VP8RGBToV(r, g, b, YUV_HALF << 2)
  132. : VP8RGBToV(r, g, b, VP8RandomBits(rg, YUV_FIX + 2));
  133. }
  134. //------------------------------------------------------------------------------
  135. // Smart RGB->YUV conversion
  136. static const int kNumIterations = 6;
  137. static const int kMinDimensionIterativeConversion = 4;
  138. // We use a-priori a different precision for storing RGB and Y/W components
  139. // We could use YFIX=0 and only uint8_t for fixed_y_t, but it produces some
  140. // banding sometimes. Better use extra precision.
  141. // TODO(skal): cleanup once TFIX/YFIX values are fixed.
  142. typedef int16_t fixed_t; // signed type with extra TFIX precision for UV
  143. typedef uint16_t fixed_y_t; // unsigned type with extra YFIX precision for W
  144. #define TFIX 6 // fixed-point precision of RGB
  145. #define YFIX 2 // fixed point precision for Y/W
  146. #define THALF ((1 << TFIX) >> 1)
  147. #define MAX_Y_T ((256 << YFIX) - 1)
  148. #define TROUNDER (1 << (YUV_FIX + TFIX - 1))
  149. #if defined(USE_GAMMA_COMPRESSION)
  150. // float variant of gamma-correction
  151. // We use tables of different size and precision, along with a 'real-world'
  152. // Gamma value close to ~2.
  153. #define kGammaF 2.2
  154. static float kGammaToLinearTabF[MAX_Y_T + 1]; // size scales with Y_FIX
  155. static float kLinearToGammaTabF[kGammaTabSize + 2];
  156. static int kGammaTablesFOk = 0;
  157. static void InitGammaTablesF(void) {
  158. if (!kGammaTablesFOk) {
  159. int v;
  160. const double norm = 1. / MAX_Y_T;
  161. const double scale = 1. / kGammaTabSize;
  162. for (v = 0; v <= MAX_Y_T; ++v) {
  163. kGammaToLinearTabF[v] = (float)pow(norm * v, kGammaF);
  164. }
  165. for (v = 0; v <= kGammaTabSize; ++v) {
  166. kLinearToGammaTabF[v] = (float)(MAX_Y_T * pow(scale * v, 1. / kGammaF));
  167. }
  168. // to prevent small rounding errors to cause read-overflow:
  169. kLinearToGammaTabF[kGammaTabSize + 1] = kLinearToGammaTabF[kGammaTabSize];
  170. kGammaTablesFOk = 1;
  171. }
  172. }
  173. static WEBP_INLINE float GammaToLinearF(int v) {
  174. return kGammaToLinearTabF[v];
  175. }
  176. static WEBP_INLINE float LinearToGammaF(float value) {
  177. const float v = value * kGammaTabSize;
  178. const int tab_pos = (int)v;
  179. const float x = v - (float)tab_pos; // fractional part
  180. const float v0 = kLinearToGammaTabF[tab_pos + 0];
  181. const float v1 = kLinearToGammaTabF[tab_pos + 1];
  182. const float y = v1 * x + v0 * (1.f - x); // interpolate
  183. return y;
  184. }
  185. #else
  186. static void InitGammaTablesF(void) {}
  187. static WEBP_INLINE float GammaToLinearF(int v) {
  188. const float norm = 1.f / MAX_Y_T;
  189. return norm * v;
  190. }
  191. static WEBP_INLINE float LinearToGammaF(float value) {
  192. return MAX_Y_T * value;
  193. }
  194. #endif // USE_GAMMA_COMPRESSION
  195. //------------------------------------------------------------------------------
  196. // precision: YFIX -> TFIX
  197. static WEBP_INLINE int FixedYToW(int v) {
  198. #if TFIX == YFIX
  199. return v;
  200. #elif TFIX >= YFIX
  201. return v << (TFIX - YFIX);
  202. #else
  203. return v >> (YFIX - TFIX);
  204. #endif
  205. }
  206. static WEBP_INLINE int FixedWToY(int v) {
  207. #if TFIX == YFIX
  208. return v;
  209. #elif YFIX >= TFIX
  210. return v << (YFIX - TFIX);
  211. #else
  212. return v >> (TFIX - YFIX);
  213. #endif
  214. }
  215. static uint8_t clip_8b(fixed_t v) {
  216. return (!(v & ~0xff)) ? (uint8_t)v : (v < 0) ? 0u : 255u;
  217. }
  218. static fixed_y_t clip_y(int y) {
  219. return (!(y & ~MAX_Y_T)) ? (fixed_y_t)y : (y < 0) ? 0 : MAX_Y_T;
  220. }
  221. // precision: TFIX -> YFIX
  222. static fixed_y_t clip_fixed_t(fixed_t v) {
  223. const int y = FixedWToY(v);
  224. const fixed_y_t w = clip_y(y);
  225. return w;
  226. }
  227. //------------------------------------------------------------------------------
  228. static int RGBToGray(int r, int g, int b) {
  229. const int luma = 19595 * r + 38470 * g + 7471 * b + YUV_HALF;
  230. return (luma >> YUV_FIX);
  231. }
  232. static float RGBToGrayF(float r, float g, float b) {
  233. return 0.299f * r + 0.587f * g + 0.114f * b;
  234. }
  235. static float ScaleDown(int a, int b, int c, int d) {
  236. const float A = GammaToLinearF(a);
  237. const float B = GammaToLinearF(b);
  238. const float C = GammaToLinearF(c);
  239. const float D = GammaToLinearF(d);
  240. return LinearToGammaF(0.25f * (A + B + C + D));
  241. }
  242. static WEBP_INLINE void UpdateW(const fixed_y_t* src, fixed_y_t* dst, int len) {
  243. while (len-- > 0) {
  244. const float R = GammaToLinearF(src[0]);
  245. const float G = GammaToLinearF(src[1]);
  246. const float B = GammaToLinearF(src[2]);
  247. const float Y = RGBToGrayF(R, G, B);
  248. *dst++ = (fixed_y_t)(LinearToGammaF(Y) + .5);
  249. src += 3;
  250. }
  251. }
  252. static WEBP_INLINE void UpdateChroma(const fixed_y_t* src1,
  253. const fixed_y_t* src2,
  254. fixed_t* dst, fixed_y_t* tmp, int len) {
  255. while (len--> 0) {
  256. const float r = ScaleDown(src1[0], src1[3], src2[0], src2[3]);
  257. const float g = ScaleDown(src1[1], src1[4], src2[1], src2[4]);
  258. const float b = ScaleDown(src1[2], src1[5], src2[2], src2[5]);
  259. const float W = RGBToGrayF(r, g, b);
  260. dst[0] = (fixed_t)FixedYToW((int)(r - W));
  261. dst[1] = (fixed_t)FixedYToW((int)(g - W));
  262. dst[2] = (fixed_t)FixedYToW((int)(b - W));
  263. dst += 3;
  264. src1 += 6;
  265. src2 += 6;
  266. if (tmp != NULL) {
  267. tmp[0] = tmp[1] = clip_y((int)(W + .5));
  268. tmp += 2;
  269. }
  270. }
  271. }
  272. //------------------------------------------------------------------------------
  273. static WEBP_INLINE int Filter(const fixed_t* const A, const fixed_t* const B,
  274. int rightwise) {
  275. int v;
  276. if (!rightwise) {
  277. v = (A[0] * 9 + A[-3] * 3 + B[0] * 3 + B[-3]);
  278. } else {
  279. v = (A[0] * 9 + A[+3] * 3 + B[0] * 3 + B[+3]);
  280. }
  281. return (v + 8) >> 4;
  282. }
  283. static WEBP_INLINE int Filter2(int A, int B) { return (A * 3 + B + 2) >> 2; }
  284. //------------------------------------------------------------------------------
  285. // 8bit -> YFIX
  286. static WEBP_INLINE fixed_y_t UpLift(uint8_t a) {
  287. return ((fixed_y_t)a << YFIX) | (1 << (YFIX - 1));
  288. }
  289. static void ImportOneRow(const uint8_t* const r_ptr,
  290. const uint8_t* const g_ptr,
  291. const uint8_t* const b_ptr,
  292. int step,
  293. int pic_width,
  294. fixed_y_t* const dst) {
  295. int i;
  296. for (i = 0; i < pic_width; ++i) {
  297. const int off = i * step;
  298. dst[3 * i + 0] = UpLift(r_ptr[off]);
  299. dst[3 * i + 1] = UpLift(g_ptr[off]);
  300. dst[3 * i + 2] = UpLift(b_ptr[off]);
  301. }
  302. if (pic_width & 1) { // replicate rightmost pixel
  303. memcpy(dst + 3 * pic_width, dst + 3 * (pic_width - 1), 3 * sizeof(*dst));
  304. }
  305. }
  306. static void InterpolateTwoRows(const fixed_y_t* const best_y,
  307. const fixed_t* const prev_uv,
  308. const fixed_t* const cur_uv,
  309. const fixed_t* const next_uv,
  310. int w,
  311. fixed_y_t* const out1,
  312. fixed_y_t* const out2) {
  313. int i, k;
  314. { // special boundary case for i==0
  315. const int W0 = FixedYToW(best_y[0]);
  316. const int W1 = FixedYToW(best_y[w]);
  317. for (k = 0; k <= 2; ++k) {
  318. out1[k] = clip_fixed_t(Filter2(cur_uv[k], prev_uv[k]) + W0);
  319. out2[k] = clip_fixed_t(Filter2(cur_uv[k], next_uv[k]) + W1);
  320. }
  321. }
  322. for (i = 1; i < w - 1; ++i) {
  323. const int W0 = FixedYToW(best_y[i + 0]);
  324. const int W1 = FixedYToW(best_y[i + w]);
  325. const int off = 3 * (i >> 1);
  326. for (k = 0; k <= 2; ++k) {
  327. const int tmp0 = Filter(cur_uv + off + k, prev_uv + off + k, i & 1);
  328. const int tmp1 = Filter(cur_uv + off + k, next_uv + off + k, i & 1);
  329. out1[3 * i + k] = clip_fixed_t(tmp0 + W0);
  330. out2[3 * i + k] = clip_fixed_t(tmp1 + W1);
  331. }
  332. }
  333. { // special boundary case for i == w - 1
  334. const int W0 = FixedYToW(best_y[i + 0]);
  335. const int W1 = FixedYToW(best_y[i + w]);
  336. const int off = 3 * (i >> 1);
  337. for (k = 0; k <= 2; ++k) {
  338. out1[3 * i + k] =
  339. clip_fixed_t(Filter2(cur_uv[off + k], prev_uv[off + k]) + W0);
  340. out2[3 * i + k] =
  341. clip_fixed_t(Filter2(cur_uv[off + k], next_uv[off + k]) + W1);
  342. }
  343. }
  344. }
  345. static WEBP_INLINE uint8_t ConvertRGBToY(int r, int g, int b) {
  346. const int luma = 16839 * r + 33059 * g + 6420 * b + TROUNDER;
  347. return clip_8b(16 + (luma >> (YUV_FIX + TFIX)));
  348. }
  349. static WEBP_INLINE uint8_t ConvertRGBToU(int r, int g, int b) {
  350. const int u = -9719 * r - 19081 * g + 28800 * b + TROUNDER;
  351. return clip_8b(128 + (u >> (YUV_FIX + TFIX)));
  352. }
  353. static WEBP_INLINE uint8_t ConvertRGBToV(int r, int g, int b) {
  354. const int v = +28800 * r - 24116 * g - 4684 * b + TROUNDER;
  355. return clip_8b(128 + (v >> (YUV_FIX + TFIX)));
  356. }
  357. static int ConvertWRGBToYUV(const fixed_y_t* const best_y,
  358. const fixed_t* const best_uv,
  359. WebPPicture* const picture) {
  360. int i, j;
  361. const int w = (picture->width + 1) & ~1;
  362. const int h = (picture->height + 1) & ~1;
  363. const int uv_w = w >> 1;
  364. const int uv_h = h >> 1;
  365. for (j = 0; j < picture->height; ++j) {
  366. for (i = 0; i < picture->width; ++i) {
  367. const int off = 3 * ((i >> 1) + (j >> 1) * uv_w);
  368. const int off2 = i + j * picture->y_stride;
  369. const int W = FixedYToW(best_y[i + j * w]);
  370. const int r = best_uv[off + 0] + W;
  371. const int g = best_uv[off + 1] + W;
  372. const int b = best_uv[off + 2] + W;
  373. picture->y[off2] = ConvertRGBToY(r, g, b);
  374. }
  375. }
  376. for (j = 0; j < uv_h; ++j) {
  377. uint8_t* const dst_u = picture->u + j * picture->uv_stride;
  378. uint8_t* const dst_v = picture->v + j * picture->uv_stride;
  379. for (i = 0; i < uv_w; ++i) {
  380. const int off = 3 * (i + j * uv_w);
  381. const int r = best_uv[off + 0];
  382. const int g = best_uv[off + 1];
  383. const int b = best_uv[off + 2];
  384. dst_u[i] = ConvertRGBToU(r, g, b);
  385. dst_v[i] = ConvertRGBToV(r, g, b);
  386. }
  387. }
  388. return 1;
  389. }
  390. //------------------------------------------------------------------------------
  391. // Main function
  392. #define SAFE_ALLOC(W, H, T) ((T*)WebPSafeMalloc((W) * (H), sizeof(T)))
  393. static int PreprocessARGB(const uint8_t* const r_ptr,
  394. const uint8_t* const g_ptr,
  395. const uint8_t* const b_ptr,
  396. int step, int rgb_stride,
  397. WebPPicture* const picture) {
  398. // we expand the right/bottom border if needed
  399. const int w = (picture->width + 1) & ~1;
  400. const int h = (picture->height + 1) & ~1;
  401. const int uv_w = w >> 1;
  402. const int uv_h = h >> 1;
  403. int i, j, iter;
  404. // TODO(skal): allocate one big memory chunk. But for now, it's easier
  405. // for valgrind debugging to have several chunks.
  406. fixed_y_t* const tmp_buffer = SAFE_ALLOC(w * 3, 2, fixed_y_t); // scratch
  407. fixed_y_t* const best_y = SAFE_ALLOC(w, h, fixed_y_t);
  408. fixed_y_t* const target_y = SAFE_ALLOC(w, h, fixed_y_t);
  409. fixed_y_t* const best_rgb_y = SAFE_ALLOC(w, 2, fixed_y_t);
  410. fixed_t* const best_uv = SAFE_ALLOC(uv_w * 3, uv_h, fixed_t);
  411. fixed_t* const target_uv = SAFE_ALLOC(uv_w * 3, uv_h, fixed_t);
  412. fixed_t* const best_rgb_uv = SAFE_ALLOC(uv_w * 3, 1, fixed_t);
  413. int ok;
  414. if (best_y == NULL || best_uv == NULL ||
  415. target_y == NULL || target_uv == NULL ||
  416. best_rgb_y == NULL || best_rgb_uv == NULL ||
  417. tmp_buffer == NULL) {
  418. ok = WebPEncodingSetError(picture, VP8_ENC_ERROR_OUT_OF_MEMORY);
  419. goto End;
  420. }
  421. assert(picture->width >= kMinDimensionIterativeConversion);
  422. assert(picture->height >= kMinDimensionIterativeConversion);
  423. // Import RGB samples to W/RGB representation.
  424. for (j = 0; j < picture->height; j += 2) {
  425. const int is_last_row = (j == picture->height - 1);
  426. fixed_y_t* const src1 = tmp_buffer;
  427. fixed_y_t* const src2 = tmp_buffer + 3 * w;
  428. const int off1 = j * rgb_stride;
  429. const int off2 = off1 + rgb_stride;
  430. const int uv_off = (j >> 1) * 3 * uv_w;
  431. fixed_y_t* const dst_y = best_y + j * w;
  432. // prepare two rows of input
  433. ImportOneRow(r_ptr + off1, g_ptr + off1, b_ptr + off1,
  434. step, picture->width, src1);
  435. if (!is_last_row) {
  436. ImportOneRow(r_ptr + off2, g_ptr + off2, b_ptr + off2,
  437. step, picture->width, src2);
  438. } else {
  439. memcpy(src2, src1, 3 * w * sizeof(*src2));
  440. }
  441. UpdateW(src1, target_y + (j + 0) * w, w);
  442. UpdateW(src2, target_y + (j + 1) * w, w);
  443. UpdateChroma(src1, src2, target_uv + uv_off, dst_y, uv_w);
  444. memcpy(best_uv + uv_off, target_uv + uv_off, 3 * uv_w * sizeof(*best_uv));
  445. memcpy(dst_y + w, dst_y, w * sizeof(*dst_y));
  446. }
  447. // Iterate and resolve clipping conflicts.
  448. for (iter = 0; iter < kNumIterations; ++iter) {
  449. int k;
  450. const fixed_t* cur_uv = best_uv;
  451. const fixed_t* prev_uv = best_uv;
  452. for (j = 0; j < h; j += 2) {
  453. fixed_y_t* const src1 = tmp_buffer;
  454. fixed_y_t* const src2 = tmp_buffer + 3 * w;
  455. {
  456. const fixed_t* const next_uv = cur_uv + ((j < h - 2) ? 3 * uv_w : 0);
  457. InterpolateTwoRows(best_y + j * w, prev_uv, cur_uv, next_uv,
  458. w, src1, src2);
  459. prev_uv = cur_uv;
  460. cur_uv = next_uv;
  461. }
  462. UpdateW(src1, best_rgb_y + 0 * w, w);
  463. UpdateW(src2, best_rgb_y + 1 * w, w);
  464. UpdateChroma(src1, src2, best_rgb_uv, NULL, uv_w);
  465. // update two rows of Y and one row of RGB
  466. for (i = 0; i < 2 * w; ++i) {
  467. const int off = i + j * w;
  468. const int diff_y = target_y[off] - best_rgb_y[i];
  469. const int new_y = (int)best_y[off] + diff_y;
  470. best_y[off] = clip_y(new_y);
  471. }
  472. for (i = 0; i < uv_w; ++i) {
  473. const int off = 3 * (i + (j >> 1) * uv_w);
  474. int W;
  475. for (k = 0; k <= 2; ++k) {
  476. const int diff_uv = (int)target_uv[off + k] - best_rgb_uv[3 * i + k];
  477. best_uv[off + k] += diff_uv;
  478. }
  479. W = RGBToGray(best_uv[off + 0], best_uv[off + 1], best_uv[off + 2]);
  480. for (k = 0; k <= 2; ++k) {
  481. best_uv[off + k] -= W;
  482. }
  483. }
  484. }
  485. // TODO(skal): add early-termination criterion
  486. }
  487. // final reconstruction
  488. ok = ConvertWRGBToYUV(best_y, best_uv, picture);
  489. End:
  490. WebPSafeFree(best_y);
  491. WebPSafeFree(best_uv);
  492. WebPSafeFree(target_y);
  493. WebPSafeFree(target_uv);
  494. WebPSafeFree(best_rgb_y);
  495. WebPSafeFree(best_rgb_uv);
  496. WebPSafeFree(tmp_buffer);
  497. return ok;
  498. }
  499. #undef SAFE_ALLOC
  500. //------------------------------------------------------------------------------
  501. // "Fast" regular RGB->YUV
  502. #define SUM4(ptr, step) LinearToGamma( \
  503. GammaToLinear((ptr)[0]) + \
  504. GammaToLinear((ptr)[(step)]) + \
  505. GammaToLinear((ptr)[rgb_stride]) + \
  506. GammaToLinear((ptr)[rgb_stride + (step)]), 0) \
  507. #define SUM2(ptr) \
  508. LinearToGamma(GammaToLinear((ptr)[0]) + GammaToLinear((ptr)[rgb_stride]), 1)
  509. #define SUM2ALPHA(ptr) ((ptr)[0] + (ptr)[rgb_stride])
  510. #define SUM4ALPHA(ptr) (SUM2ALPHA(ptr) + SUM2ALPHA((ptr) + 4))
  511. #if defined(USE_INVERSE_ALPHA_TABLE)
  512. static const int kAlphaFix = 19;
  513. // Following table is (1 << kAlphaFix) / a. The (v * kInvAlpha[a]) >> kAlphaFix
  514. // formula is then equal to v / a in most (99.6%) cases. Note that this table
  515. // and constant are adjusted very tightly to fit 32b arithmetic.
  516. // In particular, they use the fact that the operands for 'v / a' are actually
  517. // derived as v = (a0.p0 + a1.p1 + a2.p2 + a3.p3) and a = a0 + a1 + a2 + a3
  518. // with ai in [0..255] and pi in [0..1<<kGammaFix). The constraint to avoid
  519. // overflow is: kGammaFix + kAlphaFix <= 31.
  520. static const uint32_t kInvAlpha[4 * 0xff + 1] = {
  521. 0, /* alpha = 0 */
  522. 524288, 262144, 174762, 131072, 104857, 87381, 74898, 65536,
  523. 58254, 52428, 47662, 43690, 40329, 37449, 34952, 32768,
  524. 30840, 29127, 27594, 26214, 24966, 23831, 22795, 21845,
  525. 20971, 20164, 19418, 18724, 18078, 17476, 16912, 16384,
  526. 15887, 15420, 14979, 14563, 14169, 13797, 13443, 13107,
  527. 12787, 12483, 12192, 11915, 11650, 11397, 11155, 10922,
  528. 10699, 10485, 10280, 10082, 9892, 9709, 9532, 9362,
  529. 9198, 9039, 8886, 8738, 8594, 8456, 8322, 8192,
  530. 8065, 7943, 7825, 7710, 7598, 7489, 7384, 7281,
  531. 7182, 7084, 6990, 6898, 6808, 6721, 6636, 6553,
  532. 6472, 6393, 6316, 6241, 6168, 6096, 6026, 5957,
  533. 5890, 5825, 5761, 5698, 5637, 5577, 5518, 5461,
  534. 5405, 5349, 5295, 5242, 5190, 5140, 5090, 5041,
  535. 4993, 4946, 4899, 4854, 4809, 4766, 4723, 4681,
  536. 4639, 4599, 4559, 4519, 4481, 4443, 4405, 4369,
  537. 4332, 4297, 4262, 4228, 4194, 4161, 4128, 4096,
  538. 4064, 4032, 4002, 3971, 3942, 3912, 3883, 3855,
  539. 3826, 3799, 3771, 3744, 3718, 3692, 3666, 3640,
  540. 3615, 3591, 3566, 3542, 3518, 3495, 3472, 3449,
  541. 3426, 3404, 3382, 3360, 3339, 3318, 3297, 3276,
  542. 3256, 3236, 3216, 3196, 3177, 3158, 3139, 3120,
  543. 3102, 3084, 3066, 3048, 3030, 3013, 2995, 2978,
  544. 2962, 2945, 2928, 2912, 2896, 2880, 2864, 2849,
  545. 2833, 2818, 2803, 2788, 2774, 2759, 2744, 2730,
  546. 2716, 2702, 2688, 2674, 2661, 2647, 2634, 2621,
  547. 2608, 2595, 2582, 2570, 2557, 2545, 2532, 2520,
  548. 2508, 2496, 2484, 2473, 2461, 2449, 2438, 2427,
  549. 2416, 2404, 2394, 2383, 2372, 2361, 2351, 2340,
  550. 2330, 2319, 2309, 2299, 2289, 2279, 2269, 2259,
  551. 2250, 2240, 2231, 2221, 2212, 2202, 2193, 2184,
  552. 2175, 2166, 2157, 2148, 2139, 2131, 2122, 2114,
  553. 2105, 2097, 2088, 2080, 2072, 2064, 2056, 2048,
  554. 2040, 2032, 2024, 2016, 2008, 2001, 1993, 1985,
  555. 1978, 1971, 1963, 1956, 1949, 1941, 1934, 1927,
  556. 1920, 1913, 1906, 1899, 1892, 1885, 1879, 1872,
  557. 1865, 1859, 1852, 1846, 1839, 1833, 1826, 1820,
  558. 1814, 1807, 1801, 1795, 1789, 1783, 1777, 1771,
  559. 1765, 1759, 1753, 1747, 1741, 1736, 1730, 1724,
  560. 1718, 1713, 1707, 1702, 1696, 1691, 1685, 1680,
  561. 1675, 1669, 1664, 1659, 1653, 1648, 1643, 1638,
  562. 1633, 1628, 1623, 1618, 1613, 1608, 1603, 1598,
  563. 1593, 1588, 1583, 1579, 1574, 1569, 1565, 1560,
  564. 1555, 1551, 1546, 1542, 1537, 1533, 1528, 1524,
  565. 1519, 1515, 1510, 1506, 1502, 1497, 1493, 1489,
  566. 1485, 1481, 1476, 1472, 1468, 1464, 1460, 1456,
  567. 1452, 1448, 1444, 1440, 1436, 1432, 1428, 1424,
  568. 1420, 1416, 1413, 1409, 1405, 1401, 1398, 1394,
  569. 1390, 1387, 1383, 1379, 1376, 1372, 1368, 1365,
  570. 1361, 1358, 1354, 1351, 1347, 1344, 1340, 1337,
  571. 1334, 1330, 1327, 1323, 1320, 1317, 1314, 1310,
  572. 1307, 1304, 1300, 1297, 1294, 1291, 1288, 1285,
  573. 1281, 1278, 1275, 1272, 1269, 1266, 1263, 1260,
  574. 1257, 1254, 1251, 1248, 1245, 1242, 1239, 1236,
  575. 1233, 1230, 1227, 1224, 1222, 1219, 1216, 1213,
  576. 1210, 1208, 1205, 1202, 1199, 1197, 1194, 1191,
  577. 1188, 1186, 1183, 1180, 1178, 1175, 1172, 1170,
  578. 1167, 1165, 1162, 1159, 1157, 1154, 1152, 1149,
  579. 1147, 1144, 1142, 1139, 1137, 1134, 1132, 1129,
  580. 1127, 1125, 1122, 1120, 1117, 1115, 1113, 1110,
  581. 1108, 1106, 1103, 1101, 1099, 1096, 1094, 1092,
  582. 1089, 1087, 1085, 1083, 1081, 1078, 1076, 1074,
  583. 1072, 1069, 1067, 1065, 1063, 1061, 1059, 1057,
  584. 1054, 1052, 1050, 1048, 1046, 1044, 1042, 1040,
  585. 1038, 1036, 1034, 1032, 1030, 1028, 1026, 1024,
  586. 1022, 1020, 1018, 1016, 1014, 1012, 1010, 1008,
  587. 1006, 1004, 1002, 1000, 998, 996, 994, 992,
  588. 991, 989, 987, 985, 983, 981, 979, 978,
  589. 976, 974, 972, 970, 969, 967, 965, 963,
  590. 961, 960, 958, 956, 954, 953, 951, 949,
  591. 948, 946, 944, 942, 941, 939, 937, 936,
  592. 934, 932, 931, 929, 927, 926, 924, 923,
  593. 921, 919, 918, 916, 914, 913, 911, 910,
  594. 908, 907, 905, 903, 902, 900, 899, 897,
  595. 896, 894, 893, 891, 890, 888, 887, 885,
  596. 884, 882, 881, 879, 878, 876, 875, 873,
  597. 872, 870, 869, 868, 866, 865, 863, 862,
  598. 860, 859, 858, 856, 855, 853, 852, 851,
  599. 849, 848, 846, 845, 844, 842, 841, 840,
  600. 838, 837, 836, 834, 833, 832, 830, 829,
  601. 828, 826, 825, 824, 823, 821, 820, 819,
  602. 817, 816, 815, 814, 812, 811, 810, 809,
  603. 807, 806, 805, 804, 802, 801, 800, 799,
  604. 798, 796, 795, 794, 793, 791, 790, 789,
  605. 788, 787, 786, 784, 783, 782, 781, 780,
  606. 779, 777, 776, 775, 774, 773, 772, 771,
  607. 769, 768, 767, 766, 765, 764, 763, 762,
  608. 760, 759, 758, 757, 756, 755, 754, 753,
  609. 752, 751, 750, 748, 747, 746, 745, 744,
  610. 743, 742, 741, 740, 739, 738, 737, 736,
  611. 735, 734, 733, 732, 731, 730, 729, 728,
  612. 727, 726, 725, 724, 723, 722, 721, 720,
  613. 719, 718, 717, 716, 715, 714, 713, 712,
  614. 711, 710, 709, 708, 707, 706, 705, 704,
  615. 703, 702, 701, 700, 699, 699, 698, 697,
  616. 696, 695, 694, 693, 692, 691, 690, 689,
  617. 688, 688, 687, 686, 685, 684, 683, 682,
  618. 681, 680, 680, 679, 678, 677, 676, 675,
  619. 674, 673, 673, 672, 671, 670, 669, 668,
  620. 667, 667, 666, 665, 664, 663, 662, 661,
  621. 661, 660, 659, 658, 657, 657, 656, 655,
  622. 654, 653, 652, 652, 651, 650, 649, 648,
  623. 648, 647, 646, 645, 644, 644, 643, 642,
  624. 641, 640, 640, 639, 638, 637, 637, 636,
  625. 635, 634, 633, 633, 632, 631, 630, 630,
  626. 629, 628, 627, 627, 626, 625, 624, 624,
  627. 623, 622, 621, 621, 620, 619, 618, 618,
  628. 617, 616, 616, 615, 614, 613, 613, 612,
  629. 611, 611, 610, 609, 608, 608, 607, 606,
  630. 606, 605, 604, 604, 603, 602, 601, 601,
  631. 600, 599, 599, 598, 597, 597, 596, 595,
  632. 595, 594, 593, 593, 592, 591, 591, 590,
  633. 589, 589, 588, 587, 587, 586, 585, 585,
  634. 584, 583, 583, 582, 581, 581, 580, 579,
  635. 579, 578, 578, 577, 576, 576, 575, 574,
  636. 574, 573, 572, 572, 571, 571, 570, 569,
  637. 569, 568, 568, 567, 566, 566, 565, 564,
  638. 564, 563, 563, 562, 561, 561, 560, 560,
  639. 559, 558, 558, 557, 557, 556, 555, 555,
  640. 554, 554, 553, 553, 552, 551, 551, 550,
  641. 550, 549, 548, 548, 547, 547, 546, 546,
  642. 545, 544, 544, 543, 543, 542, 542, 541,
  643. 541, 540, 539, 539, 538, 538, 537, 537,
  644. 536, 536, 535, 534, 534, 533, 533, 532,
  645. 532, 531, 531, 530, 530, 529, 529, 528,
  646. 527, 527, 526, 526, 525, 525, 524, 524,
  647. 523, 523, 522, 522, 521, 521, 520, 520,
  648. 519, 519, 518, 518, 517, 517, 516, 516,
  649. 515, 515, 514, 514
  650. };
  651. // Note that LinearToGamma() expects the values to be premultiplied by 4,
  652. // so we incorporate this factor 4 inside the DIVIDE_BY_ALPHA macro directly.
  653. #define DIVIDE_BY_ALPHA(sum, a) (((sum) * kInvAlpha[(a)]) >> (kAlphaFix - 2))
  654. #else
  655. #define DIVIDE_BY_ALPHA(sum, a) (4 * (sum) / (a))
  656. #endif // USE_INVERSE_ALPHA_TABLE
  657. static WEBP_INLINE int LinearToGammaWeighted(const uint8_t* src,
  658. const uint8_t* a_ptr,
  659. uint32_t total_a, int step,
  660. int rgb_stride) {
  661. const uint32_t sum =
  662. a_ptr[0] * GammaToLinear(src[0]) +
  663. a_ptr[step] * GammaToLinear(src[step]) +
  664. a_ptr[rgb_stride] * GammaToLinear(src[rgb_stride]) +
  665. a_ptr[rgb_stride + step] * GammaToLinear(src[rgb_stride + step]);
  666. assert(total_a > 0 && total_a <= 4 * 0xff);
  667. #if defined(USE_INVERSE_ALPHA_TABLE)
  668. assert((uint64_t)sum * kInvAlpha[total_a] < ((uint64_t)1 << 32));
  669. #endif
  670. return LinearToGamma(DIVIDE_BY_ALPHA(sum, total_a), 0);
  671. }
  672. static WEBP_INLINE void ConvertRowToY(const uint8_t* const r_ptr,
  673. const uint8_t* const g_ptr,
  674. const uint8_t* const b_ptr,
  675. int step,
  676. uint8_t* const dst_y,
  677. int width,
  678. VP8Random* const rg) {
  679. int i, j;
  680. for (i = 0, j = 0; i < width; ++i, j += step) {
  681. dst_y[i] = RGBToY(r_ptr[j], g_ptr[j], b_ptr[j], rg);
  682. }
  683. }
  684. static WEBP_INLINE void ConvertRowsToUVWithAlpha(const uint8_t* const r_ptr,
  685. const uint8_t* const g_ptr,
  686. const uint8_t* const b_ptr,
  687. const uint8_t* const a_ptr,
  688. int rgb_stride,
  689. uint8_t* const dst_u,
  690. uint8_t* const dst_v,
  691. int width,
  692. VP8Random* const rg) {
  693. int i, j;
  694. // we loop over 2x2 blocks and produce one U/V value for each.
  695. for (i = 0, j = 0; i < (width >> 1); ++i, j += 2 * sizeof(uint32_t)) {
  696. const uint32_t a = SUM4ALPHA(a_ptr + j);
  697. int r, g, b;
  698. if (a == 4 * 0xff || a == 0) {
  699. r = SUM4(r_ptr + j, 4);
  700. g = SUM4(g_ptr + j, 4);
  701. b = SUM4(b_ptr + j, 4);
  702. } else {
  703. r = LinearToGammaWeighted(r_ptr + j, a_ptr + j, a, 4, rgb_stride);
  704. g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 4, rgb_stride);
  705. b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 4, rgb_stride);
  706. }
  707. dst_u[i] = RGBToU(r, g, b, rg);
  708. dst_v[i] = RGBToV(r, g, b, rg);
  709. }
  710. if (width & 1) {
  711. const uint32_t a = 2u * SUM2ALPHA(a_ptr + j);
  712. int r, g, b;
  713. if (a == 4 * 0xff || a == 0) {
  714. r = SUM2(r_ptr + j);
  715. g = SUM2(g_ptr + j);
  716. b = SUM2(b_ptr + j);
  717. } else {
  718. r = LinearToGammaWeighted(r_ptr + j, a_ptr + j, a, 0, rgb_stride);
  719. g = LinearToGammaWeighted(g_ptr + j, a_ptr + j, a, 0, rgb_stride);
  720. b = LinearToGammaWeighted(b_ptr + j, a_ptr + j, a, 0, rgb_stride);
  721. }
  722. dst_u[i] = RGBToU(r, g, b, rg);
  723. dst_v[i] = RGBToV(r, g, b, rg);
  724. }
  725. }
  726. static WEBP_INLINE void ConvertRowsToUV(const uint8_t* const r_ptr,
  727. const uint8_t* const g_ptr,
  728. const uint8_t* const b_ptr,
  729. int step, int rgb_stride,
  730. uint8_t* const dst_u,
  731. uint8_t* const dst_v,
  732. int width,
  733. VP8Random* const rg) {
  734. int i, j;
  735. for (i = 0, j = 0; i < (width >> 1); ++i, j += 2 * step) {
  736. const int r = SUM4(r_ptr + j, step);
  737. const int g = SUM4(g_ptr + j, step);
  738. const int b = SUM4(b_ptr + j, step);
  739. dst_u[i] = RGBToU(r, g, b, rg);
  740. dst_v[i] = RGBToV(r, g, b, rg);
  741. }
  742. if (width & 1) {
  743. const int r = SUM2(r_ptr + j);
  744. const int g = SUM2(g_ptr + j);
  745. const int b = SUM2(b_ptr + j);
  746. dst_u[i] = RGBToU(r, g, b, rg);
  747. dst_v[i] = RGBToV(r, g, b, rg);
  748. }
  749. }
  750. static int ImportYUVAFromRGBA(const uint8_t* const r_ptr,
  751. const uint8_t* const g_ptr,
  752. const uint8_t* const b_ptr,
  753. const uint8_t* const a_ptr,
  754. int step, // bytes per pixel
  755. int rgb_stride, // bytes per scanline
  756. float dithering,
  757. int use_iterative_conversion,
  758. WebPPicture* const picture) {
  759. int y;
  760. const int width = picture->width;
  761. const int height = picture->height;
  762. const int has_alpha = CheckNonOpaque(a_ptr, width, height, step, rgb_stride);
  763. picture->colorspace = has_alpha ? WEBP_YUV420A : WEBP_YUV420;
  764. picture->use_argb = 0;
  765. // disable smart conversion if source is too small (overkill).
  766. if (width < kMinDimensionIterativeConversion ||
  767. height < kMinDimensionIterativeConversion) {
  768. use_iterative_conversion = 0;
  769. }
  770. if (!WebPPictureAllocYUVA(picture, width, height)) {
  771. return 0;
  772. }
  773. if (has_alpha) {
  774. WebPInitAlphaProcessing();
  775. assert(step == 4);
  776. #if defined(USE_INVERSE_ALPHA_TABLE)
  777. assert(kAlphaFix + kGammaFix <= 31);
  778. #endif
  779. }
  780. if (use_iterative_conversion) {
  781. InitGammaTablesF();
  782. if (!PreprocessARGB(r_ptr, g_ptr, b_ptr, step, rgb_stride, picture)) {
  783. return 0;
  784. }
  785. if (has_alpha) {
  786. WebPExtractAlpha(a_ptr, rgb_stride, width, height,
  787. picture->a, picture->a_stride);
  788. }
  789. } else {
  790. uint8_t* dst_y = picture->y;
  791. uint8_t* dst_u = picture->u;
  792. uint8_t* dst_v = picture->v;
  793. uint8_t* dst_a = picture->a;
  794. VP8Random base_rg;
  795. VP8Random* rg = NULL;
  796. if (dithering > 0.) {
  797. VP8InitRandom(&base_rg, dithering);
  798. rg = &base_rg;
  799. }
  800. InitGammaTables();
  801. // Downsample Y/U/V planes, two rows at a time
  802. for (y = 0; y < (height >> 1); ++y) {
  803. int rows_have_alpha = has_alpha;
  804. const int off1 = (2 * y + 0) * rgb_stride;
  805. const int off2 = (2 * y + 1) * rgb_stride;
  806. ConvertRowToY(r_ptr + off1, g_ptr + off1, b_ptr + off1, step,
  807. dst_y, width, rg);
  808. ConvertRowToY(r_ptr + off2, g_ptr + off2, b_ptr + off2, step,
  809. dst_y + picture->y_stride, width, rg);
  810. dst_y += 2 * picture->y_stride;
  811. if (has_alpha) {
  812. rows_have_alpha &= !WebPExtractAlpha(a_ptr + off1, rgb_stride,
  813. width, 2,
  814. dst_a, picture->a_stride);
  815. dst_a += 2 * picture->a_stride;
  816. }
  817. if (!rows_have_alpha) {
  818. ConvertRowsToUV(r_ptr + off1, g_ptr + off1, b_ptr + off1,
  819. step, rgb_stride, dst_u, dst_v, width, rg);
  820. } else {
  821. ConvertRowsToUVWithAlpha(r_ptr + off1, g_ptr + off1, b_ptr + off1,
  822. a_ptr + off1, rgb_stride,
  823. dst_u, dst_v, width, rg);
  824. }
  825. dst_u += picture->uv_stride;
  826. dst_v += picture->uv_stride;
  827. }
  828. if (height & 1) { // extra last row
  829. const int off = 2 * y * rgb_stride;
  830. int row_has_alpha = has_alpha;
  831. ConvertRowToY(r_ptr + off, g_ptr + off, b_ptr + off, step,
  832. dst_y, width, rg);
  833. if (row_has_alpha) {
  834. row_has_alpha &= !WebPExtractAlpha(a_ptr + off, 0, width, 1, dst_a, 0);
  835. }
  836. if (!row_has_alpha) {
  837. ConvertRowsToUV(r_ptr + off, g_ptr + off, b_ptr + off,
  838. step, 0, dst_u, dst_v, width, rg);
  839. } else {
  840. ConvertRowsToUVWithAlpha(r_ptr + off, g_ptr + off, b_ptr + off,
  841. a_ptr + off, 0,
  842. dst_u, dst_v, width, rg);
  843. }
  844. }
  845. }
  846. return 1;
  847. }
  848. #undef SUM4
  849. #undef SUM2
  850. #undef SUM4ALPHA
  851. #undef SUM2ALPHA
  852. //------------------------------------------------------------------------------
  853. // call for ARGB->YUVA conversion
  854. static int PictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace,
  855. float dithering, int use_iterative_conversion) {
  856. if (picture == NULL) return 0;
  857. if (picture->argb == NULL) {
  858. return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
  859. } else if ((colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {
  860. return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
  861. } else {
  862. const uint8_t* const argb = (const uint8_t*)picture->argb;
  863. const uint8_t* const r = ALPHA_IS_LAST ? argb + 2 : argb + 1;
  864. const uint8_t* const g = ALPHA_IS_LAST ? argb + 1 : argb + 2;
  865. const uint8_t* const b = ALPHA_IS_LAST ? argb + 0 : argb + 3;
  866. const uint8_t* const a = ALPHA_IS_LAST ? argb + 3 : argb + 0;
  867. picture->colorspace = WEBP_YUV420;
  868. return ImportYUVAFromRGBA(r, g, b, a, 4, 4 * picture->argb_stride,
  869. dithering, use_iterative_conversion, picture);
  870. }
  871. }
  872. int WebPPictureARGBToYUVADithered(WebPPicture* picture, WebPEncCSP colorspace,
  873. float dithering) {
  874. return PictureARGBToYUVA(picture, colorspace, dithering, 0);
  875. }
  876. int WebPPictureARGBToYUVA(WebPPicture* picture, WebPEncCSP colorspace) {
  877. return PictureARGBToYUVA(picture, colorspace, 0.f, 0);
  878. }
  879. #if WEBP_ENCODER_ABI_VERSION > 0x0204
  880. int WebPPictureSmartARGBToYUVA(WebPPicture* picture) {
  881. return PictureARGBToYUVA(picture, WEBP_YUV420, 0.f, 1);
  882. }
  883. #endif
  884. //------------------------------------------------------------------------------
  885. // call for YUVA -> ARGB conversion
  886. int WebPPictureYUVAToARGB(WebPPicture* picture) {
  887. if (picture == NULL) return 0;
  888. if (picture->y == NULL || picture->u == NULL || picture->v == NULL) {
  889. return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
  890. }
  891. if ((picture->colorspace & WEBP_CSP_ALPHA_BIT) && picture->a == NULL) {
  892. return WebPEncodingSetError(picture, VP8_ENC_ERROR_NULL_PARAMETER);
  893. }
  894. if ((picture->colorspace & WEBP_CSP_UV_MASK) != WEBP_YUV420) {
  895. return WebPEncodingSetError(picture, VP8_ENC_ERROR_INVALID_CONFIGURATION);
  896. }
  897. // Allocate a new argb buffer (discarding the previous one).
  898. if (!WebPPictureAllocARGB(picture, picture->width, picture->height)) return 0;
  899. picture->use_argb = 1;
  900. // Convert
  901. {
  902. int y;
  903. const int width = picture->width;
  904. const int height = picture->height;
  905. const int argb_stride = 4 * picture->argb_stride;
  906. uint8_t* dst = (uint8_t*)picture->argb;
  907. const uint8_t *cur_u = picture->u, *cur_v = picture->v, *cur_y = picture->y;
  908. WebPUpsampleLinePairFunc upsample = WebPGetLinePairConverter(ALPHA_IS_LAST);
  909. // First row, with replicated top samples.
  910. upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);
  911. cur_y += picture->y_stride;
  912. dst += argb_stride;
  913. // Center rows.
  914. for (y = 1; y + 1 < height; y += 2) {
  915. const uint8_t* const top_u = cur_u;
  916. const uint8_t* const top_v = cur_v;
  917. cur_u += picture->uv_stride;
  918. cur_v += picture->uv_stride;
  919. upsample(cur_y, cur_y + picture->y_stride, top_u, top_v, cur_u, cur_v,
  920. dst, dst + argb_stride, width);
  921. cur_y += 2 * picture->y_stride;
  922. dst += 2 * argb_stride;
  923. }
  924. // Last row (if needed), with replicated bottom samples.
  925. if (height > 1 && !(height & 1)) {
  926. upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, width);
  927. }
  928. // Insert alpha values if needed, in replacement for the default 0xff ones.
  929. if (picture->colorspace & WEBP_CSP_ALPHA_BIT) {
  930. for (y = 0; y < height; ++y) {
  931. uint32_t* const argb_dst = picture->argb + y * picture->argb_stride;
  932. const uint8_t* const src = picture->a + y * picture->a_stride;
  933. int x;
  934. for (x = 0; x < width; ++x) {
  935. argb_dst[x] = (argb_dst[x] & 0x00ffffffu) | ((uint32_t)src[x] << 24);
  936. }
  937. }
  938. }
  939. }
  940. return 1;
  941. }
  942. //------------------------------------------------------------------------------
  943. // automatic import / conversion
  944. static int Import(WebPPicture* const picture,
  945. const uint8_t* const rgb, int rgb_stride,
  946. int step, int swap_rb, int import_alpha) {
  947. int y;
  948. const uint8_t* const r_ptr = rgb + (swap_rb ? 2 : 0);
  949. const uint8_t* const g_ptr = rgb + 1;
  950. const uint8_t* const b_ptr = rgb + (swap_rb ? 0 : 2);
  951. const uint8_t* const a_ptr = import_alpha ? rgb + 3 : NULL;
  952. const int width = picture->width;
  953. const int height = picture->height;
  954. if (!picture->use_argb) {
  955. return ImportYUVAFromRGBA(r_ptr, g_ptr, b_ptr, a_ptr, step, rgb_stride,
  956. 0.f /* no dithering */, 0, picture);
  957. }
  958. if (!WebPPictureAlloc(picture)) return 0;
  959. assert(step >= (import_alpha ? 4 : 3));
  960. for (y = 0; y < height; ++y) {
  961. uint32_t* const dst = &picture->argb[y * picture->argb_stride];
  962. int x;
  963. for (x = 0; x < width; ++x) {
  964. const int offset = step * x + y * rgb_stride;
  965. dst[x] = MakeARGB32(import_alpha ? a_ptr[offset] : 0xff,
  966. r_ptr[offset], g_ptr[offset], b_ptr[offset]);
  967. }
  968. }
  969. return 1;
  970. }
  971. // Public API
  972. int WebPPictureImportRGB(WebPPicture* picture,
  973. const uint8_t* rgb, int rgb_stride) {
  974. return (picture != NULL) ? Import(picture, rgb, rgb_stride, 3, 0, 0) : 0;
  975. }
  976. int WebPPictureImportBGR(WebPPicture* picture,
  977. const uint8_t* rgb, int rgb_stride) {
  978. return (picture != NULL) ? Import(picture, rgb, rgb_stride, 3, 1, 0) : 0;
  979. }
  980. int WebPPictureImportRGBA(WebPPicture* picture,
  981. const uint8_t* rgba, int rgba_stride) {
  982. return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 0, 1) : 0;
  983. }
  984. int WebPPictureImportBGRA(WebPPicture* picture,
  985. const uint8_t* rgba, int rgba_stride) {
  986. return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 1, 1) : 0;
  987. }
  988. int WebPPictureImportRGBX(WebPPicture* picture,
  989. const uint8_t* rgba, int rgba_stride) {
  990. return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 0, 0) : 0;
  991. }
  992. int WebPPictureImportBGRX(WebPPicture* picture,
  993. const uint8_t* rgba, int rgba_stride) {
  994. return (picture != NULL) ? Import(picture, rgba, rgba_stride, 4, 1, 0) : 0;
  995. }
  996. //------------------------------------------------------------------------------