michael@0: /* michael@0: * Copyright (c) 2010 The WebM project authors. All Rights Reserved. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license michael@0: * that can be found in the LICENSE file in the root of the source michael@0: * tree. An additional intellectual property rights grant can be found michael@0: * in the file PATENTS. All contributing project authors may michael@0: * be found in the AUTHORS file in the root of the source tree. michael@0: */ michael@0: michael@0: michael@0: #include "vp8/common/onyxc_int.h" michael@0: #if CONFIG_POSTPROC michael@0: #include "vp8/common/postproc.h" michael@0: #endif michael@0: #include "vp8/common/onyxd.h" michael@0: #include "onyxd_int.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vp8/common/alloccommon.h" michael@0: #include "vp8/common/loopfilter.h" michael@0: #include "vp8/common/swapyv12buffer.h" michael@0: #include "vp8/common/threading.h" michael@0: #include "decoderthreading.h" michael@0: #include michael@0: #include michael@0: michael@0: #include "vp8/common/quant_common.h" michael@0: #include "./vpx_scale_rtcd.h" michael@0: #include "vpx_scale/vpx_scale.h" michael@0: #include "vp8/common/systemdependent.h" michael@0: #include "vpx_ports/vpx_timer.h" michael@0: #include "detokenize.h" michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: #include "error_concealment.h" michael@0: #endif michael@0: #if ARCH_ARM michael@0: #include "vpx_ports/arm.h" michael@0: #endif michael@0: michael@0: extern void vp8_init_loop_filter(VP8_COMMON *cm); michael@0: extern void vp8cx_init_de_quantizer(VP8D_COMP *pbi); michael@0: static int get_free_fb (VP8_COMMON *cm); michael@0: static void ref_cnt_fb (int *buf, int *idx, int new_idx); michael@0: michael@0: static void remove_decompressor(VP8D_COMP *pbi) michael@0: { michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: vp8_de_alloc_overlap_lists(pbi); michael@0: #endif michael@0: vp8_remove_common(&pbi->common); michael@0: vpx_free(pbi); michael@0: } michael@0: michael@0: static struct VP8D_COMP * create_decompressor(VP8D_CONFIG *oxcf) michael@0: { michael@0: VP8D_COMP *pbi = vpx_memalign(32, sizeof(VP8D_COMP)); michael@0: michael@0: if (!pbi) michael@0: return NULL; michael@0: michael@0: vpx_memset(pbi, 0, sizeof(VP8D_COMP)); michael@0: michael@0: if (setjmp(pbi->common.error.jmp)) michael@0: { michael@0: pbi->common.error.setjmp = 0; michael@0: remove_decompressor(pbi); michael@0: return 0; michael@0: } michael@0: michael@0: pbi->common.error.setjmp = 1; michael@0: michael@0: vp8_create_common(&pbi->common); michael@0: michael@0: pbi->common.current_video_frame = 0; michael@0: pbi->ready_for_new_data = 1; michael@0: michael@0: /* vp8cx_init_de_quantizer() is first called here. Add check in frame_init_dequantizer() to avoid michael@0: * unnecessary calling of vp8cx_init_de_quantizer() for every frame. michael@0: */ michael@0: vp8cx_init_de_quantizer(pbi); michael@0: michael@0: vp8_loop_filter_init(&pbi->common); michael@0: michael@0: pbi->common.error.setjmp = 0; michael@0: michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: pbi->ec_enabled = oxcf->error_concealment; michael@0: pbi->overlaps = NULL; michael@0: #else michael@0: pbi->ec_enabled = 0; michael@0: #endif michael@0: /* Error concealment is activated after a key frame has been michael@0: * decoded without errors when error concealment is enabled. michael@0: */ michael@0: pbi->ec_active = 0; michael@0: michael@0: pbi->decoded_key_frame = 0; michael@0: michael@0: /* Independent partitions is activated when a frame updates the michael@0: * token probability table to have equal probabilities over the michael@0: * PREV_COEF context. michael@0: */ michael@0: pbi->independent_partitions = 0; michael@0: michael@0: vp8_setup_block_dptrs(&pbi->mb); michael@0: michael@0: return pbi; michael@0: } michael@0: michael@0: vpx_codec_err_t vp8dx_get_reference(VP8D_COMP *pbi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd) michael@0: { michael@0: VP8_COMMON *cm = &pbi->common; michael@0: int ref_fb_idx; michael@0: michael@0: if (ref_frame_flag == VP8_LAST_FRAME) michael@0: ref_fb_idx = cm->lst_fb_idx; michael@0: else if (ref_frame_flag == VP8_GOLD_FRAME) michael@0: ref_fb_idx = cm->gld_fb_idx; michael@0: else if (ref_frame_flag == VP8_ALTR_FRAME) michael@0: ref_fb_idx = cm->alt_fb_idx; michael@0: else{ michael@0: vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR, michael@0: "Invalid reference frame"); michael@0: return pbi->common.error.error_code; michael@0: } michael@0: michael@0: if(cm->yv12_fb[ref_fb_idx].y_height != sd->y_height || michael@0: cm->yv12_fb[ref_fb_idx].y_width != sd->y_width || michael@0: cm->yv12_fb[ref_fb_idx].uv_height != sd->uv_height || michael@0: cm->yv12_fb[ref_fb_idx].uv_width != sd->uv_width){ michael@0: vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR, michael@0: "Incorrect buffer dimensions"); michael@0: } michael@0: else michael@0: vp8_yv12_copy_frame(&cm->yv12_fb[ref_fb_idx], sd); michael@0: michael@0: return pbi->common.error.error_code; michael@0: } michael@0: michael@0: michael@0: vpx_codec_err_t vp8dx_set_reference(VP8D_COMP *pbi, enum vpx_ref_frame_type ref_frame_flag, YV12_BUFFER_CONFIG *sd) michael@0: { michael@0: VP8_COMMON *cm = &pbi->common; michael@0: int *ref_fb_ptr = NULL; michael@0: int free_fb; michael@0: michael@0: if (ref_frame_flag == VP8_LAST_FRAME) michael@0: ref_fb_ptr = &cm->lst_fb_idx; michael@0: else if (ref_frame_flag == VP8_GOLD_FRAME) michael@0: ref_fb_ptr = &cm->gld_fb_idx; michael@0: else if (ref_frame_flag == VP8_ALTR_FRAME) michael@0: ref_fb_ptr = &cm->alt_fb_idx; michael@0: else{ michael@0: vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR, michael@0: "Invalid reference frame"); michael@0: return pbi->common.error.error_code; michael@0: } michael@0: michael@0: if(cm->yv12_fb[*ref_fb_ptr].y_height != sd->y_height || michael@0: cm->yv12_fb[*ref_fb_ptr].y_width != sd->y_width || michael@0: cm->yv12_fb[*ref_fb_ptr].uv_height != sd->uv_height || michael@0: cm->yv12_fb[*ref_fb_ptr].uv_width != sd->uv_width){ michael@0: vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR, michael@0: "Incorrect buffer dimensions"); michael@0: } michael@0: else{ michael@0: /* Find an empty frame buffer. */ michael@0: free_fb = get_free_fb(cm); michael@0: /* Decrease fb_idx_ref_cnt since it will be increased again in michael@0: * ref_cnt_fb() below. */ michael@0: cm->fb_idx_ref_cnt[free_fb]--; michael@0: michael@0: /* Manage the reference counters and copy image. */ michael@0: ref_cnt_fb (cm->fb_idx_ref_cnt, ref_fb_ptr, free_fb); michael@0: vp8_yv12_copy_frame(sd, &cm->yv12_fb[*ref_fb_ptr]); michael@0: } michael@0: michael@0: return pbi->common.error.error_code; michael@0: } michael@0: michael@0: /*For ARM NEON, d8-d15 are callee-saved registers, and need to be saved by us.*/ michael@0: #if HAVE_NEON michael@0: extern void vp8_push_neon(int64_t *store); michael@0: extern void vp8_pop_neon(int64_t *store); michael@0: #endif michael@0: michael@0: static int get_free_fb (VP8_COMMON *cm) michael@0: { michael@0: int i; michael@0: for (i = 0; i < NUM_YV12_BUFFERS; i++) michael@0: if (cm->fb_idx_ref_cnt[i] == 0) michael@0: break; michael@0: michael@0: assert(i < NUM_YV12_BUFFERS); michael@0: cm->fb_idx_ref_cnt[i] = 1; michael@0: return i; michael@0: } michael@0: michael@0: static void ref_cnt_fb (int *buf, int *idx, int new_idx) michael@0: { michael@0: if (buf[*idx] > 0) michael@0: buf[*idx]--; michael@0: michael@0: *idx = new_idx; michael@0: michael@0: buf[new_idx]++; michael@0: } michael@0: michael@0: /* If any buffer copy / swapping is signalled it should be done here. */ michael@0: static int swap_frame_buffers (VP8_COMMON *cm) michael@0: { michael@0: int err = 0; michael@0: michael@0: /* The alternate reference frame or golden frame can be updated michael@0: * using the new, last, or golden/alt ref frame. If it michael@0: * is updated using the newly decoded frame it is a refresh. michael@0: * An update using the last or golden/alt ref frame is a copy. michael@0: */ michael@0: if (cm->copy_buffer_to_arf) michael@0: { michael@0: int new_fb = 0; michael@0: michael@0: if (cm->copy_buffer_to_arf == 1) michael@0: new_fb = cm->lst_fb_idx; michael@0: else if (cm->copy_buffer_to_arf == 2) michael@0: new_fb = cm->gld_fb_idx; michael@0: else michael@0: err = -1; michael@0: michael@0: ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, new_fb); michael@0: } michael@0: michael@0: if (cm->copy_buffer_to_gf) michael@0: { michael@0: int new_fb = 0; michael@0: michael@0: if (cm->copy_buffer_to_gf == 1) michael@0: new_fb = cm->lst_fb_idx; michael@0: else if (cm->copy_buffer_to_gf == 2) michael@0: new_fb = cm->alt_fb_idx; michael@0: else michael@0: err = -1; michael@0: michael@0: ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, new_fb); michael@0: } michael@0: michael@0: if (cm->refresh_golden_frame) michael@0: ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->gld_fb_idx, cm->new_fb_idx); michael@0: michael@0: if (cm->refresh_alt_ref_frame) michael@0: ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->alt_fb_idx, cm->new_fb_idx); michael@0: michael@0: if (cm->refresh_last_frame) michael@0: { michael@0: ref_cnt_fb (cm->fb_idx_ref_cnt, &cm->lst_fb_idx, cm->new_fb_idx); michael@0: michael@0: cm->frame_to_show = &cm->yv12_fb[cm->lst_fb_idx]; michael@0: } michael@0: else michael@0: cm->frame_to_show = &cm->yv12_fb[cm->new_fb_idx]; michael@0: michael@0: cm->fb_idx_ref_cnt[cm->new_fb_idx]--; michael@0: michael@0: return err; michael@0: } michael@0: michael@0: int check_fragments_for_errors(VP8D_COMP *pbi) michael@0: { michael@0: if (!pbi->ec_active && michael@0: pbi->fragments.count <= 1 && pbi->fragments.sizes[0] == 0) michael@0: { michael@0: VP8_COMMON *cm = &pbi->common; michael@0: michael@0: /* If error concealment is disabled we won't signal missing frames michael@0: * to the decoder. michael@0: */ michael@0: if (cm->fb_idx_ref_cnt[cm->lst_fb_idx] > 1) michael@0: { michael@0: /* The last reference shares buffer with another reference michael@0: * buffer. Move it to its own buffer before setting it as michael@0: * corrupt, otherwise we will make multiple buffers corrupt. michael@0: */ michael@0: const int prev_idx = cm->lst_fb_idx; michael@0: cm->fb_idx_ref_cnt[prev_idx]--; michael@0: cm->lst_fb_idx = get_free_fb(cm); michael@0: vp8_yv12_copy_frame(&cm->yv12_fb[prev_idx], michael@0: &cm->yv12_fb[cm->lst_fb_idx]); michael@0: } michael@0: /* This is used to signal that we are missing frames. michael@0: * We do not know if the missing frame(s) was supposed to update michael@0: * any of the reference buffers, but we act conservative and michael@0: * mark only the last buffer as corrupted. michael@0: */ michael@0: cm->yv12_fb[cm->lst_fb_idx].corrupted = 1; michael@0: michael@0: /* Signal that we have no frame to show. */ michael@0: cm->show_frame = 0; michael@0: michael@0: /* Nothing more to do. */ michael@0: return 0; michael@0: } michael@0: michael@0: return 1; michael@0: } michael@0: michael@0: int vp8dx_receive_compressed_data(VP8D_COMP *pbi, size_t size, michael@0: const uint8_t *source, michael@0: int64_t time_stamp) michael@0: { michael@0: #if HAVE_NEON michael@0: int64_t dx_store_reg[8]; michael@0: #endif michael@0: VP8_COMMON *cm = &pbi->common; michael@0: int retcode = -1; michael@0: michael@0: pbi->common.error.error_code = VPX_CODEC_OK; michael@0: michael@0: retcode = check_fragments_for_errors(pbi); michael@0: if(retcode <= 0) michael@0: return retcode; michael@0: michael@0: #if HAVE_NEON michael@0: #if CONFIG_RUNTIME_CPU_DETECT michael@0: if (cm->cpu_caps & HAS_NEON) michael@0: #endif michael@0: { michael@0: vp8_push_neon(dx_store_reg); michael@0: } michael@0: #endif michael@0: michael@0: cm->new_fb_idx = get_free_fb (cm); michael@0: michael@0: /* setup reference frames for vp8_decode_frame */ michael@0: pbi->dec_fb_ref[INTRA_FRAME] = &cm->yv12_fb[cm->new_fb_idx]; michael@0: pbi->dec_fb_ref[LAST_FRAME] = &cm->yv12_fb[cm->lst_fb_idx]; michael@0: pbi->dec_fb_ref[GOLDEN_FRAME] = &cm->yv12_fb[cm->gld_fb_idx]; michael@0: pbi->dec_fb_ref[ALTREF_FRAME] = &cm->yv12_fb[cm->alt_fb_idx]; michael@0: michael@0: if (setjmp(pbi->common.error.jmp)) michael@0: { michael@0: /* We do not know if the missing frame(s) was supposed to update michael@0: * any of the reference buffers, but we act conservative and michael@0: * mark only the last buffer as corrupted. michael@0: */ michael@0: cm->yv12_fb[cm->lst_fb_idx].corrupted = 1; michael@0: michael@0: if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0) michael@0: cm->fb_idx_ref_cnt[cm->new_fb_idx]--; michael@0: michael@0: goto decode_exit; michael@0: } michael@0: michael@0: pbi->common.error.setjmp = 1; michael@0: michael@0: retcode = vp8_decode_frame(pbi); michael@0: michael@0: if (retcode < 0) michael@0: { michael@0: if (cm->fb_idx_ref_cnt[cm->new_fb_idx] > 0) michael@0: cm->fb_idx_ref_cnt[cm->new_fb_idx]--; michael@0: michael@0: pbi->common.error.error_code = VPX_CODEC_ERROR; michael@0: goto decode_exit; michael@0: } michael@0: michael@0: if (swap_frame_buffers (cm)) michael@0: { michael@0: pbi->common.error.error_code = VPX_CODEC_ERROR; michael@0: goto decode_exit; michael@0: } michael@0: michael@0: vp8_clear_system_state(); michael@0: michael@0: if (cm->show_frame) michael@0: { michael@0: cm->current_video_frame++; michael@0: cm->show_frame_mi = cm->mi; michael@0: } michael@0: michael@0: #if CONFIG_ERROR_CONCEALMENT michael@0: /* swap the mode infos to storage for future error concealment */ michael@0: if (pbi->ec_enabled && pbi->common.prev_mi) michael@0: { michael@0: MODE_INFO* tmp = pbi->common.prev_mi; michael@0: int row, col; michael@0: pbi->common.prev_mi = pbi->common.mi; michael@0: pbi->common.mi = tmp; michael@0: michael@0: /* Propagate the segment_ids to the next frame */ michael@0: for (row = 0; row < pbi->common.mb_rows; ++row) michael@0: { michael@0: for (col = 0; col < pbi->common.mb_cols; ++col) michael@0: { michael@0: const int i = row*pbi->common.mode_info_stride + col; michael@0: pbi->common.mi[i].mbmi.segment_id = michael@0: pbi->common.prev_mi[i].mbmi.segment_id; michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: pbi->ready_for_new_data = 0; michael@0: pbi->last_time_stamp = time_stamp; michael@0: michael@0: decode_exit: michael@0: #if HAVE_NEON michael@0: #if CONFIG_RUNTIME_CPU_DETECT michael@0: if (cm->cpu_caps & HAS_NEON) michael@0: #endif michael@0: { michael@0: vp8_pop_neon(dx_store_reg); michael@0: } michael@0: #endif michael@0: michael@0: pbi->common.error.setjmp = 0; michael@0: return retcode; michael@0: } michael@0: 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) michael@0: { michael@0: int ret = -1; michael@0: michael@0: if (pbi->ready_for_new_data == 1) michael@0: return ret; michael@0: michael@0: /* ie no raw frame to show!!! */ michael@0: if (pbi->common.show_frame == 0) michael@0: return ret; michael@0: michael@0: pbi->ready_for_new_data = 1; michael@0: *time_stamp = pbi->last_time_stamp; michael@0: *time_end_stamp = 0; michael@0: michael@0: #if CONFIG_POSTPROC michael@0: ret = vp8_post_proc_frame(&pbi->common, sd, flags); michael@0: #else michael@0: michael@0: if (pbi->common.frame_to_show) michael@0: { michael@0: *sd = *pbi->common.frame_to_show; michael@0: sd->y_width = pbi->common.Width; michael@0: sd->y_height = pbi->common.Height; michael@0: sd->uv_height = pbi->common.Height / 2; michael@0: ret = 0; michael@0: } michael@0: else michael@0: { michael@0: ret = -1; michael@0: } michael@0: michael@0: #endif /*!CONFIG_POSTPROC*/ michael@0: vp8_clear_system_state(); michael@0: return ret; michael@0: } michael@0: michael@0: michael@0: /* This function as written isn't decoder specific, but the encoder has michael@0: * much faster ways of computing this, so it's ok for it to live in a michael@0: * decode specific file. michael@0: */ michael@0: int vp8dx_references_buffer( VP8_COMMON *oci, int ref_frame ) michael@0: { michael@0: const MODE_INFO *mi = oci->mi; michael@0: int mb_row, mb_col; michael@0: michael@0: for (mb_row = 0; mb_row < oci->mb_rows; mb_row++) michael@0: { michael@0: for (mb_col = 0; mb_col < oci->mb_cols; mb_col++,mi++) michael@0: { michael@0: if( mi->mbmi.ref_frame == ref_frame) michael@0: return 1; michael@0: } michael@0: mi++; michael@0: } michael@0: return 0; michael@0: michael@0: } michael@0: michael@0: int vp8_create_decoder_instances(struct frame_buffers *fb, VP8D_CONFIG *oxcf) michael@0: { michael@0: if(!fb->use_frame_threads) michael@0: { michael@0: /* decoder instance for single thread mode */ michael@0: fb->pbi[0] = create_decompressor(oxcf); michael@0: if(!fb->pbi[0]) michael@0: return VPX_CODEC_ERROR; michael@0: michael@0: #if CONFIG_MULTITHREAD michael@0: /* enable row-based threading only when use_frame_threads michael@0: * is disabled */ michael@0: fb->pbi[0]->max_threads = oxcf->max_threads; michael@0: vp8_decoder_create_threads(fb->pbi[0]); michael@0: #endif michael@0: } michael@0: else michael@0: { michael@0: /* TODO : create frame threads and decoder instances for each michael@0: * thread here */ michael@0: } michael@0: michael@0: return VPX_CODEC_OK; michael@0: } michael@0: michael@0: int vp8_remove_decoder_instances(struct frame_buffers *fb) michael@0: { michael@0: if(!fb->use_frame_threads) michael@0: { michael@0: VP8D_COMP *pbi = fb->pbi[0]; michael@0: michael@0: if (!pbi) michael@0: return VPX_CODEC_ERROR; michael@0: #if CONFIG_MULTITHREAD michael@0: if (pbi->b_multithreaded_rd) michael@0: vp8mt_de_alloc_temp_buffers(pbi, pbi->common.mb_rows); michael@0: vp8_decoder_remove_threads(pbi); michael@0: #endif michael@0: michael@0: /* decoder instance for single thread mode */ michael@0: remove_decompressor(pbi); michael@0: } michael@0: else michael@0: { michael@0: /* TODO : remove frame threads and decoder instances for each michael@0: * thread here */ michael@0: } michael@0: michael@0: return VPX_CODEC_OK; michael@0: }