vp9_alloccommon.c revision 5ae7ac49f08a179e4f054d99fcfc9dce78d26e58
1ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang/*
2ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *
4ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  Use of this source code is governed by a BSD-style license
5ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  that can be found in the LICENSE file in the root of the source
6ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  tree. An additional intellectual property rights grant can be found
7ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  in the file PATENTS.  All contributing project authors may
8ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  be found in the AUTHORS file in the root of the source tree.
9ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang */
10ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
11ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
12ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "./vpx_config.h"
13ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vpx_mem/vpx_mem.h"
1491037db265ecdd914a26e056cf69207b4f50924ehkuang
15ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/common/vp9_blockd.h"
16ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/common/vp9_entropymode.h"
17ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/common/vp9_entropymv.h"
18ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/common/vp9_findnearmv.h"
19ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/common/vp9_onyxc_int.h"
20ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "vp9/common/vp9_systemdependent.h"
21ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
22ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangvoid vp9_update_mode_info_border(VP9_COMMON *cm, MODE_INFO *mi) {
23ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  const int stride = cm->mode_info_stride;
24ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  int i;
25ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
26ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  // Clear down top border row
27ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  vpx_memset(mi, 0, sizeof(MODE_INFO) * stride);
28ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
29ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  // Clear left border column
30ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  for (i = 1; i < cm->mi_rows + 1; i++)
31ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    vpx_memset(&mi[i * stride], 0, sizeof(MODE_INFO));
32ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
33ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
341184aebb761cbeac9124c37189a80a1a58f04b6bhkuangvoid vp9_free_frame_buffers(VP9_COMMON *cm) {
35ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  int i;
36ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
37ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  for (i = 0; i < NUM_YV12_BUFFERS; i++)
381184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    vp9_free_frame_buffer(&cm->yv12_fb[i]);
39ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
401184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vp9_free_frame_buffer(&cm->post_proc_buffer);
41ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
421184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vpx_free(cm->mip);
431184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vpx_free(cm->prev_mip);
441184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vpx_free(cm->last_frame_seg_map);
451184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vpx_free(cm->mi_grid_base);
461184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vpx_free(cm->prev_mi_grid_base);
47ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
481184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->mip = NULL;
491184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->prev_mip = NULL;
501184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->last_frame_seg_map = NULL;
511184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->mi_grid_base = NULL;
521184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->prev_mi_grid_base = NULL;
53ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
54ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
55ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstatic void set_mb_mi(VP9_COMMON *cm, int aligned_width, int aligned_height) {
561184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
571184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
5891037db265ecdd914a26e056cf69207b4f50924ehkuang  cm->mode_info_stride = cm->mi_cols + MI_BLOCK_SIZE;
595ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang
605ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  cm->mb_cols = (cm->mi_cols + 1) >> 1;
615ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  cm->mb_rows = (cm->mi_rows + 1) >> 1;
625ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  cm->MBs = cm->mb_rows * cm->mb_cols;
63ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
64ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
65ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangstatic void setup_mi(VP9_COMMON *cm) {
66ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  cm->mi = cm->mip + cm->mode_info_stride + 1;
67ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1;
681184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1;
691184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1;
70ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
71ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  vpx_memset(cm->mip, 0,
72ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang             cm->mode_info_stride * (cm->mi_rows + 1) * sizeof(MODE_INFO));
73ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
741184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vpx_memset(cm->mi_grid_base, 0,
751184aebb761cbeac9124c37189a80a1a58f04b6bhkuang             cm->mode_info_stride * (cm->mi_rows + 1) *
761184aebb761cbeac9124c37189a80a1a58f04b6bhkuang             sizeof(*cm->mi_grid_base));
77ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
781184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vp9_update_mode_info_border(cm, cm->mip);
79ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  vp9_update_mode_info_border(cm, cm->prev_mip);
80ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
81ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
821184aebb761cbeac9124c37189a80a1a58f04b6bhkuangint vp9_alloc_frame_buffers(VP9_COMMON *cm, int width, int height) {
835ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  int i;
84ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
851184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
861184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
871184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  const int ss_x = cm->subsampling_x;
881184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  const int ss_y = cm->subsampling_y;
8991037db265ecdd914a26e056cf69207b4f50924ehkuang  int mi_size;
90ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
911184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vp9_free_frame_buffers(cm);
92ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
93ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  for (i = 0; i < NUM_YV12_BUFFERS; i++) {
941184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    cm->fb_idx_ref_cnt[i] = 0;
951184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    if (vp9_alloc_frame_buffer(&cm->yv12_fb[i], width, height, ss_x, ss_y,
96ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang                               VP9BORDERINPIXELS) < 0)
97ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang      goto fail;
98ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
99ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1001184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->new_fb_idx = NUM_YV12_BUFFERS - 1;
1011184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->fb_idx_ref_cnt[cm->new_fb_idx] = 1;
102ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
103ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  for (i = 0; i < ALLOWED_REFS_PER_FRAME; i++)
1041184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    cm->active_ref_idx[i] = i;
105ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
106ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  for (i = 0; i < NUM_REF_FRAMES; i++) {
1071184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    cm->ref_frame_map[i] = i;
1081184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    cm->fb_idx_ref_cnt[i] = 1;
109ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
110ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1111184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
112ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang                             VP9BORDERINPIXELS) < 0)
113ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    goto fail;
114ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1151184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  set_mb_mi(cm, aligned_width, aligned_height);
116ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
117ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  // Allocation
1181184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  mi_size = cm->mode_info_stride * (cm->mi_rows + MI_BLOCK_SIZE);
11991037db265ecdd914a26e056cf69207b4f50924ehkuang
1201184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
1211184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  if (!cm->mip)
122ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    goto fail;
123ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1241184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->prev_mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
1251184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  if (!cm->prev_mip)
126ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    goto fail;
127ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1281184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->mi_grid_base));
1291184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  if (!cm->mi_grid_base)
1301184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    goto fail;
1311184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
1321184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->prev_mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->prev_mi_grid_base));
1331184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  if (!cm->prev_mi_grid_base)
1341184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    goto fail;
1351184aebb761cbeac9124c37189a80a1a58f04b6bhkuang
1361184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  setup_mi(cm);
137ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1381184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  // Create the segmentation map structure and set to 0.
1391184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->last_frame_seg_map = vpx_calloc(cm->mi_rows * cm->mi_cols, 1);
1401184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  if (!cm->last_frame_seg_map)
141ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    goto fail;
142ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
143ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  return 0;
144ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
145ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang fail:
1461184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vp9_free_frame_buffers(cm);
147ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  return 1;
148ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
149ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1501184aebb761cbeac9124c37189a80a1a58f04b6bhkuangvoid vp9_create_common(VP9_COMMON *cm) {
1511184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vp9_machine_specific_config(cm);
152ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1531184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->tx_mode = ONLY_4X4;
1541184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  cm->comp_pred_mode = HYBRID_PREDICTION;
155ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
156ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
1571184aebb761cbeac9124c37189a80a1a58f04b6bhkuangvoid vp9_remove_common(VP9_COMMON *cm) {
1581184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  vp9_free_frame_buffers(cm);
159ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
160ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
161ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangvoid vp9_initialize_common() {
1625ae7ac49f08a179e4f054d99fcfc9dce78d26e58hkuang  vp9_init_neighbors();
163ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  vp9_coef_tree_initialize();
164ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  vp9_entropy_mode_init();
165ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  vp9_entropy_mv_init();
166ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
167ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
168ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangvoid vp9_update_frame_size(VP9_COMMON *cm) {
1691184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, MI_SIZE_LOG2);
1701184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  const int aligned_height = ALIGN_POWER_OF_TWO(cm->height, MI_SIZE_LOG2);
171ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
172ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  set_mb_mi(cm, aligned_width, aligned_height);
173ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  setup_mi(cm);
17491037db265ecdd914a26e056cf69207b4f50924ehkuang
1751184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  // Initialize the previous frame segment map to 0.
1761184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  if (cm->last_frame_seg_map)
1771184aebb761cbeac9124c37189a80a1a58f04b6bhkuang    vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
178ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
179