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: #include michael@0: #include michael@0: #include michael@0: michael@0: #include "vp9/common/vp9_onyxc_int.h" michael@0: #if CONFIG_VP9_POSTPROC michael@0: #include "vp9/common/vp9_postproc.h" michael@0: #endif michael@0: #include "vp9/decoder/vp9_onyxd.h" michael@0: #include "vp9/decoder/vp9_onyxd_int.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: #include "vp9/common/vp9_alloccommon.h" michael@0: #include "vp9/common/vp9_loopfilter.h" michael@0: #include "vp9/common/vp9_quant_common.h" michael@0: #include "vpx_scale/vpx_scale.h" michael@0: #include "vp9/common/vp9_systemdependent.h" michael@0: #include "vpx_ports/vpx_timer.h" michael@0: #include "vp9/decoder/vp9_decodframe.h" michael@0: #include "vp9/decoder/vp9_detokenize.h" michael@0: #include "./vpx_scale_rtcd.h" michael@0: michael@0: #define WRITE_RECON_BUFFER 0 michael@0: #if WRITE_RECON_BUFFER == 1 michael@0: static void recon_write_yuv_frame(const char *name, michael@0: const YV12_BUFFER_CONFIG *s, michael@0: int w, int _h) { michael@0: FILE *yuv_file = fopen(name, "ab"); michael@0: const uint8_t *src = s->y_buffer; michael@0: int h = _h; michael@0: michael@0: do { michael@0: fwrite(src, w, 1, yuv_file); michael@0: src += s->y_stride; michael@0: } while (--h); michael@0: michael@0: src = s->u_buffer; michael@0: h = (_h + 1) >> 1; michael@0: w = (w + 1) >> 1; michael@0: michael@0: do { michael@0: fwrite(src, w, 1, yuv_file); michael@0: src += s->uv_stride; michael@0: } while (--h); michael@0: michael@0: src = s->v_buffer; michael@0: h = (_h + 1) >> 1; michael@0: michael@0: do { michael@0: fwrite(src, w, 1, yuv_file); michael@0: src += s->uv_stride; michael@0: } while (--h); michael@0: michael@0: fclose(yuv_file); michael@0: } michael@0: #endif michael@0: #if WRITE_RECON_BUFFER == 2 michael@0: void write_dx_frame_to_file(YV12_BUFFER_CONFIG *frame, int this_frame) { michael@0: // write the frame michael@0: FILE *yframe; michael@0: int i; michael@0: char filename[255]; michael@0: michael@0: snprintf(filename, sizeof(filename)-1, "dx\\y%04d.raw", this_frame); michael@0: yframe = fopen(filename, "wb"); michael@0: michael@0: for (i = 0; i < frame->y_height; i++) michael@0: fwrite(frame->y_buffer + i * frame->y_stride, michael@0: frame->y_width, 1, yframe); michael@0: michael@0: fclose(yframe); michael@0: snprintf(filename, sizeof(filename)-1, "dx\\u%04d.raw", this_frame); michael@0: yframe = fopen(filename, "wb"); michael@0: michael@0: for (i = 0; i < frame->uv_height; i++) michael@0: fwrite(frame->u_buffer + i * frame->uv_stride, michael@0: frame->uv_width, 1, yframe); michael@0: michael@0: fclose(yframe); michael@0: snprintf(filename, sizeof(filename)-1, "dx\\v%04d.raw", this_frame); michael@0: yframe = fopen(filename, "wb"); michael@0: michael@0: for (i = 0; i < frame->uv_height; i++) michael@0: fwrite(frame->v_buffer + i * frame->uv_stride, michael@0: frame->uv_width, 1, yframe); michael@0: michael@0: fclose(yframe); michael@0: } michael@0: #endif michael@0: michael@0: void vp9_initialize_dec() { michael@0: static int init_done = 0; michael@0: michael@0: if (!init_done) { michael@0: vp9_initialize_common(); michael@0: vp9_init_quant_tables(); michael@0: init_done = 1; michael@0: } michael@0: } michael@0: michael@0: static void init_macroblockd(VP9D_COMP *const pbi) { michael@0: MACROBLOCKD *xd = &pbi->mb; michael@0: struct macroblockd_plane *const pd = xd->plane; michael@0: int i; michael@0: michael@0: for (i = 0; i < MAX_MB_PLANE; ++i) { michael@0: pd[i].qcoeff = pbi->qcoeff[i]; michael@0: pd[i].dqcoeff = pbi->dqcoeff[i]; michael@0: pd[i].eobs = pbi->eobs[i]; michael@0: } michael@0: } michael@0: michael@0: VP9D_PTR vp9_create_decompressor(VP9D_CONFIG *oxcf) { michael@0: VP9D_COMP *const pbi = vpx_memalign(32, sizeof(VP9D_COMP)); michael@0: VP9_COMMON *const cm = pbi ? &pbi->common : NULL; michael@0: michael@0: if (!cm) michael@0: return NULL; michael@0: michael@0: vp9_zero(*pbi); michael@0: michael@0: if (setjmp(cm->error.jmp)) { michael@0: cm->error.setjmp = 0; michael@0: vp9_remove_decompressor(pbi); michael@0: return NULL; michael@0: } michael@0: michael@0: cm->error.setjmp = 1; michael@0: vp9_initialize_dec(); michael@0: michael@0: vp9_create_common(cm); michael@0: michael@0: pbi->oxcf = *oxcf; michael@0: pbi->ready_for_new_data = 1; michael@0: cm->current_video_frame = 0; michael@0: michael@0: // vp9_init_dequantizer() is first called here. Add check in michael@0: // frame_init_dequantizer() to avoid unnecessary calling of michael@0: // vp9_init_dequantizer() for every frame. michael@0: vp9_init_dequantizer(cm); michael@0: michael@0: vp9_loop_filter_init(cm); michael@0: michael@0: cm->error.setjmp = 0; michael@0: pbi->decoded_key_frame = 0; michael@0: michael@0: init_macroblockd(pbi); michael@0: michael@0: vp9_worker_init(&pbi->lf_worker); michael@0: michael@0: return pbi; michael@0: } michael@0: michael@0: void vp9_remove_decompressor(VP9D_PTR ptr) { michael@0: int i; michael@0: VP9D_COMP *const pbi = (VP9D_COMP *)ptr; michael@0: michael@0: if (!pbi) michael@0: return; michael@0: michael@0: vp9_remove_common(&pbi->common); michael@0: vp9_worker_end(&pbi->lf_worker); michael@0: vpx_free(pbi->lf_worker.data1); michael@0: for (i = 0; i < pbi->num_tile_workers; ++i) { michael@0: VP9Worker *const worker = &pbi->tile_workers[i]; michael@0: vp9_worker_end(worker); michael@0: vpx_free(worker->data1); michael@0: vpx_free(worker->data2); michael@0: } michael@0: vpx_free(pbi->tile_workers); michael@0: vpx_free(pbi->mi_streams); michael@0: vpx_free(pbi->above_context[0]); michael@0: vpx_free(pbi->above_seg_context); michael@0: vpx_free(pbi); michael@0: } michael@0: michael@0: static int equal_dimensions(YV12_BUFFER_CONFIG *a, YV12_BUFFER_CONFIG *b) { michael@0: return a->y_height == b->y_height && a->y_width == b->y_width && michael@0: a->uv_height == b->uv_height && a->uv_width == b->uv_width; michael@0: } michael@0: michael@0: vpx_codec_err_t vp9_copy_reference_dec(VP9D_PTR ptr, michael@0: VP9_REFFRAME ref_frame_flag, michael@0: YV12_BUFFER_CONFIG *sd) { michael@0: VP9D_COMP *pbi = (VP9D_COMP *) ptr; michael@0: VP9_COMMON *cm = &pbi->common; michael@0: michael@0: /* TODO(jkoleszar): The decoder doesn't have any real knowledge of what the michael@0: * encoder is using the frame buffers for. This is just a stub to keep the michael@0: * vpxenc --test-decode functionality working, and will be replaced in a michael@0: * later commit that adds VP9-specific controls for this functionality. michael@0: */ michael@0: if (ref_frame_flag == VP9_LAST_FLAG) { michael@0: YV12_BUFFER_CONFIG *cfg = &cm->yv12_fb[cm->ref_frame_map[0]]; michael@0: if (!equal_dimensions(cfg, sd)) michael@0: vpx_internal_error(&cm->error, VPX_CODEC_ERROR, michael@0: "Incorrect buffer dimensions"); michael@0: else michael@0: vp8_yv12_copy_frame(cfg, sd); michael@0: } else { michael@0: vpx_internal_error(&cm->error, VPX_CODEC_ERROR, michael@0: "Invalid reference frame"); michael@0: } michael@0: michael@0: return cm->error.error_code; michael@0: } michael@0: michael@0: michael@0: vpx_codec_err_t vp9_set_reference_dec(VP9D_PTR ptr, VP9_REFFRAME ref_frame_flag, michael@0: YV12_BUFFER_CONFIG *sd) { michael@0: VP9D_COMP *pbi = (VP9D_COMP *) ptr; michael@0: VP9_COMMON *cm = &pbi->common; michael@0: int *ref_fb_ptr = NULL; michael@0: michael@0: /* TODO(jkoleszar): The decoder doesn't have any real knowledge of what the michael@0: * encoder is using the frame buffers for. This is just a stub to keep the michael@0: * vpxenc --test-decode functionality working, and will be replaced in a michael@0: * later commit that adds VP9-specific controls for this functionality. michael@0: */ michael@0: if (ref_frame_flag == VP9_LAST_FLAG) { michael@0: ref_fb_ptr = &pbi->common.active_ref_idx[0]; michael@0: } else if (ref_frame_flag == VP9_GOLD_FLAG) { michael@0: ref_fb_ptr = &pbi->common.active_ref_idx[1]; michael@0: } else if (ref_frame_flag == VP9_ALT_FLAG) { michael@0: ref_fb_ptr = &pbi->common.active_ref_idx[2]; 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 (!equal_dimensions(&cm->yv12_fb[*ref_fb_ptr], sd)) { michael@0: vpx_internal_error(&pbi->common.error, VPX_CODEC_ERROR, michael@0: "Incorrect buffer dimensions"); michael@0: } else { michael@0: // Find an empty frame buffer. michael@0: const int 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: michael@0: int vp9_get_reference_dec(VP9D_PTR ptr, int index, YV12_BUFFER_CONFIG **fb) { michael@0: VP9D_COMP *pbi = (VP9D_COMP *) ptr; michael@0: VP9_COMMON *cm = &pbi->common; michael@0: michael@0: if (index < 0 || index >= NUM_REF_FRAMES) michael@0: return -1; michael@0: michael@0: *fb = &cm->yv12_fb[cm->ref_frame_map[index]]; michael@0: return 0; michael@0: } michael@0: michael@0: /* If any buffer updating is signaled it should be done here. */ michael@0: static void swap_frame_buffers(VP9D_COMP *pbi) { michael@0: int ref_index = 0, mask; michael@0: VP9_COMMON *const cm = &pbi->common; michael@0: michael@0: for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) { michael@0: if (mask & 1) michael@0: ref_cnt_fb(cm->fb_idx_ref_cnt, &cm->ref_frame_map[ref_index], michael@0: cm->new_fb_idx); michael@0: ++ref_index; michael@0: } michael@0: michael@0: cm->frame_to_show = get_frame_new_buffer(cm); michael@0: cm->fb_idx_ref_cnt[cm->new_fb_idx]--; michael@0: michael@0: // Invalidate these references until the next frame starts. michael@0: for (ref_index = 0; ref_index < 3; ref_index++) michael@0: cm->active_ref_idx[ref_index] = INT_MAX; michael@0: } michael@0: michael@0: int vp9_receive_compressed_data(VP9D_PTR ptr, michael@0: size_t size, const uint8_t **psource, michael@0: int64_t time_stamp) { michael@0: VP9D_COMP *pbi = (VP9D_COMP *) ptr; michael@0: VP9_COMMON *cm = &pbi->common; michael@0: const uint8_t *source = *psource; michael@0: int retcode = 0; michael@0: michael@0: /*if(pbi->ready_for_new_data == 0) michael@0: return -1;*/ michael@0: michael@0: if (ptr == 0) michael@0: return -1; michael@0: michael@0: cm->error.error_code = VPX_CODEC_OK; michael@0: michael@0: pbi->source = source; michael@0: pbi->source_sz = size; michael@0: michael@0: if (pbi->source_sz == 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: * TODO(jkoleszar): Error concealment is undefined and non-normative michael@0: * at this point, but if it becomes so, [0] may not always be the correct michael@0: * thing to do here. michael@0: */ michael@0: if (cm->active_ref_idx[0] != INT_MAX) michael@0: get_frame_ref_buffer(cm, 0)->corrupted = 1; michael@0: } michael@0: michael@0: cm->new_fb_idx = get_free_fb(cm); michael@0: michael@0: if (setjmp(cm->error.jmp)) { michael@0: cm->error.setjmp = 0; 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: * TODO(jkoleszar): Error concealment is undefined and non-normative michael@0: * at this point, but if it becomes so, [0] may not always be the correct michael@0: * thing to do here. michael@0: */ michael@0: if (cm->active_ref_idx[0] != INT_MAX) michael@0: get_frame_ref_buffer(cm, 0)->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: return -1; michael@0: } michael@0: michael@0: cm->error.setjmp = 1; michael@0: michael@0: retcode = vp9_decode_frame(pbi, psource); michael@0: michael@0: if (retcode < 0) { michael@0: cm->error.error_code = VPX_CODEC_ERROR; michael@0: cm->error.setjmp = 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: return retcode; michael@0: } michael@0: michael@0: swap_frame_buffers(pbi); michael@0: michael@0: #if WRITE_RECON_BUFFER == 2 michael@0: if (cm->show_frame) michael@0: write_dx_frame_to_file(cm->frame_to_show, michael@0: cm->current_video_frame); michael@0: else michael@0: write_dx_frame_to_file(cm->frame_to_show, michael@0: cm->current_video_frame + 1000); michael@0: #endif michael@0: michael@0: if (!pbi->do_loopfilter_inline) { michael@0: vp9_loop_filter_frame(cm, &pbi->mb, pbi->common.lf.filter_level, 0, 0); michael@0: } michael@0: michael@0: #if WRITE_RECON_BUFFER == 2 michael@0: if (cm->show_frame) michael@0: write_dx_frame_to_file(cm->frame_to_show, michael@0: cm->current_video_frame + 2000); michael@0: else michael@0: write_dx_frame_to_file(cm->frame_to_show, michael@0: cm->current_video_frame + 3000); michael@0: #endif michael@0: michael@0: vp9_extend_frame_inner_borders(cm->frame_to_show, michael@0: cm->subsampling_x, michael@0: cm->subsampling_y); michael@0: michael@0: #if WRITE_RECON_BUFFER == 1 michael@0: if (cm->show_frame) michael@0: recon_write_yuv_frame("recon.yuv", cm->frame_to_show, michael@0: cm->width, cm->height); michael@0: #endif michael@0: michael@0: vp9_clear_system_state(); michael@0: michael@0: cm->last_show_frame = cm->show_frame; michael@0: if (cm->show_frame) { michael@0: // current mip will be the prev_mip for the next frame michael@0: MODE_INFO *temp = cm->prev_mip; michael@0: MODE_INFO **temp2 = cm->prev_mi_grid_base; michael@0: cm->prev_mip = cm->mip; michael@0: cm->mip = temp; michael@0: cm->prev_mi_grid_base = cm->mi_grid_base; michael@0: cm->mi_grid_base = temp2; michael@0: michael@0: // update the upper left visible macroblock ptrs michael@0: cm->mi = cm->mip + cm->mode_info_stride + 1; michael@0: cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1; michael@0: cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1; michael@0: cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1; michael@0: michael@0: pbi->mb.mi_8x8 = cm->mi_grid_visible; michael@0: pbi->mb.mi_8x8[0] = cm->mi; michael@0: michael@0: cm->current_video_frame++; michael@0: } michael@0: michael@0: pbi->ready_for_new_data = 0; michael@0: pbi->last_time_stamp = time_stamp; michael@0: pbi->source_sz = 0; michael@0: michael@0: cm->error.setjmp = 0; michael@0: return retcode; michael@0: } michael@0: michael@0: int vp9_get_raw_frame(VP9D_PTR ptr, YV12_BUFFER_CONFIG *sd, michael@0: int64_t *time_stamp, int64_t *time_end_stamp, michael@0: vp9_ppflags_t *flags) { michael@0: int ret = -1; michael@0: VP9D_COMP *pbi = (VP9D_COMP *) ptr; 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_VP9_POSTPROC michael@0: ret = vp9_post_proc_frame(&pbi->common, sd, flags); michael@0: #else michael@0: michael@0: if (pbi->common.frame_to_show) { 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_width = sd->y_width >> pbi->common.subsampling_x; michael@0: sd->uv_height = sd->y_height >> pbi->common.subsampling_y; michael@0: michael@0: ret = 0; michael@0: } else { michael@0: ret = -1; michael@0: } michael@0: michael@0: #endif /*!CONFIG_POSTPROC*/ michael@0: vp9_clear_system_state(); michael@0: return ret; michael@0: }