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.
11 #include "./vpx_config.h"
12 #include "vpx_mem/vpx_mem.h"
14 #include "vp9/common/vp9_blockd.h"
15 #include "vp9/common/vp9_entropymode.h"
16 #include "vp9/common/vp9_entropymv.h"
17 #include "vp9/common/vp9_onyxc_int.h"
18 #include "vp9/common/vp9_systemdependent.h"
20 static void clear_mi_border(const VP9_COMMON *cm, MODE_INFO *mi) {
24 vpx_memset(mi, 0, sizeof(*mi) * cm->mi_stride);
27 for (i = 1; i < cm->mi_rows + 1; ++i)
28 vpx_memset(&mi[i * cm->mi_stride], 0, sizeof(*mi));
31 static void set_mb_mi(VP9_COMMON *cm, int aligned_width, int aligned_height) {
32 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
33 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
34 cm->mi_stride = cm->mi_cols + MI_BLOCK_SIZE;
36 cm->mb_cols = (cm->mi_cols + 1) >> 1;
37 cm->mb_rows = (cm->mi_rows + 1) >> 1;
38 cm->MBs = cm->mb_rows * cm->mb_cols;
41 static void setup_mi(VP9_COMMON *cm) {
42 cm->mi = cm->mip + cm->mi_stride + 1;
43 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
44 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
45 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
47 vpx_memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
49 vpx_memset(cm->mi_grid_base, 0, cm->mi_stride * (cm->mi_rows + 1) *
50 sizeof(*cm->mi_grid_base));
52 clear_mi_border(cm, cm->prev_mip);
55 static int alloc_mi(VP9_COMMON *cm, int mi_size) {
56 cm->mip = (MODE_INFO *)vpx_calloc(mi_size, sizeof(*cm->mip));
60 cm->prev_mip = (MODE_INFO *)vpx_calloc(mi_size, sizeof(*cm->prev_mip));
61 if (cm->prev_mip == NULL)
65 (MODE_INFO **)vpx_calloc(mi_size, sizeof(*cm->mi_grid_base));
66 if (cm->mi_grid_base == NULL)
69 cm->prev_mi_grid_base =
70 (MODE_INFO **)vpx_calloc(mi_size, sizeof(*cm->prev_mi_grid_base));
71 if (cm->prev_mi_grid_base == NULL)
77 static void free_mi(VP9_COMMON *cm) {
79 vpx_free(cm->prev_mip);
80 vpx_free(cm->mi_grid_base);
81 vpx_free(cm->prev_mi_grid_base);
85 cm->mi_grid_base = NULL;
86 cm->prev_mi_grid_base = NULL;
89 void vp9_free_frame_buffers(VP9_COMMON *cm) {
92 for (i = 0; i < FRAME_BUFFERS; ++i) {
93 vp9_free_frame_buffer(&cm->frame_bufs[i].buf);
95 if (cm->frame_bufs[i].ref_count > 0 &&
96 cm->frame_bufs[i].raw_frame_buffer.data != NULL) {
97 cm->release_fb_cb(cm->cb_priv, &cm->frame_bufs[i].raw_frame_buffer);
98 cm->frame_bufs[i].ref_count = 0;
102 vp9_free_frame_buffer(&cm->post_proc_buffer);
106 vpx_free(cm->last_frame_seg_map);
107 cm->last_frame_seg_map = NULL;
109 vpx_free(cm->above_context);
110 cm->above_context = NULL;
112 vpx_free(cm->above_seg_context);
113 cm->above_seg_context = NULL;
116 int vp9_resize_frame_buffers(VP9_COMMON *cm, int width, int height) {
117 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
118 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
119 const int ss_x = cm->subsampling_x;
120 const int ss_y = cm->subsampling_y;
122 if (vp9_realloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
123 VP9_DEC_BORDER_IN_PIXELS, NULL, NULL, NULL) < 0)
126 set_mb_mi(cm, aligned_width, aligned_height);
129 if (alloc_mi(cm, cm->mi_stride * (cm->mi_rows + MI_BLOCK_SIZE)))
134 // Create the segmentation map structure and set to 0.
135 vpx_free(cm->last_frame_seg_map);
136 cm->last_frame_seg_map = (uint8_t *)vpx_calloc(cm->mi_rows * cm->mi_cols, 1);
137 if (!cm->last_frame_seg_map)
140 vpx_free(cm->above_context);
142 (ENTROPY_CONTEXT *)vpx_calloc(2 * mi_cols_aligned_to_sb(cm->mi_cols) *
144 sizeof(*cm->above_context));
145 if (!cm->above_context)
148 vpx_free(cm->above_seg_context);
149 cm->above_seg_context =
150 (PARTITION_CONTEXT *)vpx_calloc(mi_cols_aligned_to_sb(cm->mi_cols),
151 sizeof(*cm->above_seg_context));
152 if (!cm->above_seg_context)
158 vp9_free_frame_buffers(cm);
162 int vp9_alloc_frame_buffers(VP9_COMMON *cm, int width, int height) {
163 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
164 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
165 const int ss_x = cm->subsampling_x;
166 const int ss_y = cm->subsampling_y;
169 vp9_free_frame_buffers(cm);
171 for (i = 0; i < FRAME_BUFFERS; i++) {
172 cm->frame_bufs[i].ref_count = 0;
173 if (vp9_alloc_frame_buffer(&cm->frame_bufs[i].buf, width, height,
174 ss_x, ss_y, VP9_ENC_BORDER_IN_PIXELS) < 0)
178 cm->new_fb_idx = FRAME_BUFFERS - 1;
179 cm->frame_bufs[cm->new_fb_idx].ref_count = 1;
181 for (i = 0; i < REF_FRAMES; i++) {
182 cm->ref_frame_map[i] = i;
183 cm->frame_bufs[i].ref_count = 1;
186 if (vp9_alloc_frame_buffer(&cm->post_proc_buffer, width, height, ss_x, ss_y,
187 VP9_ENC_BORDER_IN_PIXELS) < 0)
190 set_mb_mi(cm, aligned_width, aligned_height);
192 if (alloc_mi(cm, cm->mi_stride * (cm->mi_rows + MI_BLOCK_SIZE)))
197 // Create the segmentation map structure and set to 0.
198 cm->last_frame_seg_map = (uint8_t *)vpx_calloc(cm->mi_rows * cm->mi_cols, 1);
199 if (!cm->last_frame_seg_map)
203 (ENTROPY_CONTEXT *)vpx_calloc(2 * mi_cols_aligned_to_sb(cm->mi_cols) *
205 sizeof(*cm->above_context));
206 if (!cm->above_context)
209 cm->above_seg_context =
210 (PARTITION_CONTEXT *)vpx_calloc(mi_cols_aligned_to_sb(cm->mi_cols),
211 sizeof(*cm->above_seg_context));
212 if (!cm->above_seg_context)
218 vp9_free_frame_buffers(cm);
222 void vp9_remove_common(VP9_COMMON *cm) {
223 vp9_free_frame_buffers(cm);
224 vp9_free_internal_frame_buffers(&cm->int_frame_buffers);
227 void vp9_update_frame_size(VP9_COMMON *cm) {
228 const int aligned_width = ALIGN_POWER_OF_TWO(cm->width, MI_SIZE_LOG2);
229 const int aligned_height = ALIGN_POWER_OF_TWO(cm->height, MI_SIZE_LOG2);
231 set_mb_mi(cm, aligned_width, aligned_height);
234 // Initialize the previous frame segment map to 0.
235 if (cm->last_frame_seg_map)
236 vpx_memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
239 void vp9_swap_mi_and_prev_mi(VP9_COMMON *cm) {
240 // Current mip will be the prev_mip for the next frame.
241 MODE_INFO *temp = cm->prev_mip;
242 MODE_INFO **temp2 = cm->prev_mi_grid_base;
243 cm->prev_mip = cm->mip;
245 cm->prev_mi_grid_base = cm->mi_grid_base;
246 cm->mi_grid_base = temp2;
248 // Update the upper left visible macroblock ptrs.
249 cm->mi = cm->mip + cm->mi_stride + 1;
250 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
251 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
252 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;