2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
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.
12 #include "vp8/common/onyxc_int.h"
14 #include "vp8/common/postproc.h"
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"
27 #include "vp8/common/quant_common.h"
28 #include "vpx_scale/vpxscale.h"
29 #include "vp8/common/systemdependent.h"
30 #include "vpx_ports/vpx_timer.h"
31 #include "detokenize.h"
32 #if CONFIG_ERROR_CONCEALMENT
33 #include "error_concealment.h"
36 #include "vpx_ports/arm.h"
39 extern void vp8_init_loop_filter(VP8_COMMON *cm);
40 extern void vp8cx_init_de_quantizer(VP8D_COMP *pbi);
41 static int get_free_fb (VP8_COMMON *cm);
42 static void ref_cnt_fb (int *buf, int *idx, int new_idx);
44 struct VP8D_COMP * vp8dx_create_decompressor(VP8D_CONFIG *oxcf)
46 VP8D_COMP *pbi = vpx_memalign(32, sizeof(VP8D_COMP));
51 vpx_memset(pbi, 0, sizeof(VP8D_COMP));
53 if (setjmp(pbi->common.error.jmp))
55 pbi->common.error.setjmp = 0;
56 vp8dx_remove_decompressor(pbi);
60 pbi->common.error.setjmp = 1;
62 vp8_create_common(&pbi->common);
64 pbi->common.current_video_frame = 0;
65 pbi->ready_for_new_data = 1;
67 #if CONFIG_MULTITHREAD
68 pbi->max_threads = oxcf->max_threads;
69 vp8_decoder_create_threads(pbi);
72 /* vp8cx_init_de_quantizer() is first called here. Add check in frame_init_dequantizer() to avoid
73 * unnecessary calling of vp8cx_init_de_quantizer() for every frame.
75 vp8cx_init_de_quantizer(pbi);
77 vp8_loop_filter_init(&pbi->common);
79 pbi->common.error.setjmp = 0;
81 #if CONFIG_ERROR_CONCEALMENT
82 pbi->ec_enabled = oxcf->error_concealment;
87 /* Error concealment is activated after a key frame has been
88 * decoded without errors when error concealment is enabled.
92 pbi->decoded_key_frame = 0;
94 pbi->input_fragments = oxcf->input_fragments;
95 pbi->num_fragments = 0;
97 /* Independent partitions is activated when a frame updates the
98 * token probability table to have equal probabilities over the
101 pbi->independent_partitions = 0;
103 vp8_setup_block_dptrs(&pbi->mb);
109 void vp8dx_remove_decompressor(VP8D_COMP *pbi)
114 #if CONFIG_MULTITHREAD
115 if (pbi->b_multithreaded_rd)
116 vp8mt_de_alloc_temp_buffers(pbi, pbi->common.mb_rows);
117 vp8_decoder_remove_threads(pbi);
119 #if CONFIG_ERROR_CONCEALMENT
120 vp8_de_alloc_overlap_lists(pbi);
122 vp8_remove_common(&pbi->common);
128 vpx_codec_err_t vp8dx_get_reference(VP8D_COMP *pbi, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
130 VP8_COMMON *cm = &pbi->common;
133 if (ref_frame_flag == VP8_LAST_FLAG)
134 ref_fb_idx = cm->lst_fb_idx;
135 else if (ref_frame_flag == VP8_GOLD_FLAG)
136 ref_fb_idx = cm->gld_fb_idx;
137 else if (ref_frame_flag == VP8_ALT_FLAG)
138 ref_fb_idx = cm->alt_fb_idx;
140 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
141 "Invalid reference frame");
142 return pbi->common.error.error_code;
145 if(cm->yv12_fb[ref_fb_idx].y_height != sd->y_height ||
146 cm->yv12_fb[ref_fb_idx].y_width != sd->y_width ||
147 cm->yv12_fb[ref_fb_idx].uv_height != sd->uv_height ||
148 cm->yv12_fb[ref_fb_idx].uv_width != sd->uv_width){
149 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
150 "Incorrect buffer dimensions");
153 vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd);
155 return pbi->common.error.error_code;
159 vpx_codec_err_t vp8dx_set_reference(VP8D_COMP *pbi, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
161 VP8_COMMON *cm = &pbi->common;
162 int *ref_fb_ptr = NULL;
165 if (ref_frame_flag == VP8_LAST_FLAG)
166 ref_fb_ptr = &cm->lst_fb_idx;
167 else if (ref_frame_flag == VP8_GOLD_FLAG)
168 ref_fb_ptr = &cm->gld_fb_idx;
169 else if (ref_frame_flag == VP8_ALT_FLAG)
170 ref_fb_ptr = &cm->alt_fb_idx;
172 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
173 "Invalid reference frame");
174 return pbi->common.error.error_code;
177 if(cm->yv12_fb[*ref_fb_ptr].y_height != sd->y_height ||
178 cm->yv12_fb[*ref_fb_ptr].y_width != sd->y_width ||
179 cm->yv12_fb[*ref_fb_ptr].uv_height != sd->uv_height ||
180 cm->yv12_fb[*ref_fb_ptr].uv_width != sd->uv_width){
181 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
182 "Incorrect buffer dimensions");
185 /* Find an empty frame buffer. */
186 free_fb = get_free_fb(cm);
187 /* Decrease fb_idx_ref_cnt since it will be increased again in
188 * ref_cnt_fb() below. */
189 cm->fb_idx_ref_cnt[free_fb]--;
191 /* Manage the reference counters and copy image. */
192 ref_cnt_fb (cm->fb_idx_ref_cnt, ref_fb_ptr, free_fb);
193 vp8_yv12_copy_frame(sd, &cm->yv12_fb[*ref_fb_ptr]);
196 return pbi->common.error.error_code;
199 /*For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.*/
201 extern void vp8_push_neon(int64_t *store);
202 extern void vp8_pop_neon(int64_t *store);
205 static int get_free_fb (VP8_COMMON *cm)
208 for (i = 0; i < NUM_YV12_BUFFERS; i++)
209 if (cm->fb_idx_ref_cnt[i] == 0)
212 assert(i < NUM_YV12_BUFFERS);
213 cm->fb_idx_ref_cnt[i] = 1;
217 static void ref_cnt_fb (int *buf, int *idx, int new_idx)
227 /* If any buffer copy / swapping is signalled it should be done here. */
228 static int swap_frame_buffers (VP8_COMMON *cm)
232 /* The alternate reference frame or golden frame can be updated
233 * using the new, last, or golden/alt ref frame. If it
234 * is updated using the newly decoded frame it is a refresh.
235 * An update using the last or golden/alt ref frame is a copy.
237 if (cm->copy_buffer_to_arf)
241 if (cm->copy_buffer_to_arf == 1)
242 new_fb = cm->lst_fb_idx;
243 else if (cm->copy_buffer_to_arf == 2)
244 new_fb = cm->gld_fb_idx;
248 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb);
251 if (cm->copy_buffer_to_gf)
255 if (cm->copy_buffer_to_gf == 1)
256 new_fb = cm->lst_fb_idx;
257 else if (cm->copy_buffer_to_gf == 2)
258 new_fb = cm->alt_fb_idx;
262 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb);
265 if (cm->refresh_golden_frame)
266 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx);
268 if (cm->refresh_alt_ref_frame)
269 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx);
271 if (cm->refresh_last_frame)
273 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx);
275 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
278 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
280 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
285 int vp8dx_receive_compressed_data(VP8D_COMP *pbi, unsigned long size, const unsigned char *source, int64_t time_stamp)
288 int64_t dx_store_reg[8];
290 VP8_COMMON *cm = &pbi->common;
293 /*if(pbi->ready_for_new_data == 0)
301 pbi->common.error.error_code = VPX_CODEC_OK;
303 if (pbi->num_fragments == 0)
305 /* New frame, reset fragment pointers and sizes */
306 vpx_memset((void*)pbi->fragments, 0, sizeof(pbi->fragments));
307 vpx_memset(pbi->fragment_sizes, 0, sizeof(pbi->fragment_sizes));
309 if (pbi->input_fragments && !(source == NULL && size == 0))
311 /* Store a pointer to this fragment and return. We haven't
312 * received the complete frame yet, so we will wait with decoding.
314 assert(pbi->num_fragments < MAX_PARTITIONS);
315 pbi->fragments[pbi->num_fragments] = source;
316 pbi->fragment_sizes[pbi->num_fragments] = size;
317 pbi->num_fragments++;
318 if (pbi->num_fragments > (1 << EIGHT_PARTITION) + 1)
320 pbi->common.error.error_code = VPX_CODEC_UNSUP_BITSTREAM;
321 pbi->common.error.setjmp = 0;
322 pbi->num_fragments = 0;
328 if (!pbi->input_fragments)
330 pbi->fragments[0] = source;
331 pbi->fragment_sizes[0] = size;
332 pbi->num_fragments = 1;
334 assert(pbi->common.multi_token_partition <= EIGHT_PARTITION);
335 if (pbi->num_fragments == 0)
337 pbi->num_fragments = 1;
338 pbi->fragments[0] = NULL;
339 pbi->fragment_sizes[0] = 0;
342 if (!pbi->ec_active &&
343 pbi->num_fragments <= 1 && pbi->fragment_sizes[0] == 0)
345 /* If error concealment is disabled we won't signal missing frames
348 if (cm->fb_idx_ref_cnt[cm->lst_fb_idx] > 1)
350 /* The last reference shares buffer with another reference
351 * buffer. Move it to its own buffer before setting it as
352 * corrupt, otherwise we will make multiple buffers corrupt.
354 const int prev_idx = cm->lst_fb_idx;
355 cm->fb_idx_ref_cnt[prev_idx]--;
356 cm->lst_fb_idx = get_free_fb(cm);
357 vp8_yv12_copy_frame(&cm->yv12_fb[prev_idx],
358 &cm->yv12_fb[cm->lst_fb_idx]);
360 /* This is used to signal that we are missing frames.
361 * We do not know if the missing frame(s) was supposed to update
362 * any of the reference buffers, but we act conservative and
363 * mark only the last buffer as corrupted.
365 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
367 /* Signal that we have no frame to show. */
370 pbi->num_fragments = 0;
372 /* Nothing more to do. */
377 #if CONFIG_RUNTIME_CPU_DETECT
378 if (cm->cpu_caps & HAS_NEON)
381 vp8_push_neon(dx_store_reg);
385 cm->new_fb_idx = get_free_fb (cm);
387 if (setjmp(pbi->common.error.jmp))
390 #if CONFIG_RUNTIME_CPU_DETECT
391 if (cm->cpu_caps & HAS_NEON)
394 vp8_pop_neon(dx_store_reg);
397 pbi->common.error.setjmp = 0;
399 pbi->num_fragments = 0;
401 /* We do not know if the missing frame(s) was supposed to update
402 * any of the reference buffers, but we act conservative and
403 * mark only the last buffer as corrupted.
405 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
407 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
408 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
412 pbi->common.error.setjmp = 1;
414 retcode = vp8_decode_frame(pbi);
419 #if CONFIG_RUNTIME_CPU_DETECT
420 if (cm->cpu_caps & HAS_NEON)
423 vp8_pop_neon(dx_store_reg);
426 pbi->common.error.error_code = VPX_CODEC_ERROR;
427 pbi->common.error.setjmp = 0;
428 pbi->num_fragments = 0;
429 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
430 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
434 #if CONFIG_MULTITHREAD
435 if (pbi->b_multithreaded_rd && cm->multi_token_partition != ONE_PARTITION)
437 if (swap_frame_buffers (cm))
440 #if CONFIG_RUNTIME_CPU_DETECT
441 if (cm->cpu_caps & HAS_NEON)
444 vp8_pop_neon(dx_store_reg);
447 pbi->common.error.error_code = VPX_CODEC_ERROR;
448 pbi->common.error.setjmp = 0;
449 pbi->num_fragments = 0;
455 if (swap_frame_buffers (cm))
458 #if CONFIG_RUNTIME_CPU_DETECT
459 if (cm->cpu_caps & HAS_NEON)
462 vp8_pop_neon(dx_store_reg);
465 pbi->common.error.error_code = VPX_CODEC_ERROR;
466 pbi->common.error.setjmp = 0;
467 pbi->num_fragments = 0;
473 /* Apply the loop filter if appropriate. */
474 vp8_loop_filter_frame(cm, &pbi->mb, cm->frame_type);
476 vp8_yv12_extend_frame_borders(cm->frame_to_show);
480 vp8_clear_system_state();
482 #if CONFIG_ERROR_CONCEALMENT
483 /* swap the mode infos to storage for future error concealment */
484 if (pbi->ec_enabled && pbi->common.prev_mi)
486 MODE_INFO* tmp = pbi->common.prev_mi;
488 pbi->common.prev_mi = pbi->common.mi;
489 pbi->common.mi = tmp;
491 /* Propagate the segment_ids to the next frame */
492 for (row = 0; row < pbi->common.mb_rows; ++row)
494 for (col = 0; col < pbi->common.mb_cols; ++col)
496 const int i = row*pbi->common.mode_info_stride + col;
497 pbi->common.mi[i].mbmi.segment_id =
498 pbi->common.prev_mi[i].mbmi.segment_id;
504 /*vp8_print_modes_and_motion_vectors( cm->mi, cm->mb_rows,cm->mb_cols, cm->current_video_frame);*/
507 cm->current_video_frame++;
509 pbi->ready_for_new_data = 0;
510 pbi->last_time_stamp = time_stamp;
511 pbi->num_fragments = 0;
516 int64_t earliest_time = pbi->dr[0].time_stamp;
517 int64_t latest_time = pbi->dr[0].time_stamp;
518 int64_t time_diff = 0;
521 pbi->dr[pbi->common.current_video_frame&0xf].size = pbi->bc.pos + pbi->bc2.pos + 4;;
522 pbi->dr[pbi->common.current_video_frame&0xf].time_stamp = time_stamp;
524 for (i = 0; i < 16; i++)
527 bytes += pbi->dr[i].size;
529 if (pbi->dr[i].time_stamp < earliest_time)
530 earliest_time = pbi->dr[i].time_stamp;
532 if (pbi->dr[i].time_stamp > latest_time)
533 latest_time = pbi->dr[i].time_stamp;
536 time_diff = latest_time - earliest_time;
540 pbi->common.bitrate = 80000.00 * bytes / time_diff ;
541 pbi->common.framerate = 160000000.00 / time_diff ;
548 #if CONFIG_RUNTIME_CPU_DETECT
549 if (cm->cpu_caps & HAS_NEON)
552 vp8_pop_neon(dx_store_reg);
555 pbi->common.error.setjmp = 0;
558 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)
562 if (pbi->ready_for_new_data == 1)
565 /* ie no raw frame to show!!! */
566 if (pbi->common.show_frame == 0)
569 pbi->ready_for_new_data = 1;
570 *time_stamp = pbi->last_time_stamp;
573 sd->clrtype = pbi->common.clr_type;
575 ret = vp8_post_proc_frame(&pbi->common, sd, flags);
578 if (pbi->common.frame_to_show)
580 *sd = *pbi->common.frame_to_show;
581 sd->y_width = pbi->common.Width;
582 sd->y_height = pbi->common.Height;
583 sd->uv_height = pbi->common.Height / 2;
591 #endif /*!CONFIG_POSTPROC*/
592 vp8_clear_system_state();
597 /* This function as written isn't decoder specific, but the encoder has
598 * much faster ways of computing this, so it's ok for it to live in a
599 * decode specific file.
601 int vp8dx_references_buffer( VP8_COMMON *oci, int ref_frame )
603 const MODE_INFO *mi = oci->mi;
606 for (mb_row = 0; mb_row < oci->mb_rows; mb_row++)
608 for (mb_col = 0; mb_col < oci->mb_cols; mb_col++,mi++)
610 if( mi->mbmi.ref_frame == ref_frame)