/src/libvpx/vp9/decoder/vp9_decodeframe.c
Line | Count | Source (jump to first uncovered line) |
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 | | #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG |
27 | | #include "vpx_util/vpx_debug_util.h" |
28 | | #endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG |
29 | | |
30 | | #include "vp9/common/vp9_alloccommon.h" |
31 | | #include "vp9/common/vp9_common.h" |
32 | | #include "vp9/common/vp9_entropy.h" |
33 | | #include "vp9/common/vp9_entropymode.h" |
34 | | #include "vp9/common/vp9_idct.h" |
35 | | #include "vp9/common/vp9_thread_common.h" |
36 | | #include "vp9/common/vp9_pred_common.h" |
37 | | #include "vp9/common/vp9_quant_common.h" |
38 | | #include "vp9/common/vp9_reconintra.h" |
39 | | #include "vp9/common/vp9_reconinter.h" |
40 | | #include "vp9/common/vp9_seg_common.h" |
41 | | #include "vp9/common/vp9_tile_common.h" |
42 | | |
43 | | #include "vp9/decoder/vp9_decodeframe.h" |
44 | | #include "vp9/decoder/vp9_detokenize.h" |
45 | | #include "vp9/decoder/vp9_decodemv.h" |
46 | | #include "vp9/decoder/vp9_decoder.h" |
47 | | #include "vp9/decoder/vp9_dsubexp.h" |
48 | | #include "vp9/decoder/vp9_job_queue.h" |
49 | | |
50 | | #define MAX_VP9_HEADER_SIZE 80 |
51 | | |
52 | | typedef int (*predict_recon_func)(TileWorkerData *twd, MODE_INFO *const mi, |
53 | | int plane, int row, int col, TX_SIZE tx_size); |
54 | | |
55 | | typedef void (*intra_recon_func)(TileWorkerData *twd, MODE_INFO *const mi, |
56 | | int plane, int row, int col, TX_SIZE tx_size); |
57 | | |
58 | 377k | static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) { |
59 | 377k | return len != 0 && len <= (size_t)(end - start); |
60 | 377k | } |
61 | | |
62 | 96.8k | static int decode_unsigned_max(struct vpx_read_bit_buffer *rb, int max) { |
63 | 96.8k | const int data = vpx_rb_read_literal(rb, get_unsigned_bits(max)); |
64 | 96.8k | return data > max ? max : data; |
65 | 96.8k | } |
66 | | |
67 | 110k | static TX_MODE read_tx_mode(vpx_reader *r) { |
68 | 110k | TX_MODE tx_mode = vpx_read_literal(r, 2); |
69 | 110k | if (tx_mode == ALLOW_32X32) tx_mode += vpx_read_bit(r); |
70 | 110k | return tx_mode; |
71 | 110k | } |
72 | | |
73 | 9.29k | static void read_tx_mode_probs(struct tx_probs *tx_probs, vpx_reader *r) { |
74 | 9.29k | int i, j; |
75 | | |
76 | 27.8k | for (i = 0; i < TX_SIZE_CONTEXTS; ++i) |
77 | 37.1k | for (j = 0; j < TX_SIZES - 3; ++j) |
78 | 18.5k | vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]); |
79 | | |
80 | 27.8k | for (i = 0; i < TX_SIZE_CONTEXTS; ++i) |
81 | 55.7k | for (j = 0; j < TX_SIZES - 2; ++j) |
82 | 37.1k | vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]); |
83 | | |
84 | 27.8k | for (i = 0; i < TX_SIZE_CONTEXTS; ++i) |
85 | 74.3k | for (j = 0; j < TX_SIZES - 1; ++j) |
86 | 55.7k | vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]); |
87 | 9.29k | } |
88 | | |
89 | 13.9k | static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vpx_reader *r) { |
90 | 13.9k | int i, j; |
91 | 69.5k | for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) |
92 | 166k | for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i) |
93 | 111k | vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]); |
94 | 13.9k | } |
95 | | |
96 | 80.2k | static void read_inter_mode_probs(FRAME_CONTEXT *fc, vpx_reader *r) { |
97 | 80.2k | int i, j; |
98 | 641k | for (i = 0; i < INTER_MODE_CONTEXTS; ++i) |
99 | 2.24M | for (j = 0; j < INTER_MODES - 1; ++j) |
100 | 1.68M | vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]); |
101 | 80.2k | } |
102 | | |
103 | | static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm, |
104 | 80.2k | vpx_reader *r) { |
105 | 80.2k | if (vp9_compound_reference_allowed(cm)) { |
106 | 69.1k | return vpx_read_bit(r) |
107 | 69.1k | ? (vpx_read_bit(r) ? REFERENCE_MODE_SELECT : COMPOUND_REFERENCE) |
108 | 69.1k | : SINGLE_REFERENCE; |
109 | 69.1k | } else { |
110 | 11.0k | return SINGLE_REFERENCE; |
111 | 11.0k | } |
112 | 80.2k | } |
113 | | |
114 | 80.2k | static void read_frame_reference_mode_probs(VP9_COMMON *cm, vpx_reader *r) { |
115 | 80.2k | FRAME_CONTEXT *const fc = cm->fc; |
116 | 80.2k | int i; |
117 | | |
118 | 80.2k | if (cm->reference_mode == REFERENCE_MODE_SELECT) |
119 | 44.7k | for (i = 0; i < COMP_INTER_CONTEXTS; ++i) |
120 | 37.2k | vp9_diff_update_prob(r, &fc->comp_inter_prob[i]); |
121 | | |
122 | 80.2k | if (cm->reference_mode != COMPOUND_REFERENCE) |
123 | 427k | for (i = 0; i < REF_CONTEXTS; ++i) { |
124 | 356k | vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]); |
125 | 356k | vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]); |
126 | 356k | } |
127 | | |
128 | 80.2k | if (cm->reference_mode != SINGLE_REFERENCE) |
129 | 98.1k | for (i = 0; i < REF_CONTEXTS; ++i) |
130 | 81.7k | vp9_diff_update_prob(r, &fc->comp_ref_prob[i]); |
131 | 80.2k | } |
132 | | |
133 | 1.45M | static void update_mv_probs(vpx_prob *p, int n, vpx_reader *r) { |
134 | 1.45M | int i; |
135 | 6.91M | for (i = 0; i < n; ++i) |
136 | 5.46M | if (vpx_read(r, MV_UPDATE_PROB)) p[i] = (vpx_read_literal(r, 7) << 1) | 1; |
137 | 1.45M | } |
138 | | |
139 | 80.2k | static void read_mv_probs(nmv_context *ctx, int allow_hp, vpx_reader *r) { |
140 | 80.2k | int i, j; |
141 | | |
142 | 80.2k | update_mv_probs(ctx->joints, MV_JOINTS - 1, r); |
143 | | |
144 | 240k | for (i = 0; i < 2; ++i) { |
145 | 160k | nmv_component *const comp_ctx = &ctx->comps[i]; |
146 | 160k | update_mv_probs(&comp_ctx->sign, 1, r); |
147 | 160k | update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r); |
148 | 160k | update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r); |
149 | 160k | update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r); |
150 | 160k | } |
151 | | |
152 | 240k | for (i = 0; i < 2; ++i) { |
153 | 160k | nmv_component *const comp_ctx = &ctx->comps[i]; |
154 | 481k | for (j = 0; j < CLASS0_SIZE; ++j) |
155 | 320k | update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r); |
156 | 160k | update_mv_probs(comp_ctx->fp, 3, r); |
157 | 160k | } |
158 | | |
159 | 80.2k | if (allow_hp) { |
160 | 185k | for (i = 0; i < 2; ++i) { |
161 | 123k | nmv_component *const comp_ctx = &ctx->comps[i]; |
162 | 123k | update_mv_probs(&comp_ctx->class0_hp, 1, r); |
163 | 123k | update_mv_probs(&comp_ctx->hp, 1, r); |
164 | 123k | } |
165 | 61.9k | } |
166 | 80.2k | } |
167 | | |
168 | | static void inverse_transform_block_inter(MACROBLOCKD *xd, int plane, |
169 | | const TX_SIZE tx_size, uint8_t *dst, |
170 | 3.09M | int stride, int eob) { |
171 | 3.09M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
172 | 3.09M | tran_low_t *const dqcoeff = pd->dqcoeff; |
173 | 3.09M | assert(eob > 0); |
174 | 3.09M | #if CONFIG_VP9_HIGHBITDEPTH |
175 | 3.09M | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
176 | 248k | uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst); |
177 | 248k | if (xd->lossless) { |
178 | 7.64k | vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd); |
179 | 241k | } else { |
180 | 241k | switch (tx_size) { |
181 | 151k | case TX_4X4: |
182 | 151k | vp9_highbd_idct4x4_add(dqcoeff, dst16, stride, eob, xd->bd); |
183 | 151k | break; |
184 | 43.5k | case TX_8X8: |
185 | 43.5k | vp9_highbd_idct8x8_add(dqcoeff, dst16, stride, eob, xd->bd); |
186 | 43.5k | break; |
187 | 28.2k | case TX_16X16: |
188 | 28.2k | vp9_highbd_idct16x16_add(dqcoeff, dst16, stride, eob, xd->bd); |
189 | 28.2k | break; |
190 | 18.2k | case TX_32X32: |
191 | 18.2k | vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd); |
192 | 18.2k | break; |
193 | 0 | default: assert(0 && "Invalid transform size"); |
194 | 241k | } |
195 | 241k | } |
196 | 2.84M | } else { |
197 | 2.84M | if (xd->lossless) { |
198 | 2.54k | vp9_iwht4x4_add(dqcoeff, dst, stride, eob); |
199 | 2.84M | } else { |
200 | 2.84M | switch (tx_size) { |
201 | 921k | case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break; |
202 | 859k | case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break; |
203 | 612k | case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break; |
204 | 448k | case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break; |
205 | 0 | default: assert(0 && "Invalid transform size"); return; |
206 | 2.84M | } |
207 | 2.84M | } |
208 | 2.84M | } |
209 | | #else |
210 | | if (xd->lossless) { |
211 | | vp9_iwht4x4_add(dqcoeff, dst, stride, eob); |
212 | | } else { |
213 | | switch (tx_size) { |
214 | | case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break; |
215 | | case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break; |
216 | | case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break; |
217 | | case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break; |
218 | | default: assert(0 && "Invalid transform size"); return; |
219 | | } |
220 | | } |
221 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
222 | | |
223 | 3.09M | if (eob == 1) { |
224 | 720k | dqcoeff[0] = 0; |
225 | 2.37M | } else { |
226 | 2.37M | if (tx_size <= TX_16X16 && eob <= 10) |
227 | 934k | memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0])); |
228 | 1.43M | else if (tx_size == TX_32X32 && eob <= 34) |
229 | 185k | memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0])); |
230 | 1.25M | else |
231 | 1.25M | memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0])); |
232 | 2.37M | } |
233 | 3.09M | } |
234 | | |
235 | | static void inverse_transform_block_intra(MACROBLOCKD *xd, int plane, |
236 | | const TX_TYPE tx_type, |
237 | | const TX_SIZE tx_size, uint8_t *dst, |
238 | 3.21M | int stride, int eob) { |
239 | 3.21M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
240 | 3.21M | tran_low_t *const dqcoeff = pd->dqcoeff; |
241 | 3.21M | assert(eob > 0); |
242 | 3.21M | #if CONFIG_VP9_HIGHBITDEPTH |
243 | 3.21M | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
244 | 1.73M | uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst); |
245 | 1.73M | if (xd->lossless) { |
246 | 310k | vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd); |
247 | 1.42M | } else { |
248 | 1.42M | switch (tx_size) { |
249 | 624k | case TX_4X4: |
250 | 624k | vp9_highbd_iht4x4_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd); |
251 | 624k | break; |
252 | 310k | case TX_8X8: |
253 | 310k | vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd); |
254 | 310k | break; |
255 | 200k | case TX_16X16: |
256 | 200k | vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd); |
257 | 200k | break; |
258 | 301k | case TX_32X32: |
259 | 301k | vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd); |
260 | 301k | break; |
261 | 0 | default: assert(0 && "Invalid transform size"); |
262 | 1.42M | } |
263 | 1.42M | } |
264 | 1.73M | } else { |
265 | 1.48M | if (xd->lossless) { |
266 | 96.2k | vp9_iwht4x4_add(dqcoeff, dst, stride, eob); |
267 | 1.38M | } else { |
268 | 1.38M | switch (tx_size) { |
269 | 470k | case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break; |
270 | 357k | case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break; |
271 | 251k | case TX_16X16: |
272 | 251k | vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob); |
273 | 251k | break; |
274 | 332k | case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break; |
275 | 0 | default: assert(0 && "Invalid transform size"); return; |
276 | 1.38M | } |
277 | 1.38M | } |
278 | 1.48M | } |
279 | | #else |
280 | | if (xd->lossless) { |
281 | | vp9_iwht4x4_add(dqcoeff, dst, stride, eob); |
282 | | } else { |
283 | | switch (tx_size) { |
284 | | case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break; |
285 | | case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break; |
286 | | case TX_16X16: |
287 | | vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob); |
288 | | break; |
289 | | case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break; |
290 | | default: assert(0 && "Invalid transform size"); return; |
291 | | } |
292 | | } |
293 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
294 | | |
295 | 3.22M | if (eob == 1) { |
296 | 687k | dqcoeff[0] = 0; |
297 | 2.53M | } else { |
298 | 2.53M | if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10) |
299 | 749k | memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0])); |
300 | 1.78M | else if (tx_size == TX_32X32 && eob <= 34) |
301 | 337k | memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0])); |
302 | 1.45M | else |
303 | 1.45M | memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0])); |
304 | 2.53M | } |
305 | 3.22M | } |
306 | | |
307 | | static void predict_and_reconstruct_intra_block(TileWorkerData *twd, |
308 | | MODE_INFO *const mi, int plane, |
309 | | int row, int col, |
310 | 862M | TX_SIZE tx_size) { |
311 | 862M | MACROBLOCKD *const xd = &twd->xd; |
312 | 862M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
313 | 862M | PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode; |
314 | 862M | uint8_t *dst; |
315 | 862M | dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col]; |
316 | | |
317 | 862M | if (mi->sb_type < BLOCK_8X8) |
318 | 8.01M | if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode; |
319 | | |
320 | 862M | vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride, |
321 | 862M | dst, pd->dst.stride, col, row, plane); |
322 | | |
323 | 862M | if (!mi->skip) { |
324 | 246M | const TX_TYPE tx_type = |
325 | 246M | (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode]; |
326 | 246M | const ScanOrder *sc = (plane || xd->lossless) |
327 | 246M | ? &vp9_default_scan_orders[tx_size] |
328 | 246M | : &vp9_scan_orders[tx_size][tx_type]; |
329 | 246M | const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size, |
330 | 246M | mi->segment_id); |
331 | 246M | if (eob > 0) { |
332 | 3.21M | inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst, |
333 | 3.21M | pd->dst.stride, eob); |
334 | 3.21M | } |
335 | 246M | } |
336 | 862M | } |
337 | | |
338 | | static void parse_intra_block_row_mt(TileWorkerData *twd, MODE_INFO *const mi, |
339 | | int plane, int row, int col, |
340 | 0 | TX_SIZE tx_size) { |
341 | 0 | MACROBLOCKD *const xd = &twd->xd; |
342 | 0 | PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode; |
343 | |
|
344 | 0 | if (mi->sb_type < BLOCK_8X8) |
345 | 0 | if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode; |
346 | |
|
347 | 0 | if (!mi->skip) { |
348 | 0 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
349 | 0 | const TX_TYPE tx_type = |
350 | 0 | (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode]; |
351 | 0 | const ScanOrder *sc = (plane || xd->lossless) |
352 | 0 | ? &vp9_default_scan_orders[tx_size] |
353 | 0 | : &vp9_scan_orders[tx_size][tx_type]; |
354 | 0 | *pd->eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size, |
355 | 0 | mi->segment_id); |
356 | | /* Keep the alignment to 16 */ |
357 | 0 | pd->dqcoeff += (16 << (tx_size << 1)); |
358 | 0 | pd->eob++; |
359 | 0 | } |
360 | 0 | } |
361 | | |
362 | | static void predict_and_reconstruct_intra_block_row_mt(TileWorkerData *twd, |
363 | | MODE_INFO *const mi, |
364 | | int plane, int row, |
365 | | int col, |
366 | 0 | TX_SIZE tx_size) { |
367 | 0 | MACROBLOCKD *const xd = &twd->xd; |
368 | 0 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
369 | 0 | PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode; |
370 | 0 | uint8_t *dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col]; |
371 | |
|
372 | 0 | if (mi->sb_type < BLOCK_8X8) |
373 | 0 | if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode; |
374 | |
|
375 | 0 | vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride, |
376 | 0 | dst, pd->dst.stride, col, row, plane); |
377 | |
|
378 | 0 | if (!mi->skip) { |
379 | 0 | const TX_TYPE tx_type = |
380 | 0 | (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode]; |
381 | 0 | if (*pd->eob > 0) { |
382 | 0 | inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst, |
383 | 0 | pd->dst.stride, *pd->eob); |
384 | 0 | } |
385 | | /* Keep the alignment to 16 */ |
386 | 0 | pd->dqcoeff += (16 << (tx_size << 1)); |
387 | 0 | pd->eob++; |
388 | 0 | } |
389 | 0 | } |
390 | | |
391 | | static int reconstruct_inter_block(TileWorkerData *twd, MODE_INFO *const mi, |
392 | | int plane, int row, int col, TX_SIZE tx_size, |
393 | 11.2M | int mi_row, int mi_col) { |
394 | 11.2M | MACROBLOCKD *const xd = &twd->xd; |
395 | 11.2M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
396 | 11.2M | const ScanOrder *sc = &vp9_default_scan_orders[tx_size]; |
397 | 11.2M | const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size, |
398 | 11.2M | mi->segment_id); |
399 | 11.2M | uint8_t *dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col]; |
400 | | |
401 | 11.2M | if (eob > 0) { |
402 | 3.09M | inverse_transform_block_inter(xd, plane, tx_size, dst, pd->dst.stride, eob); |
403 | 3.09M | } |
404 | | #if CONFIG_MISMATCH_DEBUG |
405 | | { |
406 | | int pixel_c, pixel_r; |
407 | | int blk_w = 1 << (tx_size + TX_UNIT_SIZE_LOG2); |
408 | | int blk_h = 1 << (tx_size + TX_UNIT_SIZE_LOG2); |
409 | | mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, col, row, |
410 | | pd->subsampling_x, pd->subsampling_y); |
411 | | mismatch_check_block_tx(dst, pd->dst.stride, plane, pixel_c, pixel_r, blk_w, |
412 | | blk_h, xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH); |
413 | | } |
414 | | #else |
415 | 11.2M | (void)mi_row; |
416 | 11.2M | (void)mi_col; |
417 | 11.2M | #endif |
418 | 11.2M | return eob; |
419 | 11.2M | } |
420 | | |
421 | | static int parse_inter_block_row_mt(TileWorkerData *twd, MODE_INFO *const mi, |
422 | | int plane, int row, int col, |
423 | 0 | TX_SIZE tx_size) { |
424 | 0 | MACROBLOCKD *const xd = &twd->xd; |
425 | 0 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
426 | 0 | const ScanOrder *sc = &vp9_default_scan_orders[tx_size]; |
427 | 0 | const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size, |
428 | 0 | mi->segment_id); |
429 | |
|
430 | 0 | *pd->eob = eob; |
431 | 0 | pd->dqcoeff += (16 << (tx_size << 1)); |
432 | 0 | pd->eob++; |
433 | |
|
434 | 0 | return eob; |
435 | 0 | } |
436 | | |
437 | | static int reconstruct_inter_block_row_mt(TileWorkerData *twd, |
438 | | MODE_INFO *const mi, int plane, |
439 | 0 | int row, int col, TX_SIZE tx_size) { |
440 | 0 | MACROBLOCKD *const xd = &twd->xd; |
441 | 0 | struct macroblockd_plane *const pd = &xd->plane[plane]; |
442 | 0 | const int eob = *pd->eob; |
443 | |
|
444 | 0 | (void)mi; |
445 | 0 | if (eob > 0) { |
446 | 0 | inverse_transform_block_inter( |
447 | 0 | xd, plane, tx_size, &pd->dst.buf[4 * row * pd->dst.stride + 4 * col], |
448 | 0 | pd->dst.stride, eob); |
449 | 0 | } |
450 | 0 | pd->dqcoeff += (16 << (tx_size << 1)); |
451 | 0 | pd->eob++; |
452 | |
|
453 | 0 | return eob; |
454 | 0 | } |
455 | | |
456 | | static void build_mc_border(const uint8_t *src, int src_stride, uint8_t *dst, |
457 | | int dst_stride, int x, int y, int b_w, int b_h, |
458 | 3.54M | int w, int h) { |
459 | | // Get a pointer to the start of the real data for this row. |
460 | 3.54M | const uint8_t *ref_row = src - x - y * src_stride; |
461 | | |
462 | 3.54M | if (y >= h) |
463 | 233k | ref_row += (h - 1) * src_stride; |
464 | 3.30M | else if (y > 0) |
465 | 2.16M | ref_row += y * src_stride; |
466 | | |
467 | 50.7M | do { |
468 | 50.7M | int right = 0, copy; |
469 | 50.7M | int left = x < 0 ? -x : 0; |
470 | | |
471 | 50.7M | if (left > b_w) left = b_w; |
472 | | |
473 | 50.7M | if (x + b_w > w) right = x + b_w - w; |
474 | | |
475 | 50.7M | if (right > b_w) right = b_w; |
476 | | |
477 | 50.7M | copy = b_w - left - right; |
478 | | |
479 | 50.7M | if (left) memset(dst, ref_row[0], left); |
480 | | |
481 | 50.7M | if (copy) memcpy(dst + left, ref_row + x + left, copy); |
482 | | |
483 | 50.7M | if (right) memset(dst + left + copy, ref_row[w - 1], right); |
484 | | |
485 | 50.7M | dst += dst_stride; |
486 | 50.7M | ++y; |
487 | | |
488 | 50.7M | if (y > 0 && y < h) ref_row += src_stride; |
489 | 50.7M | } while (--b_h); |
490 | 3.54M | } |
491 | | |
492 | | #if CONFIG_VP9_HIGHBITDEPTH |
493 | | static void high_build_mc_border(const uint8_t *src8, int src_stride, |
494 | | uint16_t *dst, int dst_stride, int x, int y, |
495 | 1.67M | int b_w, int b_h, int w, int h) { |
496 | | // Get a pointer to the start of the real data for this row. |
497 | 1.67M | const uint16_t *src = CONVERT_TO_SHORTPTR(src8); |
498 | 1.67M | const uint16_t *ref_row = src - x - y * src_stride; |
499 | | |
500 | 1.67M | if (y >= h) |
501 | 187k | ref_row += (h - 1) * src_stride; |
502 | 1.49M | else if (y > 0) |
503 | 649k | ref_row += y * src_stride; |
504 | | |
505 | 28.4M | do { |
506 | 28.4M | int right = 0, copy; |
507 | 28.4M | int left = x < 0 ? -x : 0; |
508 | | |
509 | 28.4M | if (left > b_w) left = b_w; |
510 | | |
511 | 28.4M | if (x + b_w > w) right = x + b_w - w; |
512 | | |
513 | 28.4M | if (right > b_w) right = b_w; |
514 | | |
515 | 28.4M | copy = b_w - left - right; |
516 | | |
517 | 28.4M | if (left) vpx_memset16(dst, ref_row[0], left); |
518 | | |
519 | 28.4M | if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t)); |
520 | | |
521 | 28.4M | if (right) vpx_memset16(dst + left + copy, ref_row[w - 1], right); |
522 | | |
523 | 28.4M | dst += dst_stride; |
524 | 28.4M | ++y; |
525 | | |
526 | 28.4M | if (y > 0 && y < h) ref_row += src_stride; |
527 | 28.4M | } while (--b_h); |
528 | 1.67M | } |
529 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
530 | | |
531 | | #if CONFIG_VP9_HIGHBITDEPTH |
532 | | static void extend_and_predict(TileWorkerData *twd, const uint8_t *buf_ptr1, |
533 | | int pre_buf_stride, int x0, int y0, int b_w, |
534 | | int b_h, int frame_width, int frame_height, |
535 | | int border_offset, uint8_t *const dst, |
536 | | int dst_buf_stride, int subpel_x, int subpel_y, |
537 | | const InterpKernel *kernel, |
538 | | const struct scale_factors *sf, MACROBLOCKD *xd, |
539 | 5.21M | int w, int h, int ref, int xs, int ys) { |
540 | 5.21M | uint16_t *mc_buf_high = twd->extend_and_predict_buf; |
541 | 5.21M | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
542 | 1.67M | high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w, x0, y0, |
543 | 1.67M | b_w, b_h, frame_width, frame_height); |
544 | 1.67M | highbd_inter_predictor(mc_buf_high + border_offset, b_w, |
545 | 1.67M | CONVERT_TO_SHORTPTR(dst), dst_buf_stride, subpel_x, |
546 | 1.67M | subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd); |
547 | 3.54M | } else { |
548 | 3.54M | build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w, x0, |
549 | 3.54M | y0, b_w, b_h, frame_width, frame_height); |
550 | 3.54M | inter_predictor(((uint8_t *)mc_buf_high) + border_offset, b_w, dst, |
551 | 3.54M | dst_buf_stride, subpel_x, subpel_y, sf, w, h, ref, kernel, |
552 | 3.54M | xs, ys); |
553 | 3.54M | } |
554 | 5.21M | } |
555 | | #else |
556 | | static void extend_and_predict(TileWorkerData *twd, const uint8_t *buf_ptr1, |
557 | | int pre_buf_stride, int x0, int y0, int b_w, |
558 | | int b_h, int frame_width, int frame_height, |
559 | | int border_offset, uint8_t *const dst, |
560 | | int dst_buf_stride, int subpel_x, int subpel_y, |
561 | | const InterpKernel *kernel, |
562 | | const struct scale_factors *sf, int w, int h, |
563 | | int ref, int xs, int ys) { |
564 | | uint8_t *mc_buf = (uint8_t *)twd->extend_and_predict_buf; |
565 | | const uint8_t *buf_ptr; |
566 | | |
567 | | build_mc_border(buf_ptr1, pre_buf_stride, mc_buf, b_w, x0, y0, b_w, b_h, |
568 | | frame_width, frame_height); |
569 | | buf_ptr = mc_buf + border_offset; |
570 | | |
571 | | inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x, subpel_y, sf, w, |
572 | | h, ref, kernel, xs, ys); |
573 | | } |
574 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
575 | | |
576 | | static void dec_build_inter_predictors( |
577 | | TileWorkerData *twd, MACROBLOCKD *xd, int plane, int bw, int bh, int x, |
578 | | int y, int w, int h, int mi_x, int mi_y, const InterpKernel *kernel, |
579 | | const struct scale_factors *sf, struct buf_2d *pre_buf, |
580 | | struct buf_2d *dst_buf, const MV *mv, RefCntBuffer *ref_frame_buf, |
581 | 57.4M | int is_scaled, int ref) { |
582 | 57.4M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
583 | 57.4M | uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x; |
584 | 57.4M | MV32 scaled_mv; |
585 | 57.4M | int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride, |
586 | 57.4M | subpel_x, subpel_y; |
587 | 57.4M | uint8_t *ref_frame, *buf_ptr; |
588 | | |
589 | | // Get reference frame pointer, width and height. |
590 | 57.4M | if (plane == 0) { |
591 | 22.6M | frame_width = ref_frame_buf->buf.y_crop_width; |
592 | 22.6M | frame_height = ref_frame_buf->buf.y_crop_height; |
593 | 22.6M | ref_frame = ref_frame_buf->buf.y_buffer; |
594 | 34.7M | } else { |
595 | 34.7M | frame_width = ref_frame_buf->buf.uv_crop_width; |
596 | 34.7M | frame_height = ref_frame_buf->buf.uv_crop_height; |
597 | 34.7M | ref_frame = |
598 | 34.7M | plane == 1 ? ref_frame_buf->buf.u_buffer : ref_frame_buf->buf.v_buffer; |
599 | 34.7M | } |
600 | | |
601 | 57.4M | if (is_scaled) { |
602 | 7.14M | const MV mv_q4 = clamp_mv_to_umv_border_sb( |
603 | 7.14M | xd, mv, bw, bh, pd->subsampling_x, pd->subsampling_y); |
604 | | // Co-ordinate of containing block to pixel precision. |
605 | 7.14M | int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)); |
606 | 7.14M | int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)); |
607 | | #if 0 // CONFIG_BETTER_HW_COMPATIBILITY |
608 | | assert(xd->mi[0]->sb_type != BLOCK_4X8 && |
609 | | xd->mi[0]->sb_type != BLOCK_8X4); |
610 | | assert(mv_q4.row == mv->row * (1 << (1 - pd->subsampling_y)) && |
611 | | mv_q4.col == mv->col * (1 << (1 - pd->subsampling_x))); |
612 | | #endif |
613 | | // Co-ordinate of the block to 1/16th pixel precision. |
614 | 7.14M | x0_16 = (x_start + x) << SUBPEL_BITS; |
615 | 7.14M | y0_16 = (y_start + y) << SUBPEL_BITS; |
616 | | |
617 | | // Co-ordinate of current block in reference frame |
618 | | // to 1/16th pixel precision. |
619 | 7.14M | x0_16 = sf->scale_value_x(x0_16, sf); |
620 | 7.14M | y0_16 = sf->scale_value_y(y0_16, sf); |
621 | | |
622 | | // Map the top left corner of the block into the reference frame. |
623 | 7.14M | x0 = sf->scale_value_x(x_start + x, sf); |
624 | 7.14M | y0 = sf->scale_value_y(y_start + y, sf); |
625 | | |
626 | | // Scale the MV and incorporate the sub-pixel offset of the block |
627 | | // in the reference frame. |
628 | 7.14M | scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf); |
629 | 7.14M | xs = sf->x_step_q4; |
630 | 7.14M | ys = sf->y_step_q4; |
631 | 50.2M | } else { |
632 | | // Co-ordinate of containing block to pixel precision. |
633 | 50.2M | x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x; |
634 | 50.2M | y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y; |
635 | | |
636 | | // Co-ordinate of the block to 1/16th pixel precision. |
637 | 50.2M | x0_16 = x0 << SUBPEL_BITS; |
638 | 50.2M | y0_16 = y0 << SUBPEL_BITS; |
639 | | |
640 | 50.2M | scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y)); |
641 | 50.2M | scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x)); |
642 | 50.2M | xs = ys = 16; |
643 | 50.2M | } |
644 | 57.4M | subpel_x = scaled_mv.col & SUBPEL_MASK; |
645 | 57.4M | subpel_y = scaled_mv.row & SUBPEL_MASK; |
646 | | |
647 | | // Calculate the top left corner of the best matching block in the |
648 | | // reference frame. |
649 | 57.4M | x0 += scaled_mv.col >> SUBPEL_BITS; |
650 | 57.4M | y0 += scaled_mv.row >> SUBPEL_BITS; |
651 | 57.4M | x0_16 += scaled_mv.col; |
652 | 57.4M | y0_16 += scaled_mv.row; |
653 | | |
654 | | // Get reference block pointer. |
655 | 57.4M | buf_ptr = ref_frame + y0 * pre_buf->stride + x0; |
656 | 57.4M | buf_stride = pre_buf->stride; |
657 | | |
658 | | // Do border extension if there is motion or the |
659 | | // width/height is not a multiple of 8 pixels. |
660 | 57.4M | if (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) || |
661 | 57.4M | (frame_height & 0x7)) { |
662 | 55.0M | int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1; |
663 | | |
664 | | // Get reference block bottom right horizontal coordinate. |
665 | 55.0M | int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1; |
666 | 55.0M | int x_pad = 0, y_pad = 0; |
667 | | |
668 | 55.0M | if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) { |
669 | 35.3M | x0 -= VP9_INTERP_EXTEND - 1; |
670 | 35.3M | x1 += VP9_INTERP_EXTEND; |
671 | 35.3M | x_pad = 1; |
672 | 35.3M | } |
673 | | |
674 | 55.0M | if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) { |
675 | 41.0M | y0 -= VP9_INTERP_EXTEND - 1; |
676 | 41.0M | y1 += VP9_INTERP_EXTEND; |
677 | 41.0M | y_pad = 1; |
678 | 41.0M | } |
679 | | |
680 | | // Skip border extension if block is inside the frame. |
681 | 55.0M | if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 || |
682 | 55.0M | y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) { |
683 | | // Extend the border. |
684 | 5.21M | const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0; |
685 | 5.21M | const int b_w = x1 - x0 + 1; |
686 | 5.21M | const int b_h = y1 - y0 + 1; |
687 | 5.21M | const int border_offset = y_pad * 3 * b_w + x_pad * 3; |
688 | | |
689 | 5.21M | extend_and_predict(twd, buf_ptr1, buf_stride, x0, y0, b_w, b_h, |
690 | 5.21M | frame_width, frame_height, border_offset, dst, |
691 | 5.21M | dst_buf->stride, subpel_x, subpel_y, kernel, sf, |
692 | 5.21M | #if CONFIG_VP9_HIGHBITDEPTH |
693 | 5.21M | xd, |
694 | 5.21M | #endif |
695 | 5.21M | w, h, ref, xs, ys); |
696 | 5.21M | return; |
697 | 5.21M | } |
698 | 55.0M | } |
699 | 52.1M | #if CONFIG_VP9_HIGHBITDEPTH |
700 | 52.1M | if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) { |
701 | 1.89M | highbd_inter_predictor(CONVERT_TO_SHORTPTR(buf_ptr), buf_stride, |
702 | 1.89M | CONVERT_TO_SHORTPTR(dst), dst_buf->stride, subpel_x, |
703 | 1.89M | subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd); |
704 | 50.2M | } else { |
705 | 50.2M | inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x, |
706 | 50.2M | subpel_y, sf, w, h, ref, kernel, xs, ys); |
707 | 50.2M | } |
708 | | #else |
709 | | inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x, subpel_y, |
710 | | sf, w, h, ref, kernel, xs, ys); |
711 | | #endif // CONFIG_VP9_HIGHBITDEPTH |
712 | 52.1M | } |
713 | | |
714 | | static void dec_build_inter_predictors_sb(TileWorkerData *twd, |
715 | | VP9Decoder *const pbi, |
716 | | MACROBLOCKD *xd, int mi_row, |
717 | 11.5M | int mi_col) { |
718 | 11.5M | int plane; |
719 | 11.5M | const int mi_x = mi_col * MI_SIZE; |
720 | 11.5M | const int mi_y = mi_row * MI_SIZE; |
721 | 11.5M | const MODE_INFO *mi = xd->mi[0]; |
722 | 11.5M | const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter]; |
723 | 11.5M | const BLOCK_SIZE sb_type = mi->sb_type; |
724 | 11.5M | const int is_compound = has_second_ref(mi); |
725 | 11.5M | int ref; |
726 | 11.5M | int is_scaled; |
727 | | |
728 | 28.1M | for (ref = 0; ref < 1 + is_compound; ++ref) { |
729 | 16.6M | const MV_REFERENCE_FRAME frame = mi->ref_frame[ref]; |
730 | 16.6M | RefBuffer *ref_buf = &pbi->common.frame_refs[frame - LAST_FRAME]; |
731 | 16.6M | const struct scale_factors *const sf = &ref_buf->sf; |
732 | 16.6M | const int idx = ref_buf->idx; |
733 | 16.6M | BufferPool *const pool = pbi->common.buffer_pool; |
734 | 16.6M | RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx]; |
735 | | |
736 | 16.6M | if (!vp9_is_valid_scale(sf)) |
737 | 16 | vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM, |
738 | 16 | "Reference frame has invalid dimensions"); |
739 | | |
740 | 16.6M | is_scaled = vp9_is_scaled(sf); |
741 | 16.6M | vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col, |
742 | 16.6M | is_scaled ? sf : NULL); |
743 | 16.6M | xd->block_refs[ref] = ref_buf; |
744 | | |
745 | 16.6M | if (sb_type < BLOCK_8X8) { |
746 | 8.00M | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
747 | 6.00M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
748 | 6.00M | struct buf_2d *const dst_buf = &pd->dst; |
749 | 6.00M | const int num_4x4_w = pd->n4_w; |
750 | 6.00M | const int num_4x4_h = pd->n4_h; |
751 | 6.00M | const int n4w_x4 = 4 * num_4x4_w; |
752 | 6.00M | const int n4h_x4 = 4 * num_4x4_h; |
753 | 6.00M | struct buf_2d *const pre_buf = &pd->pre[ref]; |
754 | 6.00M | int i = 0, x, y; |
755 | 14.5M | for (y = 0; y < num_4x4_h; ++y) { |
756 | 22.1M | for (x = 0; x < num_4x4_w; ++x) { |
757 | 13.6M | const MV mv = average_split_mvs(pd, mi, ref, i++); |
758 | 13.6M | dec_build_inter_predictors(twd, xd, plane, n4w_x4, n4h_x4, 4 * x, |
759 | 13.6M | 4 * y, 4, 4, mi_x, mi_y, kernel, sf, |
760 | 13.6M | pre_buf, dst_buf, &mv, ref_frame_buf, |
761 | 13.6M | is_scaled, ref); |
762 | 13.6M | } |
763 | 8.56M | } |
764 | 6.00M | } |
765 | 14.6M | } else { |
766 | 14.6M | const MV mv = mi->mv[ref].as_mv; |
767 | 58.4M | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
768 | 43.7M | struct macroblockd_plane *const pd = &xd->plane[plane]; |
769 | 43.7M | struct buf_2d *const dst_buf = &pd->dst; |
770 | 43.7M | const int num_4x4_w = pd->n4_w; |
771 | 43.7M | const int num_4x4_h = pd->n4_h; |
772 | 43.7M | const int n4w_x4 = 4 * num_4x4_w; |
773 | 43.7M | const int n4h_x4 = 4 * num_4x4_h; |
774 | 43.7M | struct buf_2d *const pre_buf = &pd->pre[ref]; |
775 | 43.7M | dec_build_inter_predictors(twd, xd, plane, n4w_x4, n4h_x4, 0, 0, n4w_x4, |
776 | 43.7M | n4h_x4, mi_x, mi_y, kernel, sf, pre_buf, |
777 | 43.7M | dst_buf, &mv, ref_frame_buf, is_scaled, ref); |
778 | 43.7M | } |
779 | 14.6M | } |
780 | 16.6M | } |
781 | 11.5M | } |
782 | | |
783 | 16.9M | static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) { |
784 | 16.9M | int i; |
785 | 67.9M | for (i = 0; i < MAX_MB_PLANE; i++) { |
786 | 50.9M | struct macroblockd_plane *const pd = &xd->plane[i]; |
787 | 50.9M | memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w); |
788 | 50.9M | memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h); |
789 | 50.9M | } |
790 | 16.9M | } |
791 | | |
792 | | static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl, |
793 | 22.4M | int bhl) { |
794 | 22.4M | int i; |
795 | 89.8M | for (i = 0; i < MAX_MB_PLANE; i++) { |
796 | 67.3M | xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x; |
797 | 67.3M | xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y; |
798 | 67.3M | xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x; |
799 | 67.3M | xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y; |
800 | 67.3M | } |
801 | 22.4M | } |
802 | | |
803 | | static MODE_INFO *set_offsets_recon(VP9_COMMON *const cm, MACROBLOCKD *const xd, |
804 | | int mi_row, int mi_col, int bw, int bh, |
805 | 0 | int bwl, int bhl) { |
806 | 0 | const int offset = mi_row * cm->mi_stride + mi_col; |
807 | 0 | const TileInfo *const tile = &xd->tile; |
808 | 0 | xd->mi = cm->mi_grid_visible + offset; |
809 | |
|
810 | 0 | set_plane_n4(xd, bw, bh, bwl, bhl); |
811 | |
|
812 | 0 | set_skip_context(xd, mi_row, mi_col); |
813 | | |
814 | | // Distance of Mb to the various image edges. These are specified to 8th pel |
815 | | // as they are always compared to values that are in 1/8th pel units |
816 | 0 | set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols); |
817 | |
|
818 | 0 | vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col); |
819 | 0 | return xd->mi[0]; |
820 | 0 | } |
821 | | |
822 | | static MODE_INFO *set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd, |
823 | | BLOCK_SIZE bsize, int mi_row, int mi_col, int bw, |
824 | 22.4M | int bh, int x_mis, int y_mis, int bwl, int bhl) { |
825 | 22.4M | const int offset = mi_row * cm->mi_stride + mi_col; |
826 | 22.4M | int x, y; |
827 | 22.4M | const TileInfo *const tile = &xd->tile; |
828 | | |
829 | 22.4M | xd->mi = cm->mi_grid_visible + offset; |
830 | 22.4M | xd->mi[0] = &cm->mi[offset]; |
831 | | // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of |
832 | | // passing bsize from decode_partition(). |
833 | 22.4M | xd->mi[0]->sb_type = bsize; |
834 | 96.9M | for (y = 0; y < y_mis; ++y) |
835 | 481M | for (x = !y; x < x_mis; ++x) { |
836 | 406M | xd->mi[y * cm->mi_stride + x] = xd->mi[0]; |
837 | 406M | } |
838 | | |
839 | 22.4M | set_plane_n4(xd, bw, bh, bwl, bhl); |
840 | | |
841 | 22.4M | set_skip_context(xd, mi_row, mi_col); |
842 | | |
843 | | // Distance of Mb to the various image edges. These are specified to 8th pel |
844 | | // as they are always compared to values that are in 1/8th pel units |
845 | 22.4M | set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols); |
846 | | |
847 | 22.4M | vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col); |
848 | 22.4M | return xd->mi[0]; |
849 | 22.4M | } |
850 | | |
851 | | static INLINE int predict_recon_inter(MACROBLOCKD *xd, MODE_INFO *mi, |
852 | | TileWorkerData *twd, |
853 | 0 | predict_recon_func func) { |
854 | 0 | int eobtotal = 0; |
855 | 0 | int plane; |
856 | 0 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
857 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
858 | 0 | const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size; |
859 | 0 | const int num_4x4_w = pd->n4_w; |
860 | 0 | const int num_4x4_h = pd->n4_h; |
861 | 0 | const int step = (1 << tx_size); |
862 | 0 | int row, col; |
863 | 0 | const int max_blocks_wide = |
864 | 0 | num_4x4_w + (xd->mb_to_right_edge >= 0 |
865 | 0 | ? 0 |
866 | 0 | : xd->mb_to_right_edge >> (5 + pd->subsampling_x)); |
867 | 0 | const int max_blocks_high = |
868 | 0 | num_4x4_h + (xd->mb_to_bottom_edge >= 0 |
869 | 0 | ? 0 |
870 | 0 | : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y)); |
871 | |
|
872 | 0 | xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide; |
873 | 0 | xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high; |
874 | |
|
875 | 0 | for (row = 0; row < max_blocks_high; row += step) |
876 | 0 | for (col = 0; col < max_blocks_wide; col += step) |
877 | 0 | eobtotal += func(twd, mi, plane, row, col, tx_size); |
878 | 0 | } |
879 | 0 | return eobtotal; |
880 | 0 | } |
881 | | |
882 | | static INLINE void predict_recon_intra(MACROBLOCKD *xd, MODE_INFO *mi, |
883 | | TileWorkerData *twd, |
884 | 0 | intra_recon_func func) { |
885 | 0 | int plane; |
886 | 0 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
887 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
888 | 0 | const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size; |
889 | 0 | const int num_4x4_w = pd->n4_w; |
890 | 0 | const int num_4x4_h = pd->n4_h; |
891 | 0 | const int step = (1 << tx_size); |
892 | 0 | int row, col; |
893 | 0 | const int max_blocks_wide = |
894 | 0 | num_4x4_w + (xd->mb_to_right_edge >= 0 |
895 | 0 | ? 0 |
896 | 0 | : xd->mb_to_right_edge >> (5 + pd->subsampling_x)); |
897 | 0 | const int max_blocks_high = |
898 | 0 | num_4x4_h + (xd->mb_to_bottom_edge >= 0 |
899 | 0 | ? 0 |
900 | 0 | : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y)); |
901 | |
|
902 | 0 | xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide; |
903 | 0 | xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high; |
904 | |
|
905 | 0 | for (row = 0; row < max_blocks_high; row += step) |
906 | 0 | for (col = 0; col < max_blocks_wide; col += step) |
907 | 0 | func(twd, mi, plane, row, col, tx_size); |
908 | 0 | } |
909 | 0 | } |
910 | | |
911 | | static void decode_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row, |
912 | 22.4M | int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) { |
913 | 22.4M | VP9_COMMON *const cm = &pbi->common; |
914 | 22.4M | const int less8x8 = bsize < BLOCK_8X8; |
915 | 22.4M | const int bw = 1 << (bwl - 1); |
916 | 22.4M | const int bh = 1 << (bhl - 1); |
917 | 22.4M | const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col); |
918 | 22.4M | const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row); |
919 | 22.4M | vpx_reader *r = &twd->bit_reader; |
920 | 22.4M | MACROBLOCKD *const xd = &twd->xd; |
921 | | |
922 | 22.4M | MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, |
923 | 22.4M | y_mis, bwl, bhl); |
924 | | |
925 | 22.4M | if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) { |
926 | 17.8M | const BLOCK_SIZE uv_subsize = |
927 | 17.8M | ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y]; |
928 | 17.8M | if (uv_subsize == BLOCK_INVALID) |
929 | 394 | vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME, |
930 | 394 | "Invalid block size."); |
931 | 17.8M | } |
932 | | |
933 | 22.4M | vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis); |
934 | | |
935 | 22.4M | if (mi->skip) { |
936 | 16.9M | dec_reset_skip_context(xd); |
937 | 16.9M | } |
938 | | |
939 | 22.4M | if (!is_inter_block(mi)) { |
940 | 10.9M | int plane; |
941 | 43.5M | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
942 | 32.6M | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
943 | 32.6M | const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size; |
944 | 32.6M | const int num_4x4_w = pd->n4_w; |
945 | 32.6M | const int num_4x4_h = pd->n4_h; |
946 | 32.6M | const int step = (1 << tx_size); |
947 | 32.6M | int row, col; |
948 | 32.6M | const int max_blocks_wide = |
949 | 32.6M | num_4x4_w + (xd->mb_to_right_edge >= 0 |
950 | 32.6M | ? 0 |
951 | 32.6M | : xd->mb_to_right_edge >> (5 + pd->subsampling_x)); |
952 | 32.6M | const int max_blocks_high = |
953 | 32.6M | num_4x4_h + (xd->mb_to_bottom_edge >= 0 |
954 | 32.6M | ? 0 |
955 | 32.6M | : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y)); |
956 | | |
957 | 32.6M | xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide; |
958 | 32.6M | xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high; |
959 | | |
960 | 145M | for (row = 0; row < max_blocks_high; row += step) |
961 | 975M | for (col = 0; col < max_blocks_wide; col += step) |
962 | 862M | predict_and_reconstruct_intra_block(twd, mi, plane, row, col, |
963 | 862M | tx_size); |
964 | 32.6M | } |
965 | 11.5M | } else { |
966 | | // Prediction |
967 | 11.5M | dec_build_inter_predictors_sb(twd, pbi, xd, mi_row, mi_col); |
968 | | #if CONFIG_MISMATCH_DEBUG |
969 | | { |
970 | | int plane; |
971 | | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
972 | | const struct macroblockd_plane *pd = &xd->plane[plane]; |
973 | | int pixel_c, pixel_r; |
974 | | const BLOCK_SIZE plane_bsize = |
975 | | get_plane_block_size(VPXMAX(bsize, BLOCK_8X8), &xd->plane[plane]); |
976 | | const int bw = get_block_width(plane_bsize); |
977 | | const int bh = get_block_height(plane_bsize); |
978 | | mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, 0, 0, |
979 | | pd->subsampling_x, pd->subsampling_y); |
980 | | mismatch_check_block_pre(pd->dst.buf, pd->dst.stride, plane, pixel_c, |
981 | | pixel_r, bw, bh, |
982 | | xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH); |
983 | | } |
984 | | } |
985 | | #endif |
986 | | |
987 | | // Reconstruction |
988 | 11.5M | if (!mi->skip) { |
989 | 1.83M | int eobtotal = 0; |
990 | 1.83M | int plane; |
991 | | |
992 | 7.32M | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
993 | 5.49M | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
994 | 5.49M | const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size; |
995 | 5.49M | const int num_4x4_w = pd->n4_w; |
996 | 5.49M | const int num_4x4_h = pd->n4_h; |
997 | 5.49M | const int step = (1 << tx_size); |
998 | 5.49M | int row, col; |
999 | 5.49M | const int max_blocks_wide = |
1000 | 5.49M | num_4x4_w + (xd->mb_to_right_edge >= 0 |
1001 | 5.49M | ? 0 |
1002 | 5.49M | : xd->mb_to_right_edge >> (5 + pd->subsampling_x)); |
1003 | 5.49M | const int max_blocks_high = |
1004 | 5.49M | num_4x4_h + |
1005 | 5.49M | (xd->mb_to_bottom_edge >= 0 |
1006 | 5.49M | ? 0 |
1007 | 5.49M | : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y)); |
1008 | | |
1009 | 5.49M | xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide; |
1010 | 5.49M | xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high; |
1011 | | |
1012 | 12.4M | for (row = 0; row < max_blocks_high; row += step) |
1013 | 18.2M | for (col = 0; col < max_blocks_wide; col += step) |
1014 | 11.2M | eobtotal += reconstruct_inter_block(twd, mi, plane, row, col, |
1015 | 11.2M | tx_size, mi_row, mi_col); |
1016 | 5.49M | } |
1017 | | |
1018 | 1.83M | if (!less8x8 && eobtotal == 0) mi->skip = 1; // skip loopfilter |
1019 | 1.83M | } |
1020 | 11.5M | } |
1021 | | |
1022 | 22.4M | xd->corrupted |= vpx_reader_has_error(r); |
1023 | | |
1024 | 22.4M | if (cm->lf.filter_level) { |
1025 | 19.0M | vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh); |
1026 | 19.0M | } |
1027 | 22.4M | } |
1028 | | |
1029 | | static void recon_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row, |
1030 | 0 | int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) { |
1031 | 0 | VP9_COMMON *const cm = &pbi->common; |
1032 | 0 | const int bw = 1 << (bwl - 1); |
1033 | 0 | const int bh = 1 << (bhl - 1); |
1034 | 0 | MACROBLOCKD *const xd = &twd->xd; |
1035 | |
|
1036 | 0 | MODE_INFO *mi = set_offsets_recon(cm, xd, mi_row, mi_col, bw, bh, bwl, bhl); |
1037 | |
|
1038 | 0 | if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) { |
1039 | 0 | const BLOCK_SIZE uv_subsize = |
1040 | 0 | ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y]; |
1041 | 0 | if (uv_subsize == BLOCK_INVALID) |
1042 | 0 | vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME, |
1043 | 0 | "Invalid block size."); |
1044 | 0 | } |
1045 | |
|
1046 | 0 | if (!is_inter_block(mi)) { |
1047 | 0 | predict_recon_intra(xd, mi, twd, |
1048 | 0 | predict_and_reconstruct_intra_block_row_mt); |
1049 | 0 | } else { |
1050 | | // Prediction |
1051 | 0 | dec_build_inter_predictors_sb(twd, pbi, xd, mi_row, mi_col); |
1052 | | |
1053 | | // Reconstruction |
1054 | 0 | if (!mi->skip) { |
1055 | 0 | predict_recon_inter(xd, mi, twd, reconstruct_inter_block_row_mt); |
1056 | 0 | } |
1057 | 0 | } |
1058 | |
|
1059 | 0 | vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh); |
1060 | 0 | } |
1061 | | |
1062 | | static void parse_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row, |
1063 | 0 | int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) { |
1064 | 0 | VP9_COMMON *const cm = &pbi->common; |
1065 | 0 | const int bw = 1 << (bwl - 1); |
1066 | 0 | const int bh = 1 << (bhl - 1); |
1067 | 0 | const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col); |
1068 | 0 | const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row); |
1069 | 0 | vpx_reader *r = &twd->bit_reader; |
1070 | 0 | MACROBLOCKD *const xd = &twd->xd; |
1071 | |
|
1072 | 0 | MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis, |
1073 | 0 | y_mis, bwl, bhl); |
1074 | |
|
1075 | 0 | if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) { |
1076 | 0 | const BLOCK_SIZE uv_subsize = |
1077 | 0 | ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y]; |
1078 | 0 | if (uv_subsize == BLOCK_INVALID) |
1079 | 0 | vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME, |
1080 | 0 | "Invalid block size."); |
1081 | 0 | } |
1082 | |
|
1083 | 0 | vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis); |
1084 | |
|
1085 | 0 | if (mi->skip) { |
1086 | 0 | dec_reset_skip_context(xd); |
1087 | 0 | } |
1088 | |
|
1089 | 0 | if (!is_inter_block(mi)) { |
1090 | 0 | predict_recon_intra(xd, mi, twd, parse_intra_block_row_mt); |
1091 | 0 | } else { |
1092 | 0 | if (!mi->skip) { |
1093 | 0 | tran_low_t *dqcoeff[MAX_MB_PLANE]; |
1094 | 0 | int *eob[MAX_MB_PLANE]; |
1095 | 0 | int plane; |
1096 | 0 | int eobtotal; |
1097 | | // Based on eobtotal and bsize, this may be mi->skip may be set to true |
1098 | | // In that case dqcoeff and eob need to be backed up and restored as |
1099 | | // recon_block will not increment these pointers for skip cases |
1100 | 0 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
1101 | 0 | const struct macroblockd_plane *const pd = &xd->plane[plane]; |
1102 | 0 | dqcoeff[plane] = pd->dqcoeff; |
1103 | 0 | eob[plane] = pd->eob; |
1104 | 0 | } |
1105 | 0 | eobtotal = predict_recon_inter(xd, mi, twd, parse_inter_block_row_mt); |
1106 | |
|
1107 | 0 | if (bsize >= BLOCK_8X8 && eobtotal == 0) { |
1108 | 0 | mi->skip = 1; // skip loopfilter |
1109 | 0 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
1110 | 0 | struct macroblockd_plane *pd = &xd->plane[plane]; |
1111 | 0 | pd->dqcoeff = dqcoeff[plane]; |
1112 | 0 | pd->eob = eob[plane]; |
1113 | 0 | } |
1114 | 0 | } |
1115 | 0 | } |
1116 | 0 | } |
1117 | |
|
1118 | 0 | xd->corrupted |= vpx_reader_has_error(r); |
1119 | 0 | } |
1120 | | |
1121 | | static INLINE int dec_partition_plane_context(TileWorkerData *twd, int mi_row, |
1122 | 25.1M | int mi_col, int bsl) { |
1123 | 25.1M | const PARTITION_CONTEXT *above_ctx = twd->xd.above_seg_context + mi_col; |
1124 | 25.1M | const PARTITION_CONTEXT *left_ctx = |
1125 | 25.1M | twd->xd.left_seg_context + (mi_row & MI_MASK); |
1126 | 25.1M | int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1; |
1127 | | |
1128 | | // assert(bsl >= 0); |
1129 | | |
1130 | 25.1M | return (left * 2 + above) + bsl * PARTITION_PLOFFSET; |
1131 | 25.1M | } |
1132 | | |
1133 | | static INLINE void dec_update_partition_context(TileWorkerData *twd, int mi_row, |
1134 | | int mi_col, BLOCK_SIZE subsize, |
1135 | 20.4M | int bw) { |
1136 | 20.4M | PARTITION_CONTEXT *const above_ctx = twd->xd.above_seg_context + mi_col; |
1137 | 20.4M | PARTITION_CONTEXT *const left_ctx = |
1138 | 20.4M | twd->xd.left_seg_context + (mi_row & MI_MASK); |
1139 | | |
1140 | | // update the partition context at the end notes. set partition bits |
1141 | | // of block sizes larger than the current one to be one, and partition |
1142 | | // bits of smaller block sizes to be zero. |
1143 | 20.4M | memset(above_ctx, partition_context_lookup[subsize].above, bw); |
1144 | 20.4M | memset(left_ctx, partition_context_lookup[subsize].left, bw); |
1145 | 20.4M | } |
1146 | | |
1147 | | static PARTITION_TYPE read_partition(TileWorkerData *twd, int mi_row, |
1148 | | int mi_col, int has_rows, int has_cols, |
1149 | 25.1M | int bsl) { |
1150 | 25.1M | const int ctx = dec_partition_plane_context(twd, mi_row, mi_col, bsl); |
1151 | 25.1M | const vpx_prob *const probs = twd->xd.partition_probs[ctx]; |
1152 | 25.1M | FRAME_COUNTS *counts = twd->xd.counts; |
1153 | 25.1M | PARTITION_TYPE p; |
1154 | 25.1M | vpx_reader *r = &twd->bit_reader; |
1155 | | |
1156 | 25.1M | if (has_rows && has_cols) |
1157 | 24.0M | p = (PARTITION_TYPE)vpx_read_tree(r, vp9_partition_tree, probs); |
1158 | 1.17M | else if (!has_rows && has_cols) |
1159 | 719k | p = vpx_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ; |
1160 | 459k | else if (has_rows && !has_cols) |
1161 | 400k | p = vpx_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT; |
1162 | 59.0k | else |
1163 | 59.0k | p = PARTITION_SPLIT; |
1164 | | |
1165 | 25.1M | if (counts) ++counts->partition[ctx][p]; |
1166 | | |
1167 | 25.1M | return p; |
1168 | 25.1M | } |
1169 | | |
1170 | | // TODO(slavarnway): eliminate bsize and subsize in future commits |
1171 | | static void decode_partition(TileWorkerData *twd, VP9Decoder *const pbi, |
1172 | | int mi_row, int mi_col, BLOCK_SIZE bsize, |
1173 | 26.4M | int n4x4_l2) { |
1174 | 26.4M | VP9_COMMON *const cm = &pbi->common; |
1175 | 26.4M | const int n8x8_l2 = n4x4_l2 - 1; |
1176 | 26.4M | const int num_8x8_wh = 1 << n8x8_l2; |
1177 | 26.4M | const int hbs = num_8x8_wh >> 1; |
1178 | 26.4M | PARTITION_TYPE partition; |
1179 | 26.4M | BLOCK_SIZE subsize; |
1180 | 26.4M | const int has_rows = (mi_row + hbs) < cm->mi_rows; |
1181 | 26.4M | const int has_cols = (mi_col + hbs) < cm->mi_cols; |
1182 | 26.4M | MACROBLOCKD *const xd = &twd->xd; |
1183 | | |
1184 | 26.4M | if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return; |
1185 | | |
1186 | 25.1M | partition = read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2); |
1187 | 25.1M | subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition); |
1188 | 25.1M | if (!hbs) { |
1189 | | // calculate bmode block dimensions (log 2) |
1190 | 7.57M | xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT); |
1191 | 7.57M | xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ); |
1192 | 7.57M | decode_block(twd, pbi, mi_row, mi_col, subsize, 1, 1); |
1193 | 17.6M | } else { |
1194 | 17.6M | switch (partition) { |
1195 | 10.2M | case PARTITION_NONE: |
1196 | 10.2M | decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2); |
1197 | 10.2M | break; |
1198 | 1.61M | case PARTITION_HORZ: |
1199 | 1.61M | decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2); |
1200 | 1.61M | if (has_rows) |
1201 | 1.24M | decode_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2, |
1202 | 1.24M | n8x8_l2); |
1203 | 1.61M | break; |
1204 | 1.02M | case PARTITION_VERT: |
1205 | 1.02M | decode_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2); |
1206 | 1.02M | if (has_cols) |
1207 | 815k | decode_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2, |
1208 | 815k | n4x4_l2); |
1209 | 1.02M | break; |
1210 | 4.79M | case PARTITION_SPLIT: |
1211 | 4.79M | decode_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2); |
1212 | 4.79M | decode_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2); |
1213 | 4.79M | decode_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2); |
1214 | 4.79M | decode_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize, |
1215 | 4.79M | n8x8_l2); |
1216 | 4.79M | break; |
1217 | 0 | default: assert(0 && "Invalid partition type"); |
1218 | 17.6M | } |
1219 | 17.6M | } |
1220 | | |
1221 | | // update partition context |
1222 | 25.2M | if (bsize >= BLOCK_8X8 && |
1223 | 25.2M | (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) |
1224 | 20.4M | dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh); |
1225 | 25.2M | } |
1226 | | |
1227 | | static void process_partition(TileWorkerData *twd, VP9Decoder *const pbi, |
1228 | | int mi_row, int mi_col, BLOCK_SIZE bsize, |
1229 | | int n4x4_l2, int parse_recon_flag, |
1230 | 0 | process_block_fn_t process_block) { |
1231 | 0 | VP9_COMMON *const cm = &pbi->common; |
1232 | 0 | const int n8x8_l2 = n4x4_l2 - 1; |
1233 | 0 | const int num_8x8_wh = 1 << n8x8_l2; |
1234 | 0 | const int hbs = num_8x8_wh >> 1; |
1235 | 0 | PARTITION_TYPE partition; |
1236 | 0 | BLOCK_SIZE subsize; |
1237 | 0 | const int has_rows = (mi_row + hbs) < cm->mi_rows; |
1238 | 0 | const int has_cols = (mi_col + hbs) < cm->mi_cols; |
1239 | 0 | MACROBLOCKD *const xd = &twd->xd; |
1240 | |
|
1241 | 0 | if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return; |
1242 | | |
1243 | 0 | if (parse_recon_flag & PARSE) { |
1244 | 0 | *xd->partition = |
1245 | 0 | read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2); |
1246 | 0 | } |
1247 | |
|
1248 | 0 | partition = *xd->partition; |
1249 | 0 | xd->partition++; |
1250 | |
|
1251 | 0 | subsize = get_subsize(bsize, partition); |
1252 | 0 | if (!hbs) { |
1253 | | // calculate bmode block dimensions (log 2) |
1254 | 0 | xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT); |
1255 | 0 | xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ); |
1256 | 0 | process_block(twd, pbi, mi_row, mi_col, subsize, 1, 1); |
1257 | 0 | } else { |
1258 | 0 | switch (partition) { |
1259 | 0 | case PARTITION_NONE: |
1260 | 0 | process_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2); |
1261 | 0 | break; |
1262 | 0 | case PARTITION_HORZ: |
1263 | 0 | process_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2); |
1264 | 0 | if (has_rows) |
1265 | 0 | process_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2, |
1266 | 0 | n8x8_l2); |
1267 | 0 | break; |
1268 | 0 | case PARTITION_VERT: |
1269 | 0 | process_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2); |
1270 | 0 | if (has_cols) |
1271 | 0 | process_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2, |
1272 | 0 | n4x4_l2); |
1273 | 0 | break; |
1274 | 0 | case PARTITION_SPLIT: |
1275 | 0 | process_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, |
1276 | 0 | parse_recon_flag, process_block); |
1277 | 0 | process_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2, |
1278 | 0 | parse_recon_flag, process_block); |
1279 | 0 | process_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2, |
1280 | 0 | parse_recon_flag, process_block); |
1281 | 0 | process_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize, |
1282 | 0 | n8x8_l2, parse_recon_flag, process_block); |
1283 | 0 | break; |
1284 | 0 | default: assert(0 && "Invalid partition type"); |
1285 | 0 | } |
1286 | 0 | } |
1287 | | |
1288 | 0 | if (parse_recon_flag & PARSE) { |
1289 | | // update partition context |
1290 | 0 | if ((bsize == BLOCK_8X8 || partition != PARTITION_SPLIT) && |
1291 | 0 | bsize >= BLOCK_8X8) |
1292 | 0 | dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh); |
1293 | 0 | } |
1294 | 0 | } |
1295 | | |
1296 | | static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end, |
1297 | | size_t read_size, |
1298 | | struct vpx_internal_error_info *error_info, |
1299 | | vpx_reader *r, vpx_decrypt_cb decrypt_cb, |
1300 | 157k | void *decrypt_state) { |
1301 | | // Validate the calculated partition length. If the buffer described by the |
1302 | | // partition can't be fully read then throw an error. |
1303 | 157k | if (!read_is_valid(data, read_size, data_end)) |
1304 | 21.6k | vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME, |
1305 | 21.6k | "Truncated packet or corrupt tile length"); |
1306 | | |
1307 | 157k | if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state)) |
1308 | 7.07k | vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR, |
1309 | 7.07k | "Failed to allocate bool decoder %d", 1); |
1310 | 157k | } |
1311 | | |
1312 | | static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs, |
1313 | 298k | vpx_reader *r) { |
1314 | 298k | int i, j, k, l, m; |
1315 | | |
1316 | 298k | if (vpx_read_bit(r)) |
1317 | 136k | for (i = 0; i < PLANE_TYPES; ++i) |
1318 | 272k | for (j = 0; j < REF_TYPES; ++j) |
1319 | 1.27M | for (k = 0; k < COEF_BANDS; ++k) |
1320 | 7.08M | for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l) |
1321 | 23.9M | for (m = 0; m < UNCONSTRAINED_NODES; ++m) |
1322 | 17.9M | vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]); |
1323 | 298k | } |
1324 | | |
1325 | 124k | static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, vpx_reader *r) { |
1326 | 124k | const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode]; |
1327 | 124k | TX_SIZE tx_size; |
1328 | 423k | for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size) |
1329 | 298k | read_coef_probs_common(fc->coef_probs[tx_size], r); |
1330 | 124k | } |
1331 | | |
1332 | | static void setup_segmentation(struct segmentation *seg, |
1333 | 202k | struct vpx_read_bit_buffer *rb) { |
1334 | 202k | int i, j; |
1335 | | |
1336 | 202k | seg->update_map = 0; |
1337 | 202k | seg->update_data = 0; |
1338 | | |
1339 | 202k | seg->enabled = vpx_rb_read_bit(rb); |
1340 | 202k | if (!seg->enabled) return; |
1341 | | |
1342 | | // Segmentation map update |
1343 | 36.5k | seg->update_map = vpx_rb_read_bit(rb); |
1344 | 36.5k | if (seg->update_map) { |
1345 | 173k | for (i = 0; i < SEG_TREE_PROBS; i++) |
1346 | 151k | seg->tree_probs[i] = |
1347 | 151k | vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB; |
1348 | | |
1349 | 22.5k | seg->temporal_update = vpx_rb_read_bit(rb); |
1350 | 22.5k | if (seg->temporal_update) { |
1351 | 18.2k | for (i = 0; i < PREDICTION_PROBS; i++) |
1352 | 13.6k | seg->pred_probs[i] = |
1353 | 13.6k | vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB; |
1354 | 18.0k | } else { |
1355 | 61.6k | for (i = 0; i < PREDICTION_PROBS; i++) seg->pred_probs[i] = MAX_PROB; |
1356 | 18.0k | } |
1357 | 22.5k | } |
1358 | | |
1359 | | // Segmentation data update |
1360 | 36.5k | seg->update_data = vpx_rb_read_bit(rb); |
1361 | 36.5k | if (seg->update_data) { |
1362 | 14.2k | seg->abs_delta = vpx_rb_read_bit(rb); |
1363 | | |
1364 | 14.2k | vp9_clearall_segfeatures(seg); |
1365 | | |
1366 | 116k | for (i = 0; i < MAX_SEGMENTS; i++) { |
1367 | 506k | for (j = 0; j < SEG_LVL_MAX; j++) { |
1368 | 404k | int data = 0; |
1369 | 404k | const int feature_enabled = vpx_rb_read_bit(rb); |
1370 | 404k | if (feature_enabled) { |
1371 | 96.8k | vp9_enable_segfeature(seg, i, j); |
1372 | 96.8k | data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j)); |
1373 | 96.8k | if (vp9_is_segfeature_signed(j)) |
1374 | 42.6k | data = vpx_rb_read_bit(rb) ? -data : data; |
1375 | 96.8k | } |
1376 | 404k | vp9_set_segdata(seg, i, j, data); |
1377 | 404k | } |
1378 | 102k | } |
1379 | 14.2k | } |
1380 | 36.5k | } |
1381 | | |
1382 | | static void setup_loopfilter(struct loopfilter *lf, |
1383 | 206k | struct vpx_read_bit_buffer *rb) { |
1384 | 206k | lf->filter_level = vpx_rb_read_literal(rb, 6); |
1385 | 206k | lf->sharpness_level = vpx_rb_read_literal(rb, 3); |
1386 | | |
1387 | | // Read in loop filter deltas applied at the MB level based on mode or ref |
1388 | | // frame. |
1389 | 206k | lf->mode_ref_delta_update = 0; |
1390 | | |
1391 | 206k | lf->mode_ref_delta_enabled = vpx_rb_read_bit(rb); |
1392 | 206k | if (lf->mode_ref_delta_enabled) { |
1393 | 82.6k | lf->mode_ref_delta_update = vpx_rb_read_bit(rb); |
1394 | 82.6k | if (lf->mode_ref_delta_update) { |
1395 | 23.8k | int i; |
1396 | | |
1397 | 119k | for (i = 0; i < MAX_REF_LF_DELTAS; i++) |
1398 | 95.3k | if (vpx_rb_read_bit(rb)) |
1399 | 33.2k | lf->ref_deltas[i] = vpx_rb_read_signed_literal(rb, 6); |
1400 | | |
1401 | 70.6k | for (i = 0; i < MAX_MODE_LF_DELTAS; i++) |
1402 | 46.8k | if (vpx_rb_read_bit(rb)) |
1403 | 19.1k | lf->mode_deltas[i] = vpx_rb_read_signed_literal(rb, 6); |
1404 | 23.8k | } |
1405 | 82.6k | } |
1406 | 206k | } |
1407 | | |
1408 | 610k | static INLINE int read_delta_q(struct vpx_read_bit_buffer *rb) { |
1409 | 610k | return vpx_rb_read_bit(rb) ? vpx_rb_read_signed_literal(rb, 4) : 0; |
1410 | 610k | } |
1411 | | |
1412 | | static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd, |
1413 | 205k | struct vpx_read_bit_buffer *rb) { |
1414 | 205k | cm->base_qindex = vpx_rb_read_literal(rb, QINDEX_BITS); |
1415 | 205k | cm->y_dc_delta_q = read_delta_q(rb); |
1416 | 205k | cm->uv_dc_delta_q = read_delta_q(rb); |
1417 | 205k | cm->uv_ac_delta_q = read_delta_q(rb); |
1418 | 205k | cm->dequant_bit_depth = cm->bit_depth; |
1419 | 205k | xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 && |
1420 | 205k | cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0; |
1421 | | |
1422 | 205k | #if CONFIG_VP9_HIGHBITDEPTH |
1423 | 205k | xd->bd = (int)cm->bit_depth; |
1424 | 205k | #endif |
1425 | 205k | } |
1426 | | |
1427 | 195k | static void setup_segmentation_dequant(VP9_COMMON *const cm) { |
1428 | | // Build y/uv dequant values based on segmentation. |
1429 | 195k | if (cm->seg.enabled) { |
1430 | 30.2k | int i; |
1431 | 272k | for (i = 0; i < MAX_SEGMENTS; ++i) { |
1432 | 242k | const int qindex = vp9_get_qindex(&cm->seg, i, cm->base_qindex); |
1433 | 242k | cm->y_dequant[i][0] = |
1434 | 242k | vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth); |
1435 | 242k | cm->y_dequant[i][1] = vp9_ac_quant(qindex, 0, cm->bit_depth); |
1436 | 242k | cm->uv_dequant[i][0] = |
1437 | 242k | vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth); |
1438 | 242k | cm->uv_dequant[i][1] = |
1439 | 242k | vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth); |
1440 | 242k | } |
1441 | 165k | } else { |
1442 | 165k | const int qindex = cm->base_qindex; |
1443 | | // When segmentation is disabled, only the first value is used. The |
1444 | | // remaining are don't cares. |
1445 | 165k | cm->y_dequant[0][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth); |
1446 | 165k | cm->y_dequant[0][1] = vp9_ac_quant(qindex, 0, cm->bit_depth); |
1447 | 165k | cm->uv_dequant[0][0] = |
1448 | 165k | vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth); |
1449 | 165k | cm->uv_dequant[0][1] = |
1450 | 165k | vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth); |
1451 | 165k | } |
1452 | 195k | } |
1453 | | |
1454 | 85.8k | static INTERP_FILTER read_interp_filter(struct vpx_read_bit_buffer *rb) { |
1455 | 85.8k | const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH, EIGHTTAP, |
1456 | 85.8k | EIGHTTAP_SHARP, BILINEAR }; |
1457 | 85.8k | return vpx_rb_read_bit(rb) ? SWITCHABLE |
1458 | 85.8k | : literal_to_filter[vpx_rb_read_literal(rb, 2)]; |
1459 | 85.8k | } |
1460 | | |
1461 | 207k | static void setup_render_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) { |
1462 | 207k | cm->render_width = cm->width; |
1463 | 207k | cm->render_height = cm->height; |
1464 | 207k | if (vpx_rb_read_bit(rb)) |
1465 | 5.86k | vp9_read_frame_size(rb, &cm->render_width, &cm->render_height); |
1466 | 207k | } |
1467 | | |
1468 | 65.5k | static void resize_mv_buffer(VP9_COMMON *cm) { |
1469 | 65.5k | vpx_free(cm->cur_frame->mvs); |
1470 | 65.5k | cm->cur_frame->mi_rows = cm->mi_rows; |
1471 | 65.5k | cm->cur_frame->mi_cols = cm->mi_cols; |
1472 | 65.5k | CHECK_MEM_ERROR(&cm->error, cm->cur_frame->mvs, |
1473 | 65.5k | (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols, |
1474 | 65.5k | sizeof(*cm->cur_frame->mvs))); |
1475 | 65.5k | } |
1476 | | |
1477 | 208k | static void resize_context_buffers(VP9_COMMON *cm, int width, int height) { |
1478 | 208k | #if CONFIG_SIZE_LIMIT |
1479 | 208k | if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT) |
1480 | 1.33k | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
1481 | 1.33k | "Dimensions of %dx%d beyond allowed size of %dx%d.", |
1482 | 1.33k | width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT); |
1483 | 208k | #endif |
1484 | 208k | if (cm->width != width || cm->height != height) { |
1485 | 113k | const int new_mi_rows = |
1486 | 113k | ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2; |
1487 | 113k | const int new_mi_cols = |
1488 | 113k | ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2; |
1489 | | |
1490 | | // Allocations in vp9_alloc_context_buffers() depend on individual |
1491 | | // dimensions as well as the overall size. |
1492 | 113k | if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) { |
1493 | 83.5k | if (vp9_alloc_context_buffers(cm, width, height)) { |
1494 | | // The cm->mi_* values have been cleared and any existing context |
1495 | | // buffers have been freed. Clear cm->width and cm->height to be |
1496 | | // consistent and to force a realloc next time. |
1497 | 0 | cm->width = 0; |
1498 | 0 | cm->height = 0; |
1499 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, |
1500 | 0 | "Failed to allocate context buffers"); |
1501 | 0 | } |
1502 | 83.5k | } else { |
1503 | 29.5k | vp9_set_mb_mi(cm, width, height); |
1504 | 29.5k | } |
1505 | 113k | vp9_init_context_buffers(cm); |
1506 | 113k | cm->width = width; |
1507 | 113k | cm->height = height; |
1508 | 113k | } |
1509 | 208k | if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows || |
1510 | 208k | cm->mi_cols > cm->cur_frame->mi_cols) { |
1511 | 65.5k | resize_mv_buffer(cm); |
1512 | 65.5k | } |
1513 | 208k | } |
1514 | | |
1515 | 123k | static void setup_frame_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) { |
1516 | 123k | int width, height; |
1517 | 123k | BufferPool *const pool = cm->buffer_pool; |
1518 | 123k | vp9_read_frame_size(rb, &width, &height); |
1519 | 123k | resize_context_buffers(cm, width, height); |
1520 | 123k | setup_render_size(cm, rb); |
1521 | | |
1522 | 123k | if (vpx_realloc_frame_buffer( |
1523 | 123k | get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x, |
1524 | 123k | cm->subsampling_y, |
1525 | 123k | #if CONFIG_VP9_HIGHBITDEPTH |
1526 | 123k | cm->use_highbitdepth, |
1527 | 123k | #endif |
1528 | 123k | VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment, |
1529 | 123k | &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb, |
1530 | 123k | pool->cb_priv)) { |
1531 | 22 | vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, |
1532 | 22 | "Failed to allocate frame buffer"); |
1533 | 22 | } |
1534 | | |
1535 | 123k | pool->frame_bufs[cm->new_fb_idx].released = 0; |
1536 | 123k | pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x; |
1537 | 123k | pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y; |
1538 | 123k | pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth; |
1539 | 123k | pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space; |
1540 | 123k | pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range; |
1541 | 123k | pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width; |
1542 | 123k | pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height; |
1543 | 123k | } |
1544 | | |
1545 | | static INLINE int valid_ref_frame_img_fmt(vpx_bit_depth_t ref_bit_depth, |
1546 | | int ref_xss, int ref_yss, |
1547 | | vpx_bit_depth_t this_bit_depth, |
1548 | 257k | int this_xss, int this_yss) { |
1549 | 257k | return ref_bit_depth == this_bit_depth && ref_xss == this_xss && |
1550 | 257k | ref_yss == this_yss; |
1551 | 257k | } |
1552 | | |
1553 | | static void setup_frame_size_with_refs(VP9_COMMON *cm, |
1554 | 86.4k | struct vpx_read_bit_buffer *rb) { |
1555 | 86.4k | int width, height; |
1556 | 86.4k | int found = 0, i; |
1557 | 86.4k | int has_valid_ref_frame = 0; |
1558 | 86.4k | BufferPool *const pool = cm->buffer_pool; |
1559 | 137k | for (i = 0; i < REFS_PER_FRAME; ++i) { |
1560 | 130k | if (vpx_rb_read_bit(rb)) { |
1561 | 79.4k | if (cm->frame_refs[i].idx != INVALID_IDX) { |
1562 | 79.4k | YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf; |
1563 | 79.4k | width = buf->y_crop_width; |
1564 | 79.4k | height = buf->y_crop_height; |
1565 | 79.4k | found = 1; |
1566 | 79.4k | break; |
1567 | 79.4k | } else { |
1568 | 46 | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
1569 | 46 | "Failed to decode frame size"); |
1570 | 46 | } |
1571 | 79.4k | } |
1572 | 130k | } |
1573 | | |
1574 | 86.4k | if (!found) vp9_read_frame_size(rb, &width, &height); |
1575 | | |
1576 | 86.4k | if (width <= 0 || height <= 0) |
1577 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
1578 | 0 | "Invalid frame size"); |
1579 | | |
1580 | | // Check to make sure at least one of frames that this frame references |
1581 | | // has valid dimensions. |
1582 | 345k | for (i = 0; i < REFS_PER_FRAME; ++i) { |
1583 | 259k | RefBuffer *const ref_frame = &cm->frame_refs[i]; |
1584 | 259k | has_valid_ref_frame |= |
1585 | 259k | (ref_frame->idx != INVALID_IDX && |
1586 | 259k | valid_ref_frame_size(ref_frame->buf->y_crop_width, |
1587 | 258k | ref_frame->buf->y_crop_height, width, height)); |
1588 | 259k | } |
1589 | 86.4k | if (!has_valid_ref_frame) |
1590 | 371 | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
1591 | 371 | "Referenced frame has invalid size"); |
1592 | 344k | for (i = 0; i < REFS_PER_FRAME; ++i) { |
1593 | 257k | RefBuffer *const ref_frame = &cm->frame_refs[i]; |
1594 | 257k | if (ref_frame->idx == INVALID_IDX || |
1595 | 257k | !valid_ref_frame_img_fmt(ref_frame->buf->bit_depth, |
1596 | 257k | ref_frame->buf->subsampling_x, |
1597 | 257k | ref_frame->buf->subsampling_y, cm->bit_depth, |
1598 | 257k | cm->subsampling_x, cm->subsampling_y)) |
1599 | 106 | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
1600 | 106 | "Referenced frame has incompatible color format"); |
1601 | 257k | } |
1602 | | |
1603 | 86.4k | resize_context_buffers(cm, width, height); |
1604 | 86.4k | setup_render_size(cm, rb); |
1605 | | |
1606 | 86.4k | if (vpx_realloc_frame_buffer( |
1607 | 86.4k | get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x, |
1608 | 86.4k | cm->subsampling_y, |
1609 | 86.4k | #if CONFIG_VP9_HIGHBITDEPTH |
1610 | 86.4k | cm->use_highbitdepth, |
1611 | 86.4k | #endif |
1612 | 86.4k | VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment, |
1613 | 86.4k | &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb, |
1614 | 86.4k | pool->cb_priv)) { |
1615 | 1 | vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, |
1616 | 1 | "Failed to allocate frame buffer"); |
1617 | 1 | } |
1618 | | |
1619 | 86.4k | pool->frame_bufs[cm->new_fb_idx].released = 0; |
1620 | 86.4k | pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x; |
1621 | 86.4k | pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y; |
1622 | 86.4k | pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth; |
1623 | 86.4k | pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space; |
1624 | 86.4k | pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range; |
1625 | 86.4k | pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width; |
1626 | 86.4k | pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height; |
1627 | 86.4k | } |
1628 | | |
1629 | 195k | static void setup_tile_info(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) { |
1630 | 195k | int min_log2_tile_cols, max_log2_tile_cols, max_ones; |
1631 | 195k | vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); |
1632 | | |
1633 | | // columns |
1634 | 195k | max_ones = max_log2_tile_cols - min_log2_tile_cols; |
1635 | 195k | cm->log2_tile_cols = min_log2_tile_cols; |
1636 | 212k | while (max_ones-- && vpx_rb_read_bit(rb)) cm->log2_tile_cols++; |
1637 | | |
1638 | 195k | if (cm->log2_tile_cols > 6) |
1639 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
1640 | 0 | "Invalid number of tile columns"); |
1641 | | |
1642 | | // rows |
1643 | 195k | cm->log2_tile_rows = vpx_rb_read_bit(rb); |
1644 | 195k | if (cm->log2_tile_rows) cm->log2_tile_rows += vpx_rb_read_bit(rb); |
1645 | 195k | } |
1646 | | |
1647 | | // Reads the next tile returning its size and adjusting '*data' accordingly |
1648 | | // based on 'is_last'. |
1649 | | static void get_tile_buffer(const uint8_t *const data_end, int is_last, |
1650 | | struct vpx_internal_error_info *error_info, |
1651 | | const uint8_t **data, vpx_decrypt_cb decrypt_cb, |
1652 | 166k | void *decrypt_state, TileBuffer *buf) { |
1653 | 166k | size_t size; |
1654 | | |
1655 | 166k | if (!is_last) { |
1656 | 43.3k | if (!read_is_valid(*data, 4, data_end)) |
1657 | 189 | vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME, |
1658 | 189 | "Truncated packet or corrupt tile length"); |
1659 | | |
1660 | 43.3k | if (decrypt_cb) { |
1661 | 0 | uint8_t be_data[4]; |
1662 | 0 | decrypt_cb(decrypt_state, *data, be_data, 4); |
1663 | 0 | size = mem_get_be32(be_data); |
1664 | 43.3k | } else { |
1665 | 43.3k | size = mem_get_be32(*data); |
1666 | 43.3k | } |
1667 | 43.3k | *data += 4; |
1668 | | |
1669 | 43.3k | if (size > (size_t)(data_end - *data)) |
1670 | 878 | vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME, |
1671 | 878 | "Truncated packet or corrupt tile size"); |
1672 | 122k | } else { |
1673 | 122k | size = data_end - *data; |
1674 | 122k | } |
1675 | | |
1676 | 166k | buf->data = *data; |
1677 | 166k | buf->size = size; |
1678 | | |
1679 | 166k | *data += size; |
1680 | 166k | } |
1681 | | |
1682 | | static void get_tile_buffers(VP9Decoder *pbi, const uint8_t *data, |
1683 | | const uint8_t *data_end, int tile_cols, |
1684 | | int tile_rows, |
1685 | 124k | TileBuffer (*tile_buffers)[1 << 6]) { |
1686 | 124k | int r, c; |
1687 | | |
1688 | 249k | for (r = 0; r < tile_rows; ++r) { |
1689 | 291k | for (c = 0; c < tile_cols; ++c) { |
1690 | 166k | const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1); |
1691 | 166k | TileBuffer *const buf = &tile_buffers[r][c]; |
1692 | 166k | buf->col = c; |
1693 | 166k | get_tile_buffer(data_end, is_last, &pbi->common.error, &data, |
1694 | 166k | pbi->decrypt_cb, pbi->decrypt_state, buf); |
1695 | 166k | } |
1696 | 125k | } |
1697 | 124k | } |
1698 | | |
1699 | | static void map_write(RowMTWorkerData *const row_mt_worker_data, int map_idx, |
1700 | 0 | int sync_idx) { |
1701 | 0 | #if CONFIG_MULTITHREAD |
1702 | 0 | pthread_mutex_lock(&row_mt_worker_data->recon_sync_mutex[sync_idx]); |
1703 | 0 | row_mt_worker_data->recon_map[map_idx] = 1; |
1704 | 0 | pthread_cond_signal(&row_mt_worker_data->recon_sync_cond[sync_idx]); |
1705 | 0 | pthread_mutex_unlock(&row_mt_worker_data->recon_sync_mutex[sync_idx]); |
1706 | | #else |
1707 | | (void)row_mt_worker_data; |
1708 | | (void)map_idx; |
1709 | | (void)sync_idx; |
1710 | | #endif // CONFIG_MULTITHREAD |
1711 | 0 | } |
1712 | | |
1713 | | static void map_read(RowMTWorkerData *const row_mt_worker_data, int map_idx, |
1714 | 0 | int sync_idx) { |
1715 | 0 | #if CONFIG_MULTITHREAD |
1716 | 0 | volatile int8_t *map = row_mt_worker_data->recon_map + map_idx; |
1717 | 0 | pthread_mutex_t *const mutex = |
1718 | 0 | &row_mt_worker_data->recon_sync_mutex[sync_idx]; |
1719 | 0 | pthread_mutex_lock(mutex); |
1720 | 0 | while (!(*map)) { |
1721 | 0 | pthread_cond_wait(&row_mt_worker_data->recon_sync_cond[sync_idx], mutex); |
1722 | 0 | } |
1723 | 0 | pthread_mutex_unlock(mutex); |
1724 | | #else |
1725 | | (void)row_mt_worker_data; |
1726 | | (void)map_idx; |
1727 | | (void)sync_idx; |
1728 | | #endif // CONFIG_MULTITHREAD |
1729 | 0 | } |
1730 | | |
1731 | 0 | static int lpf_map_write_check(VP9LfSync *lf_sync, int row, int num_tile_cols) { |
1732 | 0 | int return_val = 0; |
1733 | 0 | #if CONFIG_MULTITHREAD |
1734 | 0 | int corrupted; |
1735 | 0 | pthread_mutex_lock(lf_sync->lf_mutex); |
1736 | 0 | corrupted = lf_sync->corrupted; |
1737 | 0 | pthread_mutex_unlock(lf_sync->lf_mutex); |
1738 | 0 | if (!corrupted) { |
1739 | 0 | pthread_mutex_lock(&lf_sync->recon_done_mutex[row]); |
1740 | 0 | lf_sync->num_tiles_done[row] += 1; |
1741 | 0 | if (num_tile_cols == lf_sync->num_tiles_done[row]) return_val = 1; |
1742 | 0 | pthread_mutex_unlock(&lf_sync->recon_done_mutex[row]); |
1743 | 0 | } |
1744 | | #else |
1745 | | (void)lf_sync; |
1746 | | (void)row; |
1747 | | (void)num_tile_cols; |
1748 | | #endif |
1749 | 0 | return return_val; |
1750 | 0 | } |
1751 | | |
1752 | 0 | static void vp9_tile_done(VP9Decoder *pbi) { |
1753 | 0 | #if CONFIG_MULTITHREAD |
1754 | 0 | int terminate; |
1755 | 0 | RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data; |
1756 | 0 | const int all_parse_done = 1 << pbi->common.log2_tile_cols; |
1757 | 0 | pthread_mutex_lock(&row_mt_worker_data->recon_done_mutex); |
1758 | 0 | row_mt_worker_data->num_tiles_done++; |
1759 | 0 | terminate = all_parse_done == row_mt_worker_data->num_tiles_done; |
1760 | 0 | pthread_mutex_unlock(&row_mt_worker_data->recon_done_mutex); |
1761 | 0 | if (terminate) { |
1762 | 0 | vp9_jobq_terminate(&row_mt_worker_data->jobq); |
1763 | 0 | } |
1764 | | #else |
1765 | | (void)pbi; |
1766 | | #endif |
1767 | 0 | } |
1768 | | |
1769 | 0 | static void vp9_jobq_alloc(VP9Decoder *pbi) { |
1770 | 0 | VP9_COMMON *const cm = &pbi->common; |
1771 | 0 | RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data; |
1772 | 0 | const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows); |
1773 | 0 | const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2; |
1774 | 0 | const int tile_cols = 1 << cm->log2_tile_cols; |
1775 | 0 | const size_t jobq_size = (tile_cols * sb_rows * 2 + sb_rows) * sizeof(Job); |
1776 | |
|
1777 | 0 | if (jobq_size > row_mt_worker_data->jobq_size) { |
1778 | 0 | vpx_free(row_mt_worker_data->jobq_buf); |
1779 | 0 | CHECK_MEM_ERROR(&cm->error, row_mt_worker_data->jobq_buf, |
1780 | 0 | vpx_calloc(1, jobq_size)); |
1781 | 0 | vp9_jobq_init(&row_mt_worker_data->jobq, row_mt_worker_data->jobq_buf, |
1782 | 0 | jobq_size); |
1783 | 0 | row_mt_worker_data->jobq_size = jobq_size; |
1784 | 0 | } |
1785 | 0 | } |
1786 | | |
1787 | | static void recon_tile_row(TileWorkerData *tile_data, VP9Decoder *pbi, |
1788 | | int mi_row, int is_last_row, VP9LfSync *lf_sync, |
1789 | 0 | int cur_tile_col) { |
1790 | 0 | VP9_COMMON *const cm = &pbi->common; |
1791 | 0 | RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data; |
1792 | 0 | const int tile_cols = 1 << cm->log2_tile_cols; |
1793 | 0 | const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols); |
1794 | 0 | const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2; |
1795 | 0 | const int cur_sb_row = mi_row >> MI_BLOCK_SIZE_LOG2; |
1796 | 0 | int mi_col_start = tile_data->xd.tile.mi_col_start; |
1797 | 0 | int mi_col_end = tile_data->xd.tile.mi_col_end; |
1798 | 0 | int mi_col; |
1799 | |
|
1800 | 0 | vp9_zero(tile_data->xd.left_context); |
1801 | 0 | vp9_zero(tile_data->xd.left_seg_context); |
1802 | 0 | for (mi_col = mi_col_start; mi_col < mi_col_end; mi_col += MI_BLOCK_SIZE) { |
1803 | 0 | const int c = mi_col >> MI_BLOCK_SIZE_LOG2; |
1804 | 0 | int plane; |
1805 | 0 | const int sb_num = (cur_sb_row * (aligned_cols >> MI_BLOCK_SIZE_LOG2) + c); |
1806 | | |
1807 | | // Top Dependency |
1808 | 0 | if (cur_sb_row) { |
1809 | 0 | map_read(row_mt_worker_data, ((cur_sb_row - 1) * sb_cols) + c, |
1810 | 0 | ((cur_sb_row - 1) * tile_cols) + cur_tile_col); |
1811 | 0 | } |
1812 | |
|
1813 | 0 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
1814 | 0 | tile_data->xd.plane[plane].eob = |
1815 | 0 | row_mt_worker_data->eob[plane] + (sb_num << EOBS_PER_SB_LOG2); |
1816 | 0 | tile_data->xd.plane[plane].dqcoeff = |
1817 | 0 | row_mt_worker_data->dqcoeff[plane] + (sb_num << DQCOEFFS_PER_SB_LOG2); |
1818 | 0 | } |
1819 | 0 | tile_data->xd.partition = |
1820 | 0 | row_mt_worker_data->partition + (sb_num * PARTITIONS_PER_SB); |
1821 | 0 | process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4, RECON, |
1822 | 0 | recon_block); |
1823 | 0 | if (cm->lf.filter_level && !cm->skip_loop_filter) { |
1824 | | // Queue LPF_JOB |
1825 | 0 | int is_lpf_job_ready = 0; |
1826 | |
|
1827 | 0 | if (mi_col + MI_BLOCK_SIZE >= mi_col_end) { |
1828 | | // Checks if this row has been decoded in all tiles |
1829 | 0 | is_lpf_job_ready = lpf_map_write_check(lf_sync, cur_sb_row, tile_cols); |
1830 | |
|
1831 | 0 | if (is_lpf_job_ready) { |
1832 | 0 | Job lpf_job; |
1833 | 0 | lpf_job.job_type = LPF_JOB; |
1834 | 0 | if (cur_sb_row > 0) { |
1835 | 0 | lpf_job.row_num = mi_row - MI_BLOCK_SIZE; |
1836 | 0 | vp9_jobq_queue(&row_mt_worker_data->jobq, &lpf_job, |
1837 | 0 | sizeof(lpf_job)); |
1838 | 0 | } |
1839 | 0 | if (is_last_row) { |
1840 | 0 | lpf_job.row_num = mi_row; |
1841 | 0 | vp9_jobq_queue(&row_mt_worker_data->jobq, &lpf_job, |
1842 | 0 | sizeof(lpf_job)); |
1843 | 0 | } |
1844 | 0 | } |
1845 | 0 | } |
1846 | 0 | } |
1847 | 0 | map_write(row_mt_worker_data, (cur_sb_row * sb_cols) + c, |
1848 | 0 | (cur_sb_row * tile_cols) + cur_tile_col); |
1849 | 0 | } |
1850 | 0 | } |
1851 | | |
1852 | | static void parse_tile_row(TileWorkerData *tile_data, VP9Decoder *pbi, |
1853 | 0 | int mi_row, int cur_tile_col, uint8_t **data_end) { |
1854 | 0 | int mi_col; |
1855 | 0 | VP9_COMMON *const cm = &pbi->common; |
1856 | 0 | RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data; |
1857 | 0 | TileInfo *tile = &tile_data->xd.tile; |
1858 | 0 | TileBuffer *const buf = &pbi->tile_buffers[cur_tile_col]; |
1859 | 0 | const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols); |
1860 | |
|
1861 | 0 | vp9_zero(tile_data->dqcoeff); |
1862 | 0 | vp9_tile_init(tile, cm, 0, cur_tile_col); |
1863 | | |
1864 | | /* Update reader only at the beginning of each row in a tile */ |
1865 | 0 | if (mi_row == 0) { |
1866 | 0 | setup_token_decoder(buf->data, *data_end, buf->size, &tile_data->error_info, |
1867 | 0 | &tile_data->bit_reader, pbi->decrypt_cb, |
1868 | 0 | pbi->decrypt_state); |
1869 | 0 | } |
1870 | 0 | vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff); |
1871 | 0 | tile_data->xd.error_info = &tile_data->error_info; |
1872 | |
|
1873 | 0 | vp9_zero(tile_data->xd.left_context); |
1874 | 0 | vp9_zero(tile_data->xd.left_seg_context); |
1875 | 0 | for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end; |
1876 | 0 | mi_col += MI_BLOCK_SIZE) { |
1877 | 0 | const int r = mi_row >> MI_BLOCK_SIZE_LOG2; |
1878 | 0 | const int c = mi_col >> MI_BLOCK_SIZE_LOG2; |
1879 | 0 | int plane; |
1880 | 0 | const int sb_num = (r * (aligned_cols >> MI_BLOCK_SIZE_LOG2) + c); |
1881 | 0 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
1882 | 0 | tile_data->xd.plane[plane].eob = |
1883 | 0 | row_mt_worker_data->eob[plane] + (sb_num << EOBS_PER_SB_LOG2); |
1884 | 0 | tile_data->xd.plane[plane].dqcoeff = |
1885 | 0 | row_mt_worker_data->dqcoeff[plane] + (sb_num << DQCOEFFS_PER_SB_LOG2); |
1886 | 0 | } |
1887 | 0 | tile_data->xd.partition = |
1888 | 0 | row_mt_worker_data->partition + sb_num * PARTITIONS_PER_SB; |
1889 | 0 | process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4, PARSE, |
1890 | 0 | parse_block); |
1891 | 0 | } |
1892 | 0 | } |
1893 | | |
1894 | 0 | static int row_decode_worker_hook(void *arg1, void *arg2) { |
1895 | 0 | ThreadData *const thread_data = (ThreadData *)arg1; |
1896 | 0 | uint8_t **data_end = (uint8_t **)arg2; |
1897 | 0 | VP9Decoder *const pbi = thread_data->pbi; |
1898 | 0 | VP9_COMMON *const cm = &pbi->common; |
1899 | 0 | RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data; |
1900 | 0 | const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols); |
1901 | 0 | const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows); |
1902 | 0 | const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2; |
1903 | 0 | const int tile_cols = 1 << cm->log2_tile_cols; |
1904 | 0 | Job job; |
1905 | 0 | LFWorkerData *lf_data = thread_data->lf_data; |
1906 | 0 | VP9LfSync *lf_sync = thread_data->lf_sync; |
1907 | 0 | volatile int corrupted = 0; |
1908 | 0 | TileWorkerData *volatile tile_data_recon = NULL; |
1909 | |
|
1910 | 0 | while (!vp9_jobq_dequeue(&row_mt_worker_data->jobq, &job, sizeof(job), 1)) { |
1911 | 0 | int mi_col; |
1912 | 0 | const int mi_row = job.row_num; |
1913 | |
|
1914 | 0 | if (job.job_type == LPF_JOB) { |
1915 | 0 | lf_data->start = mi_row; |
1916 | 0 | lf_data->stop = lf_data->start + MI_BLOCK_SIZE; |
1917 | |
|
1918 | 0 | if (cm->lf.filter_level && !cm->skip_loop_filter && |
1919 | 0 | mi_row < cm->mi_rows) { |
1920 | 0 | vp9_loopfilter_job(lf_data, lf_sync); |
1921 | 0 | } |
1922 | 0 | } else if (job.job_type == RECON_JOB) { |
1923 | 0 | const int cur_sb_row = mi_row >> MI_BLOCK_SIZE_LOG2; |
1924 | 0 | const int is_last_row = sb_rows - 1 == cur_sb_row; |
1925 | 0 | int mi_col_start, mi_col_end; |
1926 | 0 | if (!tile_data_recon) |
1927 | 0 | CHECK_MEM_ERROR(&cm->error, tile_data_recon, |
1928 | 0 | vpx_memalign(32, sizeof(TileWorkerData))); |
1929 | | |
1930 | 0 | tile_data_recon->xd = pbi->mb; |
1931 | 0 | vp9_tile_init(&tile_data_recon->xd.tile, cm, 0, job.tile_col); |
1932 | 0 | vp9_init_macroblockd(cm, &tile_data_recon->xd, tile_data_recon->dqcoeff); |
1933 | 0 | mi_col_start = tile_data_recon->xd.tile.mi_col_start; |
1934 | 0 | mi_col_end = tile_data_recon->xd.tile.mi_col_end; |
1935 | |
|
1936 | 0 | if (setjmp(tile_data_recon->error_info.jmp)) { |
1937 | 0 | const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2; |
1938 | 0 | tile_data_recon->error_info.setjmp = 0; |
1939 | 0 | corrupted = 1; |
1940 | 0 | for (mi_col = mi_col_start; mi_col < mi_col_end; |
1941 | 0 | mi_col += MI_BLOCK_SIZE) { |
1942 | 0 | const int c = mi_col >> MI_BLOCK_SIZE_LOG2; |
1943 | 0 | map_write(row_mt_worker_data, (cur_sb_row * sb_cols) + c, |
1944 | 0 | (cur_sb_row * tile_cols) + job.tile_col); |
1945 | 0 | } |
1946 | 0 | if (is_last_row) { |
1947 | 0 | vp9_tile_done(pbi); |
1948 | 0 | } |
1949 | 0 | continue; |
1950 | 0 | } |
1951 | | |
1952 | 0 | tile_data_recon->error_info.setjmp = 1; |
1953 | 0 | tile_data_recon->xd.error_info = &tile_data_recon->error_info; |
1954 | |
|
1955 | 0 | recon_tile_row(tile_data_recon, pbi, mi_row, is_last_row, lf_sync, |
1956 | 0 | job.tile_col); |
1957 | |
|
1958 | 0 | if (corrupted) |
1959 | 0 | vpx_internal_error(&tile_data_recon->error_info, |
1960 | 0 | VPX_CODEC_CORRUPT_FRAME, |
1961 | 0 | "Failed to decode tile data"); |
1962 | |
|
1963 | 0 | if (is_last_row) { |
1964 | 0 | vp9_tile_done(pbi); |
1965 | 0 | } |
1966 | 0 | } else if (job.job_type == PARSE_JOB) { |
1967 | 0 | TileWorkerData *const tile_data = &pbi->tile_worker_data[job.tile_col]; |
1968 | |
|
1969 | 0 | if (setjmp(tile_data->error_info.jmp)) { |
1970 | 0 | tile_data->error_info.setjmp = 0; |
1971 | 0 | corrupted = 1; |
1972 | 0 | vp9_tile_done(pbi); |
1973 | 0 | continue; |
1974 | 0 | } |
1975 | | |
1976 | 0 | tile_data->xd = pbi->mb; |
1977 | 0 | tile_data->xd.counts = |
1978 | 0 | cm->frame_parallel_decoding_mode ? 0 : &tile_data->counts; |
1979 | |
|
1980 | 0 | tile_data->error_info.setjmp = 1; |
1981 | |
|
1982 | 0 | parse_tile_row(tile_data, pbi, mi_row, job.tile_col, data_end); |
1983 | |
|
1984 | 0 | corrupted |= tile_data->xd.corrupted; |
1985 | 0 | if (corrupted) |
1986 | 0 | vpx_internal_error(&tile_data->error_info, VPX_CODEC_CORRUPT_FRAME, |
1987 | 0 | "Failed to decode tile data"); |
1988 | | |
1989 | | /* Queue in the recon_job for this row */ |
1990 | 0 | { |
1991 | 0 | Job recon_job; |
1992 | 0 | recon_job.row_num = mi_row; |
1993 | 0 | recon_job.tile_col = job.tile_col; |
1994 | 0 | recon_job.job_type = RECON_JOB; |
1995 | 0 | vp9_jobq_queue(&row_mt_worker_data->jobq, &recon_job, |
1996 | 0 | sizeof(recon_job)); |
1997 | 0 | } |
1998 | | |
1999 | | /* Queue next parse job */ |
2000 | 0 | if (mi_row + MI_BLOCK_SIZE < cm->mi_rows) { |
2001 | 0 | Job parse_job; |
2002 | 0 | parse_job.row_num = mi_row + MI_BLOCK_SIZE; |
2003 | 0 | parse_job.tile_col = job.tile_col; |
2004 | 0 | parse_job.job_type = PARSE_JOB; |
2005 | 0 | vp9_jobq_queue(&row_mt_worker_data->jobq, &parse_job, |
2006 | 0 | sizeof(parse_job)); |
2007 | 0 | } |
2008 | 0 | } |
2009 | 0 | } |
2010 | | |
2011 | 0 | vpx_free(tile_data_recon); |
2012 | 0 | return !corrupted; |
2013 | 0 | } |
2014 | | |
2015 | | static const uint8_t *decode_tiles(VP9Decoder *pbi, const uint8_t *data, |
2016 | 109k | const uint8_t *data_end) { |
2017 | 109k | VP9_COMMON *const cm = &pbi->common; |
2018 | 109k | const VPxWorkerInterface *const winterface = vpx_get_worker_interface(); |
2019 | 109k | const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols); |
2020 | 109k | const int tile_cols = 1 << cm->log2_tile_cols; |
2021 | 109k | const int tile_rows = 1 << cm->log2_tile_rows; |
2022 | 109k | TileBuffer tile_buffers[4][1 << 6]; |
2023 | 109k | int tile_row, tile_col; |
2024 | 109k | int mi_row, mi_col; |
2025 | 109k | TileWorkerData *tile_data = NULL; |
2026 | | |
2027 | 109k | if (cm->lf.filter_level && !cm->skip_loop_filter && |
2028 | 109k | pbi->lf_worker.data1 == NULL) { |
2029 | 11.5k | CHECK_MEM_ERROR(&cm->error, pbi->lf_worker.data1, |
2030 | 11.5k | vpx_memalign(32, sizeof(LFWorkerData))); |
2031 | 11.5k | pbi->lf_worker.hook = vp9_loop_filter_worker; |
2032 | 11.5k | if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) { |
2033 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
2034 | 0 | "Loop filter thread creation failed"); |
2035 | 0 | } |
2036 | 11.5k | } |
2037 | | |
2038 | 109k | if (cm->lf.filter_level && !cm->skip_loop_filter) { |
2039 | 55.0k | LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1; |
2040 | | // Be sure to sync as we might be resuming after a failed frame decode. |
2041 | 55.0k | winterface->sync(&pbi->lf_worker); |
2042 | 55.0k | vp9_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm, |
2043 | 55.0k | pbi->mb.plane); |
2044 | 55.0k | } |
2045 | | |
2046 | 109k | assert(tile_rows <= 4); |
2047 | 109k | assert(tile_cols <= (1 << 6)); |
2048 | | |
2049 | | // Note: this memset assumes above_context[0], [1] and [2] |
2050 | | // are allocated as part of the same buffer. |
2051 | 109k | memset(cm->above_context, 0, |
2052 | 109k | sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols); |
2053 | | |
2054 | 109k | memset(cm->above_seg_context, 0, |
2055 | 109k | sizeof(*cm->above_seg_context) * aligned_cols); |
2056 | | |
2057 | 109k | vp9_reset_lfm(cm); |
2058 | | |
2059 | 109k | get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers); |
2060 | | |
2061 | | // Load all tile information into tile_data. |
2062 | 220k | for (tile_row = 0; tile_row < tile_rows; ++tile_row) { |
2063 | 221k | for (tile_col = 0; tile_col < tile_cols; ++tile_col) { |
2064 | 110k | const TileBuffer *const buf = &tile_buffers[tile_row][tile_col]; |
2065 | 110k | tile_data = pbi->tile_worker_data + tile_cols * tile_row + tile_col; |
2066 | 110k | tile_data->xd = pbi->mb; |
2067 | 110k | tile_data->xd.corrupted = 0; |
2068 | 110k | tile_data->xd.counts = |
2069 | 110k | cm->frame_parallel_decoding_mode ? NULL : &cm->counts; |
2070 | 110k | vp9_zero(tile_data->dqcoeff); |
2071 | 110k | vp9_tile_init(&tile_data->xd.tile, cm, tile_row, tile_col); |
2072 | 110k | setup_token_decoder(buf->data, data_end, buf->size, &cm->error, |
2073 | 110k | &tile_data->bit_reader, pbi->decrypt_cb, |
2074 | 110k | pbi->decrypt_state); |
2075 | 110k | vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff); |
2076 | 110k | } |
2077 | 110k | } |
2078 | | |
2079 | 212k | for (tile_row = 0; tile_row < tile_rows; ++tile_row) { |
2080 | 102k | TileInfo tile; |
2081 | 102k | vp9_tile_set_row(&tile, cm, tile_row); |
2082 | 516k | for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end; |
2083 | 413k | mi_row += MI_BLOCK_SIZE) { |
2084 | 828k | for (tile_col = 0; tile_col < tile_cols; ++tile_col) { |
2085 | 414k | const int col = |
2086 | 414k | pbi->inv_tile_order ? tile_cols - tile_col - 1 : tile_col; |
2087 | 414k | tile_data = pbi->tile_worker_data + tile_cols * tile_row + col; |
2088 | 414k | vp9_tile_set_col(&tile, cm, col); |
2089 | 414k | vp9_zero(tile_data->xd.left_context); |
2090 | 414k | vp9_zero(tile_data->xd.left_seg_context); |
2091 | 4.42M | for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end; |
2092 | 4.00M | mi_col += MI_BLOCK_SIZE) { |
2093 | 4.00M | if (pbi->row_mt == 1) { |
2094 | 0 | int plane; |
2095 | 0 | RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data; |
2096 | 0 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
2097 | 0 | tile_data->xd.plane[plane].eob = row_mt_worker_data->eob[plane]; |
2098 | 0 | tile_data->xd.plane[plane].dqcoeff = |
2099 | 0 | row_mt_worker_data->dqcoeff[plane]; |
2100 | 0 | } |
2101 | 0 | tile_data->xd.partition = row_mt_worker_data->partition; |
2102 | 0 | process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4, |
2103 | 0 | PARSE, parse_block); |
2104 | |
|
2105 | 0 | for (plane = 0; plane < MAX_MB_PLANE; ++plane) { |
2106 | 0 | tile_data->xd.plane[plane].eob = row_mt_worker_data->eob[plane]; |
2107 | 0 | tile_data->xd.plane[plane].dqcoeff = |
2108 | 0 | row_mt_worker_data->dqcoeff[plane]; |
2109 | 0 | } |
2110 | 0 | tile_data->xd.partition = row_mt_worker_data->partition; |
2111 | 0 | process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4, |
2112 | 0 | RECON, recon_block); |
2113 | 4.00M | } else { |
2114 | 4.00M | decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4); |
2115 | 4.00M | } |
2116 | 4.00M | } |
2117 | 414k | pbi->mb.corrupted |= tile_data->xd.corrupted; |
2118 | 414k | if (pbi->mb.corrupted) |
2119 | 12.5k | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
2120 | 12.5k | "Failed to decode tile data"); |
2121 | 414k | } |
2122 | | // Loopfilter one row. |
2123 | 413k | if (cm->lf.filter_level && !cm->skip_loop_filter) { |
2124 | 230k | const int lf_start = mi_row - MI_BLOCK_SIZE; |
2125 | 230k | LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1; |
2126 | | |
2127 | | // delay the loopfilter by 1 macroblock row. |
2128 | 230k | if (lf_start < 0) continue; |
2129 | | |
2130 | | // decoding has completed: finish up the loop filter in this thread. |
2131 | 182k | if (mi_row + MI_BLOCK_SIZE >= cm->mi_rows) continue; |
2132 | | |
2133 | 167k | winterface->sync(&pbi->lf_worker); |
2134 | 167k | lf_data->start = lf_start; |
2135 | 167k | lf_data->stop = mi_row; |
2136 | 167k | if (pbi->max_threads > 1) { |
2137 | 112k | winterface->launch(&pbi->lf_worker); |
2138 | 112k | } else { |
2139 | 55.5k | winterface->execute(&pbi->lf_worker); |
2140 | 55.5k | } |
2141 | 167k | } |
2142 | 413k | } |
2143 | 102k | } |
2144 | | |
2145 | | // Loopfilter remaining rows in the frame. |
2146 | 109k | if (cm->lf.filter_level && !cm->skip_loop_filter) { |
2147 | 46.2k | LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1; |
2148 | 46.2k | winterface->sync(&pbi->lf_worker); |
2149 | 46.2k | lf_data->start = lf_data->stop; |
2150 | 46.2k | lf_data->stop = cm->mi_rows; |
2151 | 46.2k | winterface->execute(&pbi->lf_worker); |
2152 | 46.2k | } |
2153 | | |
2154 | | // Get last tile data. |
2155 | 109k | tile_data = pbi->tile_worker_data + tile_cols * tile_rows - 1; |
2156 | | |
2157 | 109k | return vpx_reader_find_end(&tile_data->bit_reader); |
2158 | 109k | } |
2159 | | |
2160 | | static void set_rows_after_error(VP9LfSync *lf_sync, int start_row, int mi_rows, |
2161 | 7.58k | int num_tiles_left, int total_num_tiles) { |
2162 | 7.91k | do { |
2163 | 7.91k | int mi_row; |
2164 | 7.91k | const int aligned_rows = mi_cols_aligned_to_sb(mi_rows); |
2165 | 7.91k | const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2); |
2166 | 7.91k | const int corrupted = 1; |
2167 | 141k | for (mi_row = start_row; mi_row < mi_rows; mi_row += MI_BLOCK_SIZE) { |
2168 | 134k | const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2); |
2169 | 134k | vp9_set_row(lf_sync, total_num_tiles, mi_row >> MI_BLOCK_SIZE_LOG2, |
2170 | 134k | is_last_row, corrupted); |
2171 | 134k | } |
2172 | | /* If there are multiple tiles, the second tile should start marking row |
2173 | | * progress from row 0. |
2174 | | */ |
2175 | 7.91k | start_row = 0; |
2176 | 7.91k | } while (num_tiles_left--); |
2177 | 7.58k | } |
2178 | | |
2179 | | // On entry 'tile_data->data_end' points to the end of the input frame, on exit |
2180 | | // it is updated to reflect the bitreader position of the final tile column if |
2181 | | // present in the tile buffer group or NULL otherwise. |
2182 | 46.4k | static int tile_worker_hook(void *arg1, void *arg2) { |
2183 | 46.4k | TileWorkerData *const tile_data = (TileWorkerData *)arg1; |
2184 | 46.4k | VP9Decoder *const pbi = (VP9Decoder *)arg2; |
2185 | | |
2186 | 46.4k | TileInfo *volatile tile = &tile_data->xd.tile; |
2187 | 46.4k | const int final_col = (1 << pbi->common.log2_tile_cols) - 1; |
2188 | 46.4k | const uint8_t *volatile bit_reader_end = NULL; |
2189 | 46.4k | VP9_COMMON *cm = &pbi->common; |
2190 | | |
2191 | 46.4k | LFWorkerData *lf_data = tile_data->lf_data; |
2192 | 46.4k | VP9LfSync *lf_sync = tile_data->lf_sync; |
2193 | | |
2194 | 46.4k | volatile int mi_row = 0; |
2195 | 46.4k | volatile int n = tile_data->buf_start; |
2196 | 46.4k | if (setjmp(tile_data->error_info.jmp)) { |
2197 | 21.3k | tile_data->error_info.setjmp = 0; |
2198 | 21.3k | tile_data->xd.corrupted = 1; |
2199 | 21.3k | tile_data->data_end = NULL; |
2200 | 21.3k | if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) { |
2201 | 6.69k | const int num_tiles_left = tile_data->buf_end - n; |
2202 | 6.69k | const int mi_row_start = mi_row; |
2203 | 6.69k | set_rows_after_error(lf_sync, mi_row_start, cm->mi_rows, num_tiles_left, |
2204 | 6.69k | 1 << cm->log2_tile_cols); |
2205 | 6.69k | } |
2206 | 21.3k | return 0; |
2207 | 21.3k | } |
2208 | 25.0k | tile_data->error_info.setjmp = 1; |
2209 | | |
2210 | 25.0k | tile_data->xd.corrupted = 0; |
2211 | | |
2212 | 25.3k | do { |
2213 | 25.3k | int mi_col; |
2214 | 25.3k | const TileBuffer *const buf = pbi->tile_buffers + n; |
2215 | | |
2216 | | /* Initialize to 0 is safe since we do not deal with streams that have |
2217 | | * more than one row of tiles. (So tile->mi_row_start will be 0) |
2218 | | */ |
2219 | 25.3k | assert(cm->log2_tile_rows == 0); |
2220 | 46.6k | mi_row = 0; |
2221 | 46.6k | vp9_zero(tile_data->dqcoeff); |
2222 | 46.6k | vp9_tile_init(tile, &pbi->common, 0, buf->col); |
2223 | 46.6k | setup_token_decoder(buf->data, tile_data->data_end, buf->size, |
2224 | 46.6k | &tile_data->error_info, &tile_data->bit_reader, |
2225 | 46.6k | pbi->decrypt_cb, pbi->decrypt_state); |
2226 | 46.6k | vp9_init_macroblockd(&pbi->common, &tile_data->xd, tile_data->dqcoeff); |
2227 | | // init resets xd.error_info |
2228 | 46.6k | tile_data->xd.error_info = &tile_data->error_info; |
2229 | | |
2230 | 295k | for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end; |
2231 | 248k | mi_row += MI_BLOCK_SIZE) { |
2232 | 248k | vp9_zero(tile_data->xd.left_context); |
2233 | 248k | vp9_zero(tile_data->xd.left_seg_context); |
2234 | 3.55M | for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end; |
2235 | 3.30M | mi_col += MI_BLOCK_SIZE) { |
2236 | 3.30M | decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4); |
2237 | 3.30M | } |
2238 | 248k | if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) { |
2239 | 172k | const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows); |
2240 | 172k | const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2); |
2241 | 172k | const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2); |
2242 | 172k | vp9_set_row(lf_sync, 1 << cm->log2_tile_cols, |
2243 | 172k | mi_row >> MI_BLOCK_SIZE_LOG2, is_last_row, |
2244 | 172k | tile_data->xd.corrupted); |
2245 | 172k | } |
2246 | 248k | } |
2247 | | |
2248 | 46.6k | if (buf->col == final_col) { |
2249 | 12.6k | bit_reader_end = vpx_reader_find_end(&tile_data->bit_reader); |
2250 | 12.6k | } |
2251 | 46.6k | } while (!tile_data->xd.corrupted && ++n <= tile_data->buf_end); |
2252 | | |
2253 | 46.3k | if (pbi->lpf_mt_opt && n < tile_data->buf_end && cm->lf.filter_level && |
2254 | 46.3k | !cm->skip_loop_filter) { |
2255 | | /* This was not incremented in the tile loop, so increment before tiles left |
2256 | | * calculation |
2257 | | */ |
2258 | 897 | ++n; |
2259 | 897 | set_rows_after_error(lf_sync, 0, cm->mi_rows, tile_data->buf_end - n, |
2260 | 897 | 1 << cm->log2_tile_cols); |
2261 | 897 | } |
2262 | | |
2263 | 46.3k | if (pbi->lpf_mt_opt && !tile_data->xd.corrupted && cm->lf.filter_level && |
2264 | 46.3k | !cm->skip_loop_filter) { |
2265 | 11.7k | vp9_loopfilter_rows(lf_data, lf_sync); |
2266 | 11.7k | } |
2267 | | |
2268 | 46.3k | tile_data->data_end = bit_reader_end; |
2269 | 46.3k | return !tile_data->xd.corrupted; |
2270 | 25.0k | } |
2271 | | |
2272 | | // sorts in descending order |
2273 | 66.0k | static int compare_tile_buffers(const void *a, const void *b) { |
2274 | 66.0k | const TileBuffer *const buf_a = (const TileBuffer *)a; |
2275 | 66.0k | const TileBuffer *const buf_b = (const TileBuffer *)b; |
2276 | 66.0k | return (buf_a->size < buf_b->size) - (buf_a->size > buf_b->size); |
2277 | 66.0k | } |
2278 | | |
2279 | 14.5k | static INLINE void init_mt(VP9Decoder *pbi) { |
2280 | 14.5k | int n; |
2281 | 14.5k | VP9_COMMON *const cm = &pbi->common; |
2282 | 14.5k | VP9LfSync *lf_row_sync = &pbi->lf_row_sync; |
2283 | 14.5k | const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols); |
2284 | 14.5k | const VPxWorkerInterface *const winterface = vpx_get_worker_interface(); |
2285 | | |
2286 | 14.5k | if (pbi->num_tile_workers == 0) { |
2287 | 1.14k | const int num_threads = pbi->max_threads; |
2288 | 1.14k | CHECK_MEM_ERROR(&cm->error, pbi->tile_workers, |
2289 | 1.14k | vpx_malloc(num_threads * sizeof(*pbi->tile_workers))); |
2290 | 38.9k | for (n = 0; n < num_threads; ++n) { |
2291 | 37.7k | VPxWorker *const worker = &pbi->tile_workers[n]; |
2292 | 37.7k | ++pbi->num_tile_workers; |
2293 | | |
2294 | 37.7k | winterface->init(worker); |
2295 | 37.7k | if (n < num_threads - 1 && !winterface->reset(worker)) { |
2296 | 0 | do { |
2297 | 0 | winterface->end(&pbi->tile_workers[pbi->num_tile_workers - 1]); |
2298 | 0 | } while (--pbi->num_tile_workers != 0); |
2299 | 0 | vpx_free(pbi->tile_workers); |
2300 | 0 | pbi->tile_workers = NULL; |
2301 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_ERROR, |
2302 | 0 | "Tile decoder thread creation failed"); |
2303 | 0 | } |
2304 | 37.7k | } |
2305 | 1.14k | } |
2306 | | |
2307 | | // Initialize LPF |
2308 | 14.5k | if ((pbi->lpf_mt_opt || pbi->row_mt) && cm->lf.filter_level && |
2309 | 14.5k | !cm->skip_loop_filter) { |
2310 | 6.72k | vp9_lpf_mt_init(lf_row_sync, cm, cm->lf.filter_level, |
2311 | 6.72k | pbi->num_tile_workers); |
2312 | 6.72k | } |
2313 | | |
2314 | | // Note: this memset assumes above_context[0], [1] and [2] |
2315 | | // are allocated as part of the same buffer. |
2316 | 14.5k | memset(cm->above_context, 0, |
2317 | 14.5k | sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols); |
2318 | | |
2319 | 14.5k | memset(cm->above_seg_context, 0, |
2320 | 14.5k | sizeof(*cm->above_seg_context) * aligned_mi_cols); |
2321 | | |
2322 | 14.5k | vp9_reset_lfm(cm); |
2323 | 14.5k | } |
2324 | | |
2325 | | static const uint8_t *decode_tiles_row_wise_mt(VP9Decoder *pbi, |
2326 | | const uint8_t *data, |
2327 | 0 | const uint8_t *data_end) { |
2328 | 0 | VP9_COMMON *const cm = &pbi->common; |
2329 | 0 | RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data; |
2330 | 0 | const VPxWorkerInterface *const winterface = vpx_get_worker_interface(); |
2331 | 0 | const int tile_cols = 1 << cm->log2_tile_cols; |
2332 | 0 | const int tile_rows = 1 << cm->log2_tile_rows; |
2333 | 0 | const int num_workers = pbi->max_threads; |
2334 | 0 | int i, n; |
2335 | 0 | int col; |
2336 | 0 | int corrupted = 0; |
2337 | 0 | const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2; |
2338 | 0 | const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2; |
2339 | 0 | VP9LfSync *lf_row_sync = &pbi->lf_row_sync; |
2340 | 0 | YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm); |
2341 | |
|
2342 | 0 | assert(tile_cols <= (1 << 6)); |
2343 | 0 | assert(tile_rows == 1); |
2344 | 0 | (void)tile_rows; |
2345 | |
|
2346 | 0 | memset(row_mt_worker_data->recon_map, 0, |
2347 | 0 | sb_rows * sb_cols * sizeof(*row_mt_worker_data->recon_map)); |
2348 | |
|
2349 | 0 | init_mt(pbi); |
2350 | | |
2351 | | // Reset tile decoding hook |
2352 | 0 | for (n = 0; n < num_workers; ++n) { |
2353 | 0 | VPxWorker *const worker = &pbi->tile_workers[n]; |
2354 | 0 | ThreadData *const thread_data = &pbi->row_mt_worker_data->thread_data[n]; |
2355 | 0 | winterface->sync(worker); |
2356 | |
|
2357 | 0 | if (cm->lf.filter_level && !cm->skip_loop_filter) { |
2358 | 0 | thread_data->lf_sync = lf_row_sync; |
2359 | 0 | thread_data->lf_data = &thread_data->lf_sync->lfdata[n]; |
2360 | 0 | vp9_loop_filter_data_reset(thread_data->lf_data, new_fb, cm, |
2361 | 0 | pbi->mb.plane); |
2362 | 0 | } |
2363 | |
|
2364 | 0 | thread_data->pbi = pbi; |
2365 | |
|
2366 | 0 | worker->hook = row_decode_worker_hook; |
2367 | 0 | worker->data1 = thread_data; |
2368 | 0 | worker->data2 = (void *)&row_mt_worker_data->data_end; |
2369 | 0 | } |
2370 | |
|
2371 | 0 | for (col = 0; col < tile_cols; ++col) { |
2372 | 0 | TileWorkerData *const tile_data = &pbi->tile_worker_data[col]; |
2373 | 0 | tile_data->xd = pbi->mb; |
2374 | 0 | tile_data->xd.counts = |
2375 | 0 | cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts; |
2376 | 0 | } |
2377 | | |
2378 | | /* Reset the jobq to start of the jobq buffer */ |
2379 | 0 | vp9_jobq_reset(&row_mt_worker_data->jobq); |
2380 | 0 | row_mt_worker_data->num_tiles_done = 0; |
2381 | 0 | row_mt_worker_data->data_end = NULL; |
2382 | | |
2383 | | // Load tile data into tile_buffers |
2384 | 0 | get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, |
2385 | 0 | &pbi->tile_buffers); |
2386 | | |
2387 | | // Initialize thread frame counts. |
2388 | 0 | if (!cm->frame_parallel_decoding_mode) { |
2389 | 0 | for (col = 0; col < tile_cols; ++col) { |
2390 | 0 | TileWorkerData *const tile_data = &pbi->tile_worker_data[col]; |
2391 | 0 | vp9_zero(tile_data->counts); |
2392 | 0 | } |
2393 | 0 | } |
2394 | | |
2395 | | // queue parse jobs for 0th row of every tile |
2396 | 0 | for (col = 0; col < tile_cols; ++col) { |
2397 | 0 | Job parse_job; |
2398 | 0 | parse_job.row_num = 0; |
2399 | 0 | parse_job.tile_col = col; |
2400 | 0 | parse_job.job_type = PARSE_JOB; |
2401 | 0 | vp9_jobq_queue(&row_mt_worker_data->jobq, &parse_job, sizeof(parse_job)); |
2402 | 0 | } |
2403 | |
|
2404 | 0 | for (i = 0; i < num_workers; ++i) { |
2405 | 0 | VPxWorker *const worker = &pbi->tile_workers[i]; |
2406 | 0 | worker->had_error = 0; |
2407 | 0 | if (i == num_workers - 1) { |
2408 | 0 | winterface->execute(worker); |
2409 | 0 | } else { |
2410 | 0 | winterface->launch(worker); |
2411 | 0 | } |
2412 | 0 | } |
2413 | |
|
2414 | 0 | for (; n > 0; --n) { |
2415 | 0 | VPxWorker *const worker = &pbi->tile_workers[n - 1]; |
2416 | | // TODO(jzern): The tile may have specific error data associated with |
2417 | | // its vpx_internal_error_info which could be propagated to the main info |
2418 | | // in cm. Additionally once the threads have been synced and an error is |
2419 | | // detected, there's no point in continuing to decode tiles. |
2420 | 0 | corrupted |= !winterface->sync(worker); |
2421 | 0 | } |
2422 | |
|
2423 | 0 | pbi->mb.corrupted = corrupted; |
2424 | |
|
2425 | 0 | { |
2426 | | /* Set data end */ |
2427 | 0 | TileWorkerData *const tile_data = &pbi->tile_worker_data[tile_cols - 1]; |
2428 | 0 | row_mt_worker_data->data_end = vpx_reader_find_end(&tile_data->bit_reader); |
2429 | 0 | } |
2430 | | |
2431 | | // Accumulate thread frame counts. |
2432 | 0 | if (!cm->frame_parallel_decoding_mode) { |
2433 | 0 | for (i = 0; i < tile_cols; ++i) { |
2434 | 0 | TileWorkerData *const tile_data = &pbi->tile_worker_data[i]; |
2435 | 0 | vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1); |
2436 | 0 | } |
2437 | 0 | } |
2438 | |
|
2439 | 0 | return row_mt_worker_data->data_end; |
2440 | 0 | } |
2441 | | |
2442 | | static const uint8_t *decode_tiles_mt(VP9Decoder *pbi, const uint8_t *data, |
2443 | 14.5k | const uint8_t *data_end) { |
2444 | 14.5k | VP9_COMMON *const cm = &pbi->common; |
2445 | 14.5k | const VPxWorkerInterface *const winterface = vpx_get_worker_interface(); |
2446 | 14.5k | const uint8_t *bit_reader_end = NULL; |
2447 | 14.5k | VP9LfSync *lf_row_sync = &pbi->lf_row_sync; |
2448 | 14.5k | YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm); |
2449 | 14.5k | const int tile_cols = 1 << cm->log2_tile_cols; |
2450 | 14.5k | const int tile_rows = 1 << cm->log2_tile_rows; |
2451 | 14.5k | const int num_workers = VPXMIN(pbi->max_threads, tile_cols); |
2452 | 14.5k | int n; |
2453 | | |
2454 | 14.5k | assert(tile_cols <= (1 << 6)); |
2455 | 14.5k | assert(tile_rows == 1); |
2456 | 14.5k | (void)tile_rows; |
2457 | | |
2458 | 14.5k | init_mt(pbi); |
2459 | | |
2460 | | // Reset tile decoding hook |
2461 | 66.2k | for (n = 0; n < num_workers; ++n) { |
2462 | 51.7k | VPxWorker *const worker = &pbi->tile_workers[n]; |
2463 | 51.7k | TileWorkerData *const tile_data = |
2464 | 51.7k | &pbi->tile_worker_data[n + pbi->total_tiles]; |
2465 | 51.7k | winterface->sync(worker); |
2466 | | |
2467 | 51.7k | if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) { |
2468 | 20.8k | tile_data->lf_sync = lf_row_sync; |
2469 | 20.8k | tile_data->lf_data = &tile_data->lf_sync->lfdata[n]; |
2470 | 20.8k | vp9_loop_filter_data_reset(tile_data->lf_data, new_fb, cm, pbi->mb.plane); |
2471 | 20.8k | tile_data->lf_data->y_only = 0; |
2472 | 20.8k | } |
2473 | | |
2474 | 51.7k | tile_data->xd = pbi->mb; |
2475 | 51.7k | tile_data->xd.counts = |
2476 | 51.7k | cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts; |
2477 | 51.7k | worker->hook = tile_worker_hook; |
2478 | 51.7k | worker->data1 = tile_data; |
2479 | 51.7k | worker->data2 = pbi; |
2480 | 51.7k | } |
2481 | | |
2482 | | // Load tile data into tile_buffers |
2483 | 14.5k | get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, |
2484 | 14.5k | &pbi->tile_buffers); |
2485 | | |
2486 | | // Sort the buffers based on size in descending order. |
2487 | 14.5k | qsort(pbi->tile_buffers, tile_cols, sizeof(pbi->tile_buffers[0]), |
2488 | 14.5k | compare_tile_buffers); |
2489 | | |
2490 | 14.5k | if (num_workers == tile_cols) { |
2491 | | // Rearrange the tile buffers such that the largest, and |
2492 | | // presumably the most difficult, tile will be decoded in the main thread. |
2493 | | // This should help minimize the number of instances where the main thread |
2494 | | // is waiting for a worker to complete. |
2495 | 11.1k | const TileBuffer largest = pbi->tile_buffers[0]; |
2496 | 11.1k | memmove(pbi->tile_buffers, pbi->tile_buffers + 1, |
2497 | 11.1k | (tile_cols - 1) * sizeof(pbi->tile_buffers[0])); |
2498 | 11.1k | pbi->tile_buffers[tile_cols - 1] = largest; |
2499 | 11.1k | } else { |
2500 | 3.36k | int start = 0, end = tile_cols - 2; |
2501 | 3.36k | TileBuffer tmp; |
2502 | | |
2503 | | // Interleave the tiles to distribute the load between threads, assuming a |
2504 | | // larger tile implies it is more difficult to decode. |
2505 | 8.04k | while (start < end) { |
2506 | 4.68k | tmp = pbi->tile_buffers[start]; |
2507 | 4.68k | pbi->tile_buffers[start] = pbi->tile_buffers[end]; |
2508 | 4.68k | pbi->tile_buffers[end] = tmp; |
2509 | 4.68k | start += 2; |
2510 | 4.68k | end -= 2; |
2511 | 4.68k | } |
2512 | 3.36k | } |
2513 | | |
2514 | | // Initialize thread frame counts. |
2515 | 14.5k | if (!cm->frame_parallel_decoding_mode) { |
2516 | 39.7k | for (n = 0; n < num_workers; ++n) { |
2517 | 32.1k | TileWorkerData *const tile_data = |
2518 | 32.1k | (TileWorkerData *)pbi->tile_workers[n].data1; |
2519 | 32.1k | vp9_zero(tile_data->counts); |
2520 | 32.1k | } |
2521 | 7.55k | } |
2522 | | |
2523 | 14.5k | { |
2524 | 14.5k | const int base = tile_cols / num_workers; |
2525 | 14.5k | const int remain = tile_cols % num_workers; |
2526 | 14.5k | int buf_start = 0; |
2527 | | |
2528 | 61.0k | for (n = 0; n < num_workers; ++n) { |
2529 | 46.5k | const int count = base + (remain + n) / num_workers; |
2530 | 46.5k | VPxWorker *const worker = &pbi->tile_workers[n]; |
2531 | 46.5k | TileWorkerData *const tile_data = (TileWorkerData *)worker->data1; |
2532 | | |
2533 | 46.5k | tile_data->buf_start = buf_start; |
2534 | 46.5k | tile_data->buf_end = buf_start + count - 1; |
2535 | 46.5k | tile_data->data_end = data_end; |
2536 | 46.5k | buf_start += count; |
2537 | | |
2538 | 46.5k | worker->had_error = 0; |
2539 | 46.5k | if (n == num_workers - 1) { |
2540 | 13.5k | assert(tile_data->buf_end == tile_cols - 1); |
2541 | 13.5k | winterface->execute(worker); |
2542 | 32.9k | } else { |
2543 | 32.9k | winterface->launch(worker); |
2544 | 32.9k | } |
2545 | 46.5k | } |
2546 | | |
2547 | 61.0k | for (; n > 0; --n) { |
2548 | 46.5k | VPxWorker *const worker = &pbi->tile_workers[n - 1]; |
2549 | 46.5k | TileWorkerData *const tile_data = (TileWorkerData *)worker->data1; |
2550 | | // TODO(jzern): The tile may have specific error data associated with |
2551 | | // its vpx_internal_error_info which could be propagated to the main info |
2552 | | // in cm. Additionally once the threads have been synced and an error is |
2553 | | // detected, there's no point in continuing to decode tiles. |
2554 | 46.5k | pbi->mb.corrupted |= !winterface->sync(worker); |
2555 | 46.5k | if (!bit_reader_end) bit_reader_end = tile_data->data_end; |
2556 | 46.5k | } |
2557 | 14.5k | } |
2558 | | |
2559 | | // Accumulate thread frame counts. |
2560 | 14.5k | if (!cm->frame_parallel_decoding_mode) { |
2561 | 39.7k | for (n = 0; n < num_workers; ++n) { |
2562 | 32.1k | TileWorkerData *const tile_data = |
2563 | 32.1k | (TileWorkerData *)pbi->tile_workers[n].data1; |
2564 | 32.1k | vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1); |
2565 | 32.1k | } |
2566 | 7.55k | } |
2567 | | |
2568 | 14.5k | assert(bit_reader_end || pbi->mb.corrupted); |
2569 | 13.5k | return bit_reader_end; |
2570 | 14.5k | } |
2571 | | |
2572 | 12.9k | static void error_handler(void *data) { |
2573 | 12.9k | VP9_COMMON *const cm = (VP9_COMMON *)data; |
2574 | 12.9k | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet"); |
2575 | 12.9k | } |
2576 | | |
2577 | | static void read_bitdepth_colorspace_sampling(VP9_COMMON *cm, |
2578 | 51.9k | struct vpx_read_bit_buffer *rb) { |
2579 | 51.9k | if (cm->profile >= PROFILE_2) { |
2580 | 9.14k | cm->bit_depth = vpx_rb_read_bit(rb) ? VPX_BITS_12 : VPX_BITS_10; |
2581 | 9.14k | #if CONFIG_VP9_HIGHBITDEPTH |
2582 | 9.14k | cm->use_highbitdepth = 1; |
2583 | 9.14k | #endif |
2584 | 42.8k | } else { |
2585 | 42.8k | cm->bit_depth = VPX_BITS_8; |
2586 | 42.8k | #if CONFIG_VP9_HIGHBITDEPTH |
2587 | 42.8k | cm->use_highbitdepth = 0; |
2588 | 42.8k | #endif |
2589 | 42.8k | } |
2590 | 51.9k | cm->color_space = vpx_rb_read_literal(rb, 3); |
2591 | 51.9k | if (cm->color_space != VPX_CS_SRGB) { |
2592 | 51.4k | cm->color_range = (vpx_color_range_t)vpx_rb_read_bit(rb); |
2593 | 51.4k | if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) { |
2594 | 6.77k | cm->subsampling_x = vpx_rb_read_bit(rb); |
2595 | 6.77k | cm->subsampling_y = vpx_rb_read_bit(rb); |
2596 | 6.77k | if (cm->subsampling_x == 1 && cm->subsampling_y == 1) |
2597 | 219 | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2598 | 219 | "4:2:0 color not supported in profile 1 or 3"); |
2599 | 6.77k | if (vpx_rb_read_bit(rb)) |
2600 | 79 | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2601 | 79 | "Reserved bit set"); |
2602 | 44.6k | } else { |
2603 | 44.6k | cm->subsampling_y = cm->subsampling_x = 1; |
2604 | 44.6k | } |
2605 | 51.4k | } else { |
2606 | 501 | cm->color_range = VPX_CR_FULL_RANGE; |
2607 | 501 | if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) { |
2608 | | // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed. |
2609 | | // 4:2:2 or 4:4:0 chroma sampling is not allowed. |
2610 | 430 | cm->subsampling_y = cm->subsampling_x = 0; |
2611 | 430 | if (vpx_rb_read_bit(rb)) |
2612 | 129 | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2613 | 129 | "Reserved bit set"); |
2614 | 430 | } else { |
2615 | 71 | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2616 | 71 | "4:4:4 color not supported in profile 0 or 2"); |
2617 | 71 | } |
2618 | 501 | } |
2619 | 51.9k | } |
2620 | | |
2621 | 41.2k | static INLINE void flush_all_fb_on_key(VP9_COMMON *cm) { |
2622 | 41.2k | if (cm->frame_type == KEY_FRAME && cm->current_video_frame > 0) { |
2623 | 25.7k | RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs; |
2624 | 25.7k | BufferPool *const pool = cm->buffer_pool; |
2625 | 25.7k | int i; |
2626 | 334k | for (i = 0; i < FRAME_BUFFERS; ++i) { |
2627 | 309k | if (i == cm->new_fb_idx) continue; |
2628 | 283k | frame_bufs[i].ref_count = 0; |
2629 | 283k | if (!frame_bufs[i].released) { |
2630 | 95.7k | pool->release_fb_cb(pool->cb_priv, &frame_bufs[i].raw_frame_buffer); |
2631 | 95.7k | frame_bufs[i].released = 1; |
2632 | 95.7k | } |
2633 | 283k | } |
2634 | 25.7k | } |
2635 | 41.2k | } |
2636 | | |
2637 | | static size_t read_uncompressed_header(VP9Decoder *pbi, |
2638 | 282k | struct vpx_read_bit_buffer *rb) { |
2639 | 282k | VP9_COMMON *const cm = &pbi->common; |
2640 | 282k | BufferPool *const pool = cm->buffer_pool; |
2641 | 282k | RefCntBuffer *const frame_bufs = pool->frame_bufs; |
2642 | 282k | int i, mask, ref_index = 0; |
2643 | 282k | size_t sz; |
2644 | | |
2645 | 282k | cm->last_frame_type = cm->frame_type; |
2646 | 282k | cm->last_intra_only = cm->intra_only; |
2647 | | |
2648 | 282k | if (vpx_rb_read_literal(rb, 2) != VP9_FRAME_MARKER) |
2649 | 34.0k | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2650 | 34.0k | "Invalid frame marker"); |
2651 | | |
2652 | 282k | cm->profile = vp9_read_profile(rb); |
2653 | 282k | #if CONFIG_VP9_HIGHBITDEPTH |
2654 | 282k | if (cm->profile >= MAX_PROFILES) |
2655 | 86 | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2656 | 86 | "Unsupported bitstream profile"); |
2657 | | #else |
2658 | | if (cm->profile >= PROFILE_2) |
2659 | | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2660 | | "Unsupported bitstream profile"); |
2661 | | #endif |
2662 | | |
2663 | 282k | cm->show_existing_frame = vpx_rb_read_bit(rb); |
2664 | 282k | if (cm->show_existing_frame) { |
2665 | | // Show an existing frame directly. |
2666 | 6.77k | const int frame_to_show = cm->ref_frame_map[vpx_rb_read_literal(rb, 3)]; |
2667 | 6.77k | if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) { |
2668 | 785 | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2669 | 785 | "Buffer %d does not contain a decoded frame", |
2670 | 785 | frame_to_show); |
2671 | 785 | } |
2672 | | |
2673 | 6.77k | ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show); |
2674 | 6.77k | pbi->refresh_frame_flags = 0; |
2675 | 6.77k | cm->lf.filter_level = 0; |
2676 | 6.77k | cm->show_frame = 1; |
2677 | | |
2678 | 6.77k | return 0; |
2679 | 6.77k | } |
2680 | | |
2681 | 275k | cm->frame_type = (FRAME_TYPE)vpx_rb_read_bit(rb); |
2682 | 275k | cm->show_frame = vpx_rb_read_bit(rb); |
2683 | 275k | cm->error_resilient_mode = vpx_rb_read_bit(rb); |
2684 | | |
2685 | 275k | if (cm->frame_type == KEY_FRAME) { |
2686 | 53.0k | if (!vp9_read_sync_code(rb)) |
2687 | 1.36k | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2688 | 1.36k | "Invalid frame sync code"); |
2689 | | |
2690 | 53.0k | read_bitdepth_colorspace_sampling(cm, rb); |
2691 | 53.0k | pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1; |
2692 | | |
2693 | 207k | for (i = 0; i < REFS_PER_FRAME; ++i) { |
2694 | 154k | cm->frame_refs[i].idx = INVALID_IDX; |
2695 | 154k | cm->frame_refs[i].buf = NULL; |
2696 | 154k | } |
2697 | | |
2698 | 53.0k | setup_frame_size(cm, rb); |
2699 | 53.0k | if (pbi->need_resync) { |
2700 | 41.2k | memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map)); |
2701 | 41.2k | flush_all_fb_on_key(cm); |
2702 | 41.2k | pbi->need_resync = 0; |
2703 | 41.2k | } |
2704 | 222k | } else { |
2705 | 222k | cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb); |
2706 | | |
2707 | 222k | cm->reset_frame_context = |
2708 | 222k | cm->error_resilient_mode ? 0 : vpx_rb_read_literal(rb, 2); |
2709 | | |
2710 | 222k | if (cm->intra_only) { |
2711 | 76.4k | if (!vp9_read_sync_code(rb)) |
2712 | 4.55k | vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, |
2713 | 4.55k | "Invalid frame sync code"); |
2714 | 76.4k | if (cm->profile > PROFILE_0) { |
2715 | 322 | read_bitdepth_colorspace_sampling(cm, rb); |
2716 | 76.1k | } else { |
2717 | | // NOTE: The intra-only frame header does not include the specification |
2718 | | // of either the color format or color sub-sampling in profile 0. VP9 |
2719 | | // specifies that the default color format should be YUV 4:2:0 in this |
2720 | | // case (normative). |
2721 | 76.1k | cm->color_space = VPX_CS_BT_601; |
2722 | 76.1k | cm->color_range = VPX_CR_STUDIO_RANGE; |
2723 | 76.1k | cm->subsampling_y = cm->subsampling_x = 1; |
2724 | 76.1k | cm->bit_depth = VPX_BITS_8; |
2725 | 76.1k | #if CONFIG_VP9_HIGHBITDEPTH |
2726 | 76.1k | cm->use_highbitdepth = 0; |
2727 | 76.1k | #endif |
2728 | 76.1k | } |
2729 | | |
2730 | 76.4k | pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES); |
2731 | 76.4k | setup_frame_size(cm, rb); |
2732 | 76.4k | if (pbi->need_resync) { |
2733 | 70.6k | memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map)); |
2734 | 70.6k | pbi->need_resync = 0; |
2735 | 70.6k | } |
2736 | 146k | } else if (pbi->need_resync != 1) { /* Skip if need resync */ |
2737 | 86.4k | pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES); |
2738 | 345k | for (i = 0; i < REFS_PER_FRAME; ++i) { |
2739 | 259k | const int ref = vpx_rb_read_literal(rb, REF_FRAMES_LOG2); |
2740 | 259k | const int idx = cm->ref_frame_map[ref]; |
2741 | 259k | RefBuffer *const ref_frame = &cm->frame_refs[i]; |
2742 | 259k | ref_frame->idx = idx; |
2743 | 259k | ref_frame->buf = &frame_bufs[idx].buf; |
2744 | 259k | cm->ref_frame_sign_bias[LAST_FRAME + i] = vpx_rb_read_bit(rb); |
2745 | 259k | } |
2746 | | |
2747 | 86.4k | setup_frame_size_with_refs(cm, rb); |
2748 | | |
2749 | 86.4k | cm->allow_high_precision_mv = vpx_rb_read_bit(rb); |
2750 | 86.4k | cm->interp_filter = read_interp_filter(rb); |
2751 | | |
2752 | 344k | for (i = 0; i < REFS_PER_FRAME; ++i) { |
2753 | 257k | RefBuffer *const ref_buf = &cm->frame_refs[i]; |
2754 | 257k | #if CONFIG_VP9_HIGHBITDEPTH |
2755 | 257k | vp9_setup_scale_factors_for_frame( |
2756 | 257k | &ref_buf->sf, ref_buf->buf->y_crop_width, |
2757 | 257k | ref_buf->buf->y_crop_height, cm->width, cm->height, |
2758 | 257k | cm->use_highbitdepth); |
2759 | | #else |
2760 | | vp9_setup_scale_factors_for_frame( |
2761 | | &ref_buf->sf, ref_buf->buf->y_crop_width, |
2762 | | ref_buf->buf->y_crop_height, cm->width, cm->height); |
2763 | | #endif |
2764 | 257k | } |
2765 | 86.4k | } |
2766 | 222k | } |
2767 | 275k | #if CONFIG_VP9_HIGHBITDEPTH |
2768 | 275k | get_frame_new_buffer(cm)->bit_depth = cm->bit_depth; |
2769 | 275k | #endif |
2770 | 275k | get_frame_new_buffer(cm)->color_space = cm->color_space; |
2771 | 275k | get_frame_new_buffer(cm)->color_range = cm->color_range; |
2772 | 275k | get_frame_new_buffer(cm)->render_width = cm->render_width; |
2773 | 275k | get_frame_new_buffer(cm)->render_height = cm->render_height; |
2774 | | |
2775 | 275k | if (pbi->need_resync) { |
2776 | 25.4k | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
2777 | 25.4k | "Keyframe / intra-only frame required to reset decoder" |
2778 | 25.4k | " state"); |
2779 | 25.4k | } |
2780 | | |
2781 | 275k | if (!cm->error_resilient_mode) { |
2782 | 132k | cm->refresh_frame_context = vpx_rb_read_bit(rb); |
2783 | 132k | cm->frame_parallel_decoding_mode = vpx_rb_read_bit(rb); |
2784 | 132k | if (!cm->frame_parallel_decoding_mode) vp9_zero(cm->counts); |
2785 | 143k | } else { |
2786 | 143k | cm->refresh_frame_context = 0; |
2787 | 143k | cm->frame_parallel_decoding_mode = 1; |
2788 | 143k | } |
2789 | | |
2790 | | // This flag will be overridden by the call to vp9_setup_past_independence |
2791 | | // below, forcing the use of context 0 for those frame types. |
2792 | 275k | cm->frame_context_idx = vpx_rb_read_literal(rb, FRAME_CONTEXTS_LOG2); |
2793 | | |
2794 | | // Generate next_ref_frame_map. |
2795 | 1.15M | for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) { |
2796 | 883k | if (mask & 1) { |
2797 | 633k | cm->next_ref_frame_map[ref_index] = cm->new_fb_idx; |
2798 | 633k | ++frame_bufs[cm->new_fb_idx].ref_count; |
2799 | 633k | } else { |
2800 | 249k | cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index]; |
2801 | 249k | } |
2802 | | // Current thread holds the reference frame. |
2803 | 883k | if (cm->ref_frame_map[ref_index] >= 0) |
2804 | 431k | ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count; |
2805 | 883k | ++ref_index; |
2806 | 883k | } |
2807 | | |
2808 | 1.04M | for (; ref_index < REF_FRAMES; ++ref_index) { |
2809 | 769k | cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index]; |
2810 | | // Current thread holds the reference frame. |
2811 | 769k | if (cm->ref_frame_map[ref_index] >= 0) |
2812 | 323k | ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count; |
2813 | 769k | } |
2814 | 275k | pbi->hold_ref_buf = 1; |
2815 | | |
2816 | 275k | if (frame_is_intra_only(cm) || cm->error_resilient_mode) |
2817 | 120k | vp9_setup_past_independence(cm); |
2818 | | |
2819 | 275k | setup_loopfilter(&cm->lf, rb); |
2820 | 275k | setup_quantization(cm, &pbi->mb, rb); |
2821 | 275k | setup_segmentation(&cm->seg, rb); |
2822 | 275k | setup_segmentation_dequant(cm); |
2823 | | |
2824 | 275k | setup_tile_info(cm, rb); |
2825 | 275k | if (pbi->row_mt == 1) { |
2826 | 0 | int num_sbs = 1; |
2827 | 0 | const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows); |
2828 | 0 | const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2; |
2829 | 0 | const int num_jobs = sb_rows << cm->log2_tile_cols; |
2830 | |
|
2831 | 0 | if (pbi->row_mt_worker_data == NULL) { |
2832 | 0 | CHECK_MEM_ERROR(&cm->error, pbi->row_mt_worker_data, |
2833 | 0 | vpx_calloc(1, sizeof(*pbi->row_mt_worker_data))); |
2834 | 0 | #if CONFIG_MULTITHREAD |
2835 | 0 | pthread_mutex_init(&pbi->row_mt_worker_data->recon_done_mutex, NULL); |
2836 | 0 | #endif |
2837 | 0 | } |
2838 | | |
2839 | 0 | if (pbi->max_threads > 1) { |
2840 | 0 | const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols); |
2841 | 0 | const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2; |
2842 | |
|
2843 | 0 | num_sbs = sb_cols * sb_rows; |
2844 | 0 | } |
2845 | |
|
2846 | 0 | if (num_sbs > pbi->row_mt_worker_data->num_sbs || |
2847 | 0 | num_jobs > pbi->row_mt_worker_data->num_jobs) { |
2848 | 0 | vp9_dec_free_row_mt_mem(pbi->row_mt_worker_data); |
2849 | 0 | vp9_dec_alloc_row_mt_mem(pbi->row_mt_worker_data, cm, num_sbs, |
2850 | 0 | pbi->max_threads, num_jobs); |
2851 | 0 | } |
2852 | 0 | vp9_jobq_alloc(pbi); |
2853 | 0 | } |
2854 | 275k | sz = vpx_rb_read_literal(rb, 16); |
2855 | | |
2856 | 275k | if (sz == 0) |
2857 | 17.6k | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
2858 | 17.6k | "Invalid header size"); |
2859 | | |
2860 | 275k | return sz; |
2861 | 275k | } |
2862 | | |
2863 | | static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data, |
2864 | 127k | size_t partition_size) { |
2865 | 127k | VP9_COMMON *const cm = &pbi->common; |
2866 | 127k | MACROBLOCKD *const xd = &pbi->mb; |
2867 | 127k | FRAME_CONTEXT *const fc = cm->fc; |
2868 | 127k | vpx_reader r; |
2869 | 127k | int k; |
2870 | | |
2871 | 127k | if (vpx_reader_init(&r, data, partition_size, pbi->decrypt_cb, |
2872 | 127k | pbi->decrypt_state)) |
2873 | 2.76k | vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, |
2874 | 2.76k | "Failed to allocate bool decoder 0"); |
2875 | | |
2876 | 127k | cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r); |
2877 | 127k | if (cm->tx_mode == TX_MODE_SELECT) read_tx_mode_probs(&fc->tx_probs, &r); |
2878 | 127k | read_coef_probs(fc, cm->tx_mode, &r); |
2879 | | |
2880 | 502k | for (k = 0; k < SKIP_CONTEXTS; ++k) |
2881 | 374k | vp9_diff_update_prob(&r, &fc->skip_probs[k]); |
2882 | | |
2883 | 127k | if (!frame_is_intra_only(cm)) { |
2884 | 80.2k | nmv_context *const nmvc = &fc->nmvc; |
2885 | 80.2k | int i, j; |
2886 | | |
2887 | 80.2k | read_inter_mode_probs(fc, &r); |
2888 | | |
2889 | 80.2k | if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r); |
2890 | | |
2891 | 401k | for (i = 0; i < INTRA_INTER_CONTEXTS; i++) |
2892 | 320k | vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]); |
2893 | | |
2894 | 80.2k | cm->reference_mode = read_frame_reference_mode(cm, &r); |
2895 | 80.2k | if (cm->reference_mode != SINGLE_REFERENCE) |
2896 | 16.3k | vp9_setup_compound_reference_mode(cm); |
2897 | 80.2k | read_frame_reference_mode_probs(cm, &r); |
2898 | | |
2899 | 401k | for (j = 0; j < BLOCK_SIZE_GROUPS; j++) |
2900 | 3.20M | for (i = 0; i < INTRA_MODES - 1; ++i) |
2901 | 2.88M | vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]); |
2902 | | |
2903 | 1.36M | for (j = 0; j < PARTITION_CONTEXTS; ++j) |
2904 | 5.13M | for (i = 0; i < PARTITION_TYPES - 1; ++i) |
2905 | 3.85M | vp9_diff_update_prob(&r, &fc->partition_prob[j][i]); |
2906 | | |
2907 | 80.2k | read_mv_probs(nmvc, cm->allow_high_precision_mv, &r); |
2908 | 80.2k | } |
2909 | | |
2910 | 127k | return vpx_reader_has_error(&r); |
2911 | 127k | } |
2912 | | |
2913 | | static struct vpx_read_bit_buffer *init_read_bit_buffer( |
2914 | | VP9Decoder *pbi, struct vpx_read_bit_buffer *rb, const uint8_t *data, |
2915 | 282k | const uint8_t *data_end, uint8_t clear_data[MAX_VP9_HEADER_SIZE]) { |
2916 | 282k | rb->bit_offset = 0; |
2917 | 282k | rb->error_handler = error_handler; |
2918 | 282k | rb->error_handler_data = &pbi->common; |
2919 | 282k | if (pbi->decrypt_cb) { |
2920 | 0 | const int n = (int)VPXMIN(MAX_VP9_HEADER_SIZE, data_end - data); |
2921 | 0 | pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n); |
2922 | 0 | rb->bit_buffer = clear_data; |
2923 | 0 | rb->bit_buffer_end = clear_data + n; |
2924 | 282k | } else { |
2925 | 282k | rb->bit_buffer = data; |
2926 | 282k | rb->bit_buffer_end = data_end; |
2927 | 282k | } |
2928 | 282k | return rb; |
2929 | 282k | } |
2930 | | |
2931 | | //------------------------------------------------------------------------------ |
2932 | | |
2933 | 145k | int vp9_read_sync_code(struct vpx_read_bit_buffer *const rb) { |
2934 | 145k | return vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 && |
2935 | 145k | vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 && |
2936 | 145k | vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2; |
2937 | 145k | } |
2938 | | |
2939 | | void vp9_read_frame_size(struct vpx_read_bit_buffer *rb, int *width, |
2940 | 150k | int *height) { |
2941 | 150k | *width = vpx_rb_read_literal(rb, 16) + 1; |
2942 | 150k | *height = vpx_rb_read_literal(rb, 16) + 1; |
2943 | 150k | } |
2944 | | |
2945 | 266k | BITSTREAM_PROFILE vp9_read_profile(struct vpx_read_bit_buffer *rb) { |
2946 | 266k | int profile = vpx_rb_read_bit(rb); |
2947 | 266k | profile |= vpx_rb_read_bit(rb) << 1; |
2948 | 266k | if (profile > 2) profile += vpx_rb_read_bit(rb); |
2949 | 266k | return (BITSTREAM_PROFILE)profile; |
2950 | 266k | } |
2951 | | |
2952 | | void vp9_decode_frame(VP9Decoder *pbi, const uint8_t *data, |
2953 | 282k | const uint8_t *data_end, const uint8_t **p_data_end) { |
2954 | 282k | VP9_COMMON *const cm = &pbi->common; |
2955 | 282k | MACROBLOCKD *const xd = &pbi->mb; |
2956 | 282k | struct vpx_read_bit_buffer rb; |
2957 | 282k | int context_updated = 0; |
2958 | 282k | uint8_t clear_data[MAX_VP9_HEADER_SIZE]; |
2959 | 282k | const size_t first_partition_size = read_uncompressed_header( |
2960 | 282k | pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data)); |
2961 | 282k | const int tile_rows = 1 << cm->log2_tile_rows; |
2962 | 282k | const int tile_cols = 1 << cm->log2_tile_cols; |
2963 | 282k | YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm); |
2964 | | #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG |
2965 | | bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame); |
2966 | | #endif |
2967 | | #if CONFIG_MISMATCH_DEBUG |
2968 | | mismatch_move_frame_idx_r(); |
2969 | | #endif |
2970 | 282k | xd->cur_buf = new_fb; |
2971 | | |
2972 | 282k | if (!first_partition_size) { |
2973 | | // showing a frame directly |
2974 | 5.98k | *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2); |
2975 | 5.98k | return; |
2976 | 5.98k | } |
2977 | | |
2978 | 276k | data += vpx_rb_bytes_read(&rb); |
2979 | 276k | if (!read_is_valid(data, first_partition_size, data_end)) |
2980 | 49.5k | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
2981 | 49.5k | "Truncated packet or corrupt header length"); |
2982 | | |
2983 | 276k | cm->use_prev_frame_mvs = |
2984 | 276k | !cm->error_resilient_mode && cm->width == cm->last_width && |
2985 | 276k | cm->height == cm->last_height && !cm->last_intra_only && |
2986 | 276k | cm->last_show_frame && (cm->last_frame_type != KEY_FRAME); |
2987 | | |
2988 | 276k | vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y); |
2989 | | |
2990 | 276k | *cm->fc = cm->frame_contexts[cm->frame_context_idx]; |
2991 | 276k | if (!cm->fc->initialized) |
2992 | 0 | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
2993 | 0 | "Uninitialized entropy context."); |
2994 | | |
2995 | 276k | xd->corrupted = 0; |
2996 | 276k | new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size); |
2997 | 276k | if (new_fb->corrupted) |
2998 | 883 | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
2999 | 883 | "Decode failed. Frame data header is corrupted."); |
3000 | | |
3001 | 276k | if (cm->lf.filter_level && !cm->skip_loop_filter) { |
3002 | 64.9k | vp9_loop_filter_frame_init(cm, cm->lf.filter_level); |
3003 | 64.9k | } |
3004 | | |
3005 | 276k | if (pbi->tile_worker_data == NULL || |
3006 | 276k | (tile_cols * tile_rows) != pbi->total_tiles) { |
3007 | 17.0k | const int num_tile_workers = |
3008 | 17.0k | tile_cols * tile_rows + ((pbi->max_threads > 1) ? pbi->max_threads : 0); |
3009 | 17.0k | const size_t twd_size = num_tile_workers * sizeof(*pbi->tile_worker_data); |
3010 | | // Ensure tile data offsets will be properly aligned. This may fail on |
3011 | | // platforms without DECLARE_ALIGNED(). |
3012 | 17.0k | assert((sizeof(*pbi->tile_worker_data) % 16) == 0); |
3013 | 17.0k | vpx_free(pbi->tile_worker_data); |
3014 | 17.0k | CHECK_MEM_ERROR(&cm->error, pbi->tile_worker_data, |
3015 | 17.0k | vpx_memalign(32, twd_size)); |
3016 | 17.0k | pbi->total_tiles = tile_rows * tile_cols; |
3017 | 17.0k | } |
3018 | | |
3019 | 276k | if (pbi->max_threads > 1 && tile_rows == 1 && |
3020 | 276k | (tile_cols > 1 || pbi->row_mt == 1)) { |
3021 | 14.5k | if (pbi->row_mt == 1) { |
3022 | 0 | *p_data_end = |
3023 | 0 | decode_tiles_row_wise_mt(pbi, data + first_partition_size, data_end); |
3024 | 14.5k | } else { |
3025 | | // Multi-threaded tile decoder |
3026 | 14.5k | *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end); |
3027 | 14.5k | if (!pbi->lpf_mt_opt) { |
3028 | 4.16k | if (!xd->corrupted) { |
3029 | 3.47k | if (!cm->skip_loop_filter) { |
3030 | | // If multiple threads are used to decode tiles, then we use those |
3031 | | // threads to do parallel loopfiltering. |
3032 | 3.47k | vp9_loop_filter_frame_mt( |
3033 | 3.47k | new_fb, cm, pbi->mb.plane, cm->lf.filter_level, 0, 0, |
3034 | 3.47k | pbi->tile_workers, pbi->num_tile_workers, &pbi->lf_row_sync); |
3035 | 3.47k | } |
3036 | 3.47k | } else { |
3037 | 694 | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
3038 | 694 | "Decode failed. Frame data is corrupted."); |
3039 | 694 | } |
3040 | 4.16k | } |
3041 | 14.5k | } |
3042 | 262k | } else { |
3043 | 262k | *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end); |
3044 | 262k | } |
3045 | | |
3046 | 276k | if (!xd->corrupted) { |
3047 | 97.8k | if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) { |
3048 | 42.1k | vp9_adapt_coef_probs(cm); |
3049 | | |
3050 | 42.1k | if (!frame_is_intra_only(cm)) { |
3051 | 27.1k | vp9_adapt_mode_probs(cm); |
3052 | 27.1k | vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv); |
3053 | 27.1k | } |
3054 | 42.1k | } |
3055 | 178k | } else { |
3056 | 178k | vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, |
3057 | 178k | "Decode failed. Frame data is corrupted."); |
3058 | 178k | } |
3059 | | |
3060 | | // Non frame parallel update frame context here. |
3061 | 276k | if (cm->refresh_frame_context && !context_updated) |
3062 | 53.4k | cm->frame_contexts[cm->frame_context_idx] = *cm->fc; |
3063 | 276k | } |