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 "vpx_scale/yv12extend.h"
21 #include "vp8/common/loopfilter.h"
22 #include "vp8/common/swapyv12buffer.h"
23 #include "vp8/common/g_common.h"
24 #include "vp8/common/threading.h"
25 #include "decoderthreading.h"
29 #include "vp8/common/quant_common.h"
30 #include "vpx_scale/vpxscale.h"
31 #include "vp8/common/systemdependent.h"
32 #include "vpx_ports/vpx_timer.h"
33 #include "detokenize.h"
34 #if CONFIG_ERROR_CONCEALMENT
35 #include "error_concealment.h"
38 #include "vpx_ports/arm.h"
41 extern void vp8_init_loop_filter(VP8_COMMON *cm);
42 extern void vp8cx_init_de_quantizer(VP8D_COMP *pbi);
43 static int get_free_fb (VP8_COMMON *cm);
44 static void ref_cnt_fb (int *buf, int *idx, int new_idx);
47 void vp8dx_initialize()
49 static int init_done = 0;
53 vp8_initialize_common();
54 vp8_scale_machine_specific_config();
60 VP8D_PTR vp8dx_create_decompressor(VP8D_CONFIG *oxcf)
62 VP8D_COMP *pbi = vpx_memalign(32, sizeof(VP8D_COMP));
67 vpx_memset(pbi, 0, sizeof(VP8D_COMP));
69 if (setjmp(pbi->common.error.jmp))
71 pbi->common.error.setjmp = 0;
72 vp8dx_remove_decompressor(pbi);
76 pbi->common.error.setjmp = 1;
79 vp8_create_common(&pbi->common);
80 vp8_dmachine_specific_config(pbi);
82 pbi->common.current_video_frame = 0;
83 pbi->ready_for_new_data = 1;
85 #if CONFIG_MULTITHREAD
86 pbi->max_threads = oxcf->max_threads;
87 vp8_decoder_create_threads(pbi);
90 /* vp8cx_init_de_quantizer() is first called here. Add check in frame_init_dequantizer() to avoid
91 * unnecessary calling of vp8cx_init_de_quantizer() for every frame.
93 vp8cx_init_de_quantizer(pbi);
96 VP8_COMMON *cm = &pbi->common;
98 vp8_init_loop_filter(cm);
99 cm->last_frame_type = KEY_FRAME;
100 cm->last_filter_type = cm->filter_type;
101 cm->last_sharpness_level = cm->sharpness_level;
104 pbi->common.error.setjmp = 0;
106 #if CONFIG_ERROR_CONCEALMENT
107 pbi->ec_enabled = oxcf->error_concealment;
112 return (VP8D_PTR) pbi;
116 void vp8dx_remove_decompressor(VP8D_PTR ptr)
118 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
123 #if CONFIG_MULTITHREAD
124 if (pbi->b_multithreaded_rd)
125 vp8mt_de_alloc_temp_buffers(pbi, pbi->common.mb_rows);
126 vp8_decoder_remove_threads(pbi);
128 #if CONFIG_ERROR_CONCEALMENT
129 vp8_de_alloc_overlap_lists(pbi);
131 vp8_remove_common(&pbi->common);
136 int vp8dx_get_reference(VP8D_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
138 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
139 VP8_COMMON *cm = &pbi->common;
142 if (ref_frame_flag == VP8_LAST_FLAG)
143 ref_fb_idx = cm->lst_fb_idx;
144 else if (ref_frame_flag == VP8_GOLD_FLAG)
145 ref_fb_idx = cm->gld_fb_idx;
146 else if (ref_frame_flag == VP8_ALT_FLAG)
147 ref_fb_idx = cm->alt_fb_idx;
151 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
157 int vp8dx_set_reference(VP8D_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
159 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
160 VP8_COMMON *cm = &pbi->common;
161 int *ref_fb_ptr = NULL;
164 if (ref_frame_flag == VP8_LAST_FLAG)
165 ref_fb_ptr = &cm->lst_fb_idx;
166 else if (ref_frame_flag == VP8_GOLD_FLAG)
167 ref_fb_ptr = &cm->gld_fb_idx;
168 else if (ref_frame_flag == VP8_ALT_FLAG)
169 ref_fb_ptr = &cm->alt_fb_idx;
173 /* Find an empty frame buffer. */
174 free_fb = get_free_fb(cm);
175 /* Decrease fb_idx_ref_cnt since it will be increased again in
176 * ref_cnt_fb() below. */
177 cm->fb_idx_ref_cnt[free_fb]--;
179 /* Manage the reference counters and copy image. */
180 ref_cnt_fb (cm->fb_idx_ref_cnt, ref_fb_ptr, free_fb);
181 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[*ref_fb_ptr]);
186 /*For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.*/
188 extern void vp8_push_neon(INT64 *store);
189 extern void vp8_pop_neon(INT64 *store);
192 static int get_free_fb (VP8_COMMON *cm)
195 for (i = 0; i < NUM_YV12_BUFFERS; i++)
196 if (cm->fb_idx_ref_cnt[i] == 0)
199 assert(i < NUM_YV12_BUFFERS);
200 cm->fb_idx_ref_cnt[i] = 1;
204 static void ref_cnt_fb (int *buf, int *idx, int new_idx)
214 /* If any buffer copy / swapping is signalled it should be done here. */
215 static int swap_frame_buffers (VP8_COMMON *cm)
219 /* The alternate reference frame or golden frame can be updated
220 * using the new, last, or golden/alt ref frame. If it
221 * is updated using the newly decoded frame it is a refresh.
222 * An update using the last or golden/alt ref frame is a copy.
224 if (cm->copy_buffer_to_arf)
228 if (cm->copy_buffer_to_arf == 1)
229 new_fb = cm->lst_fb_idx;
230 else if (cm->copy_buffer_to_arf == 2)
231 new_fb = cm->gld_fb_idx;
235 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb);
238 if (cm->copy_buffer_to_gf)
242 if (cm->copy_buffer_to_gf == 1)
243 new_fb = cm->lst_fb_idx;
244 else if (cm->copy_buffer_to_gf == 2)
245 new_fb = cm->alt_fb_idx;
249 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb);
252 if (cm->refresh_golden_frame)
253 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx);
255 if (cm->refresh_alt_ref_frame)
256 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx);
258 if (cm->refresh_last_frame)
260 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx);
262 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
265 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
267 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
272 int vp8dx_receive_compressed_data(VP8D_PTR ptr, unsigned long size, const unsigned char *source, INT64 time_stamp)
275 INT64 dx_store_reg[8];
277 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
278 VP8_COMMON *cm = &pbi->common;
281 /*if(pbi->ready_for_new_data == 0)
289 pbi->common.error.error_code = VPX_CODEC_OK;
293 /* This is used to signal that we are missing frames.
294 * We do not know if the missing frame(s) was supposed to update
295 * any of the reference buffers, but we act conservative and
296 * mark only the last buffer as corrupted.
298 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
300 /* If error concealment is disabled we won't signal missing frames to
303 if (!pbi->ec_enabled)
305 /* Signal that we have no frame to show. */
308 /* Nothing more to do. */
315 #if CONFIG_RUNTIME_CPU_DETECT
316 if (cm->rtcd.flags & HAS_NEON)
319 vp8_push_neon(dx_store_reg);
323 cm->new_fb_idx = get_free_fb (cm);
325 if (setjmp(pbi->common.error.jmp))
328 #if CONFIG_RUNTIME_CPU_DETECT
329 if (cm->rtcd.flags & HAS_NEON)
332 vp8_pop_neon(dx_store_reg);
335 pbi->common.error.setjmp = 0;
337 /* We do not know if the missing frame(s) was supposed to update
338 * any of the reference buffers, but we act conservative and
339 * mark only the last buffer as corrupted.
341 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
343 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
344 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
348 pbi->common.error.setjmp = 1;
350 /*cm->current_video_frame++;*/
351 pbi->Source = source;
352 pbi->source_sz = size;
354 retcode = vp8_decode_frame(pbi);
359 #if CONFIG_RUNTIME_CPU_DETECT
360 if (cm->rtcd.flags & HAS_NEON)
363 vp8_pop_neon(dx_store_reg);
366 pbi->common.error.error_code = VPX_CODEC_ERROR;
367 pbi->common.error.setjmp = 0;
368 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
369 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
373 #if CONFIG_MULTITHREAD
374 if (pbi->b_multithreaded_rd && cm->multi_token_partition != ONE_PARTITION)
376 if (swap_frame_buffers (cm))
379 #if CONFIG_RUNTIME_CPU_DETECT
380 if (cm->rtcd.flags & HAS_NEON)
383 vp8_pop_neon(dx_store_reg);
386 pbi->common.error.error_code = VPX_CODEC_ERROR;
387 pbi->common.error.setjmp = 0;
393 if (swap_frame_buffers (cm))
396 #if CONFIG_RUNTIME_CPU_DETECT
397 if (cm->rtcd.flags & HAS_NEON)
400 vp8_pop_neon(dx_store_reg);
403 pbi->common.error.error_code = VPX_CODEC_ERROR;
404 pbi->common.error.setjmp = 0;
408 if(pbi->common.filter_level)
410 /* Apply the loop filter if appropriate. */
411 vp8_loop_filter_frame(cm, &pbi->mb, cm->filter_level);
413 cm->last_frame_type = cm->frame_type;
414 cm->last_filter_type = cm->filter_type;
415 cm->last_sharpness_level = cm->sharpness_level;
417 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
421 vp8_clear_system_state();
423 #if CONFIG_ERROR_CONCEALMENT
424 /* swap the mode infos to storage for future error concealment */
425 if (pbi->ec_enabled && pbi->common.prev_mi)
427 const MODE_INFO* tmp = pbi->common.prev_mi;
429 pbi->common.prev_mi = pbi->common.mi;
430 pbi->common.mi = tmp;
432 /* Propagate the segment_ids to the next frame */
433 for (row = 0; row < pbi->common.mb_rows; ++row)
435 for (col = 0; col < pbi->common.mb_cols; ++col)
437 const int i = row*pbi->common.mode_info_stride + col;
438 pbi->common.mi[i].mbmi.segment_id =
439 pbi->common.prev_mi[i].mbmi.segment_id;
445 /*vp8_print_modes_and_motion_vectors( cm->mi, cm->mb_rows,cm->mb_cols, cm->current_video_frame);*/
448 cm->current_video_frame++;
450 pbi->ready_for_new_data = 0;
451 pbi->last_time_stamp = time_stamp;
456 INT64 earliest_time = pbi->dr[0].time_stamp;
457 INT64 latest_time = pbi->dr[0].time_stamp;
461 pbi->dr[pbi->common.current_video_frame&0xf].size = pbi->bc.pos + pbi->bc2.pos + 4;;
462 pbi->dr[pbi->common.current_video_frame&0xf].time_stamp = time_stamp;
464 for (i = 0; i < 16; i++)
467 bytes += pbi->dr[i].size;
469 if (pbi->dr[i].time_stamp < earliest_time)
470 earliest_time = pbi->dr[i].time_stamp;
472 if (pbi->dr[i].time_stamp > latest_time)
473 latest_time = pbi->dr[i].time_stamp;
476 time_diff = latest_time - earliest_time;
480 pbi->common.bitrate = 80000.00 * bytes / time_diff ;
481 pbi->common.framerate = 160000000.00 / time_diff ;
488 #if CONFIG_RUNTIME_CPU_DETECT
489 if (cm->rtcd.flags & HAS_NEON)
492 vp8_pop_neon(dx_store_reg);
495 pbi->common.error.setjmp = 0;
498 int vp8dx_get_raw_frame(VP8D_PTR ptr, YV12_BUFFER_CONFIG *sd, INT64 *time_stamp, INT64 *time_end_stamp, vp8_ppflags_t *flags)
501 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
503 if (pbi->ready_for_new_data == 1)
506 /* ie no raw frame to show!!! */
507 if (pbi->common.show_frame == 0)
510 pbi->ready_for_new_data = 1;
511 *time_stamp = pbi->last_time_stamp;
514 sd->clrtype = pbi->common.clr_type;
516 ret = vp8_post_proc_frame(&pbi->common, sd, flags);
519 if (pbi->common.frame_to_show)
521 *sd = *pbi->common.frame_to_show;
522 sd->y_width = pbi->common.Width;
523 sd->y_height = pbi->common.Height;
524 sd->uv_height = pbi->common.Height / 2;
532 #endif /*!CONFIG_POSTPROC*/
533 vp8_clear_system_state();