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