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/entropymode.h"
21 #include "av1/common/entropymv.h"
22 
av1_get_MBs(int width,int height)23 int av1_get_MBs(int width, int height) {
24   const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
25   const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
26   const int mi_cols = aligned_width >> MI_SIZE_LOG2;
27   const int mi_rows = aligned_height >> MI_SIZE_LOG2;
28 
29   const int mb_cols = (mi_cols + 2) >> 2;
30   const int mb_rows = (mi_rows + 2) >> 2;
31   return mb_rows * mb_cols;
32 }
33 
av1_free_ref_frame_buffers(BufferPool * pool)34 void av1_free_ref_frame_buffers(BufferPool *pool) {
35   int i;
36 
37   for (i = 0; i < FRAME_BUFFERS; ++i) {
38     if (pool->frame_bufs[i].ref_count > 0 &&
39         pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
40       pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
41       pool->frame_bufs[i].raw_frame_buffer.data = NULL;
42       pool->frame_bufs[i].raw_frame_buffer.size = 0;
43       pool->frame_bufs[i].raw_frame_buffer.priv = NULL;
44       pool->frame_bufs[i].ref_count = 0;
45     }
46     aom_free(pool->frame_bufs[i].mvs);
47     pool->frame_bufs[i].mvs = NULL;
48     aom_free(pool->frame_bufs[i].seg_map);
49     pool->frame_bufs[i].seg_map = NULL;
50     aom_free_frame_buffer(&pool->frame_bufs[i].buf);
51   }
52 }
53 
54 // Assumes cm->rst_info[p].restoration_unit_size is already initialized
av1_alloc_restoration_buffers(AV1_COMMON * cm)55 void av1_alloc_restoration_buffers(AV1_COMMON *cm) {
56   const int num_planes = av1_num_planes(cm);
57   for (int p = 0; p < num_planes; ++p)
58     av1_alloc_restoration_struct(cm, &cm->rst_info[p], p > 0);
59 
60   if (cm->rst_tmpbuf == NULL) {
61     CHECK_MEM_ERROR(cm, cm->rst_tmpbuf,
62                     (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
63   }
64 
65   if (cm->rlbs == NULL) {
66     CHECK_MEM_ERROR(cm, cm->rlbs, aom_malloc(sizeof(RestorationLineBuffers)));
67   }
68 
69   // For striped loop restoration, we divide each row of tiles into "stripes",
70   // of height 64 luma pixels but with an offset by RESTORATION_UNIT_OFFSET
71   // luma pixels to match the output from CDEF. We will need to store 2 *
72   // RESTORATION_CTX_VERT lines of data for each stripe, and also need to be
73   // able to quickly answer the question "Where is the <n>'th stripe for tile
74   // row <m>?" To make that efficient, we generate the rst_last_stripe array.
75   int num_stripes = 0;
76   for (int i = 0; i < cm->tiles.rows; ++i) {
77     TileInfo tile_info;
78     av1_tile_set_row(&tile_info, cm, i);
79     const int mi_h = tile_info.mi_row_end - tile_info.mi_row_start;
80     const int ext_h = RESTORATION_UNIT_OFFSET + (mi_h << MI_SIZE_LOG2);
81     const int tile_stripes = (ext_h + 63) / 64;
82     num_stripes += tile_stripes;
83   }
84 
85   // Now we need to allocate enough space to store the line buffers for the
86   // stripes
87   const int frame_w = cm->superres_upscaled_width;
88   const int use_highbd = cm->seq_params.use_highbitdepth;
89 
90   for (int p = 0; p < num_planes; ++p) {
91     const int is_uv = p > 0;
92     const int ss_x = is_uv && cm->seq_params.subsampling_x;
93     const int plane_w = ((frame_w + ss_x) >> ss_x) + 2 * RESTORATION_EXTRA_HORZ;
94     const int stride = ALIGN_POWER_OF_TWO(plane_w, 5);
95     const int buf_size = num_stripes * stride * RESTORATION_CTX_VERT
96                          << use_highbd;
97     RestorationStripeBoundaries *boundaries = &cm->rst_info[p].boundaries;
98 
99     if (buf_size != boundaries->stripe_boundary_size ||
100         boundaries->stripe_boundary_above == NULL ||
101         boundaries->stripe_boundary_below == NULL) {
102       aom_free(boundaries->stripe_boundary_above);
103       aom_free(boundaries->stripe_boundary_below);
104 
105       CHECK_MEM_ERROR(cm, boundaries->stripe_boundary_above,
106                       (uint8_t *)aom_memalign(32, buf_size));
107       CHECK_MEM_ERROR(cm, boundaries->stripe_boundary_below,
108                       (uint8_t *)aom_memalign(32, buf_size));
109 
110       boundaries->stripe_boundary_size = buf_size;
111     }
112     boundaries->stripe_boundary_stride = stride;
113   }
114 }
115 
av1_free_restoration_buffers(AV1_COMMON * cm)116 void av1_free_restoration_buffers(AV1_COMMON *cm) {
117   int p;
118   for (p = 0; p < MAX_MB_PLANE; ++p)
119     av1_free_restoration_struct(&cm->rst_info[p]);
120   aom_free(cm->rst_tmpbuf);
121   cm->rst_tmpbuf = NULL;
122   aom_free(cm->rlbs);
123   cm->rlbs = NULL;
124   for (p = 0; p < MAX_MB_PLANE; ++p) {
125     RestorationStripeBoundaries *boundaries = &cm->rst_info[p].boundaries;
126     aom_free(boundaries->stripe_boundary_above);
127     aom_free(boundaries->stripe_boundary_below);
128     boundaries->stripe_boundary_above = NULL;
129     boundaries->stripe_boundary_below = NULL;
130   }
131 
132   aom_free_frame_buffer(&cm->rst_frame);
133 }
134 
av1_free_above_context_buffers(CommonContexts * above_contexts)135 void av1_free_above_context_buffers(CommonContexts *above_contexts) {
136   int i;
137   const int num_planes = above_contexts->num_planes;
138 
139   for (int tile_row = 0; tile_row < above_contexts->num_tile_rows; tile_row++) {
140     for (i = 0; i < num_planes; i++) {
141       aom_free(above_contexts->entropy[i][tile_row]);
142       above_contexts->entropy[i][tile_row] = NULL;
143     }
144     aom_free(above_contexts->partition[tile_row]);
145     above_contexts->partition[tile_row] = NULL;
146 
147     aom_free(above_contexts->txfm[tile_row]);
148     above_contexts->txfm[tile_row] = NULL;
149   }
150   for (i = 0; i < num_planes; i++) {
151     aom_free(above_contexts->entropy[i]);
152     above_contexts->entropy[i] = NULL;
153   }
154   aom_free(above_contexts->partition);
155   above_contexts->partition = NULL;
156 
157   aom_free(above_contexts->txfm);
158   above_contexts->txfm = NULL;
159 
160   above_contexts->num_tile_rows = 0;
161   above_contexts->num_mi_cols = 0;
162   above_contexts->num_planes = 0;
163 }
164 
av1_free_context_buffers(AV1_COMMON * cm)165 void av1_free_context_buffers(AV1_COMMON *cm) {
166   cm->mi_params.free_mi(&cm->mi_params);
167 
168   av1_free_above_context_buffers(&cm->above_contexts);
169 
170 #if CONFIG_LPF_MASK
171   av1_free_loop_filter_mask(cm);
172 #endif
173 }
174 
av1_alloc_above_context_buffers(CommonContexts * above_contexts,int num_tile_rows,int num_mi_cols,int num_planes)175 int av1_alloc_above_context_buffers(CommonContexts *above_contexts,
176                                     int num_tile_rows, int num_mi_cols,
177                                     int num_planes) {
178   const int aligned_mi_cols =
179       ALIGN_POWER_OF_TWO(num_mi_cols, MAX_MIB_SIZE_LOG2);
180 
181   // Allocate above context buffers
182   above_contexts->num_tile_rows = num_tile_rows;
183   above_contexts->num_mi_cols = aligned_mi_cols;
184   above_contexts->num_planes = num_planes;
185   for (int plane_idx = 0; plane_idx < num_planes; plane_idx++) {
186     above_contexts->entropy[plane_idx] = (ENTROPY_CONTEXT **)aom_calloc(
187         num_tile_rows, sizeof(above_contexts->entropy[0]));
188     if (!above_contexts->entropy[plane_idx]) return 1;
189   }
190 
191   above_contexts->partition = (PARTITION_CONTEXT **)aom_calloc(
192       num_tile_rows, sizeof(above_contexts->partition));
193   if (!above_contexts->partition) return 1;
194 
195   above_contexts->txfm =
196       (TXFM_CONTEXT **)aom_calloc(num_tile_rows, sizeof(above_contexts->txfm));
197   if (!above_contexts->txfm) return 1;
198 
199   for (int tile_row = 0; tile_row < num_tile_rows; tile_row++) {
200     for (int plane_idx = 0; plane_idx < num_planes; plane_idx++) {
201       above_contexts->entropy[plane_idx][tile_row] =
202           (ENTROPY_CONTEXT *)aom_calloc(
203               aligned_mi_cols, sizeof(*above_contexts->entropy[0][tile_row]));
204       if (!above_contexts->entropy[plane_idx][tile_row]) return 1;
205     }
206 
207     above_contexts->partition[tile_row] = (PARTITION_CONTEXT *)aom_calloc(
208         aligned_mi_cols, sizeof(*above_contexts->partition[tile_row]));
209     if (!above_contexts->partition[tile_row]) return 1;
210 
211     above_contexts->txfm[tile_row] = (TXFM_CONTEXT *)aom_calloc(
212         aligned_mi_cols, sizeof(*above_contexts->txfm[tile_row]));
213     if (!above_contexts->txfm[tile_row]) return 1;
214   }
215 
216   return 0;
217 }
218 
219 // Allocate the dynamically allocated arrays in 'mi_params' assuming
220 // 'mi_params->set_mb_mi()' was already called earlier to initialize the rest of
221 // the struct members.
alloc_mi(CommonModeInfoParams * mi_params)222 static int alloc_mi(CommonModeInfoParams *mi_params) {
223   const int aligned_mi_rows = calc_mi_size(mi_params->mi_rows);
224   const int mi_grid_size = mi_params->mi_stride * aligned_mi_rows;
225   const int alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
226   const int alloc_mi_size =
227       mi_params->mi_alloc_stride * (aligned_mi_rows / alloc_size_1d);
228 
229   if (mi_params->mi_alloc_size < alloc_mi_size ||
230       mi_params->mi_grid_size < mi_grid_size) {
231     mi_params->free_mi(mi_params);
232 
233     mi_params->mi_alloc =
234         aom_calloc(alloc_mi_size, sizeof(*mi_params->mi_alloc));
235     if (!mi_params->mi_alloc) return 1;
236     mi_params->mi_alloc_size = alloc_mi_size;
237 
238     mi_params->mi_grid_base = (MB_MODE_INFO **)aom_calloc(
239         mi_grid_size, sizeof(*mi_params->mi_grid_base));
240     if (!mi_params->mi_grid_base) return 1;
241     mi_params->mi_grid_size = mi_grid_size;
242 
243     mi_params->tx_type_map =
244         aom_calloc(mi_grid_size, sizeof(*mi_params->tx_type_map));
245     if (!mi_params->tx_type_map) return 1;
246   }
247 
248   return 0;
249 }
250 
av1_alloc_context_buffers(AV1_COMMON * cm,int width,int height)251 int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) {
252   CommonModeInfoParams *const mi_params = &cm->mi_params;
253   mi_params->set_mb_mi(mi_params, width, height);
254   if (alloc_mi(mi_params)) goto fail;
255   return 0;
256 
257 fail:
258   // clear the mi_* values to force a realloc on resync
259   mi_params->set_mb_mi(mi_params, 0, 0);
260   av1_free_context_buffers(cm);
261   return 1;
262 }
263 
av1_remove_common(AV1_COMMON * cm)264 void av1_remove_common(AV1_COMMON *cm) {
265   av1_free_context_buffers(cm);
266 
267   aom_free(cm->fc);
268   cm->fc = NULL;
269   aom_free(cm->default_frame_context);
270   cm->default_frame_context = NULL;
271 }
272 
av1_init_mi_buffers(CommonModeInfoParams * mi_params)273 void av1_init_mi_buffers(CommonModeInfoParams *mi_params) {
274   mi_params->setup_mi(mi_params);
275 }
276 
277 #if CONFIG_LPF_MASK
av1_alloc_loop_filter_mask(AV1_COMMON * cm)278 int av1_alloc_loop_filter_mask(AV1_COMMON *cm) {
279   aom_free(cm->lf.lfm);
280   cm->lf.lfm = NULL;
281 
282   // Each lfm holds bit masks for all the 4x4 blocks in a max
283   // 64x64 (128x128 for ext_partitions) region.  The stride
284   // and rows are rounded up / truncated to a multiple of 16
285   // (32 for ext_partition).
286   cm->lf.lfm_stride =
287       (cm->mi_params.mi_cols + (MI_SIZE_64X64 - 1)) >> MIN_MIB_SIZE_LOG2;
288   cm->lf.lfm_num =
289       ((cm->mi_params.mi_rows + (MI_SIZE_64X64 - 1)) >> MIN_MIB_SIZE_LOG2) *
290       cm->lf.lfm_stride;
291   cm->lf.lfm =
292       (LoopFilterMask *)aom_calloc(cm->lf.lfm_num, sizeof(*cm->lf.lfm));
293   if (!cm->lf.lfm) return 1;
294 
295   unsigned int i;
296   for (i = 0; i < cm->lf.lfm_num; ++i) av1_zero(cm->lf.lfm[i]);
297 
298   return 0;
299 }
300 
av1_free_loop_filter_mask(AV1_COMMON * cm)301 void av1_free_loop_filter_mask(AV1_COMMON *cm) {
302   if (cm->lf.lfm == NULL) return;
303 
304   aom_free(cm->lf.lfm);
305   cm->lf.lfm = NULL;
306   cm->lf.lfm_num = 0;
307   cm->lf.lfm_stride = 0;
308 }
309 #endif
310