Line data Source code
1 : /*
2 : * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 : *
4 : * Use of this source code is governed by a BSD-style license
5 : * that can be found in the LICENSE file in the root of the source
6 : * tree. An additional intellectual property rights grant can be found
7 : * in the file PATENTS. All contributing project authors may
8 : * be found in the AUTHORS file in the root of the source tree.
9 : */
10 :
11 : #include "vpx_config.h"
12 : #include "vp8_rtcd.h"
13 : #include "./vpx_scale_rtcd.h"
14 : #include "onyxd_int.h"
15 : #include "vp8/common/header.h"
16 : #include "vp8/common/reconintra4x4.h"
17 : #include "vp8/common/reconinter.h"
18 : #include "detokenize.h"
19 : #include "vp8/common/common.h"
20 : #include "vp8/common/invtrans.h"
21 : #include "vp8/common/alloccommon.h"
22 : #include "vp8/common/entropymode.h"
23 : #include "vp8/common/quant_common.h"
24 : #include "vpx_scale/vpx_scale.h"
25 : #include "vp8/common/reconintra.h"
26 : #include "vp8/common/setupintrarecon.h"
27 :
28 : #include "decodemv.h"
29 : #include "vp8/common/extend.h"
30 : #if CONFIG_ERROR_CONCEALMENT
31 : #include "error_concealment.h"
32 : #endif
33 : #include "vpx_mem/vpx_mem.h"
34 : #include "vp8/common/threading.h"
35 : #include "decoderthreading.h"
36 : #include "dboolhuff.h"
37 : #include "vpx_dsp/vpx_dsp_common.h"
38 :
39 : #include <assert.h>
40 : #include <stdio.h>
41 :
42 0 : void vp8cx_init_de_quantizer(VP8D_COMP *pbi) {
43 : int Q;
44 0 : VP8_COMMON *const pc = &pbi->common;
45 :
46 0 : for (Q = 0; Q < QINDEX_RANGE; ++Q) {
47 0 : pc->Y1dequant[Q][0] = (short)vp8_dc_quant(Q, pc->y1dc_delta_q);
48 0 : pc->Y2dequant[Q][0] = (short)vp8_dc2quant(Q, pc->y2dc_delta_q);
49 0 : pc->UVdequant[Q][0] = (short)vp8_dc_uv_quant(Q, pc->uvdc_delta_q);
50 :
51 0 : pc->Y1dequant[Q][1] = (short)vp8_ac_yquant(Q);
52 0 : pc->Y2dequant[Q][1] = (short)vp8_ac2quant(Q, pc->y2ac_delta_q);
53 0 : pc->UVdequant[Q][1] = (short)vp8_ac_uv_quant(Q, pc->uvac_delta_q);
54 : }
55 0 : }
56 :
57 0 : void vp8_mb_init_dequantizer(VP8D_COMP *pbi, MACROBLOCKD *xd) {
58 : int i;
59 : int QIndex;
60 0 : MB_MODE_INFO *mbmi = &xd->mode_info_context->mbmi;
61 0 : VP8_COMMON *const pc = &pbi->common;
62 :
63 : /* Decide whether to use the default or alternate baseline Q value. */
64 0 : if (xd->segmentation_enabled) {
65 : /* Abs Value */
66 0 : if (xd->mb_segement_abs_delta == SEGMENT_ABSDATA) {
67 0 : QIndex = xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
68 :
69 : /* Delta Value */
70 : } else {
71 0 : QIndex = pc->base_qindex +
72 0 : xd->segment_feature_data[MB_LVL_ALT_Q][mbmi->segment_id];
73 : }
74 :
75 0 : QIndex = (QIndex >= 0) ? ((QIndex <= MAXQ) ? QIndex : MAXQ)
76 0 : : 0; /* Clamp to valid range */
77 : } else {
78 0 : QIndex = pc->base_qindex;
79 : }
80 :
81 : /* Set up the macroblock dequant constants */
82 0 : xd->dequant_y1_dc[0] = 1;
83 0 : xd->dequant_y1[0] = pc->Y1dequant[QIndex][0];
84 0 : xd->dequant_y2[0] = pc->Y2dequant[QIndex][0];
85 0 : xd->dequant_uv[0] = pc->UVdequant[QIndex][0];
86 :
87 0 : for (i = 1; i < 16; ++i) {
88 0 : xd->dequant_y1_dc[i] = xd->dequant_y1[i] = pc->Y1dequant[QIndex][1];
89 0 : xd->dequant_y2[i] = pc->Y2dequant[QIndex][1];
90 0 : xd->dequant_uv[i] = pc->UVdequant[QIndex][1];
91 : }
92 0 : }
93 :
94 0 : static void decode_macroblock(VP8D_COMP *pbi, MACROBLOCKD *xd,
95 : unsigned int mb_idx) {
96 : MB_PREDICTION_MODE mode;
97 : int i;
98 : #if CONFIG_ERROR_CONCEALMENT
99 : int corruption_detected = 0;
100 : #else
101 : (void)mb_idx;
102 : #endif
103 :
104 0 : if (xd->mode_info_context->mbmi.mb_skip_coeff) {
105 0 : vp8_reset_mb_tokens_context(xd);
106 0 : } else if (!vp8dx_bool_error(xd->current_bc)) {
107 : int eobtotal;
108 0 : eobtotal = vp8_decode_mb_tokens(pbi, xd);
109 :
110 : /* Special case: Force the loopfilter to skip when eobtotal is zero */
111 0 : xd->mode_info_context->mbmi.mb_skip_coeff = (eobtotal == 0);
112 : }
113 :
114 0 : mode = xd->mode_info_context->mbmi.mode;
115 :
116 0 : if (xd->segmentation_enabled) vp8_mb_init_dequantizer(pbi, xd);
117 :
118 : #if CONFIG_ERROR_CONCEALMENT
119 :
120 : if (pbi->ec_active) {
121 : int throw_residual;
122 : /* When we have independent partitions we can apply residual even
123 : * though other partitions within the frame are corrupt.
124 : */
125 : throw_residual =
126 : (!pbi->independent_partitions && pbi->frame_corrupt_residual);
127 : throw_residual = (throw_residual || vp8dx_bool_error(xd->current_bc));
128 :
129 : if ((mb_idx >= pbi->mvs_corrupt_from_mb || throw_residual)) {
130 : /* MB with corrupt residuals or corrupt mode/motion vectors.
131 : * Better to use the predictor as reconstruction.
132 : */
133 : pbi->frame_corrupt_residual = 1;
134 : memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
135 :
136 : corruption_detected = 1;
137 :
138 : /* force idct to be skipped for B_PRED and use the
139 : * prediction only for reconstruction
140 : * */
141 : memset(xd->eobs, 0, 25);
142 : }
143 : }
144 : #endif
145 :
146 : /* do prediction */
147 0 : if (xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME) {
148 0 : vp8_build_intra_predictors_mbuv_s(
149 : xd, xd->recon_above[1], xd->recon_above[2], xd->recon_left[1],
150 0 : xd->recon_left[2], xd->recon_left_stride[1], xd->dst.u_buffer,
151 0 : xd->dst.v_buffer, xd->dst.uv_stride);
152 :
153 0 : if (mode != B_PRED) {
154 0 : vp8_build_intra_predictors_mby_s(
155 : xd, xd->recon_above[0], xd->recon_left[0], xd->recon_left_stride[0],
156 0 : xd->dst.y_buffer, xd->dst.y_stride);
157 : } else {
158 0 : short *DQC = xd->dequant_y1;
159 0 : int dst_stride = xd->dst.y_stride;
160 :
161 : /* clear out residual eob info */
162 0 : if (xd->mode_info_context->mbmi.mb_skip_coeff) memset(xd->eobs, 0, 25);
163 :
164 0 : intra_prediction_down_copy(xd, xd->recon_above[0] + 16);
165 :
166 0 : for (i = 0; i < 16; ++i) {
167 0 : BLOCKD *b = &xd->block[i];
168 0 : unsigned char *dst = xd->dst.y_buffer + b->offset;
169 0 : B_PREDICTION_MODE b_mode = xd->mode_info_context->bmi[i].as_mode;
170 0 : unsigned char *Above = dst - dst_stride;
171 0 : unsigned char *yleft = dst - 1;
172 0 : int left_stride = dst_stride;
173 0 : unsigned char top_left = Above[-1];
174 :
175 0 : vp8_intra4x4_predict(Above, yleft, left_stride, b_mode, dst, dst_stride,
176 : top_left);
177 :
178 0 : if (xd->eobs[i]) {
179 0 : if (xd->eobs[i] > 1) {
180 0 : vp8_dequant_idct_add(b->qcoeff, DQC, dst, dst_stride);
181 : } else {
182 0 : vp8_dc_only_idct_add(b->qcoeff[0] * DQC[0], dst, dst_stride, dst,
183 : dst_stride);
184 0 : memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
185 : }
186 : }
187 : }
188 : }
189 : } else {
190 0 : vp8_build_inter_predictors_mb(xd);
191 : }
192 :
193 : #if CONFIG_ERROR_CONCEALMENT
194 : if (corruption_detected) {
195 : return;
196 : }
197 : #endif
198 :
199 0 : if (!xd->mode_info_context->mbmi.mb_skip_coeff) {
200 : /* dequantization and idct */
201 0 : if (mode != B_PRED) {
202 0 : short *DQC = xd->dequant_y1;
203 :
204 0 : if (mode != SPLITMV) {
205 0 : BLOCKD *b = &xd->block[24];
206 :
207 : /* do 2nd order transform on the dc block */
208 0 : if (xd->eobs[24] > 1) {
209 0 : vp8_dequantize_b(b, xd->dequant_y2);
210 :
211 0 : vp8_short_inv_walsh4x4(&b->dqcoeff[0], xd->qcoeff);
212 0 : memset(b->qcoeff, 0, 16 * sizeof(b->qcoeff[0]));
213 : } else {
214 0 : b->dqcoeff[0] = b->qcoeff[0] * xd->dequant_y2[0];
215 0 : vp8_short_inv_walsh4x4_1(&b->dqcoeff[0], xd->qcoeff);
216 0 : memset(b->qcoeff, 0, 2 * sizeof(b->qcoeff[0]));
217 : }
218 :
219 : /* override the dc dequant constant in order to preserve the
220 : * dc components
221 : */
222 0 : DQC = xd->dequant_y1_dc;
223 : }
224 :
225 0 : vp8_dequant_idct_add_y_block(xd->qcoeff, DQC, xd->dst.y_buffer,
226 0 : xd->dst.y_stride, xd->eobs);
227 : }
228 :
229 0 : vp8_dequant_idct_add_uv_block(xd->qcoeff + 16 * 16, xd->dequant_uv,
230 0 : xd->dst.u_buffer, xd->dst.v_buffer,
231 0 : xd->dst.uv_stride, xd->eobs + 16);
232 : }
233 0 : }
234 :
235 0 : static int get_delta_q(vp8_reader *bc, int prev, int *q_update) {
236 0 : int ret_val = 0;
237 :
238 0 : if (vp8_read_bit(bc)) {
239 0 : ret_val = vp8_read_literal(bc, 4);
240 :
241 0 : if (vp8_read_bit(bc)) ret_val = -ret_val;
242 : }
243 :
244 : /* Trigger a quantizer update if the delta-q value has changed */
245 0 : if (ret_val != prev) *q_update = 1;
246 :
247 0 : return ret_val;
248 : }
249 :
250 : #ifdef PACKET_TESTING
251 : #include <stdio.h>
252 : FILE *vpxlog = 0;
253 : #endif
254 :
255 0 : static void yv12_extend_frame_top_c(YV12_BUFFER_CONFIG *ybf) {
256 : int i;
257 : unsigned char *src_ptr1;
258 : unsigned char *dest_ptr1;
259 :
260 : unsigned int Border;
261 : int plane_stride;
262 :
263 : /***********/
264 : /* Y Plane */
265 : /***********/
266 0 : Border = ybf->border;
267 0 : plane_stride = ybf->y_stride;
268 0 : src_ptr1 = ybf->y_buffer - Border;
269 0 : dest_ptr1 = src_ptr1 - (Border * plane_stride);
270 :
271 0 : for (i = 0; i < (int)Border; ++i) {
272 0 : memcpy(dest_ptr1, src_ptr1, plane_stride);
273 0 : dest_ptr1 += plane_stride;
274 : }
275 :
276 : /***********/
277 : /* U Plane */
278 : /***********/
279 0 : plane_stride = ybf->uv_stride;
280 0 : Border /= 2;
281 0 : src_ptr1 = ybf->u_buffer - Border;
282 0 : dest_ptr1 = src_ptr1 - (Border * plane_stride);
283 :
284 0 : for (i = 0; i < (int)(Border); ++i) {
285 0 : memcpy(dest_ptr1, src_ptr1, plane_stride);
286 0 : dest_ptr1 += plane_stride;
287 : }
288 :
289 : /***********/
290 : /* V Plane */
291 : /***********/
292 :
293 0 : src_ptr1 = ybf->v_buffer - Border;
294 0 : dest_ptr1 = src_ptr1 - (Border * plane_stride);
295 :
296 0 : for (i = 0; i < (int)(Border); ++i) {
297 0 : memcpy(dest_ptr1, src_ptr1, plane_stride);
298 0 : dest_ptr1 += plane_stride;
299 : }
300 0 : }
301 :
302 0 : static void yv12_extend_frame_bottom_c(YV12_BUFFER_CONFIG *ybf) {
303 : int i;
304 : unsigned char *src_ptr1, *src_ptr2;
305 : unsigned char *dest_ptr2;
306 :
307 : unsigned int Border;
308 : int plane_stride;
309 : int plane_height;
310 :
311 : /***********/
312 : /* Y Plane */
313 : /***********/
314 0 : Border = ybf->border;
315 0 : plane_stride = ybf->y_stride;
316 0 : plane_height = ybf->y_height;
317 :
318 0 : src_ptr1 = ybf->y_buffer - Border;
319 0 : src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
320 0 : dest_ptr2 = src_ptr2 + plane_stride;
321 :
322 0 : for (i = 0; i < (int)Border; ++i) {
323 0 : memcpy(dest_ptr2, src_ptr2, plane_stride);
324 0 : dest_ptr2 += plane_stride;
325 : }
326 :
327 : /***********/
328 : /* U Plane */
329 : /***********/
330 0 : plane_stride = ybf->uv_stride;
331 0 : plane_height = ybf->uv_height;
332 0 : Border /= 2;
333 :
334 0 : src_ptr1 = ybf->u_buffer - Border;
335 0 : src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
336 0 : dest_ptr2 = src_ptr2 + plane_stride;
337 :
338 0 : for (i = 0; i < (int)(Border); ++i) {
339 0 : memcpy(dest_ptr2, src_ptr2, plane_stride);
340 0 : dest_ptr2 += plane_stride;
341 : }
342 :
343 : /***********/
344 : /* V Plane */
345 : /***********/
346 :
347 0 : src_ptr1 = ybf->v_buffer - Border;
348 0 : src_ptr2 = src_ptr1 + (plane_height * plane_stride) - plane_stride;
349 0 : dest_ptr2 = src_ptr2 + plane_stride;
350 :
351 0 : for (i = 0; i < (int)(Border); ++i) {
352 0 : memcpy(dest_ptr2, src_ptr2, plane_stride);
353 0 : dest_ptr2 += plane_stride;
354 : }
355 0 : }
356 :
357 0 : static void yv12_extend_frame_left_right_c(YV12_BUFFER_CONFIG *ybf,
358 : unsigned char *y_src,
359 : unsigned char *u_src,
360 : unsigned char *v_src) {
361 : int i;
362 : unsigned char *src_ptr1, *src_ptr2;
363 : unsigned char *dest_ptr1, *dest_ptr2;
364 :
365 : unsigned int Border;
366 : int plane_stride;
367 : int plane_height;
368 : int plane_width;
369 :
370 : /***********/
371 : /* Y Plane */
372 : /***********/
373 0 : Border = ybf->border;
374 0 : plane_stride = ybf->y_stride;
375 0 : plane_height = 16;
376 0 : plane_width = ybf->y_width;
377 :
378 : /* copy the left and right most columns out */
379 0 : src_ptr1 = y_src;
380 0 : src_ptr2 = src_ptr1 + plane_width - 1;
381 0 : dest_ptr1 = src_ptr1 - Border;
382 0 : dest_ptr2 = src_ptr2 + 1;
383 :
384 0 : for (i = 0; i < plane_height; ++i) {
385 0 : memset(dest_ptr1, src_ptr1[0], Border);
386 0 : memset(dest_ptr2, src_ptr2[0], Border);
387 0 : src_ptr1 += plane_stride;
388 0 : src_ptr2 += plane_stride;
389 0 : dest_ptr1 += plane_stride;
390 0 : dest_ptr2 += plane_stride;
391 : }
392 :
393 : /***********/
394 : /* U Plane */
395 : /***********/
396 0 : plane_stride = ybf->uv_stride;
397 0 : plane_height = 8;
398 0 : plane_width = ybf->uv_width;
399 0 : Border /= 2;
400 :
401 : /* copy the left and right most columns out */
402 0 : src_ptr1 = u_src;
403 0 : src_ptr2 = src_ptr1 + plane_width - 1;
404 0 : dest_ptr1 = src_ptr1 - Border;
405 0 : dest_ptr2 = src_ptr2 + 1;
406 :
407 0 : for (i = 0; i < plane_height; ++i) {
408 0 : memset(dest_ptr1, src_ptr1[0], Border);
409 0 : memset(dest_ptr2, src_ptr2[0], Border);
410 0 : src_ptr1 += plane_stride;
411 0 : src_ptr2 += plane_stride;
412 0 : dest_ptr1 += plane_stride;
413 0 : dest_ptr2 += plane_stride;
414 : }
415 :
416 : /***********/
417 : /* V Plane */
418 : /***********/
419 :
420 : /* copy the left and right most columns out */
421 0 : src_ptr1 = v_src;
422 0 : src_ptr2 = src_ptr1 + plane_width - 1;
423 0 : dest_ptr1 = src_ptr1 - Border;
424 0 : dest_ptr2 = src_ptr2 + 1;
425 :
426 0 : for (i = 0; i < plane_height; ++i) {
427 0 : memset(dest_ptr1, src_ptr1[0], Border);
428 0 : memset(dest_ptr2, src_ptr2[0], Border);
429 0 : src_ptr1 += plane_stride;
430 0 : src_ptr2 += plane_stride;
431 0 : dest_ptr1 += plane_stride;
432 0 : dest_ptr2 += plane_stride;
433 : }
434 0 : }
435 :
436 0 : static void decode_mb_rows(VP8D_COMP *pbi) {
437 0 : VP8_COMMON *const pc = &pbi->common;
438 0 : MACROBLOCKD *const xd = &pbi->mb;
439 :
440 0 : MODE_INFO *lf_mic = xd->mode_info_context;
441 :
442 0 : int ibc = 0;
443 0 : int num_part = 1 << pc->multi_token_partition;
444 :
445 : int recon_yoffset, recon_uvoffset;
446 : int mb_row, mb_col;
447 0 : int mb_idx = 0;
448 :
449 0 : YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
450 :
451 0 : int recon_y_stride = yv12_fb_new->y_stride;
452 0 : int recon_uv_stride = yv12_fb_new->uv_stride;
453 :
454 : unsigned char *ref_buffer[MAX_REF_FRAMES][3];
455 : unsigned char *dst_buffer[3];
456 : unsigned char *lf_dst[3];
457 : unsigned char *eb_dst[3];
458 : int i;
459 : int ref_fb_corrupted[MAX_REF_FRAMES];
460 :
461 0 : ref_fb_corrupted[INTRA_FRAME] = 0;
462 :
463 0 : for (i = 1; i < MAX_REF_FRAMES; ++i) {
464 0 : YV12_BUFFER_CONFIG *this_fb = pbi->dec_fb_ref[i];
465 :
466 0 : ref_buffer[i][0] = this_fb->y_buffer;
467 0 : ref_buffer[i][1] = this_fb->u_buffer;
468 0 : ref_buffer[i][2] = this_fb->v_buffer;
469 :
470 0 : ref_fb_corrupted[i] = this_fb->corrupted;
471 : }
472 :
473 : /* Set up the buffer pointers */
474 0 : eb_dst[0] = lf_dst[0] = dst_buffer[0] = yv12_fb_new->y_buffer;
475 0 : eb_dst[1] = lf_dst[1] = dst_buffer[1] = yv12_fb_new->u_buffer;
476 0 : eb_dst[2] = lf_dst[2] = dst_buffer[2] = yv12_fb_new->v_buffer;
477 :
478 0 : xd->up_available = 0;
479 :
480 : /* Initialize the loop filter for this frame. */
481 0 : if (pc->filter_level) vp8_loop_filter_frame_init(pc, xd, pc->filter_level);
482 :
483 0 : vp8_setup_intra_recon_top_line(yv12_fb_new);
484 :
485 : /* Decode the individual macro block */
486 0 : for (mb_row = 0; mb_row < pc->mb_rows; ++mb_row) {
487 0 : if (num_part > 1) {
488 0 : xd->current_bc = &pbi->mbc[ibc];
489 0 : ibc++;
490 :
491 0 : if (ibc == num_part) ibc = 0;
492 : }
493 :
494 0 : recon_yoffset = mb_row * recon_y_stride * 16;
495 0 : recon_uvoffset = mb_row * recon_uv_stride * 8;
496 :
497 : /* reset contexts */
498 0 : xd->above_context = pc->above_context;
499 0 : memset(xd->left_context, 0, sizeof(ENTROPY_CONTEXT_PLANES));
500 :
501 0 : xd->left_available = 0;
502 :
503 0 : xd->mb_to_top_edge = -((mb_row * 16) << 3);
504 0 : xd->mb_to_bottom_edge = ((pc->mb_rows - 1 - mb_row) * 16) << 3;
505 :
506 0 : xd->recon_above[0] = dst_buffer[0] + recon_yoffset;
507 0 : xd->recon_above[1] = dst_buffer[1] + recon_uvoffset;
508 0 : xd->recon_above[2] = dst_buffer[2] + recon_uvoffset;
509 :
510 0 : xd->recon_left[0] = xd->recon_above[0] - 1;
511 0 : xd->recon_left[1] = xd->recon_above[1] - 1;
512 0 : xd->recon_left[2] = xd->recon_above[2] - 1;
513 :
514 0 : xd->recon_above[0] -= xd->dst.y_stride;
515 0 : xd->recon_above[1] -= xd->dst.uv_stride;
516 0 : xd->recon_above[2] -= xd->dst.uv_stride;
517 :
518 : /* TODO: move to outside row loop */
519 0 : xd->recon_left_stride[0] = xd->dst.y_stride;
520 0 : xd->recon_left_stride[1] = xd->dst.uv_stride;
521 :
522 0 : setup_intra_recon_left(xd->recon_left[0], xd->recon_left[1],
523 : xd->recon_left[2], xd->dst.y_stride,
524 : xd->dst.uv_stride);
525 :
526 0 : for (mb_col = 0; mb_col < pc->mb_cols; ++mb_col) {
527 : /* Distance of Mb to the various image edges.
528 : * These are specified to 8th pel as they are always compared to values
529 : * that are in 1/8th pel units
530 : */
531 0 : xd->mb_to_left_edge = -((mb_col * 16) << 3);
532 0 : xd->mb_to_right_edge = ((pc->mb_cols - 1 - mb_col) * 16) << 3;
533 :
534 : #if CONFIG_ERROR_CONCEALMENT
535 : {
536 : int corrupt_residual =
537 : (!pbi->independent_partitions && pbi->frame_corrupt_residual) ||
538 : vp8dx_bool_error(xd->current_bc);
539 : if (pbi->ec_active &&
540 : xd->mode_info_context->mbmi.ref_frame == INTRA_FRAME &&
541 : corrupt_residual) {
542 : /* We have an intra block with corrupt coefficients, better to
543 : * conceal with an inter block. Interpolate MVs from neighboring
544 : * MBs.
545 : *
546 : * Note that for the first mb with corrupt residual in a frame,
547 : * we might not discover that before decoding the residual. That
548 : * happens after this check, and therefore no inter concealment
549 : * will be done.
550 : */
551 : vp8_interpolate_motion(xd, mb_row, mb_col, pc->mb_rows, pc->mb_cols);
552 : }
553 : }
554 : #endif
555 :
556 0 : xd->dst.y_buffer = dst_buffer[0] + recon_yoffset;
557 0 : xd->dst.u_buffer = dst_buffer[1] + recon_uvoffset;
558 0 : xd->dst.v_buffer = dst_buffer[2] + recon_uvoffset;
559 :
560 0 : if (xd->mode_info_context->mbmi.ref_frame >= LAST_FRAME) {
561 0 : const MV_REFERENCE_FRAME ref = xd->mode_info_context->mbmi.ref_frame;
562 0 : xd->pre.y_buffer = ref_buffer[ref][0] + recon_yoffset;
563 0 : xd->pre.u_buffer = ref_buffer[ref][1] + recon_uvoffset;
564 0 : xd->pre.v_buffer = ref_buffer[ref][2] + recon_uvoffset;
565 : } else {
566 : // ref_frame is INTRA_FRAME, pre buffer should not be used.
567 0 : xd->pre.y_buffer = 0;
568 0 : xd->pre.u_buffer = 0;
569 0 : xd->pre.v_buffer = 0;
570 : }
571 :
572 : /* propagate errors from reference frames */
573 0 : xd->corrupted |= ref_fb_corrupted[xd->mode_info_context->mbmi.ref_frame];
574 :
575 0 : decode_macroblock(pbi, xd, mb_idx);
576 :
577 0 : mb_idx++;
578 0 : xd->left_available = 1;
579 :
580 : /* check if the boolean decoder has suffered an error */
581 0 : xd->corrupted |= vp8dx_bool_error(xd->current_bc);
582 :
583 0 : xd->recon_above[0] += 16;
584 0 : xd->recon_above[1] += 8;
585 0 : xd->recon_above[2] += 8;
586 0 : xd->recon_left[0] += 16;
587 0 : xd->recon_left[1] += 8;
588 0 : xd->recon_left[2] += 8;
589 :
590 0 : recon_yoffset += 16;
591 0 : recon_uvoffset += 8;
592 :
593 0 : ++xd->mode_info_context; /* next mb */
594 :
595 0 : xd->above_context++;
596 : }
597 :
598 : /* adjust to the next row of mbs */
599 0 : vp8_extend_mb_row(yv12_fb_new, xd->dst.y_buffer + 16, xd->dst.u_buffer + 8,
600 0 : xd->dst.v_buffer + 8);
601 :
602 0 : ++xd->mode_info_context; /* skip prediction column */
603 0 : xd->up_available = 1;
604 :
605 0 : if (pc->filter_level) {
606 0 : if (mb_row > 0) {
607 0 : if (pc->filter_type == NORMAL_LOOPFILTER) {
608 0 : vp8_loop_filter_row_normal(pc, lf_mic, mb_row - 1, recon_y_stride,
609 : recon_uv_stride, lf_dst[0], lf_dst[1],
610 : lf_dst[2]);
611 : } else {
612 0 : vp8_loop_filter_row_simple(pc, lf_mic, mb_row - 1, recon_y_stride,
613 : recon_uv_stride, lf_dst[0], lf_dst[1],
614 : lf_dst[2]);
615 : }
616 0 : if (mb_row > 1) {
617 0 : yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1],
618 : eb_dst[2]);
619 :
620 0 : eb_dst[0] += recon_y_stride * 16;
621 0 : eb_dst[1] += recon_uv_stride * 8;
622 0 : eb_dst[2] += recon_uv_stride * 8;
623 : }
624 :
625 0 : lf_dst[0] += recon_y_stride * 16;
626 0 : lf_dst[1] += recon_uv_stride * 8;
627 0 : lf_dst[2] += recon_uv_stride * 8;
628 0 : lf_mic += pc->mb_cols;
629 0 : lf_mic++; /* Skip border mb */
630 : }
631 : } else {
632 0 : if (mb_row > 0) {
633 : /**/
634 0 : yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1],
635 : eb_dst[2]);
636 0 : eb_dst[0] += recon_y_stride * 16;
637 0 : eb_dst[1] += recon_uv_stride * 8;
638 0 : eb_dst[2] += recon_uv_stride * 8;
639 : }
640 : }
641 : }
642 :
643 0 : if (pc->filter_level) {
644 0 : if (pc->filter_type == NORMAL_LOOPFILTER) {
645 0 : vp8_loop_filter_row_normal(pc, lf_mic, mb_row - 1, recon_y_stride,
646 : recon_uv_stride, lf_dst[0], lf_dst[1],
647 : lf_dst[2]);
648 : } else {
649 0 : vp8_loop_filter_row_simple(pc, lf_mic, mb_row - 1, recon_y_stride,
650 : recon_uv_stride, lf_dst[0], lf_dst[1],
651 : lf_dst[2]);
652 : }
653 :
654 0 : yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1],
655 : eb_dst[2]);
656 0 : eb_dst[0] += recon_y_stride * 16;
657 0 : eb_dst[1] += recon_uv_stride * 8;
658 0 : eb_dst[2] += recon_uv_stride * 8;
659 : }
660 0 : yv12_extend_frame_left_right_c(yv12_fb_new, eb_dst[0], eb_dst[1], eb_dst[2]);
661 0 : yv12_extend_frame_top_c(yv12_fb_new);
662 0 : yv12_extend_frame_bottom_c(yv12_fb_new);
663 0 : }
664 :
665 0 : static unsigned int read_partition_size(VP8D_COMP *pbi,
666 : const unsigned char *cx_size) {
667 : unsigned char temp[3];
668 0 : if (pbi->decrypt_cb) {
669 0 : pbi->decrypt_cb(pbi->decrypt_state, cx_size, temp, 3);
670 0 : cx_size = temp;
671 : }
672 0 : return cx_size[0] + (cx_size[1] << 8) + (cx_size[2] << 16);
673 : }
674 :
675 0 : static int read_is_valid(const unsigned char *start, size_t len,
676 : const unsigned char *end) {
677 0 : return (start + len > start && start + len <= end);
678 : }
679 :
680 0 : static unsigned int read_available_partition_size(
681 : VP8D_COMP *pbi, const unsigned char *token_part_sizes,
682 : const unsigned char *fragment_start,
683 : const unsigned char *first_fragment_end, const unsigned char *fragment_end,
684 : int i, int num_part) {
685 0 : VP8_COMMON *pc = &pbi->common;
686 0 : const unsigned char *partition_size_ptr = token_part_sizes + i * 3;
687 0 : unsigned int partition_size = 0;
688 0 : ptrdiff_t bytes_left = fragment_end - fragment_start;
689 : /* Calculate the length of this partition. The last partition
690 : * size is implicit. If the partition size can't be read, then
691 : * either use the remaining data in the buffer (for EC mode)
692 : * or throw an error.
693 : */
694 0 : if (i < num_part - 1) {
695 0 : if (read_is_valid(partition_size_ptr, 3, first_fragment_end)) {
696 0 : partition_size = read_partition_size(pbi, partition_size_ptr);
697 0 : } else if (pbi->ec_active) {
698 0 : partition_size = (unsigned int)bytes_left;
699 : } else {
700 0 : vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
701 : "Truncated partition size data");
702 : }
703 : } else {
704 0 : partition_size = (unsigned int)bytes_left;
705 : }
706 :
707 : /* Validate the calculated partition length. If the buffer
708 : * described by the partition can't be fully read, then restrict
709 : * it to the portion that can be (for EC mode) or throw an error.
710 : */
711 0 : if (!read_is_valid(fragment_start, partition_size, fragment_end)) {
712 0 : if (pbi->ec_active) {
713 0 : partition_size = (unsigned int)bytes_left;
714 : } else {
715 0 : vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
716 : "Truncated packet or corrupt partition "
717 : "%d length",
718 : i + 1);
719 : }
720 : }
721 0 : return partition_size;
722 : }
723 :
724 0 : static void setup_token_decoder(VP8D_COMP *pbi,
725 : const unsigned char *token_part_sizes) {
726 0 : vp8_reader *bool_decoder = &pbi->mbc[0];
727 : unsigned int partition_idx;
728 : unsigned int fragment_idx;
729 : unsigned int num_token_partitions;
730 0 : const unsigned char *first_fragment_end =
731 0 : pbi->fragments.ptrs[0] + pbi->fragments.sizes[0];
732 :
733 0 : TOKEN_PARTITION multi_token_partition =
734 0 : (TOKEN_PARTITION)vp8_read_literal(&pbi->mbc[8], 2);
735 0 : if (!vp8dx_bool_error(&pbi->mbc[8])) {
736 0 : pbi->common.multi_token_partition = multi_token_partition;
737 : }
738 0 : num_token_partitions = 1 << pbi->common.multi_token_partition;
739 :
740 : /* Check for partitions within the fragments and unpack the fragments
741 : * so that each fragment pointer points to its corresponding partition. */
742 0 : for (fragment_idx = 0; fragment_idx < pbi->fragments.count; ++fragment_idx) {
743 0 : unsigned int fragment_size = pbi->fragments.sizes[fragment_idx];
744 0 : const unsigned char *fragment_end =
745 0 : pbi->fragments.ptrs[fragment_idx] + fragment_size;
746 : /* Special case for handling the first partition since we have already
747 : * read its size. */
748 0 : if (fragment_idx == 0) {
749 : /* Size of first partition + token partition sizes element */
750 0 : ptrdiff_t ext_first_part_size = token_part_sizes -
751 0 : pbi->fragments.ptrs[0] +
752 0 : 3 * (num_token_partitions - 1);
753 0 : fragment_size -= (unsigned int)ext_first_part_size;
754 0 : if (fragment_size > 0) {
755 0 : pbi->fragments.sizes[0] = (unsigned int)ext_first_part_size;
756 : /* The fragment contains an additional partition. Move to
757 : * next. */
758 0 : fragment_idx++;
759 0 : pbi->fragments.ptrs[fragment_idx] =
760 0 : pbi->fragments.ptrs[0] + pbi->fragments.sizes[0];
761 : }
762 : }
763 : /* Split the chunk into partitions read from the bitstream */
764 0 : while (fragment_size > 0) {
765 0 : ptrdiff_t partition_size = read_available_partition_size(
766 : pbi, token_part_sizes, pbi->fragments.ptrs[fragment_idx],
767 0 : first_fragment_end, fragment_end, fragment_idx - 1,
768 : num_token_partitions);
769 0 : pbi->fragments.sizes[fragment_idx] = (unsigned int)partition_size;
770 0 : fragment_size -= (unsigned int)partition_size;
771 0 : assert(fragment_idx <= num_token_partitions);
772 0 : if (fragment_size > 0) {
773 : /* The fragment contains an additional partition.
774 : * Move to next. */
775 0 : fragment_idx++;
776 0 : pbi->fragments.ptrs[fragment_idx] =
777 0 : pbi->fragments.ptrs[fragment_idx - 1] + partition_size;
778 : }
779 : }
780 : }
781 :
782 0 : pbi->fragments.count = num_token_partitions + 1;
783 :
784 0 : for (partition_idx = 1; partition_idx < pbi->fragments.count;
785 0 : ++partition_idx) {
786 0 : if (vp8dx_start_decode(bool_decoder, pbi->fragments.ptrs[partition_idx],
787 : pbi->fragments.sizes[partition_idx], pbi->decrypt_cb,
788 : pbi->decrypt_state)) {
789 0 : vpx_internal_error(&pbi->common.error, VPX_CODEC_MEM_ERROR,
790 : "Failed to allocate bool decoder %d", partition_idx);
791 : }
792 :
793 0 : bool_decoder++;
794 : }
795 :
796 : #if CONFIG_MULTITHREAD
797 : /* Clamp number of decoder threads */
798 0 : if (pbi->decoding_thread_count > num_token_partitions - 1) {
799 0 : pbi->decoding_thread_count = num_token_partitions - 1;
800 : }
801 0 : if ((int)pbi->decoding_thread_count > pbi->common.mb_rows - 1) {
802 0 : assert(pbi->common.mb_rows > 0);
803 0 : pbi->decoding_thread_count = pbi->common.mb_rows - 1;
804 : }
805 : #endif
806 0 : }
807 :
808 0 : static void init_frame(VP8D_COMP *pbi) {
809 0 : VP8_COMMON *const pc = &pbi->common;
810 0 : MACROBLOCKD *const xd = &pbi->mb;
811 :
812 0 : if (pc->frame_type == KEY_FRAME) {
813 : /* Various keyframe initializations */
814 0 : memcpy(pc->fc.mvc, vp8_default_mv_context, sizeof(vp8_default_mv_context));
815 :
816 0 : vp8_init_mbmode_probs(pc);
817 :
818 0 : vp8_default_coef_probs(pc);
819 :
820 : /* reset the segment feature data to 0 with delta coding (Default state). */
821 0 : memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
822 0 : xd->mb_segement_abs_delta = SEGMENT_DELTADATA;
823 :
824 : /* reset the mode ref deltasa for loop filter */
825 0 : memset(xd->ref_lf_deltas, 0, sizeof(xd->ref_lf_deltas));
826 0 : memset(xd->mode_lf_deltas, 0, sizeof(xd->mode_lf_deltas));
827 :
828 : /* All buffers are implicitly updated on key frames. */
829 0 : pc->refresh_golden_frame = 1;
830 0 : pc->refresh_alt_ref_frame = 1;
831 0 : pc->copy_buffer_to_gf = 0;
832 0 : pc->copy_buffer_to_arf = 0;
833 :
834 : /* Note that Golden and Altref modes cannot be used on a key frame so
835 : * ref_frame_sign_bias[] is undefined and meaningless
836 : */
837 0 : pc->ref_frame_sign_bias[GOLDEN_FRAME] = 0;
838 0 : pc->ref_frame_sign_bias[ALTREF_FRAME] = 0;
839 : } else {
840 : /* To enable choice of different interploation filters */
841 0 : if (!pc->use_bilinear_mc_filter) {
842 0 : xd->subpixel_predict = vp8_sixtap_predict4x4;
843 0 : xd->subpixel_predict8x4 = vp8_sixtap_predict8x4;
844 0 : xd->subpixel_predict8x8 = vp8_sixtap_predict8x8;
845 0 : xd->subpixel_predict16x16 = vp8_sixtap_predict16x16;
846 : } else {
847 0 : xd->subpixel_predict = vp8_bilinear_predict4x4;
848 0 : xd->subpixel_predict8x4 = vp8_bilinear_predict8x4;
849 0 : xd->subpixel_predict8x8 = vp8_bilinear_predict8x8;
850 0 : xd->subpixel_predict16x16 = vp8_bilinear_predict16x16;
851 : }
852 :
853 0 : if (pbi->decoded_key_frame && pbi->ec_enabled && !pbi->ec_active) {
854 0 : pbi->ec_active = 1;
855 : }
856 : }
857 :
858 0 : xd->left_context = &pc->left_context;
859 0 : xd->mode_info_context = pc->mi;
860 0 : xd->frame_type = pc->frame_type;
861 0 : xd->mode_info_context->mbmi.mode = DC_PRED;
862 0 : xd->mode_info_stride = pc->mode_info_stride;
863 0 : xd->corrupted = 0; /* init without corruption */
864 :
865 0 : xd->fullpixel_mask = 0xffffffff;
866 0 : if (pc->full_pixel) xd->fullpixel_mask = 0xfffffff8;
867 0 : }
868 :
869 0 : int vp8_decode_frame(VP8D_COMP *pbi) {
870 0 : vp8_reader *const bc = &pbi->mbc[8];
871 0 : VP8_COMMON *const pc = &pbi->common;
872 0 : MACROBLOCKD *const xd = &pbi->mb;
873 0 : const unsigned char *data = pbi->fragments.ptrs[0];
874 0 : const unsigned int data_sz = pbi->fragments.sizes[0];
875 0 : const unsigned char *data_end = data + data_sz;
876 : ptrdiff_t first_partition_length_in_bytes;
877 :
878 : int i, j, k, l;
879 0 : const int *const mb_feature_data_bits = vp8_mb_feature_data_bits;
880 0 : int corrupt_tokens = 0;
881 0 : int prev_independent_partitions = pbi->independent_partitions;
882 :
883 0 : YV12_BUFFER_CONFIG *yv12_fb_new = pbi->dec_fb_ref[INTRA_FRAME];
884 :
885 : /* start with no corruption of current frame */
886 0 : xd->corrupted = 0;
887 0 : yv12_fb_new->corrupted = 0;
888 :
889 0 : if (data_end - data < 3) {
890 0 : if (!pbi->ec_active) {
891 0 : vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
892 : "Truncated packet");
893 : }
894 :
895 : /* Declare the missing frame as an inter frame since it will
896 : be handled as an inter frame when we have estimated its
897 : motion vectors. */
898 0 : pc->frame_type = INTER_FRAME;
899 0 : pc->version = 0;
900 0 : pc->show_frame = 1;
901 0 : first_partition_length_in_bytes = 0;
902 : } else {
903 : unsigned char clear_buffer[10];
904 0 : const unsigned char *clear = data;
905 0 : if (pbi->decrypt_cb) {
906 0 : int n = (int)VPXMIN(sizeof(clear_buffer), data_sz);
907 0 : pbi->decrypt_cb(pbi->decrypt_state, data, clear_buffer, n);
908 0 : clear = clear_buffer;
909 : }
910 :
911 0 : pc->frame_type = (FRAME_TYPE)(clear[0] & 1);
912 0 : pc->version = (clear[0] >> 1) & 7;
913 0 : pc->show_frame = (clear[0] >> 4) & 1;
914 0 : first_partition_length_in_bytes =
915 0 : (clear[0] | (clear[1] << 8) | (clear[2] << 16)) >> 5;
916 :
917 0 : if (!pbi->ec_active && (data + first_partition_length_in_bytes > data_end ||
918 0 : data + first_partition_length_in_bytes < data)) {
919 0 : vpx_internal_error(&pc->error, VPX_CODEC_CORRUPT_FRAME,
920 : "Truncated packet or corrupt partition 0 length");
921 : }
922 :
923 0 : data += 3;
924 0 : clear += 3;
925 :
926 0 : vp8_setup_version(pc);
927 :
928 0 : if (pc->frame_type == KEY_FRAME) {
929 : /* vet via sync code */
930 : /* When error concealment is enabled we should only check the sync
931 : * code if we have enough bits available
932 : */
933 0 : if (!pbi->ec_active || data + 3 < data_end) {
934 0 : if (clear[0] != 0x9d || clear[1] != 0x01 || clear[2] != 0x2a) {
935 0 : vpx_internal_error(&pc->error, VPX_CODEC_UNSUP_BITSTREAM,
936 : "Invalid frame sync code");
937 : }
938 : }
939 :
940 : /* If error concealment is enabled we should only parse the new size
941 : * if we have enough data. Otherwise we will end up with the wrong
942 : * size.
943 : */
944 0 : if (!pbi->ec_active || data + 6 < data_end) {
945 0 : pc->Width = (clear[3] | (clear[4] << 8)) & 0x3fff;
946 0 : pc->horiz_scale = clear[4] >> 6;
947 0 : pc->Height = (clear[5] | (clear[6] << 8)) & 0x3fff;
948 0 : pc->vert_scale = clear[6] >> 6;
949 : }
950 0 : data += 7;
951 : } else {
952 0 : memcpy(&xd->pre, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
953 0 : memcpy(&xd->dst, yv12_fb_new, sizeof(YV12_BUFFER_CONFIG));
954 : }
955 : }
956 0 : if ((!pbi->decoded_key_frame && pc->frame_type != KEY_FRAME)) {
957 0 : return -1;
958 : }
959 :
960 0 : init_frame(pbi);
961 :
962 0 : if (vp8dx_start_decode(bc, data, (unsigned int)(data_end - data),
963 : pbi->decrypt_cb, pbi->decrypt_state)) {
964 0 : vpx_internal_error(&pc->error, VPX_CODEC_MEM_ERROR,
965 : "Failed to allocate bool decoder 0");
966 : }
967 0 : if (pc->frame_type == KEY_FRAME) {
968 0 : (void)vp8_read_bit(bc); // colorspace
969 0 : pc->clamp_type = (CLAMP_TYPE)vp8_read_bit(bc);
970 : }
971 :
972 : /* Is segmentation enabled */
973 0 : xd->segmentation_enabled = (unsigned char)vp8_read_bit(bc);
974 :
975 0 : if (xd->segmentation_enabled) {
976 : /* Signal whether or not the segmentation map is being explicitly updated
977 : * this frame. */
978 0 : xd->update_mb_segmentation_map = (unsigned char)vp8_read_bit(bc);
979 0 : xd->update_mb_segmentation_data = (unsigned char)vp8_read_bit(bc);
980 :
981 0 : if (xd->update_mb_segmentation_data) {
982 0 : xd->mb_segement_abs_delta = (unsigned char)vp8_read_bit(bc);
983 :
984 0 : memset(xd->segment_feature_data, 0, sizeof(xd->segment_feature_data));
985 :
986 : /* For each segmentation feature (Quant and loop filter level) */
987 0 : for (i = 0; i < MB_LVL_MAX; ++i) {
988 0 : for (j = 0; j < MAX_MB_SEGMENTS; ++j) {
989 : /* Frame level data */
990 0 : if (vp8_read_bit(bc)) {
991 0 : xd->segment_feature_data[i][j] =
992 0 : (signed char)vp8_read_literal(bc, mb_feature_data_bits[i]);
993 :
994 0 : if (vp8_read_bit(bc)) {
995 0 : xd->segment_feature_data[i][j] = -xd->segment_feature_data[i][j];
996 : }
997 : } else {
998 0 : xd->segment_feature_data[i][j] = 0;
999 : }
1000 : }
1001 : }
1002 : }
1003 :
1004 0 : if (xd->update_mb_segmentation_map) {
1005 : /* Which macro block level features are enabled */
1006 0 : memset(xd->mb_segment_tree_probs, 255, sizeof(xd->mb_segment_tree_probs));
1007 :
1008 : /* Read the probs used to decode the segment id for each macro block. */
1009 0 : for (i = 0; i < MB_FEATURE_TREE_PROBS; ++i) {
1010 : /* If not explicitly set value is defaulted to 255 by memset above */
1011 0 : if (vp8_read_bit(bc)) {
1012 0 : xd->mb_segment_tree_probs[i] = (vp8_prob)vp8_read_literal(bc, 8);
1013 : }
1014 : }
1015 : }
1016 : } else {
1017 : /* No segmentation updates on this frame */
1018 0 : xd->update_mb_segmentation_map = 0;
1019 0 : xd->update_mb_segmentation_data = 0;
1020 : }
1021 :
1022 : /* Read the loop filter level and type */
1023 0 : pc->filter_type = (LOOPFILTERTYPE)vp8_read_bit(bc);
1024 0 : pc->filter_level = vp8_read_literal(bc, 6);
1025 0 : pc->sharpness_level = vp8_read_literal(bc, 3);
1026 :
1027 : /* Read in loop filter deltas applied at the MB level based on mode or ref
1028 : * frame. */
1029 0 : xd->mode_ref_lf_delta_update = 0;
1030 0 : xd->mode_ref_lf_delta_enabled = (unsigned char)vp8_read_bit(bc);
1031 :
1032 0 : if (xd->mode_ref_lf_delta_enabled) {
1033 : /* Do the deltas need to be updated */
1034 0 : xd->mode_ref_lf_delta_update = (unsigned char)vp8_read_bit(bc);
1035 :
1036 0 : if (xd->mode_ref_lf_delta_update) {
1037 : /* Send update */
1038 0 : for (i = 0; i < MAX_REF_LF_DELTAS; ++i) {
1039 0 : if (vp8_read_bit(bc)) {
1040 : /*sign = vp8_read_bit( bc );*/
1041 0 : xd->ref_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
1042 :
1043 0 : if (vp8_read_bit(bc)) { /* Apply sign */
1044 0 : xd->ref_lf_deltas[i] = xd->ref_lf_deltas[i] * -1;
1045 : }
1046 : }
1047 : }
1048 :
1049 : /* Send update */
1050 0 : for (i = 0; i < MAX_MODE_LF_DELTAS; ++i) {
1051 0 : if (vp8_read_bit(bc)) {
1052 : /*sign = vp8_read_bit( bc );*/
1053 0 : xd->mode_lf_deltas[i] = (signed char)vp8_read_literal(bc, 6);
1054 :
1055 0 : if (vp8_read_bit(bc)) { /* Apply sign */
1056 0 : xd->mode_lf_deltas[i] = xd->mode_lf_deltas[i] * -1;
1057 : }
1058 : }
1059 : }
1060 : }
1061 : }
1062 :
1063 0 : setup_token_decoder(pbi, data + first_partition_length_in_bytes);
1064 :
1065 0 : xd->current_bc = &pbi->mbc[0];
1066 :
1067 : /* Read the default quantizers. */
1068 : {
1069 : int Q, q_update;
1070 :
1071 0 : Q = vp8_read_literal(bc, 7); /* AC 1st order Q = default */
1072 0 : pc->base_qindex = Q;
1073 0 : q_update = 0;
1074 0 : pc->y1dc_delta_q = get_delta_q(bc, pc->y1dc_delta_q, &q_update);
1075 0 : pc->y2dc_delta_q = get_delta_q(bc, pc->y2dc_delta_q, &q_update);
1076 0 : pc->y2ac_delta_q = get_delta_q(bc, pc->y2ac_delta_q, &q_update);
1077 0 : pc->uvdc_delta_q = get_delta_q(bc, pc->uvdc_delta_q, &q_update);
1078 0 : pc->uvac_delta_q = get_delta_q(bc, pc->uvac_delta_q, &q_update);
1079 :
1080 0 : if (q_update) vp8cx_init_de_quantizer(pbi);
1081 :
1082 : /* MB level dequantizer setup */
1083 0 : vp8_mb_init_dequantizer(pbi, &pbi->mb);
1084 : }
1085 :
1086 : /* Determine if the golden frame or ARF buffer should be updated and how.
1087 : * For all non key frames the GF and ARF refresh flags and sign bias
1088 : * flags must be set explicitly.
1089 : */
1090 0 : if (pc->frame_type != KEY_FRAME) {
1091 : /* Should the GF or ARF be updated from the current frame */
1092 0 : pc->refresh_golden_frame = vp8_read_bit(bc);
1093 : #if CONFIG_ERROR_CONCEALMENT
1094 : /* Assume we shouldn't refresh golden if the bit is missing */
1095 : xd->corrupted |= vp8dx_bool_error(bc);
1096 : if (pbi->ec_active && xd->corrupted) pc->refresh_golden_frame = 0;
1097 : #endif
1098 :
1099 0 : pc->refresh_alt_ref_frame = vp8_read_bit(bc);
1100 : #if CONFIG_ERROR_CONCEALMENT
1101 : /* Assume we shouldn't refresh altref if the bit is missing */
1102 : xd->corrupted |= vp8dx_bool_error(bc);
1103 : if (pbi->ec_active && xd->corrupted) pc->refresh_alt_ref_frame = 0;
1104 : #endif
1105 :
1106 : /* Buffer to buffer copy flags. */
1107 0 : pc->copy_buffer_to_gf = 0;
1108 :
1109 0 : if (!pc->refresh_golden_frame) {
1110 0 : pc->copy_buffer_to_gf = vp8_read_literal(bc, 2);
1111 : }
1112 :
1113 : #if CONFIG_ERROR_CONCEALMENT
1114 : /* Assume we shouldn't copy to the golden if the bit is missing */
1115 : xd->corrupted |= vp8dx_bool_error(bc);
1116 : if (pbi->ec_active && xd->corrupted) pc->copy_buffer_to_gf = 0;
1117 : #endif
1118 :
1119 0 : pc->copy_buffer_to_arf = 0;
1120 :
1121 0 : if (!pc->refresh_alt_ref_frame) {
1122 0 : pc->copy_buffer_to_arf = vp8_read_literal(bc, 2);
1123 : }
1124 :
1125 : #if CONFIG_ERROR_CONCEALMENT
1126 : /* Assume we shouldn't copy to the alt-ref if the bit is missing */
1127 : xd->corrupted |= vp8dx_bool_error(bc);
1128 : if (pbi->ec_active && xd->corrupted) pc->copy_buffer_to_arf = 0;
1129 : #endif
1130 :
1131 0 : pc->ref_frame_sign_bias[GOLDEN_FRAME] = vp8_read_bit(bc);
1132 0 : pc->ref_frame_sign_bias[ALTREF_FRAME] = vp8_read_bit(bc);
1133 : }
1134 :
1135 0 : pc->refresh_entropy_probs = vp8_read_bit(bc);
1136 : #if CONFIG_ERROR_CONCEALMENT
1137 : /* Assume we shouldn't refresh the probabilities if the bit is
1138 : * missing */
1139 : xd->corrupted |= vp8dx_bool_error(bc);
1140 : if (pbi->ec_active && xd->corrupted) pc->refresh_entropy_probs = 0;
1141 : #endif
1142 0 : if (pc->refresh_entropy_probs == 0) {
1143 0 : memcpy(&pc->lfc, &pc->fc, sizeof(pc->fc));
1144 : }
1145 :
1146 0 : pc->refresh_last_frame = pc->frame_type == KEY_FRAME || vp8_read_bit(bc);
1147 :
1148 : #if CONFIG_ERROR_CONCEALMENT
1149 : /* Assume we should refresh the last frame if the bit is missing */
1150 : xd->corrupted |= vp8dx_bool_error(bc);
1151 : if (pbi->ec_active && xd->corrupted) pc->refresh_last_frame = 1;
1152 : #endif
1153 :
1154 : if (0) {
1155 : FILE *z = fopen("decodestats.stt", "a");
1156 : fprintf(z, "%6d F:%d,G:%d,A:%d,L:%d,Q:%d\n", pc->current_video_frame,
1157 : pc->frame_type, pc->refresh_golden_frame, pc->refresh_alt_ref_frame,
1158 : pc->refresh_last_frame, pc->base_qindex);
1159 : fclose(z);
1160 : }
1161 :
1162 : {
1163 0 : pbi->independent_partitions = 1;
1164 :
1165 : /* read coef probability tree */
1166 0 : for (i = 0; i < BLOCK_TYPES; ++i) {
1167 0 : for (j = 0; j < COEF_BANDS; ++j) {
1168 0 : for (k = 0; k < PREV_COEF_CONTEXTS; ++k) {
1169 0 : for (l = 0; l < ENTROPY_NODES; ++l) {
1170 0 : vp8_prob *const p = pc->fc.coef_probs[i][j][k] + l;
1171 :
1172 0 : if (vp8_read(bc, vp8_coef_update_probs[i][j][k][l])) {
1173 0 : *p = (vp8_prob)vp8_read_literal(bc, 8);
1174 : }
1175 0 : if (k > 0 && *p != pc->fc.coef_probs[i][j][k - 1][l]) {
1176 0 : pbi->independent_partitions = 0;
1177 : }
1178 : }
1179 : }
1180 : }
1181 : }
1182 : }
1183 :
1184 : /* clear out the coeff buffer */
1185 0 : memset(xd->qcoeff, 0, sizeof(xd->qcoeff));
1186 :
1187 0 : vp8_decode_mode_mvs(pbi);
1188 :
1189 : #if CONFIG_ERROR_CONCEALMENT
1190 : if (pbi->ec_active &&
1191 : pbi->mvs_corrupt_from_mb < (unsigned int)pc->mb_cols * pc->mb_rows) {
1192 : /* Motion vectors are missing in this frame. We will try to estimate
1193 : * them and then continue decoding the frame as usual */
1194 : vp8_estimate_missing_mvs(pbi);
1195 : }
1196 : #endif
1197 :
1198 0 : memset(pc->above_context, 0, sizeof(ENTROPY_CONTEXT_PLANES) * pc->mb_cols);
1199 0 : pbi->frame_corrupt_residual = 0;
1200 :
1201 : #if CONFIG_MULTITHREAD
1202 0 : if (pbi->b_multithreaded_rd && pc->multi_token_partition != ONE_PARTITION) {
1203 : unsigned int thread;
1204 0 : vp8mt_decode_mb_rows(pbi, xd);
1205 0 : vp8_yv12_extend_frame_borders(yv12_fb_new);
1206 0 : for (thread = 0; thread < pbi->decoding_thread_count; ++thread) {
1207 0 : corrupt_tokens |= pbi->mb_row_di[thread].mbd.corrupted;
1208 : }
1209 : } else
1210 : #endif
1211 : {
1212 0 : decode_mb_rows(pbi);
1213 0 : corrupt_tokens |= xd->corrupted;
1214 : }
1215 :
1216 : /* Collect information about decoder corruption. */
1217 : /* 1. Check first boolean decoder for errors. */
1218 0 : yv12_fb_new->corrupted = vp8dx_bool_error(bc);
1219 : /* 2. Check the macroblock information */
1220 0 : yv12_fb_new->corrupted |= corrupt_tokens;
1221 :
1222 0 : if (!pbi->decoded_key_frame) {
1223 0 : if (pc->frame_type == KEY_FRAME && !yv12_fb_new->corrupted) {
1224 0 : pbi->decoded_key_frame = 1;
1225 : } else {
1226 0 : vpx_internal_error(&pbi->common.error, VPX_CODEC_CORRUPT_FRAME,
1227 : "A stream must start with a complete key frame");
1228 : }
1229 : }
1230 :
1231 : /* vpx_log("Decoder: Frame Decoded, Size Roughly:%d bytes
1232 : * \n",bc->pos+pbi->bc2.pos); */
1233 :
1234 0 : if (pc->refresh_entropy_probs == 0) {
1235 0 : memcpy(&pc->fc, &pc->lfc, sizeof(pc->fc));
1236 0 : pbi->independent_partitions = prev_independent_partitions;
1237 : }
1238 :
1239 : #ifdef PACKET_TESTING
1240 : {
1241 : FILE *f = fopen("decompressor.VP8", "ab");
1242 : unsigned int size = pbi->bc2.pos + pbi->bc.pos + 8;
1243 : fwrite((void *)&size, 4, 1, f);
1244 : fwrite((void *)pbi->Source, size, 1, f);
1245 : fclose(f);
1246 : }
1247 : #endif
1248 :
1249 0 : return 0;
1250 : }
|