io_dec.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. // Copyright 2011 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. // functions for sample output.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <assert.h>
  14. #include <stdlib.h>
  15. #include "./vp8i_dec.h"
  16. #include "./webpi_dec.h"
  17. #include "../dsp/dsp.h"
  18. #include "../dsp/yuv.h"
  19. #include "../utils/utils.h"
  20. //------------------------------------------------------------------------------
  21. // Main YUV<->RGB conversion functions
  22. static int EmitYUV(const VP8Io* const io, WebPDecParams* const p) {
  23. WebPDecBuffer* output = p->output;
  24. const WebPYUVABuffer* const buf = &output->u.YUVA;
  25. uint8_t* const y_dst = buf->y + (size_t)io->mb_y * buf->y_stride;
  26. uint8_t* const u_dst = buf->u + (size_t)(io->mb_y >> 1) * buf->u_stride;
  27. uint8_t* const v_dst = buf->v + (size_t)(io->mb_y >> 1) * buf->v_stride;
  28. const int mb_w = io->mb_w;
  29. const int mb_h = io->mb_h;
  30. const int uv_w = (mb_w + 1) / 2;
  31. const int uv_h = (mb_h + 1) / 2;
  32. WebPCopyPlane(io->y, io->y_stride, y_dst, buf->y_stride, mb_w, mb_h);
  33. WebPCopyPlane(io->u, io->uv_stride, u_dst, buf->u_stride, uv_w, uv_h);
  34. WebPCopyPlane(io->v, io->uv_stride, v_dst, buf->v_stride, uv_w, uv_h);
  35. return io->mb_h;
  36. }
  37. // Point-sampling U/V sampler.
  38. static int EmitSampledRGB(const VP8Io* const io, WebPDecParams* const p) {
  39. WebPDecBuffer* const output = p->output;
  40. WebPRGBABuffer* const buf = &output->u.RGBA;
  41. uint8_t* const dst = buf->rgba + (size_t)io->mb_y * buf->stride;
  42. WebPSamplerProcessPlane(io->y, io->y_stride,
  43. io->u, io->v, io->uv_stride,
  44. dst, buf->stride, io->mb_w, io->mb_h,
  45. WebPSamplers[output->colorspace]);
  46. return io->mb_h;
  47. }
  48. //------------------------------------------------------------------------------
  49. // Fancy upsampling
  50. #ifdef FANCY_UPSAMPLING
  51. static int EmitFancyRGB(const VP8Io* const io, WebPDecParams* const p) {
  52. int num_lines_out = io->mb_h; // a priori guess
  53. const WebPRGBABuffer* const buf = &p->output->u.RGBA;
  54. uint8_t* dst = buf->rgba + (size_t)io->mb_y * buf->stride;
  55. WebPUpsampleLinePairFunc upsample = WebPUpsamplers[p->output->colorspace];
  56. const uint8_t* cur_y = io->y;
  57. const uint8_t* cur_u = io->u;
  58. const uint8_t* cur_v = io->v;
  59. const uint8_t* top_u = p->tmp_u;
  60. const uint8_t* top_v = p->tmp_v;
  61. int y = io->mb_y;
  62. const int y_end = io->mb_y + io->mb_h;
  63. const int mb_w = io->mb_w;
  64. const int uv_w = (mb_w + 1) / 2;
  65. if (y == 0) {
  66. // First line is special cased. We mirror the u/v samples at boundary.
  67. upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v, dst, NULL, mb_w);
  68. } else {
  69. // We can finish the left-over line from previous call.
  70. upsample(p->tmp_y, cur_y, top_u, top_v, cur_u, cur_v,
  71. dst - buf->stride, dst, mb_w);
  72. ++num_lines_out;
  73. }
  74. // Loop over each output pairs of row.
  75. for (; y + 2 < y_end; y += 2) {
  76. top_u = cur_u;
  77. top_v = cur_v;
  78. cur_u += io->uv_stride;
  79. cur_v += io->uv_stride;
  80. dst += 2 * buf->stride;
  81. cur_y += 2 * io->y_stride;
  82. upsample(cur_y - io->y_stride, cur_y,
  83. top_u, top_v, cur_u, cur_v,
  84. dst - buf->stride, dst, mb_w);
  85. }
  86. // move to last row
  87. cur_y += io->y_stride;
  88. if (io->crop_top + y_end < io->crop_bottom) {
  89. // Save the unfinished samples for next call (as we're not done yet).
  90. memcpy(p->tmp_y, cur_y, mb_w * sizeof(*p->tmp_y));
  91. memcpy(p->tmp_u, cur_u, uv_w * sizeof(*p->tmp_u));
  92. memcpy(p->tmp_v, cur_v, uv_w * sizeof(*p->tmp_v));
  93. // The fancy upsampler leaves a row unfinished behind
  94. // (except for the very last row)
  95. num_lines_out--;
  96. } else {
  97. // Process the very last row of even-sized picture
  98. if (!(y_end & 1)) {
  99. upsample(cur_y, NULL, cur_u, cur_v, cur_u, cur_v,
  100. dst + buf->stride, NULL, mb_w);
  101. }
  102. }
  103. return num_lines_out;
  104. }
  105. #endif /* FANCY_UPSAMPLING */
  106. //------------------------------------------------------------------------------
  107. static void FillAlphaPlane(uint8_t* dst, int w, int h, int stride) {
  108. int j;
  109. for (j = 0; j < h; ++j) {
  110. memset(dst, 0xff, w * sizeof(*dst));
  111. dst += stride;
  112. }
  113. }
  114. static int EmitAlphaYUV(const VP8Io* const io, WebPDecParams* const p,
  115. int expected_num_lines_out) {
  116. const uint8_t* alpha = io->a;
  117. const WebPYUVABuffer* const buf = &p->output->u.YUVA;
  118. const int mb_w = io->mb_w;
  119. const int mb_h = io->mb_h;
  120. uint8_t* dst = buf->a + (size_t)io->mb_y * buf->a_stride;
  121. int j;
  122. (void)expected_num_lines_out;
  123. assert(expected_num_lines_out == mb_h);
  124. if (alpha != NULL) {
  125. for (j = 0; j < mb_h; ++j) {
  126. memcpy(dst, alpha, mb_w * sizeof(*dst));
  127. alpha += io->width;
  128. dst += buf->a_stride;
  129. }
  130. } else if (buf->a != NULL) {
  131. // the user requested alpha, but there is none, set it to opaque.
  132. FillAlphaPlane(dst, mb_w, mb_h, buf->a_stride);
  133. }
  134. return 0;
  135. }
  136. static int GetAlphaSourceRow(const VP8Io* const io,
  137. const uint8_t** alpha, int* const num_rows) {
  138. int start_y = io->mb_y;
  139. *num_rows = io->mb_h;
  140. // Compensate for the 1-line delay of the fancy upscaler.
  141. // This is similar to EmitFancyRGB().
  142. if (io->fancy_upsampling) {
  143. if (start_y == 0) {
  144. // We don't process the last row yet. It'll be done during the next call.
  145. --*num_rows;
  146. } else {
  147. --start_y;
  148. // Fortunately, *alpha data is persistent, so we can go back
  149. // one row and finish alpha blending, now that the fancy upscaler
  150. // completed the YUV->RGB interpolation.
  151. *alpha -= io->width;
  152. }
  153. if (io->crop_top + io->mb_y + io->mb_h == io->crop_bottom) {
  154. // If it's the very last call, we process all the remaining rows!
  155. *num_rows = io->crop_bottom - io->crop_top - start_y;
  156. }
  157. }
  158. return start_y;
  159. }
  160. static int EmitAlphaRGB(const VP8Io* const io, WebPDecParams* const p,
  161. int expected_num_lines_out) {
  162. const uint8_t* alpha = io->a;
  163. if (alpha != NULL) {
  164. const int mb_w = io->mb_w;
  165. const WEBP_CSP_MODE colorspace = p->output->colorspace;
  166. const int alpha_first =
  167. (colorspace == MODE_ARGB || colorspace == MODE_Argb);
  168. const WebPRGBABuffer* const buf = &p->output->u.RGBA;
  169. int num_rows;
  170. const size_t start_y = GetAlphaSourceRow(io, &alpha, &num_rows);
  171. uint8_t* const base_rgba = buf->rgba + start_y * buf->stride;
  172. uint8_t* const dst = base_rgba + (alpha_first ? 0 : 3);
  173. const int has_alpha = WebPDispatchAlpha(alpha, io->width, mb_w,
  174. num_rows, dst, buf->stride);
  175. (void)expected_num_lines_out;
  176. assert(expected_num_lines_out == num_rows);
  177. // has_alpha is true if there's non-trivial alpha to premultiply with.
  178. if (has_alpha && WebPIsPremultipliedMode(colorspace)) {
  179. WebPApplyAlphaMultiply(base_rgba, alpha_first,
  180. mb_w, num_rows, buf->stride);
  181. }
  182. }
  183. return 0;
  184. }
  185. static int EmitAlphaRGBA4444(const VP8Io* const io, WebPDecParams* const p,
  186. int expected_num_lines_out) {
  187. const uint8_t* alpha = io->a;
  188. if (alpha != NULL) {
  189. const int mb_w = io->mb_w;
  190. const WEBP_CSP_MODE colorspace = p->output->colorspace;
  191. const WebPRGBABuffer* const buf = &p->output->u.RGBA;
  192. int num_rows;
  193. const size_t start_y = GetAlphaSourceRow(io, &alpha, &num_rows);
  194. uint8_t* const base_rgba = buf->rgba + start_y * buf->stride;
  195. #if (WEBP_SWAP_16BIT_CSP == 1)
  196. uint8_t* alpha_dst = base_rgba;
  197. #else
  198. uint8_t* alpha_dst = base_rgba + 1;
  199. #endif
  200. uint32_t alpha_mask = 0x0f;
  201. int i, j;
  202. for (j = 0; j < num_rows; ++j) {
  203. for (i = 0; i < mb_w; ++i) {
  204. // Fill in the alpha value (converted to 4 bits).
  205. const uint32_t alpha_value = alpha[i] >> 4;
  206. alpha_dst[2 * i] = (alpha_dst[2 * i] & 0xf0) | alpha_value;
  207. alpha_mask &= alpha_value;
  208. }
  209. alpha += io->width;
  210. alpha_dst += buf->stride;
  211. }
  212. (void)expected_num_lines_out;
  213. assert(expected_num_lines_out == num_rows);
  214. if (alpha_mask != 0x0f && WebPIsPremultipliedMode(colorspace)) {
  215. WebPApplyAlphaMultiply4444(base_rgba, mb_w, num_rows, buf->stride);
  216. }
  217. }
  218. return 0;
  219. }
  220. //------------------------------------------------------------------------------
  221. // YUV rescaling (no final RGB conversion needed)
  222. #if !defined(WEBP_REDUCE_SIZE)
  223. static int Rescale(const uint8_t* src, int src_stride,
  224. int new_lines, WebPRescaler* const wrk) {
  225. int num_lines_out = 0;
  226. while (new_lines > 0) { // import new contributions of source rows.
  227. const int lines_in = WebPRescalerImport(wrk, new_lines, src, src_stride);
  228. src += lines_in * src_stride;
  229. new_lines -= lines_in;
  230. num_lines_out += WebPRescalerExport(wrk); // emit output row(s)
  231. }
  232. return num_lines_out;
  233. }
  234. static int EmitRescaledYUV(const VP8Io* const io, WebPDecParams* const p) {
  235. const int mb_h = io->mb_h;
  236. const int uv_mb_h = (mb_h + 1) >> 1;
  237. WebPRescaler* const scaler = p->scaler_y;
  238. int num_lines_out = 0;
  239. if (WebPIsAlphaMode(p->output->colorspace) && io->a != NULL) {
  240. // Before rescaling, we premultiply the luma directly into the io->y
  241. // internal buffer. This is OK since these samples are not used for
  242. // intra-prediction (the top samples are saved in cache_y_/u_/v_).
  243. // But we need to cast the const away, though.
  244. WebPMultRows((uint8_t*)io->y, io->y_stride,
  245. io->a, io->width, io->mb_w, mb_h, 0);
  246. }
  247. num_lines_out = Rescale(io->y, io->y_stride, mb_h, scaler);
  248. Rescale(io->u, io->uv_stride, uv_mb_h, p->scaler_u);
  249. Rescale(io->v, io->uv_stride, uv_mb_h, p->scaler_v);
  250. return num_lines_out;
  251. }
  252. static int EmitRescaledAlphaYUV(const VP8Io* const io, WebPDecParams* const p,
  253. int expected_num_lines_out) {
  254. const WebPYUVABuffer* const buf = &p->output->u.YUVA;
  255. uint8_t* const dst_a = buf->a + (size_t)p->last_y * buf->a_stride;
  256. if (io->a != NULL) {
  257. uint8_t* const dst_y = buf->y + (size_t)p->last_y * buf->y_stride;
  258. const int num_lines_out = Rescale(io->a, io->width, io->mb_h, p->scaler_a);
  259. assert(expected_num_lines_out == num_lines_out);
  260. if (num_lines_out > 0) { // unmultiply the Y
  261. WebPMultRows(dst_y, buf->y_stride, dst_a, buf->a_stride,
  262. p->scaler_a->dst_width, num_lines_out, 1);
  263. }
  264. } else if (buf->a != NULL) {
  265. // the user requested alpha, but there is none, set it to opaque.
  266. assert(p->last_y + expected_num_lines_out <= io->scaled_height);
  267. FillAlphaPlane(dst_a, io->scaled_width, expected_num_lines_out,
  268. buf->a_stride);
  269. }
  270. return 0;
  271. }
  272. static int InitYUVRescaler(const VP8Io* const io, WebPDecParams* const p) {
  273. const int has_alpha = WebPIsAlphaMode(p->output->colorspace);
  274. const WebPYUVABuffer* const buf = &p->output->u.YUVA;
  275. const int out_width = io->scaled_width;
  276. const int out_height = io->scaled_height;
  277. const int uv_out_width = (out_width + 1) >> 1;
  278. const int uv_out_height = (out_height + 1) >> 1;
  279. const int uv_in_width = (io->mb_w + 1) >> 1;
  280. const int uv_in_height = (io->mb_h + 1) >> 1;
  281. // scratch memory for luma rescaler
  282. const size_t work_size = 2 * (size_t)out_width;
  283. const size_t uv_work_size = 2 * uv_out_width; // and for each u/v ones
  284. uint64_t total_size;
  285. size_t rescaler_size;
  286. rescaler_t* work;
  287. WebPRescaler* scalers;
  288. const int num_rescalers = has_alpha ? 4 : 3;
  289. total_size = ((uint64_t)work_size + 2 * uv_work_size) * sizeof(*work);
  290. if (has_alpha) {
  291. total_size += (uint64_t)work_size * sizeof(*work);
  292. }
  293. rescaler_size = num_rescalers * sizeof(*p->scaler_y) + WEBP_ALIGN_CST;
  294. total_size += rescaler_size;
  295. if (!CheckSizeOverflow(total_size)) {
  296. return 0;
  297. }
  298. p->memory = WebPSafeMalloc(1ULL, (size_t)total_size);
  299. if (p->memory == NULL) {
  300. return 0; // memory error
  301. }
  302. work = (rescaler_t*)p->memory;
  303. scalers = (WebPRescaler*)WEBP_ALIGN(
  304. (const uint8_t*)work + total_size - rescaler_size);
  305. p->scaler_y = &scalers[0];
  306. p->scaler_u = &scalers[1];
  307. p->scaler_v = &scalers[2];
  308. p->scaler_a = has_alpha ? &scalers[3] : NULL;
  309. if (!WebPRescalerInit(p->scaler_y, io->mb_w, io->mb_h,
  310. buf->y, out_width, out_height, buf->y_stride, 1,
  311. work) ||
  312. !WebPRescalerInit(p->scaler_u, uv_in_width, uv_in_height,
  313. buf->u, uv_out_width, uv_out_height, buf->u_stride, 1,
  314. work + work_size) ||
  315. !WebPRescalerInit(p->scaler_v, uv_in_width, uv_in_height,
  316. buf->v, uv_out_width, uv_out_height, buf->v_stride, 1,
  317. work + work_size + uv_work_size)) {
  318. return 0;
  319. }
  320. p->emit = EmitRescaledYUV;
  321. if (has_alpha) {
  322. if (!WebPRescalerInit(p->scaler_a, io->mb_w, io->mb_h,
  323. buf->a, out_width, out_height, buf->a_stride, 1,
  324. work + work_size + 2 * uv_work_size)) {
  325. return 0;
  326. }
  327. p->emit_alpha = EmitRescaledAlphaYUV;
  328. WebPInitAlphaProcessing();
  329. }
  330. return 1;
  331. }
  332. //------------------------------------------------------------------------------
  333. // RGBA rescaling
  334. static int ExportRGB(WebPDecParams* const p, int y_pos) {
  335. const WebPYUV444Converter convert =
  336. WebPYUV444Converters[p->output->colorspace];
  337. const WebPRGBABuffer* const buf = &p->output->u.RGBA;
  338. uint8_t* dst = buf->rgba + (size_t)y_pos * buf->stride;
  339. int num_lines_out = 0;
  340. // For RGB rescaling, because of the YUV420, current scan position
  341. // U/V can be +1/-1 line from the Y one. Hence the double test.
  342. while (WebPRescalerHasPendingOutput(p->scaler_y) &&
  343. WebPRescalerHasPendingOutput(p->scaler_u)) {
  344. assert(y_pos + num_lines_out < p->output->height);
  345. assert(p->scaler_u->y_accum == p->scaler_v->y_accum);
  346. WebPRescalerExportRow(p->scaler_y);
  347. WebPRescalerExportRow(p->scaler_u);
  348. WebPRescalerExportRow(p->scaler_v);
  349. convert(p->scaler_y->dst, p->scaler_u->dst, p->scaler_v->dst,
  350. dst, p->scaler_y->dst_width);
  351. dst += buf->stride;
  352. ++num_lines_out;
  353. }
  354. return num_lines_out;
  355. }
  356. static int EmitRescaledRGB(const VP8Io* const io, WebPDecParams* const p) {
  357. const int mb_h = io->mb_h;
  358. const int uv_mb_h = (mb_h + 1) >> 1;
  359. int j = 0, uv_j = 0;
  360. int num_lines_out = 0;
  361. while (j < mb_h) {
  362. const int y_lines_in =
  363. WebPRescalerImport(p->scaler_y, mb_h - j,
  364. io->y + (size_t)j * io->y_stride, io->y_stride);
  365. j += y_lines_in;
  366. if (WebPRescaleNeededLines(p->scaler_u, uv_mb_h - uv_j)) {
  367. const int u_lines_in = WebPRescalerImport(
  368. p->scaler_u, uv_mb_h - uv_j, io->u + (size_t)uv_j * io->uv_stride,
  369. io->uv_stride);
  370. const int v_lines_in = WebPRescalerImport(
  371. p->scaler_v, uv_mb_h - uv_j, io->v + (size_t)uv_j * io->uv_stride,
  372. io->uv_stride);
  373. (void)v_lines_in; // remove a gcc warning
  374. assert(u_lines_in == v_lines_in);
  375. uv_j += u_lines_in;
  376. }
  377. num_lines_out += ExportRGB(p, p->last_y + num_lines_out);
  378. }
  379. return num_lines_out;
  380. }
  381. static int ExportAlpha(WebPDecParams* const p, int y_pos, int max_lines_out) {
  382. const WebPRGBABuffer* const buf = &p->output->u.RGBA;
  383. uint8_t* const base_rgba = buf->rgba + (size_t)y_pos * buf->stride;
  384. const WEBP_CSP_MODE colorspace = p->output->colorspace;
  385. const int alpha_first =
  386. (colorspace == MODE_ARGB || colorspace == MODE_Argb);
  387. uint8_t* dst = base_rgba + (alpha_first ? 0 : 3);
  388. int num_lines_out = 0;
  389. const int is_premult_alpha = WebPIsPremultipliedMode(colorspace);
  390. uint32_t non_opaque = 0;
  391. const int width = p->scaler_a->dst_width;
  392. while (WebPRescalerHasPendingOutput(p->scaler_a) &&
  393. num_lines_out < max_lines_out) {
  394. assert(y_pos + num_lines_out < p->output->height);
  395. WebPRescalerExportRow(p->scaler_a);
  396. non_opaque |= WebPDispatchAlpha(p->scaler_a->dst, 0, width, 1, dst, 0);
  397. dst += buf->stride;
  398. ++num_lines_out;
  399. }
  400. if (is_premult_alpha && non_opaque) {
  401. WebPApplyAlphaMultiply(base_rgba, alpha_first,
  402. width, num_lines_out, buf->stride);
  403. }
  404. return num_lines_out;
  405. }
  406. static int ExportAlphaRGBA4444(WebPDecParams* const p, int y_pos,
  407. int max_lines_out) {
  408. const WebPRGBABuffer* const buf = &p->output->u.RGBA;
  409. uint8_t* const base_rgba = buf->rgba + (size_t)y_pos * buf->stride;
  410. #if (WEBP_SWAP_16BIT_CSP == 1)
  411. uint8_t* alpha_dst = base_rgba;
  412. #else
  413. uint8_t* alpha_dst = base_rgba + 1;
  414. #endif
  415. int num_lines_out = 0;
  416. const WEBP_CSP_MODE colorspace = p->output->colorspace;
  417. const int width = p->scaler_a->dst_width;
  418. const int is_premult_alpha = WebPIsPremultipliedMode(colorspace);
  419. uint32_t alpha_mask = 0x0f;
  420. while (WebPRescalerHasPendingOutput(p->scaler_a) &&
  421. num_lines_out < max_lines_out) {
  422. int i;
  423. assert(y_pos + num_lines_out < p->output->height);
  424. WebPRescalerExportRow(p->scaler_a);
  425. for (i = 0; i < width; ++i) {
  426. // Fill in the alpha value (converted to 4 bits).
  427. const uint32_t alpha_value = p->scaler_a->dst[i] >> 4;
  428. alpha_dst[2 * i] = (alpha_dst[2 * i] & 0xf0) | alpha_value;
  429. alpha_mask &= alpha_value;
  430. }
  431. alpha_dst += buf->stride;
  432. ++num_lines_out;
  433. }
  434. if (is_premult_alpha && alpha_mask != 0x0f) {
  435. WebPApplyAlphaMultiply4444(base_rgba, width, num_lines_out, buf->stride);
  436. }
  437. return num_lines_out;
  438. }
  439. static int EmitRescaledAlphaRGB(const VP8Io* const io, WebPDecParams* const p,
  440. int expected_num_out_lines) {
  441. if (io->a != NULL) {
  442. WebPRescaler* const scaler = p->scaler_a;
  443. int lines_left = expected_num_out_lines;
  444. const int y_end = p->last_y + lines_left;
  445. while (lines_left > 0) {
  446. const int64_t row_offset = (int64_t)scaler->src_y - io->mb_y;
  447. WebPRescalerImport(scaler, io->mb_h + io->mb_y - scaler->src_y,
  448. io->a + row_offset * io->width, io->width);
  449. lines_left -= p->emit_alpha_row(p, y_end - lines_left, lines_left);
  450. }
  451. }
  452. return 0;
  453. }
  454. static int InitRGBRescaler(const VP8Io* const io, WebPDecParams* const p) {
  455. const int has_alpha = WebPIsAlphaMode(p->output->colorspace);
  456. const int out_width = io->scaled_width;
  457. const int out_height = io->scaled_height;
  458. const int uv_in_width = (io->mb_w + 1) >> 1;
  459. const int uv_in_height = (io->mb_h + 1) >> 1;
  460. // scratch memory for one rescaler
  461. const size_t work_size = 2 * (size_t)out_width;
  462. rescaler_t* work; // rescalers work area
  463. uint8_t* tmp; // tmp storage for scaled YUV444 samples before RGB conversion
  464. uint64_t tmp_size1, tmp_size2, total_size;
  465. size_t rescaler_size;
  466. WebPRescaler* scalers;
  467. const int num_rescalers = has_alpha ? 4 : 3;
  468. tmp_size1 = (uint64_t)num_rescalers * work_size;
  469. tmp_size2 = (uint64_t)num_rescalers * out_width;
  470. total_size = tmp_size1 * sizeof(*work) + tmp_size2 * sizeof(*tmp);
  471. rescaler_size = num_rescalers * sizeof(*p->scaler_y) + WEBP_ALIGN_CST;
  472. total_size += rescaler_size;
  473. if (!CheckSizeOverflow(total_size)) {
  474. return 0;
  475. }
  476. p->memory = WebPSafeMalloc(1ULL, (size_t)total_size);
  477. if (p->memory == NULL) {
  478. return 0; // memory error
  479. }
  480. work = (rescaler_t*)p->memory;
  481. tmp = (uint8_t*)(work + tmp_size1);
  482. scalers = (WebPRescaler*)WEBP_ALIGN(
  483. (const uint8_t*)work + total_size - rescaler_size);
  484. p->scaler_y = &scalers[0];
  485. p->scaler_u = &scalers[1];
  486. p->scaler_v = &scalers[2];
  487. p->scaler_a = has_alpha ? &scalers[3] : NULL;
  488. if (!WebPRescalerInit(p->scaler_y, io->mb_w, io->mb_h,
  489. tmp + 0 * out_width, out_width, out_height, 0, 1,
  490. work + 0 * work_size) ||
  491. !WebPRescalerInit(p->scaler_u, uv_in_width, uv_in_height,
  492. tmp + 1 * out_width, out_width, out_height, 0, 1,
  493. work + 1 * work_size) ||
  494. !WebPRescalerInit(p->scaler_v, uv_in_width, uv_in_height,
  495. tmp + 2 * out_width, out_width, out_height, 0, 1,
  496. work + 2 * work_size)) {
  497. return 0;
  498. }
  499. p->emit = EmitRescaledRGB;
  500. WebPInitYUV444Converters();
  501. if (has_alpha) {
  502. if (!WebPRescalerInit(p->scaler_a, io->mb_w, io->mb_h,
  503. tmp + 3 * out_width, out_width, out_height, 0, 1,
  504. work + 3 * work_size)) {
  505. return 0;
  506. }
  507. p->emit_alpha = EmitRescaledAlphaRGB;
  508. if (p->output->colorspace == MODE_RGBA_4444 ||
  509. p->output->colorspace == MODE_rgbA_4444) {
  510. p->emit_alpha_row = ExportAlphaRGBA4444;
  511. } else {
  512. p->emit_alpha_row = ExportAlpha;
  513. }
  514. WebPInitAlphaProcessing();
  515. }
  516. return 1;
  517. }
  518. #endif // WEBP_REDUCE_SIZE
  519. //------------------------------------------------------------------------------
  520. // Default custom functions
  521. static int CustomSetup(VP8Io* io) {
  522. WebPDecParams* const p = (WebPDecParams*)io->opaque;
  523. const WEBP_CSP_MODE colorspace = p->output->colorspace;
  524. const int is_rgb = WebPIsRGBMode(colorspace);
  525. const int is_alpha = WebPIsAlphaMode(colorspace);
  526. p->memory = NULL;
  527. p->emit = NULL;
  528. p->emit_alpha = NULL;
  529. p->emit_alpha_row = NULL;
  530. if (!WebPIoInitFromOptions(p->options, io, is_alpha ? MODE_YUV : MODE_YUVA)) {
  531. return 0;
  532. }
  533. if (is_alpha && WebPIsPremultipliedMode(colorspace)) {
  534. WebPInitUpsamplers();
  535. }
  536. if (io->use_scaling) {
  537. #if !defined(WEBP_REDUCE_SIZE)
  538. const int ok = is_rgb ? InitRGBRescaler(io, p) : InitYUVRescaler(io, p);
  539. if (!ok) {
  540. return 0; // memory error
  541. }
  542. #else
  543. return 0; // rescaling support not compiled
  544. #endif
  545. } else {
  546. if (is_rgb) {
  547. WebPInitSamplers();
  548. p->emit = EmitSampledRGB; // default
  549. if (io->fancy_upsampling) {
  550. #ifdef FANCY_UPSAMPLING
  551. const int uv_width = (io->mb_w + 1) >> 1;
  552. p->memory = WebPSafeMalloc(1ULL, (size_t)(io->mb_w + 2 * uv_width));
  553. if (p->memory == NULL) {
  554. return 0; // memory error.
  555. }
  556. p->tmp_y = (uint8_t*)p->memory;
  557. p->tmp_u = p->tmp_y + io->mb_w;
  558. p->tmp_v = p->tmp_u + uv_width;
  559. p->emit = EmitFancyRGB;
  560. WebPInitUpsamplers();
  561. #endif
  562. }
  563. } else {
  564. p->emit = EmitYUV;
  565. }
  566. if (is_alpha) { // need transparency output
  567. p->emit_alpha =
  568. (colorspace == MODE_RGBA_4444 || colorspace == MODE_rgbA_4444) ?
  569. EmitAlphaRGBA4444
  570. : is_rgb ? EmitAlphaRGB
  571. : EmitAlphaYUV;
  572. if (is_rgb) {
  573. WebPInitAlphaProcessing();
  574. }
  575. }
  576. }
  577. return 1;
  578. }
  579. //------------------------------------------------------------------------------
  580. static int CustomPut(const VP8Io* io) {
  581. WebPDecParams* const p = (WebPDecParams*)io->opaque;
  582. const int mb_w = io->mb_w;
  583. const int mb_h = io->mb_h;
  584. int num_lines_out;
  585. assert(!(io->mb_y & 1));
  586. if (mb_w <= 0 || mb_h <= 0) {
  587. return 0;
  588. }
  589. num_lines_out = p->emit(io, p);
  590. if (p->emit_alpha != NULL) {
  591. p->emit_alpha(io, p, num_lines_out);
  592. }
  593. p->last_y += num_lines_out;
  594. return 1;
  595. }
  596. //------------------------------------------------------------------------------
  597. static void CustomTeardown(const VP8Io* io) {
  598. WebPDecParams* const p = (WebPDecParams*)io->opaque;
  599. WebPSafeFree(p->memory);
  600. p->memory = NULL;
  601. }
  602. //------------------------------------------------------------------------------
  603. // Main entry point
  604. void WebPInitCustomIo(WebPDecParams* const params, VP8Io* const io) {
  605. io->put = CustomPut;
  606. io->setup = CustomSetup;
  607. io->teardown = CustomTeardown;
  608. io->opaque = params;
  609. }
  610. //------------------------------------------------------------------------------