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);
95 vp8_loop_filter_init(&pbi->common);
97 pbi->common.error.setjmp = 0;
99 #if CONFIG_ERROR_CONCEALMENT
100 pbi->ec_enabled = oxcf->error_concealment;
104 /* Error concealment is activated after a key frame has been
105 * decoded without errors when error concealment is enabled.
109 pbi->decoded_key_frame = 0;
111 pbi->input_partition = oxcf->input_partition;
113 /* Independent partitions is activated when a frame updates the
114 * token probability table to have equal probabilities over the
117 pbi->independent_partitions = 0;
119 return (VP8D_PTR) pbi;
123 void vp8dx_remove_decompressor(VP8D_PTR ptr)
125 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
130 #if CONFIG_MULTITHREAD
131 if (pbi->b_multithreaded_rd)
132 vp8mt_de_alloc_temp_buffers(pbi, pbi->common.mb_rows);
133 vp8_decoder_remove_threads(pbi);
135 #if CONFIG_ERROR_CONCEALMENT
136 vp8_de_alloc_overlap_lists(pbi);
138 vp8_remove_common(&pbi->common);
144 vpx_codec_err_t vp8dx_get_reference(VP8D_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
146 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
147 VP8_COMMON *cm = &pbi->common;
150 if (ref_frame_flag == VP8_LAST_FLAG)
151 ref_fb_idx = cm->lst_fb_idx;
152 else if (ref_frame_flag == VP8_GOLD_FLAG)
153 ref_fb_idx = cm->gld_fb_idx;
154 else if (ref_frame_flag == VP8_ALT_FLAG)
155 ref_fb_idx = cm->alt_fb_idx;
157 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
158 "Invalid reference frame");
159 return pbi->common.error.error_code;
162 if(cm->yv12_fb[ref_fb_idx].y_height != sd->y_height ||
163 cm->yv12_fb[ref_fb_idx].y_width != sd->y_width ||
164 cm->yv12_fb[ref_fb_idx].uv_height != sd->uv_height ||
165 cm->yv12_fb[ref_fb_idx].uv_width != sd->uv_width){
166 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
167 "Incorrect buffer dimensions");
170 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
172 return pbi->common.error.error_code;
176 vpx_codec_err_t vp8dx_set_reference(VP8D_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
178 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
179 VP8_COMMON *cm = &pbi->common;
180 int *ref_fb_ptr = NULL;
183 if (ref_frame_flag == VP8_LAST_FLAG)
184 ref_fb_ptr = &cm->lst_fb_idx;
185 else if (ref_frame_flag == VP8_GOLD_FLAG)
186 ref_fb_ptr = &cm->gld_fb_idx;
187 else if (ref_frame_flag == VP8_ALT_FLAG)
188 ref_fb_ptr = &cm->alt_fb_idx;
190 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
191 "Invalid reference frame");
192 return pbi->common.error.error_code;
195 if(cm->yv12_fb[*ref_fb_ptr].y_height != sd->y_height ||
196 cm->yv12_fb[*ref_fb_ptr].y_width != sd->y_width ||
197 cm->yv12_fb[*ref_fb_ptr].uv_height != sd->uv_height ||
198 cm->yv12_fb[*ref_fb_ptr].uv_width != sd->uv_width){
199 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
200 "Incorrect buffer dimensions");
203 /* Find an empty frame buffer. */
204 free_fb = get_free_fb(cm);
205 /* Decrease fb_idx_ref_cnt since it will be increased again in
206 * ref_cnt_fb() below. */
207 cm->fb_idx_ref_cnt[free_fb]--;
209 /* Manage the reference counters and copy image. */
210 ref_cnt_fb (cm->fb_idx_ref_cnt, ref_fb_ptr, free_fb);
211 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[*ref_fb_ptr]);
214 return pbi->common.error.error_code;
217 /*For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.*/
219 extern void vp8_push_neon(int64_t *store);
220 extern void vp8_pop_neon(int64_t *store);
223 static int get_free_fb (VP8_COMMON *cm)
226 for (i = 0; i < NUM_YV12_BUFFERS; i++)
227 if (cm->fb_idx_ref_cnt[i] == 0)
230 assert(i < NUM_YV12_BUFFERS);
231 cm->fb_idx_ref_cnt[i] = 1;
235 static void ref_cnt_fb (int *buf, int *idx, int new_idx)
245 /* If any buffer copy / swapping is signalled it should be done here. */
246 static int swap_frame_buffers (VP8_COMMON *cm)
250 /* The alternate reference frame or golden frame can be updated
251 * using the new, last, or golden/alt ref frame. If it
252 * is updated using the newly decoded frame it is a refresh.
253 * An update using the last or golden/alt ref frame is a copy.
255 if (cm->copy_buffer_to_arf)
259 if (cm->copy_buffer_to_arf == 1)
260 new_fb = cm->lst_fb_idx;
261 else if (cm->copy_buffer_to_arf == 2)
262 new_fb = cm->gld_fb_idx;
266 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb);
269 if (cm->copy_buffer_to_gf)
273 if (cm->copy_buffer_to_gf == 1)
274 new_fb = cm->lst_fb_idx;
275 else if (cm->copy_buffer_to_gf == 2)
276 new_fb = cm->alt_fb_idx;
280 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb);
283 if (cm->refresh_golden_frame)
284 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx);
286 if (cm->refresh_alt_ref_frame)
287 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx);
289 if (cm->refresh_last_frame)
291 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx);
293 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
296 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
298 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
303 int vp8dx_receive_compressed_data(VP8D_PTR ptr, unsigned long size, const unsigned char *source, int64_t time_stamp)
306 int64_t dx_store_reg[8];
308 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
309 VP8_COMMON *cm = &pbi->common;
312 /*if(pbi->ready_for_new_data == 0)
320 pbi->common.error.error_code = VPX_CODEC_OK;
322 if (pbi->input_partition && !(source == NULL && size == 0))
324 /* Store a pointer to this partition and return. We haven't
325 * received the complete frame yet, so we will wait with decoding.
327 pbi->partitions[pbi->num_partitions] = source;
328 pbi->partition_sizes[pbi->num_partitions] = size;
329 pbi->source_sz += size;
330 pbi->num_partitions++;
331 if (pbi->num_partitions > (1<<pbi->common.multi_token_partition) + 1)
332 pbi->common.multi_token_partition++;
333 if (pbi->common.multi_token_partition > EIGHT_PARTITION)
335 pbi->common.error.error_code = VPX_CODEC_UNSUP_BITSTREAM;
336 pbi->common.error.setjmp = 0;
343 if (!pbi->input_partition)
345 pbi->Source = source;
346 pbi->source_sz = size;
349 if (pbi->source_sz == 0)
351 /* This is used to signal that we are missing frames.
352 * We do not know if the missing frame(s) was supposed to update
353 * any of the reference buffers, but we act conservative and
354 * mark only the last buffer as corrupted.
356 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
358 /* If error concealment is disabled we won't signal missing frames to
363 /* Signal that we have no frame to show. */
366 pbi->num_partitions = 0;
367 if (pbi->input_partition)
368 pbi->common.multi_token_partition = 0;
370 /* Nothing more to do. */
376 #if CONFIG_RUNTIME_CPU_DETECT
377 if (cm->rtcd.flags & HAS_NEON)
380 vp8_push_neon(dx_store_reg);
384 cm->new_fb_idx = get_free_fb (cm);
386 if (setjmp(pbi->common.error.jmp))
389 #if CONFIG_RUNTIME_CPU_DETECT
390 if (cm->rtcd.flags & HAS_NEON)
393 vp8_pop_neon(dx_store_reg);
396 pbi->common.error.setjmp = 0;
398 pbi->num_partitions = 0;
399 if (pbi->input_partition)
400 pbi->common.multi_token_partition = 0;
402 /* We do not know if the missing frame(s) was supposed to update
403 * any of the reference buffers, but we act conservative and
404 * mark only the last buffer as corrupted.
406 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
408 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
409 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
413 pbi->common.error.setjmp = 1;
416 retcode = vp8_decode_frame(pbi);
421 #if CONFIG_RUNTIME_CPU_DETECT
422 if (cm->rtcd.flags & HAS_NEON)
425 vp8_pop_neon(dx_store_reg);
428 pbi->common.error.error_code = VPX_CODEC_ERROR;
429 pbi->common.error.setjmp = 0;
430 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
431 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
435 #if CONFIG_MULTITHREAD
436 if (pbi->b_multithreaded_rd && cm->multi_token_partition != ONE_PARTITION)
438 if (swap_frame_buffers (cm))
441 #if CONFIG_RUNTIME_CPU_DETECT
442 if (cm->rtcd.flags & HAS_NEON)
445 vp8_pop_neon(dx_store_reg);
448 pbi->common.error.error_code = VPX_CODEC_ERROR;
449 pbi->common.error.setjmp = 0;
455 if (swap_frame_buffers (cm))
458 #if CONFIG_RUNTIME_CPU_DETECT
459 if (cm->rtcd.flags & HAS_NEON)
462 vp8_pop_neon(dx_store_reg);
465 pbi->common.error.error_code = VPX_CODEC_ERROR;
466 pbi->common.error.setjmp = 0;
472 /* Apply the loop filter if appropriate. */
473 vp8_loop_filter_frame(cm, &pbi->mb);
475 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
479 vp8_clear_system_state();
481 #if CONFIG_ERROR_CONCEALMENT
482 /* swap the mode infos to storage for future error concealment */
483 if (pbi->ec_enabled && pbi->common.prev_mi)
485 const MODE_INFO* tmp = pbi->common.prev_mi;
487 pbi->common.prev_mi = pbi->common.mi;
488 pbi->common.mi = tmp;
490 /* Propagate the segment_ids to the next frame */
491 for (row = 0; row < pbi->common.mb_rows; ++row)
493 for (col = 0; col < pbi->common.mb_cols; ++col)
495 const int i = row*pbi->common.mode_info_stride + col;
496 pbi->common.mi[i].mbmi.segment_id =
497 pbi->common.prev_mi[i].mbmi.segment_id;
503 /*vp8_print_modes_and_motion_vectors( cm->mi, cm->mb_rows,cm->mb_cols, cm->current_video_frame);*/
506 cm->current_video_frame++;
508 pbi->ready_for_new_data = 0;
509 pbi->last_time_stamp = time_stamp;
510 pbi->num_partitions = 0;
511 if (pbi->input_partition)
512 pbi->common.multi_token_partition = 0;
518 int64_t earliest_time = pbi->dr[0].time_stamp;
519 int64_t latest_time = pbi->dr[0].time_stamp;
520 int64_t time_diff = 0;
523 pbi->dr[pbi->common.current_video_frame&0xf].size = pbi->bc.pos + pbi->bc2.pos + 4;;
524 pbi->dr[pbi->common.current_video_frame&0xf].time_stamp = time_stamp;
526 for (i = 0; i < 16; i++)
529 bytes += pbi->dr[i].size;
531 if (pbi->dr[i].time_stamp < earliest_time)
532 earliest_time = pbi->dr[i].time_stamp;
534 if (pbi->dr[i].time_stamp > latest_time)
535 latest_time = pbi->dr[i].time_stamp;
538 time_diff = latest_time - earliest_time;
542 pbi->common.bitrate = 80000.00 * bytes / time_diff ;
543 pbi->common.framerate = 160000000.00 / time_diff ;
550 #if CONFIG_RUNTIME_CPU_DETECT
551 if (cm->rtcd.flags & HAS_NEON)
554 vp8_pop_neon(dx_store_reg);
557 pbi->common.error.setjmp = 0;
560 int vp8dx_get_raw_frame(VP8D_PTR ptr, YV12_BUFFER_CONFIG *sd, int64_t *time_stamp, int64_t *time_end_stamp, vp8_ppflags_t *flags)
563 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
565 if (pbi->ready_for_new_data == 1)
568 /* ie no raw frame to show!!! */
569 if (pbi->common.show_frame == 0)
572 pbi->ready_for_new_data = 1;
573 *time_stamp = pbi->last_time_stamp;
576 sd->clrtype = pbi->common.clr_type;
578 ret = vp8_post_proc_frame(&pbi->common, sd, flags);
581 if (pbi->common.frame_to_show)
583 *sd = *pbi->common.frame_to_show;
584 sd->y_width = pbi->common.Width;
585 sd->y_height = pbi->common.Height;
586 sd->uv_height = pbi->common.Height / 2;
594 #endif /*!CONFIG_POSTPROC*/
595 vp8_clear_system_state();