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