Coverage Report

Created: 2026-03-31 06:59

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