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 "./vpx_config.h" michael@0: #include "vpx_mem/vpx_mem.h" michael@0: michael@0: #include "vp9/common/vp9_blockd.h" michael@0: #include "vp9/common/vp9_entropymode.h" michael@0: #include "vp9/common/vp9_entropymv.h" michael@0: #include "vp9/common/vp9_findnearmv.h" michael@0: #include "vp9/common/vp9_onyxc_int.h" michael@0: #include "vp9/common/vp9_systemdependent.h" michael@0: michael@0: void vp9_update_mode_info_border(VP9_COMMON *cm, MODE_INFO *mi) { michael@0: const int stride = cm->mode_info_stride; michael@0: int i; michael@0: michael@0: // Clear down top border row michael@0: vpx_memset(mi, 0, sizeof(MODE_INFO) * stride); michael@0: michael@0: // Clear left border column michael@0: for (i = 1; i < cm->mi_rows + 1; i++) michael@0: vpx_memset(&mi[i * stride], 0, sizeof(MODE_INFO)); michael@0: } michael@0: michael@0: void vp9_free_frame_buffers(VP9_COMMON *cm) { michael@0: int i; michael@0: michael@0: for (i = 0; i < NUM_YV12_BUFFERS; i++) michael@0: vp9_free_frame_buffer(&cm->yv12_fb[i]); michael@0: michael@0: vp9_free_frame_buffer(&cm->post_proc_buffer); michael@0: michael@0: vpx_free(cm->mip); michael@0: vpx_free(cm->prev_mip); michael@0: vpx_free(cm->last_frame_seg_map); michael@0: vpx_free(cm->mi_grid_base); michael@0: vpx_free(cm->prev_mi_grid_base); michael@0: michael@0: cm->mip = NULL; michael@0: cm->prev_mip = NULL; michael@0: cm->last_frame_seg_map = NULL; michael@0: cm->mi_grid_base = NULL; michael@0: cm->prev_mi_grid_base = NULL; michael@0: } michael@0: michael@0: static void set_mb_mi(VP9_COMMON *cm, int aligned_width, int aligned_height) { michael@0: cm->mi_cols = aligned_width >> MI_SIZE_LOG2; michael@0: cm->mi_rows = aligned_height >> MI_SIZE_LOG2; michael@0: cm->mode_info_stride = cm->mi_cols + MI_BLOCK_SIZE; michael@0: michael@0: cm->mb_cols = (cm->mi_cols + 1) >> 1; michael@0: cm->mb_rows = (cm->mi_rows + 1) >> 1; michael@0: cm->MBs = cm->mb_rows * cm->mb_cols; michael@0: } michael@0: michael@0: static void setup_mi(VP9_COMMON *cm) { 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: vpx_memset(cm->mip, 0, michael@0: cm->mode_info_stride * (cm->mi_rows + 1) * sizeof(MODE_INFO)); michael@0: michael@0: vpx_memset(cm->mi_grid_base, 0, michael@0: cm->mode_info_stride * (cm->mi_rows + 1) * michael@0: sizeof(*cm->mi_grid_base)); michael@0: michael@0: vp9_update_mode_info_border(cm, cm->mip); michael@0: vp9_update_mode_info_border(cm, cm->prev_mip); michael@0: } michael@0: michael@0: int vp9_resize_frame_buffers(VP9_COMMON *cm, int width, int height) { michael@0: const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2); michael@0: const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2); michael@0: const int ss_x = cm->subsampling_x; michael@0: const int ss_y = cm->subsampling_y; michael@0: int mi_size; michael@0: michael@0: if (vp9_realloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y, michael@0: VP9BORDERINPIXELS) < 0) michael@0: goto fail; michael@0: michael@0: set_mb_mi(cm, aligned_width, aligned_height); michael@0: michael@0: // Allocation michael@0: mi_size = cm->mode_info_stride * (cm->mi_rows + MI_BLOCK_SIZE); michael@0: michael@0: vpx_free(cm->mip); michael@0: cm->mip = vpx_calloc(mi_size, sizeof(MODE_INFO)); michael@0: if (!cm->mip) michael@0: goto fail; michael@0: michael@0: vpx_free(cm->prev_mip); michael@0: cm->prev_mip = vpx_calloc(mi_size, sizeof(MODE_INFO)); michael@0: if (!cm->prev_mip) michael@0: goto fail; michael@0: michael@0: vpx_free(cm->mi_grid_base); michael@0: cm->mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->mi_grid_base)); michael@0: if (!cm->mi_grid_base) michael@0: goto fail; michael@0: michael@0: vpx_free(cm->prev_mi_grid_base); michael@0: cm->prev_mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->prev_mi_grid_base)); michael@0: if (!cm->prev_mi_grid_base) michael@0: goto fail; michael@0: michael@0: setup_mi(cm); michael@0: michael@0: // Create the segmentation map structure and set to 0. michael@0: vpx_free(cm->last_frame_seg_map); michael@0: cm->last_frame_seg_map = vpx_calloc(cm->mi_rows * cm->mi_cols, 1); michael@0: if (!cm->last_frame_seg_map) michael@0: goto fail; michael@0: michael@0: return 0; michael@0: michael@0: fail: michael@0: vp9_free_frame_buffers(cm); michael@0: return 1; michael@0: } michael@0: michael@0: int vp9_alloc_frame_buffers(VP9_COMMON *cm, int width, int height) { michael@0: int i; michael@0: michael@0: const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2); michael@0: const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2); michael@0: const int ss_x = cm->subsampling_x; michael@0: const int ss_y = cm->subsampling_y; michael@0: int mi_size; michael@0: michael@0: vp9_free_frame_buffers(cm); michael@0: michael@0: for (i = 0; i < NUM_YV12_BUFFERS; i++) { michael@0: cm->fb_idx_ref_cnt[i] = 0; michael@0: if (vp9_alloc_frame_buffer(&cm->yv12_fb[i], width, height, ss_x, ss_y, michael@0: VP9BORDERINPIXELS) < 0) michael@0: goto fail; michael@0: } michael@0: michael@0: cm->new_fb_idx = NUM_YV12_BUFFERS - 1; michael@0: cm->fb_idx_ref_cnt[cm->new_fb_idx] = 1; michael@0: michael@0: for (i = 0; i < ALLOWED_REFS_PER_FRAME; i++) michael@0: cm->active_ref_idx[i] = i; michael@0: michael@0: for (i = 0; i < NUM_REF_FRAMES; i++) { michael@0: cm->ref_frame_map[i] = i; michael@0: cm->fb_idx_ref_cnt[i] = 1; michael@0: } michael@0: michael@0: if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y, michael@0: VP9BORDERINPIXELS) < 0) michael@0: goto fail; michael@0: michael@0: set_mb_mi(cm, aligned_width, aligned_height); michael@0: michael@0: // Allocation michael@0: mi_size = cm->mode_info_stride * (cm->mi_rows + MI_BLOCK_SIZE); michael@0: michael@0: cm->mip = vpx_calloc(mi_size, sizeof(MODE_INFO)); michael@0: if (!cm->mip) michael@0: goto fail; michael@0: michael@0: cm->prev_mip = vpx_calloc(mi_size, sizeof(MODE_INFO)); michael@0: if (!cm->prev_mip) michael@0: goto fail; michael@0: michael@0: cm->mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->mi_grid_base)); michael@0: if (!cm->mi_grid_base) michael@0: goto fail; michael@0: michael@0: cm->prev_mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->prev_mi_grid_base)); michael@0: if (!cm->prev_mi_grid_base) michael@0: goto fail; michael@0: michael@0: setup_mi(cm); michael@0: michael@0: // Create the segmentation map structure and set to 0. michael@0: cm->last_frame_seg_map = vpx_calloc(cm->mi_rows * cm->mi_cols, 1); michael@0: if (!cm->last_frame_seg_map) michael@0: goto fail; michael@0: michael@0: return 0; michael@0: michael@0: fail: michael@0: vp9_free_frame_buffers(cm); michael@0: return 1; michael@0: } michael@0: michael@0: void vp9_create_common(VP9_COMMON *cm) { michael@0: vp9_machine_specific_config(cm); michael@0: michael@0: cm->tx_mode = ONLY_4X4; michael@0: cm->comp_pred_mode = HYBRID_PREDICTION; michael@0: } michael@0: michael@0: void vp9_remove_common(VP9_COMMON *cm) { michael@0: vp9_free_frame_buffers(cm); michael@0: } michael@0: michael@0: void vp9_initialize_common() { michael@0: vp9_init_neighbors(); michael@0: vp9_coef_tree_initialize(); michael@0: vp9_entropy_mode_init(); michael@0: vp9_entropy_mv_init(); michael@0: } michael@0: michael@0: void vp9_update_frame_size(VP9_COMMON *cm) { michael@0: const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, MI_SIZE_LOG2); michael@0: const int aligned_height = ALIGN_POWER_OF_TWO(cm->height, MI_SIZE_LOG2); michael@0: michael@0: set_mb_mi(cm, aligned_width, aligned_height); michael@0: setup_mi(cm); michael@0: michael@0: // Initialize the previous frame segment map to 0. michael@0: if (cm->last_frame_seg_map) michael@0: vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols); michael@0: }