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