Line data Source code
1 : /*
2 : * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : */
10 :
11 : #include <assert.h>
12 : #include <stdlib.h> // qsort()
13 :
14 : #include "./vp9_rtcd.h"
15 : #include "./vpx_dsp_rtcd.h"
16 : #include "./vpx_scale_rtcd.h"
17 :
18 : #include "vpx_dsp/bitreader_buffer.h"
19 : #include "vpx_dsp/bitreader.h"
20 : #include "vpx_dsp/vpx_dsp_common.h"
21 : #include "vpx_mem/vpx_mem.h"
22 : #include "vpx_ports/mem.h"
23 : #include "vpx_ports/mem_ops.h"
24 : #include "vpx_scale/vpx_scale.h"
25 : #include "vpx_util/vpx_thread.h"
26 :
27 : #include "vp9/common/vp9_alloccommon.h"
28 : #include "vp9/common/vp9_common.h"
29 : #include "vp9/common/vp9_entropy.h"
30 : #include "vp9/common/vp9_entropymode.h"
31 : #include "vp9/common/vp9_idct.h"
32 : #include "vp9/common/vp9_thread_common.h"
33 : #include "vp9/common/vp9_pred_common.h"
34 : #include "vp9/common/vp9_quant_common.h"
35 : #include "vp9/common/vp9_reconintra.h"
36 : #include "vp9/common/vp9_reconinter.h"
37 : #include "vp9/common/vp9_seg_common.h"
38 : #include "vp9/common/vp9_tile_common.h"
39 :
40 : #include "vp9/decoder/vp9_decodeframe.h"
41 : #include "vp9/decoder/vp9_detokenize.h"
42 : #include "vp9/decoder/vp9_decodemv.h"
43 : #include "vp9/decoder/vp9_decoder.h"
44 : #include "vp9/decoder/vp9_dsubexp.h"
45 :
46 : #define MAX_VP9_HEADER_SIZE 80
47 :
48 0 : static int is_compound_reference_allowed(const VP9_COMMON *cm) {
49 : int i;
50 0 : for (i = 1; i < REFS_PER_FRAME; ++i)
51 0 : if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) return 1;
52 :
53 0 : return 0;
54 : }
55 :
56 0 : static void setup_compound_reference_mode(VP9_COMMON *cm) {
57 0 : if (cm->ref_frame_sign_bias[LAST_FRAME] ==
58 0 : cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
59 0 : cm->comp_fixed_ref = ALTREF_FRAME;
60 0 : cm->comp_var_ref[0] = LAST_FRAME;
61 0 : cm->comp_var_ref[1] = GOLDEN_FRAME;
62 0 : } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
63 0 : cm->ref_frame_sign_bias[ALTREF_FRAME]) {
64 0 : cm->comp_fixed_ref = GOLDEN_FRAME;
65 0 : cm->comp_var_ref[0] = LAST_FRAME;
66 0 : cm->comp_var_ref[1] = ALTREF_FRAME;
67 : } else {
68 0 : cm->comp_fixed_ref = LAST_FRAME;
69 0 : cm->comp_var_ref[0] = GOLDEN_FRAME;
70 0 : cm->comp_var_ref[1] = ALTREF_FRAME;
71 : }
72 0 : }
73 :
74 0 : static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
75 0 : return len != 0 && len <= (size_t)(end - start);
76 : }
77 :
78 0 : static int decode_unsigned_max(struct vpx_read_bit_buffer *rb, int max) {
79 0 : const int data = vpx_rb_read_literal(rb, get_unsigned_bits(max));
80 0 : return data > max ? max : data;
81 : }
82 :
83 0 : static TX_MODE read_tx_mode(vpx_reader *r) {
84 0 : TX_MODE tx_mode = vpx_read_literal(r, 2);
85 0 : if (tx_mode == ALLOW_32X32) tx_mode += vpx_read_bit(r);
86 0 : return tx_mode;
87 : }
88 :
89 0 : static void read_tx_mode_probs(struct tx_probs *tx_probs, vpx_reader *r) {
90 : int i, j;
91 :
92 0 : for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
93 0 : for (j = 0; j < TX_SIZES - 3; ++j)
94 0 : vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
95 :
96 0 : for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
97 0 : for (j = 0; j < TX_SIZES - 2; ++j)
98 0 : vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
99 :
100 0 : for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
101 0 : for (j = 0; j < TX_SIZES - 1; ++j)
102 0 : vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
103 0 : }
104 :
105 0 : static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
106 : int i, j;
107 0 : for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
108 0 : for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
109 0 : vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
110 0 : }
111 :
112 0 : static void read_inter_mode_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
113 : int i, j;
114 0 : for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
115 0 : for (j = 0; j < INTER_MODES - 1; ++j)
116 0 : vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
117 0 : }
118 :
119 0 : static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
120 : vpx_reader *r) {
121 0 : if (is_compound_reference_allowed(cm)) {
122 0 : return vpx_read_bit(r)
123 0 : ? (vpx_read_bit(r) ? REFERENCE_MODE_SELECT : COMPOUND_REFERENCE)
124 0 : : SINGLE_REFERENCE;
125 : } else {
126 0 : return SINGLE_REFERENCE;
127 : }
128 : }
129 :
130 0 : static void read_frame_reference_mode_probs(VP9_COMMON *cm, vpx_reader *r) {
131 0 : FRAME_CONTEXT *const fc = cm->fc;
132 : int i;
133 :
134 0 : if (cm->reference_mode == REFERENCE_MODE_SELECT)
135 0 : for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
136 0 : vp9_diff_update_prob(r, &fc->comp_inter_prob[i]);
137 :
138 0 : if (cm->reference_mode != COMPOUND_REFERENCE)
139 0 : for (i = 0; i < REF_CONTEXTS; ++i) {
140 0 : vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]);
141 0 : vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]);
142 : }
143 :
144 0 : if (cm->reference_mode != SINGLE_REFERENCE)
145 0 : for (i = 0; i < REF_CONTEXTS; ++i)
146 0 : vp9_diff_update_prob(r, &fc->comp_ref_prob[i]);
147 0 : }
148 :
149 0 : static void update_mv_probs(vpx_prob *p, int n, vpx_reader *r) {
150 : int i;
151 0 : for (i = 0; i < n; ++i)
152 0 : if (vpx_read(r, MV_UPDATE_PROB)) p[i] = (vpx_read_literal(r, 7) << 1) | 1;
153 0 : }
154 :
155 0 : static void read_mv_probs(nmv_context *ctx, int allow_hp, vpx_reader *r) {
156 : int i, j;
157 :
158 0 : update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
159 :
160 0 : for (i = 0; i < 2; ++i) {
161 0 : nmv_component *const comp_ctx = &ctx->comps[i];
162 0 : update_mv_probs(&comp_ctx->sign, 1, r);
163 0 : update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
164 0 : update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
165 0 : update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
166 : }
167 :
168 0 : for (i = 0; i < 2; ++i) {
169 0 : nmv_component *const comp_ctx = &ctx->comps[i];
170 0 : for (j = 0; j < CLASS0_SIZE; ++j)
171 0 : update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
172 0 : update_mv_probs(comp_ctx->fp, 3, r);
173 : }
174 :
175 0 : if (allow_hp) {
176 0 : for (i = 0; i < 2; ++i) {
177 0 : nmv_component *const comp_ctx = &ctx->comps[i];
178 0 : update_mv_probs(&comp_ctx->class0_hp, 1, r);
179 0 : update_mv_probs(&comp_ctx->hp, 1, r);
180 : }
181 : }
182 0 : }
183 :
184 0 : static void inverse_transform_block_inter(MACROBLOCKD *xd, int plane,
185 : const TX_SIZE tx_size, uint8_t *dst,
186 : int stride, int eob) {
187 0 : struct macroblockd_plane *const pd = &xd->plane[plane];
188 0 : tran_low_t *const dqcoeff = pd->dqcoeff;
189 0 : assert(eob > 0);
190 : #if CONFIG_VP9_HIGHBITDEPTH
191 : if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
192 : if (xd->lossless) {
193 : vp9_highbd_iwht4x4_add(dqcoeff, dst, stride, eob, xd->bd);
194 : } else {
195 : switch (tx_size) {
196 : case TX_4X4:
197 : vp9_highbd_idct4x4_add(dqcoeff, dst, stride, eob, xd->bd);
198 : break;
199 : case TX_8X8:
200 : vp9_highbd_idct8x8_add(dqcoeff, dst, stride, eob, xd->bd);
201 : break;
202 : case TX_16X16:
203 : vp9_highbd_idct16x16_add(dqcoeff, dst, stride, eob, xd->bd);
204 : break;
205 : case TX_32X32:
206 : vp9_highbd_idct32x32_add(dqcoeff, dst, stride, eob, xd->bd);
207 : break;
208 : default: assert(0 && "Invalid transform size");
209 : }
210 : }
211 : } else {
212 : if (xd->lossless) {
213 : vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
214 : } else {
215 : switch (tx_size) {
216 : case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break;
217 : case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break;
218 : case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break;
219 : case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
220 : default: assert(0 && "Invalid transform size"); return;
221 : }
222 : }
223 : }
224 : #else
225 0 : if (xd->lossless) {
226 0 : vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
227 : } else {
228 0 : switch (tx_size) {
229 0 : case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break;
230 0 : case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break;
231 0 : case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break;
232 0 : case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
233 0 : default: assert(0 && "Invalid transform size"); return;
234 : }
235 : }
236 : #endif // CONFIG_VP9_HIGHBITDEPTH
237 :
238 0 : if (eob == 1) {
239 0 : dqcoeff[0] = 0;
240 : } else {
241 0 : if (tx_size <= TX_16X16 && eob <= 10)
242 0 : memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
243 0 : else if (tx_size == TX_32X32 && eob <= 34)
244 0 : memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
245 : else
246 0 : memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
247 : }
248 0 : }
249 :
250 0 : static void inverse_transform_block_intra(MACROBLOCKD *xd, int plane,
251 : const TX_TYPE tx_type,
252 : const TX_SIZE tx_size, uint8_t *dst,
253 : int stride, int eob) {
254 0 : struct macroblockd_plane *const pd = &xd->plane[plane];
255 0 : tran_low_t *const dqcoeff = pd->dqcoeff;
256 0 : assert(eob > 0);
257 : #if CONFIG_VP9_HIGHBITDEPTH
258 : if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
259 : if (xd->lossless) {
260 : vp9_highbd_iwht4x4_add(dqcoeff, dst, stride, eob, xd->bd);
261 : } else {
262 : switch (tx_size) {
263 : case TX_4X4:
264 : vp9_highbd_iht4x4_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
265 : break;
266 : case TX_8X8:
267 : vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
268 : break;
269 : case TX_16X16:
270 : vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
271 : break;
272 : case TX_32X32:
273 : vp9_highbd_idct32x32_add(dqcoeff, dst, stride, eob, xd->bd);
274 : break;
275 : default: assert(0 && "Invalid transform size");
276 : }
277 : }
278 : } else {
279 : if (xd->lossless) {
280 : vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
281 : } else {
282 : switch (tx_size) {
283 : case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break;
284 : case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break;
285 : case TX_16X16:
286 : vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
287 : break;
288 : case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
289 : default: assert(0 && "Invalid transform size"); return;
290 : }
291 : }
292 : }
293 : #else
294 0 : if (xd->lossless) {
295 0 : vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
296 : } else {
297 0 : switch (tx_size) {
298 0 : case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break;
299 0 : case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break;
300 : case TX_16X16:
301 0 : vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
302 0 : break;
303 0 : case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
304 0 : default: assert(0 && "Invalid transform size"); return;
305 : }
306 : }
307 : #endif // CONFIG_VP9_HIGHBITDEPTH
308 :
309 0 : if (eob == 1) {
310 0 : dqcoeff[0] = 0;
311 : } else {
312 0 : if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
313 0 : memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
314 0 : else if (tx_size == TX_32X32 && eob <= 34)
315 0 : memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
316 : else
317 0 : memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
318 : }
319 0 : }
320 :
321 0 : static void predict_and_reconstruct_intra_block(TileWorkerData *twd,
322 : MODE_INFO *const mi, int plane,
323 : int row, int col,
324 : TX_SIZE tx_size) {
325 0 : MACROBLOCKD *const xd = &twd->xd;
326 0 : struct macroblockd_plane *const pd = &xd->plane[plane];
327 0 : PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
328 : uint8_t *dst;
329 0 : dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
330 :
331 0 : if (mi->sb_type < BLOCK_8X8)
332 0 : if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
333 :
334 0 : vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride,
335 : dst, pd->dst.stride, col, row, plane);
336 :
337 0 : if (!mi->skip) {
338 0 : const TX_TYPE tx_type =
339 0 : (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
340 0 : const scan_order *sc = (plane || xd->lossless)
341 0 : ? &vp9_default_scan_orders[tx_size]
342 0 : : &vp9_scan_orders[tx_size][tx_type];
343 0 : const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
344 0 : mi->segment_id);
345 0 : if (eob > 0) {
346 0 : inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
347 : pd->dst.stride, eob);
348 : }
349 : }
350 0 : }
351 :
352 0 : static int reconstruct_inter_block(TileWorkerData *twd, MODE_INFO *const mi,
353 : int plane, int row, int col,
354 : TX_SIZE tx_size) {
355 0 : MACROBLOCKD *const xd = &twd->xd;
356 0 : struct macroblockd_plane *const pd = &xd->plane[plane];
357 0 : const scan_order *sc = &vp9_default_scan_orders[tx_size];
358 0 : const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
359 0 : mi->segment_id);
360 :
361 0 : if (eob > 0) {
362 0 : inverse_transform_block_inter(
363 0 : xd, plane, tx_size, &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
364 : pd->dst.stride, eob);
365 : }
366 0 : return eob;
367 : }
368 :
369 0 : static void build_mc_border(const uint8_t *src, int src_stride, uint8_t *dst,
370 : int dst_stride, int x, int y, int b_w, int b_h,
371 : int w, int h) {
372 : // Get a pointer to the start of the real data for this row.
373 0 : const uint8_t *ref_row = src - x - y * src_stride;
374 :
375 0 : if (y >= h)
376 0 : ref_row += (h - 1) * src_stride;
377 0 : else if (y > 0)
378 0 : ref_row += y * src_stride;
379 :
380 : do {
381 0 : int right = 0, copy;
382 0 : int left = x < 0 ? -x : 0;
383 :
384 0 : if (left > b_w) left = b_w;
385 :
386 0 : if (x + b_w > w) right = x + b_w - w;
387 :
388 0 : if (right > b_w) right = b_w;
389 :
390 0 : copy = b_w - left - right;
391 :
392 0 : if (left) memset(dst, ref_row[0], left);
393 :
394 0 : if (copy) memcpy(dst + left, ref_row + x + left, copy);
395 :
396 0 : if (right) memset(dst + left + copy, ref_row[w - 1], right);
397 :
398 0 : dst += dst_stride;
399 0 : ++y;
400 :
401 0 : if (y > 0 && y < h) ref_row += src_stride;
402 0 : } while (--b_h);
403 0 : }
404 :
405 : #if CONFIG_VP9_HIGHBITDEPTH
406 : static void high_build_mc_border(const uint8_t *src8, int src_stride,
407 : uint16_t *dst, int dst_stride, int x, int y,
408 : int b_w, int b_h, int w, int h) {
409 : // Get a pointer to the start of the real data for this row.
410 : const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
411 : const uint16_t *ref_row = src - x - y * src_stride;
412 :
413 : if (y >= h)
414 : ref_row += (h - 1) * src_stride;
415 : else if (y > 0)
416 : ref_row += y * src_stride;
417 :
418 : do {
419 : int right = 0, copy;
420 : int left = x < 0 ? -x : 0;
421 :
422 : if (left > b_w) left = b_w;
423 :
424 : if (x + b_w > w) right = x + b_w - w;
425 :
426 : if (right > b_w) right = b_w;
427 :
428 : copy = b_w - left - right;
429 :
430 : if (left) vpx_memset16(dst, ref_row[0], left);
431 :
432 : if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
433 :
434 : if (right) vpx_memset16(dst + left + copy, ref_row[w - 1], right);
435 :
436 : dst += dst_stride;
437 : ++y;
438 :
439 : if (y > 0 && y < h) ref_row += src_stride;
440 : } while (--b_h);
441 : }
442 : #endif // CONFIG_VP9_HIGHBITDEPTH
443 :
444 : #if CONFIG_VP9_HIGHBITDEPTH
445 : static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
446 : int x0, int y0, int b_w, int b_h,
447 : int frame_width, int frame_height,
448 : int border_offset, uint8_t *const dst,
449 : int dst_buf_stride, int subpel_x, int subpel_y,
450 : const InterpKernel *kernel,
451 : const struct scale_factors *sf, MACROBLOCKD *xd,
452 : int w, int h, int ref, int xs, int ys) {
453 : DECLARE_ALIGNED(16, uint16_t, mc_buf_high[80 * 2 * 80 * 2]);
454 : const uint8_t *buf_ptr;
455 :
456 : if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
457 : high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w, x0, y0,
458 : b_w, b_h, frame_width, frame_height);
459 : buf_ptr = CONVERT_TO_BYTEPTR(mc_buf_high) + border_offset;
460 : } else {
461 : build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w, x0,
462 : y0, b_w, b_h, frame_width, frame_height);
463 : buf_ptr = ((uint8_t *)mc_buf_high) + border_offset;
464 : }
465 :
466 : if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
467 : highbd_inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
468 : subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
469 : } else {
470 : inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x, subpel_y, sf,
471 : w, h, ref, kernel, xs, ys);
472 : }
473 : }
474 : #else
475 0 : static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
476 : int x0, int y0, int b_w, int b_h,
477 : int frame_width, int frame_height,
478 : int border_offset, uint8_t *const dst,
479 : int dst_buf_stride, int subpel_x, int subpel_y,
480 : const InterpKernel *kernel,
481 : const struct scale_factors *sf, int w, int h,
482 : int ref, int xs, int ys) {
483 : DECLARE_ALIGNED(16, uint8_t, mc_buf[80 * 2 * 80 * 2]);
484 : const uint8_t *buf_ptr;
485 :
486 0 : build_mc_border(buf_ptr1, pre_buf_stride, mc_buf, b_w, x0, y0, b_w, b_h,
487 : frame_width, frame_height);
488 0 : buf_ptr = mc_buf + border_offset;
489 :
490 0 : inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x, subpel_y, sf, w,
491 : h, ref, kernel, xs, ys);
492 0 : }
493 : #endif // CONFIG_VP9_HIGHBITDEPTH
494 :
495 0 : static void dec_build_inter_predictors(
496 : VPxWorker *const worker, MACROBLOCKD *xd, int plane, int bw, int bh, int x,
497 : int y, int w, int h, int mi_x, int mi_y, const InterpKernel *kernel,
498 : const struct scale_factors *sf, struct buf_2d *pre_buf,
499 : struct buf_2d *dst_buf, const MV *mv, RefCntBuffer *ref_frame_buf,
500 : int is_scaled, int ref) {
501 0 : struct macroblockd_plane *const pd = &xd->plane[plane];
502 0 : uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
503 : MV32 scaled_mv;
504 : int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
505 : subpel_x, subpel_y;
506 : uint8_t *ref_frame, *buf_ptr;
507 :
508 : // Get reference frame pointer, width and height.
509 0 : if (plane == 0) {
510 0 : frame_width = ref_frame_buf->buf.y_crop_width;
511 0 : frame_height = ref_frame_buf->buf.y_crop_height;
512 0 : ref_frame = ref_frame_buf->buf.y_buffer;
513 : } else {
514 0 : frame_width = ref_frame_buf->buf.uv_crop_width;
515 0 : frame_height = ref_frame_buf->buf.uv_crop_height;
516 0 : ref_frame =
517 0 : plane == 1 ? ref_frame_buf->buf.u_buffer : ref_frame_buf->buf.v_buffer;
518 : }
519 :
520 0 : if (is_scaled) {
521 0 : const MV mv_q4 = clamp_mv_to_umv_border_sb(
522 : xd, mv, bw, bh, pd->subsampling_x, pd->subsampling_y);
523 : // Co-ordinate of containing block to pixel precision.
524 0 : int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
525 0 : int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
526 : #if 0 // CONFIG_BETTER_HW_COMPATIBILITY
527 : assert(xd->mi[0]->sb_type != BLOCK_4X8 &&
528 : xd->mi[0]->sb_type != BLOCK_8X4);
529 : assert(mv_q4.row == mv->row * (1 << (1 - pd->subsampling_y)) &&
530 : mv_q4.col == mv->col * (1 << (1 - pd->subsampling_x)));
531 : #endif
532 : // Co-ordinate of the block to 1/16th pixel precision.
533 0 : x0_16 = (x_start + x) << SUBPEL_BITS;
534 0 : y0_16 = (y_start + y) << SUBPEL_BITS;
535 :
536 : // Co-ordinate of current block in reference frame
537 : // to 1/16th pixel precision.
538 0 : x0_16 = sf->scale_value_x(x0_16, sf);
539 0 : y0_16 = sf->scale_value_y(y0_16, sf);
540 :
541 : // Map the top left corner of the block into the reference frame.
542 0 : x0 = sf->scale_value_x(x_start + x, sf);
543 0 : y0 = sf->scale_value_y(y_start + y, sf);
544 :
545 : // Scale the MV and incorporate the sub-pixel offset of the block
546 : // in the reference frame.
547 0 : scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
548 0 : xs = sf->x_step_q4;
549 0 : ys = sf->y_step_q4;
550 : } else {
551 : // Co-ordinate of containing block to pixel precision.
552 0 : x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
553 0 : y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
554 :
555 : // Co-ordinate of the block to 1/16th pixel precision.
556 0 : x0_16 = x0 << SUBPEL_BITS;
557 0 : y0_16 = y0 << SUBPEL_BITS;
558 :
559 0 : scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y));
560 0 : scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x));
561 0 : xs = ys = 16;
562 : }
563 0 : subpel_x = scaled_mv.col & SUBPEL_MASK;
564 0 : subpel_y = scaled_mv.row & SUBPEL_MASK;
565 :
566 : // Calculate the top left corner of the best matching block in the
567 : // reference frame.
568 0 : x0 += scaled_mv.col >> SUBPEL_BITS;
569 0 : y0 += scaled_mv.row >> SUBPEL_BITS;
570 0 : x0_16 += scaled_mv.col;
571 0 : y0_16 += scaled_mv.row;
572 :
573 : // Get reference block pointer.
574 0 : buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
575 0 : buf_stride = pre_buf->stride;
576 :
577 : // Do border extension if there is motion or the
578 : // width/height is not a multiple of 8 pixels.
579 0 : if (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
580 0 : (frame_height & 0x7)) {
581 0 : int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
582 :
583 : // Get reference block bottom right horizontal coordinate.
584 0 : int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
585 0 : int x_pad = 0, y_pad = 0;
586 :
587 0 : if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
588 0 : x0 -= VP9_INTERP_EXTEND - 1;
589 0 : x1 += VP9_INTERP_EXTEND;
590 0 : x_pad = 1;
591 : }
592 :
593 0 : if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
594 0 : y0 -= VP9_INTERP_EXTEND - 1;
595 0 : y1 += VP9_INTERP_EXTEND;
596 0 : y_pad = 1;
597 : }
598 :
599 : // Wait until reference block is ready. Pad 7 more pixels as last 7
600 : // pixels of each superblock row can be changed by next superblock row.
601 0 : if (worker != NULL)
602 0 : vp9_frameworker_wait(worker, ref_frame_buf, VPXMAX(0, (y1 + 7))
603 0 : << (plane == 0 ? 0 : 1));
604 :
605 : // Skip border extension if block is inside the frame.
606 0 : if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
607 0 : y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
608 : // Extend the border.
609 0 : const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0;
610 0 : const int b_w = x1 - x0 + 1;
611 0 : const int b_h = y1 - y0 + 1;
612 0 : const int border_offset = y_pad * 3 * b_w + x_pad * 3;
613 :
614 0 : extend_and_predict(buf_ptr1, buf_stride, x0, y0, b_w, b_h, frame_width,
615 : frame_height, border_offset, dst, dst_buf->stride,
616 : subpel_x, subpel_y, kernel, sf,
617 : #if CONFIG_VP9_HIGHBITDEPTH
618 : xd,
619 : #endif
620 : w, h, ref, xs, ys);
621 0 : return;
622 : }
623 : } else {
624 : // Wait until reference block is ready. Pad 7 more pixels as last 7
625 : // pixels of each superblock row can be changed by next superblock row.
626 0 : if (worker != NULL) {
627 0 : const int y1 = (y0_16 + (h - 1) * ys) >> SUBPEL_BITS;
628 0 : vp9_frameworker_wait(worker, ref_frame_buf, VPXMAX(0, (y1 + 7))
629 0 : << (plane == 0 ? 0 : 1));
630 : }
631 : }
632 : #if CONFIG_VP9_HIGHBITDEPTH
633 : if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
634 : highbd_inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
635 : subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
636 : } else {
637 : inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
638 : subpel_y, sf, w, h, ref, kernel, xs, ys);
639 : }
640 : #else
641 0 : inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x, subpel_y,
642 : sf, w, h, ref, kernel, xs, ys);
643 : #endif // CONFIG_VP9_HIGHBITDEPTH
644 : }
645 :
646 0 : static void dec_build_inter_predictors_sb(VP9Decoder *const pbi,
647 : MACROBLOCKD *xd, int mi_row,
648 : int mi_col) {
649 : int plane;
650 0 : const int mi_x = mi_col * MI_SIZE;
651 0 : const int mi_y = mi_row * MI_SIZE;
652 0 : const MODE_INFO *mi = xd->mi[0];
653 0 : const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
654 0 : const BLOCK_SIZE sb_type = mi->sb_type;
655 0 : const int is_compound = has_second_ref(mi);
656 : int ref;
657 : int is_scaled;
658 0 : VPxWorker *const fwo =
659 0 : pbi->frame_parallel_decode ? pbi->frame_worker_owner : NULL;
660 :
661 0 : for (ref = 0; ref < 1 + is_compound; ++ref) {
662 0 : const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
663 0 : RefBuffer *ref_buf = &pbi->common.frame_refs[frame - LAST_FRAME];
664 0 : const struct scale_factors *const sf = &ref_buf->sf;
665 0 : const int idx = ref_buf->idx;
666 0 : BufferPool *const pool = pbi->common.buffer_pool;
667 0 : RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx];
668 :
669 0 : if (!vp9_is_valid_scale(sf))
670 0 : vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
671 : "Reference frame has invalid dimensions");
672 :
673 0 : is_scaled = vp9_is_scaled(sf);
674 0 : vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
675 : is_scaled ? sf : NULL);
676 0 : xd->block_refs[ref] = ref_buf;
677 :
678 0 : if (sb_type < BLOCK_8X8) {
679 0 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
680 0 : struct macroblockd_plane *const pd = &xd->plane[plane];
681 0 : struct buf_2d *const dst_buf = &pd->dst;
682 0 : const int num_4x4_w = pd->n4_w;
683 0 : const int num_4x4_h = pd->n4_h;
684 0 : const int n4w_x4 = 4 * num_4x4_w;
685 0 : const int n4h_x4 = 4 * num_4x4_h;
686 0 : struct buf_2d *const pre_buf = &pd->pre[ref];
687 0 : int i = 0, x, y;
688 0 : for (y = 0; y < num_4x4_h; ++y) {
689 0 : for (x = 0; x < num_4x4_w; ++x) {
690 0 : const MV mv = average_split_mvs(pd, mi, ref, i++);
691 0 : dec_build_inter_predictors(fwo, xd, plane, n4w_x4, n4h_x4, 4 * x,
692 : 4 * y, 4, 4, mi_x, mi_y, kernel, sf,
693 : pre_buf, dst_buf, &mv, ref_frame_buf,
694 : is_scaled, ref);
695 : }
696 : }
697 : }
698 : } else {
699 0 : const MV mv = mi->mv[ref].as_mv;
700 0 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
701 0 : struct macroblockd_plane *const pd = &xd->plane[plane];
702 0 : struct buf_2d *const dst_buf = &pd->dst;
703 0 : const int num_4x4_w = pd->n4_w;
704 0 : const int num_4x4_h = pd->n4_h;
705 0 : const int n4w_x4 = 4 * num_4x4_w;
706 0 : const int n4h_x4 = 4 * num_4x4_h;
707 0 : struct buf_2d *const pre_buf = &pd->pre[ref];
708 0 : dec_build_inter_predictors(fwo, xd, plane, n4w_x4, n4h_x4, 0, 0, n4w_x4,
709 : n4h_x4, mi_x, mi_y, kernel, sf, pre_buf,
710 : dst_buf, &mv, ref_frame_buf, is_scaled, ref);
711 : }
712 : }
713 : }
714 0 : }
715 :
716 0 : static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
717 : int i;
718 0 : for (i = 0; i < MAX_MB_PLANE; i++) {
719 0 : struct macroblockd_plane *const pd = &xd->plane[i];
720 0 : memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
721 0 : memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
722 : }
723 0 : }
724 :
725 0 : static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
726 : int bhl) {
727 : int i;
728 0 : for (i = 0; i < MAX_MB_PLANE; i++) {
729 0 : xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
730 0 : xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
731 0 : xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
732 0 : xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
733 : }
734 0 : }
735 :
736 0 : static MODE_INFO *set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd,
737 : BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
738 : int bh, int x_mis, int y_mis, int bwl, int bhl) {
739 0 : const int offset = mi_row * cm->mi_stride + mi_col;
740 : int x, y;
741 0 : const TileInfo *const tile = &xd->tile;
742 :
743 0 : xd->mi = cm->mi_grid_visible + offset;
744 0 : xd->mi[0] = &cm->mi[offset];
745 : // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
746 : // passing bsize from decode_partition().
747 0 : xd->mi[0]->sb_type = bsize;
748 0 : for (y = 0; y < y_mis; ++y)
749 0 : for (x = !y; x < x_mis; ++x) {
750 0 : xd->mi[y * cm->mi_stride + x] = xd->mi[0];
751 : }
752 :
753 0 : set_plane_n4(xd, bw, bh, bwl, bhl);
754 :
755 0 : set_skip_context(xd, mi_row, mi_col);
756 :
757 : // Distance of Mb to the various image edges. These are specified to 8th pel
758 : // as they are always compared to values that are in 1/8th pel units
759 0 : set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
760 :
761 0 : vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
762 0 : return xd->mi[0];
763 : }
764 :
765 0 : static void decode_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
766 : int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
767 0 : VP9_COMMON *const cm = &pbi->common;
768 0 : const int less8x8 = bsize < BLOCK_8X8;
769 0 : const int bw = 1 << (bwl - 1);
770 0 : const int bh = 1 << (bhl - 1);
771 0 : const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
772 0 : const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
773 0 : vpx_reader *r = &twd->bit_reader;
774 0 : MACROBLOCKD *const xd = &twd->xd;
775 :
776 0 : MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
777 : y_mis, bwl, bhl);
778 :
779 0 : if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
780 0 : const BLOCK_SIZE uv_subsize =
781 0 : ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
782 0 : if (uv_subsize == BLOCK_INVALID)
783 0 : vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
784 : "Invalid block size.");
785 : }
786 :
787 0 : vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis);
788 :
789 0 : if (mi->skip) {
790 0 : dec_reset_skip_context(xd);
791 : }
792 :
793 0 : if (!is_inter_block(mi)) {
794 : int plane;
795 0 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
796 0 : const struct macroblockd_plane *const pd = &xd->plane[plane];
797 0 : const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
798 0 : const int num_4x4_w = pd->n4_w;
799 0 : const int num_4x4_h = pd->n4_h;
800 0 : const int step = (1 << tx_size);
801 : int row, col;
802 0 : const int max_blocks_wide =
803 0 : num_4x4_w + (xd->mb_to_right_edge >= 0
804 : ? 0
805 0 : : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
806 0 : const int max_blocks_high =
807 0 : num_4x4_h + (xd->mb_to_bottom_edge >= 0
808 : ? 0
809 0 : : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
810 :
811 0 : xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
812 0 : xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
813 :
814 0 : for (row = 0; row < max_blocks_high; row += step)
815 0 : for (col = 0; col < max_blocks_wide; col += step)
816 0 : predict_and_reconstruct_intra_block(twd, mi, plane, row, col,
817 : tx_size);
818 : }
819 : } else {
820 : // Prediction
821 0 : dec_build_inter_predictors_sb(pbi, xd, mi_row, mi_col);
822 :
823 : // Reconstruction
824 0 : if (!mi->skip) {
825 0 : int eobtotal = 0;
826 : int plane;
827 :
828 0 : for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
829 0 : const struct macroblockd_plane *const pd = &xd->plane[plane];
830 0 : const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
831 0 : const int num_4x4_w = pd->n4_w;
832 0 : const int num_4x4_h = pd->n4_h;
833 0 : const int step = (1 << tx_size);
834 : int row, col;
835 0 : const int max_blocks_wide =
836 0 : num_4x4_w + (xd->mb_to_right_edge >= 0
837 : ? 0
838 0 : : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
839 0 : const int max_blocks_high =
840 : num_4x4_h +
841 0 : (xd->mb_to_bottom_edge >= 0 ? 0 : xd->mb_to_bottom_edge >>
842 0 : (5 + pd->subsampling_y));
843 :
844 0 : xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
845 0 : xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
846 :
847 0 : for (row = 0; row < max_blocks_high; row += step)
848 0 : for (col = 0; col < max_blocks_wide; col += step)
849 0 : eobtotal +=
850 0 : reconstruct_inter_block(twd, mi, plane, row, col, tx_size);
851 : }
852 :
853 0 : if (!less8x8 && eobtotal == 0) mi->skip = 1; // skip loopfilter
854 : }
855 : }
856 :
857 0 : xd->corrupted |= vpx_reader_has_error(r);
858 :
859 0 : if (cm->lf.filter_level) {
860 0 : vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
861 : }
862 0 : }
863 :
864 0 : static INLINE int dec_partition_plane_context(TileWorkerData *twd, int mi_row,
865 : int mi_col, int bsl) {
866 0 : const PARTITION_CONTEXT *above_ctx = twd->xd.above_seg_context + mi_col;
867 0 : const PARTITION_CONTEXT *left_ctx =
868 0 : twd->xd.left_seg_context + (mi_row & MI_MASK);
869 0 : int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
870 :
871 : // assert(bsl >= 0);
872 :
873 0 : return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
874 : }
875 :
876 0 : static INLINE void dec_update_partition_context(TileWorkerData *twd, int mi_row,
877 : int mi_col, BLOCK_SIZE subsize,
878 : int bw) {
879 0 : PARTITION_CONTEXT *const above_ctx = twd->xd.above_seg_context + mi_col;
880 0 : PARTITION_CONTEXT *const left_ctx =
881 0 : twd->xd.left_seg_context + (mi_row & MI_MASK);
882 :
883 : // update the partition context at the end notes. set partition bits
884 : // of block sizes larger than the current one to be one, and partition
885 : // bits of smaller block sizes to be zero.
886 0 : memset(above_ctx, partition_context_lookup[subsize].above, bw);
887 0 : memset(left_ctx, partition_context_lookup[subsize].left, bw);
888 0 : }
889 :
890 0 : static PARTITION_TYPE read_partition(TileWorkerData *twd, int mi_row,
891 : int mi_col, int has_rows, int has_cols,
892 : int bsl) {
893 0 : const int ctx = dec_partition_plane_context(twd, mi_row, mi_col, bsl);
894 0 : const vpx_prob *const probs = twd->xd.partition_probs[ctx];
895 0 : FRAME_COUNTS *counts = twd->xd.counts;
896 : PARTITION_TYPE p;
897 0 : vpx_reader *r = &twd->bit_reader;
898 :
899 0 : if (has_rows && has_cols)
900 0 : p = (PARTITION_TYPE)vpx_read_tree(r, vp9_partition_tree, probs);
901 0 : else if (!has_rows && has_cols)
902 0 : p = vpx_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
903 0 : else if (has_rows && !has_cols)
904 0 : p = vpx_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
905 : else
906 0 : p = PARTITION_SPLIT;
907 :
908 0 : if (counts) ++counts->partition[ctx][p];
909 :
910 0 : return p;
911 : }
912 :
913 : // TODO(slavarnway): eliminate bsize and subsize in future commits
914 0 : static void decode_partition(TileWorkerData *twd, VP9Decoder *const pbi,
915 : int mi_row, int mi_col, BLOCK_SIZE bsize,
916 : int n4x4_l2) {
917 0 : VP9_COMMON *const cm = &pbi->common;
918 0 : const int n8x8_l2 = n4x4_l2 - 1;
919 0 : const int num_8x8_wh = 1 << n8x8_l2;
920 0 : const int hbs = num_8x8_wh >> 1;
921 : PARTITION_TYPE partition;
922 : BLOCK_SIZE subsize;
923 0 : const int has_rows = (mi_row + hbs) < cm->mi_rows;
924 0 : const int has_cols = (mi_col + hbs) < cm->mi_cols;
925 0 : MACROBLOCKD *const xd = &twd->xd;
926 :
927 0 : if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
928 :
929 0 : partition = read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2);
930 0 : subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
931 0 : if (!hbs) {
932 : // calculate bmode block dimensions (log 2)
933 0 : xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
934 0 : xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
935 0 : decode_block(twd, pbi, mi_row, mi_col, subsize, 1, 1);
936 : } else {
937 0 : switch (partition) {
938 : case PARTITION_NONE:
939 0 : decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2);
940 0 : break;
941 : case PARTITION_HORZ:
942 0 : decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2);
943 0 : if (has_rows)
944 0 : decode_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2,
945 : n8x8_l2);
946 0 : break;
947 : case PARTITION_VERT:
948 0 : decode_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2);
949 0 : if (has_cols)
950 0 : decode_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
951 : n4x4_l2);
952 0 : break;
953 : case PARTITION_SPLIT:
954 0 : decode_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2);
955 0 : decode_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2);
956 0 : decode_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2);
957 0 : decode_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize,
958 : n8x8_l2);
959 0 : break;
960 0 : default: assert(0 && "Invalid partition type");
961 : }
962 : }
963 :
964 : // update partition context
965 0 : if (bsize >= BLOCK_8X8 &&
966 0 : (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
967 0 : dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh);
968 : }
969 :
970 0 : static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end,
971 : size_t read_size,
972 : struct vpx_internal_error_info *error_info,
973 : vpx_reader *r, vpx_decrypt_cb decrypt_cb,
974 : void *decrypt_state) {
975 : // Validate the calculated partition length. If the buffer
976 : // described by the partition can't be fully read, then restrict
977 : // it to the portion that can be (for EC mode) or throw an error.
978 0 : if (!read_is_valid(data, read_size, data_end))
979 0 : vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
980 : "Truncated packet or corrupt tile length");
981 :
982 0 : if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
983 0 : vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
984 : "Failed to allocate bool decoder %d", 1);
985 0 : }
986 :
987 0 : static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
988 : vpx_reader *r) {
989 : int i, j, k, l, m;
990 :
991 0 : if (vpx_read_bit(r))
992 0 : for (i = 0; i < PLANE_TYPES; ++i)
993 0 : for (j = 0; j < REF_TYPES; ++j)
994 0 : for (k = 0; k < COEF_BANDS; ++k)
995 0 : for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
996 0 : for (m = 0; m < UNCONSTRAINED_NODES; ++m)
997 0 : vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
998 0 : }
999 :
1000 0 : static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, vpx_reader *r) {
1001 0 : const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
1002 : TX_SIZE tx_size;
1003 0 : for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
1004 0 : read_coef_probs_common(fc->coef_probs[tx_size], r);
1005 0 : }
1006 :
1007 0 : static void setup_segmentation(struct segmentation *seg,
1008 : struct vpx_read_bit_buffer *rb) {
1009 : int i, j;
1010 :
1011 0 : seg->update_map = 0;
1012 0 : seg->update_data = 0;
1013 :
1014 0 : seg->enabled = vpx_rb_read_bit(rb);
1015 0 : if (!seg->enabled) return;
1016 :
1017 : // Segmentation map update
1018 0 : seg->update_map = vpx_rb_read_bit(rb);
1019 0 : if (seg->update_map) {
1020 0 : for (i = 0; i < SEG_TREE_PROBS; i++)
1021 0 : seg->tree_probs[i] =
1022 0 : vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
1023 :
1024 0 : seg->temporal_update = vpx_rb_read_bit(rb);
1025 0 : if (seg->temporal_update) {
1026 0 : for (i = 0; i < PREDICTION_PROBS; i++)
1027 0 : seg->pred_probs[i] =
1028 0 : vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
1029 : } else {
1030 0 : for (i = 0; i < PREDICTION_PROBS; i++) seg->pred_probs[i] = MAX_PROB;
1031 : }
1032 : }
1033 :
1034 : // Segmentation data update
1035 0 : seg->update_data = vpx_rb_read_bit(rb);
1036 0 : if (seg->update_data) {
1037 0 : seg->abs_delta = vpx_rb_read_bit(rb);
1038 :
1039 0 : vp9_clearall_segfeatures(seg);
1040 :
1041 0 : for (i = 0; i < MAX_SEGMENTS; i++) {
1042 0 : for (j = 0; j < SEG_LVL_MAX; j++) {
1043 0 : int data = 0;
1044 0 : const int feature_enabled = vpx_rb_read_bit(rb);
1045 0 : if (feature_enabled) {
1046 0 : vp9_enable_segfeature(seg, i, j);
1047 0 : data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
1048 0 : if (vp9_is_segfeature_signed(j))
1049 0 : data = vpx_rb_read_bit(rb) ? -data : data;
1050 : }
1051 0 : vp9_set_segdata(seg, i, j, data);
1052 : }
1053 : }
1054 : }
1055 : }
1056 :
1057 0 : static void setup_loopfilter(struct loopfilter *lf,
1058 : struct vpx_read_bit_buffer *rb) {
1059 0 : lf->filter_level = vpx_rb_read_literal(rb, 6);
1060 0 : lf->sharpness_level = vpx_rb_read_literal(rb, 3);
1061 :
1062 : // Read in loop filter deltas applied at the MB level based on mode or ref
1063 : // frame.
1064 0 : lf->mode_ref_delta_update = 0;
1065 :
1066 0 : lf->mode_ref_delta_enabled = vpx_rb_read_bit(rb);
1067 0 : if (lf->mode_ref_delta_enabled) {
1068 0 : lf->mode_ref_delta_update = vpx_rb_read_bit(rb);
1069 0 : if (lf->mode_ref_delta_update) {
1070 : int i;
1071 :
1072 0 : for (i = 0; i < MAX_REF_LF_DELTAS; i++)
1073 0 : if (vpx_rb_read_bit(rb))
1074 0 : lf->ref_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1075 :
1076 0 : for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
1077 0 : if (vpx_rb_read_bit(rb))
1078 0 : lf->mode_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
1079 : }
1080 : }
1081 0 : }
1082 :
1083 0 : static INLINE int read_delta_q(struct vpx_read_bit_buffer *rb) {
1084 0 : return vpx_rb_read_bit(rb) ? vpx_rb_read_signed_literal(rb, 4) : 0;
1085 : }
1086 :
1087 0 : static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd,
1088 : struct vpx_read_bit_buffer *rb) {
1089 0 : cm->base_qindex = vpx_rb_read_literal(rb, QINDEX_BITS);
1090 0 : cm->y_dc_delta_q = read_delta_q(rb);
1091 0 : cm->uv_dc_delta_q = read_delta_q(rb);
1092 0 : cm->uv_ac_delta_q = read_delta_q(rb);
1093 0 : cm->dequant_bit_depth = cm->bit_depth;
1094 0 : xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 &&
1095 0 : cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
1096 :
1097 : #if CONFIG_VP9_HIGHBITDEPTH
1098 : xd->bd = (int)cm->bit_depth;
1099 : #endif
1100 0 : }
1101 :
1102 0 : static void setup_segmentation_dequant(VP9_COMMON *const cm) {
1103 : // Build y/uv dequant values based on segmentation.
1104 0 : if (cm->seg.enabled) {
1105 : int i;
1106 0 : for (i = 0; i < MAX_SEGMENTS; ++i) {
1107 0 : const int qindex = vp9_get_qindex(&cm->seg, i, cm->base_qindex);
1108 0 : cm->y_dequant[i][0] =
1109 0 : vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1110 0 : cm->y_dequant[i][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1111 0 : cm->uv_dequant[i][0] =
1112 0 : vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
1113 0 : cm->uv_dequant[i][1] =
1114 0 : vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
1115 : }
1116 : } else {
1117 0 : const int qindex = cm->base_qindex;
1118 : // When segmentation is disabled, only the first value is used. The
1119 : // remaining are don't cares.
1120 0 : cm->y_dequant[0][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
1121 0 : cm->y_dequant[0][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
1122 0 : cm->uv_dequant[0][0] =
1123 0 : vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
1124 0 : cm->uv_dequant[0][1] =
1125 0 : vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
1126 : }
1127 0 : }
1128 :
1129 0 : static INTERP_FILTER read_interp_filter(struct vpx_read_bit_buffer *rb) {
1130 0 : const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH, EIGHTTAP,
1131 : EIGHTTAP_SHARP, BILINEAR };
1132 0 : return vpx_rb_read_bit(rb) ? SWITCHABLE
1133 0 : : literal_to_filter[vpx_rb_read_literal(rb, 2)];
1134 : }
1135 :
1136 0 : static void setup_render_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1137 0 : cm->render_width = cm->width;
1138 0 : cm->render_height = cm->height;
1139 0 : if (vpx_rb_read_bit(rb))
1140 0 : vp9_read_frame_size(rb, &cm->render_width, &cm->render_height);
1141 0 : }
1142 :
1143 0 : static void resize_mv_buffer(VP9_COMMON *cm) {
1144 0 : vpx_free(cm->cur_frame->mvs);
1145 0 : cm->cur_frame->mi_rows = cm->mi_rows;
1146 0 : cm->cur_frame->mi_cols = cm->mi_cols;
1147 0 : CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
1148 : (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
1149 : sizeof(*cm->cur_frame->mvs)));
1150 0 : }
1151 :
1152 0 : static void resize_context_buffers(VP9_COMMON *cm, int width, int height) {
1153 : #if CONFIG_SIZE_LIMIT
1154 0 : if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
1155 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1156 : "Dimensions of %dx%d beyond allowed size of %dx%d.",
1157 : width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
1158 : #endif
1159 0 : if (cm->width != width || cm->height != height) {
1160 0 : const int new_mi_rows =
1161 0 : ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1162 0 : const int new_mi_cols =
1163 0 : ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
1164 :
1165 : // Allocations in vp9_alloc_context_buffers() depend on individual
1166 : // dimensions as well as the overall size.
1167 0 : if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
1168 0 : if (vp9_alloc_context_buffers(cm, width, height))
1169 0 : vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1170 : "Failed to allocate context buffers");
1171 : } else {
1172 0 : vp9_set_mb_mi(cm, width, height);
1173 : }
1174 0 : vp9_init_context_buffers(cm);
1175 0 : cm->width = width;
1176 0 : cm->height = height;
1177 : }
1178 0 : if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
1179 0 : cm->mi_cols > cm->cur_frame->mi_cols) {
1180 0 : resize_mv_buffer(cm);
1181 : }
1182 0 : }
1183 :
1184 0 : static void setup_frame_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1185 : int width, height;
1186 0 : BufferPool *const pool = cm->buffer_pool;
1187 0 : vp9_read_frame_size(rb, &width, &height);
1188 0 : resize_context_buffers(cm, width, height);
1189 0 : setup_render_size(cm, rb);
1190 :
1191 0 : lock_buffer_pool(pool);
1192 0 : if (vpx_realloc_frame_buffer(
1193 : get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1194 : cm->subsampling_y,
1195 : #if CONFIG_VP9_HIGHBITDEPTH
1196 : cm->use_highbitdepth,
1197 : #endif
1198 : VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
1199 0 : &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1200 : pool->cb_priv)) {
1201 0 : unlock_buffer_pool(pool);
1202 0 : vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1203 : "Failed to allocate frame buffer");
1204 : }
1205 0 : unlock_buffer_pool(pool);
1206 :
1207 0 : pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1208 0 : pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1209 0 : pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1210 0 : pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1211 0 : pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1212 0 : pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1213 0 : pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1214 0 : }
1215 :
1216 0 : static INLINE int valid_ref_frame_img_fmt(vpx_bit_depth_t ref_bit_depth,
1217 : int ref_xss, int ref_yss,
1218 : vpx_bit_depth_t this_bit_depth,
1219 : int this_xss, int this_yss) {
1220 0 : return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
1221 : ref_yss == this_yss;
1222 : }
1223 :
1224 0 : static void setup_frame_size_with_refs(VP9_COMMON *cm,
1225 : struct vpx_read_bit_buffer *rb) {
1226 : int width, height;
1227 0 : int found = 0, i;
1228 0 : int has_valid_ref_frame = 0;
1229 0 : BufferPool *const pool = cm->buffer_pool;
1230 0 : for (i = 0; i < REFS_PER_FRAME; ++i) {
1231 0 : if (vpx_rb_read_bit(rb)) {
1232 0 : if (cm->frame_refs[i].idx != INVALID_IDX) {
1233 0 : YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
1234 0 : width = buf->y_crop_width;
1235 0 : height = buf->y_crop_height;
1236 0 : found = 1;
1237 0 : break;
1238 : } else {
1239 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1240 : "Failed to decode frame size");
1241 : }
1242 : }
1243 : }
1244 :
1245 0 : if (!found) vp9_read_frame_size(rb, &width, &height);
1246 :
1247 0 : if (width <= 0 || height <= 0)
1248 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1249 : "Invalid frame size");
1250 :
1251 : // Check to make sure at least one of frames that this frame references
1252 : // has valid dimensions.
1253 0 : for (i = 0; i < REFS_PER_FRAME; ++i) {
1254 0 : RefBuffer *const ref_frame = &cm->frame_refs[i];
1255 0 : has_valid_ref_frame |=
1256 0 : (ref_frame->idx != INVALID_IDX &&
1257 0 : valid_ref_frame_size(ref_frame->buf->y_crop_width,
1258 0 : ref_frame->buf->y_crop_height, width, height));
1259 : }
1260 0 : if (!has_valid_ref_frame)
1261 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1262 : "Referenced frame has invalid size");
1263 0 : for (i = 0; i < REFS_PER_FRAME; ++i) {
1264 0 : RefBuffer *const ref_frame = &cm->frame_refs[i];
1265 0 : if (ref_frame->idx == INVALID_IDX ||
1266 0 : !valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
1267 0 : ref_frame->buf->subsampling_x,
1268 0 : ref_frame->buf->subsampling_y, cm->bit_depth,
1269 : cm->subsampling_x, cm->subsampling_y))
1270 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1271 : "Referenced frame has incompatible color format");
1272 : }
1273 :
1274 0 : resize_context_buffers(cm, width, height);
1275 0 : setup_render_size(cm, rb);
1276 :
1277 0 : lock_buffer_pool(pool);
1278 0 : if (vpx_realloc_frame_buffer(
1279 : get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
1280 : cm->subsampling_y,
1281 : #if CONFIG_VP9_HIGHBITDEPTH
1282 : cm->use_highbitdepth,
1283 : #endif
1284 : VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
1285 0 : &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
1286 : pool->cb_priv)) {
1287 0 : unlock_buffer_pool(pool);
1288 0 : vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1289 : "Failed to allocate frame buffer");
1290 : }
1291 0 : unlock_buffer_pool(pool);
1292 :
1293 0 : pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
1294 0 : pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
1295 0 : pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
1296 0 : pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
1297 0 : pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
1298 0 : pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
1299 0 : pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
1300 0 : }
1301 :
1302 0 : static void setup_tile_info(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
1303 : int min_log2_tile_cols, max_log2_tile_cols, max_ones;
1304 0 : vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
1305 :
1306 : // columns
1307 0 : max_ones = max_log2_tile_cols - min_log2_tile_cols;
1308 0 : cm->log2_tile_cols = min_log2_tile_cols;
1309 0 : while (max_ones-- && vpx_rb_read_bit(rb)) cm->log2_tile_cols++;
1310 :
1311 0 : if (cm->log2_tile_cols > 6)
1312 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1313 : "Invalid number of tile columns");
1314 :
1315 : // rows
1316 0 : cm->log2_tile_rows = vpx_rb_read_bit(rb);
1317 0 : if (cm->log2_tile_rows) cm->log2_tile_rows += vpx_rb_read_bit(rb);
1318 0 : }
1319 :
1320 : // Reads the next tile returning its size and adjusting '*data' accordingly
1321 : // based on 'is_last'.
1322 0 : static void get_tile_buffer(const uint8_t *const data_end, int is_last,
1323 : struct vpx_internal_error_info *error_info,
1324 : const uint8_t **data, vpx_decrypt_cb decrypt_cb,
1325 : void *decrypt_state, TileBuffer *buf) {
1326 : size_t size;
1327 :
1328 0 : if (!is_last) {
1329 0 : if (!read_is_valid(*data, 4, data_end))
1330 0 : vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1331 : "Truncated packet or corrupt tile length");
1332 :
1333 0 : if (decrypt_cb) {
1334 : uint8_t be_data[4];
1335 0 : decrypt_cb(decrypt_state, *data, be_data, 4);
1336 0 : size = mem_get_be32(be_data);
1337 : } else {
1338 0 : size = mem_get_be32(*data);
1339 : }
1340 0 : *data += 4;
1341 :
1342 0 : if (size > (size_t)(data_end - *data))
1343 0 : vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
1344 : "Truncated packet or corrupt tile size");
1345 : } else {
1346 0 : size = data_end - *data;
1347 : }
1348 :
1349 0 : buf->data = *data;
1350 0 : buf->size = size;
1351 :
1352 0 : *data += size;
1353 0 : }
1354 :
1355 0 : static void get_tile_buffers(VP9Decoder *pbi, const uint8_t *data,
1356 : const uint8_t *data_end, int tile_cols,
1357 : int tile_rows,
1358 : TileBuffer (*tile_buffers)[1 << 6]) {
1359 : int r, c;
1360 :
1361 0 : for (r = 0; r < tile_rows; ++r) {
1362 0 : for (c = 0; c < tile_cols; ++c) {
1363 0 : const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
1364 0 : TileBuffer *const buf = &tile_buffers[r][c];
1365 0 : buf->col = c;
1366 0 : get_tile_buffer(data_end, is_last, &pbi->common.error, &data,
1367 : pbi->decrypt_cb, pbi->decrypt_state, buf);
1368 : }
1369 : }
1370 0 : }
1371 :
1372 0 : static const uint8_t *decode_tiles(VP9Decoder *pbi, const uint8_t *data,
1373 : const uint8_t *data_end) {
1374 0 : VP9_COMMON *const cm = &pbi->common;
1375 0 : const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
1376 0 : const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1377 0 : const int tile_cols = 1 << cm->log2_tile_cols;
1378 0 : const int tile_rows = 1 << cm->log2_tile_rows;
1379 : TileBuffer tile_buffers[4][1 << 6];
1380 : int tile_row, tile_col;
1381 : int mi_row, mi_col;
1382 0 : TileWorkerData *tile_data = NULL;
1383 :
1384 0 : if (cm->lf.filter_level && !cm->skip_loop_filter &&
1385 0 : pbi->lf_worker.data1 == NULL) {
1386 0 : CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
1387 : vpx_memalign(32, sizeof(LFWorkerData)));
1388 0 : pbi->lf_worker.hook = (VPxWorkerHook)vp9_loop_filter_worker;
1389 0 : if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
1390 0 : vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1391 : "Loop filter thread creation failed");
1392 : }
1393 : }
1394 :
1395 0 : if (cm->lf.filter_level && !cm->skip_loop_filter) {
1396 0 : LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
1397 : // Be sure to sync as we might be resuming after a failed frame decode.
1398 0 : winterface->sync(&pbi->lf_worker);
1399 0 : vp9_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
1400 0 : pbi->mb.plane);
1401 : }
1402 :
1403 0 : assert(tile_rows <= 4);
1404 0 : assert(tile_cols <= (1 << 6));
1405 :
1406 : // Note: this memset assumes above_context[0], [1] and [2]
1407 : // are allocated as part of the same buffer.
1408 0 : memset(cm->above_context, 0,
1409 : sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
1410 :
1411 0 : memset(cm->above_seg_context, 0,
1412 : sizeof(*cm->above_seg_context) * aligned_cols);
1413 :
1414 0 : vp9_reset_lfm(cm);
1415 :
1416 0 : get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
1417 :
1418 : // Load all tile information into tile_data.
1419 0 : for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1420 0 : for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1421 0 : const TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
1422 0 : tile_data = pbi->tile_worker_data + tile_cols * tile_row + tile_col;
1423 0 : tile_data->xd = pbi->mb;
1424 0 : tile_data->xd.corrupted = 0;
1425 0 : tile_data->xd.counts =
1426 0 : cm->frame_parallel_decoding_mode ? NULL : &cm->counts;
1427 0 : vp9_zero(tile_data->dqcoeff);
1428 0 : vp9_tile_init(&tile_data->xd.tile, cm, tile_row, tile_col);
1429 0 : setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
1430 : &tile_data->bit_reader, pbi->decrypt_cb,
1431 : pbi->decrypt_state);
1432 0 : vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
1433 : }
1434 : }
1435 :
1436 0 : for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
1437 : TileInfo tile;
1438 0 : vp9_tile_set_row(&tile, cm, tile_row);
1439 0 : for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end;
1440 0 : mi_row += MI_BLOCK_SIZE) {
1441 0 : for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
1442 0 : const int col =
1443 0 : pbi->inv_tile_order ? tile_cols - tile_col - 1 : tile_col;
1444 0 : tile_data = pbi->tile_worker_data + tile_cols * tile_row + col;
1445 0 : vp9_tile_set_col(&tile, cm, col);
1446 0 : vp9_zero(tile_data->xd.left_context);
1447 0 : vp9_zero(tile_data->xd.left_seg_context);
1448 0 : for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
1449 0 : mi_col += MI_BLOCK_SIZE) {
1450 0 : decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
1451 : }
1452 0 : pbi->mb.corrupted |= tile_data->xd.corrupted;
1453 0 : if (pbi->mb.corrupted)
1454 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1455 : "Failed to decode tile data");
1456 : }
1457 : // Loopfilter one row.
1458 0 : if (cm->lf.filter_level && !cm->skip_loop_filter) {
1459 0 : const int lf_start = mi_row - MI_BLOCK_SIZE;
1460 0 : LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
1461 :
1462 : // delay the loopfilter by 1 macroblock row.
1463 0 : if (lf_start < 0) continue;
1464 :
1465 : // decoding has completed: finish up the loop filter in this thread.
1466 0 : if (mi_row + MI_BLOCK_SIZE >= cm->mi_rows) continue;
1467 :
1468 0 : winterface->sync(&pbi->lf_worker);
1469 0 : lf_data->start = lf_start;
1470 0 : lf_data->stop = mi_row;
1471 0 : if (pbi->max_threads > 1) {
1472 0 : winterface->launch(&pbi->lf_worker);
1473 : } else {
1474 0 : winterface->execute(&pbi->lf_worker);
1475 : }
1476 : }
1477 : // After loopfiltering, the last 7 row pixels in each superblock row may
1478 : // still be changed by the longest loopfilter of the next superblock
1479 : // row.
1480 0 : if (pbi->frame_parallel_decode)
1481 0 : vp9_frameworker_broadcast(pbi->cur_buf, mi_row << MI_BLOCK_SIZE_LOG2);
1482 : }
1483 : }
1484 :
1485 : // Loopfilter remaining rows in the frame.
1486 0 : if (cm->lf.filter_level && !cm->skip_loop_filter) {
1487 0 : LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
1488 0 : winterface->sync(&pbi->lf_worker);
1489 0 : lf_data->start = lf_data->stop;
1490 0 : lf_data->stop = cm->mi_rows;
1491 0 : winterface->execute(&pbi->lf_worker);
1492 : }
1493 :
1494 : // Get last tile data.
1495 0 : tile_data = pbi->tile_worker_data + tile_cols * tile_rows - 1;
1496 :
1497 0 : if (pbi->frame_parallel_decode)
1498 0 : vp9_frameworker_broadcast(pbi->cur_buf, INT_MAX);
1499 0 : return vpx_reader_find_end(&tile_data->bit_reader);
1500 : }
1501 :
1502 : // On entry 'tile_data->data_end' points to the end of the input frame, on exit
1503 : // it is updated to reflect the bitreader position of the final tile column if
1504 : // present in the tile buffer group or NULL otherwise.
1505 0 : static int tile_worker_hook(TileWorkerData *const tile_data,
1506 : VP9Decoder *const pbi) {
1507 0 : TileInfo *volatile tile = &tile_data->xd.tile;
1508 0 : const int final_col = (1 << pbi->common.log2_tile_cols) - 1;
1509 0 : const uint8_t *volatile bit_reader_end = NULL;
1510 0 : volatile int n = tile_data->buf_start;
1511 0 : tile_data->error_info.setjmp = 1;
1512 :
1513 0 : if (setjmp(tile_data->error_info.jmp)) {
1514 0 : tile_data->error_info.setjmp = 0;
1515 0 : tile_data->xd.corrupted = 1;
1516 0 : tile_data->data_end = NULL;
1517 0 : return 0;
1518 : }
1519 :
1520 0 : tile_data->xd.corrupted = 0;
1521 :
1522 : do {
1523 : int mi_row, mi_col;
1524 0 : const TileBuffer *const buf = pbi->tile_buffers + n;
1525 0 : vp9_zero(tile_data->dqcoeff);
1526 0 : vp9_tile_init(tile, &pbi->common, 0, buf->col);
1527 0 : setup_token_decoder(buf->data, tile_data->data_end, buf->size,
1528 : &tile_data->error_info, &tile_data->bit_reader,
1529 : pbi->decrypt_cb, pbi->decrypt_state);
1530 0 : vp9_init_macroblockd(&pbi->common, &tile_data->xd, tile_data->dqcoeff);
1531 : // init resets xd.error_info
1532 0 : tile_data->xd.error_info = &tile_data->error_info;
1533 :
1534 0 : for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
1535 0 : mi_row += MI_BLOCK_SIZE) {
1536 0 : vp9_zero(tile_data->xd.left_context);
1537 0 : vp9_zero(tile_data->xd.left_seg_context);
1538 0 : for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
1539 0 : mi_col += MI_BLOCK_SIZE) {
1540 0 : decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
1541 : }
1542 : }
1543 :
1544 0 : if (buf->col == final_col) {
1545 0 : bit_reader_end = vpx_reader_find_end(&tile_data->bit_reader);
1546 : }
1547 0 : } while (!tile_data->xd.corrupted && ++n <= tile_data->buf_end);
1548 :
1549 0 : tile_data->data_end = bit_reader_end;
1550 0 : return !tile_data->xd.corrupted;
1551 : }
1552 :
1553 : // sorts in descending order
1554 0 : static int compare_tile_buffers(const void *a, const void *b) {
1555 0 : const TileBuffer *const buf1 = (const TileBuffer *)a;
1556 0 : const TileBuffer *const buf2 = (const TileBuffer *)b;
1557 0 : return (int)(buf2->size - buf1->size);
1558 : }
1559 :
1560 0 : static const uint8_t *decode_tiles_mt(VP9Decoder *pbi, const uint8_t *data,
1561 : const uint8_t *data_end) {
1562 0 : VP9_COMMON *const cm = &pbi->common;
1563 0 : const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
1564 0 : const uint8_t *bit_reader_end = NULL;
1565 0 : const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
1566 0 : const int tile_cols = 1 << cm->log2_tile_cols;
1567 0 : const int tile_rows = 1 << cm->log2_tile_rows;
1568 0 : const int num_workers = VPXMIN(pbi->max_threads, tile_cols);
1569 : int n;
1570 :
1571 0 : assert(tile_cols <= (1 << 6));
1572 0 : assert(tile_rows == 1);
1573 : (void)tile_rows;
1574 :
1575 0 : if (pbi->num_tile_workers == 0) {
1576 0 : const int num_threads = pbi->max_threads;
1577 0 : CHECK_MEM_ERROR(cm, pbi->tile_workers,
1578 : vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
1579 0 : for (n = 0; n < num_threads; ++n) {
1580 0 : VPxWorker *const worker = &pbi->tile_workers[n];
1581 0 : ++pbi->num_tile_workers;
1582 :
1583 0 : winterface->init(worker);
1584 0 : if (n < num_threads - 1 && !winterface->reset(worker)) {
1585 0 : vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
1586 : "Tile decoder thread creation failed");
1587 : }
1588 : }
1589 : }
1590 :
1591 : // Reset tile decoding hook
1592 0 : for (n = 0; n < num_workers; ++n) {
1593 0 : VPxWorker *const worker = &pbi->tile_workers[n];
1594 0 : TileWorkerData *const tile_data =
1595 0 : &pbi->tile_worker_data[n + pbi->total_tiles];
1596 0 : winterface->sync(worker);
1597 0 : tile_data->xd = pbi->mb;
1598 0 : tile_data->xd.counts =
1599 0 : cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts;
1600 0 : worker->hook = (VPxWorkerHook)tile_worker_hook;
1601 0 : worker->data1 = tile_data;
1602 0 : worker->data2 = pbi;
1603 : }
1604 :
1605 : // Note: this memset assumes above_context[0], [1] and [2]
1606 : // are allocated as part of the same buffer.
1607 0 : memset(cm->above_context, 0,
1608 : sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
1609 0 : memset(cm->above_seg_context, 0,
1610 : sizeof(*cm->above_seg_context) * aligned_mi_cols);
1611 :
1612 0 : vp9_reset_lfm(cm);
1613 :
1614 : // Load tile data into tile_buffers
1615 0 : get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows,
1616 : &pbi->tile_buffers);
1617 :
1618 : // Sort the buffers based on size in descending order.
1619 0 : qsort(pbi->tile_buffers, tile_cols, sizeof(pbi->tile_buffers[0]),
1620 : compare_tile_buffers);
1621 :
1622 0 : if (num_workers == tile_cols) {
1623 : // Rearrange the tile buffers such that the largest, and
1624 : // presumably the most difficult, tile will be decoded in the main thread.
1625 : // This should help minimize the number of instances where the main thread
1626 : // is waiting for a worker to complete.
1627 0 : const TileBuffer largest = pbi->tile_buffers[0];
1628 0 : memmove(pbi->tile_buffers, pbi->tile_buffers + 1,
1629 0 : (tile_cols - 1) * sizeof(pbi->tile_buffers[0]));
1630 0 : pbi->tile_buffers[tile_cols - 1] = largest;
1631 : } else {
1632 0 : int start = 0, end = tile_cols - 2;
1633 : TileBuffer tmp;
1634 :
1635 : // Interleave the tiles to distribute the load between threads, assuming a
1636 : // larger tile implies it is more difficult to decode.
1637 0 : while (start < end) {
1638 0 : tmp = pbi->tile_buffers[start];
1639 0 : pbi->tile_buffers[start] = pbi->tile_buffers[end];
1640 0 : pbi->tile_buffers[end] = tmp;
1641 0 : start += 2;
1642 0 : end -= 2;
1643 : }
1644 : }
1645 :
1646 : // Initialize thread frame counts.
1647 0 : if (!cm->frame_parallel_decoding_mode) {
1648 0 : for (n = 0; n < num_workers; ++n) {
1649 0 : TileWorkerData *const tile_data =
1650 0 : (TileWorkerData *)pbi->tile_workers[n].data1;
1651 0 : vp9_zero(tile_data->counts);
1652 : }
1653 : }
1654 :
1655 : {
1656 0 : const int base = tile_cols / num_workers;
1657 0 : const int remain = tile_cols % num_workers;
1658 0 : int buf_start = 0;
1659 :
1660 0 : for (n = 0; n < num_workers; ++n) {
1661 0 : const int count = base + (remain + n) / num_workers;
1662 0 : VPxWorker *const worker = &pbi->tile_workers[n];
1663 0 : TileWorkerData *const tile_data = (TileWorkerData *)worker->data1;
1664 :
1665 0 : tile_data->buf_start = buf_start;
1666 0 : tile_data->buf_end = buf_start + count - 1;
1667 0 : tile_data->data_end = data_end;
1668 0 : buf_start += count;
1669 :
1670 0 : worker->had_error = 0;
1671 0 : if (n == num_workers - 1) {
1672 0 : assert(tile_data->buf_end == tile_cols - 1);
1673 0 : winterface->execute(worker);
1674 : } else {
1675 0 : winterface->launch(worker);
1676 : }
1677 : }
1678 :
1679 0 : for (; n > 0; --n) {
1680 0 : VPxWorker *const worker = &pbi->tile_workers[n - 1];
1681 0 : TileWorkerData *const tile_data = (TileWorkerData *)worker->data1;
1682 : // TODO(jzern): The tile may have specific error data associated with
1683 : // its vpx_internal_error_info which could be propagated to the main info
1684 : // in cm. Additionally once the threads have been synced and an error is
1685 : // detected, there's no point in continuing to decode tiles.
1686 0 : pbi->mb.corrupted |= !winterface->sync(worker);
1687 0 : if (!bit_reader_end) bit_reader_end = tile_data->data_end;
1688 : }
1689 : }
1690 :
1691 : // Accumulate thread frame counts.
1692 0 : if (!cm->frame_parallel_decoding_mode) {
1693 0 : for (n = 0; n < num_workers; ++n) {
1694 0 : TileWorkerData *const tile_data =
1695 0 : (TileWorkerData *)pbi->tile_workers[n].data1;
1696 0 : vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1);
1697 : }
1698 : }
1699 :
1700 0 : assert(bit_reader_end || pbi->mb.corrupted);
1701 0 : return bit_reader_end;
1702 : }
1703 :
1704 0 : static void error_handler(void *data) {
1705 0 : VP9_COMMON *const cm = (VP9_COMMON *)data;
1706 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
1707 0 : }
1708 :
1709 0 : static void read_bitdepth_colorspace_sampling(VP9_COMMON *cm,
1710 : struct vpx_read_bit_buffer *rb) {
1711 0 : if (cm->profile >= PROFILE_2) {
1712 0 : cm->bit_depth = vpx_rb_read_bit(rb) ? VPX_BITS_12 : VPX_BITS_10;
1713 : #if CONFIG_VP9_HIGHBITDEPTH
1714 : cm->use_highbitdepth = 1;
1715 : #endif
1716 : } else {
1717 0 : cm->bit_depth = VPX_BITS_8;
1718 : #if CONFIG_VP9_HIGHBITDEPTH
1719 : cm->use_highbitdepth = 0;
1720 : #endif
1721 : }
1722 0 : cm->color_space = vpx_rb_read_literal(rb, 3);
1723 0 : if (cm->color_space != VPX_CS_SRGB) {
1724 0 : cm->color_range = (vpx_color_range_t)vpx_rb_read_bit(rb);
1725 0 : if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
1726 0 : cm->subsampling_x = vpx_rb_read_bit(rb);
1727 0 : cm->subsampling_y = vpx_rb_read_bit(rb);
1728 0 : if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
1729 0 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1730 : "4:2:0 color not supported in profile 1 or 3");
1731 0 : if (vpx_rb_read_bit(rb))
1732 0 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1733 : "Reserved bit set");
1734 : } else {
1735 0 : cm->subsampling_y = cm->subsampling_x = 1;
1736 : }
1737 : } else {
1738 0 : cm->color_range = VPX_CR_FULL_RANGE;
1739 0 : if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
1740 : // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
1741 : // 4:2:2 or 4:4:0 chroma sampling is not allowed.
1742 0 : cm->subsampling_y = cm->subsampling_x = 0;
1743 0 : if (vpx_rb_read_bit(rb))
1744 0 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1745 : "Reserved bit set");
1746 : } else {
1747 0 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1748 : "4:4:4 color not supported in profile 0 or 2");
1749 : }
1750 : }
1751 0 : }
1752 :
1753 0 : static size_t read_uncompressed_header(VP9Decoder *pbi,
1754 : struct vpx_read_bit_buffer *rb) {
1755 0 : VP9_COMMON *const cm = &pbi->common;
1756 0 : BufferPool *const pool = cm->buffer_pool;
1757 0 : RefCntBuffer *const frame_bufs = pool->frame_bufs;
1758 0 : int i, mask, ref_index = 0;
1759 : size_t sz;
1760 :
1761 0 : cm->last_frame_type = cm->frame_type;
1762 0 : cm->last_intra_only = cm->intra_only;
1763 :
1764 0 : if (vpx_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
1765 0 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1766 : "Invalid frame marker");
1767 :
1768 0 : cm->profile = vp9_read_profile(rb);
1769 : #if CONFIG_VP9_HIGHBITDEPTH
1770 : if (cm->profile >= MAX_PROFILES)
1771 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1772 : "Unsupported bitstream profile");
1773 : #else
1774 0 : if (cm->profile >= PROFILE_2)
1775 0 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1776 : "Unsupported bitstream profile");
1777 : #endif
1778 :
1779 0 : cm->show_existing_frame = vpx_rb_read_bit(rb);
1780 0 : if (cm->show_existing_frame) {
1781 : // Show an existing frame directly.
1782 0 : const int frame_to_show = cm->ref_frame_map[vpx_rb_read_literal(rb, 3)];
1783 0 : lock_buffer_pool(pool);
1784 0 : if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
1785 0 : unlock_buffer_pool(pool);
1786 0 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1787 : "Buffer %d does not contain a decoded frame",
1788 : frame_to_show);
1789 : }
1790 :
1791 0 : ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
1792 0 : unlock_buffer_pool(pool);
1793 0 : pbi->refresh_frame_flags = 0;
1794 0 : cm->lf.filter_level = 0;
1795 0 : cm->show_frame = 1;
1796 :
1797 0 : if (pbi->frame_parallel_decode) {
1798 0 : for (i = 0; i < REF_FRAMES; ++i)
1799 0 : cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
1800 : }
1801 0 : return 0;
1802 : }
1803 :
1804 0 : cm->frame_type = (FRAME_TYPE)vpx_rb_read_bit(rb);
1805 0 : cm->show_frame = vpx_rb_read_bit(rb);
1806 0 : cm->error_resilient_mode = vpx_rb_read_bit(rb);
1807 :
1808 0 : if (cm->frame_type == KEY_FRAME) {
1809 0 : if (!vp9_read_sync_code(rb))
1810 0 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1811 : "Invalid frame sync code");
1812 :
1813 0 : read_bitdepth_colorspace_sampling(cm, rb);
1814 0 : pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
1815 :
1816 0 : for (i = 0; i < REFS_PER_FRAME; ++i) {
1817 0 : cm->frame_refs[i].idx = INVALID_IDX;
1818 0 : cm->frame_refs[i].buf = NULL;
1819 : }
1820 :
1821 0 : setup_frame_size(cm, rb);
1822 0 : if (pbi->need_resync) {
1823 0 : memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
1824 0 : pbi->need_resync = 0;
1825 : }
1826 : } else {
1827 0 : cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb);
1828 :
1829 0 : cm->reset_frame_context =
1830 0 : cm->error_resilient_mode ? 0 : vpx_rb_read_literal(rb, 2);
1831 :
1832 0 : if (cm->intra_only) {
1833 0 : if (!vp9_read_sync_code(rb))
1834 0 : vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
1835 : "Invalid frame sync code");
1836 0 : if (cm->profile > PROFILE_0) {
1837 0 : read_bitdepth_colorspace_sampling(cm, rb);
1838 : } else {
1839 : // NOTE: The intra-only frame header does not include the specification
1840 : // of either the color format or color sub-sampling in profile 0. VP9
1841 : // specifies that the default color format should be YUV 4:2:0 in this
1842 : // case (normative).
1843 0 : cm->color_space = VPX_CS_BT_601;
1844 0 : cm->color_range = VPX_CR_STUDIO_RANGE;
1845 0 : cm->subsampling_y = cm->subsampling_x = 1;
1846 0 : cm->bit_depth = VPX_BITS_8;
1847 : #if CONFIG_VP9_HIGHBITDEPTH
1848 : cm->use_highbitdepth = 0;
1849 : #endif
1850 : }
1851 :
1852 0 : pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
1853 0 : setup_frame_size(cm, rb);
1854 0 : if (pbi->need_resync) {
1855 0 : memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
1856 0 : pbi->need_resync = 0;
1857 : }
1858 0 : } else if (pbi->need_resync != 1) { /* Skip if need resync */
1859 0 : pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
1860 0 : for (i = 0; i < REFS_PER_FRAME; ++i) {
1861 0 : const int ref = vpx_rb_read_literal(rb, REF_FRAMES_LOG2);
1862 0 : const int idx = cm->ref_frame_map[ref];
1863 0 : RefBuffer *const ref_frame = &cm->frame_refs[i];
1864 0 : ref_frame->idx = idx;
1865 0 : ref_frame->buf = &frame_bufs[idx].buf;
1866 0 : cm->ref_frame_sign_bias[LAST_FRAME + i] = vpx_rb_read_bit(rb);
1867 : }
1868 :
1869 0 : setup_frame_size_with_refs(cm, rb);
1870 :
1871 0 : cm->allow_high_precision_mv = vpx_rb_read_bit(rb);
1872 0 : cm->interp_filter = read_interp_filter(rb);
1873 :
1874 0 : for (i = 0; i < REFS_PER_FRAME; ++i) {
1875 0 : RefBuffer *const ref_buf = &cm->frame_refs[i];
1876 : #if CONFIG_VP9_HIGHBITDEPTH
1877 : vp9_setup_scale_factors_for_frame(
1878 : &ref_buf->sf, ref_buf->buf->y_crop_width,
1879 : ref_buf->buf->y_crop_height, cm->width, cm->height,
1880 : cm->use_highbitdepth);
1881 : #else
1882 0 : vp9_setup_scale_factors_for_frame(
1883 0 : &ref_buf->sf, ref_buf->buf->y_crop_width,
1884 0 : ref_buf->buf->y_crop_height, cm->width, cm->height);
1885 : #endif
1886 : }
1887 : }
1888 : }
1889 : #if CONFIG_VP9_HIGHBITDEPTH
1890 : get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
1891 : #endif
1892 0 : get_frame_new_buffer(cm)->color_space = cm->color_space;
1893 0 : get_frame_new_buffer(cm)->color_range = cm->color_range;
1894 0 : get_frame_new_buffer(cm)->render_width = cm->render_width;
1895 0 : get_frame_new_buffer(cm)->render_height = cm->render_height;
1896 :
1897 0 : if (pbi->need_resync) {
1898 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1899 : "Keyframe / intra-only frame required to reset decoder"
1900 : " state");
1901 : }
1902 :
1903 0 : if (!cm->error_resilient_mode) {
1904 0 : cm->refresh_frame_context = vpx_rb_read_bit(rb);
1905 0 : cm->frame_parallel_decoding_mode = vpx_rb_read_bit(rb);
1906 0 : if (!cm->frame_parallel_decoding_mode) vp9_zero(cm->counts);
1907 : } else {
1908 0 : cm->refresh_frame_context = 0;
1909 0 : cm->frame_parallel_decoding_mode = 1;
1910 : }
1911 :
1912 : // This flag will be overridden by the call to vp9_setup_past_independence
1913 : // below, forcing the use of context 0 for those frame types.
1914 0 : cm->frame_context_idx = vpx_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
1915 :
1916 : // Generate next_ref_frame_map.
1917 0 : lock_buffer_pool(pool);
1918 0 : for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
1919 0 : if (mask & 1) {
1920 0 : cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
1921 0 : ++frame_bufs[cm->new_fb_idx].ref_count;
1922 : } else {
1923 0 : cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
1924 : }
1925 : // Current thread holds the reference frame.
1926 0 : if (cm->ref_frame_map[ref_index] >= 0)
1927 0 : ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
1928 0 : ++ref_index;
1929 : }
1930 :
1931 0 : for (; ref_index < REF_FRAMES; ++ref_index) {
1932 0 : cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
1933 : // Current thread holds the reference frame.
1934 0 : if (cm->ref_frame_map[ref_index] >= 0)
1935 0 : ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
1936 : }
1937 0 : unlock_buffer_pool(pool);
1938 0 : pbi->hold_ref_buf = 1;
1939 :
1940 0 : if (frame_is_intra_only(cm) || cm->error_resilient_mode)
1941 0 : vp9_setup_past_independence(cm);
1942 :
1943 0 : setup_loopfilter(&cm->lf, rb);
1944 0 : setup_quantization(cm, &pbi->mb, rb);
1945 0 : setup_segmentation(&cm->seg, rb);
1946 0 : setup_segmentation_dequant(cm);
1947 :
1948 0 : setup_tile_info(cm, rb);
1949 0 : sz = vpx_rb_read_literal(rb, 16);
1950 :
1951 0 : if (sz == 0)
1952 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
1953 : "Invalid header size");
1954 :
1955 0 : return sz;
1956 : }
1957 :
1958 0 : static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data,
1959 : size_t partition_size) {
1960 0 : VP9_COMMON *const cm = &pbi->common;
1961 0 : MACROBLOCKD *const xd = &pbi->mb;
1962 0 : FRAME_CONTEXT *const fc = cm->fc;
1963 : vpx_reader r;
1964 : int k;
1965 :
1966 0 : if (vpx_reader_init(&r, data, partition_size, pbi->decrypt_cb,
1967 : pbi->decrypt_state))
1968 0 : vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
1969 : "Failed to allocate bool decoder 0");
1970 :
1971 0 : cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
1972 0 : if (cm->tx_mode == TX_MODE_SELECT) read_tx_mode_probs(&fc->tx_probs, &r);
1973 0 : read_coef_probs(fc, cm->tx_mode, &r);
1974 :
1975 0 : for (k = 0; k < SKIP_CONTEXTS; ++k)
1976 0 : vp9_diff_update_prob(&r, &fc->skip_probs[k]);
1977 :
1978 0 : if (!frame_is_intra_only(cm)) {
1979 0 : nmv_context *const nmvc = &fc->nmvc;
1980 : int i, j;
1981 :
1982 0 : read_inter_mode_probs(fc, &r);
1983 :
1984 0 : if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
1985 :
1986 0 : for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
1987 0 : vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]);
1988 :
1989 0 : cm->reference_mode = read_frame_reference_mode(cm, &r);
1990 0 : if (cm->reference_mode != SINGLE_REFERENCE)
1991 0 : setup_compound_reference_mode(cm);
1992 0 : read_frame_reference_mode_probs(cm, &r);
1993 :
1994 0 : for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
1995 0 : for (i = 0; i < INTRA_MODES - 1; ++i)
1996 0 : vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
1997 :
1998 0 : for (j = 0; j < PARTITION_CONTEXTS; ++j)
1999 0 : for (i = 0; i < PARTITION_TYPES - 1; ++i)
2000 0 : vp9_diff_update_prob(&r, &fc->partition_prob[j][i]);
2001 :
2002 0 : read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
2003 : }
2004 :
2005 0 : return vpx_reader_has_error(&r);
2006 : }
2007 :
2008 0 : static struct vpx_read_bit_buffer *init_read_bit_buffer(
2009 : VP9Decoder *pbi, struct vpx_read_bit_buffer *rb, const uint8_t *data,
2010 : const uint8_t *data_end, uint8_t clear_data[MAX_VP9_HEADER_SIZE]) {
2011 0 : rb->bit_offset = 0;
2012 0 : rb->error_handler = error_handler;
2013 0 : rb->error_handler_data = &pbi->common;
2014 0 : if (pbi->decrypt_cb) {
2015 0 : const int n = (int)VPXMIN(MAX_VP9_HEADER_SIZE, data_end - data);
2016 0 : pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
2017 0 : rb->bit_buffer = clear_data;
2018 0 : rb->bit_buffer_end = clear_data + n;
2019 : } else {
2020 0 : rb->bit_buffer = data;
2021 0 : rb->bit_buffer_end = data_end;
2022 : }
2023 0 : return rb;
2024 : }
2025 :
2026 : //------------------------------------------------------------------------------
2027 :
2028 0 : int vp9_read_sync_code(struct vpx_read_bit_buffer *const rb) {
2029 0 : return vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 &&
2030 0 : vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 &&
2031 0 : vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2;
2032 : }
2033 :
2034 0 : void vp9_read_frame_size(struct vpx_read_bit_buffer *rb, int *width,
2035 : int *height) {
2036 0 : *width = vpx_rb_read_literal(rb, 16) + 1;
2037 0 : *height = vpx_rb_read_literal(rb, 16) + 1;
2038 0 : }
2039 :
2040 0 : BITSTREAM_PROFILE vp9_read_profile(struct vpx_read_bit_buffer *rb) {
2041 0 : int profile = vpx_rb_read_bit(rb);
2042 0 : profile |= vpx_rb_read_bit(rb) << 1;
2043 0 : if (profile > 2) profile += vpx_rb_read_bit(rb);
2044 0 : return (BITSTREAM_PROFILE)profile;
2045 : }
2046 :
2047 0 : void vp9_decode_frame(VP9Decoder *pbi, const uint8_t *data,
2048 : const uint8_t *data_end, const uint8_t **p_data_end) {
2049 0 : VP9_COMMON *const cm = &pbi->common;
2050 0 : MACROBLOCKD *const xd = &pbi->mb;
2051 : struct vpx_read_bit_buffer rb;
2052 0 : int context_updated = 0;
2053 : uint8_t clear_data[MAX_VP9_HEADER_SIZE];
2054 0 : const size_t first_partition_size = read_uncompressed_header(
2055 : pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
2056 0 : const int tile_rows = 1 << cm->log2_tile_rows;
2057 0 : const int tile_cols = 1 << cm->log2_tile_cols;
2058 0 : YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
2059 0 : xd->cur_buf = new_fb;
2060 :
2061 0 : if (!first_partition_size) {
2062 : // showing a frame directly
2063 0 : *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
2064 0 : return;
2065 : }
2066 :
2067 0 : data += vpx_rb_bytes_read(&rb);
2068 0 : if (!read_is_valid(data, first_partition_size, data_end))
2069 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2070 : "Truncated packet or corrupt header length");
2071 :
2072 0 : cm->use_prev_frame_mvs =
2073 0 : !cm->error_resilient_mode && cm->width == cm->last_width &&
2074 0 : cm->height == cm->last_height && !cm->last_intra_only &&
2075 0 : cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
2076 :
2077 0 : vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
2078 :
2079 0 : *cm->fc = cm->frame_contexts[cm->frame_context_idx];
2080 0 : if (!cm->fc->initialized)
2081 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2082 : "Uninitialized entropy context.");
2083 :
2084 0 : xd->corrupted = 0;
2085 0 : new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
2086 0 : if (new_fb->corrupted)
2087 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2088 : "Decode failed. Frame data header is corrupted.");
2089 :
2090 0 : if (cm->lf.filter_level && !cm->skip_loop_filter) {
2091 0 : vp9_loop_filter_frame_init(cm, cm->lf.filter_level);
2092 : }
2093 :
2094 : // If encoded in frame parallel mode, frame context is ready after decoding
2095 : // the frame header.
2096 0 : if (pbi->frame_parallel_decode && cm->frame_parallel_decoding_mode) {
2097 0 : VPxWorker *const worker = pbi->frame_worker_owner;
2098 0 : FrameWorkerData *const frame_worker_data = worker->data1;
2099 0 : if (cm->refresh_frame_context) {
2100 0 : context_updated = 1;
2101 0 : cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
2102 : }
2103 0 : vp9_frameworker_lock_stats(worker);
2104 0 : pbi->cur_buf->row = -1;
2105 0 : pbi->cur_buf->col = -1;
2106 0 : frame_worker_data->frame_context_ready = 1;
2107 : // Signal the main thread that context is ready.
2108 0 : vp9_frameworker_signal_stats(worker);
2109 0 : vp9_frameworker_unlock_stats(worker);
2110 : }
2111 :
2112 0 : if (pbi->tile_worker_data == NULL ||
2113 0 : (tile_cols * tile_rows) != pbi->total_tiles) {
2114 0 : const int num_tile_workers =
2115 0 : tile_cols * tile_rows + ((pbi->max_threads > 1) ? pbi->max_threads : 0);
2116 0 : const size_t twd_size = num_tile_workers * sizeof(*pbi->tile_worker_data);
2117 : // Ensure tile data offsets will be properly aligned. This may fail on
2118 : // platforms without DECLARE_ALIGNED().
2119 : assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
2120 0 : vpx_free(pbi->tile_worker_data);
2121 0 : CHECK_MEM_ERROR(cm, pbi->tile_worker_data, vpx_memalign(32, twd_size));
2122 0 : pbi->total_tiles = tile_rows * tile_cols;
2123 : }
2124 :
2125 0 : if (pbi->max_threads > 1 && tile_rows == 1 && tile_cols > 1) {
2126 : // Multi-threaded tile decoder
2127 0 : *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
2128 0 : if (!xd->corrupted) {
2129 0 : if (!cm->skip_loop_filter) {
2130 : // If multiple threads are used to decode tiles, then we use those
2131 : // threads to do parallel loopfiltering.
2132 0 : vp9_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane, cm->lf.filter_level,
2133 : 0, 0, pbi->tile_workers, pbi->num_tile_workers,
2134 : &pbi->lf_row_sync);
2135 : }
2136 : } else {
2137 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2138 : "Decode failed. Frame data is corrupted.");
2139 : }
2140 : } else {
2141 0 : *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
2142 : }
2143 :
2144 0 : if (!xd->corrupted) {
2145 0 : if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
2146 0 : vp9_adapt_coef_probs(cm);
2147 :
2148 0 : if (!frame_is_intra_only(cm)) {
2149 0 : vp9_adapt_mode_probs(cm);
2150 0 : vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
2151 : }
2152 : }
2153 : } else {
2154 0 : vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
2155 : "Decode failed. Frame data is corrupted.");
2156 : }
2157 :
2158 : // Non frame parallel update frame context here.
2159 0 : if (cm->refresh_frame_context && !context_updated)
2160 0 : cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
2161 : }
|