Coverage Report

Created: 2025-07-23 08:18

/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 = (mi_cols + 2) >> 2;
32
0
  const int mb_rows = (mi_rows + 2) >> 2;
33
0
  return mb_rows * mb_cols;
34
0
}
35
36
28.6k
void av1_free_ref_frame_buffers(BufferPool *pool) {
37
28.6k
  int i;
38
39
486k
  for (i = 0; i < FRAME_BUFFERS; ++i) {
40
458k
    if (pool->frame_bufs[i].ref_count > 0 &&
41
458k
        pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
42
9.70k
      pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
43
9.70k
      pool->frame_bufs[i].raw_frame_buffer.data = NULL;
44
9.70k
      pool->frame_bufs[i].raw_frame_buffer.size = 0;
45
9.70k
      pool->frame_bufs[i].raw_frame_buffer.priv = NULL;
46
9.70k
      pool->frame_bufs[i].ref_count = 0;
47
9.70k
    }
48
458k
    aom_free(pool->frame_bufs[i].mvs);
49
458k
    pool->frame_bufs[i].mvs = NULL;
50
458k
    aom_free(pool->frame_bufs[i].seg_map);
51
458k
    pool->frame_bufs[i].seg_map = NULL;
52
458k
    aom_free_frame_buffer(&pool->frame_bufs[i].buf);
53
458k
  }
54
28.6k
}
55
56
static INLINE void free_cdef_linebuf_conditional(
57
7.64k
    AV1_COMMON *const cm, const size_t *new_linebuf_size) {
58
7.64k
  CdefInfo *cdef_info = &cm->cdef_info;
59
30.5k
  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
60
22.9k
    if (new_linebuf_size[plane] != cdef_info->allocated_linebuf_size[plane]) {
61
5.49k
      aom_free(cdef_info->linebuf[plane]);
62
5.49k
      cdef_info->linebuf[plane] = NULL;
63
5.49k
    }
64
22.9k
  }
65
7.64k
}
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
7.64k
                                              const size_t new_srcbuf_size) {
72
7.64k
  CdefInfo *cdef_info = &cm->cdef_info;
73
7.64k
  if (new_srcbuf_size != cdef_info->allocated_srcbuf_size) {
74
1.95k
    aom_free(*srcbuf);
75
1.95k
    *srcbuf = NULL;
76
1.95k
  }
77
30.5k
  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
78
22.9k
    if (new_colbuf_size[plane] != cdef_info->allocated_colbuf_size[plane]) {
79
5.49k
      aom_free(colbuf[plane]);
80
5.49k
      colbuf[plane] = NULL;
81
5.49k
    }
82
22.9k
  }
83
7.64k
}
84
85
28.6k
static INLINE void free_cdef_bufs(uint16_t **colbuf, uint16_t **srcbuf) {
86
28.6k
  aom_free(*srcbuf);
87
28.6k
  *srcbuf = NULL;
88
114k
  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
89
85.9k
    aom_free(colbuf[plane]);
90
85.9k
    colbuf[plane] = NULL;
91
85.9k
  }
92
28.6k
}
93
94
static INLINE void free_cdef_row_sync(AV1CdefRowSync **cdef_row_mt,
95
5.03k
                                      const int num_mi_rows) {
96
5.03k
  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
28.6k
                           AV1CdefSync *cdef_sync, int num_workers) {
114
28.6k
  CdefInfo *cdef_info = &cm->cdef_info;
115
28.6k
  const int num_mi_rows = cdef_info->allocated_mi_rows;
116
117
114k
  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
118
85.9k
    aom_free(cdef_info->linebuf[plane]);
119
85.9k
    cdef_info->linebuf[plane] = NULL;
120
85.9k
  }
121
  // De-allocation of column buffer & source buffer (worker_0).
122
28.6k
  free_cdef_bufs(cdef_info->colbuf, &cdef_info->srcbuf);
123
124
28.6k
  if (num_workers < 2) return;
125
0
  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
0
  free_cdef_row_sync(&cdef_sync->cdef_row_mt, num_mi_rows);
134
0
}
135
136
static INLINE void alloc_cdef_linebuf(AV1_COMMON *const cm, uint16_t **linebuf,
137
2.02k
                                      const int num_planes) {
138
2.02k
  CdefInfo *cdef_info = &cm->cdef_info;
139
7.66k
  for (int plane = 0; plane < num_planes; plane++) {
140
5.64k
    if (linebuf[plane] == NULL)
141
5.64k
      CHECK_MEM_ERROR(cm, linebuf[plane],
142
5.64k
                      aom_malloc(cdef_info->allocated_linebuf_size[plane]));
143
5.64k
  }
144
2.02k
}
145
146
static INLINE void alloc_cdef_bufs(AV1_COMMON *const cm, uint16_t **colbuf,
147
2.02k
                                   uint16_t **srcbuf, const int num_planes) {
148
2.02k
  CdefInfo *cdef_info = &cm->cdef_info;
149
2.02k
  if (*srcbuf == NULL)
150
2.02k
    CHECK_MEM_ERROR(cm, *srcbuf,
151
2.02k
                    aom_memalign(16, cdef_info->allocated_srcbuf_size));
152
153
7.66k
  for (int plane = 0; plane < num_planes; plane++) {
154
5.64k
    if (colbuf[plane] == NULL)
155
5.64k
      CHECK_MEM_ERROR(cm, colbuf[plane],
156
5.64k
                      aom_malloc(cdef_info->allocated_colbuf_size[plane]));
157
5.64k
  }
158
2.02k
}
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
7.64k
                            int init_worker) {
186
7.64k
  const int num_planes = av1_num_planes(cm);
187
7.64k
  size_t new_linebuf_size[MAX_MB_PLANE] = { 0 };
188
7.64k
  size_t new_colbuf_size[MAX_MB_PLANE] = { 0 };
189
7.64k
  size_t new_srcbuf_size = 0;
190
7.64k
  CdefInfo *const cdef_info = &cm->cdef_info;
191
  // Check for configuration change
192
7.64k
  const int num_mi_rows =
193
7.64k
      (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
194
7.64k
  const int is_num_workers_changed =
195
7.64k
      cdef_info->allocated_num_workers != num_workers;
196
7.64k
  const int is_cdef_enabled =
197
7.64k
      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
7.64k
  int num_bufs = 3;
203
7.64k
  if (num_workers > 1)
204
0
    num_bufs = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
205
206
7.64k
  if (is_cdef_enabled) {
207
    // Calculate src buffer size
208
2.02k
    new_srcbuf_size = sizeof(*cdef_info->srcbuf) * CDEF_INBUF_SIZE;
209
7.66k
    for (int plane = 0; plane < num_planes; plane++) {
210
5.64k
      const int shift =
211
5.64k
          plane == AOM_PLANE_Y ? 0 : cm->seq_params->subsampling_x;
212
      // Calculate top and bottom line buffer size
213
5.64k
      const int luma_stride =
214
5.64k
          ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols << MI_SIZE_LOG2, 4);
215
5.64k
      new_linebuf_size[plane] = sizeof(*cdef_info->linebuf) * num_bufs *
216
5.64k
                                (CDEF_VBORDER << 1) * (luma_stride >> shift);
217
      // Calculate column buffer size
218
5.64k
      const int block_height =
219
5.64k
          (CDEF_BLOCKSIZE << (MI_SIZE_LOG2 - shift)) * 2 * CDEF_VBORDER;
220
5.64k
      new_colbuf_size[plane] =
221
5.64k
          sizeof(*cdef_info->colbuf[plane]) * block_height * CDEF_HBORDER;
222
5.64k
    }
223
2.02k
  }
224
225
  // Free src, line and column buffers for worker 0 in case of reallocation
226
7.64k
  free_cdef_linebuf_conditional(cm, new_linebuf_size);
227
7.64k
  free_cdef_bufs_conditional(cm, cdef_info->colbuf, &cdef_info->srcbuf,
228
7.64k
                             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
7.64k
  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
7.64k
  if (cdef_info->allocated_mi_rows != num_mi_rows)
251
5.03k
    free_cdef_row_sync(&cdef_sync->cdef_row_mt, cdef_info->allocated_mi_rows);
252
253
  // Store allocated sizes for reallocation
254
7.64k
  cdef_info->allocated_srcbuf_size = new_srcbuf_size;
255
7.64k
  av1_copy(cdef_info->allocated_colbuf_size, new_colbuf_size);
256
7.64k
  av1_copy(cdef_info->allocated_linebuf_size, new_linebuf_size);
257
  // Store configuration to check change in configuration
258
7.64k
  cdef_info->allocated_mi_rows = num_mi_rows;
259
7.64k
  cdef_info->allocated_num_workers = num_workers;
260
261
7.64k
  if (!is_cdef_enabled) return;
262
263
  // Memory allocation of column buffer & source buffer (worker_0).
264
2.02k
  alloc_cdef_bufs(cm, cdef_info->colbuf, &cdef_info->srcbuf, num_planes);
265
2.02k
  alloc_cdef_linebuf(cm, cdef_info->linebuf, num_planes);
266
267
2.02k
  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
5.13k
void av1_alloc_restoration_buffers(AV1_COMMON *cm) {
287
5.13k
  const int num_planes = av1_num_planes(cm);
288
20.2k
  for (int p = 0; p < num_planes; ++p)
289
15.1k
    av1_alloc_restoration_struct(cm, &cm->rst_info[p], p > 0);
290
291
5.13k
  if (cm->rst_tmpbuf == NULL) {
292
4.70k
    CHECK_MEM_ERROR(cm, cm->rst_tmpbuf,
293
4.70k
                    (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
294
4.70k
  }
295
296
5.13k
  if (cm->rlbs == NULL) {
297
4.70k
    CHECK_MEM_ERROR(cm, cm->rlbs, aom_malloc(sizeof(RestorationLineBuffers)));
298
4.70k
  }
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
5.13k
  int num_stripes = 0;
307
10.5k
  for (int i = 0; i < cm->tiles.rows; ++i) {
308
5.46k
    TileInfo tile_info;
309
5.46k
    av1_tile_set_row(&tile_info, cm, i);
310
5.46k
    const int mi_h = tile_info.mi_row_end - tile_info.mi_row_start;
311
5.46k
    const int ext_h = RESTORATION_UNIT_OFFSET + (mi_h << MI_SIZE_LOG2);
312
5.46k
    const int tile_stripes = (ext_h + 63) / 64;
313
5.46k
    num_stripes += tile_stripes;
314
5.46k
  }
315
316
  // Now we need to allocate enough space to store the line buffers for the
317
  // stripes
318
5.13k
  const int frame_w = cm->superres_upscaled_width;
319
5.13k
  const int use_highbd = cm->seq_params->use_highbitdepth;
320
321
20.2k
  for (int p = 0; p < num_planes; ++p) {
322
15.1k
    const int is_uv = p > 0;
323
15.1k
    const int ss_x = is_uv && cm->seq_params->subsampling_x;
324
15.1k
    const int plane_w = ((frame_w + ss_x) >> ss_x) + 2 * RESTORATION_EXTRA_HORZ;
325
15.1k
    const int stride = ALIGN_POWER_OF_TWO(plane_w, 5);
326
15.1k
    const int buf_size = num_stripes * stride * RESTORATION_CTX_VERT
327
15.1k
                         << use_highbd;
328
15.1k
    RestorationStripeBoundaries *boundaries = &cm->rst_info[p].boundaries;
329
330
15.1k
    if (buf_size != boundaries->stripe_boundary_size ||
331
15.1k
        boundaries->stripe_boundary_above == NULL ||
332
15.1k
        boundaries->stripe_boundary_below == NULL) {
333
14.0k
      aom_free(boundaries->stripe_boundary_above);
334
14.0k
      aom_free(boundaries->stripe_boundary_below);
335
336
14.0k
      CHECK_MEM_ERROR(cm, boundaries->stripe_boundary_above,
337
14.0k
                      (uint8_t *)aom_memalign(32, buf_size));
338
14.0k
      CHECK_MEM_ERROR(cm, boundaries->stripe_boundary_below,
339
14.0k
                      (uint8_t *)aom_memalign(32, buf_size));
340
341
14.0k
      boundaries->stripe_boundary_size = buf_size;
342
14.0k
    }
343
15.1k
    boundaries->stripe_boundary_stride = stride;
344
15.1k
  }
345
5.13k
}
346
347
28.6k
void av1_free_restoration_buffers(AV1_COMMON *cm) {
348
28.6k
  int p;
349
114k
  for (p = 0; p < MAX_MB_PLANE; ++p)
350
85.9k
    av1_free_restoration_struct(&cm->rst_info[p]);
351
28.6k
  aom_free(cm->rst_tmpbuf);
352
28.6k
  cm->rst_tmpbuf = NULL;
353
28.6k
  aom_free(cm->rlbs);
354
28.6k
  cm->rlbs = NULL;
355
114k
  for (p = 0; p < MAX_MB_PLANE; ++p) {
356
85.9k
    RestorationStripeBoundaries *boundaries = &cm->rst_info[p].boundaries;
357
85.9k
    aom_free(boundaries->stripe_boundary_above);
358
85.9k
    aom_free(boundaries->stripe_boundary_below);
359
85.9k
    boundaries->stripe_boundary_above = NULL;
360
85.9k
    boundaries->stripe_boundary_below = NULL;
361
85.9k
  }
362
363
28.6k
  aom_free_frame_buffer(&cm->rst_frame);
364
28.6k
}
365
#endif  // !CONFIG_REALTIME_ONLY
366
367
56.7k
void av1_free_above_context_buffers(CommonContexts *above_contexts) {
368
56.7k
  int i;
369
56.7k
  const int num_planes = above_contexts->num_planes;
370
371
87.5k
  for (int tile_row = 0; tile_row < above_contexts->num_tile_rows; tile_row++) {
372
117k
    for (i = 0; i < num_planes; i++) {
373
86.6k
      aom_free(above_contexts->entropy[i][tile_row]);
374
86.6k
      above_contexts->entropy[i][tile_row] = NULL;
375
86.6k
    }
376
30.7k
    aom_free(above_contexts->partition[tile_row]);
377
30.7k
    above_contexts->partition[tile_row] = NULL;
378
379
30.7k
    aom_free(above_contexts->txfm[tile_row]);
380
30.7k
    above_contexts->txfm[tile_row] = NULL;
381
30.7k
  }
382
135k
  for (i = 0; i < num_planes; i++) {
383
78.7k
    aom_free(above_contexts->entropy[i]);
384
78.7k
    above_contexts->entropy[i] = NULL;
385
78.7k
  }
386
56.7k
  aom_free(above_contexts->partition);
387
56.7k
  above_contexts->partition = NULL;
388
389
56.7k
  aom_free(above_contexts->txfm);
390
56.7k
  above_contexts->txfm = NULL;
391
392
56.7k
  above_contexts->num_tile_rows = 0;
393
56.7k
  above_contexts->num_mi_cols = 0;
394
56.7k
  above_contexts->num_planes = 0;
395
56.7k
}
396
397
28.6k
void av1_free_context_buffers(AV1_COMMON *cm) {
398
28.6k
  cm->mi_params.free_mi(&cm->mi_params);
399
400
28.6k
  av1_free_above_context_buffers(&cm->above_contexts);
401
28.6k
}
402
403
int av1_alloc_above_context_buffers(CommonContexts *above_contexts,
404
                                    int num_tile_rows, int num_mi_cols,
405
28.1k
                                    int num_planes) {
406
28.1k
  const int aligned_mi_cols =
407
28.1k
      ALIGN_POWER_OF_TWO(num_mi_cols, MAX_MIB_SIZE_LOG2);
408
409
  // Allocate above context buffers
410
28.1k
  above_contexts->num_tile_rows = num_tile_rows;
411
28.1k
  above_contexts->num_mi_cols = aligned_mi_cols;
412
28.1k
  above_contexts->num_planes = num_planes;
413
106k
  for (int plane_idx = 0; plane_idx < num_planes; plane_idx++) {
414
78.7k
    above_contexts->entropy[plane_idx] = (ENTROPY_CONTEXT **)aom_calloc(
415
78.7k
        num_tile_rows, sizeof(above_contexts->entropy[0]));
416
78.7k
    if (!above_contexts->entropy[plane_idx]) return 1;
417
78.7k
  }
418
419
28.1k
  above_contexts->partition = (PARTITION_CONTEXT **)aom_calloc(
420
28.1k
      num_tile_rows, sizeof(above_contexts->partition));
421
28.1k
  if (!above_contexts->partition) return 1;
422
423
28.1k
  above_contexts->txfm =
424
28.1k
      (TXFM_CONTEXT **)aom_calloc(num_tile_rows, sizeof(above_contexts->txfm));
425
28.1k
  if (!above_contexts->txfm) return 1;
426
427
58.8k
  for (int tile_row = 0; tile_row < num_tile_rows; tile_row++) {
428
117k
    for (int plane_idx = 0; plane_idx < num_planes; plane_idx++) {
429
86.6k
      above_contexts->entropy[plane_idx][tile_row] =
430
86.6k
          (ENTROPY_CONTEXT *)aom_calloc(
431
86.6k
              aligned_mi_cols, sizeof(*above_contexts->entropy[0][tile_row]));
432
86.6k
      if (!above_contexts->entropy[plane_idx][tile_row]) return 1;
433
86.6k
    }
434
435
30.7k
    above_contexts->partition[tile_row] = (PARTITION_CONTEXT *)aom_calloc(
436
30.7k
        aligned_mi_cols, sizeof(*above_contexts->partition[tile_row]));
437
30.7k
    if (!above_contexts->partition[tile_row]) return 1;
438
439
30.7k
    above_contexts->txfm[tile_row] = (TXFM_CONTEXT *)aom_calloc(
440
30.7k
        aligned_mi_cols, sizeof(*above_contexts->txfm[tile_row]));
441
30.7k
    if (!above_contexts->txfm[tile_row]) return 1;
442
30.7k
  }
443
444
28.1k
  return 0;
445
28.1k
}
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
28.2k
static int alloc_mi(CommonModeInfoParams *mi_params) {
451
28.2k
  const int aligned_mi_rows = calc_mi_size(mi_params->mi_rows);
452
28.2k
  const int mi_grid_size = mi_params->mi_stride * aligned_mi_rows;
453
28.2k
  const int alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
454
28.2k
  const int alloc_mi_size =
455
28.2k
      mi_params->mi_alloc_stride * (aligned_mi_rows / alloc_size_1d);
456
457
28.2k
  if (mi_params->mi_alloc_size < alloc_mi_size ||
458
28.2k
      mi_params->mi_grid_size < mi_grid_size) {
459
27.7k
    mi_params->free_mi(mi_params);
460
461
27.7k
    mi_params->mi_alloc =
462
27.7k
        aom_calloc(alloc_mi_size, sizeof(*mi_params->mi_alloc));
463
27.7k
    if (!mi_params->mi_alloc) return 1;
464
27.7k
    mi_params->mi_alloc_size = alloc_mi_size;
465
466
27.7k
    mi_params->mi_grid_base = (MB_MODE_INFO **)aom_calloc(
467
27.7k
        mi_grid_size, sizeof(*mi_params->mi_grid_base));
468
27.7k
    if (!mi_params->mi_grid_base) return 1;
469
27.7k
    mi_params->mi_grid_size = mi_grid_size;
470
471
27.7k
    mi_params->tx_type_map =
472
27.7k
        aom_calloc(mi_grid_size, sizeof(*mi_params->tx_type_map));
473
27.7k
    if (!mi_params->tx_type_map) return 1;
474
27.7k
  }
475
476
28.2k
  return 0;
477
28.2k
}
478
479
int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height, int mode,
480
28.2k
                              BLOCK_SIZE min_partition_size) {
481
28.2k
  CommonModeInfoParams *const mi_params = &cm->mi_params;
482
28.2k
  mi_params->set_mb_mi(mi_params, width, height, mode, min_partition_size);
483
28.2k
  if (alloc_mi(mi_params)) goto fail;
484
28.2k
  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
28.2k
}
492
493
28.6k
void av1_remove_common(AV1_COMMON *cm) {
494
28.6k
  av1_free_context_buffers(cm);
495
496
28.6k
  aom_free(cm->fc);
497
28.6k
  cm->fc = NULL;
498
28.6k
  aom_free(cm->default_frame_context);
499
28.6k
  cm->default_frame_context = NULL;
500
28.6k
}
501
502
29.9k
void av1_init_mi_buffers(CommonModeInfoParams *mi_params) {
503
29.9k
  mi_params->setup_mi(mi_params);
504
29.9k
}