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 "vp10/common/vp9_alloccommon.h"
15 #include "vp10/common/vp9_blockd.h"
16 #include "vp10/common/vp9_entropymode.h"
17 #include "vp10/common/vp9_entropymv.h"
18 #include "vp10/common/vp9_onyxc_int.h"
19 #include "vp10/common/vp9_systemdependent.h"
21 void vp10_set_mb_mi(VP9_COMMON *cm, int width, int height) {
22 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
23 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
25 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
26 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
27 cm->mi_stride = calc_mi_size(cm->mi_cols);
29 cm->mb_cols = (cm->mi_cols + 1) >> 1;
30 cm->mb_rows = (cm->mi_rows + 1) >> 1;
31 cm->MBs = cm->mb_rows * cm->mb_cols;
34 static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
37 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
38 cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
39 if (cm->seg_map_array[i] == NULL)
42 cm->seg_map_alloc_size = seg_map_size;
46 cm->prev_seg_map_idx = 1;
48 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
49 if (!cm->frame_parallel_decode)
50 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
55 static void free_seg_map(VP9_COMMON *cm) {
58 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
59 vpx_free(cm->seg_map_array[i]);
60 cm->seg_map_array[i] = NULL;
63 cm->current_frame_seg_map = NULL;
65 if (!cm->frame_parallel_decode) {
66 cm->last_frame_seg_map = NULL;
70 void vp10_free_ref_frame_buffers(BufferPool *pool) {
73 for (i = 0; i < FRAME_BUFFERS; ++i) {
74 if (pool->frame_bufs[i].ref_count > 0 &&
75 pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
76 pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
77 pool->frame_bufs[i].ref_count = 0;
79 vpx_free(pool->frame_bufs[i].mvs);
80 pool->frame_bufs[i].mvs = NULL;
81 vp9_free_frame_buffer(&pool->frame_bufs[i].buf);
85 void vp10_free_postproc_buffers(VP9_COMMON *cm) {
86 #if CONFIG_VP9_POSTPROC
87 vp9_free_frame_buffer(&cm->post_proc_buffer);
88 vp9_free_frame_buffer(&cm->post_proc_buffer_int);
94 void vp10_free_context_buffers(VP9_COMMON *cm) {
97 vpx_free(cm->above_context);
98 cm->above_context = NULL;
99 vpx_free(cm->above_seg_context);
100 cm->above_seg_context = NULL;
103 int vp10_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
106 vp10_set_mb_mi(cm, width, height);
107 new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
108 if (cm->mi_alloc_size < new_mi_size) {
110 if (cm->alloc_mi(cm, new_mi_size))
114 if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
115 // Create the segmentation map structure and set to 0.
117 if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols))
121 if (cm->above_context_alloc_cols < cm->mi_cols) {
122 vpx_free(cm->above_context);
123 cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
124 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
125 sizeof(*cm->above_context));
126 if (!cm->above_context) goto fail;
128 vpx_free(cm->above_seg_context);
129 cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
130 mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
131 if (!cm->above_seg_context) goto fail;
132 cm->above_context_alloc_cols = cm->mi_cols;
138 vp10_free_context_buffers(cm);
142 void vp10_remove_common(VP9_COMMON *cm) {
143 vp10_free_context_buffers(cm);
147 vpx_free(cm->frame_contexts);
148 cm->frame_contexts = NULL;
151 void vp10_init_context_buffers(VP9_COMMON *cm) {
153 if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
154 memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
157 void vp10_swap_current_and_last_seg_map(VP9_COMMON *cm) {
159 const int tmp = cm->seg_map_idx;
160 cm->seg_map_idx = cm->prev_seg_map_idx;
161 cm->prev_seg_map_idx = tmp;
163 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
164 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];