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