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;
105 pbi->input_partition = oxcf->input_partition;
107 return (VP8D_PTR) pbi;
111 void vp8dx_remove_decompressor(VP8D_PTR ptr)
113 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
118 #if CONFIG_MULTITHREAD
119 if (pbi->b_multithreaded_rd)
120 vp8mt_de_alloc_temp_buffers(pbi, pbi->common.mb_rows);
121 vp8_decoder_remove_threads(pbi);
123 #if CONFIG_ERROR_CONCEALMENT
124 vp8_de_alloc_overlap_lists(pbi);
126 vp8_remove_common(&pbi->common);
132 vpx_codec_err_t vp8dx_get_reference(VP8D_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
134 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
135 VP8_COMMON *cm = &pbi->common;
138 if (ref_frame_flag == VP8_LAST_FLAG)
139 ref_fb_idx = cm->lst_fb_idx;
140 else if (ref_frame_flag == VP8_GOLD_FLAG)
141 ref_fb_idx = cm->gld_fb_idx;
142 else if (ref_frame_flag == VP8_ALT_FLAG)
143 ref_fb_idx = cm->alt_fb_idx;
145 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
146 "Invalid reference frame");
147 return pbi->common.error.error_code;
150 if(cm->yv12_fb[ref_fb_idx].y_height != sd->y_height ||
151 cm->yv12_fb[ref_fb_idx].y_width != sd->y_width ||
152 cm->yv12_fb[ref_fb_idx].uv_height != sd->uv_height ||
153 cm->yv12_fb[ref_fb_idx].uv_width != sd->uv_width){
154 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
155 "Incorrect buffer dimensions");
158 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
160 return pbi->common.error.error_code;
164 vpx_codec_err_t vp8dx_set_reference(VP8D_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
166 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
167 VP8_COMMON *cm = &pbi->common;
168 int *ref_fb_ptr = NULL;
171 if (ref_frame_flag == VP8_LAST_FLAG)
172 ref_fb_ptr = &cm->lst_fb_idx;
173 else if (ref_frame_flag == VP8_GOLD_FLAG)
174 ref_fb_ptr = &cm->gld_fb_idx;
175 else if (ref_frame_flag == VP8_ALT_FLAG)
176 ref_fb_ptr = &cm->alt_fb_idx;
178 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
179 "Invalid reference frame");
180 return pbi->common.error.error_code;
183 if(cm->yv12_fb[*ref_fb_ptr].y_height != sd->y_height ||
184 cm->yv12_fb[*ref_fb_ptr].y_width != sd->y_width ||
185 cm->yv12_fb[*ref_fb_ptr].uv_height != sd->uv_height ||
186 cm->yv12_fb[*ref_fb_ptr].uv_width != sd->uv_width){
187 vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR,
188 "Incorrect buffer dimensions");
191 /* Find an empty frame buffer. */
192 free_fb = get_free_fb(cm);
193 /* Decrease fb_idx_ref_cnt since it will be increased again in
194 * ref_cnt_fb() below. */
195 cm->fb_idx_ref_cnt[free_fb]--;
197 /* Manage the reference counters and copy image. */
198 ref_cnt_fb (cm->fb_idx_ref_cnt, ref_fb_ptr, free_fb);
199 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[*ref_fb_ptr]);
202 return pbi->common.error.error_code;
205 /*For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.*/
207 extern void vp8_push_neon(INT64 *store);
208 extern void vp8_pop_neon(INT64 *store);
211 static int get_free_fb (VP8_COMMON *cm)
214 for (i = 0; i < NUM_YV12_BUFFERS; i++)
215 if (cm->fb_idx_ref_cnt[i] == 0)
218 assert(i < NUM_YV12_BUFFERS);
219 cm->fb_idx_ref_cnt[i] = 1;
223 static void ref_cnt_fb (int *buf, int *idx, int new_idx)
233 /* If any buffer copy / swapping is signalled it should be done here. */
234 static int swap_frame_buffers (VP8_COMMON *cm)
238 /* The alternate reference frame or golden frame can be updated
239 * using the new, last, or golden/alt ref frame. If it
240 * is updated using the newly decoded frame it is a refresh.
241 * An update using the last or golden/alt ref frame is a copy.
243 if (cm->copy_buffer_to_arf)
247 if (cm->copy_buffer_to_arf == 1)
248 new_fb = cm->lst_fb_idx;
249 else if (cm->copy_buffer_to_arf == 2)
250 new_fb = cm->gld_fb_idx;
254 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb);
257 if (cm->copy_buffer_to_gf)
261 if (cm->copy_buffer_to_gf == 1)
262 new_fb = cm->lst_fb_idx;
263 else if (cm->copy_buffer_to_gf == 2)
264 new_fb = cm->alt_fb_idx;
268 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb);
271 if (cm->refresh_golden_frame)
272 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx);
274 if (cm->refresh_alt_ref_frame)
275 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx);
277 if (cm->refresh_last_frame)
279 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx);
281 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
284 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
286 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
291 int vp8dx_receive_compressed_data(VP8D_PTR ptr, unsigned long size, const unsigned char *source, INT64 time_stamp)
294 INT64 dx_store_reg[8];
296 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
297 VP8_COMMON *cm = &pbi->common;
300 /*if(pbi->ready_for_new_data == 0)
308 pbi->common.error.error_code = VPX_CODEC_OK;
310 if (pbi->input_partition && !(source == NULL && size == 0))
312 /* Store a pointer to this partition and return. We haven't
313 * received the complete frame yet, so we will wait with decoding.
315 pbi->partitions[pbi->num_partitions] = source;
316 pbi->partition_sizes[pbi->num_partitions] = size;
317 pbi->source_sz += size;
318 pbi->num_partitions++;
319 if (pbi->num_partitions > (1<<pbi->common.multi_token_partition) + 1)
320 pbi->common.multi_token_partition++;
321 if (pbi->common.multi_token_partition > EIGHT_PARTITION)
323 pbi->common.error.error_code = VPX_CODEC_UNSUP_BITSTREAM;
324 pbi->common.error.setjmp = 0;
331 if (!pbi->input_partition)
333 pbi->Source = source;
334 pbi->source_sz = size;
337 if (pbi->source_sz == 0)
339 /* This is used to signal that we are missing frames.
340 * We do not know if the missing frame(s) was supposed to update
341 * any of the reference buffers, but we act conservative and
342 * mark only the last buffer as corrupted.
344 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
346 /* If error concealment is disabled we won't signal missing frames to
349 if (!pbi->ec_enabled)
351 /* Signal that we have no frame to show. */
354 /* Nothing more to do. */
360 #if CONFIG_RUNTIME_CPU_DETECT
361 if (cm->rtcd.flags & HAS_NEON)
364 vp8_push_neon(dx_store_reg);
368 cm->new_fb_idx = get_free_fb (cm);
370 if (setjmp(pbi->common.error.jmp))
373 #if CONFIG_RUNTIME_CPU_DETECT
374 if (cm->rtcd.flags & HAS_NEON)
377 vp8_pop_neon(dx_store_reg);
380 pbi->common.error.setjmp = 0;
382 /* We do not know if the missing frame(s) was supposed to update
383 * any of the reference buffers, but we act conservative and
384 * mark only the last buffer as corrupted.
386 cm->yv12_fb[cm->lst_fb_idx].corrupted = 1;
388 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
389 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
393 pbi->common.error.setjmp = 1;
396 retcode = vp8_decode_frame(pbi);
401 #if CONFIG_RUNTIME_CPU_DETECT
402 if (cm->rtcd.flags & HAS_NEON)
405 vp8_pop_neon(dx_store_reg);
408 pbi->common.error.error_code = VPX_CODEC_ERROR;
409 pbi->common.error.setjmp = 0;
410 if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0)
411 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
415 #if CONFIG_MULTITHREAD
416 if (pbi->b_multithreaded_rd && cm->multi_token_partition != ONE_PARTITION)
418 if (swap_frame_buffers (cm))
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;
435 if (swap_frame_buffers (cm))
438 #if CONFIG_RUNTIME_CPU_DETECT
439 if (cm->rtcd.flags & HAS_NEON)
442 vp8_pop_neon(dx_store_reg);
445 pbi->common.error.error_code = VPX_CODEC_ERROR;
446 pbi->common.error.setjmp = 0;
452 /* Apply the loop filter if appropriate. */
453 vp8_loop_filter_frame(cm, &pbi->mb);
455 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
459 vp8_clear_system_state();
461 #if CONFIG_ERROR_CONCEALMENT
462 /* swap the mode infos to storage for future error concealment */
463 if (pbi->ec_enabled && pbi->common.prev_mi)
465 const MODE_INFO* tmp = pbi->common.prev_mi;
467 pbi->common.prev_mi = pbi->common.mi;
468 pbi->common.mi = tmp;
470 /* Propagate the segment_ids to the next frame */
471 for (row = 0; row < pbi->common.mb_rows; ++row)
473 for (col = 0; col < pbi->common.mb_cols; ++col)
475 const int i = row*pbi->common.mode_info_stride + col;
476 pbi->common.mi[i].mbmi.segment_id =
477 pbi->common.prev_mi[i].mbmi.segment_id;
483 /*vp8_print_modes_and_motion_vectors( cm->mi, cm->mb_rows,cm->mb_cols, cm->current_video_frame);*/
486 cm->current_video_frame++;
488 pbi->ready_for_new_data = 0;
489 pbi->last_time_stamp = time_stamp;
490 pbi->num_partitions = 0;
491 if (pbi->input_partition)
492 pbi->common.multi_token_partition = 0;
498 INT64 earliest_time = pbi->dr[0].time_stamp;
499 INT64 latest_time = pbi->dr[0].time_stamp;
503 pbi->dr[pbi->common.current_video_frame&0xf].size = pbi->bc.pos + pbi->bc2.pos + 4;;
504 pbi->dr[pbi->common.current_video_frame&0xf].time_stamp = time_stamp;
506 for (i = 0; i < 16; i++)
509 bytes += pbi->dr[i].size;
511 if (pbi->dr[i].time_stamp < earliest_time)
512 earliest_time = pbi->dr[i].time_stamp;
514 if (pbi->dr[i].time_stamp > latest_time)
515 latest_time = pbi->dr[i].time_stamp;
518 time_diff = latest_time - earliest_time;
522 pbi->common.bitrate = 80000.00 * bytes / time_diff ;
523 pbi->common.framerate = 160000000.00 / time_diff ;
530 #if CONFIG_RUNTIME_CPU_DETECT
531 if (cm->rtcd.flags & HAS_NEON)
534 vp8_pop_neon(dx_store_reg);
537 pbi->common.error.setjmp = 0;
540 int vp8dx_get_raw_frame(VP8D_PTR ptr, YV12_BUFFER_CONFIG *sd, INT64 *time_stamp, INT64 *time_end_stamp, vp8_ppflags_t *flags)
543 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
545 if (pbi->ready_for_new_data == 1)
548 /* ie no raw frame to show!!! */
549 if (pbi->common.show_frame == 0)
552 pbi->ready_for_new_data = 1;
553 *time_stamp = pbi->last_time_stamp;
556 sd->clrtype = pbi->common.clr_type;
558 ret = vp8_post_proc_frame(&pbi->common, sd, flags);
561 if (pbi->common.frame_to_show)
563 *sd = *pbi->common.frame_to_show;
564 sd->y_width = pbi->common.Width;
565 sd->y_height = pbi->common.Height;
566 sd->uv_height = pbi->common.Height / 2;
574 #endif /*!CONFIG_POSTPROC*/
575 vp8_clear_system_state();