blob: fd635686f1274e78fe29cfebbbe7aaa9a8841f9c [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Debargha Mukherjee999d2f62016-12-15 13:23:21 -08002 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07003 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07004 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07005 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070011 */
12
Yaowu Xuf883b422016-08-30 14:01:10 -070013#include "./aom_config.h"
14#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070015
16#include "av1/common/alloccommon.h"
17#include "av1/common/blockd.h"
18#include "av1/common/entropymode.h"
19#include "av1/common/entropymv.h"
20#include "av1/common/onyxc_int.h"
21
Debargha Mukherjee7166f222017-09-05 21:32:42 -070022int av1_get_MBs(int width, int height) {
23 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
24 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
25 const int mi_cols = aligned_width >> MI_SIZE_LOG2;
26 const int mi_rows = aligned_height >> MI_SIZE_LOG2;
27
28#if CONFIG_CB4X4
29 const int mb_cols = (mi_cols + 2) >> 2;
30 const int mb_rows = (mi_rows + 2) >> 2;
31#else
32 const int mb_cols = (mi_cols + 1) >> 1;
33 const int mb_rows = (mi_rows + 1) >> 1;
34#endif
35 return mb_rows * mb_cols;
36}
37
Yaowu Xuf883b422016-08-30 14:01:10 -070038void av1_set_mb_mi(AV1_COMMON *cm, int width, int height) {
David Barkeree0fd922017-09-07 15:18:35 +010039 // Ensure that the decoded width and height are both multiples of
40 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
41 // subsampling is used).
42 // This simplifies the implementation of various experiments,
43 // eg. cdef, which operates on units of 8x8 luma pixels.
Jingning Han944b8052017-03-31 08:57:30 -070044 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
45 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -070046
47 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
48 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
49 cm->mi_stride = calc_mi_size(cm->mi_cols);
50
Jingning Han91ad0e82016-12-07 17:59:32 -080051#if CONFIG_CB4X4
52 cm->mb_cols = (cm->mi_cols + 2) >> 2;
53 cm->mb_rows = (cm->mi_rows + 2) >> 2;
54#else
Yaowu Xuc27fc142016-08-22 16:08:15 -070055 cm->mb_cols = (cm->mi_cols + 1) >> 1;
56 cm->mb_rows = (cm->mi_rows + 1) >> 1;
Jingning Han91ad0e82016-12-07 17:59:32 -080057#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070058 cm->MBs = cm->mb_rows * cm->mb_cols;
59}
60
Yaowu Xuf883b422016-08-30 14:01:10 -070061static int alloc_seg_map(AV1_COMMON *cm, int seg_map_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070062 int i;
63
64 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -070065 cm->seg_map_array[i] = (uint8_t *)aom_calloc(seg_map_size, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -070066 if (cm->seg_map_array[i] == NULL) return 1;
67 }
68 cm->seg_map_alloc_size = seg_map_size;
69
70 // Init the index.
71 cm->seg_map_idx = 0;
72 cm->prev_seg_map_idx = 1;
73
74 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
75 if (!cm->frame_parallel_decode)
76 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
77
78 return 0;
79}
80
Yaowu Xuf883b422016-08-30 14:01:10 -070081static void free_seg_map(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070082 int i;
83
84 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -070085 aom_free(cm->seg_map_array[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070086 cm->seg_map_array[i] = NULL;
87 }
88
89 cm->current_frame_seg_map = NULL;
90
91 if (!cm->frame_parallel_decode) {
92 cm->last_frame_seg_map = NULL;
93 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -070094 cm->seg_map_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -070095}
96
Debargha Mukherjee5d108a32017-10-05 19:47:08 -070097static void free_scratch_buffers(AV1_COMMON *cm) {
98 (void)cm;
99#if CONFIG_NCOBMC && CONFIG_NCOBMC_ADAPT_WEIGHT
100 for (int i = 0; i < 4; ++i) {
101 if (cm->ncobmcaw_buf[i]) {
102 aom_free(cm->ncobmcaw_buf[i]);
103 cm->ncobmcaw_buf[i] = NULL;
104 }
105 }
106#endif // CONFIG_NCOBMC && CONFIG_NCOBMC_ADAPT_WEIGHT
107}
108
109static int alloc_scratch_buffers(AV1_COMMON *cm) {
110 (void)cm;
111#if CONFIG_NCOBMC && CONFIG_NCOBMC_ADAPT_WEIGHT
112 // If not allocated already, allocate
113 if (!cm->ncobmcaw_buf[0] && !cm->ncobmcaw_buf[1] && !cm->ncobmcaw_buf[2] &&
114 !cm->ncobmcaw_buf[3]) {
115 for (int i = 0; i < 4; ++i) {
116 CHECK_MEM_ERROR(
117 cm, cm->ncobmcaw_buf[i],
118 (uint8_t *)aom_memalign(
119 16, (1 + CONFIG_HIGHBITDEPTH) * MAX_MB_PLANE * MAX_SB_SQUARE));
120 }
121 }
122#endif // CONFIG_NCOBMC && CONFIG_NCOBMC_ADAPT_WEIGHT
123 return 0;
124}
125
Yaowu Xuf883b422016-08-30 14:01:10 -0700126void av1_free_ref_frame_buffers(BufferPool *pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700127 int i;
128
129 for (i = 0; i < FRAME_BUFFERS; ++i) {
130 if (pool->frame_bufs[i].ref_count > 0 &&
131 pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
132 pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
133 pool->frame_bufs[i].ref_count = 0;
134 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700135 aom_free(pool->frame_bufs[i].mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700136 pool->frame_bufs[i].mvs = NULL;
Jingning Hanffbb0f92017-08-24 11:52:21 -0700137#if CONFIG_MFMV
138 aom_free(pool->frame_bufs[i].tpl_mvs);
139 pool->frame_bufs[i].tpl_mvs = NULL;
140#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700141 aom_free_frame_buffer(&pool->frame_bufs[i].buf);
RogerZhoucc5d35d2017-08-07 22:20:15 -0700142#if CONFIG_HASH_ME
143 av1_hash_table_destroy(&pool->frame_bufs[i].hash_table);
144#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700145 }
146}
147
148#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -0800149// Assumes cm->rst_info[p].restoration_tilesize is already initialized
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800150void av1_alloc_restoration_buffers(AV1_COMMON *cm) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800151 int p;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700152#if CONFIG_FRAME_SUPERRES
153 int width = cm->superres_upscaled_width;
154 int height = cm->superres_upscaled_height;
155#else
156 int width = cm->width;
157 int height = cm->height;
158#endif // CONFIG_FRAME_SUPERRES
159 av1_alloc_restoration_struct(cm, &cm->rst_info[0], width, height);
Debargha Mukherjeed7489142017-01-05 13:58:16 -0800160 for (p = 1; p < MAX_MB_PLANE; ++p)
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700161 av1_alloc_restoration_struct(cm, &cm->rst_info[p],
162 ROUND_POWER_OF_TWO(width, cm->subsampling_x),
163 ROUND_POWER_OF_TWO(height, cm->subsampling_y));
Alex Converse7f094f12017-02-23 17:29:40 -0800164 aom_free(cm->rst_internal.tmpbuf);
165 CHECK_MEM_ERROR(cm, cm->rst_internal.tmpbuf,
Yaowu Xubcf25cd2017-03-10 10:27:22 -0800166 (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
Ola Hugosson1e7f2d02017-09-22 21:36:26 +0200167
168#if CONFIG_STRIPED_LOOP_RESTORATION
169 // Allocate internal storage for the loop restoration stripe boundary lines
170 for (p = 0; p < MAX_MB_PLANE; ++p) {
171 int w = p == 0 ? width : ROUND_POWER_OF_TWO(width, cm->subsampling_x);
172 int align_bits = 5; // align for efficiency
173 int stride = ALIGN_POWER_OF_TWO(w, align_bits);
174 int num_stripes = (height + 63) / 64;
175 // for each processing stripe: 2 lines above, 2 below
176 int buf_size = num_stripes * 2 * stride;
177 uint8_t *above_buf, *below_buf;
178
179 aom_free(cm->rst_internal.stripe_boundary_above[p]);
180 aom_free(cm->rst_internal.stripe_boundary_below[p]);
181
182#if CONFIG_HIGHBITDEPTH
183 if (cm->use_highbitdepth) buf_size = buf_size * 2;
184#endif
185 CHECK_MEM_ERROR(cm, above_buf,
186 (uint8_t *)aom_memalign(1 << align_bits, buf_size));
187 CHECK_MEM_ERROR(cm, below_buf,
188 (uint8_t *)aom_memalign(1 << align_bits, buf_size));
189 cm->rst_internal.stripe_boundary_above[p] = above_buf;
190 cm->rst_internal.stripe_boundary_below[p] = below_buf;
191 cm->rst_internal.stripe_boundary_stride[p] = stride;
192 }
193#endif // CONFIG_STRIPED_LOOP_RESTORATION
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800194}
195
Yaowu Xuf883b422016-08-30 14:01:10 -0700196void av1_free_restoration_buffers(AV1_COMMON *cm) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800197 int p;
198 for (p = 0; p < MAX_MB_PLANE; ++p)
199 av1_free_restoration_struct(&cm->rst_info[p]);
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800200 aom_free(cm->rst_internal.tmpbuf);
201 cm->rst_internal.tmpbuf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202}
203#endif // CONFIG_LOOP_RESTORATION
204
Yaowu Xuf883b422016-08-30 14:01:10 -0700205void av1_free_context_buffers(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700206 int i;
207 cm->free_mi(cm);
208 free_seg_map(cm);
Debargha Mukherjee5d108a32017-10-05 19:47:08 -0700209 free_scratch_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700211 aom_free(cm->above_context[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700212 cm->above_context[i] = NULL;
213 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700214 aom_free(cm->above_seg_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700215 cm->above_seg_context = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700216 cm->above_context_alloc_cols = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700217#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700218 aom_free(cm->above_txfm_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 cm->above_txfm_context = NULL;
Jingning Han6e4955d2017-05-30 22:54:48 -0700220
221 for (i = 0; i < MAX_MB_PLANE; ++i) {
222 aom_free(cm->top_txfm_context[i]);
223 cm->top_txfm_context[i] = NULL;
224 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225#endif
226}
227
Yaowu Xuf883b422016-08-30 14:01:10 -0700228int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700229 int new_mi_size;
230
Yaowu Xuf883b422016-08-30 14:01:10 -0700231 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700232 new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
233 if (cm->mi_alloc_size < new_mi_size) {
234 cm->free_mi(cm);
235 if (cm->alloc_mi(cm, new_mi_size)) goto fail;
236 }
237
238 if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
239 // Create the segmentation map structure and set to 0.
240 free_seg_map(cm);
241 if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
242 }
Debargha Mukherjee5d108a32017-10-05 19:47:08 -0700243 if (alloc_scratch_buffers(cm)) goto fail;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700244
245 if (cm->above_context_alloc_cols < cm->mi_cols) {
246 // TODO(geza.lore): These are bigger than they need to be.
247 // cm->tile_width would be enough but it complicates indexing a
248 // little elsewhere.
249 const int aligned_mi_cols =
250 ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
251 int i;
252
253 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700254 aom_free(cm->above_context[i]);
255 cm->above_context[i] = (ENTROPY_CONTEXT *)aom_calloc(
Timothy B. Terriberry5e816432017-05-05 13:58:32 -0700256 aligned_mi_cols << (MI_SIZE_LOG2 - tx_size_wide_log2[0]),
257 sizeof(*cm->above_context[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 if (!cm->above_context[i]) goto fail;
259 }
260
Yaowu Xuf883b422016-08-30 14:01:10 -0700261 aom_free(cm->above_seg_context);
262 cm->above_seg_context = (PARTITION_CONTEXT *)aom_calloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263 aligned_mi_cols, sizeof(*cm->above_seg_context));
264 if (!cm->above_seg_context) goto fail;
265
266#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700267 aom_free(cm->above_txfm_context);
268 cm->above_txfm_context = (TXFM_CONTEXT *)aom_calloc(
Jingning Han331662e2017-05-30 17:03:32 -0700269 aligned_mi_cols << TX_UNIT_WIDE_LOG2, sizeof(*cm->above_txfm_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 if (!cm->above_txfm_context) goto fail;
Jingning Han6e4955d2017-05-30 22:54:48 -0700271
272 for (i = 0; i < MAX_MB_PLANE; ++i) {
273 aom_free(cm->top_txfm_context[i]);
274 cm->top_txfm_context[i] =
275 (TXFM_CONTEXT *)aom_calloc(aligned_mi_cols << TX_UNIT_WIDE_LOG2,
276 sizeof(*cm->top_txfm_context[0]));
277 if (!cm->top_txfm_context[i]) goto fail;
278 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279#endif
280
281 cm->above_context_alloc_cols = aligned_mi_cols;
282 }
283
284 return 0;
285
286fail:
287 // clear the mi_* values to force a realloc on resync
Yaowu Xuf883b422016-08-30 14:01:10 -0700288 av1_set_mb_mi(cm, 0, 0);
289 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 return 1;
291}
292
Yaowu Xuf883b422016-08-30 14:01:10 -0700293void av1_remove_common(AV1_COMMON *cm) {
294 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295
Yaowu Xuf883b422016-08-30 14:01:10 -0700296 aom_free(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700297 cm->fc = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700298 aom_free(cm->frame_contexts);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700299 cm->frame_contexts = NULL;
300}
301
Yaowu Xuf883b422016-08-30 14:01:10 -0700302void av1_init_context_buffers(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700303 cm->setup_mi(cm);
304 if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
305 memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
306}
307
Yaowu Xuf883b422016-08-30 14:01:10 -0700308void av1_swap_current_and_last_seg_map(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309 // Swap indices.
310 const int tmp = cm->seg_map_idx;
311 cm->seg_map_idx = cm->prev_seg_map_idx;
312 cm->prev_seg_map_idx = tmp;
313
314 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
315 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
316}