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