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 "onyxc_int.h"
17 #include "onyxd_int.h"
18 #include "vpx_mem/vpx_mem.h"
19 #include "alloccommon.h"
20 #include "vpx_scale/yv12extend.h"
21 #include "loopfilter.h"
22 #include "swapyv12buffer.h"
24 #include "threading.h"
25 #include "decoderthreading.h"
28 #include "quant_common.h"
29 #include "vpx_scale/vpxscale.h"
30 #include "systemdependent.h"
31 #include "vpx_ports/vpx_timer.h"
32 #include "detokenize.h"
34 extern void vp8_init_loop_filter(VP8_COMMON *cm);
35 extern void vp8cx_init_de_quantizer(VP8D_COMP *pbi);
38 void vp8_recon_write_yuv_frame(unsigned char *name, YV12_BUFFER_CONFIG *s)
40 FILE *yuv_file = fopen((char *)name, "ab");
41 unsigned char *src = s->y_buffer;
46 fwrite(src, s->y_width, 1, yuv_file);
56 fwrite(src, s->uv_width, 1, yuv_file);
66 fwrite(src, s->uv_width, 1, yuv_file);
75 void vp8dx_initialize()
77 static int init_done = 0;
81 vp8_initialize_common();
82 vp8_scale_machine_specific_config();
88 VP8D_PTR vp8dx_create_decompressor(VP8D_CONFIG *oxcf)
90 VP8D_COMP *pbi = vpx_memalign(32, sizeof(VP8D_COMP));
95 vpx_memset(pbi, 0, sizeof(VP8D_COMP));
97 if (setjmp(pbi->common.error.jmp))
99 pbi->common.error.setjmp = 0;
100 vp8dx_remove_decompressor(pbi);
104 pbi->common.error.setjmp = 1;
107 vp8_create_common(&pbi->common);
108 vp8_dmachine_specific_config(pbi);
110 pbi->common.current_video_frame = 0;
111 pbi->ready_for_new_data = 1;
113 pbi->CPUFreq = 0; //vp8_get_processor_freq();
114 pbi->max_threads = oxcf->max_threads;
115 vp8_decoder_create_threads(pbi);
117 //vp8cx_init_de_quantizer() is first called here. Add check in frame_init_dequantizer() to avoid
118 // unnecessary calling of vp8cx_init_de_quantizer() for every frame.
119 vp8cx_init_de_quantizer(pbi);
122 VP8_COMMON *cm = &pbi->common;
124 vp8_init_loop_filter(cm);
125 cm->last_frame_type = KEY_FRAME;
126 cm->last_filter_type = cm->filter_type;
127 cm->last_sharpness_level = cm->sharpness_level;
130 #if CONFIG_ARM_ASM_DETOK
131 vp8_init_detokenizer(pbi);
133 pbi->common.error.setjmp = 0;
134 return (VP8D_PTR) pbi;
138 void vp8dx_remove_decompressor(VP8D_PTR ptr)
140 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
145 #if CONFIG_MULTITHREAD
146 if (pbi->b_multithreaded_rd)
147 vp8mt_de_alloc_temp_buffers(pbi, pbi->common.mb_rows);
149 vp8_decoder_remove_threads(pbi);
150 vp8_remove_common(&pbi->common);
155 void vp8dx_set_setting(VP8D_PTR comp, VP8D_SETTING oxst, int x)
157 VP8D_COMP *pbi = (VP8D_COMP *) comp;
169 int vp8dx_get_setting(VP8D_PTR comp, VP8D_SETTING oxst)
171 VP8D_COMP *pbi = (VP8D_COMP *) comp;
184 int vp8dx_get_reference(VP8D_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
186 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
187 VP8_COMMON *cm = &pbi->common;
190 if (ref_frame_flag == VP8_LAST_FLAG)
191 ref_fb_idx = cm->lst_fb_idx;
192 else if (ref_frame_flag == VP8_GOLD_FLAG)
193 ref_fb_idx = cm->gld_fb_idx;
194 else if (ref_frame_flag == VP8_ALT_FLAG)
195 ref_fb_idx = cm->alt_fb_idx;
199 vp8_yv12_copy_frame_ptr(&cm->yv12_fb[ref_fb_idx], sd);
203 int vp8dx_set_reference(VP8D_PTR ptr, VP8_REFFRAME ref_frame_flag, YV12_BUFFER_CONFIG *sd)
205 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
206 VP8_COMMON *cm = &pbi->common;
209 if (ref_frame_flag == VP8_LAST_FLAG)
210 ref_fb_idx = cm->lst_fb_idx;
211 else if (ref_frame_flag == VP8_GOLD_FLAG)
212 ref_fb_idx = cm->gld_fb_idx;
213 else if (ref_frame_flag == VP8_ALT_FLAG)
214 ref_fb_idx = cm->alt_fb_idx;
218 vp8_yv12_copy_frame_ptr(sd, &cm->yv12_fb[ref_fb_idx]);
223 //For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.
225 extern void vp8_push_neon(INT64 *store);
226 extern void vp8_pop_neon(INT64 *store);
227 static INT64 dx_store_reg[8];
230 static int get_free_fb (VP8_COMMON *cm)
233 for (i = 0; i < NUM_YV12_BUFFERS; i++)
234 if (cm->fb_idx_ref_cnt[i] == 0)
237 cm->fb_idx_ref_cnt[i] = 1;
241 static void ref_cnt_fb (int *buf, int *idx, int new_idx)
251 // If any buffer copy / swapping is signalled it should be done here.
252 static int swap_frame_buffers (VP8_COMMON *cm)
254 int fb_to_update_with, err = 0;
256 if (cm->refresh_last_frame)
257 fb_to_update_with = cm->lst_fb_idx;
259 fb_to_update_with = cm->new_fb_idx;
261 // The alternate reference frame or golden frame can be updated
262 // using the new, last, or golden/alt ref frame. If it
263 // is updated using the newly decoded frame it is a refresh.
264 // An update using the last or golden/alt ref frame is a copy.
265 if (cm->copy_buffer_to_arf)
269 if (cm->copy_buffer_to_arf == 1)
270 new_fb = fb_to_update_with;
271 else if (cm->copy_buffer_to_arf == 2)
272 new_fb = cm->gld_fb_idx;
276 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb);
279 if (cm->copy_buffer_to_gf)
283 if (cm->copy_buffer_to_gf == 1)
284 new_fb = fb_to_update_with;
285 else if (cm->copy_buffer_to_gf == 2)
286 new_fb = cm->alt_fb_idx;
290 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb);
293 if (cm->refresh_golden_frame)
294 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx);
296 if (cm->refresh_alt_ref_frame)
297 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx);
299 if (cm->refresh_last_frame)
301 ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx);
303 cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx];
306 cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx];
308 cm->fb_idx_ref_cnt[cm->new_fb_idx]--;
313 int vp8dx_receive_compressed_data(VP8D_PTR ptr, unsigned long size, const unsigned char *source, INT64 time_stamp)
315 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
316 VP8_COMMON *cm = &pbi->common;
318 struct vpx_usec_timer timer;
320 // if(pbi->ready_for_new_data == 0)
328 pbi->common.error.error_code = VPX_CODEC_OK;
330 if (setjmp(pbi->common.error.jmp))
332 pbi->common.error.setjmp = 0;
336 pbi->common.error.setjmp = 1;
339 vp8_push_neon(dx_store_reg);
342 vpx_usec_timer_start(&timer);
344 //cm->current_video_frame++;
345 pbi->Source = source;
346 pbi->source_sz = size;
348 cm->new_fb_idx = get_free_fb (cm);
350 retcode = vp8_decode_frame(pbi);
355 vp8_pop_neon(dx_store_reg);
357 pbi->common.error.error_code = VPX_CODEC_ERROR;
358 pbi->common.error.setjmp = 0;
362 if (pbi->b_multithreaded_rd && cm->multi_token_partition != ONE_PARTITION)
364 if (swap_frame_buffers (cm))
366 pbi->common.error.error_code = VPX_CODEC_ERROR;
367 pbi->common.error.setjmp = 0;
372 if (swap_frame_buffers (cm))
374 pbi->common.error.error_code = VPX_CODEC_ERROR;
375 pbi->common.error.setjmp = 0;
379 if(pbi->common.filter_level)
381 struct vpx_usec_timer lpftimer;
382 vpx_usec_timer_start(&lpftimer);
383 // Apply the loop filter if appropriate.
385 vp8_loop_filter_frame(cm, &pbi->mb, cm->filter_level);
387 vpx_usec_timer_mark(&lpftimer);
388 pbi->time_loop_filtering += vpx_usec_timer_elapsed(&lpftimer);
390 cm->last_frame_type = cm->frame_type;
391 cm->last_filter_type = cm->filter_type;
392 cm->last_sharpness_level = cm->sharpness_level;
394 vp8_yv12_extend_frame_borders_ptr(cm->frame_to_show);
399 //vp8_recon_write_yuv_frame("recon.yuv", cm->frame_to_show);
400 if (cm->current_video_frame <= 5)
401 write_dx_frame_to_file(cm->frame_to_show, cm->current_video_frame);
404 vp8_clear_system_state();
406 vpx_usec_timer_mark(&timer);
407 pbi->decode_microseconds = vpx_usec_timer_elapsed(&timer);
409 pbi->time_decoding += pbi->decode_microseconds;
411 // vp8_print_modes_and_motion_vectors( cm->mi, cm->mb_rows,cm->mb_cols, cm->current_video_frame);
414 cm->current_video_frame++;
416 pbi->ready_for_new_data = 0;
417 pbi->last_time_stamp = time_stamp;
422 INT64 earliest_time = pbi->dr[0].time_stamp;
423 INT64 latest_time = pbi->dr[0].time_stamp;
427 pbi->dr[pbi->common.current_video_frame&0xf].size = pbi->bc.pos + pbi->bc2.pos + 4;;
428 pbi->dr[pbi->common.current_video_frame&0xf].time_stamp = time_stamp;
430 for (i = 0; i < 16; i++)
433 bytes += pbi->dr[i].size;
435 if (pbi->dr[i].time_stamp < earliest_time)
436 earliest_time = pbi->dr[i].time_stamp;
438 if (pbi->dr[i].time_stamp > latest_time)
439 latest_time = pbi->dr[i].time_stamp;
442 time_diff = latest_time - earliest_time;
446 pbi->common.bitrate = 80000.00 * bytes / time_diff ;
447 pbi->common.framerate = 160000000.00 / time_diff ;
454 vp8_pop_neon(dx_store_reg);
456 pbi->common.error.setjmp = 0;
459 int vp8dx_get_raw_frame(VP8D_PTR ptr, YV12_BUFFER_CONFIG *sd, INT64 *time_stamp, INT64 *time_end_stamp, int deblock_level, int noise_level, int flags)
462 VP8D_COMP *pbi = (VP8D_COMP *) ptr;
464 if (pbi->ready_for_new_data == 1)
467 // ie no raw frame to show!!!
468 if (pbi->common.show_frame == 0)
471 pbi->ready_for_new_data = 1;
472 *time_stamp = pbi->last_time_stamp;
475 sd->clrtype = pbi->common.clr_type;
477 ret = vp8_post_proc_frame(&pbi->common, sd, deblock_level, noise_level, flags);
480 if (pbi->common.frame_to_show)
482 *sd = *pbi->common.frame_to_show;
483 sd->y_width = pbi->common.Width;
484 sd->y_height = pbi->common.Height;
485 sd->uv_height = pbi->common.Height / 2;
493 #endif //!CONFIG_POSTPROC
494 vp8_clear_system_state();