io.c 22 KB

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