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