/src/aom/av1/common/alloccommon.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2016, Alliance for Open Media. All rights reserved |
4 | | * |
5 | | * This source code is subject to the terms of the BSD 2 Clause License and |
6 | | * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License |
7 | | * was not distributed with this source code in the LICENSE file, you can |
8 | | * obtain it at www.aomedia.org/license/software. If the Alliance for Open |
9 | | * Media Patent License 1.0 was not distributed with this source code in the |
10 | | * PATENTS file, you can obtain it at www.aomedia.org/license/patent. |
11 | | */ |
12 | | |
13 | | #include "config/aom_config.h" |
14 | | |
15 | | #include "aom_mem/aom_mem.h" |
16 | | |
17 | | #include "av1/common/alloccommon.h" |
18 | | #include "av1/common/av1_common_int.h" |
19 | | #include "av1/common/blockd.h" |
20 | | #include "av1/common/cdef_block.h" |
21 | | #include "av1/common/entropymode.h" |
22 | | #include "av1/common/entropymv.h" |
23 | | #include "av1/common/thread_common.h" |
24 | | |
25 | 10.3k | int av1_get_MBs(int width, int height) { |
26 | 10.3k | const int aligned_width = ALIGN_POWER_OF_TWO(width, 3); |
27 | 10.3k | const int aligned_height = ALIGN_POWER_OF_TWO(height, 3); |
28 | 10.3k | const int mi_cols = aligned_width >> MI_SIZE_LOG2; |
29 | 10.3k | const int mi_rows = aligned_height >> MI_SIZE_LOG2; |
30 | | |
31 | 10.3k | const int mb_cols = (mi_cols + 2) >> 2; |
32 | 10.3k | const int mb_rows = (mi_rows + 2) >> 2; |
33 | 10.3k | return mb_rows * mb_cols; |
34 | 10.3k | } |
35 | | |
36 | 3.25k | void av1_free_ref_frame_buffers(BufferPool *pool) { |
37 | 3.25k | int i; |
38 | | |
39 | 55.2k | for (i = 0; i < FRAME_BUFFERS; ++i) { |
40 | 52.0k | if (pool->frame_bufs[i].ref_count > 0 && |
41 | 52.0k | pool->frame_bufs[i].raw_frame_buffer.data != NULL) { |
42 | 12 | pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer); |
43 | 12 | pool->frame_bufs[i].raw_frame_buffer.data = NULL; |
44 | 12 | pool->frame_bufs[i].raw_frame_buffer.size = 0; |
45 | 12 | pool->frame_bufs[i].raw_frame_buffer.priv = NULL; |
46 | 12 | pool->frame_bufs[i].ref_count = 0; |
47 | 12 | } |
48 | 52.0k | aom_free(pool->frame_bufs[i].mvs); |
49 | 52.0k | pool->frame_bufs[i].mvs = NULL; |
50 | 52.0k | aom_free(pool->frame_bufs[i].seg_map); |
51 | 52.0k | pool->frame_bufs[i].seg_map = NULL; |
52 | 52.0k | aom_free_frame_buffer(&pool->frame_bufs[i].buf); |
53 | 52.0k | } |
54 | 3.25k | } |
55 | | |
56 | | static INLINE void free_cdef_linebuf_conditional( |
57 | 2.53k | AV1_COMMON *const cm, const size_t *new_linebuf_size) { |
58 | 2.53k | CdefInfo *cdef_info = &cm->cdef_info; |
59 | 10.1k | for (int plane = 0; plane < MAX_MB_PLANE; plane++) { |
60 | 7.60k | if (new_linebuf_size[plane] != cdef_info->allocated_linebuf_size[plane]) { |
61 | 30 | aom_free(cdef_info->linebuf[plane]); |
62 | 30 | cdef_info->linebuf[plane] = NULL; |
63 | 30 | } |
64 | 7.60k | } |
65 | 2.53k | } |
66 | | |
67 | | static INLINE void free_cdef_bufs_conditional(AV1_COMMON *const cm, |
68 | | uint16_t **colbuf, |
69 | | uint16_t **srcbuf, |
70 | | const size_t *new_colbuf_size, |
71 | 2.53k | const size_t new_srcbuf_size) { |
72 | 2.53k | CdefInfo *cdef_info = &cm->cdef_info; |
73 | 2.53k | if (new_srcbuf_size != cdef_info->allocated_srcbuf_size) { |
74 | 10 | aom_free(*srcbuf); |
75 | 10 | *srcbuf = NULL; |
76 | 10 | } |
77 | 10.1k | for (int plane = 0; plane < MAX_MB_PLANE; plane++) { |
78 | 7.60k | if (new_colbuf_size[plane] != cdef_info->allocated_colbuf_size[plane]) { |
79 | 30 | aom_free(colbuf[plane]); |
80 | 30 | colbuf[plane] = NULL; |
81 | 30 | } |
82 | 7.60k | } |
83 | 2.53k | } |
84 | | |
85 | 3.25k | static INLINE void free_cdef_bufs(uint16_t **colbuf, uint16_t **srcbuf) { |
86 | 3.25k | aom_free(*srcbuf); |
87 | 3.25k | *srcbuf = NULL; |
88 | 13.0k | for (int plane = 0; plane < MAX_MB_PLANE; plane++) { |
89 | 9.75k | aom_free(colbuf[plane]); |
90 | 9.75k | colbuf[plane] = NULL; |
91 | 9.75k | } |
92 | 3.25k | } |
93 | | |
94 | | static INLINE void free_cdef_row_sync(AV1CdefRowSync **cdef_row_mt, |
95 | 1.90k | const int num_mi_rows) { |
96 | 1.90k | if (*cdef_row_mt == NULL) return; |
97 | 0 | #if CONFIG_MULTITHREAD |
98 | 0 | for (int row_idx = 0; row_idx < num_mi_rows; row_idx++) { |
99 | 0 | pthread_mutex_destroy((*cdef_row_mt)[row_idx].row_mutex_); |
100 | 0 | pthread_cond_destroy((*cdef_row_mt)[row_idx].row_cond_); |
101 | 0 | aom_free((*cdef_row_mt)[row_idx].row_mutex_); |
102 | 0 | aom_free((*cdef_row_mt)[row_idx].row_cond_); |
103 | 0 | } |
104 | | #else |
105 | | (void)num_mi_rows; |
106 | | #endif // CONFIG_MULTITHREAD |
107 | 0 | aom_free(*cdef_row_mt); |
108 | 0 | *cdef_row_mt = NULL; |
109 | 0 | } |
110 | | |
111 | | void av1_free_cdef_buffers(AV1_COMMON *const cm, |
112 | | AV1CdefWorkerData **cdef_worker, |
113 | 3.25k | AV1CdefSync *cdef_sync, int num_workers) { |
114 | 3.25k | CdefInfo *cdef_info = &cm->cdef_info; |
115 | 3.25k | const int num_mi_rows = cdef_info->allocated_mi_rows; |
116 | | |
117 | 13.0k | for (int plane = 0; plane < MAX_MB_PLANE; plane++) { |
118 | 9.75k | aom_free(cdef_info->linebuf[plane]); |
119 | 9.75k | cdef_info->linebuf[plane] = NULL; |
120 | 9.75k | } |
121 | | // De-allocation of column buffer & source buffer (worker_0). |
122 | 3.25k | free_cdef_bufs(cdef_info->colbuf, &cdef_info->srcbuf); |
123 | | |
124 | 3.25k | if (num_workers < 2) return; |
125 | 628 | if (*cdef_worker != NULL) { |
126 | 0 | for (int idx = num_workers - 1; idx >= 1; idx--) { |
127 | | // De-allocation of column buffer & source buffer for remaining workers. |
128 | 0 | free_cdef_bufs((*cdef_worker)[idx].colbuf, &(*cdef_worker)[idx].srcbuf); |
129 | 0 | } |
130 | 0 | aom_free(*cdef_worker); |
131 | 0 | *cdef_worker = NULL; |
132 | 0 | } |
133 | 628 | free_cdef_row_sync(&cdef_sync->cdef_row_mt, num_mi_rows); |
134 | 628 | } |
135 | | |
136 | | static INLINE void alloc_cdef_linebuf(AV1_COMMON *const cm, uint16_t **linebuf, |
137 | 10 | const int num_planes) { |
138 | 10 | CdefInfo *cdef_info = &cm->cdef_info; |
139 | 40 | for (int plane = 0; plane < num_planes; plane++) { |
140 | 30 | if (linebuf[plane] == NULL) |
141 | 30 | CHECK_MEM_ERROR(cm, linebuf[plane], |
142 | 30 | aom_malloc(cdef_info->allocated_linebuf_size[plane])); |
143 | 30 | } |
144 | 10 | } |
145 | | |
146 | | static INLINE void alloc_cdef_bufs(AV1_COMMON *const cm, uint16_t **colbuf, |
147 | 10 | uint16_t **srcbuf, const int num_planes) { |
148 | 10 | CdefInfo *cdef_info = &cm->cdef_info; |
149 | 10 | if (*srcbuf == NULL) |
150 | 10 | CHECK_MEM_ERROR(cm, *srcbuf, |
151 | 10 | aom_memalign(16, cdef_info->allocated_srcbuf_size)); |
152 | | |
153 | 40 | for (int plane = 0; plane < num_planes; plane++) { |
154 | 30 | if (colbuf[plane] == NULL) |
155 | 30 | CHECK_MEM_ERROR(cm, colbuf[plane], |
156 | 30 | aom_malloc(cdef_info->allocated_colbuf_size[plane])); |
157 | 30 | } |
158 | 10 | } |
159 | | |
160 | | static INLINE void alloc_cdef_row_sync(AV1_COMMON *const cm, |
161 | | AV1CdefRowSync **cdef_row_mt, |
162 | 0 | const int num_mi_rows) { |
163 | 0 | if (*cdef_row_mt != NULL) return; |
164 | | |
165 | 0 | CHECK_MEM_ERROR(cm, *cdef_row_mt, |
166 | 0 | aom_malloc(sizeof(**cdef_row_mt) * num_mi_rows)); |
167 | 0 | #if CONFIG_MULTITHREAD |
168 | 0 | for (int row_idx = 0; row_idx < num_mi_rows; row_idx++) { |
169 | 0 | CHECK_MEM_ERROR(cm, (*cdef_row_mt)[row_idx].row_mutex_, |
170 | 0 | aom_malloc(sizeof(*(*cdef_row_mt)[row_idx].row_mutex_))); |
171 | 0 | pthread_mutex_init((*cdef_row_mt)[row_idx].row_mutex_, NULL); |
172 | |
|
173 | 0 | CHECK_MEM_ERROR(cm, (*cdef_row_mt)[row_idx].row_cond_, |
174 | 0 | aom_malloc(sizeof(*(*cdef_row_mt)[row_idx].row_cond_))); |
175 | 0 | pthread_cond_init((*cdef_row_mt)[row_idx].row_cond_, NULL); |
176 | |
|
177 | 0 | (*cdef_row_mt)[row_idx].is_row_done = 0; |
178 | 0 | } |
179 | 0 | #endif // CONFIG_MULTITHREAD |
180 | 0 | } |
181 | | |
182 | | void av1_alloc_cdef_buffers(AV1_COMMON *const cm, |
183 | | AV1CdefWorkerData **cdef_worker, |
184 | | AV1CdefSync *cdef_sync, int num_workers, |
185 | 2.53k | int init_worker) { |
186 | 2.53k | const int num_planes = av1_num_planes(cm); |
187 | 2.53k | size_t new_linebuf_size[MAX_MB_PLANE] = { 0 }; |
188 | 2.53k | size_t new_colbuf_size[MAX_MB_PLANE] = { 0 }; |
189 | 2.53k | size_t new_srcbuf_size = 0; |
190 | 2.53k | CdefInfo *const cdef_info = &cm->cdef_info; |
191 | | // Check for configuration change |
192 | 2.53k | const int num_mi_rows = |
193 | 2.53k | (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64; |
194 | 2.53k | const int is_num_workers_changed = |
195 | 2.53k | cdef_info->allocated_num_workers != num_workers; |
196 | 2.53k | const int is_cdef_enabled = |
197 | 2.53k | cm->seq_params->enable_cdef && !cm->tiles.large_scale; |
198 | | |
199 | | // num-bufs=3 represents ping-pong buffers for top linebuf, |
200 | | // followed by bottom linebuf. |
201 | | // ping-pong is to avoid top linebuf over-write by consecutive row. |
202 | 2.53k | int num_bufs = 3; |
203 | 2.53k | if (num_workers > 1) |
204 | 1.25k | num_bufs = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64; |
205 | | |
206 | 2.53k | if (is_cdef_enabled) { |
207 | | // Calculate src buffer size |
208 | 10 | new_srcbuf_size = sizeof(*cdef_info->srcbuf) * CDEF_INBUF_SIZE; |
209 | 40 | for (int plane = 0; plane < num_planes; plane++) { |
210 | 30 | const int shift = |
211 | 30 | plane == AOM_PLANE_Y ? 0 : cm->seq_params->subsampling_x; |
212 | | // Calculate top and bottom line buffer size |
213 | 30 | const int luma_stride = |
214 | 30 | ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols << MI_SIZE_LOG2, 4); |
215 | 30 | new_linebuf_size[plane] = sizeof(*cdef_info->linebuf) * num_bufs * |
216 | 30 | (CDEF_VBORDER << 1) * (luma_stride >> shift); |
217 | | // Calculate column buffer size |
218 | 30 | const int block_height = |
219 | 30 | (CDEF_BLOCKSIZE << (MI_SIZE_LOG2 - shift)) * 2 * CDEF_VBORDER; |
220 | 30 | new_colbuf_size[plane] = |
221 | 30 | sizeof(*cdef_info->colbuf[plane]) * block_height * CDEF_HBORDER; |
222 | 30 | } |
223 | 10 | } |
224 | | |
225 | | // Free src, line and column buffers for worker 0 in case of reallocation |
226 | 2.53k | free_cdef_linebuf_conditional(cm, new_linebuf_size); |
227 | 2.53k | free_cdef_bufs_conditional(cm, cdef_info->colbuf, &cdef_info->srcbuf, |
228 | 2.53k | new_colbuf_size, new_srcbuf_size); |
229 | | |
230 | | // The flag init_worker indicates if cdef_worker has to be allocated for the |
231 | | // frame. This is passed as 1 always from decoder. At encoder side, it is 0 |
232 | | // when called for parallel frames during FPMT (where cdef_worker is shared |
233 | | // across parallel frames) and 1 otherwise. |
234 | 2.53k | if (*cdef_worker != NULL && init_worker) { |
235 | 0 | if (is_num_workers_changed) { |
236 | | // Free src and column buffers for remaining workers in case of change in |
237 | | // num_workers |
238 | 0 | for (int idx = cdef_info->allocated_num_workers - 1; idx >= 1; idx--) |
239 | 0 | free_cdef_bufs((*cdef_worker)[idx].colbuf, &(*cdef_worker)[idx].srcbuf); |
240 | 0 | } else if (num_workers > 1) { |
241 | | // Free src and column buffers for remaining workers in case of |
242 | | // reallocation |
243 | 0 | for (int idx = num_workers - 1; idx >= 1; idx--) |
244 | 0 | free_cdef_bufs_conditional(cm, (*cdef_worker)[idx].colbuf, |
245 | 0 | &(*cdef_worker)[idx].srcbuf, new_colbuf_size, |
246 | 0 | new_srcbuf_size); |
247 | 0 | } |
248 | 0 | } |
249 | | |
250 | 2.53k | if (cdef_info->allocated_mi_rows != num_mi_rows) |
251 | 1.27k | free_cdef_row_sync(&cdef_sync->cdef_row_mt, cdef_info->allocated_mi_rows); |
252 | | |
253 | | // Store allocated sizes for reallocation |
254 | 2.53k | cdef_info->allocated_srcbuf_size = new_srcbuf_size; |
255 | 2.53k | av1_copy(cdef_info->allocated_colbuf_size, new_colbuf_size); |
256 | 2.53k | av1_copy(cdef_info->allocated_linebuf_size, new_linebuf_size); |
257 | | // Store configuration to check change in configuration |
258 | 2.53k | cdef_info->allocated_mi_rows = num_mi_rows; |
259 | 2.53k | cdef_info->allocated_num_workers = num_workers; |
260 | | |
261 | 2.53k | if (!is_cdef_enabled) return; |
262 | | |
263 | | // Memory allocation of column buffer & source buffer (worker_0). |
264 | 10 | alloc_cdef_bufs(cm, cdef_info->colbuf, &cdef_info->srcbuf, num_planes); |
265 | 10 | alloc_cdef_linebuf(cm, cdef_info->linebuf, num_planes); |
266 | | |
267 | 10 | if (num_workers < 2) return; |
268 | | |
269 | 0 | if (init_worker) { |
270 | 0 | if (*cdef_worker == NULL) |
271 | 0 | CHECK_MEM_ERROR(cm, *cdef_worker, |
272 | 0 | aom_calloc(num_workers, sizeof(**cdef_worker))); |
273 | | |
274 | | // Memory allocation of column buffer & source buffer for remaining workers. |
275 | 0 | for (int idx = num_workers - 1; idx >= 1; idx--) |
276 | 0 | alloc_cdef_bufs(cm, (*cdef_worker)[idx].colbuf, |
277 | 0 | &(*cdef_worker)[idx].srcbuf, num_planes); |
278 | 0 | } |
279 | |
|
280 | 0 | alloc_cdef_row_sync(cm, &cdef_sync->cdef_row_mt, |
281 | 0 | cdef_info->allocated_mi_rows); |
282 | 0 | } |
283 | | |
284 | | #if !CONFIG_REALTIME_ONLY |
285 | | // Assumes cm->rst_info[p].restoration_unit_size is already initialized |
286 | 426 | void av1_alloc_restoration_buffers(AV1_COMMON *cm) { |
287 | 426 | const int num_planes = av1_num_planes(cm); |
288 | 1.70k | for (int p = 0; p < num_planes; ++p) |
289 | 1.27k | av1_alloc_restoration_struct(cm, &cm->rst_info[p], p > 0); |
290 | | |
291 | 426 | if (cm->rst_tmpbuf == NULL) { |
292 | 426 | CHECK_MEM_ERROR(cm, cm->rst_tmpbuf, |
293 | 426 | (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE)); |
294 | 426 | } |
295 | | |
296 | 426 | if (cm->rlbs == NULL) { |
297 | 426 | CHECK_MEM_ERROR(cm, cm->rlbs, aom_malloc(sizeof(RestorationLineBuffers))); |
298 | 426 | } |
299 | | |
300 | | // For striped loop restoration, we divide each row of tiles into "stripes", |
301 | | // of height 64 luma pixels but with an offset by RESTORATION_UNIT_OFFSET |
302 | | // luma pixels to match the output from CDEF. We will need to store 2 * |
303 | | // RESTORATION_CTX_VERT lines of data for each stripe, and also need to be |
304 | | // able to quickly answer the question "Where is the <n>'th stripe for tile |
305 | | // row <m>?" To make that efficient, we generate the rst_last_stripe array. |
306 | 426 | int num_stripes = 0; |
307 | 884 | for (int i = 0; i < cm->tiles.rows; ++i) { |
308 | 458 | TileInfo tile_info; |
309 | 458 | av1_tile_set_row(&tile_info, cm, i); |
310 | 458 | const int mi_h = tile_info.mi_row_end - tile_info.mi_row_start; |
311 | 458 | const int ext_h = RESTORATION_UNIT_OFFSET + (mi_h << MI_SIZE_LOG2); |
312 | 458 | const int tile_stripes = (ext_h + 63) / 64; |
313 | 458 | num_stripes += tile_stripes; |
314 | 458 | } |
315 | | |
316 | | // Now we need to allocate enough space to store the line buffers for the |
317 | | // stripes |
318 | 426 | const int frame_w = cm->superres_upscaled_width; |
319 | 426 | const int use_highbd = cm->seq_params->use_highbitdepth; |
320 | | |
321 | 1.70k | for (int p = 0; p < num_planes; ++p) { |
322 | 1.27k | const int is_uv = p > 0; |
323 | 1.27k | const int ss_x = is_uv && cm->seq_params->subsampling_x; |
324 | 1.27k | const int plane_w = ((frame_w + ss_x) >> ss_x) + 2 * RESTORATION_EXTRA_HORZ; |
325 | 1.27k | const int stride = ALIGN_POWER_OF_TWO(plane_w, 5); |
326 | 1.27k | const int buf_size = num_stripes * stride * RESTORATION_CTX_VERT |
327 | 1.27k | << use_highbd; |
328 | 1.27k | RestorationStripeBoundaries *boundaries = &cm->rst_info[p].boundaries; |
329 | | |
330 | 1.27k | if (buf_size != boundaries->stripe_boundary_size || |
331 | 1.27k | boundaries->stripe_boundary_above == NULL || |
332 | 1.27k | boundaries->stripe_boundary_below == NULL) { |
333 | 1.27k | aom_free(boundaries->stripe_boundary_above); |
334 | 1.27k | aom_free(boundaries->stripe_boundary_below); |
335 | | |
336 | 1.27k | CHECK_MEM_ERROR(cm, boundaries->stripe_boundary_above, |
337 | 1.27k | (uint8_t *)aom_memalign(32, buf_size)); |
338 | 1.27k | CHECK_MEM_ERROR(cm, boundaries->stripe_boundary_below, |
339 | 1.27k | (uint8_t *)aom_memalign(32, buf_size)); |
340 | | |
341 | 1.27k | boundaries->stripe_boundary_size = buf_size; |
342 | 1.27k | } |
343 | 1.27k | boundaries->stripe_boundary_stride = stride; |
344 | 1.27k | } |
345 | 426 | } |
346 | | |
347 | 3.25k | void av1_free_restoration_buffers(AV1_COMMON *cm) { |
348 | 3.25k | int p; |
349 | 13.0k | for (p = 0; p < MAX_MB_PLANE; ++p) |
350 | 9.75k | av1_free_restoration_struct(&cm->rst_info[p]); |
351 | 3.25k | aom_free(cm->rst_tmpbuf); |
352 | 3.25k | cm->rst_tmpbuf = NULL; |
353 | 3.25k | aom_free(cm->rlbs); |
354 | 3.25k | cm->rlbs = NULL; |
355 | 13.0k | for (p = 0; p < MAX_MB_PLANE; ++p) { |
356 | 9.75k | RestorationStripeBoundaries *boundaries = &cm->rst_info[p].boundaries; |
357 | 9.75k | aom_free(boundaries->stripe_boundary_above); |
358 | 9.75k | aom_free(boundaries->stripe_boundary_below); |
359 | 9.75k | boundaries->stripe_boundary_above = NULL; |
360 | 9.75k | boundaries->stripe_boundary_below = NULL; |
361 | 9.75k | } |
362 | | |
363 | 3.25k | aom_free_frame_buffer(&cm->rst_frame); |
364 | 3.25k | } |
365 | | #endif // !CONFIG_REALTIME_ONLY |
366 | | |
367 | 9.02k | void av1_free_above_context_buffers(CommonContexts *above_contexts) { |
368 | 9.02k | int i; |
369 | 9.02k | const int num_planes = above_contexts->num_planes; |
370 | | |
371 | 12.4k | for (int tile_row = 0; tile_row < above_contexts->num_tile_rows; tile_row++) { |
372 | 13.7k | for (i = 0; i < num_planes; i++) { |
373 | 10.2k | aom_free(above_contexts->entropy[i][tile_row]); |
374 | 10.2k | above_contexts->entropy[i][tile_row] = NULL; |
375 | 10.2k | } |
376 | 3.42k | aom_free(above_contexts->partition[tile_row]); |
377 | 3.42k | above_contexts->partition[tile_row] = NULL; |
378 | | |
379 | 3.42k | aom_free(above_contexts->txfm[tile_row]); |
380 | 3.42k | above_contexts->txfm[tile_row] = NULL; |
381 | 3.42k | } |
382 | 18.7k | for (i = 0; i < num_planes; i++) { |
383 | 9.73k | aom_free(above_contexts->entropy[i]); |
384 | 9.73k | above_contexts->entropy[i] = NULL; |
385 | 9.73k | } |
386 | 9.02k | aom_free(above_contexts->partition); |
387 | 9.02k | above_contexts->partition = NULL; |
388 | | |
389 | 9.02k | aom_free(above_contexts->txfm); |
390 | 9.02k | above_contexts->txfm = NULL; |
391 | | |
392 | 9.02k | above_contexts->num_tile_rows = 0; |
393 | 9.02k | above_contexts->num_mi_cols = 0; |
394 | 9.02k | above_contexts->num_planes = 0; |
395 | 9.02k | } |
396 | | |
397 | 5.77k | void av1_free_context_buffers(AV1_COMMON *cm) { |
398 | 5.77k | cm->mi_params.free_mi(&cm->mi_params); |
399 | | |
400 | 5.77k | av1_free_above_context_buffers(&cm->above_contexts); |
401 | 5.77k | } |
402 | | |
403 | | int av1_alloc_above_context_buffers(CommonContexts *above_contexts, |
404 | | int num_tile_rows, int num_mi_cols, |
405 | 3.24k | int num_planes) { |
406 | 3.24k | const int aligned_mi_cols = |
407 | 3.24k | ALIGN_POWER_OF_TWO(num_mi_cols, MAX_MIB_SIZE_LOG2); |
408 | | |
409 | | // Allocate above context buffers |
410 | 3.24k | above_contexts->num_tile_rows = num_tile_rows; |
411 | 3.24k | above_contexts->num_mi_cols = aligned_mi_cols; |
412 | 3.24k | above_contexts->num_planes = num_planes; |
413 | 12.9k | for (int plane_idx = 0; plane_idx < num_planes; plane_idx++) { |
414 | 9.73k | above_contexts->entropy[plane_idx] = (ENTROPY_CONTEXT **)aom_calloc( |
415 | 9.73k | num_tile_rows, sizeof(above_contexts->entropy[0])); |
416 | 9.73k | if (!above_contexts->entropy[plane_idx]) return 1; |
417 | 9.73k | } |
418 | | |
419 | 3.24k | above_contexts->partition = (PARTITION_CONTEXT **)aom_calloc( |
420 | 3.24k | num_tile_rows, sizeof(above_contexts->partition)); |
421 | 3.24k | if (!above_contexts->partition) return 1; |
422 | | |
423 | 3.24k | above_contexts->txfm = |
424 | 3.24k | (TXFM_CONTEXT **)aom_calloc(num_tile_rows, sizeof(above_contexts->txfm)); |
425 | 3.24k | if (!above_contexts->txfm) return 1; |
426 | | |
427 | 6.67k | for (int tile_row = 0; tile_row < num_tile_rows; tile_row++) { |
428 | 13.7k | for (int plane_idx = 0; plane_idx < num_planes; plane_idx++) { |
429 | 10.2k | above_contexts->entropy[plane_idx][tile_row] = |
430 | 10.2k | (ENTROPY_CONTEXT *)aom_calloc( |
431 | 10.2k | aligned_mi_cols, sizeof(*above_contexts->entropy[0][tile_row])); |
432 | 10.2k | if (!above_contexts->entropy[plane_idx][tile_row]) return 1; |
433 | 10.2k | } |
434 | | |
435 | 3.42k | above_contexts->partition[tile_row] = (PARTITION_CONTEXT *)aom_calloc( |
436 | 3.42k | aligned_mi_cols, sizeof(*above_contexts->partition[tile_row])); |
437 | 3.42k | if (!above_contexts->partition[tile_row]) return 1; |
438 | | |
439 | 3.42k | above_contexts->txfm[tile_row] = (TXFM_CONTEXT *)aom_calloc( |
440 | 3.42k | aligned_mi_cols, sizeof(*above_contexts->txfm[tile_row])); |
441 | 3.42k | if (!above_contexts->txfm[tile_row]) return 1; |
442 | 3.42k | } |
443 | | |
444 | 3.24k | return 0; |
445 | 3.24k | } |
446 | | |
447 | | // Allocate the dynamically allocated arrays in 'mi_params' assuming |
448 | | // 'mi_params->set_mb_mi()' was already called earlier to initialize the rest of |
449 | | // the struct members. |
450 | 3.24k | static int alloc_mi(CommonModeInfoParams *mi_params) { |
451 | 3.24k | const int aligned_mi_rows = calc_mi_size(mi_params->mi_rows); |
452 | 3.24k | const int mi_grid_size = mi_params->mi_stride * aligned_mi_rows; |
453 | 3.24k | const int alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize]; |
454 | 3.24k | const int alloc_mi_size = |
455 | 3.24k | mi_params->mi_alloc_stride * (aligned_mi_rows / alloc_size_1d); |
456 | | |
457 | 3.24k | if (mi_params->mi_alloc_size < alloc_mi_size || |
458 | 3.24k | mi_params->mi_grid_size < mi_grid_size) { |
459 | 3.24k | mi_params->free_mi(mi_params); |
460 | | |
461 | 3.24k | mi_params->mi_alloc = |
462 | 3.24k | aom_calloc(alloc_mi_size, sizeof(*mi_params->mi_alloc)); |
463 | 3.24k | if (!mi_params->mi_alloc) return 1; |
464 | 3.24k | mi_params->mi_alloc_size = alloc_mi_size; |
465 | | |
466 | 3.24k | mi_params->mi_grid_base = (MB_MODE_INFO **)aom_calloc( |
467 | 3.24k | mi_grid_size, sizeof(*mi_params->mi_grid_base)); |
468 | 3.24k | if (!mi_params->mi_grid_base) return 1; |
469 | 3.24k | mi_params->mi_grid_size = mi_grid_size; |
470 | | |
471 | 3.24k | mi_params->tx_type_map = |
472 | 3.24k | aom_calloc(mi_grid_size, sizeof(*mi_params->tx_type_map)); |
473 | 3.24k | if (!mi_params->tx_type_map) return 1; |
474 | 3.24k | } |
475 | | |
476 | 3.24k | return 0; |
477 | 3.24k | } |
478 | | |
479 | | int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height, int mode, |
480 | 3.24k | BLOCK_SIZE min_partition_size) { |
481 | 3.24k | CommonModeInfoParams *const mi_params = &cm->mi_params; |
482 | 3.24k | mi_params->set_mb_mi(mi_params, width, height, mode, min_partition_size); |
483 | 3.24k | if (alloc_mi(mi_params)) goto fail; |
484 | 3.24k | return 0; |
485 | | |
486 | 0 | fail: |
487 | | // clear the mi_* values to force a realloc on resync |
488 | 0 | mi_params->set_mb_mi(mi_params, 0, 0, 0, BLOCK_4X4); |
489 | 0 | av1_free_context_buffers(cm); |
490 | 0 | return 1; |
491 | 3.24k | } |
492 | | |
493 | 3.25k | void av1_remove_common(AV1_COMMON *cm) { |
494 | 3.25k | av1_free_context_buffers(cm); |
495 | | |
496 | 3.25k | aom_free(cm->fc); |
497 | 3.25k | cm->fc = NULL; |
498 | 3.25k | aom_free(cm->default_frame_context); |
499 | 3.25k | cm->default_frame_context = NULL; |
500 | 3.25k | } |
501 | | |
502 | 1.98k | void av1_init_mi_buffers(CommonModeInfoParams *mi_params) { |
503 | 1.98k | mi_params->setup_mi(mi_params); |
504 | 1.98k | } |