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