frame.c 29 KB

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