OLD | NEW |
| (Empty) |
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 | |
13 #include "./vp9_rtcd.h" | |
14 #include "vpx_mem/vpx_mem.h" | |
15 #include "vpx_scale/vpx_scale.h" | |
16 | |
17 #include "vp9/common/vp9_alloccommon.h" | |
18 #include "vp9/common/vp9_common.h" | |
19 #include "vp9/common/vp9_entropy.h" | |
20 #include "vp9/common/vp9_entropymode.h" | |
21 #include "vp9/common/vp9_extend.h" | |
22 #include "vp9/common/vp9_idct.h" | |
23 #include "vp9/common/vp9_pred_common.h" | |
24 #include "vp9/common/vp9_quant_common.h" | |
25 #include "vp9/common/vp9_reconintra.h" | |
26 #include "vp9/common/vp9_reconinter.h" | |
27 #include "vp9/common/vp9_seg_common.h" | |
28 #include "vp9/common/vp9_tile_common.h" | |
29 | |
30 #include "vp9/decoder/vp9_dboolhuff.h" | |
31 #include "vp9/decoder/vp9_decodframe.h" | |
32 #include "vp9/decoder/vp9_detokenize.h" | |
33 #include "vp9/decoder/vp9_decodemv.h" | |
34 #include "vp9/decoder/vp9_dsubexp.h" | |
35 #include "vp9/decoder/vp9_onyxd_int.h" | |
36 #include "vp9/decoder/vp9_read_bit_buffer.h" | |
37 #include "vp9/decoder/vp9_thread.h" | |
38 #include "vp9/decoder/vp9_treereader.h" | |
39 | |
40 typedef struct TileWorkerData { | |
41 VP9_COMMON *cm; | |
42 vp9_reader bit_reader; | |
43 DECLARE_ALIGNED(16, MACROBLOCKD, xd); | |
44 } TileWorkerData; | |
45 | |
46 static int read_be32(const uint8_t *p) { | |
47 return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]; | |
48 } | |
49 | |
50 static int is_compound_prediction_allowed(const VP9_COMMON *cm) { | |
51 int i; | |
52 for (i = 1; i < ALLOWED_REFS_PER_FRAME; ++i) | |
53 if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1]) | |
54 return 1; | |
55 | |
56 return 0; | |
57 } | |
58 | |
59 static void setup_compound_prediction(VP9_COMMON *cm) { | |
60 if (cm->ref_frame_sign_bias[LAST_FRAME] == | |
61 cm->ref_frame_sign_bias[GOLDEN_FRAME]) { | |
62 cm->comp_fixed_ref = ALTREF_FRAME; | |
63 cm->comp_var_ref[0] = LAST_FRAME; | |
64 cm->comp_var_ref[1] = GOLDEN_FRAME; | |
65 } else if (cm->ref_frame_sign_bias[LAST_FRAME] == | |
66 cm->ref_frame_sign_bias[ALTREF_FRAME]) { | |
67 cm->comp_fixed_ref = GOLDEN_FRAME; | |
68 cm->comp_var_ref[0] = LAST_FRAME; | |
69 cm->comp_var_ref[1] = ALTREF_FRAME; | |
70 } else { | |
71 cm->comp_fixed_ref = LAST_FRAME; | |
72 cm->comp_var_ref[0] = GOLDEN_FRAME; | |
73 cm->comp_var_ref[1] = ALTREF_FRAME; | |
74 } | |
75 } | |
76 | |
77 // len == 0 is not allowed | |
78 static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) { | |
79 return start + len > start && start + len <= end; | |
80 } | |
81 | |
82 static int decode_unsigned_max(struct vp9_read_bit_buffer *rb, int max) { | |
83 const int data = vp9_rb_read_literal(rb, get_unsigned_bits(max)); | |
84 return data > max ? max : data; | |
85 } | |
86 | |
87 static TX_MODE read_tx_mode(vp9_reader *r) { | |
88 TX_MODE tx_mode = vp9_read_literal(r, 2); | |
89 if (tx_mode == ALLOW_32X32) | |
90 tx_mode += vp9_read_bit(r); | |
91 return tx_mode; | |
92 } | |
93 | |
94 static void read_tx_probs(struct tx_probs *tx_probs, vp9_reader *r) { | |
95 int i, j; | |
96 | |
97 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) | |
98 for (j = 0; j < TX_SIZES - 3; ++j) | |
99 vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]); | |
100 | |
101 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) | |
102 for (j = 0; j < TX_SIZES - 2; ++j) | |
103 vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]); | |
104 | |
105 for (i = 0; i < TX_SIZE_CONTEXTS; ++i) | |
106 for (j = 0; j < TX_SIZES - 1; ++j) | |
107 vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]); | |
108 } | |
109 | |
110 static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vp9_reader *r) { | |
111 int i, j; | |
112 for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j) | |
113 for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i) | |
114 vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]); | |
115 } | |
116 | |
117 static void read_inter_mode_probs(FRAME_CONTEXT *fc, vp9_reader *r) { | |
118 int i, j; | |
119 for (i = 0; i < INTER_MODE_CONTEXTS; ++i) | |
120 for (j = 0; j < INTER_MODES - 1; ++j) | |
121 vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]); | |
122 } | |
123 | |
124 static INLINE COMPPREDMODE_TYPE read_comp_pred_mode(vp9_reader *r) { | |
125 COMPPREDMODE_TYPE mode = vp9_read_bit(r); | |
126 if (mode) | |
127 mode += vp9_read_bit(r); | |
128 return mode; | |
129 } | |
130 | |
131 static void read_comp_pred(VP9_COMMON *cm, vp9_reader *r) { | |
132 int i; | |
133 | |
134 const int compound_allowed = is_compound_prediction_allowed(cm); | |
135 cm->comp_pred_mode = compound_allowed ? read_comp_pred_mode(r) | |
136 : SINGLE_PREDICTION_ONLY; | |
137 if (compound_allowed) | |
138 setup_compound_prediction(cm); | |
139 | |
140 if (cm->comp_pred_mode == HYBRID_PREDICTION) | |
141 for (i = 0; i < COMP_INTER_CONTEXTS; i++) | |
142 vp9_diff_update_prob(r, &cm->fc.comp_inter_prob[i]); | |
143 | |
144 if (cm->comp_pred_mode != COMP_PREDICTION_ONLY) | |
145 for (i = 0; i < REF_CONTEXTS; i++) { | |
146 vp9_diff_update_prob(r, &cm->fc.single_ref_prob[i][0]); | |
147 vp9_diff_update_prob(r, &cm->fc.single_ref_prob[i][1]); | |
148 } | |
149 | |
150 if (cm->comp_pred_mode != SINGLE_PREDICTION_ONLY) | |
151 for (i = 0; i < REF_CONTEXTS; i++) | |
152 vp9_diff_update_prob(r, &cm->fc.comp_ref_prob[i]); | |
153 } | |
154 | |
155 static void update_mv(vp9_reader *r, vp9_prob *p) { | |
156 if (vp9_read(r, NMV_UPDATE_PROB)) | |
157 *p = (vp9_read_literal(r, 7) << 1) | 1; | |
158 } | |
159 | |
160 static void read_mv_probs(vp9_reader *r, nmv_context *mvc, int allow_hp) { | |
161 int i, j, k; | |
162 | |
163 for (j = 0; j < MV_JOINTS - 1; ++j) | |
164 update_mv(r, &mvc->joints[j]); | |
165 | |
166 for (i = 0; i < 2; ++i) { | |
167 nmv_component *const comp = &mvc->comps[i]; | |
168 | |
169 update_mv(r, &comp->sign); | |
170 | |
171 for (j = 0; j < MV_CLASSES - 1; ++j) | |
172 update_mv(r, &comp->classes[j]); | |
173 | |
174 for (j = 0; j < CLASS0_SIZE - 1; ++j) | |
175 update_mv(r, &comp->class0[j]); | |
176 | |
177 for (j = 0; j < MV_OFFSET_BITS; ++j) | |
178 update_mv(r, &comp->bits[j]); | |
179 } | |
180 | |
181 for (i = 0; i < 2; ++i) { | |
182 nmv_component *const comp = &mvc->comps[i]; | |
183 | |
184 for (j = 0; j < CLASS0_SIZE; ++j) | |
185 for (k = 0; k < 3; ++k) | |
186 update_mv(r, &comp->class0_fp[j][k]); | |
187 | |
188 for (j = 0; j < 3; ++j) | |
189 update_mv(r, &comp->fp[j]); | |
190 } | |
191 | |
192 if (allow_hp) { | |
193 for (i = 0; i < 2; ++i) { | |
194 update_mv(r, &mvc->comps[i].class0_hp); | |
195 update_mv(r, &mvc->comps[i].hp); | |
196 } | |
197 } | |
198 } | |
199 | |
200 static void setup_plane_dequants(VP9_COMMON *cm, MACROBLOCKD *xd, int q_index) { | |
201 int i; | |
202 xd->plane[0].dequant = cm->y_dequant[q_index]; | |
203 | |
204 for (i = 1; i < MAX_MB_PLANE; i++) | |
205 xd->plane[i].dequant = cm->uv_dequant[q_index]; | |
206 } | |
207 | |
208 // Allocate storage for each tile column. | |
209 // TODO(jzern): when max_threads <= 1 the same storage could be used for each | |
210 // tile. | |
211 static void alloc_tile_storage(VP9D_COMP *pbi, int tile_cols) { | |
212 VP9_COMMON *const cm = &pbi->common; | |
213 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols); | |
214 int i, tile_col; | |
215 | |
216 CHECK_MEM_ERROR(cm, pbi->mi_streams, | |
217 vpx_realloc(pbi->mi_streams, tile_cols * | |
218 sizeof(*pbi->mi_streams))); | |
219 for (tile_col = 0; tile_col < tile_cols; ++tile_col) { | |
220 TileInfo tile; | |
221 | |
222 vp9_tile_init(&tile, cm, 0, tile_col); | |
223 pbi->mi_streams[tile_col] = | |
224 &cm->mi[cm->mi_rows * tile.mi_col_start]; | |
225 } | |
226 | |
227 // 2 contexts per 'mi unit', so that we have one context per 4x4 txfm | |
228 // block where mi unit size is 8x8. | |
229 CHECK_MEM_ERROR(cm, pbi->above_context[0], | |
230 vpx_realloc(pbi->above_context[0], | |
231 sizeof(*pbi->above_context[0]) * MAX_MB_PLANE * | |
232 2 * aligned_mi_cols)); | |
233 for (i = 1; i < MAX_MB_PLANE; ++i) { | |
234 pbi->above_context[i] = pbi->above_context[0] + | |
235 i * sizeof(*pbi->above_context[0]) * | |
236 2 * aligned_mi_cols; | |
237 } | |
238 | |
239 // This is sized based on the entire frame. Each tile operates within its | |
240 // column bounds. | |
241 CHECK_MEM_ERROR(cm, pbi->above_seg_context, | |
242 vpx_realloc(pbi->above_seg_context, | |
243 sizeof(*pbi->above_seg_context) * | |
244 aligned_mi_cols)); | |
245 } | |
246 | |
247 static void inverse_transform_block(MACROBLOCKD* xd, int plane, int block, | |
248 BLOCK_SIZE plane_bsize, TX_SIZE tx_size) { | |
249 struct macroblockd_plane *const pd = &xd->plane[plane]; | |
250 int16_t* const qcoeff = BLOCK_OFFSET(pd->qcoeff, block); | |
251 const int stride = pd->dst.stride; | |
252 const int eob = pd->eobs[block]; | |
253 if (eob > 0) { | |
254 TX_TYPE tx_type; | |
255 const int raster_block = txfrm_block_to_raster_block(plane_bsize, tx_size, | |
256 block); | |
257 uint8_t* const dst = raster_block_offset_uint8(plane_bsize, raster_block, | |
258 pd->dst.buf, stride); | |
259 switch (tx_size) { | |
260 case TX_4X4: | |
261 tx_type = get_tx_type_4x4(pd->plane_type, xd, raster_block); | |
262 if (tx_type == DCT_DCT) | |
263 xd->itxm_add(qcoeff, dst, stride, eob); | |
264 else | |
265 vp9_iht4x4_add(tx_type, qcoeff, dst, stride, eob); | |
266 break; | |
267 case TX_8X8: | |
268 tx_type = get_tx_type_8x8(pd->plane_type, xd); | |
269 vp9_iht8x8_add(tx_type, qcoeff, dst, stride, eob); | |
270 break; | |
271 case TX_16X16: | |
272 tx_type = get_tx_type_16x16(pd->plane_type, xd); | |
273 vp9_iht16x16_add(tx_type, qcoeff, dst, stride, eob); | |
274 break; | |
275 case TX_32X32: | |
276 tx_type = DCT_DCT; | |
277 vp9_idct32x32_add(qcoeff, dst, stride, eob); | |
278 break; | |
279 default: | |
280 assert(!"Invalid transform size"); | |
281 } | |
282 | |
283 if (eob == 1) { | |
284 vpx_memset(qcoeff, 0, 2 * sizeof(qcoeff[0])); | |
285 } else { | |
286 if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10) | |
287 vpx_memset(qcoeff, 0, 4 * (4 << tx_size) * sizeof(qcoeff[0])); | |
288 else | |
289 vpx_memset(qcoeff, 0, (16 << (tx_size << 1)) * sizeof(qcoeff[0])); | |
290 } | |
291 } | |
292 } | |
293 | |
294 struct intra_args { | |
295 VP9_COMMON *cm; | |
296 MACROBLOCKD *xd; | |
297 vp9_reader *r; | |
298 }; | |
299 | |
300 static void predict_and_reconstruct_intra_block(int plane, int block, | |
301 BLOCK_SIZE plane_bsize, | |
302 TX_SIZE tx_size, void *arg) { | |
303 struct intra_args *const args = arg; | |
304 VP9_COMMON *const cm = args->cm; | |
305 MACROBLOCKD *const xd = args->xd; | |
306 | |
307 struct macroblockd_plane *const pd = &xd->plane[plane]; | |
308 MODE_INFO *const mi = xd->mi_8x8[0]; | |
309 const int raster_block = txfrm_block_to_raster_block(plane_bsize, tx_size, | |
310 block); | |
311 uint8_t* const dst = raster_block_offset_uint8(plane_bsize, raster_block, | |
312 pd->dst.buf, pd->dst.stride); | |
313 const MB_PREDICTION_MODE mode = (plane == 0) | |
314 ? ((mi->mbmi.sb_type < BLOCK_8X8) ? mi->bmi[raster_block].as_mode | |
315 : mi->mbmi.mode) | |
316 : mi->mbmi.uv_mode; | |
317 | |
318 if (xd->mb_to_right_edge < 0 || xd->mb_to_bottom_edge < 0) | |
319 extend_for_intra(xd, plane_bsize, plane, block, tx_size); | |
320 | |
321 vp9_predict_intra_block(xd, raster_block >> tx_size, | |
322 b_width_log2(plane_bsize), tx_size, mode, | |
323 dst, pd->dst.stride, dst, pd->dst.stride); | |
324 | |
325 if (!mi->mbmi.skip_coeff) { | |
326 vp9_decode_block_tokens(cm, xd, plane, block, plane_bsize, tx_size, | |
327 args->r); | |
328 inverse_transform_block(xd, plane, block, plane_bsize, tx_size); | |
329 } | |
330 } | |
331 | |
332 struct inter_args { | |
333 VP9_COMMON *cm; | |
334 MACROBLOCKD *xd; | |
335 vp9_reader *r; | |
336 int *eobtotal; | |
337 }; | |
338 | |
339 static void reconstruct_inter_block(int plane, int block, | |
340 BLOCK_SIZE plane_bsize, | |
341 TX_SIZE tx_size, void *arg) { | |
342 struct inter_args *args = arg; | |
343 VP9_COMMON *const cm = args->cm; | |
344 MACROBLOCKD *const xd = args->xd; | |
345 | |
346 *args->eobtotal += vp9_decode_block_tokens(cm, xd, plane, block, | |
347 plane_bsize, tx_size, args->r); | |
348 inverse_transform_block(xd, plane, block, plane_bsize, tx_size); | |
349 } | |
350 | |
351 static void set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd, | |
352 const TileInfo *const tile, | |
353 BLOCK_SIZE bsize, int mi_row, int mi_col) { | |
354 const int bh = num_8x8_blocks_high_lookup[bsize]; | |
355 const int bw = num_8x8_blocks_wide_lookup[bsize]; | |
356 const int offset = mi_row * cm->mode_info_stride + mi_col; | |
357 | |
358 xd->mode_info_stride = cm->mode_info_stride; | |
359 | |
360 xd->mi_8x8 = cm->mi_grid_visible + offset; | |
361 xd->prev_mi_8x8 = cm->prev_mi_grid_visible + offset; | |
362 | |
363 // we are using the mode info context stream here | |
364 xd->mi_8x8[0] = xd->mi_stream; | |
365 xd->mi_8x8[0]->mbmi.sb_type = bsize; | |
366 ++xd->mi_stream; | |
367 | |
368 // Special case: if prev_mi is NULL, the previous mode info context | |
369 // cannot be used. | |
370 xd->last_mi = cm->prev_mi ? xd->prev_mi_8x8[0] : NULL; | |
371 | |
372 set_skip_context(xd, xd->above_context, xd->left_context, mi_row, mi_col); | |
373 | |
374 // Distance of Mb to the various image edges. These are specified to 8th pel | |
375 // as they are always compared to values that are in 1/8th pel units | |
376 set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols); | |
377 | |
378 setup_dst_planes(xd, get_frame_new_buffer(cm), mi_row, mi_col); | |
379 } | |
380 | |
381 static void set_ref(VP9_COMMON *const cm, MACROBLOCKD *const xd, | |
382 int idx, int mi_row, int mi_col) { | |
383 MB_MODE_INFO *const mbmi = &xd->mi_8x8[0]->mbmi; | |
384 const int ref = mbmi->ref_frame[idx] - LAST_FRAME; | |
385 const YV12_BUFFER_CONFIG *cfg = get_frame_ref_buffer(cm, ref); | |
386 const struct scale_factors_common *sfc = &cm->active_ref_scale_comm[ref]; | |
387 if (!vp9_is_valid_scale(sfc)) | |
388 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, | |
389 "Invalid scale factors"); | |
390 | |
391 xd->scale_factor[idx].sfc = sfc; | |
392 setup_pre_planes(xd, idx, cfg, mi_row, mi_col, &xd->scale_factor[idx]); | |
393 xd->corrupted |= cfg->corrupted; | |
394 } | |
395 | |
396 static void decode_modes_b(VP9_COMMON *const cm, MACROBLOCKD *const xd, | |
397 const TileInfo *const tile, | |
398 int mi_row, int mi_col, | |
399 vp9_reader *r, BLOCK_SIZE bsize) { | |
400 const int less8x8 = bsize < BLOCK_8X8; | |
401 MB_MODE_INFO *mbmi; | |
402 | |
403 set_offsets(cm, xd, tile, bsize, mi_row, mi_col); | |
404 vp9_read_mode_info(cm, xd, tile, mi_row, mi_col, r); | |
405 | |
406 if (less8x8) | |
407 bsize = BLOCK_8X8; | |
408 | |
409 // Has to be called after set_offsets | |
410 mbmi = &xd->mi_8x8[0]->mbmi; | |
411 | |
412 if (mbmi->skip_coeff) { | |
413 reset_skip_context(xd, bsize); | |
414 } else { | |
415 if (cm->seg.enabled) | |
416 setup_plane_dequants(cm, xd, vp9_get_qindex(&cm->seg, mbmi->segment_id, | |
417 cm->base_qindex)); | |
418 } | |
419 | |
420 if (!is_inter_block(mbmi)) { | |
421 struct intra_args arg = { cm, xd, r }; | |
422 foreach_transformed_block(xd, bsize, predict_and_reconstruct_intra_block, | |
423 &arg); | |
424 } else { | |
425 // Setup | |
426 set_ref(cm, xd, 0, mi_row, mi_col); | |
427 if (has_second_ref(mbmi)) | |
428 set_ref(cm, xd, 1, mi_row, mi_col); | |
429 | |
430 xd->subpix.filter_x = xd->subpix.filter_y = | |
431 vp9_get_filter_kernel(mbmi->interp_filter); | |
432 | |
433 // Prediction | |
434 vp9_build_inter_predictors_sb(xd, mi_row, mi_col, bsize); | |
435 | |
436 // Reconstruction | |
437 if (!mbmi->skip_coeff) { | |
438 int eobtotal = 0; | |
439 struct inter_args arg = { cm, xd, r, &eobtotal }; | |
440 foreach_transformed_block(xd, bsize, reconstruct_inter_block, &arg); | |
441 if (!less8x8 && eobtotal == 0) | |
442 mbmi->skip_coeff = 1; // skip loopfilter | |
443 } | |
444 } | |
445 | |
446 xd->corrupted |= vp9_reader_has_error(r); | |
447 } | |
448 | |
449 static PARTITION_TYPE read_partition(int hbs, int mi_rows, int mi_cols, | |
450 int mi_row, int mi_col, | |
451 vp9_prob probs[PARTITION_TYPES - 1], | |
452 vp9_reader *r) { | |
453 const int has_rows = (mi_row + hbs) < mi_rows; | |
454 const int has_cols = (mi_col + hbs) < mi_cols; | |
455 | |
456 if (has_rows && has_cols) | |
457 return treed_read(r, vp9_partition_tree, probs); | |
458 else if (!has_rows && has_cols) | |
459 return vp9_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ; | |
460 else if (has_rows && !has_cols) | |
461 return vp9_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT; | |
462 else | |
463 return PARTITION_SPLIT; | |
464 } | |
465 | |
466 static void decode_modes_sb(VP9_COMMON *const cm, MACROBLOCKD *const xd, | |
467 const TileInfo *const tile, | |
468 int mi_row, int mi_col, | |
469 vp9_reader* r, BLOCK_SIZE bsize) { | |
470 const int hbs = num_8x8_blocks_wide_lookup[bsize] / 2; | |
471 PARTITION_TYPE partition; | |
472 BLOCK_SIZE subsize; | |
473 int ctx; | |
474 | |
475 if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) | |
476 return; | |
477 | |
478 ctx = partition_plane_context(xd->above_seg_context, xd->left_seg_context, | |
479 mi_row, mi_col, bsize); | |
480 partition = read_partition(hbs, cm->mi_rows, cm->mi_cols, mi_row, mi_col, | |
481 cm->fc.partition_prob[cm->frame_type][ctx], r); | |
482 | |
483 if (!cm->frame_parallel_decoding_mode) | |
484 ++cm->counts.partition[ctx][partition]; | |
485 | |
486 subsize = get_subsize(bsize, partition); | |
487 if (subsize < BLOCK_8X8) { | |
488 decode_modes_b(cm, xd, tile, mi_row, mi_col, r, subsize); | |
489 } else { | |
490 switch (partition) { | |
491 case PARTITION_NONE: | |
492 decode_modes_b(cm, xd, tile, mi_row, mi_col, r, subsize); | |
493 break; | |
494 case PARTITION_HORZ: | |
495 decode_modes_b(cm, xd, tile, mi_row, mi_col, r, subsize); | |
496 if (mi_row + hbs < cm->mi_rows) | |
497 decode_modes_b(cm, xd, tile, mi_row + hbs, mi_col, r, subsize); | |
498 break; | |
499 case PARTITION_VERT: | |
500 decode_modes_b(cm, xd, tile, mi_row, mi_col, r, subsize); | |
501 if (mi_col + hbs < cm->mi_cols) | |
502 decode_modes_b(cm, xd, tile, mi_row, mi_col + hbs, r, subsize); | |
503 break; | |
504 case PARTITION_SPLIT: | |
505 decode_modes_sb(cm, xd, tile, mi_row, mi_col, r, subsize); | |
506 decode_modes_sb(cm, xd, tile, mi_row, mi_col + hbs, r, subsize); | |
507 decode_modes_sb(cm, xd, tile, mi_row + hbs, mi_col, r, subsize); | |
508 decode_modes_sb(cm, xd, tile, mi_row + hbs, mi_col + hbs, r, subsize); | |
509 break; | |
510 default: | |
511 assert(!"Invalid partition type"); | |
512 } | |
513 } | |
514 | |
515 // update partition context | |
516 if (bsize >= BLOCK_8X8 && | |
517 (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT)) | |
518 update_partition_context(xd->above_seg_context, xd->left_seg_context, | |
519 mi_row, mi_col, subsize, bsize); | |
520 } | |
521 | |
522 static void setup_token_decoder(const uint8_t *data, | |
523 const uint8_t *data_end, | |
524 size_t read_size, | |
525 struct vpx_internal_error_info *error_info, | |
526 vp9_reader *r) { | |
527 // Validate the calculated partition length. If the buffer | |
528 // described by the partition can't be fully read, then restrict | |
529 // it to the portion that can be (for EC mode) or throw an error. | |
530 if (!read_is_valid(data, read_size, data_end)) | |
531 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME, | |
532 "Truncated packet or corrupt tile length"); | |
533 | |
534 if (vp9_reader_init(r, data, read_size)) | |
535 vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR, | |
536 "Failed to allocate bool decoder %d", 1); | |
537 } | |
538 | |
539 static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs, | |
540 vp9_reader *r) { | |
541 int i, j, k, l, m; | |
542 | |
543 if (vp9_read_bit(r)) | |
544 for (i = 0; i < BLOCK_TYPES; i++) | |
545 for (j = 0; j < REF_TYPES; j++) | |
546 for (k = 0; k < COEF_BANDS; k++) | |
547 for (l = 0; l < PREV_COEF_CONTEXTS; l++) | |
548 if (k > 0 || l < 3) | |
549 for (m = 0; m < UNCONSTRAINED_NODES; m++) | |
550 vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]); | |
551 } | |
552 | |
553 static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, | |
554 vp9_reader *r) { | |
555 read_coef_probs_common(fc->coef_probs[TX_4X4], r); | |
556 | |
557 if (tx_mode > ONLY_4X4) | |
558 read_coef_probs_common(fc->coef_probs[TX_8X8], r); | |
559 | |
560 if (tx_mode > ALLOW_8X8) | |
561 read_coef_probs_common(fc->coef_probs[TX_16X16], r); | |
562 | |
563 if (tx_mode > ALLOW_16X16) | |
564 read_coef_probs_common(fc->coef_probs[TX_32X32], r); | |
565 } | |
566 | |
567 static void setup_segmentation(struct segmentation *seg, | |
568 struct vp9_read_bit_buffer *rb) { | |
569 int i, j; | |
570 | |
571 seg->update_map = 0; | |
572 seg->update_data = 0; | |
573 | |
574 seg->enabled = vp9_rb_read_bit(rb); | |
575 if (!seg->enabled) | |
576 return; | |
577 | |
578 // Segmentation map update | |
579 seg->update_map = vp9_rb_read_bit(rb); | |
580 if (seg->update_map) { | |
581 for (i = 0; i < SEG_TREE_PROBS; i++) | |
582 seg->tree_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8) | |
583 : MAX_PROB; | |
584 | |
585 seg->temporal_update = vp9_rb_read_bit(rb); | |
586 if (seg->temporal_update) { | |
587 for (i = 0; i < PREDICTION_PROBS; i++) | |
588 seg->pred_probs[i] = vp9_rb_read_bit(rb) ? vp9_rb_read_literal(rb, 8) | |
589 : MAX_PROB; | |
590 } else { | |
591 for (i = 0; i < PREDICTION_PROBS; i++) | |
592 seg->pred_probs[i] = MAX_PROB; | |
593 } | |
594 } | |
595 | |
596 // Segmentation data update | |
597 seg->update_data = vp9_rb_read_bit(rb); | |
598 if (seg->update_data) { | |
599 seg->abs_delta = vp9_rb_read_bit(rb); | |
600 | |
601 vp9_clearall_segfeatures(seg); | |
602 | |
603 for (i = 0; i < MAX_SEGMENTS; i++) { | |
604 for (j = 0; j < SEG_LVL_MAX; j++) { | |
605 int data = 0; | |
606 const int feature_enabled = vp9_rb_read_bit(rb); | |
607 if (feature_enabled) { | |
608 vp9_enable_segfeature(seg, i, j); | |
609 data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j)); | |
610 if (vp9_is_segfeature_signed(j)) | |
611 data = vp9_rb_read_bit(rb) ? -data : data; | |
612 } | |
613 vp9_set_segdata(seg, i, j, data); | |
614 } | |
615 } | |
616 } | |
617 } | |
618 | |
619 static void setup_loopfilter(struct loopfilter *lf, | |
620 struct vp9_read_bit_buffer *rb) { | |
621 lf->filter_level = vp9_rb_read_literal(rb, 6); | |
622 lf->sharpness_level = vp9_rb_read_literal(rb, 3); | |
623 | |
624 // Read in loop filter deltas applied at the MB level based on mode or ref | |
625 // frame. | |
626 lf->mode_ref_delta_update = 0; | |
627 | |
628 lf->mode_ref_delta_enabled = vp9_rb_read_bit(rb); | |
629 if (lf->mode_ref_delta_enabled) { | |
630 lf->mode_ref_delta_update = vp9_rb_read_bit(rb); | |
631 if (lf->mode_ref_delta_update) { | |
632 int i; | |
633 | |
634 for (i = 0; i < MAX_REF_LF_DELTAS; i++) | |
635 if (vp9_rb_read_bit(rb)) | |
636 lf->ref_deltas[i] = vp9_rb_read_signed_literal(rb, 6); | |
637 | |
638 for (i = 0; i < MAX_MODE_LF_DELTAS; i++) | |
639 if (vp9_rb_read_bit(rb)) | |
640 lf->mode_deltas[i] = vp9_rb_read_signed_literal(rb, 6); | |
641 } | |
642 } | |
643 } | |
644 | |
645 static int read_delta_q(struct vp9_read_bit_buffer *rb, int *delta_q) { | |
646 const int old = *delta_q; | |
647 *delta_q = vp9_rb_read_bit(rb) ? vp9_rb_read_signed_literal(rb, 4) : 0; | |
648 return old != *delta_q; | |
649 } | |
650 | |
651 static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd, | |
652 struct vp9_read_bit_buffer *rb) { | |
653 int update = 0; | |
654 | |
655 cm->base_qindex = vp9_rb_read_literal(rb, QINDEX_BITS); | |
656 update |= read_delta_q(rb, &cm->y_dc_delta_q); | |
657 update |= read_delta_q(rb, &cm->uv_dc_delta_q); | |
658 update |= read_delta_q(rb, &cm->uv_ac_delta_q); | |
659 if (update) | |
660 vp9_init_dequantizer(cm); | |
661 | |
662 xd->lossless = cm->base_qindex == 0 && | |
663 cm->y_dc_delta_q == 0 && | |
664 cm->uv_dc_delta_q == 0 && | |
665 cm->uv_ac_delta_q == 0; | |
666 | |
667 xd->itxm_add = xd->lossless ? vp9_iwht4x4_add : vp9_idct4x4_add; | |
668 } | |
669 | |
670 static INTERPOLATION_TYPE read_interp_filter_type( | |
671 struct vp9_read_bit_buffer *rb) { | |
672 const INTERPOLATION_TYPE literal_to_type[] = { EIGHTTAP_SMOOTH, | |
673 EIGHTTAP, | |
674 EIGHTTAP_SHARP, | |
675 BILINEAR }; | |
676 return vp9_rb_read_bit(rb) ? SWITCHABLE | |
677 : literal_to_type[vp9_rb_read_literal(rb, 2)]; | |
678 } | |
679 | |
680 static void read_frame_size(struct vp9_read_bit_buffer *rb, | |
681 int *width, int *height) { | |
682 const int w = vp9_rb_read_literal(rb, 16) + 1; | |
683 const int h = vp9_rb_read_literal(rb, 16) + 1; | |
684 *width = w; | |
685 *height = h; | |
686 } | |
687 | |
688 static void setup_display_size(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { | |
689 cm->display_width = cm->width; | |
690 cm->display_height = cm->height; | |
691 if (vp9_rb_read_bit(rb)) | |
692 read_frame_size(rb, &cm->display_width, &cm->display_height); | |
693 } | |
694 | |
695 static void apply_frame_size(VP9D_COMP *pbi, int width, int height) { | |
696 VP9_COMMON *cm = &pbi->common; | |
697 | |
698 if (cm->width != width || cm->height != height) { | |
699 if (!pbi->initial_width || !pbi->initial_height) { | |
700 if (vp9_alloc_frame_buffers(cm, width, height)) | |
701 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, | |
702 "Failed to allocate frame buffers"); | |
703 pbi->initial_width = width; | |
704 pbi->initial_height = height; | |
705 } else { | |
706 if (width > pbi->initial_width) | |
707 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, | |
708 "Frame width too large"); | |
709 | |
710 if (height > pbi->initial_height) | |
711 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, | |
712 "Frame height too large"); | |
713 } | |
714 | |
715 cm->width = width; | |
716 cm->height = height; | |
717 | |
718 vp9_update_frame_size(cm); | |
719 } | |
720 | |
721 vp9_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height, | |
722 cm->subsampling_x, cm->subsampling_y, | |
723 VP9BORDERINPIXELS); | |
724 } | |
725 | |
726 static void setup_frame_size(VP9D_COMP *pbi, | |
727 struct vp9_read_bit_buffer *rb) { | |
728 int width, height; | |
729 read_frame_size(rb, &width, &height); | |
730 apply_frame_size(pbi, width, height); | |
731 setup_display_size(&pbi->common, rb); | |
732 } | |
733 | |
734 static void setup_frame_size_with_refs(VP9D_COMP *pbi, | |
735 struct vp9_read_bit_buffer *rb) { | |
736 VP9_COMMON *const cm = &pbi->common; | |
737 | |
738 int width, height; | |
739 int found = 0, i; | |
740 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) { | |
741 if (vp9_rb_read_bit(rb)) { | |
742 YV12_BUFFER_CONFIG *const cfg = get_frame_ref_buffer(cm, i); | |
743 width = cfg->y_crop_width; | |
744 height = cfg->y_crop_height; | |
745 found = 1; | |
746 break; | |
747 } | |
748 } | |
749 | |
750 if (!found) | |
751 read_frame_size(rb, &width, &height); | |
752 | |
753 if (!width || !height) | |
754 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, | |
755 "Referenced frame with invalid size"); | |
756 | |
757 apply_frame_size(pbi, width, height); | |
758 setup_display_size(cm, rb); | |
759 } | |
760 | |
761 static void setup_tile_context(VP9D_COMP *const pbi, MACROBLOCKD *const xd, | |
762 int tile_col) { | |
763 int i; | |
764 xd->mi_stream = pbi->mi_streams[tile_col]; | |
765 | |
766 for (i = 0; i < MAX_MB_PLANE; ++i) { | |
767 xd->above_context[i] = pbi->above_context[i]; | |
768 } | |
769 // see note in alloc_tile_storage(). | |
770 xd->above_seg_context = pbi->above_seg_context; | |
771 } | |
772 | |
773 static void decode_tile(VP9D_COMP *pbi, const TileInfo *const tile, | |
774 vp9_reader *r) { | |
775 const int num_threads = pbi->oxcf.max_threads; | |
776 VP9_COMMON *const cm = &pbi->common; | |
777 int mi_row, mi_col; | |
778 MACROBLOCKD *xd = &pbi->mb; | |
779 | |
780 if (pbi->do_loopfilter_inline) { | |
781 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1; | |
782 lf_data->frame_buffer = get_frame_new_buffer(cm); | |
783 lf_data->cm = cm; | |
784 lf_data->xd = pbi->mb; | |
785 lf_data->stop = 0; | |
786 lf_data->y_only = 0; | |
787 vp9_loop_filter_frame_init(cm, cm->lf.filter_level); | |
788 } | |
789 | |
790 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end; | |
791 mi_row += MI_BLOCK_SIZE) { | |
792 // For a SB there are 2 left contexts, each pertaining to a MB row within | |
793 vp9_zero(xd->left_context); | |
794 vp9_zero(xd->left_seg_context); | |
795 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end; | |
796 mi_col += MI_BLOCK_SIZE) | |
797 decode_modes_sb(cm, xd, tile, mi_row, mi_col, r, BLOCK_64X64); | |
798 | |
799 if (pbi->do_loopfilter_inline) { | |
800 const int lf_start = mi_row - MI_BLOCK_SIZE; | |
801 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1; | |
802 | |
803 // delay the loopfilter by 1 macroblock row. | |
804 if (lf_start < 0) continue; | |
805 | |
806 // decoding has completed: finish up the loop filter in this thread. | |
807 if (mi_row + MI_BLOCK_SIZE >= tile->mi_row_end) continue; | |
808 | |
809 vp9_worker_sync(&pbi->lf_worker); | |
810 lf_data->start = lf_start; | |
811 lf_data->stop = mi_row; | |
812 if (num_threads > 1) { | |
813 vp9_worker_launch(&pbi->lf_worker); | |
814 } else { | |
815 vp9_worker_execute(&pbi->lf_worker); | |
816 } | |
817 } | |
818 } | |
819 | |
820 if (pbi->do_loopfilter_inline) { | |
821 LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1; | |
822 | |
823 vp9_worker_sync(&pbi->lf_worker); | |
824 lf_data->start = lf_data->stop; | |
825 lf_data->stop = cm->mi_rows; | |
826 vp9_worker_execute(&pbi->lf_worker); | |
827 } | |
828 } | |
829 | |
830 static void setup_tile_info(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { | |
831 int min_log2_tile_cols, max_log2_tile_cols, max_ones; | |
832 vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols); | |
833 | |
834 // columns | |
835 max_ones = max_log2_tile_cols - min_log2_tile_cols; | |
836 cm->log2_tile_cols = min_log2_tile_cols; | |
837 while (max_ones-- && vp9_rb_read_bit(rb)) | |
838 cm->log2_tile_cols++; | |
839 | |
840 // rows | |
841 cm->log2_tile_rows = vp9_rb_read_bit(rb); | |
842 if (cm->log2_tile_rows) | |
843 cm->log2_tile_rows += vp9_rb_read_bit(rb); | |
844 } | |
845 | |
846 // Reads the next tile returning its size and adjusting '*data' accordingly | |
847 // based on 'is_last'. | |
848 static size_t get_tile(const uint8_t *const data_end, | |
849 int is_last, | |
850 struct vpx_internal_error_info *error_info, | |
851 const uint8_t **data) { | |
852 size_t size; | |
853 | |
854 if (!is_last) { | |
855 if (!read_is_valid(*data, 4, data_end)) | |
856 vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME, | |
857 "Truncated packet or corrupt tile length"); | |
858 | |
859 size = read_be32(*data); | |
860 *data += 4; | |
861 } else { | |
862 size = data_end - *data; | |
863 } | |
864 return size; | |
865 } | |
866 | |
867 static const uint8_t *decode_tiles(VP9D_COMP *pbi, const uint8_t *data) { | |
868 vp9_reader residual_bc; | |
869 | |
870 VP9_COMMON *const cm = &pbi->common; | |
871 MACROBLOCKD *const xd = &pbi->mb; | |
872 | |
873 const uint8_t *const data_end = pbi->source + pbi->source_sz; | |
874 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols); | |
875 const int tile_cols = 1 << cm->log2_tile_cols; | |
876 const int tile_rows = 1 << cm->log2_tile_rows; | |
877 int tile_row, tile_col; | |
878 | |
879 // Note: this memset assumes above_context[0], [1] and [2] | |
880 // are allocated as part of the same buffer. | |
881 vpx_memset(pbi->above_context[0], 0, | |
882 sizeof(*pbi->above_context[0]) * MAX_MB_PLANE * | |
883 2 * aligned_mi_cols); | |
884 | |
885 vpx_memset(pbi->above_seg_context, 0, | |
886 sizeof(*pbi->above_seg_context) * aligned_mi_cols); | |
887 | |
888 if (pbi->oxcf.inv_tile_order) { | |
889 const uint8_t *data_ptr2[4][1 << 6]; | |
890 vp9_reader bc_bak = {0}; | |
891 | |
892 // pre-initialize the offsets, we're going to decode in inverse order | |
893 data_ptr2[0][0] = data; | |
894 for (tile_row = 0; tile_row < tile_rows; tile_row++) { | |
895 for (tile_col = 0; tile_col < tile_cols; tile_col++) { | |
896 const int last_tile = | |
897 tile_row == tile_rows - 1 && tile_col == tile_cols - 1; | |
898 const size_t size = get_tile(data_end, last_tile, &cm->error, &data); | |
899 data_ptr2[tile_row][tile_col] = data; | |
900 data += size; | |
901 } | |
902 } | |
903 | |
904 for (tile_row = 0; tile_row < tile_rows; tile_row++) { | |
905 for (tile_col = tile_cols - 1; tile_col >= 0; tile_col--) { | |
906 TileInfo tile; | |
907 | |
908 vp9_tile_init(&tile, cm, tile_row, tile_col); | |
909 setup_token_decoder(data_ptr2[tile_row][tile_col], data_end, | |
910 data_end - data_ptr2[tile_row][tile_col], | |
911 &cm->error, &residual_bc); | |
912 setup_tile_context(pbi, xd, tile_col); | |
913 decode_tile(pbi, &tile, &residual_bc); | |
914 if (tile_row == tile_rows - 1 && tile_col == tile_cols - 1) | |
915 bc_bak = residual_bc; | |
916 } | |
917 } | |
918 residual_bc = bc_bak; | |
919 } else { | |
920 for (tile_row = 0; tile_row < tile_rows; tile_row++) { | |
921 for (tile_col = 0; tile_col < tile_cols; tile_col++) { | |
922 const int last_tile = | |
923 tile_row == tile_rows - 1 && tile_col == tile_cols - 1; | |
924 const size_t size = get_tile(data_end, last_tile, &cm->error, &data); | |
925 TileInfo tile; | |
926 | |
927 vp9_tile_init(&tile, cm, tile_row, tile_col); | |
928 | |
929 setup_token_decoder(data, data_end, size, &cm->error, &residual_bc); | |
930 setup_tile_context(pbi, xd, tile_col); | |
931 decode_tile(pbi, &tile, &residual_bc); | |
932 data += size; | |
933 } | |
934 } | |
935 } | |
936 | |
937 return vp9_reader_find_end(&residual_bc); | |
938 } | |
939 | |
940 static int tile_worker_hook(void *arg1, void *arg2) { | |
941 TileWorkerData *const tile_data = (TileWorkerData*)arg1; | |
942 const TileInfo *const tile = (TileInfo*)arg2; | |
943 int mi_row, mi_col; | |
944 | |
945 for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end; | |
946 mi_row += MI_BLOCK_SIZE) { | |
947 vp9_zero(tile_data->xd.left_context); | |
948 vp9_zero(tile_data->xd.left_seg_context); | |
949 for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end; | |
950 mi_col += MI_BLOCK_SIZE) | |
951 decode_modes_sb(tile_data->cm, &tile_data->xd, tile, | |
952 mi_row, mi_col, &tile_data->bit_reader, BLOCK_64X64); | |
953 } | |
954 return !tile_data->xd.corrupted; | |
955 } | |
956 | |
957 static const uint8_t *decode_tiles_mt(VP9D_COMP *pbi, const uint8_t *data) { | |
958 VP9_COMMON *const cm = &pbi->common; | |
959 const uint8_t *const data_end = pbi->source + pbi->source_sz; | |
960 const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols); | |
961 const int tile_cols = 1 << cm->log2_tile_cols; | |
962 const int tile_rows = 1 << cm->log2_tile_rows; | |
963 const int num_workers = MIN(pbi->oxcf.max_threads & ~1, tile_cols); | |
964 int tile_col = 0; | |
965 | |
966 assert(tile_rows == 1); | |
967 (void)tile_rows; | |
968 | |
969 if (num_workers > pbi->num_tile_workers) { | |
970 int i; | |
971 CHECK_MEM_ERROR(cm, pbi->tile_workers, | |
972 vpx_realloc(pbi->tile_workers, | |
973 num_workers * sizeof(*pbi->tile_workers))); | |
974 for (i = pbi->num_tile_workers; i < num_workers; ++i) { | |
975 VP9Worker *const worker = &pbi->tile_workers[i]; | |
976 ++pbi->num_tile_workers; | |
977 | |
978 vp9_worker_init(worker); | |
979 worker->hook = (VP9WorkerHook)tile_worker_hook; | |
980 CHECK_MEM_ERROR(cm, worker->data1, | |
981 vpx_memalign(32, sizeof(TileWorkerData))); | |
982 CHECK_MEM_ERROR(cm, worker->data2, vpx_malloc(sizeof(TileInfo))); | |
983 if (i < num_workers - 1 && !vp9_worker_reset(worker)) { | |
984 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, | |
985 "Tile decoder thread creation failed"); | |
986 } | |
987 } | |
988 } | |
989 | |
990 // Note: this memset assumes above_context[0], [1] and [2] | |
991 // are allocated as part of the same buffer. | |
992 vpx_memset(pbi->above_context[0], 0, | |
993 sizeof(*pbi->above_context[0]) * MAX_MB_PLANE * | |
994 2 * aligned_mi_cols); | |
995 vpx_memset(pbi->above_seg_context, 0, | |
996 sizeof(*pbi->above_seg_context) * aligned_mi_cols); | |
997 | |
998 while (tile_col < tile_cols) { | |
999 int i; | |
1000 for (i = 0; i < num_workers && tile_col < tile_cols; ++i) { | |
1001 VP9Worker *const worker = &pbi->tile_workers[i]; | |
1002 TileWorkerData *const tile_data = (TileWorkerData*)worker->data1; | |
1003 TileInfo *const tile = (TileInfo*)worker->data2; | |
1004 const size_t size = | |
1005 get_tile(data_end, tile_col == tile_cols - 1, &cm->error, &data); | |
1006 | |
1007 tile_data->cm = cm; | |
1008 tile_data->xd = pbi->mb; | |
1009 tile_data->xd.corrupted = 0; | |
1010 vp9_tile_init(tile, tile_data->cm, 0, tile_col); | |
1011 | |
1012 setup_token_decoder(data, data_end, size, &cm->error, | |
1013 &tile_data->bit_reader); | |
1014 setup_tile_context(pbi, &tile_data->xd, tile_col); | |
1015 | |
1016 worker->had_error = 0; | |
1017 if (i == num_workers - 1 || tile_col == tile_cols - 1) { | |
1018 vp9_worker_execute(worker); | |
1019 } else { | |
1020 vp9_worker_launch(worker); | |
1021 } | |
1022 | |
1023 data += size; | |
1024 ++tile_col; | |
1025 } | |
1026 | |
1027 for (; i > 0; --i) { | |
1028 VP9Worker *const worker = &pbi->tile_workers[i - 1]; | |
1029 pbi->mb.corrupted |= !vp9_worker_sync(worker); | |
1030 } | |
1031 } | |
1032 | |
1033 { | |
1034 const int final_worker = (tile_cols + num_workers - 1) % num_workers; | |
1035 TileWorkerData *const tile_data = | |
1036 (TileWorkerData*)pbi->tile_workers[final_worker].data1; | |
1037 return vp9_reader_find_end(&tile_data->bit_reader); | |
1038 } | |
1039 } | |
1040 | |
1041 static void check_sync_code(VP9_COMMON *cm, struct vp9_read_bit_buffer *rb) { | |
1042 if (vp9_rb_read_literal(rb, 8) != VP9_SYNC_CODE_0 || | |
1043 vp9_rb_read_literal(rb, 8) != VP9_SYNC_CODE_1 || | |
1044 vp9_rb_read_literal(rb, 8) != VP9_SYNC_CODE_2) { | |
1045 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, | |
1046 "Invalid frame sync code"); | |
1047 } | |
1048 } | |
1049 | |
1050 static void error_handler(void *data, size_t bit_offset) { | |
1051 VP9_COMMON *const cm = (VP9_COMMON *)data; | |
1052 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet"); | |
1053 } | |
1054 | |
1055 #define RESERVED \ | |
1056 if (vp9_rb_read_bit(rb)) \ | |
1057 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, \ | |
1058 "Reserved bit must be unset") | |
1059 | |
1060 static size_t read_uncompressed_header(VP9D_COMP *pbi, | |
1061 struct vp9_read_bit_buffer *rb) { | |
1062 VP9_COMMON *const cm = &pbi->common; | |
1063 size_t sz; | |
1064 int i; | |
1065 | |
1066 cm->last_frame_type = cm->frame_type; | |
1067 | |
1068 if (vp9_rb_read_literal(rb, 2) != VP9_FRAME_MARKER) | |
1069 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, | |
1070 "Invalid frame marker"); | |
1071 | |
1072 cm->version = vp9_rb_read_bit(rb); | |
1073 RESERVED; | |
1074 | |
1075 if (vp9_rb_read_bit(rb)) { | |
1076 // show an existing frame directly | |
1077 int frame_to_show = cm->ref_frame_map[vp9_rb_read_literal(rb, 3)]; | |
1078 ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->new_fb_idx, frame_to_show); | |
1079 pbi->refresh_frame_flags = 0; | |
1080 cm->lf.filter_level = 0; | |
1081 return 0; | |
1082 } | |
1083 | |
1084 cm->frame_type = (FRAME_TYPE) vp9_rb_read_bit(rb); | |
1085 cm->show_frame = vp9_rb_read_bit(rb); | |
1086 cm->error_resilient_mode = vp9_rb_read_bit(rb); | |
1087 | |
1088 if (cm->frame_type == KEY_FRAME) { | |
1089 check_sync_code(cm, rb); | |
1090 | |
1091 cm->color_space = vp9_rb_read_literal(rb, 3); // colorspace | |
1092 if (cm->color_space != SRGB) { | |
1093 vp9_rb_read_bit(rb); // [16,235] (including xvycc) vs [0,255] range | |
1094 if (cm->version == 1) { | |
1095 cm->subsampling_x = vp9_rb_read_bit(rb); | |
1096 cm->subsampling_y = vp9_rb_read_bit(rb); | |
1097 vp9_rb_read_bit(rb); // has extra plane | |
1098 } else { | |
1099 cm->subsampling_y = cm->subsampling_x = 1; | |
1100 } | |
1101 } else { | |
1102 if (cm->version == 1) { | |
1103 cm->subsampling_y = cm->subsampling_x = 0; | |
1104 vp9_rb_read_bit(rb); // has extra plane | |
1105 } else { | |
1106 vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM, | |
1107 "RGB not supported in profile 0"); | |
1108 } | |
1109 } | |
1110 | |
1111 pbi->refresh_frame_flags = (1 << NUM_REF_FRAMES) - 1; | |
1112 | |
1113 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) | |
1114 cm->active_ref_idx[i] = cm->new_fb_idx; | |
1115 | |
1116 setup_frame_size(pbi, rb); | |
1117 } else { | |
1118 cm->intra_only = cm->show_frame ? 0 : vp9_rb_read_bit(rb); | |
1119 | |
1120 cm->reset_frame_context = cm->error_resilient_mode ? | |
1121 0 : vp9_rb_read_literal(rb, 2); | |
1122 | |
1123 if (cm->intra_only) { | |
1124 check_sync_code(cm, rb); | |
1125 | |
1126 pbi->refresh_frame_flags = vp9_rb_read_literal(rb, NUM_REF_FRAMES); | |
1127 setup_frame_size(pbi, rb); | |
1128 } else { | |
1129 pbi->refresh_frame_flags = vp9_rb_read_literal(rb, NUM_REF_FRAMES); | |
1130 | |
1131 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) { | |
1132 const int ref = vp9_rb_read_literal(rb, NUM_REF_FRAMES_LOG2); | |
1133 cm->active_ref_idx[i] = cm->ref_frame_map[ref]; | |
1134 cm->ref_frame_sign_bias[LAST_FRAME + i] = vp9_rb_read_bit(rb); | |
1135 } | |
1136 | |
1137 setup_frame_size_with_refs(pbi, rb); | |
1138 | |
1139 cm->allow_high_precision_mv = vp9_rb_read_bit(rb); | |
1140 cm->mcomp_filter_type = read_interp_filter_type(rb); | |
1141 | |
1142 for (i = 0; i < ALLOWED_REFS_PER_FRAME; ++i) | |
1143 vp9_setup_scale_factors(cm, i); | |
1144 } | |
1145 } | |
1146 | |
1147 if (!cm->error_resilient_mode) { | |
1148 cm->refresh_frame_context = vp9_rb_read_bit(rb); | |
1149 cm->frame_parallel_decoding_mode = vp9_rb_read_bit(rb); | |
1150 } else { | |
1151 cm->refresh_frame_context = 0; | |
1152 cm->frame_parallel_decoding_mode = 1; | |
1153 } | |
1154 | |
1155 // This flag will be overridden by the call to vp9_setup_past_independence | |
1156 // below, forcing the use of context 0 for those frame types. | |
1157 cm->frame_context_idx = vp9_rb_read_literal(rb, NUM_FRAME_CONTEXTS_LOG2); | |
1158 | |
1159 if (frame_is_intra_only(cm) || cm->error_resilient_mode) | |
1160 vp9_setup_past_independence(cm); | |
1161 | |
1162 setup_loopfilter(&cm->lf, rb); | |
1163 setup_quantization(cm, &pbi->mb, rb); | |
1164 setup_segmentation(&cm->seg, rb); | |
1165 | |
1166 setup_tile_info(cm, rb); | |
1167 sz = vp9_rb_read_literal(rb, 16); | |
1168 | |
1169 if (sz == 0) | |
1170 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, | |
1171 "Invalid header size"); | |
1172 | |
1173 return sz; | |
1174 } | |
1175 | |
1176 static int read_compressed_header(VP9D_COMP *pbi, const uint8_t *data, | |
1177 size_t partition_size) { | |
1178 VP9_COMMON *const cm = &pbi->common; | |
1179 MACROBLOCKD *const xd = &pbi->mb; | |
1180 FRAME_CONTEXT *const fc = &cm->fc; | |
1181 vp9_reader r; | |
1182 int k; | |
1183 | |
1184 if (vp9_reader_init(&r, data, partition_size)) | |
1185 vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR, | |
1186 "Failed to allocate bool decoder 0"); | |
1187 | |
1188 cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r); | |
1189 if (cm->tx_mode == TX_MODE_SELECT) | |
1190 read_tx_probs(&fc->tx_probs, &r); | |
1191 read_coef_probs(fc, cm->tx_mode, &r); | |
1192 | |
1193 for (k = 0; k < MBSKIP_CONTEXTS; ++k) | |
1194 vp9_diff_update_prob(&r, &fc->mbskip_probs[k]); | |
1195 | |
1196 if (!frame_is_intra_only(cm)) { | |
1197 nmv_context *const nmvc = &fc->nmvc; | |
1198 int i, j; | |
1199 | |
1200 read_inter_mode_probs(fc, &r); | |
1201 | |
1202 if (cm->mcomp_filter_type == SWITCHABLE) | |
1203 read_switchable_interp_probs(fc, &r); | |
1204 | |
1205 for (i = 0; i < INTRA_INTER_CONTEXTS; i++) | |
1206 vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]); | |
1207 | |
1208 read_comp_pred(cm, &r); | |
1209 | |
1210 for (j = 0; j < BLOCK_SIZE_GROUPS; j++) | |
1211 for (i = 0; i < INTRA_MODES - 1; ++i) | |
1212 vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]); | |
1213 | |
1214 for (j = 0; j < PARTITION_CONTEXTS; ++j) | |
1215 for (i = 0; i < PARTITION_TYPES - 1; ++i) | |
1216 vp9_diff_update_prob(&r, &fc->partition_prob[INTER_FRAME][j][i]); | |
1217 | |
1218 read_mv_probs(&r, nmvc, cm->allow_high_precision_mv); | |
1219 } | |
1220 | |
1221 return vp9_reader_has_error(&r); | |
1222 } | |
1223 | |
1224 void vp9_init_dequantizer(VP9_COMMON *cm) { | |
1225 int q; | |
1226 | |
1227 for (q = 0; q < QINDEX_RANGE; q++) { | |
1228 cm->y_dequant[q][0] = vp9_dc_quant(q, cm->y_dc_delta_q); | |
1229 cm->y_dequant[q][1] = vp9_ac_quant(q, 0); | |
1230 | |
1231 cm->uv_dequant[q][0] = vp9_dc_quant(q, cm->uv_dc_delta_q); | |
1232 cm->uv_dequant[q][1] = vp9_ac_quant(q, cm->uv_ac_delta_q); | |
1233 } | |
1234 } | |
1235 | |
1236 #ifdef NDEBUG | |
1237 #define debug_check_frame_counts(cm) (void)0 | |
1238 #else // !NDEBUG | |
1239 // Counts should only be incremented when frame_parallel_decoding_mode and | |
1240 // error_resilient_mode are disabled. | |
1241 static void debug_check_frame_counts(const VP9_COMMON *const cm) { | |
1242 FRAME_COUNTS zero_counts; | |
1243 vp9_zero(zero_counts); | |
1244 assert(cm->frame_parallel_decoding_mode || cm->error_resilient_mode); | |
1245 assert(!memcmp(cm->counts.y_mode, zero_counts.y_mode, | |
1246 sizeof(cm->counts.y_mode))); | |
1247 assert(!memcmp(cm->counts.uv_mode, zero_counts.uv_mode, | |
1248 sizeof(cm->counts.uv_mode))); | |
1249 assert(!memcmp(cm->counts.partition, zero_counts.partition, | |
1250 sizeof(cm->counts.partition))); | |
1251 assert(!memcmp(cm->counts.coef, zero_counts.coef, | |
1252 sizeof(cm->counts.coef))); | |
1253 assert(!memcmp(cm->counts.eob_branch, zero_counts.eob_branch, | |
1254 sizeof(cm->counts.eob_branch))); | |
1255 assert(!memcmp(cm->counts.switchable_interp, zero_counts.switchable_interp, | |
1256 sizeof(cm->counts.switchable_interp))); | |
1257 assert(!memcmp(cm->counts.inter_mode, zero_counts.inter_mode, | |
1258 sizeof(cm->counts.inter_mode))); | |
1259 assert(!memcmp(cm->counts.intra_inter, zero_counts.intra_inter, | |
1260 sizeof(cm->counts.intra_inter))); | |
1261 assert(!memcmp(cm->counts.comp_inter, zero_counts.comp_inter, | |
1262 sizeof(cm->counts.comp_inter))); | |
1263 assert(!memcmp(cm->counts.single_ref, zero_counts.single_ref, | |
1264 sizeof(cm->counts.single_ref))); | |
1265 assert(!memcmp(cm->counts.comp_ref, zero_counts.comp_ref, | |
1266 sizeof(cm->counts.comp_ref))); | |
1267 assert(!memcmp(&cm->counts.tx, &zero_counts.tx, sizeof(cm->counts.tx))); | |
1268 assert(!memcmp(cm->counts.mbskip, zero_counts.mbskip, | |
1269 sizeof(cm->counts.mbskip))); | |
1270 assert(!memcmp(&cm->counts.mv, &zero_counts.mv, sizeof(cm->counts.mv))); | |
1271 } | |
1272 #endif // NDEBUG | |
1273 | |
1274 int vp9_decode_frame(VP9D_COMP *pbi, const uint8_t **p_data_end) { | |
1275 int i; | |
1276 VP9_COMMON *const cm = &pbi->common; | |
1277 MACROBLOCKD *const xd = &pbi->mb; | |
1278 | |
1279 const uint8_t *data = pbi->source; | |
1280 const uint8_t *const data_end = pbi->source + pbi->source_sz; | |
1281 | |
1282 struct vp9_read_bit_buffer rb = { data, data_end, 0, cm, error_handler }; | |
1283 const size_t first_partition_size = read_uncompressed_header(pbi, &rb); | |
1284 const int keyframe = cm->frame_type == KEY_FRAME; | |
1285 const int tile_rows = 1 << cm->log2_tile_rows; | |
1286 const int tile_cols = 1 << cm->log2_tile_cols; | |
1287 YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm); | |
1288 | |
1289 if (!first_partition_size) { | |
1290 // showing a frame directly | |
1291 *p_data_end = data + 1; | |
1292 return 0; | |
1293 } | |
1294 | |
1295 if (!pbi->decoded_key_frame && !keyframe) | |
1296 return -1; | |
1297 | |
1298 data += vp9_rb_bytes_read(&rb); | |
1299 if (!read_is_valid(data, first_partition_size, data_end)) | |
1300 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, | |
1301 "Truncated packet or corrupt header length"); | |
1302 | |
1303 pbi->do_loopfilter_inline = | |
1304 (cm->log2_tile_rows | cm->log2_tile_cols) == 0 && cm->lf.filter_level; | |
1305 if (pbi->do_loopfilter_inline && pbi->lf_worker.data1 == NULL) { | |
1306 CHECK_MEM_ERROR(cm, pbi->lf_worker.data1, vpx_malloc(sizeof(LFWorkerData))); | |
1307 pbi->lf_worker.hook = (VP9WorkerHook)vp9_loop_filter_worker; | |
1308 if (pbi->oxcf.max_threads > 1 && !vp9_worker_reset(&pbi->lf_worker)) { | |
1309 vpx_internal_error(&cm->error, VPX_CODEC_ERROR, | |
1310 "Loop filter thread creation failed"); | |
1311 } | |
1312 } | |
1313 | |
1314 alloc_tile_storage(pbi, tile_cols); | |
1315 | |
1316 xd->mi_8x8 = cm->mi_grid_visible; | |
1317 xd->mode_info_stride = cm->mode_info_stride; | |
1318 set_prev_mi(cm); | |
1319 | |
1320 setup_plane_dequants(cm, xd, cm->base_qindex); | |
1321 setup_block_dptrs(xd, cm->subsampling_x, cm->subsampling_y); | |
1322 | |
1323 cm->fc = cm->frame_contexts[cm->frame_context_idx]; | |
1324 vp9_zero(cm->counts); | |
1325 for (i = 0; i < MAX_MB_PLANE; ++i) | |
1326 vp9_zero(xd->plane[i].qcoeff); | |
1327 | |
1328 xd->corrupted = 0; | |
1329 new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size); | |
1330 | |
1331 // TODO(jzern): remove frame_parallel_decoding_mode restriction for | |
1332 // single-frame tile decoding. | |
1333 if (pbi->oxcf.max_threads > 1 && tile_rows == 1 && tile_cols > 1 && | |
1334 cm->frame_parallel_decoding_mode) { | |
1335 *p_data_end = decode_tiles_mt(pbi, data + first_partition_size); | |
1336 } else { | |
1337 *p_data_end = decode_tiles(pbi, data + first_partition_size); | |
1338 } | |
1339 | |
1340 cm->last_width = cm->width; | |
1341 cm->last_height = cm->height; | |
1342 | |
1343 new_fb->corrupted |= xd->corrupted; | |
1344 | |
1345 if (!pbi->decoded_key_frame) { | |
1346 if (keyframe && !new_fb->corrupted) | |
1347 pbi->decoded_key_frame = 1; | |
1348 else | |
1349 vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, | |
1350 "A stream must start with a complete key frame"); | |
1351 } | |
1352 | |
1353 if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) { | |
1354 vp9_adapt_coef_probs(cm); | |
1355 | |
1356 if (!frame_is_intra_only(cm)) { | |
1357 vp9_adapt_mode_probs(cm); | |
1358 vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv); | |
1359 } | |
1360 } else { | |
1361 debug_check_frame_counts(cm); | |
1362 } | |
1363 | |
1364 if (cm->refresh_frame_context) | |
1365 cm->frame_contexts[cm->frame_context_idx] = cm->fc; | |
1366 | |
1367 return 0; | |
1368 } | |
OLD | NEW |