picture_csp_enc.c 43 KB

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