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 "./vpx_config.h"
13 #include "vpx_mem/vpx_mem.h"
15 #include "vp9/common/vp9_blockd.h"
16 #include "vp9/common/vp9_entropymode.h"
17 #include "vp9/common/vp9_entropymv.h"
18 #include "vp9/common/vp9_onyxc_int.h"
19 #include "vp9/common/vp9_systemdependent.h"
21 void vp9_update_mode_info_border(VP9_COMMON *cm, MODE_INFO *mi) {
22 const int stride = cm->mode_info_stride;
25 // Clear down top border row
26 vpx_memset(mi, 0, sizeof(MODE_INFO) * stride);
28 // Clear left border column
29 for (i = 1; i < cm->mi_rows + 1; i++)
30 vpx_memset(&mi[i * stride], 0, sizeof(MODE_INFO));
33 void vp9_free_frame_buffers(VP9_COMMON *cm) {
36 for (i = 0; i < cm->fb_count; i++)
37 vp9_free_frame_buffer(&cm->yv12_fb[i]);
39 vp9_free_frame_buffer(&cm->post_proc_buffer);
42 vpx_free(cm->prev_mip);
43 vpx_free(cm->last_frame_seg_map);
44 vpx_free(cm->mi_grid_base);
45 vpx_free(cm->prev_mi_grid_base);
49 cm->last_frame_seg_map = NULL;
50 cm->mi_grid_base = NULL;
51 cm->prev_mi_grid_base = NULL;
54 static void set_mb_mi(VP9_COMMON *cm, int aligned_width, int aligned_height) {
55 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
56 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
57 cm->mode_info_stride = cm->mi_cols + MI_BLOCK_SIZE;
59 cm->mb_cols = (cm->mi_cols + 1) >> 1;
60 cm->mb_rows = (cm->mi_rows + 1) >> 1;
61 cm->MBs = cm->mb_rows * cm->mb_cols;
64 static void setup_mi(VP9_COMMON *cm) {
65 cm->mi = cm->mip + cm->mode_info_stride + 1;
66 cm->prev_mi = cm->prev_mip + cm->mode_info_stride + 1;
67 cm->mi_grid_visible = cm->mi_grid_base + cm->mode_info_stride + 1;
68 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mode_info_stride + 1;
70 vpx_memset(cm->mip, 0,
71 cm->mode_info_stride * (cm->mi_rows + 1) * sizeof(MODE_INFO));
73 vpx_memset(cm->mi_grid_base, 0,
74 cm->mode_info_stride * (cm->mi_rows + 1) *
75 sizeof(*cm->mi_grid_base));
77 vp9_update_mode_info_border(cm, cm->prev_mip);
80 int vp9_resize_frame_buffers(VP9_COMMON *cm, int width, int height) {
81 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
82 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
83 const int ss_x = cm->subsampling_x;
84 const int ss_y = cm->subsampling_y;
87 if (vp9_realloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
88 VP9BORDERINPIXELS, NULL, NULL, NULL) < 0)
91 set_mb_mi(cm, aligned_width, aligned_height);
94 mi_size = cm->mode_info_stride * (cm->mi_rows + MI_BLOCK_SIZE);
97 cm->mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
101 vpx_free(cm->prev_mip);
102 cm->prev_mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
106 vpx_free(cm->mi_grid_base);
107 cm->mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->mi_grid_base));
108 if (!cm->mi_grid_base)
111 vpx_free(cm->prev_mi_grid_base);
112 cm->prev_mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->prev_mi_grid_base));
113 if (!cm->prev_mi_grid_base)
118 // Create the segmentation map structure and set to 0.
119 vpx_free(cm->last_frame_seg_map);
120 cm->last_frame_seg_map = vpx_calloc(cm->mi_rows * cm->mi_cols, 1);
121 if (!cm->last_frame_seg_map)
127 vp9_free_frame_buffers(cm);
131 int vp9_alloc_frame_buffers(VP9_COMMON *cm, int width, int height) {
134 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
135 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
136 const int ss_x = cm->subsampling_x;
137 const int ss_y = cm->subsampling_y;
140 if (cm->fb_count == 0) {
141 cm->fb_count = FRAME_BUFFERS;
142 CHECK_MEM_ERROR(cm, cm->yv12_fb,
143 vpx_calloc(cm->fb_count, sizeof(*cm->yv12_fb)));
144 CHECK_MEM_ERROR(cm, cm->fb_idx_ref_cnt,
145 vpx_calloc(cm->fb_count, sizeof(*cm->fb_idx_ref_cnt)));
147 CHECK_MEM_ERROR(cm, cm->fb_idx_ref_lru,
148 vpx_calloc(cm->fb_count, sizeof(*cm->fb_idx_ref_lru)));
152 vp9_free_frame_buffers(cm);
154 for (i = 0; i < cm->fb_count; i++) {
155 cm->fb_idx_ref_cnt[i] = 0;
156 if (vp9_alloc_frame_buffer(&cm->yv12_fb[i], width, height, ss_x, ss_y,
157 VP9BORDERINPIXELS) < 0)
161 cm->new_fb_idx = cm->fb_count - 1;
162 cm->fb_idx_ref_cnt[cm->new_fb_idx] = 1;
164 for (i = 0; i < REF_FRAMES; i++) {
165 cm->ref_frame_map[i] = i;
166 cm->fb_idx_ref_cnt[i] = 1;
169 if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
170 VP9BORDERINPIXELS) < 0)
173 set_mb_mi(cm, aligned_width, aligned_height);
176 mi_size = cm->mode_info_stride * (cm->mi_rows + MI_BLOCK_SIZE);
178 cm->mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
182 cm->prev_mip = vpx_calloc(mi_size, sizeof(MODE_INFO));
186 cm->mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->mi_grid_base));
187 if (!cm->mi_grid_base)
190 cm->prev_mi_grid_base = vpx_calloc(mi_size, sizeof(*cm->prev_mi_grid_base));
191 if (!cm->prev_mi_grid_base)
196 // Create the segmentation map structure and set to 0.
197 cm->last_frame_seg_map = vpx_calloc(cm->mi_rows * cm->mi_cols, 1);
198 if (!cm->last_frame_seg_map)
204 vp9_free_frame_buffers(cm);
208 void vp9_create_common(VP9_COMMON *cm) {
209 vp9_machine_specific_config(cm);
212 void vp9_remove_common(VP9_COMMON *cm) {
213 vp9_free_frame_buffers(cm);
215 vpx_free(cm->yv12_fb);
216 vpx_free(cm->fb_idx_ref_cnt);
217 vpx_free(cm->fb_idx_ref_lru);
220 cm->fb_idx_ref_cnt = NULL;
221 cm->fb_idx_ref_lru = NULL;
224 void vp9_initialize_common() {
225 vp9_init_neighbors();
228 void vp9_update_frame_size(VP9_COMMON *cm) {
229 const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, MI_SIZE_LOG2);
230 const int aligned_height = ALIGN_POWER_OF_TWO(cm->height, MI_SIZE_LOG2);
232 set_mb_mi(cm, aligned_width, aligned_height);
235 // Initialize the previous frame segment map to 0.
236 if (cm->last_frame_seg_map)
237 vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);