1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3 *
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.
9 */
10
11 #include "./vpx_config.h"
12 #include "vpx_mem/vpx_mem.h"
13
14 #include "vp9/common/vp9_alloccommon.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
vp9_set_mi_size(int * mi_rows,int * mi_cols,int * mi_stride,int width,int height)20 void vp9_set_mi_size(int *mi_rows, int *mi_cols, int *mi_stride, int width,
21 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);
24 *mi_cols = aligned_width >> MI_SIZE_LOG2;
25 *mi_rows = aligned_height >> MI_SIZE_LOG2;
26 *mi_stride = calc_mi_size(*mi_cols);
27 }
28
vp9_set_mb_size(int * mb_rows,int * mb_cols,int * mb_num,int mi_rows,int mi_cols)29 void vp9_set_mb_size(int *mb_rows, int *mb_cols, int *mb_num, int mi_rows,
30 int mi_cols) {
31 *mb_cols = (mi_cols + 1) >> 1;
32 *mb_rows = (mi_rows + 1) >> 1;
33 *mb_num = (*mb_rows) * (*mb_cols);
34 }
35
vp9_set_mb_mi(VP9_COMMON * cm,int width,int height)36 void vp9_set_mb_mi(VP9_COMMON *cm, int width, int height) {
37 vp9_set_mi_size(&cm->mi_rows, &cm->mi_cols, &cm->mi_stride, width, height);
38 vp9_set_mb_size(&cm->mb_rows, &cm->mb_cols, &cm->MBs, cm->mi_rows,
39 cm->mi_cols);
40 }
41
alloc_seg_map(VP9_COMMON * cm,int seg_map_size)42 static int alloc_seg_map(VP9_COMMON *cm, int seg_map_size) {
43 int i;
44
45 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
46 cm->seg_map_array[i] = (uint8_t *)vpx_calloc(seg_map_size, 1);
47 if (cm->seg_map_array[i] == NULL) return 1;
48 }
49 cm->seg_map_alloc_size = seg_map_size;
50
51 // Init the index.
52 cm->seg_map_idx = 0;
53 cm->prev_seg_map_idx = 1;
54
55 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
56 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
57
58 return 0;
59 }
60
free_seg_map(VP9_COMMON * cm)61 static void free_seg_map(VP9_COMMON *cm) {
62 int i;
63
64 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
65 vpx_free(cm->seg_map_array[i]);
66 cm->seg_map_array[i] = NULL;
67 }
68
69 cm->current_frame_seg_map = NULL;
70 cm->last_frame_seg_map = NULL;
71 }
72
vp9_free_ref_frame_buffers(BufferPool * pool)73 void vp9_free_ref_frame_buffers(BufferPool *pool) {
74 int i;
75
76 for (i = 0; i < FRAME_BUFFERS; ++i) {
77 if (!pool->frame_bufs[i].released &&
78 pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
79 pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
80 pool->frame_bufs[i].ref_count = 0;
81 pool->frame_bufs[i].released = 1;
82 }
83 vpx_free(pool->frame_bufs[i].mvs);
84 pool->frame_bufs[i].mvs = NULL;
85 vpx_free_frame_buffer(&pool->frame_bufs[i].buf);
86 }
87 }
88
vp9_free_postproc_buffers(VP9_COMMON * cm)89 void vp9_free_postproc_buffers(VP9_COMMON *cm) {
90 #if CONFIG_VP9_POSTPROC
91 vpx_free_frame_buffer(&cm->post_proc_buffer);
92 vpx_free_frame_buffer(&cm->post_proc_buffer_int);
93 vpx_free(cm->postproc_state.limits);
94 cm->postproc_state.limits = NULL;
95 vpx_free(cm->postproc_state.generated_noise);
96 cm->postproc_state.generated_noise = NULL;
97 #else
98 (void)cm;
99 #endif
100 }
101
vp9_free_context_buffers(VP9_COMMON * cm)102 void vp9_free_context_buffers(VP9_COMMON *cm) {
103 cm->free_mi(cm);
104 free_seg_map(cm);
105 vpx_free(cm->above_context);
106 cm->above_context = NULL;
107 vpx_free(cm->above_seg_context);
108 cm->above_seg_context = NULL;
109 vpx_free(cm->lf.lfm);
110 cm->lf.lfm = NULL;
111 }
112
vp9_alloc_loop_filter(VP9_COMMON * cm)113 int vp9_alloc_loop_filter(VP9_COMMON *cm) {
114 vpx_free(cm->lf.lfm);
115 // Each lfm holds bit masks for all the 8x8 blocks in a 64x64 region. The
116 // stride and rows are rounded up / truncated to a multiple of 8.
117 cm->lf.lfm_stride = (cm->mi_cols + (MI_BLOCK_SIZE - 1)) >> 3;
118 cm->lf.lfm = (LOOP_FILTER_MASK *)vpx_calloc(
119 ((cm->mi_rows + (MI_BLOCK_SIZE - 1)) >> 3) * cm->lf.lfm_stride,
120 sizeof(*cm->lf.lfm));
121 if (!cm->lf.lfm) return 1;
122 return 0;
123 }
124
vp9_alloc_context_buffers(VP9_COMMON * cm,int width,int height)125 int vp9_alloc_context_buffers(VP9_COMMON *cm, int width, int height) {
126 int new_mi_size;
127
128 vp9_set_mb_mi(cm, width, height);
129 new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
130 if (cm->mi_alloc_size < new_mi_size) {
131 cm->free_mi(cm);
132 if (cm->alloc_mi(cm, new_mi_size)) goto fail;
133 }
134
135 if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
136 // Create the segmentation map structure and set to 0.
137 free_seg_map(cm);
138 if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
139 }
140
141 if (cm->above_context_alloc_cols < cm->mi_cols) {
142 vpx_free(cm->above_context);
143 cm->above_context = (ENTROPY_CONTEXT *)vpx_calloc(
144 2 * mi_cols_aligned_to_sb(cm->mi_cols) * MAX_MB_PLANE,
145 sizeof(*cm->above_context));
146 if (!cm->above_context) goto fail;
147
148 vpx_free(cm->above_seg_context);
149 cm->above_seg_context = (PARTITION_CONTEXT *)vpx_calloc(
150 mi_cols_aligned_to_sb(cm->mi_cols), sizeof(*cm->above_seg_context));
151 if (!cm->above_seg_context) goto fail;
152 cm->above_context_alloc_cols = cm->mi_cols;
153 }
154
155 if (vp9_alloc_loop_filter(cm)) goto fail;
156
157 return 0;
158
159 fail:
160 // clear the mi_* values to force a realloc on resync
161 vp9_set_mb_mi(cm, 0, 0);
162 vp9_free_context_buffers(cm);
163 return 1;
164 }
165
vp9_remove_common(VP9_COMMON * cm)166 void vp9_remove_common(VP9_COMMON *cm) {
167 #if CONFIG_VP9_POSTPROC
168 vp9_free_postproc_buffers(cm);
169 #endif
170 vp9_free_context_buffers(cm);
171
172 vpx_free(cm->fc);
173 cm->fc = NULL;
174 vpx_free(cm->frame_contexts);
175 cm->frame_contexts = NULL;
176 }
177
vp9_init_context_buffers(VP9_COMMON * cm)178 void vp9_init_context_buffers(VP9_COMMON *cm) {
179 cm->setup_mi(cm);
180 if (cm->last_frame_seg_map)
181 memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
182 }
183
vp9_swap_current_and_last_seg_map(VP9_COMMON * cm)184 void vp9_swap_current_and_last_seg_map(VP9_COMMON *cm) {
185 // Swap indices.
186 const int tmp = cm->seg_map_idx;
187 cm->seg_map_idx = cm->prev_seg_map_idx;
188 cm->prev_seg_map_idx = tmp;
189
190 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
191 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
192 }
193