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