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 "vp8/common/onyxc_int.h"
12 #if CONFIG_POSTPROC
13 #include "vp8/common/postproc.h"
14 #endif
15 #include "vp8/common/onyxd.h"
16 #include "onyxd_int.h"
17 #include "vpx_mem/vpx_mem.h"
18 #include "vp8/common/alloccommon.h"
19 #include "vp8/common/common.h"
20 #include "vp8/common/loopfilter.h"
21 #include "vp8/common/swapyv12buffer.h"
22 #include "vp8/common/threading.h"
23 #include "decoderthreading.h"
24 #include <stdio.h>
25 #include <assert.h>
26
27 #include "vp8/common/quant_common.h"
28 #include "vp8/common/reconintra.h"
29 #include "./vpx_dsp_rtcd.h"
30 #include "./vpx_scale_rtcd.h"
31 #include "vpx_scale/vpx_scale.h"
32 #include "vp8/common/systemdependent.h"
33 #include "vpx_ports/system_state.h"
34 #include "vpx_ports/vpx_once.h"
35 #include "vpx_ports/vpx_timer.h"
36 #include "detokenize.h"
37 #if CONFIG_ERROR_CONCEALMENT
38 #include "error_concealment.h"
39 #endif
40 #if VPX_ARCH_ARM
41 #include "vpx_ports/arm.h"
42 #endif
43
44 extern void vp8_init_loop_filter(VP8_COMMON *cm);
45 static int get_free_fb(VP8_COMMON *cm);
46 static void ref_cnt_fb(int *buf, int *idx, int new_idx);
47
initialize_dec(void)48 static void initialize_dec(void) {
49 static volatile int init_done = 0;
50
51 if (!init_done) {
52 vpx_dsp_rtcd();
53 vp8_init_intra_predictors();
54 init_done = 1;
55 }
56 }
57
remove_decompressor(VP8D_COMP * pbi)58 static void remove_decompressor(VP8D_COMP *pbi) {
59 #if CONFIG_ERROR_CONCEALMENT
60 vp8_de_alloc_overlap_lists(pbi);
61 #endif
62 vp8_remove_common(&pbi->common);
63 vpx_free(pbi);
64 }
65
create_decompressor(VP8D_CONFIG * oxcf)66 static struct VP8D_COMP *create_decompressor(VP8D_CONFIG *oxcf) {
67 VP8D_COMP *pbi = vpx_memalign(32, sizeof(VP8D_COMP));
68
69 if (!pbi) return NULL;
70
71 memset(pbi, 0, sizeof(VP8D_COMP));
72
73 if (setjmp(pbi->common.error.jmp)) {
74 pbi->common.error.setjmp = 0;
75 remove_decompressor(pbi);
76 return 0;
77 }
78
79 pbi->common.error.setjmp = 1;
80
81 vp8_create_common(&pbi->common);
82
83 pbi->common.current_video_frame = 0;
84 pbi->ready_for_new_data = 1;
85
86 /* vp8cx_init_de_quantizer() is first called here. Add check in
87 * frame_init_dequantizer() to avoid
88 * unnecessary calling of vp8cx_init_de_quantizer() for every frame.
89 */
90 vp8cx_init_de_quantizer(pbi);
91
92 vp8_loop_filter_init(&pbi->common);
93
94 pbi->common.error.setjmp = 0;
95
96 #if CONFIG_ERROR_CONCEALMENT
97 pbi->ec_enabled = oxcf->error_concealment;
98 pbi->overlaps = NULL;
99 #else
100 (void)oxcf;
101 pbi->ec_enabled = 0;
102 #endif
103 /* Error concealment is activated after a key frame has been
104 * decoded without errors when error concealment is enabled.
105 */
106 pbi->ec_active = 0;
107
108 pbi->decoded_key_frame = 0;
109
110 /* Independent partitions is activated when a frame updates the
111 * token probability table to have equal probabilities over the
112 * PREV_COEF context.
113 */
114 pbi->independent_partitions = 0;
115
116 vp8_setup_block_dptrs(&pbi->mb);
117
118 once(initialize_dec);
119
120 return pbi;
121 }
122
vp8dx_get_reference(VP8D_COMP * pbi,enum vpx_ref_frame_type ref_frame_flag,YV12_BUFFER_CONFIG * sd)123 vpx_codec_err_t vp8dx_get_reference(VP8D_COMP *pbi,
124 enum vpx_ref_frame_type ref_frame_flag,
125 YV12_BUFFER_CONFIG *sd) {
126 VP8_COMMON *cm = &pbi->common;
127 int ref_fb_idx;
128
129 if (ref_frame_flag == VP8_LAST_FRAME) {
130 ref_fb_idx = cm->lst_fb_idx;
131 } else if (ref_frame_flag == VP8_GOLD_FRAME) {
132 ref_fb_idx = cm->gld_fb_idx;
133 } else if (ref_frame_flag == VP8_ALTR_FRAME) {
134 ref_fb_idx = cm->alt_fb_idx;
135 } else {
136 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
137 "Invalid reference frame");
138 return pbi->common.error.error_code;
139 }
140
141 if (cm->yv12_fb[ref_fb_idx].y_height != sd->y_height ||
142 cm->yv12_fb[ref_fb_idx].y_width != sd->y_width ||
143 cm->yv12_fb[ref_fb_idx].uv_height != sd->uv_height ||
144 cm->yv12_fb[ref_fb_idx].uv_width != sd->uv_width) {
145 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
146 "Incorrect buffer dimensions");
147 } else
148 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
149
150 return pbi->common.error.error_code;
151 }
152
vp8dx_set_reference(VP8D_COMP * pbi,enum vpx_ref_frame_type ref_frame_flag,YV12_BUFFER_CONFIG * sd)153 vpx_codec_err_t vp8dx_set_reference(VP8D_COMP *pbi,
154 enum vpx_ref_frame_type ref_frame_flag,
155 YV12_BUFFER_CONFIG *sd) {
156 VP8_COMMON *cm = &pbi->common;
157 int *ref_fb_ptr = NULL;
158 int free_fb;
159
160 if (ref_frame_flag == VP8_LAST_FRAME) {
161 ref_fb_ptr = &cm->lst_fb_idx;
162 } else if (ref_frame_flag == VP8_GOLD_FRAME) {
163 ref_fb_ptr = &cm->gld_fb_idx;
164 } else if (ref_frame_flag == VP8_ALTR_FRAME) {
165 ref_fb_ptr = &cm->alt_fb_idx;
166 } else {
167 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
168 "Invalid reference frame");
169 return pbi->common.error.error_code;
170 }
171
172 if (cm->yv12_fb[*ref_fb_ptr].y_height != sd->y_height ||
173 cm->yv12_fb[*ref_fb_ptr].y_width != sd->y_width ||
174 cm->yv12_fb[*ref_fb_ptr].uv_height != sd->uv_height ||
175 cm->yv12_fb[*ref_fb_ptr].uv_width != sd->uv_width) {
176 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
177 "Incorrect buffer dimensions");
178 } else {
179 /* Find an empty frame buffer. */
180 free_fb = get_free_fb(cm);
181 /* Decrease fb_idx_ref_cnt since it will be increased again in
182 * ref_cnt_fb() below. */
183 cm->fb_idx_ref_cnt[free_fb]--;
184
185 /* Manage the reference counters and copy image. */
186 ref_cnt_fb(cm->fb_idx_ref_cnt, ref_fb_ptr, free_fb);
187 vp8_yv12_copy_frame(sd, &cm->yv12_fb[*ref_fb_ptr]);
188 }
189
190 return pbi->common.error.error_code;
191 }
192
get_free_fb(VP8_COMMON * cm)193 static int get_free_fb(VP8_COMMON *cm) {
194 int i;
195 for (i = 0; i < NUM_YV12_BUFFERS; ++i) {
196 if (cm->fb_idx_ref_cnt[i] == 0) break;
197 }
198
199 assert(i < NUM_YV12_BUFFERS);
200 cm->fb_idx_ref_cnt[i] = 1;
201 return i;
202 }
203
ref_cnt_fb(int * buf,int * idx,int new_idx)204 static void ref_cnt_fb(int *buf, int *idx, int new_idx) {
205 if (buf[*idx] > 0) buf[*idx]--;
206
207 *idx = new_idx;
208
209 buf[new_idx]++;
210 }
211
212 /* If any buffer copy / swapping is signalled it should be done here. */
swap_frame_buffers(VP8_COMMON * cm)213 static int swap_frame_buffers(VP8_COMMON *cm) {
214 int err = 0;
215
216 /* The alternate reference frame or golden frame can be updated
217 * using the new, last, or golden/alt ref frame. If it
218 * is updated using the newly decoded frame it is a refresh.
219 * An update using the last or golden/alt ref frame is a copy.
220 */
221 if (cm->copy_buffer_to_arf) {
222 int new_fb = 0;
223
224 if (cm->copy_buffer_to_arf == 1) {
225 new_fb = cm->lst_fb_idx;
226 } else if (cm->copy_buffer_to_arf == 2) {
227 new_fb = cm->gld_fb_idx;
228 } else {
229 err = -1;
230 }
231
232 ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb);
233 }
234
235 if (cm->copy_buffer_to_gf) {
236 int new_fb = 0;
237
238 if (cm->copy_buffer_to_gf == 1) {
239 new_fb = cm->lst_fb_idx;
240 } else if (cm->copy_buffer_to_gf == 2) {
241 new_fb = cm->alt_fb_idx;
242 } else {
243 err = -1;
244 }
245
246 ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb);
247 }
248
249 if (cm->refresh_golden_frame) {
250 ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx);
251 }
252
253 if (cm->refresh_alt_ref_frame) {
254 ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx);
255 }
256
257 if (cm->refresh_last_frame) {
258 ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx);
259
260 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
261 } else {
262 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
263 }
264
265 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
266
267 return err;
268 }
269
check_fragments_for_errors(VP8D_COMP * pbi)270 static int check_fragments_for_errors(VP8D_COMP *pbi) {
271 if (!pbi->ec_active && pbi->fragments.count <= 1 &&
272 pbi->fragments.sizes[0] == 0) {
273 VP8_COMMON *cm = &pbi->common;
274
275 /* If error concealment is disabled we won't signal missing frames
276 * to the decoder.
277 */
278 if (cm->fb_idx_ref_cnt[cm->lst_fb_idx] > 1) {
279 /* The last reference shares buffer with another reference
280 * buffer. Move it to its own buffer before setting it as
281 * corrupt, otherwise we will make multiple buffers corrupt.
282 */
283 const int prev_idx = cm->lst_fb_idx;
284 cm->fb_idx_ref_cnt[prev_idx]--;
285 cm->lst_fb_idx = get_free_fb(cm);
286 vp8_yv12_copy_frame(&cm->yv12_fb[prev_idx], &cm->yv12_fb[cm->lst_fb_idx]);
287 }
288 /* This is used to signal that we are missing frames.
289 * We do not know if the missing frame(s) was supposed to update
290 * any of the reference buffers, but we act conservative and
291 * mark only the last buffer as corrupted.
292 */
293 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
294
295 /* Signal that we have no frame to show. */
296 cm->show_frame = 0;
297
298 /* Nothing more to do. */
299 return 0;
300 }
301
302 return 1;
303 }
304
vp8dx_receive_compressed_data(VP8D_COMP * pbi,int64_t time_stamp)305 int vp8dx_receive_compressed_data(VP8D_COMP *pbi, int64_t time_stamp) {
306 VP8_COMMON *cm = &pbi->common;
307 int retcode = -1;
308
309 pbi->common.error.error_code = VPX_CODEC_OK;
310
311 retcode = check_fragments_for_errors(pbi);
312 if (retcode <= 0) return retcode;
313
314 cm->new_fb_idx = get_free_fb(cm);
315
316 /* setup reference frames for vp8_decode_frame */
317 pbi->dec_fb_ref[INTRA_FRAME] = &cm->yv12_fb[cm->new_fb_idx];
318 pbi->dec_fb_ref[LAST_FRAME] = &cm->yv12_fb[cm->lst_fb_idx];
319 pbi->dec_fb_ref[GOLDEN_FRAME] = &cm->yv12_fb[cm->gld_fb_idx];
320 pbi->dec_fb_ref[ALTREF_FRAME] = &cm->yv12_fb[cm->alt_fb_idx];
321
322 retcode = vp8_decode_frame(pbi);
323
324 if (retcode < 0) {
325 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0) {
326 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
327 }
328
329 pbi->common.error.error_code = VPX_CODEC_ERROR;
330 // Propagate the error info.
331 if (pbi->mb.error_info.error_code != 0) {
332 pbi->common.error.error_code = pbi->mb.error_info.error_code;
333 memcpy(pbi->common.error.detail, pbi->mb.error_info.detail,
334 sizeof(pbi->mb.error_info.detail));
335 }
336 goto decode_exit;
337 }
338
339 if (swap_frame_buffers(cm)) {
340 pbi->common.error.error_code = VPX_CODEC_ERROR;
341 goto decode_exit;
342 }
343
344 vpx_clear_system_state();
345
346 if (cm->show_frame) {
347 cm->current_video_frame++;
348 cm->show_frame_mi = cm->mi;
349 }
350
351 #if CONFIG_ERROR_CONCEALMENT
352 /* swap the mode infos to storage for future error concealment */
353 if (pbi->ec_enabled && pbi->common.prev_mi) {
354 MODE_INFO *tmp = pbi->common.prev_mi;
355 int row, col;
356 pbi->common.prev_mi = pbi->common.mi;
357 pbi->common.mi = tmp;
358
359 /* Propagate the segment_ids to the next frame */
360 for (row = 0; row < pbi->common.mb_rows; ++row) {
361 for (col = 0; col < pbi->common.mb_cols; ++col) {
362 const int i = row * pbi->common.mode_info_stride + col;
363 pbi->common.mi[i].mbmi.segment_id =
364 pbi->common.prev_mi[i].mbmi.segment_id;
365 }
366 }
367 }
368 #endif
369
370 pbi->ready_for_new_data = 0;
371 pbi->last_time_stamp = time_stamp;
372
373 decode_exit:
374 vpx_clear_system_state();
375 return retcode;
376 }
vp8dx_get_raw_frame(VP8D_COMP * pbi,YV12_BUFFER_CONFIG * sd,int64_t * time_stamp,int64_t * time_end_stamp,vp8_ppflags_t * flags)377 int vp8dx_get_raw_frame(VP8D_COMP *pbi, YV12_BUFFER_CONFIG *sd,
378 int64_t *time_stamp, int64_t *time_end_stamp,
379 vp8_ppflags_t *flags) {
380 int ret = -1;
381
382 if (pbi->ready_for_new_data == 1) return ret;
383
384 /* ie no raw frame to show!!! */
385 if (pbi->common.show_frame == 0) return ret;
386
387 pbi->ready_for_new_data = 1;
388 *time_stamp = pbi->last_time_stamp;
389 *time_end_stamp = 0;
390
391 #if CONFIG_POSTPROC
392 ret = vp8_post_proc_frame(&pbi->common, sd, flags);
393 #else
394 (void)flags;
395
396 if (pbi->common.frame_to_show) {
397 *sd = *pbi->common.frame_to_show;
398 sd->y_width = pbi->common.Width;
399 sd->y_height = pbi->common.Height;
400 sd->uv_height = pbi->common.Height / 2;
401 ret = 0;
402 } else {
403 ret = -1;
404 }
405
406 #endif /*!CONFIG_POSTPROC*/
407 vpx_clear_system_state();
408 return ret;
409 }
410
411 /* This function as written isn't decoder specific, but the encoder has
412 * much faster ways of computing this, so it's ok for it to live in a
413 * decode specific file.
414 */
vp8dx_references_buffer(VP8_COMMON * oci,int ref_frame)415 int vp8dx_references_buffer(VP8_COMMON *oci, int ref_frame) {
416 const MODE_INFO *mi = oci->mi;
417 int mb_row, mb_col;
418
419 for (mb_row = 0; mb_row < oci->mb_rows; ++mb_row) {
420 for (mb_col = 0; mb_col < oci->mb_cols; mb_col++, mi++) {
421 if (mi->mbmi.ref_frame == ref_frame) return 1;
422 }
423 mi++;
424 }
425 return 0;
426 }
427
vp8_create_decoder_instances(struct frame_buffers * fb,VP8D_CONFIG * oxcf)428 int vp8_create_decoder_instances(struct frame_buffers *fb, VP8D_CONFIG *oxcf) {
429 /* decoder instance for single thread mode */
430 fb->pbi[0] = create_decompressor(oxcf);
431 if (!fb->pbi[0]) return VPX_CODEC_ERROR;
432
433 #if CONFIG_MULTITHREAD
434 if (setjmp(fb->pbi[0]->common.error.jmp)) {
435 vp8_remove_decoder_instances(fb);
436 vp8_zero(fb->pbi);
437 vpx_clear_system_state();
438 return VPX_CODEC_ERROR;
439 }
440
441 fb->pbi[0]->common.error.setjmp = 1;
442 fb->pbi[0]->max_threads = oxcf->max_threads;
443 vp8_decoder_create_threads(fb->pbi[0]);
444 fb->pbi[0]->common.error.setjmp = 0;
445 #endif
446 return VPX_CODEC_OK;
447 }
448
vp8_remove_decoder_instances(struct frame_buffers * fb)449 int vp8_remove_decoder_instances(struct frame_buffers *fb) {
450 VP8D_COMP *pbi = fb->pbi[0];
451
452 if (!pbi) return VPX_CODEC_ERROR;
453 #if CONFIG_MULTITHREAD
454 vp8_decoder_remove_threads(pbi);
455 #endif
456
457 /* decoder instance for single thread mode */
458 remove_decompressor(pbi);
459 return VPX_CODEC_OK;
460 }
461
vp8dx_get_quantizer(const VP8D_COMP * pbi)462 int vp8dx_get_quantizer(const VP8D_COMP *pbi) {
463 return pbi->common.base_qindex;
464 }
465