• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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           const int idx = (dqm->uv_quant_ < 0) ? 0 : dqm->uv_quant_;
342           dqm->dither_ = (f * kQuantToDitherAmp[idx]) >> 3;
343         }
344         all_amp |= dqm->dither_;
345       }
346       if (all_amp != 0) {
347         VP8InitRandom(&dec->dithering_rg_, 1.0f);
348         dec->dither_ = 1;
349       }
350     }
351     // potentially allow alpha dithering
352     dec->alpha_dithering_ = options->alpha_dithering_strength;
353     if (dec->alpha_dithering_ > 100) {
354       dec->alpha_dithering_ = 100;
355     } else if (dec->alpha_dithering_ < 0) {
356       dec->alpha_dithering_ = 0;
357     }
358   }
359 }
360 
361 // Convert to range: [-2,2] for dither=50, [-4,4] for dither=100
Dither8x8(VP8Random * const rg,uint8_t * dst,int bps,int amp)362 static void Dither8x8(VP8Random* const rg, uint8_t* dst, int bps, int amp) {
363   uint8_t dither[64];
364   int i;
365   for (i = 0; i < 8 * 8; ++i) {
366     dither[i] = VP8RandomBits2(rg, VP8_DITHER_AMP_BITS + 1, amp);
367   }
368   VP8DitherCombine8x8(dither, dst, bps);
369 }
370 
DitherRow(VP8Decoder * const dec)371 static void DitherRow(VP8Decoder* const dec) {
372   int mb_x;
373   assert(dec->dither_);
374   for (mb_x = dec->tl_mb_x_; mb_x < dec->br_mb_x_; ++mb_x) {
375     const VP8ThreadContext* const ctx = &dec->thread_ctx_;
376     const VP8MBData* const data = ctx->mb_data_ + mb_x;
377     const int cache_id = ctx->id_;
378     const int uv_bps = dec->cache_uv_stride_;
379     if (data->dither_ >= MIN_DITHER_AMP) {
380       uint8_t* const u_dst = dec->cache_u_ + cache_id * 8 * uv_bps + mb_x * 8;
381       uint8_t* const v_dst = dec->cache_v_ + cache_id * 8 * uv_bps + mb_x * 8;
382       Dither8x8(&dec->dithering_rg_, u_dst, uv_bps, data->dither_);
383       Dither8x8(&dec->dithering_rg_, v_dst, uv_bps, data->dither_);
384     }
385   }
386 }
387 
388 //------------------------------------------------------------------------------
389 // This function is called after a row of macroblocks is finished decoding.
390 // It also takes into account the following restrictions:
391 //  * In case of in-loop filtering, we must hold off sending some of the bottom
392 //    pixels as they are yet unfiltered. They will be when the next macroblock
393 //    row is decoded. Meanwhile, we must preserve them by rotating them in the
394 //    cache area. This doesn't hold for the very bottom row of the uncropped
395 //    picture of course.
396 //  * we must clip the remaining pixels against the cropping area. The VP8Io
397 //    struct must have the following fields set correctly before calling put():
398 
399 #define MACROBLOCK_VPOS(mb_y)  ((mb_y) * 16)    // vertical position of a MB
400 
401 // Finalize and transmit a complete row. Return false in case of user-abort.
FinishRow(void * arg1,void * arg2)402 static int FinishRow(void* arg1, void* arg2) {
403   VP8Decoder* const dec = (VP8Decoder*)arg1;
404   VP8Io* const io = (VP8Io*)arg2;
405   int ok = 1;
406   const VP8ThreadContext* const ctx = &dec->thread_ctx_;
407   const int cache_id = ctx->id_;
408   const int extra_y_rows = kFilterExtraRows[dec->filter_type_];
409   const int ysize = extra_y_rows * dec->cache_y_stride_;
410   const int uvsize = (extra_y_rows / 2) * dec->cache_uv_stride_;
411   const int y_offset = cache_id * 16 * dec->cache_y_stride_;
412   const int uv_offset = cache_id * 8 * dec->cache_uv_stride_;
413   uint8_t* const ydst = dec->cache_y_ - ysize + y_offset;
414   uint8_t* const udst = dec->cache_u_ - uvsize + uv_offset;
415   uint8_t* const vdst = dec->cache_v_ - uvsize + uv_offset;
416   const int mb_y = ctx->mb_y_;
417   const int is_first_row = (mb_y == 0);
418   const int is_last_row = (mb_y >= dec->br_mb_y_ - 1);
419 
420   if (dec->mt_method_ == 2) {
421     ReconstructRow(dec, ctx);
422   }
423 
424   if (ctx->filter_row_) {
425     FilterRow(dec);
426   }
427 
428   if (dec->dither_) {
429     DitherRow(dec);
430   }
431 
432   if (io->put != NULL) {
433     int y_start = MACROBLOCK_VPOS(mb_y);
434     int y_end = MACROBLOCK_VPOS(mb_y + 1);
435     if (!is_first_row) {
436       y_start -= extra_y_rows;
437       io->y = ydst;
438       io->u = udst;
439       io->v = vdst;
440     } else {
441       io->y = dec->cache_y_ + y_offset;
442       io->u = dec->cache_u_ + uv_offset;
443       io->v = dec->cache_v_ + uv_offset;
444     }
445 
446     if (!is_last_row) {
447       y_end -= extra_y_rows;
448     }
449     if (y_end > io->crop_bottom) {
450       y_end = io->crop_bottom;    // make sure we don't overflow on last row.
451     }
452     // If dec->alpha_data_ is not NULL, we have some alpha plane present.
453     io->a = NULL;
454     if (dec->alpha_data_ != NULL && y_start < y_end) {
455       io->a = VP8DecompressAlphaRows(dec, io, y_start, y_end - y_start);
456       if (io->a == NULL) {
457         return VP8SetError(dec, VP8_STATUS_BITSTREAM_ERROR,
458                            "Could not decode alpha data.");
459       }
460     }
461     if (y_start < io->crop_top) {
462       const int delta_y = io->crop_top - y_start;
463       y_start = io->crop_top;
464       assert(!(delta_y & 1));
465       io->y += dec->cache_y_stride_ * delta_y;
466       io->u += dec->cache_uv_stride_ * (delta_y >> 1);
467       io->v += dec->cache_uv_stride_ * (delta_y >> 1);
468       if (io->a != NULL) {
469         io->a += io->width * delta_y;
470       }
471     }
472     if (y_start < y_end) {
473       io->y += io->crop_left;
474       io->u += io->crop_left >> 1;
475       io->v += io->crop_left >> 1;
476       if (io->a != NULL) {
477         io->a += io->crop_left;
478       }
479       io->mb_y = y_start - io->crop_top;
480       io->mb_w = io->crop_right - io->crop_left;
481       io->mb_h = y_end - y_start;
482       ok = io->put(io);
483     }
484   }
485   // rotate top samples if needed
486   if (cache_id + 1 == dec->num_caches_) {
487     if (!is_last_row) {
488       memcpy(dec->cache_y_ - ysize, ydst + 16 * dec->cache_y_stride_, ysize);
489       memcpy(dec->cache_u_ - uvsize, udst + 8 * dec->cache_uv_stride_, uvsize);
490       memcpy(dec->cache_v_ - uvsize, vdst + 8 * dec->cache_uv_stride_, uvsize);
491     }
492   }
493 
494   return ok;
495 }
496 
497 #undef MACROBLOCK_VPOS
498 
499 //------------------------------------------------------------------------------
500 
VP8ProcessRow(VP8Decoder * const dec,VP8Io * const io)501 int VP8ProcessRow(VP8Decoder* const dec, VP8Io* const io) {
502   int ok = 1;
503   VP8ThreadContext* const ctx = &dec->thread_ctx_;
504   const int filter_row =
505       (dec->filter_type_ > 0) &&
506       (dec->mb_y_ >= dec->tl_mb_y_) && (dec->mb_y_ <= dec->br_mb_y_);
507   if (dec->mt_method_ == 0) {
508     // ctx->id_ and ctx->f_info_ are already set
509     ctx->mb_y_ = dec->mb_y_;
510     ctx->filter_row_ = filter_row;
511     ReconstructRow(dec, ctx);
512     ok = FinishRow(dec, io);
513   } else {
514     WebPWorker* const worker = &dec->worker_;
515     // Finish previous job *before* updating context
516     ok &= WebPGetWorkerInterface()->Sync(worker);
517     assert(worker->status_ == OK);
518     if (ok) {   // spawn a new deblocking/output job
519       ctx->io_ = *io;
520       ctx->id_ = dec->cache_id_;
521       ctx->mb_y_ = dec->mb_y_;
522       ctx->filter_row_ = filter_row;
523       if (dec->mt_method_ == 2) {  // swap macroblock data
524         VP8MBData* const tmp = ctx->mb_data_;
525         ctx->mb_data_ = dec->mb_data_;
526         dec->mb_data_ = tmp;
527       } else {
528         // perform reconstruction directly in main thread
529         ReconstructRow(dec, ctx);
530       }
531       if (filter_row) {            // swap filter info
532         VP8FInfo* const tmp = ctx->f_info_;
533         ctx->f_info_ = dec->f_info_;
534         dec->f_info_ = tmp;
535       }
536       // (reconstruct)+filter in parallel
537       WebPGetWorkerInterface()->Launch(worker);
538       if (++dec->cache_id_ == dec->num_caches_) {
539         dec->cache_id_ = 0;
540       }
541     }
542   }
543   return ok;
544 }
545 
546 //------------------------------------------------------------------------------
547 // Finish setting up the decoding parameter once user's setup() is called.
548 
VP8EnterCritical(VP8Decoder * const dec,VP8Io * const io)549 VP8StatusCode VP8EnterCritical(VP8Decoder* const dec, VP8Io* const io) {
550   // Call setup() first. This may trigger additional decoding features on 'io'.
551   // Note: Afterward, we must call teardown() no matter what.
552   if (io->setup != NULL && !io->setup(io)) {
553     VP8SetError(dec, VP8_STATUS_USER_ABORT, "Frame setup failed");
554     return dec->status_;
555   }
556 
557   // Disable filtering per user request
558   if (io->bypass_filtering) {
559     dec->filter_type_ = 0;
560   }
561 
562   // Define the area where we can skip in-loop filtering, in case of cropping.
563   //
564   // 'Simple' filter reads two luma samples outside of the macroblock
565   // and filters one. It doesn't filter the chroma samples. Hence, we can
566   // avoid doing the in-loop filtering before crop_top/crop_left position.
567   // For the 'Complex' filter, 3 samples are read and up to 3 are filtered.
568   // Means: there's a dependency chain that goes all the way up to the
569   // top-left corner of the picture (MB #0). We must filter all the previous
570   // macroblocks.
571   {
572     const int extra_pixels = kFilterExtraRows[dec->filter_type_];
573     if (dec->filter_type_ == 2) {
574       // For complex filter, we need to preserve the dependency chain.
575       dec->tl_mb_x_ = 0;
576       dec->tl_mb_y_ = 0;
577     } else {
578       // For simple filter, we can filter only the cropped region.
579       // We include 'extra_pixels' on the other side of the boundary, since
580       // vertical or horizontal filtering of the previous macroblock can
581       // modify some abutting pixels.
582       dec->tl_mb_x_ = (io->crop_left - extra_pixels) >> 4;
583       dec->tl_mb_y_ = (io->crop_top - extra_pixels) >> 4;
584       if (dec->tl_mb_x_ < 0) dec->tl_mb_x_ = 0;
585       if (dec->tl_mb_y_ < 0) dec->tl_mb_y_ = 0;
586     }
587     // We need some 'extra' pixels on the right/bottom.
588     dec->br_mb_y_ = (io->crop_bottom + 15 + extra_pixels) >> 4;
589     dec->br_mb_x_ = (io->crop_right + 15 + extra_pixels) >> 4;
590     if (dec->br_mb_x_ > dec->mb_w_) {
591       dec->br_mb_x_ = dec->mb_w_;
592     }
593     if (dec->br_mb_y_ > dec->mb_h_) {
594       dec->br_mb_y_ = dec->mb_h_;
595     }
596   }
597   PrecomputeFilterStrengths(dec);
598   return VP8_STATUS_OK;
599 }
600 
VP8ExitCritical(VP8Decoder * const dec,VP8Io * const io)601 int VP8ExitCritical(VP8Decoder* const dec, VP8Io* const io) {
602   int ok = 1;
603   if (dec->mt_method_ > 0) {
604     ok = WebPGetWorkerInterface()->Sync(&dec->worker_);
605   }
606 
607   if (io->teardown != NULL) {
608     io->teardown(io);
609   }
610   return ok;
611 }
612 
613 //------------------------------------------------------------------------------
614 // For multi-threaded decoding we need to use 3 rows of 16 pixels as delay line.
615 //
616 // Reason is: the deblocking filter cannot deblock the bottom horizontal edges
617 // immediately, and needs to wait for first few rows of the next macroblock to
618 // be decoded. Hence, deblocking is lagging behind by 4 or 8 pixels (depending
619 // on strength).
620 // With two threads, the vertical positions of the rows being decoded are:
621 // Decode:  [ 0..15][16..31][32..47][48..63][64..79][...
622 // Deblock:         [ 0..11][12..27][28..43][44..59][...
623 // If we use two threads and two caches of 16 pixels, the sequence would be:
624 // Decode:  [ 0..15][16..31][ 0..15!!][16..31][ 0..15][...
625 // Deblock:         [ 0..11][12..27!!][-4..11][12..27][...
626 // The problem occurs during row [12..15!!] that both the decoding and
627 // deblocking threads are writing simultaneously.
628 // With 3 cache lines, one get a safe write pattern:
629 // Decode:  [ 0..15][16..31][32..47][ 0..15][16..31][32..47][0..
630 // Deblock:         [ 0..11][12..27][28..43][-4..11][12..27][28...
631 // Note that multi-threaded output _without_ deblocking can make use of two
632 // cache lines of 16 pixels only, since there's no lagging behind. The decoding
633 // and output process have non-concurrent writing:
634 // Decode:  [ 0..15][16..31][ 0..15][16..31][...
635 // io->put:         [ 0..15][16..31][ 0..15][...
636 
637 #define MT_CACHE_LINES 3
638 #define ST_CACHE_LINES 1   // 1 cache row only for single-threaded case
639 
640 // Initialize multi/single-thread worker
InitThreadContext(VP8Decoder * const dec)641 static int InitThreadContext(VP8Decoder* const dec) {
642   dec->cache_id_ = 0;
643   if (dec->mt_method_ > 0) {
644     WebPWorker* const worker = &dec->worker_;
645     if (!WebPGetWorkerInterface()->Reset(worker)) {
646       return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
647                          "thread initialization failed.");
648     }
649     worker->data1 = dec;
650     worker->data2 = (void*)&dec->thread_ctx_.io_;
651     worker->hook = FinishRow;
652     dec->num_caches_ =
653       (dec->filter_type_ > 0) ? MT_CACHE_LINES : MT_CACHE_LINES - 1;
654   } else {
655     dec->num_caches_ = ST_CACHE_LINES;
656   }
657   return 1;
658 }
659 
VP8GetThreadMethod(const WebPDecoderOptions * const options,const WebPHeaderStructure * const headers,int width,int height)660 int VP8GetThreadMethod(const WebPDecoderOptions* const options,
661                        const WebPHeaderStructure* const headers,
662                        int width, int height) {
663   if (options == NULL || options->use_threads == 0) {
664     return 0;
665   }
666   (void)headers;
667   (void)width;
668   (void)height;
669   assert(headers == NULL || !headers->is_lossless);
670 #if defined(WEBP_USE_THREAD)
671   if (width >= MIN_WIDTH_FOR_THREADS) return 2;
672 #endif
673   return 0;
674 }
675 
676 #undef MT_CACHE_LINES
677 #undef ST_CACHE_LINES
678 
679 //------------------------------------------------------------------------------
680 // Memory setup
681 
AllocateMemory(VP8Decoder * const dec)682 static int AllocateMemory(VP8Decoder* const dec) {
683   const int num_caches = dec->num_caches_;
684   const int mb_w = dec->mb_w_;
685   // Note: we use 'size_t' when there's no overflow risk, uint64_t otherwise.
686   const size_t intra_pred_mode_size = 4 * mb_w * sizeof(uint8_t);
687   const size_t top_size = sizeof(VP8TopSamples) * mb_w;
688   const size_t mb_info_size = (mb_w + 1) * sizeof(VP8MB);
689   const size_t f_info_size =
690       (dec->filter_type_ > 0) ?
691           mb_w * (dec->mt_method_ > 0 ? 2 : 1) * sizeof(VP8FInfo)
692         : 0;
693   const size_t yuv_size = YUV_SIZE * sizeof(*dec->yuv_b_);
694   const size_t mb_data_size =
695       (dec->mt_method_ == 2 ? 2 : 1) * mb_w * sizeof(*dec->mb_data_);
696   const size_t cache_height = (16 * num_caches
697                             + kFilterExtraRows[dec->filter_type_]) * 3 / 2;
698   const size_t cache_size = top_size * cache_height;
699   // alpha_size is the only one that scales as width x height.
700   const uint64_t alpha_size = (dec->alpha_data_ != NULL) ?
701       (uint64_t)dec->pic_hdr_.width_ * dec->pic_hdr_.height_ : 0ULL;
702   const uint64_t needed = (uint64_t)intra_pred_mode_size
703                         + top_size + mb_info_size + f_info_size
704                         + yuv_size + mb_data_size
705                         + cache_size + alpha_size + WEBP_ALIGN_CST;
706   uint8_t* mem;
707 
708   if (needed != (size_t)needed) return 0;  // check for overflow
709   if (needed > dec->mem_size_) {
710     WebPSafeFree(dec->mem_);
711     dec->mem_size_ = 0;
712     dec->mem_ = WebPSafeMalloc(needed, sizeof(uint8_t));
713     if (dec->mem_ == NULL) {
714       return VP8SetError(dec, VP8_STATUS_OUT_OF_MEMORY,
715                          "no memory during frame initialization.");
716     }
717     // down-cast is ok, thanks to WebPSafeMalloc() above.
718     dec->mem_size_ = (size_t)needed;
719   }
720 
721   mem = (uint8_t*)dec->mem_;
722   dec->intra_t_ = mem;
723   mem += intra_pred_mode_size;
724 
725   dec->yuv_t_ = (VP8TopSamples*)mem;
726   mem += top_size;
727 
728   dec->mb_info_ = ((VP8MB*)mem) + 1;
729   mem += mb_info_size;
730 
731   dec->f_info_ = f_info_size ? (VP8FInfo*)mem : NULL;
732   mem += f_info_size;
733   dec->thread_ctx_.id_ = 0;
734   dec->thread_ctx_.f_info_ = dec->f_info_;
735   if (dec->filter_type_ > 0 && dec->mt_method_ > 0) {
736     // secondary cache line. The deblocking process need to make use of the
737     // filtering strength from previous macroblock row, while the new ones
738     // are being decoded in parallel. We'll just swap the pointers.
739     dec->thread_ctx_.f_info_ += mb_w;
740   }
741 
742   mem = (uint8_t*)WEBP_ALIGN(mem);
743   assert((yuv_size & WEBP_ALIGN_CST) == 0);
744   dec->yuv_b_ = mem;
745   mem += yuv_size;
746 
747   dec->mb_data_ = (VP8MBData*)mem;
748   dec->thread_ctx_.mb_data_ = (VP8MBData*)mem;
749   if (dec->mt_method_ == 2) {
750     dec->thread_ctx_.mb_data_ += mb_w;
751   }
752   mem += mb_data_size;
753 
754   dec->cache_y_stride_ = 16 * mb_w;
755   dec->cache_uv_stride_ = 8 * mb_w;
756   {
757     const int extra_rows = kFilterExtraRows[dec->filter_type_];
758     const int extra_y = extra_rows * dec->cache_y_stride_;
759     const int extra_uv = (extra_rows / 2) * dec->cache_uv_stride_;
760     dec->cache_y_ = mem + extra_y;
761     dec->cache_u_ = dec->cache_y_
762                   + 16 * num_caches * dec->cache_y_stride_ + extra_uv;
763     dec->cache_v_ = dec->cache_u_
764                   + 8 * num_caches * dec->cache_uv_stride_ + extra_uv;
765     dec->cache_id_ = 0;
766   }
767   mem += cache_size;
768 
769   // alpha plane
770   dec->alpha_plane_ = alpha_size ? mem : NULL;
771   mem += alpha_size;
772   assert(mem <= (uint8_t*)dec->mem_ + dec->mem_size_);
773 
774   // note: left/top-info is initialized once for all.
775   memset(dec->mb_info_ - 1, 0, mb_info_size);
776   VP8InitScanline(dec);   // initialize left too.
777 
778   // initialize top
779   memset(dec->intra_t_, B_DC_PRED, intra_pred_mode_size);
780 
781   return 1;
782 }
783 
InitIo(VP8Decoder * const dec,VP8Io * io)784 static void InitIo(VP8Decoder* const dec, VP8Io* io) {
785   // prepare 'io'
786   io->mb_y = 0;
787   io->y = dec->cache_y_;
788   io->u = dec->cache_u_;
789   io->v = dec->cache_v_;
790   io->y_stride = dec->cache_y_stride_;
791   io->uv_stride = dec->cache_uv_stride_;
792   io->a = NULL;
793 }
794 
VP8InitFrame(VP8Decoder * const dec,VP8Io * const io)795 int VP8InitFrame(VP8Decoder* const dec, VP8Io* const io) {
796   if (!InitThreadContext(dec)) return 0;  // call first. Sets dec->num_caches_.
797   if (!AllocateMemory(dec)) return 0;
798   InitIo(dec, io);
799   VP8DspInit();  // Init critical function pointers and look-up tables.
800   return 1;
801 }
802 
803 //------------------------------------------------------------------------------
804