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