frame_dec.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. // Copyright 2010 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. // Frame-reconstruction function. Memory allocation.
  11. //
  12. // Author: Skal (pascal.massimino@gmail.com)
  13. #include <stdlib.h>
  14. #include "./vp8i_dec.h"
  15. #include "../utils/utils.h"
  16. //------------------------------------------------------------------------------
  17. // Main reconstruction function.
  18. static const uint16_t kScan[16] = {
  19. 0 + 0 * BPS, 4 + 0 * BPS, 8 + 0 * BPS, 12 + 0 * BPS,
  20. 0 + 4 * BPS, 4 + 4 * BPS, 8 + 4 * BPS, 12 + 4 * BPS,
  21. 0 + 8 * BPS, 4 + 8 * BPS, 8 + 8 * BPS, 12 + 8 * BPS,
  22. 0 + 12 * BPS, 4 + 12 * BPS, 8 + 12 * BPS, 12 + 12 * BPS
  23. };
  24. static int CheckMode(int mb_x, int mb_y, int mode) {
  25. if (mode == B_DC_PRED) {
  26. if (mb_x == 0) {
  27. return (mb_y == 0) ? B_DC_PRED_NOTOPLEFT : B_DC_PRED_NOLEFT;
  28. } else {
  29. return (mb_y == 0) ? B_DC_PRED_NOTOP : B_DC_PRED;
  30. }
  31. }
  32. return mode;
  33. }
  34. static void Copy32b(uint8_t* const dst, const uint8_t* const src) {
  35. memcpy(dst, src, 4);
  36. }
  37. static WEBP_INLINE void DoTransform(uint32_t bits, const int16_t* const src,
  38. uint8_t* const dst) {
  39. switch (bits >> 30) {
  40. case 3:
  41. VP8Transform(src, dst, 0);
  42. break;
  43. case 2:
  44. VP8TransformAC3(src, dst);
  45. break;
  46. case 1:
  47. VP8TransformDC(src, dst);
  48. break;
  49. default:
  50. break;
  51. }
  52. }
  53. static void DoUVTransform(uint32_t bits, const int16_t* const src,
  54. uint8_t* const dst) {
  55. if (bits & 0xff) { // any non-zero coeff at all?
  56. if (bits & 0xaa) { // any non-zero AC coefficient?
  57. VP8TransformUV(src, dst); // note we don't use the AC3 variant for U/V
  58. } else {
  59. VP8TransformDCUV(src, dst);
  60. }
  61. }
  62. }
  63. static void ReconstructRow(const VP8Decoder* const dec,
  64. const VP8ThreadContext* ctx) {
  65. int j;
  66. int mb_x;
  67. const int mb_y = ctx->mb_y_;
  68. const int cache_id = ctx->id_;
  69. uint8_t* const y_dst = dec->yuv_b_ + Y_OFF;
  70. uint8_t* const u_dst = dec->yuv_b_ + U_OFF;
  71. uint8_t* const v_dst = dec->yuv_b_ + V_OFF;
  72. // Initialize left-most block.
  73. for (j = 0; j < 16; ++j) {
  74. y_dst[j * BPS - 1] = 129;
  75. }
  76. for (j = 0; j < 8; ++j) {
  77. u_dst[j * BPS - 1] = 129;
  78. v_dst[j * BPS - 1] = 129;
  79. }
  80. // Init top-left sample on left column too.
  81. if (mb_y > 0) {
  82. y_dst[-1 - BPS] = u_dst[-1 - BPS] = v_dst[-1 - BPS] = 129;
  83. } else {
  84. // we only need to do this init once at block (0,0).
  85. // Afterward, it remains valid for the whole topmost row.
  86. memset(y_dst - BPS - 1, 127, 16 + 4 + 1);
  87. memset(u_dst - BPS - 1, 127, 8 + 1);
  88. memset(v_dst - BPS - 1, 127, 8 + 1);
  89. }
  90. // Reconstruct one row.
  91. for (mb_x = 0; mb_x < dec->mb_w_; ++mb_x) {
  92. const VP8MBData* const block = ctx->mb_data_ + mb_x;
  93. // Rotate in the left samples from previously decoded block. We move four
  94. // pixels at a time for alignment reason, and because of in-loop filter.
  95. if (mb_x > 0) {
  96. for (j = -1; j < 16; ++j) {
  97. Copy32b(&y_dst[j * BPS - 4], &y_dst[j * BPS + 12]);
  98. }
  99. for (j = -1; j < 8; ++j) {
  100. Copy32b(&u_dst[j * BPS - 4], &u_dst[j * BPS + 4]);
  101. Copy32b(&v_dst[j * BPS - 4], &v_dst[j * BPS + 4]);
  102. }
  103. }
  104. {
  105. // bring top samples into the cache
  106. VP8TopSamples* const top_yuv = dec->yuv_t_ + mb_x;
  107. const int16_t* const coeffs = block->coeffs_;
  108. uint32_t bits = block->non_zero_y_;
  109. int n;
  110. if (mb_y > 0) {
  111. memcpy(y_dst - BPS, top_yuv[0].y, 16);
  112. memcpy(u_dst - BPS, top_yuv[0].u, 8);
  113. memcpy(v_dst - BPS, top_yuv[0].v, 8);
  114. }
  115. // predict and add residuals
  116. if (block->is_i4x4_) { // 4x4
  117. uint32_t* const top_right = (uint32_t*)(y_dst - BPS + 16);
  118. if (mb_y > 0) {
  119. if (mb_x >= dec->mb_w_ - 1) { // on rightmost border
  120. memset(top_right, top_yuv[0].y[15], sizeof(*top_right));
  121. } else {
  122. memcpy(top_right, top_yuv[1].y, sizeof(*top_right));
  123. }
  124. }
  125. // replicate the top-right pixels below
  126. top_right[BPS] = top_right[2 * BPS] = top_right[3 * BPS] = top_right[0];
  127. // predict and add residuals for all 4x4 blocks in turn.
  128. for (n = 0; n < 16; ++n, bits <<= 2) {
  129. uint8_t* const dst = y_dst + kScan[n];
  130. VP8PredLuma4[block->imodes_[n]](dst);
  131. DoTransform(bits, coeffs + n * 16, dst);
  132. }
  133. } else { // 16x16
  134. const int pred_func = CheckMode(mb_x, mb_y, block->imodes_[0]);
  135. VP8PredLuma16[pred_func](y_dst);
  136. if (bits != 0) {
  137. for (n = 0; n < 16; ++n, bits <<= 2) {
  138. DoTransform(bits, coeffs + n * 16, y_dst + kScan[n]);
  139. }
  140. }
  141. }
  142. {
  143. // Chroma
  144. const uint32_t bits_uv = block->non_zero_uv_;
  145. const int pred_func = CheckMode(mb_x, mb_y, block->uvmode_);
  146. VP8PredChroma8[pred_func](u_dst);
  147. VP8PredChroma8[pred_func](v_dst);
  148. DoUVTransform(bits_uv >> 0, coeffs + 16 * 16, u_dst);
  149. DoUVTransform(bits_uv >> 8, coeffs + 20 * 16, v_dst);
  150. }
  151. // stash away top samples for next block
  152. if (mb_y < dec->mb_h_ - 1) {
  153. memcpy(top_yuv[0].y, y_dst + 15 * BPS, 16);
  154. memcpy(top_yuv[0].u, u_dst + 7 * BPS, 8);
  155. memcpy(top_yuv[0].v, v_dst + 7 * BPS, 8);
  156. }
  157. }
  158. // Transfer reconstructed samples from yuv_b_ cache to final destination.
  159. {
  160. const int y_offset = cache_id * 16 * dec->cache_y_stride_;
  161. const int uv_offset = cache_id * 8 * dec->cache_uv_stride_;
  162. uint8_t* const y_out = dec->cache_y_ + mb_x * 16 + y_offset;
  163. uint8_t* const u_out = dec->cache_u_ + mb_x * 8 + uv_offset;
  164. uint8_t* const v_out = dec->cache_v_ + mb_x * 8 + uv_offset;
  165. for (j = 0; j < 16; ++j) {
  166. memcpy(y_out + j * dec->cache_y_stride_, y_dst + j * BPS, 16);
  167. }
  168. for (j = 0; j < 8; ++j) {
  169. memcpy(u_out + j * dec->cache_uv_stride_, u_dst + j * BPS, 8);
  170. memcpy(v_out + j * dec->cache_uv_stride_, v_dst + j * BPS, 8);
  171. }
  172. }
  173. }
  174. }
  175. //------------------------------------------------------------------------------
  176. // Filtering
  177. // kFilterExtraRows[] = How many extra lines are needed on the MB boundary
  178. // for caching, given a filtering level.
  179. // Simple filter: up to 2 luma samples are read and 1 is written.
  180. // Complex filter: up to 4 luma samples are read and 3 are written. Same for
  181. // U/V, so it's 8 samples total (because of the 2x upsampling).
  182. static const uint8_t kFilterExtraRows[3] = { 0, 2, 8 };
  183. static void DoFilter(const VP8Decoder* const dec, int mb_x, int mb_y) {
  184. const VP8ThreadContext* const ctx = &dec->thread_ctx_;
  185. const int cache_id = ctx->id_;
  186. const int y_bps = dec->cache_y_stride_;
  187. const VP8FInfo* const f_info = ctx->f_info_ + mb_x;
  188. uint8_t* const y_dst = dec->cache_y_ + cache_id * 16 * y_bps + mb_x * 16;
  189. const int ilevel = f_info->f_ilevel_;
  190. const int limit = f_info->f_limit_;
  191. if (limit == 0) {
  192. return;
  193. }
  194. assert(limit >= 3);
  195. if (dec->filter_type_ == 1) { // simple
  196. if (mb_x > 0) {
  197. VP8SimpleHFilter16(y_dst, y_bps, limit + 4);
  198. }
  199. if (f_info->f_inner_) {
  200. VP8SimpleHFilter16i(y_dst, y_bps, limit);
  201. }
  202. if (mb_y > 0) {
  203. VP8SimpleVFilter16(y_dst, y_bps, limit + 4);
  204. }
  205. if (f_info->f_inner_) {
  206. VP8SimpleVFilter16i(y_dst, y_bps, limit);
  207. }
  208. } else { // complex
  209. const int uv_bps = dec->cache_uv_stride_;
  210. uint8_t* const u_dst = dec->cache_u_ + cache_id * 8 * uv_bps + mb_x * 8;
  211. uint8_t* const v_dst = dec->cache_v_ + cache_id * 8 * uv_bps + mb_x * 8;
  212. const int hev_thresh = f_info->hev_thresh_;
  213. if (mb_x > 0) {
  214. VP8HFilter16(y_dst, y_bps, limit + 4, ilevel, hev_thresh);
  215. VP8HFilter8(u_dst, v_dst, uv_bps, limit + 4, ilevel, hev_thresh);
  216. }
  217. if (f_info->f_inner_) {
  218. VP8HFilter16i(y_dst, y_bps, limit, ilevel, hev_thresh);
  219. VP8HFilter8i(u_dst, v_dst, uv_bps, limit, ilevel, hev_thresh);
  220. }
  221. if (mb_y > 0) {
  222. VP8VFilter16(y_dst, y_bps, limit + 4, ilevel, hev_thresh);
  223. VP8VFilter8(u_dst, v_dst, uv_bps, limit + 4, ilevel, hev_thresh);
  224. }
  225. if (f_info->f_inner_) {
  226. VP8VFilter16i(y_dst, y_bps, limit, ilevel, hev_thresh);
  227. VP8VFilter8i(u_dst, v_dst, uv_bps, limit, ilevel, hev_thresh);
  228. }
  229. }
  230. }
  231. // Filter the decoded macroblock row (if needed)
  232. static void FilterRow(const VP8Decoder* const dec) {
  233. int mb_x;
  234. const int mb_y = dec->thread_ctx_.mb_y_;
  235. assert(dec->thread_ctx_.filter_row_);
  236. for (mb_x = dec->tl_mb_x_; mb_x < dec->br_mb_x_; ++mb_x) {
  237. DoFilter(dec, mb_x, mb_y);
  238. }
  239. }
  240. //------------------------------------------------------------------------------
  241. // Precompute the filtering strength for each segment and each i4x4/i16x16 mode.
  242. static void PrecomputeFilterStrengths(VP8Decoder* const dec) {
  243. if (dec->filter_type_ > 0) {
  244. int s;
  245. const VP8FilterHeader* const hdr = &dec->filter_hdr_;
  246. for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
  247. int i4x4;
  248. // First, compute the initial level
  249. int base_level;
  250. if (dec->segment_hdr_.use_segment_) {
  251. base_level = dec->segment_hdr_.filter_strength_[s];
  252. if (!dec->segment_hdr_.absolute_delta_) {
  253. base_level += hdr->level_;
  254. }
  255. } else {
  256. base_level = hdr->level_;
  257. }
  258. for (i4x4 = 0; i4x4 <= 1; ++i4x4) {
  259. VP8FInfo* const info = &dec->fstrengths_[s][i4x4];
  260. int level = base_level;
  261. if (hdr->use_lf_delta_) {
  262. level += hdr->ref_lf_delta_[0];
  263. if (i4x4) {
  264. level += hdr->mode_lf_delta_[0];
  265. }
  266. }
  267. level = (level < 0) ? 0 : (level > 63) ? 63 : level;
  268. if (level > 0) {
  269. int ilevel = level;
  270. if (hdr->sharpness_ > 0) {
  271. if (hdr->sharpness_ > 4) {
  272. ilevel >>= 2;
  273. } else {
  274. ilevel >>= 1;
  275. }
  276. if (ilevel > 9 - hdr->sharpness_) {
  277. ilevel = 9 - hdr->sharpness_;
  278. }
  279. }
  280. if (ilevel < 1) ilevel = 1;
  281. info->f_ilevel_ = ilevel;
  282. info->f_limit_ = 2 * level + ilevel;
  283. info->hev_thresh_ = (level >= 40) ? 2 : (level >= 15) ? 1 : 0;
  284. } else {
  285. info->f_limit_ = 0; // no filtering
  286. }
  287. info->f_inner_ = i4x4;
  288. }
  289. }
  290. }
  291. }
  292. //------------------------------------------------------------------------------
  293. // Dithering
  294. // minimal amp that will provide a non-zero dithering effect
  295. #define MIN_DITHER_AMP 4
  296. #define DITHER_AMP_TAB_SIZE 12
  297. static const uint8_t kQuantToDitherAmp[DITHER_AMP_TAB_SIZE] = {
  298. // roughly, it's dqm->uv_mat_[1]
  299. 8, 7, 6, 4, 4, 2, 2, 2, 1, 1, 1, 1
  300. };
  301. void VP8InitDithering(const WebPDecoderOptions* const options,
  302. VP8Decoder* const dec) {
  303. assert(dec != NULL);
  304. if (options != NULL) {
  305. const int d = options->dithering_strength;
  306. const int max_amp = (1 << VP8_RANDOM_DITHER_FIX) - 1;
  307. const int f = (d < 0) ? 0 : (d > 100) ? max_amp : (d * max_amp / 100);
  308. if (f > 0) {
  309. int s;
  310. int all_amp = 0;
  311. for (s = 0; s < NUM_MB_SEGMENTS; ++s) {
  312. VP8QuantMatrix* const dqm = &dec->dqm_[s];
  313. if (dqm->uv_quant_ < DITHER_AMP_TAB_SIZE) {
  314. const int idx = (dqm->uv_quant_ < 0) ? 0 : dqm->uv_quant_;
  315. dqm->dither_ = (f * kQuantToDitherAmp[idx]) >> 3;
  316. }
  317. all_amp |= dqm->dither_;
  318. }
  319. if (all_amp != 0) {
  320. VP8InitRandom(&dec->dithering_rg_, 1.0f);
  321. dec->dither_ = 1;
  322. }
  323. }
  324. // potentially allow alpha dithering
  325. dec->alpha_dithering_ = options->alpha_dithering_strength;
  326. if (dec->alpha_dithering_ > 100) {
  327. dec->alpha_dithering_ = 100;
  328. } else if (dec->alpha_dithering_ < 0) {
  329. dec->alpha_dithering_ = 0;
  330. }
  331. }
  332. }
  333. // Convert to range: [-2,2] for dither=50, [-4,4] for dither=100
  334. static void Dither8x8(VP8Random* const rg, uint8_t* dst, int bps, int amp) {
  335. uint8_t dither[64];
  336. int i;
  337. for (i = 0; i < 8 * 8; ++i) {
  338. dither[i] = VP8RandomBits2(rg, VP8_DITHER_AMP_BITS + 1, amp);
  339. }
  340. VP8DitherCombine8x8(dither, dst, bps);
  341. }
  342. static void DitherRow(VP8Decoder* const dec) {
  343. int mb_x;
  344. assert(dec->dither_);
  345. for (mb_x = dec->tl_mb_x_; mb_x < dec->br_mb_x_; ++mb_x) {
  346. const VP8ThreadContext* const ctx = &dec->thread_ctx_;
  347. const VP8MBData* const data = ctx->mb_data_ + mb_x;
  348. const int cache_id = ctx->id_;
  349. const int uv_bps = dec->cache_uv_stride_;
  350. if (data->dither_ >= MIN_DITHER_AMP) {
  351. uint8_t* const u_dst = dec->cache_u_ + cache_id * 8 * uv_bps + mb_x * 8;
  352. uint8_t* const v_dst = dec->cache_v_ + cache_id * 8 * uv_bps + mb_x * 8;
  353. Dither8x8(&dec->dithering_rg_, u_dst, uv_bps, data->dither_);
  354. Dither8x8(&dec->dithering_rg_, v_dst, uv_bps, data->dither_);
  355. }
  356. }
  357. }
  358. //------------------------------------------------------------------------------
  359. // This function is called after a row of macroblocks is finished decoding.
  360. // It also takes into account the following restrictions:
  361. // * In case of in-loop filtering, we must hold off sending some of the bottom
  362. // pixels as they are yet unfiltered. They will be when the next macroblock
  363. // row is decoded. Meanwhile, we must preserve them by rotating them in the
  364. // cache area. This doesn't hold for the very bottom row of the uncropped
  365. // picture of course.
  366. // * we must clip the remaining pixels against the cropping area. The VP8Io
  367. // struct must have the following fields set correctly before calling put():
  368. #define MACROBLOCK_VPOS(mb_y) ((mb_y) * 16) // vertical position of a MB
  369. // Finalize and transmit a complete row. Return false in case of user-abort.
  370. static int FinishRow(void* arg1, void* arg2) {
  371. VP8Decoder* const dec = (VP8Decoder*)arg1;
  372. VP8Io* const io = (VP8Io*)arg2;
  373. int ok = 1;
  374. const VP8ThreadContext* const ctx = &dec->thread_ctx_;
  375. const int cache_id = ctx->id_;
  376. const int extra_y_rows = kFilterExtraRows[dec->filter_type_];
  377. const int ysize = extra_y_rows * dec->cache_y_stride_;
  378. const int uvsize = (extra_y_rows / 2) * dec->cache_uv_stride_;
  379. const int y_offset = cache_id * 16 * dec->cache_y_stride_;
  380. const int uv_offset = cache_id * 8 * dec->cache_uv_stride_;
  381. uint8_t* const ydst = dec->cache_y_ - ysize + y_offset;
  382. uint8_t* const udst = dec->cache_u_ - uvsize + uv_offset;
  383. uint8_t* const vdst = dec->cache_v_ - uvsize + uv_offset;
  384. const int mb_y = ctx->mb_y_;
  385. const int is_first_row = (mb_y == 0);
  386. const int is_last_row = (mb_y >= dec->br_mb_y_ - 1);
  387. if (dec->mt_method_ == 2) {
  388. ReconstructRow(dec, ctx);
  389. }
  390. if (ctx->filter_row_) {
  391. FilterRow(dec);
  392. }
  393. if (dec->dither_) {
  394. DitherRow(dec);
  395. }
  396. if (io->put != NULL) {
  397. int y_start = MACROBLOCK_VPOS(mb_y);
  398. int y_end = MACROBLOCK_VPOS(mb_y + 1);
  399. if (!is_first_row) {
  400. y_start -= extra_y_rows;
  401. io->y = ydst;
  402. io->u = udst;
  403. io->v = vdst;
  404. } else {
  405. io->y = dec->cache_y_ + y_offset;
  406. io->u = dec->cache_u_ + uv_offset;
  407. io->v = dec->cache_v_ + uv_offset;
  408. }
  409. if (!is_last_row) {
  410. y_end -= extra_y_rows;
  411. }
  412. if (y_end > io->crop_bottom) {
  413. y_end = io->crop_bottom; // make sure we don't overflow on last row.
  414. }
  415. // If dec->alpha_data_ is not NULL, we have some alpha plane present.
  416. io->a = NULL;
  417. if (dec->alpha_data_ != NULL && y_start < y_end) {
  418. io->a = VP8DecompressAlphaRows(dec, io, y_start, y_end - y_start);
  419. if (io->a == NULL) {
  420. return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
  421. "Could not decode alpha data.");
  422. }
  423. }
  424. if (y_start < io->crop_top) {
  425. const int delta_y = io->crop_top - y_start;
  426. y_start = io->crop_top;
  427. assert(!(delta_y & 1));
  428. io->y += dec->cache_y_stride_ * delta_y;
  429. io->u += dec->cache_uv_stride_ * (delta_y >> 1);
  430. io->v += dec->cache_uv_stride_ * (delta_y >> 1);
  431. if (io->a != NULL) {
  432. io->a += io->width * delta_y;
  433. }
  434. }
  435. if (y_start < y_end) {
  436. io->y += io->crop_left;
  437. io->u += io->crop_left >> 1;
  438. io->v += io->crop_left >> 1;
  439. if (io->a != NULL) {
  440. io->a += io->crop_left;
  441. }
  442. io->mb_y = y_start - io->crop_top;
  443. io->mb_w = io->crop_right - io->crop_left;
  444. io->mb_h = y_end - y_start;
  445. ok = io->put(io);
  446. }
  447. }
  448. // rotate top samples if needed
  449. if (cache_id + 1 == dec->num_caches_) {
  450. if (!is_last_row) {
  451. memcpy(dec->cache_y_ - ysize, ydst + 16 * dec->cache_y_stride_, ysize);
  452. memcpy(dec->cache_u_ - uvsize, udst + 8 * dec->cache_uv_stride_, uvsize);
  453. memcpy(dec->cache_v_ - uvsize, vdst + 8 * dec->cache_uv_stride_, uvsize);
  454. }
  455. }
  456. return ok;
  457. }
  458. #undef MACROBLOCK_VPOS
  459. //------------------------------------------------------------------------------
  460. int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io) {
  461. int ok = 1;
  462. VP8ThreadContext* const ctx = &dec->thread_ctx_;
  463. const int filter_row =
  464. (dec->filter_type_ > 0) &&
  465. (dec->mb_y_ >= dec->tl_mb_y_) && (dec->mb_y_ <= dec->br_mb_y_);
  466. if (dec->mt_method_ == 0) {
  467. // ctx->id_ and ctx->f_info_ are already set
  468. ctx->mb_y_ = dec->mb_y_;
  469. ctx->filter_row_ = filter_row;
  470. ReconstructRow(dec, ctx);
  471. ok = FinishRow(dec, io);
  472. } else {
  473. WebPWorker* const worker = &dec->worker_;
  474. // Finish previous job *before* updating context
  475. ok &= WebPGetWorkerInterface()->Sync(worker);
  476. assert(worker->status_ == OK);
  477. if (ok) { // spawn a new deblocking/output job
  478. ctx->io_ = *io;
  479. ctx->id_ = dec->cache_id_;
  480. ctx->mb_y_ = dec->mb_y_;
  481. ctx->filter_row_ = filter_row;
  482. if (dec->mt_method_ == 2) { // swap macroblock data
  483. VP8MBData* const tmp = ctx->mb_data_;
  484. ctx->mb_data_ = dec->mb_data_;
  485. dec->mb_data_ = tmp;
  486. } else {
  487. // perform reconstruction directly in main thread
  488. ReconstructRow(dec, ctx);
  489. }
  490. if (filter_row) { // swap filter info
  491. VP8FInfo* const tmp = ctx->f_info_;
  492. ctx->f_info_ = dec->f_info_;
  493. dec->f_info_ = tmp;
  494. }
  495. // (reconstruct)+filter in parallel
  496. WebPGetWorkerInterface()->Launch(worker);
  497. if (++dec->cache_id_ == dec->num_caches_) {
  498. dec->cache_id_ = 0;
  499. }
  500. }
  501. }
  502. return ok;
  503. }
  504. //------------------------------------------------------------------------------
  505. // Finish setting up the decoding parameter once user's setup() is called.
  506. VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io) {
  507. // Call setup() first. This may trigger additional decoding features on 'io'.
  508. // Note: Afterward, we must call teardown() no matter what.
  509. if (io->setup != NULL && !io->setup(io)) {
  510. VP8SetError(dec, VP8_STATUS_USER_ABORT, "Frame setup failed");
  511. return dec->status_;
  512. }
  513. // Disable filtering per user request
  514. if (io->bypass_filtering) {
  515. dec->filter_type_ = 0;
  516. }
  517. // Define the area where we can skip in-loop filtering, in case of cropping.
  518. //
  519. // 'Simple' filter reads two luma samples outside of the macroblock
  520. // and filters one. It doesn't filter the chroma samples. Hence, we can
  521. // avoid doing the in-loop filtering before crop_top/crop_left position.
  522. // For the 'Complex' filter, 3 samples are read and up to 3 are filtered.
  523. // Means: there's a dependency chain that goes all the way up to the
  524. // top-left corner of the picture (MB #0). We must filter all the previous
  525. // macroblocks.
  526. {
  527. const int extra_pixels = kFilterExtraRows[dec->filter_type_];
  528. if (dec->filter_type_ == 2) {
  529. // For complex filter, we need to preserve the dependency chain.
  530. dec->tl_mb_x_ = 0;
  531. dec->tl_mb_y_ = 0;
  532. } else {
  533. // For simple filter, we can filter only the cropped region.
  534. // We include 'extra_pixels' on the other side of the boundary, since
  535. // vertical or horizontal filtering of the previous macroblock can
  536. // modify some abutting pixels.
  537. dec->tl_mb_x_ = (io->crop_left - extra_pixels) >> 4;
  538. dec->tl_mb_y_ = (io->crop_top - extra_pixels) >> 4;
  539. if (dec->tl_mb_x_ < 0) dec->tl_mb_x_ = 0;
  540. if (dec->tl_mb_y_ < 0) dec->tl_mb_y_ = 0;
  541. }
  542. // We need some 'extra' pixels on the right/bottom.
  543. dec->br_mb_y_ = (io->crop_bottom + 15 + extra_pixels) >> 4;
  544. dec->br_mb_x_ = (io->crop_right + 15 + extra_pixels) >> 4;
  545. if (dec->br_mb_x_ > dec->mb_w_) {
  546. dec->br_mb_x_ = dec->mb_w_;
  547. }
  548. if (dec->br_mb_y_ > dec->mb_h_) {
  549. dec->br_mb_y_ = dec->mb_h_;
  550. }
  551. }
  552. PrecomputeFilterStrengths(dec);
  553. return VP8_STATUS_OK;
  554. }
  555. int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io) {
  556. int ok = 1;
  557. if (dec->mt_method_ > 0) {
  558. ok = WebPGetWorkerInterface()->Sync(&dec->worker_);
  559. }
  560. if (io->teardown != NULL) {
  561. io->teardown(io);
  562. }
  563. return ok;
  564. }
  565. //------------------------------------------------------------------------------
  566. // For multi-threaded decoding we need to use 3 rows of 16 pixels as delay line.
  567. //
  568. // Reason is: the deblocking filter cannot deblock the bottom horizontal edges
  569. // immediately, and needs to wait for first few rows of the next macroblock to
  570. // be decoded. Hence, deblocking is lagging behind by 4 or 8 pixels (depending
  571. // on strength).
  572. // With two threads, the vertical positions of the rows being decoded are:
  573. // Decode: [ 0..15][16..31][32..47][48..63][64..79][...
  574. // Deblock: [ 0..11][12..27][28..43][44..59][...
  575. // If we use two threads and two caches of 16 pixels, the sequence would be:
  576. // Decode: [ 0..15][16..31][ 0..15!!][16..31][ 0..15][...
  577. // Deblock: [ 0..11][12..27!!][-4..11][12..27][...
  578. // The problem occurs during row [12..15!!] that both the decoding and
  579. // deblocking threads are writing simultaneously.
  580. // With 3 cache lines, one get a safe write pattern:
  581. // Decode: [ 0..15][16..31][32..47][ 0..15][16..31][32..47][0..
  582. // Deblock: [ 0..11][12..27][28..43][-4..11][12..27][28...
  583. // Note that multi-threaded output _without_ deblocking can make use of two
  584. // cache lines of 16 pixels only, since there's no lagging behind. The decoding
  585. // and output process have non-concurrent writing:
  586. // Decode: [ 0..15][16..31][ 0..15][16..31][...
  587. // io->put: [ 0..15][16..31][ 0..15][...
  588. #define MT_CACHE_LINES 3
  589. #define ST_CACHE_LINES 1 // 1 cache row only for single-threaded case
  590. // Initialize multi/single-thread worker
  591. static int InitThreadContext(VP8Decoder* const dec) {
  592. dec->cache_id_ = 0;
  593. if (dec->mt_method_ > 0) {
  594. WebPWorker* const worker = &dec->worker_;
  595. if (!WebPGetWorkerInterface()->Reset(worker)) {
  596. return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
  597. "thread initialization failed.");
  598. }
  599. worker->data1 = dec;
  600. worker->data2 = (void*)&dec->thread_ctx_.io_;
  601. worker->hook = FinishRow;
  602. dec->num_caches_ =
  603. (dec->filter_type_ > 0) ? MT_CACHE_LINES : MT_CACHE_LINES - 1;
  604. } else {
  605. dec->num_caches_ = ST_CACHE_LINES;
  606. }
  607. return 1;
  608. }
  609. int VP8GetThreadMethod(const WebPDecoderOptions* const options,
  610. const WebPHeaderStructure* const headers,
  611. int width, int height) {
  612. if (options == NULL || options->use_threads == 0) {
  613. return 0;
  614. }
  615. (void)headers;
  616. (void)width;
  617. (void)height;
  618. assert(headers == NULL || !headers->is_lossless);
  619. #if defined(WEBP_USE_THREAD)
  620. if (width >= MIN_WIDTH_FOR_THREADS) return 2;
  621. #endif
  622. return 0;
  623. }
  624. #undef MT_CACHE_LINES
  625. #undef ST_CACHE_LINES
  626. //------------------------------------------------------------------------------
  627. // Memory setup
  628. static int AllocateMemory(VP8Decoder* const dec) {
  629. const int num_caches = dec->num_caches_;
  630. const int mb_w = dec->mb_w_;
  631. // Note: we use 'size_t' when there's no overflow risk, uint64_t otherwise.
  632. const size_t intra_pred_mode_size = 4 * mb_w * sizeof(uint8_t);
  633. const size_t top_size = sizeof(VP8TopSamples) * mb_w;
  634. const size_t mb_info_size = (mb_w + 1) * sizeof(VP8MB);
  635. const size_t f_info_size =
  636. (dec->filter_type_ > 0) ?
  637. mb_w * (dec->mt_method_ > 0 ? 2 : 1) * sizeof(VP8FInfo)
  638. : 0;
  639. const size_t yuv_size = YUV_SIZE * sizeof(*dec->yuv_b_);
  640. const size_t mb_data_size =
  641. (dec->mt_method_ == 2 ? 2 : 1) * mb_w * sizeof(*dec->mb_data_);
  642. const size_t cache_height = (16 * num_caches
  643. + kFilterExtraRows[dec->filter_type_]) * 3 / 2;
  644. const size_t cache_size = top_size * cache_height;
  645. // alpha_size is the only one that scales as width x height.
  646. const uint64_t alpha_size = (dec->alpha_data_ != NULL) ?
  647. (uint64_t)dec->pic_hdr_.width_ * dec->pic_hdr_.height_ : 0ULL;
  648. const uint64_t needed = (uint64_t)intra_pred_mode_size
  649. + top_size + mb_info_size + f_info_size
  650. + yuv_size + mb_data_size
  651. + cache_size + alpha_size + WEBP_ALIGN_CST;
  652. uint8_t* mem;
  653. if (!CheckSizeOverflow(needed)) return 0; // check for overflow
  654. if (needed > dec->mem_size_) {
  655. WebPSafeFree(dec->mem_);
  656. dec->mem_size_ = 0;
  657. dec->mem_ = WebPSafeMalloc(needed, sizeof(uint8_t));
  658. if (dec->mem_ == NULL) {
  659. return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
  660. "no memory during frame initialization.");
  661. }
  662. // down-cast is ok, thanks to WebPSafeMalloc() above.
  663. dec->mem_size_ = (size_t)needed;
  664. }
  665. mem = (uint8_t*)dec->mem_;
  666. dec->intra_t_ = mem;
  667. mem += intra_pred_mode_size;
  668. dec->yuv_t_ = (VP8TopSamples*)mem;
  669. mem += top_size;
  670. dec->mb_info_ = ((VP8MB*)mem) + 1;
  671. mem += mb_info_size;
  672. dec->f_info_ = f_info_size ? (VP8FInfo*)mem : NULL;
  673. mem += f_info_size;
  674. dec->thread_ctx_.id_ = 0;
  675. dec->thread_ctx_.f_info_ = dec->f_info_;
  676. if (dec->filter_type_ > 0 && dec->mt_method_ > 0) {
  677. // secondary cache line. The deblocking process need to make use of the
  678. // filtering strength from previous macroblock row, while the new ones
  679. // are being decoded in parallel. We'll just swap the pointers.
  680. dec->thread_ctx_.f_info_ += mb_w;
  681. }
  682. mem = (uint8_t*)WEBP_ALIGN(mem);
  683. assert((yuv_size & WEBP_ALIGN_CST) == 0);
  684. dec->yuv_b_ = mem;
  685. mem += yuv_size;
  686. dec->mb_data_ = (VP8MBData*)mem;
  687. dec->thread_ctx_.mb_data_ = (VP8MBData*)mem;
  688. if (dec->mt_method_ == 2) {
  689. dec->thread_ctx_.mb_data_ += mb_w;
  690. }
  691. mem += mb_data_size;
  692. dec->cache_y_stride_ = 16 * mb_w;
  693. dec->cache_uv_stride_ = 8 * mb_w;
  694. {
  695. const int extra_rows = kFilterExtraRows[dec->filter_type_];
  696. const int extra_y = extra_rows * dec->cache_y_stride_;
  697. const int extra_uv = (extra_rows / 2) * dec->cache_uv_stride_;
  698. dec->cache_y_ = mem + extra_y;
  699. dec->cache_u_ = dec->cache_y_
  700. + 16 * num_caches * dec->cache_y_stride_ + extra_uv;
  701. dec->cache_v_ = dec->cache_u_
  702. + 8 * num_caches * dec->cache_uv_stride_ + extra_uv;
  703. dec->cache_id_ = 0;
  704. }
  705. mem += cache_size;
  706. // alpha plane
  707. dec->alpha_plane_ = alpha_size ? mem : NULL;
  708. mem += alpha_size;
  709. assert(mem <= (uint8_t*)dec->mem_ + dec->mem_size_);
  710. // note: left/top-info is initialized once for all.
  711. memset(dec->mb_info_ - 1, 0, mb_info_size);
  712. VP8InitScanline(dec); // initialize left too.
  713. // initialize top
  714. memset(dec->intra_t_, B_DC_PRED, intra_pred_mode_size);
  715. return 1;
  716. }
  717. static void InitIo(VP8Decoder* const dec, VP8Io* io) {
  718. // prepare 'io'
  719. io->mb_y = 0;
  720. io->y = dec->cache_y_;
  721. io->u = dec->cache_u_;
  722. io->v = dec->cache_v_;
  723. io->y_stride = dec->cache_y_stride_;
  724. io->uv_stride = dec->cache_uv_stride_;
  725. io->a = NULL;
  726. }
  727. int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io) {
  728. if (!InitThreadContext(dec)) return 0; // call first. Sets dec->num_caches_.
  729. if (!AllocateMemory(dec)) return 0;
  730. InitIo(dec, io);
  731. VP8DspInit(); // Init critical function pointers and look-up tables.
  732. return 1;
  733. }
  734. //------------------------------------------------------------------------------