picture_csp_enc.c 42 KB

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