Coverage Report

Created: 2022-08-24 06:17

/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
1.98k
void av1_free_ref_frame_buffers(BufferPool *pool) {
37
1.98k
  int i;
38
39
33.7k
  for (i = 0; i < FRAME_BUFFERS; ++i) {
40
31.8k
    if (pool->frame_bufs[i].ref_count > 0 &&
41
31.8k
        pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
42
12
      pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
43
12
      pool->frame_bufs[i].raw_frame_buffer.data = NULL;
44
12
      pool->frame_bufs[i].raw_frame_buffer.size = 0;
45
12
      pool->frame_bufs[i].raw_frame_buffer.priv = NULL;
46
12
      pool->frame_bufs[i].ref_count = 0;
47
12
    }
48
31.8k
    aom_free(pool->frame_bufs[i].mvs);
49
31.8k
    pool->frame_bufs[i].mvs = NULL;
50
31.8k
    aom_free(pool->frame_bufs[i].seg_map);
51
31.8k
    pool->frame_bufs[i].seg_map = NULL;
52
31.8k
    aom_free_frame_buffer(&pool->frame_bufs[i].buf);
53
31.8k
  }
54
1.98k
}
55
56
static INLINE void free_cdef_linebuf_conditional(
57
12
    AV1_COMMON *const cm, const size_t *new_linebuf_size) {
58
12
  CdefInfo *cdef_info = &cm->cdef_info;
59
48
  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
60
36
    if (new_linebuf_size[plane] != cdef_info->allocated_linebuf_size[plane]) {
61
30
      aom_free(cdef_info->linebuf[plane]);
62
30
      cdef_info->linebuf[plane] = NULL;
63
30
    }
64
36
  }
65
12
}
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
12
                                              const size_t new_srcbuf_size) {
72
12
  CdefInfo *cdef_info = &cm->cdef_info;
73
12
  if (new_srcbuf_size != cdef_info->allocated_srcbuf_size) {
74
10
    aom_free(*srcbuf);
75
10
    *srcbuf = NULL;
76
10
  }
77
48
  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
78
36
    if (new_colbuf_size[plane] != cdef_info->allocated_colbuf_size[plane]) {
79
30
      aom_free(colbuf[plane]);
80
30
      colbuf[plane] = NULL;
81
30
    }
82
36
  }
83
12
}
84
85
1.98k
static INLINE void free_cdef_bufs(uint16_t **colbuf, uint16_t **srcbuf) {
86
1.98k
  aom_free(*srcbuf);
87
1.98k
  *srcbuf = NULL;
88
7.95k
  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
89
5.96k
    aom_free(colbuf[plane]);
90
5.96k
    colbuf[plane] = NULL;
91
5.96k
  }
92
1.98k
}
93
94
static INLINE void free_cdef_row_sync(AV1CdefRowSync **cdef_row_mt,
95
12
                                      const int num_mi_rows) {
96
12
  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
1.98k
                           AV1CdefSync *cdef_sync, int num_workers) {
114
1.98k
  CdefInfo *cdef_info = &cm->cdef_info;
115
1.98k
  const int num_mi_rows = cdef_info->allocated_mi_rows;
116
117
7.95k
  for (int plane = 0; plane < MAX_MB_PLANE; plane++) {
118
5.96k
    aom_free(cdef_info->linebuf[plane]);
119
5.96k
    cdef_info->linebuf[plane] = NULL;
120
5.96k
  }
121
  // De-allocation of column buffer & source buffer (worker_0).
122
1.98k
  free_cdef_bufs(cdef_info->colbuf, &cdef_info->srcbuf);
123
124
1.98k
  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
10
                                      const int num_planes) {
138
10
  CdefInfo *cdef_info = &cm->cdef_info;
139
40
  for (int plane = 0; plane < num_planes; plane++) {
140
30
    if (linebuf[plane] == NULL)
141
30
      CHECK_MEM_ERROR(cm, linebuf[plane],
142
30
                      aom_malloc(cdef_info->allocated_linebuf_size[plane]));
143
30
  }
144
10
}
145
146
static INLINE void alloc_cdef_bufs(AV1_COMMON *const cm, uint16_t **colbuf,
147
10
                                   uint16_t **srcbuf, const int num_planes) {
148
10
  CdefInfo *cdef_info = &cm->cdef_info;
149
10
  if (*srcbuf == NULL)
150
10
    CHECK_MEM_ERROR(cm, *srcbuf,
151
10
                    aom_memalign(16, cdef_info->allocated_srcbuf_size));
152
153
40
  for (int plane = 0; plane < num_planes; plane++) {
154
30
    if (colbuf[plane] == NULL)
155
30
      CHECK_MEM_ERROR(cm, colbuf[plane],
156
30
                      aom_malloc(cdef_info->allocated_colbuf_size[plane]));
157
30
  }
158
10
}
159
160
static INLINE void alloc_cdef_row_sync(AV1_COMMON *const cm,
161
                                       AV1CdefRowSync **cdef_row_mt,
162
0
                                       const int num_mi_rows) {
163
0
  if (*cdef_row_mt != NULL) return;
164
165
0
  CHECK_MEM_ERROR(cm, *cdef_row_mt,
166
0
                  aom_malloc(sizeof(**cdef_row_mt) * num_mi_rows));
167
0
#if CONFIG_MULTITHREAD
168
0
  for (int row_idx = 0; row_idx < num_mi_rows; row_idx++) {
169
0
    CHECK_MEM_ERROR(cm, (*cdef_row_mt)[row_idx].row_mutex_,
170
0
                    aom_malloc(sizeof(*(*cdef_row_mt)[row_idx].row_mutex_)));
171
0
    pthread_mutex_init((*cdef_row_mt)[row_idx].row_mutex_, NULL);
172
173
0
    CHECK_MEM_ERROR(cm, (*cdef_row_mt)[row_idx].row_cond_,
174
0
                    aom_malloc(sizeof(*(*cdef_row_mt)[row_idx].row_cond_)));
175
0
    pthread_cond_init((*cdef_row_mt)[row_idx].row_cond_, NULL);
176
177
0
    (*cdef_row_mt)[row_idx].is_row_done = 0;
178
0
  }
179
0
#endif  // CONFIG_MULTITHREAD
180
0
}
181
182
void av1_alloc_cdef_buffers(AV1_COMMON *const cm,
183
                            AV1CdefWorkerData **cdef_worker,
184
                            AV1CdefSync *cdef_sync, int num_workers,
185
12
                            int init_worker) {
186
12
  const int num_planes = av1_num_planes(cm);
187
12
  size_t new_linebuf_size[MAX_MB_PLANE] = { 0 };
188
12
  size_t new_colbuf_size[MAX_MB_PLANE] = { 0 };
189
12
  size_t new_srcbuf_size = 0;
190
12
  CdefInfo *const cdef_info = &cm->cdef_info;
191
  // Check for configuration change
192
12
  const int num_mi_rows =
193
12
      (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
194
12
  const int is_num_workers_changed =
195
12
      cdef_info->allocated_num_workers != num_workers;
196
12
  const int is_cdef_enabled =
197
12
      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
12
  int num_bufs = 3;
203
12
  if (num_workers > 1)
204
0
    num_bufs = (cm->mi_params.mi_rows + MI_SIZE_64X64 - 1) / MI_SIZE_64X64;
205
206
12
  if (is_cdef_enabled) {
207
    // Calculate src buffer size
208
10
    new_srcbuf_size = sizeof(*cdef_info->srcbuf) * CDEF_INBUF_SIZE;
209
40
    for (int plane = 0; plane < num_planes; plane++) {
210
30
      const int shift =
211
30
          plane == AOM_PLANE_Y ? 0 : cm->seq_params->subsampling_x;
212
      // Calculate top and bottom line buffer size
213
30
      const int luma_stride =
214
30
          ALIGN_POWER_OF_TWO(cm->mi_params.mi_cols << MI_SIZE_LOG2, 4);
215
30
      new_linebuf_size[plane] = sizeof(*cdef_info->linebuf) * num_bufs *
216
30
                                (CDEF_VBORDER << 1) * (luma_stride >> shift);
217
      // Calculate column buffer size
218
30
      const int block_height =
219
30
          (CDEF_BLOCKSIZE << (MI_SIZE_LOG2 - shift)) * 2 * CDEF_VBORDER;
220
30
      new_colbuf_size[plane] =
221
30
          sizeof(*cdef_info->colbuf[plane]) * block_height * CDEF_HBORDER;
222
30
    }
223
10
  }
224
225
  // Free src, line and column buffers for worker 0 in case of reallocation
226
12
  free_cdef_linebuf_conditional(cm, new_linebuf_size);
227
12
  free_cdef_bufs_conditional(cm, cdef_info->colbuf, &cdef_info->srcbuf,
228
12
                             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
12
  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
12
  if (cdef_info->allocated_mi_rows != num_mi_rows)
251
12
    free_cdef_row_sync(&cdef_sync->cdef_row_mt, cdef_info->allocated_mi_rows);
252
253
  // Store allocated sizes for reallocation
254
12
  cdef_info->allocated_srcbuf_size = new_srcbuf_size;
255
12
  av1_copy(cdef_info->allocated_colbuf_size, new_colbuf_size);
256
12
  av1_copy(cdef_info->allocated_linebuf_size, new_linebuf_size);
257
  // Store configuration to check change in configuration
258
12
  cdef_info->allocated_mi_rows = num_mi_rows;
259
12
  cdef_info->allocated_num_workers = num_workers;
260
261
12
  if (!is_cdef_enabled) return;
262
263
  // Memory allocation of column buffer & source buffer (worker_0).
264
10
  alloc_cdef_bufs(cm, cdef_info->colbuf, &cdef_info->srcbuf, num_planes);
265
10
  alloc_cdef_linebuf(cm, cdef_info->linebuf, num_planes);
266
267
10
  if (num_workers < 2) return;
268
269
0
  if (init_worker) {
270
0
    if (*cdef_worker == NULL)
271
0
      CHECK_MEM_ERROR(cm, *cdef_worker,
272
0
                      aom_calloc(num_workers, sizeof(**cdef_worker)));
273
274
    // Memory allocation of column buffer & source buffer for remaining workers.
275
0
    for (int idx = num_workers - 1; idx >= 1; idx--)
276
0
      alloc_cdef_bufs(cm, (*cdef_worker)[idx].colbuf,
277
0
                      &(*cdef_worker)[idx].srcbuf, num_planes);
278
0
  }
279
280
0
  alloc_cdef_row_sync(cm, &cdef_sync->cdef_row_mt,
281
0
                      cdef_info->allocated_mi_rows);
282
0
}
283
284
#if !CONFIG_REALTIME_ONLY
285
// Assumes cm->rst_info[p].restoration_unit_size is already initialized
286
426
void av1_alloc_restoration_buffers(AV1_COMMON *cm) {
287
426
  const int num_planes = av1_num_planes(cm);
288
1.70k
  for (int p = 0; p < num_planes; ++p)
289
1.27k
    av1_alloc_restoration_struct(cm, &cm->rst_info[p], p > 0);
290
291
426
  if (cm->rst_tmpbuf == NULL) {
292
426
    CHECK_MEM_ERROR(cm, cm->rst_tmpbuf,
293
426
                    (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
294
426
  }
295
296
426
  if (cm->rlbs == NULL) {
297
426
    CHECK_MEM_ERROR(cm, cm->rlbs, aom_malloc(sizeof(RestorationLineBuffers)));
298
426
  }
299
300
  // For striped loop restoration, we divide each row of tiles into "stripes",
301
  // of height 64 luma pixels but with an offset by RESTORATION_UNIT_OFFSET
302
  // luma pixels to match the output from CDEF. We will need to store 2 *
303
  // RESTORATION_CTX_VERT lines of data for each stripe, and also need to be
304
  // able to quickly answer the question "Where is the <n>'th stripe for tile
305
  // row <m>?" To make that efficient, we generate the rst_last_stripe array.
306
426
  int num_stripes = 0;
307
884
  for (int i = 0; i < cm->tiles.rows; ++i) {
308
458
    TileInfo tile_info;
309
458
    av1_tile_set_row(&tile_info, cm, i);
310
458
    const int mi_h = tile_info.mi_row_end - tile_info.mi_row_start;
311
458
    const int ext_h = RESTORATION_UNIT_OFFSET + (mi_h << MI_SIZE_LOG2);
312
458
    const int tile_stripes = (ext_h + 63) / 64;
313
458
    num_stripes += tile_stripes;
314
458
  }
315
316
  // Now we need to allocate enough space to store the line buffers for the
317
  // stripes
318
426
  const int frame_w = cm->superres_upscaled_width;
319
426
  const int use_highbd = cm->seq_params->use_highbitdepth;
320
321
1.70k
  for (int p = 0; p < num_planes; ++p) {
322
1.27k
    const int is_uv = p > 0;
323
1.27k
    const int ss_x = is_uv && cm->seq_params->subsampling_x;
324
1.27k
    const int plane_w = ((frame_w + ss_x) >> ss_x) + 2 * RESTORATION_EXTRA_HORZ;
325
1.27k
    const int stride = ALIGN_POWER_OF_TWO(plane_w, 5);
326
1.27k
    const int buf_size = num_stripes * stride * RESTORATION_CTX_VERT
327
1.27k
                         << use_highbd;
328
1.27k
    RestorationStripeBoundaries *boundaries = &cm->rst_info[p].boundaries;
329
330
1.27k
    if (buf_size != boundaries->stripe_boundary_size ||
331
1.27k
        boundaries->stripe_boundary_above == NULL ||
332
1.27k
        boundaries->stripe_boundary_below == NULL) {
333
1.27k
      aom_free(boundaries->stripe_boundary_above);
334
1.27k
      aom_free(boundaries->stripe_boundary_below);
335
336
1.27k
      CHECK_MEM_ERROR(cm, boundaries->stripe_boundary_above,
337
1.27k
                      (uint8_t *)aom_memalign(32, buf_size));
338
1.27k
      CHECK_MEM_ERROR(cm, boundaries->stripe_boundary_below,
339
1.27k
                      (uint8_t *)aom_memalign(32, buf_size));
340
341
1.27k
      boundaries->stripe_boundary_size = buf_size;
342
1.27k
    }
343
1.27k
    boundaries->stripe_boundary_stride = stride;
344
1.27k
  }
345
426
}
346
347
1.98k
void av1_free_restoration_buffers(AV1_COMMON *cm) {
348
1.98k
  int p;
349
7.95k
  for (p = 0; p < MAX_MB_PLANE; ++p)
350
5.96k
    av1_free_restoration_struct(&cm->rst_info[p]);
351
1.98k
  aom_free(cm->rst_tmpbuf);
352
1.98k
  cm->rst_tmpbuf = NULL;
353
1.98k
  aom_free(cm->rlbs);
354
1.98k
  cm->rlbs = NULL;
355
7.95k
  for (p = 0; p < MAX_MB_PLANE; ++p) {
356
5.96k
    RestorationStripeBoundaries *boundaries = &cm->rst_info[p].boundaries;
357
5.96k
    aom_free(boundaries->stripe_boundary_above);
358
5.96k
    aom_free(boundaries->stripe_boundary_below);
359
5.96k
    boundaries->stripe_boundary_above = NULL;
360
5.96k
    boundaries->stripe_boundary_below = NULL;
361
5.96k
  }
362
363
1.98k
  aom_free_frame_buffer(&cm->rst_frame);
364
1.98k
}
365
#endif  // !CONFIG_REALTIME_ONLY
366
367
3.97k
void av1_free_above_context_buffers(CommonContexts *above_contexts) {
368
3.97k
  int i;
369
3.97k
  const int num_planes = above_contexts->num_planes;
370
371
6.13k
  for (int tile_row = 0; tile_row < above_contexts->num_tile_rows; tile_row++) {
372
8.65k
    for (i = 0; i < num_planes; i++) {
373
6.49k
      aom_free(above_contexts->entropy[i][tile_row]);
374
6.49k
      above_contexts->entropy[i][tile_row] = NULL;
375
6.49k
    }
376
2.16k
    aom_free(above_contexts->partition[tile_row]);
377
2.16k
    above_contexts->partition[tile_row] = NULL;
378
379
2.16k
    aom_free(above_contexts->txfm[tile_row]);
380
2.16k
    above_contexts->txfm[tile_row] = NULL;
381
2.16k
  }
382
9.92k
  for (i = 0; i < num_planes; i++) {
383
5.95k
    aom_free(above_contexts->entropy[i]);
384
5.95k
    above_contexts->entropy[i] = NULL;
385
5.95k
  }
386
3.97k
  aom_free(above_contexts->partition);
387
3.97k
  above_contexts->partition = NULL;
388
389
3.97k
  aom_free(above_contexts->txfm);
390
3.97k
  above_contexts->txfm = NULL;
391
392
3.97k
  above_contexts->num_tile_rows = 0;
393
3.97k
  above_contexts->num_mi_cols = 0;
394
3.97k
  above_contexts->num_planes = 0;
395
3.97k
}
396
397
1.98k
void av1_free_context_buffers(AV1_COMMON *cm) {
398
1.98k
  cm->mi_params.free_mi(&cm->mi_params);
399
400
1.98k
  av1_free_above_context_buffers(&cm->above_contexts);
401
1.98k
}
402
403
int av1_alloc_above_context_buffers(CommonContexts *above_contexts,
404
                                    int num_tile_rows, int num_mi_cols,
405
1.98k
                                    int num_planes) {
406
1.98k
  const int aligned_mi_cols =
407
1.98k
      ALIGN_POWER_OF_TWO(num_mi_cols, MAX_MIB_SIZE_LOG2);
408
409
  // Allocate above context buffers
410
1.98k
  above_contexts->num_tile_rows = num_tile_rows;
411
1.98k
  above_contexts->num_mi_cols = aligned_mi_cols;
412
1.98k
  above_contexts->num_planes = num_planes;
413
7.93k
  for (int plane_idx = 0; plane_idx < num_planes; plane_idx++) {
414
5.95k
    above_contexts->entropy[plane_idx] = (ENTROPY_CONTEXT **)aom_calloc(
415
5.95k
        num_tile_rows, sizeof(above_contexts->entropy[0]));
416
5.95k
    if (!above_contexts->entropy[plane_idx]) return 1;
417
5.95k
  }
418
419
1.98k
  above_contexts->partition = (PARTITION_CONTEXT **)aom_calloc(
420
1.98k
      num_tile_rows, sizeof(above_contexts->partition));
421
1.98k
  if (!above_contexts->partition) return 1;
422
423
1.98k
  above_contexts->txfm =
424
1.98k
      (TXFM_CONTEXT **)aom_calloc(num_tile_rows, sizeof(above_contexts->txfm));
425
1.98k
  if (!above_contexts->txfm) return 1;
426
427
4.14k
  for (int tile_row = 0; tile_row < num_tile_rows; tile_row++) {
428
8.65k
    for (int plane_idx = 0; plane_idx < num_planes; plane_idx++) {
429
6.49k
      above_contexts->entropy[plane_idx][tile_row] =
430
6.49k
          (ENTROPY_CONTEXT *)aom_calloc(
431
6.49k
              aligned_mi_cols, sizeof(*above_contexts->entropy[0][tile_row]));
432
6.49k
      if (!above_contexts->entropy[plane_idx][tile_row]) return 1;
433
6.49k
    }
434
435
2.16k
    above_contexts->partition[tile_row] = (PARTITION_CONTEXT *)aom_calloc(
436
2.16k
        aligned_mi_cols, sizeof(*above_contexts->partition[tile_row]));
437
2.16k
    if (!above_contexts->partition[tile_row]) return 1;
438
439
2.16k
    above_contexts->txfm[tile_row] = (TXFM_CONTEXT *)aom_calloc(
440
2.16k
        aligned_mi_cols, sizeof(*above_contexts->txfm[tile_row]));
441
2.16k
    if (!above_contexts->txfm[tile_row]) return 1;
442
2.16k
  }
443
444
1.98k
  return 0;
445
1.98k
}
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
1.98k
static int alloc_mi(CommonModeInfoParams *mi_params) {
451
1.98k
  const int aligned_mi_rows = calc_mi_size(mi_params->mi_rows);
452
1.98k
  const int mi_grid_size = mi_params->mi_stride * aligned_mi_rows;
453
1.98k
  const int alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
454
1.98k
  const int alloc_mi_size =
455
1.98k
      mi_params->mi_alloc_stride * (aligned_mi_rows / alloc_size_1d);
456
457
1.98k
  if (mi_params->mi_alloc_size < alloc_mi_size ||
458
1.98k
      mi_params->mi_grid_size < mi_grid_size) {
459
1.98k
    mi_params->free_mi(mi_params);
460
461
1.98k
    mi_params->mi_alloc =
462
1.98k
        aom_calloc(alloc_mi_size, sizeof(*mi_params->mi_alloc));
463
1.98k
    if (!mi_params->mi_alloc) return 1;
464
1.98k
    mi_params->mi_alloc_size = alloc_mi_size;
465
466
1.98k
    mi_params->mi_grid_base = (MB_MODE_INFO **)aom_calloc(
467
1.98k
        mi_grid_size, sizeof(*mi_params->mi_grid_base));
468
1.98k
    if (!mi_params->mi_grid_base) return 1;
469
1.98k
    mi_params->mi_grid_size = mi_grid_size;
470
471
1.98k
    mi_params->tx_type_map =
472
1.98k
        aom_calloc(mi_grid_size, sizeof(*mi_params->tx_type_map));
473
1.98k
    if (!mi_params->tx_type_map) return 1;
474
1.98k
  }
475
476
1.98k
  return 0;
477
1.98k
}
478
479
int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height, int mode,
480
1.98k
                              BLOCK_SIZE min_partition_size) {
481
1.98k
  CommonModeInfoParams *const mi_params = &cm->mi_params;
482
1.98k
  mi_params->set_mb_mi(mi_params, width, height, mode, min_partition_size);
483
1.98k
  if (alloc_mi(mi_params)) goto fail;
484
1.98k
  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
1.98k
}
492
493
1.98k
void av1_remove_common(AV1_COMMON *cm) {
494
1.98k
  av1_free_context_buffers(cm);
495
496
1.98k
  aom_free(cm->fc);
497
1.98k
  cm->fc = NULL;
498
1.98k
  aom_free(cm->default_frame_context);
499
1.98k
  cm->default_frame_context = NULL;
500
1.98k
}
501
502
1.98k
void av1_init_mi_buffers(CommonModeInfoParams *mi_params) {
503
1.98k
  mi_params->setup_mi(mi_params);
504
1.98k
}