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/loopfilter.h"
20 #include "vp8/common/swapyv12buffer.h"
21 #include "vp8/common/threading.h"
22 #include "decoderthreading.h"
23 #include <stdio.h>
24 #include <assert.h>
25
26 #include "vp8/common/quant_common.h"
27 #include "vp8/common/reconintra.h"
28 #include "./vpx_dsp_rtcd.h"
29 #include "./vpx_scale_rtcd.h"
30 #include "vpx_scale/vpx_scale.h"
31 #include "vp8/common/systemdependent.h"
32 #include "vpx_ports/system_state.h"
33 #include "vpx_ports/vpx_once.h"
34 #include "vpx_ports/vpx_timer.h"
35 #include "detokenize.h"
36 #if CONFIG_ERROR_CONCEALMENT
37 #include "error_concealment.h"
38 #endif
39 #if ARCH_ARM
40 #include "vpx_ports/arm.h"
41 #endif
42
43 extern void vp8_init_loop_filter(VP8_COMMON *cm);
44 extern void vp8cx_init_de_quantizer(VP8D_COMP *pbi);
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,size_t size,const uint8_t * source,int64_t time_stamp)305 int vp8dx_receive_compressed_data(VP8D_COMP *pbi, size_t size,
306 const uint8_t *source, int64_t time_stamp) {
307 VP8_COMMON *cm = &pbi->common;
308 int retcode = -1;
309 (void)size;
310 (void)source;
311
312 pbi->common.error.error_code = VPX_CODEC_OK;
313
314 retcode = check_fragments_for_errors(pbi);
315 if (retcode <= 0) return retcode;
316
317 cm->new_fb_idx = get_free_fb(cm);
318
319 /* setup reference frames for vp8_decode_frame */
320 pbi->dec_fb_ref[INTRA_FRAME] = &cm->yv12_fb[cm->new_fb_idx];
321 pbi->dec_fb_ref[LAST_FRAME] = &cm->yv12_fb[cm->lst_fb_idx];
322 pbi->dec_fb_ref[GOLDEN_FRAME] = &cm->yv12_fb[cm->gld_fb_idx];
323 pbi->dec_fb_ref[ALTREF_FRAME] = &cm->yv12_fb[cm->alt_fb_idx];
324
325 if (setjmp(pbi->common.error.jmp)) {
326 /* We do not know if the missing frame(s) was supposed to update
327 * any of the reference buffers, but we act conservative and
328 * mark only the last buffer as corrupted.
329 */
330 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
331
332 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0) {
333 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
334 }
335 goto decode_exit;
336 }
337
338 pbi->common.error.setjmp = 1;
339
340 retcode = vp8_decode_frame(pbi);
341
342 if (retcode < 0) {
343 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0) {
344 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
345 }
346
347 pbi->common.error.error_code = VPX_CODEC_ERROR;
348 goto decode_exit;
349 }
350
351 if (swap_frame_buffers(cm)) {
352 pbi->common.error.error_code = VPX_CODEC_ERROR;
353 goto decode_exit;
354 }
355
356 vpx_clear_system_state();
357
358 if (cm->show_frame) {
359 cm->current_video_frame++;
360 cm->show_frame_mi = cm->mi;
361 }
362
363 #if CONFIG_ERROR_CONCEALMENT
364 /* swap the mode infos to storage for future error concealment */
365 if (pbi->ec_enabled && pbi->common.prev_mi) {
366 MODE_INFO *tmp = pbi->common.prev_mi;
367 int row, col;
368 pbi->common.prev_mi = pbi->common.mi;
369 pbi->common.mi = tmp;
370
371 /* Propagate the segment_ids to the next frame */
372 for (row = 0; row < pbi->common.mb_rows; ++row) {
373 for (col = 0; col < pbi->common.mb_cols; ++col) {
374 const int i = row * pbi->common.mode_info_stride + col;
375 pbi->common.mi[i].mbmi.segment_id =
376 pbi->common.prev_mi[i].mbmi.segment_id;
377 }
378 }
379 }
380 #endif
381
382 pbi->ready_for_new_data = 0;
383 pbi->last_time_stamp = time_stamp;
384
385 decode_exit:
386 pbi->common.error.setjmp = 0;
387 vpx_clear_system_state();
388 return retcode;
389 }
vp8dx_get_raw_frame(VP8D_COMP * pbi,YV12_BUFFER_CONFIG * sd,int64_t * time_stamp,int64_t * time_end_stamp,vp8_ppflags_t * flags)390 int vp8dx_get_raw_frame(VP8D_COMP *pbi, YV12_BUFFER_CONFIG *sd,
391 int64_t *time_stamp, int64_t *time_end_stamp,
392 vp8_ppflags_t *flags) {
393 int ret = -1;
394
395 if (pbi->ready_for_new_data == 1) return ret;
396
397 /* ie no raw frame to show!!! */
398 if (pbi->common.show_frame == 0) return ret;
399
400 pbi->ready_for_new_data = 1;
401 *time_stamp = pbi->last_time_stamp;
402 *time_end_stamp = 0;
403
404 #if CONFIG_POSTPROC
405 ret = vp8_post_proc_frame(&pbi->common, sd, flags);
406 #else
407 (void)flags;
408
409 if (pbi->common.frame_to_show) {
410 *sd = *pbi->common.frame_to_show;
411 sd->y_width = pbi->common.Width;
412 sd->y_height = pbi->common.Height;
413 sd->uv_height = pbi->common.Height / 2;
414 ret = 0;
415 } else {
416 ret = -1;
417 }
418
419 #endif /*!CONFIG_POSTPROC*/
420 vpx_clear_system_state();
421 return ret;
422 }
423
424 /* This function as written isn't decoder specific, but the encoder has
425 * much faster ways of computing this, so it's ok for it to live in a
426 * decode specific file.
427 */
vp8dx_references_buffer(VP8_COMMON * oci,int ref_frame)428 int vp8dx_references_buffer(VP8_COMMON *oci, int ref_frame) {
429 const MODE_INFO *mi = oci->mi;
430 int mb_row, mb_col;
431
432 for (mb_row = 0; mb_row < oci->mb_rows; ++mb_row) {
433 for (mb_col = 0; mb_col < oci->mb_cols; mb_col++, mi++) {
434 if (mi->mbmi.ref_frame == ref_frame) return 1;
435 }
436 mi++;
437 }
438 return 0;
439 }
440
vp8_create_decoder_instances(struct frame_buffers * fb,VP8D_CONFIG * oxcf)441 int vp8_create_decoder_instances(struct frame_buffers *fb, VP8D_CONFIG *oxcf) {
442 /* decoder instance for single thread mode */
443 fb->pbi[0] = create_decompressor(oxcf);
444 if (!fb->pbi[0]) return VPX_CODEC_ERROR;
445
446 #if CONFIG_MULTITHREAD
447 if (setjmp(fb->pbi[0]->common.error.jmp)) {
448 vp8_remove_decoder_instances(fb);
449 memset(fb->pbi, 0, sizeof(fb->pbi));
450 vpx_clear_system_state();
451 return VPX_CODEC_ERROR;
452 }
453
454 fb->pbi[0]->common.error.setjmp = 1;
455 fb->pbi[0]->max_threads = oxcf->max_threads;
456 vp8_decoder_create_threads(fb->pbi[0]);
457 fb->pbi[0]->common.error.setjmp = 0;
458 #endif
459 return VPX_CODEC_OK;
460 }
461
vp8_remove_decoder_instances(struct frame_buffers * fb)462 int vp8_remove_decoder_instances(struct frame_buffers *fb) {
463 VP8D_COMP *pbi = fb->pbi[0];
464
465 if (!pbi) return VPX_CODEC_ERROR;
466 #if CONFIG_MULTITHREAD
467 vp8_decoder_remove_threads(pbi);
468 #endif
469
470 /* decoder instance for single thread mode */
471 remove_decompressor(pbi);
472 return VPX_CODEC_OK;
473 }
474
vp8dx_get_quantizer(const VP8D_COMP * cpi)475 int vp8dx_get_quantizer(const VP8D_COMP *cpi) {
476 return cpi->common.base_qindex;
477 }
478