blob: c37f1ea509389aa24845d7be816b0944efce768f [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
Yaowu Xuf883b422016-08-30 14:01:10 -070022void av1_set_mb_mi(AV1_COMMON *cm, int width, int height) {
Jingning Han944b8052017-03-31 08:57:30 -070023 // TODO(jingning): Fine tune the loop filter operations and bring this
24 // back to integer multiple of 4 for cb4x4.
25 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
26 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -070027
28 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
29 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
30 cm->mi_stride = calc_mi_size(cm->mi_cols);
31
Jingning Han91ad0e82016-12-07 17:59:32 -080032#if CONFIG_CB4X4
33 cm->mb_cols = (cm->mi_cols + 2) >> 2;
34 cm->mb_rows = (cm->mi_rows + 2) >> 2;
35#else
Yaowu Xuc27fc142016-08-22 16:08:15 -070036 cm->mb_cols = (cm->mi_cols + 1) >> 1;
37 cm->mb_rows = (cm->mi_rows + 1) >> 1;
Jingning Han91ad0e82016-12-07 17:59:32 -080038#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070039 cm->MBs = cm->mb_rows * cm->mb_cols;
40}
41
Yaowu Xuf883b422016-08-30 14:01:10 -070042static int alloc_seg_map(AV1_COMMON *cm, int seg_map_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070043 int i;
44
45 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -070046 cm->seg_map_array[i] = (uint8_t *)aom_calloc(seg_map_size, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -070047 if (cm->seg_map_array[i] == NULL) return 1;
48 }
49 cm->seg_map_alloc_size = seg_map_size;
50
51 // Init the index.
52 cm->seg_map_idx = 0;
53 cm->prev_seg_map_idx = 1;
54
55 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
56 if (!cm->frame_parallel_decode)
57 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
58
59 return 0;
60}
61
Yaowu Xuf883b422016-08-30 14:01:10 -070062static void free_seg_map(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070063 int i;
64
65 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -070066 aom_free(cm->seg_map_array[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070067 cm->seg_map_array[i] = NULL;
68 }
69
70 cm->current_frame_seg_map = NULL;
71
72 if (!cm->frame_parallel_decode) {
73 cm->last_frame_seg_map = NULL;
74 }
75}
76
Yaowu Xuf883b422016-08-30 14:01:10 -070077void av1_free_ref_frame_buffers(BufferPool *pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070078 int i;
79
80 for (i = 0; i < FRAME_BUFFERS; ++i) {
81 if (pool->frame_bufs[i].ref_count > 0 &&
82 pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
83 pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
84 pool->frame_bufs[i].ref_count = 0;
85 }
Yaowu Xuf883b422016-08-30 14:01:10 -070086 aom_free(pool->frame_bufs[i].mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -070087 pool->frame_bufs[i].mvs = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -070088 aom_free_frame_buffer(&pool->frame_bufs[i].buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -070089 }
90}
91
92#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -080093// Assumes cm->rst_info[p].restoration_tilesize is already initialized
Debargha Mukherjee874d36d2016-12-14 16:53:17 -080094void av1_alloc_restoration_buffers(AV1_COMMON *cm) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -080095 int p;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -070096#if CONFIG_FRAME_SUPERRES
97 int width = cm->superres_upscaled_width;
98 int height = cm->superres_upscaled_height;
99#else
100 int width = cm->width;
101 int height = cm->height;
102#endif // CONFIG_FRAME_SUPERRES
103 av1_alloc_restoration_struct(cm, &cm->rst_info[0], width, height);
Debargha Mukherjeed7489142017-01-05 13:58:16 -0800104 for (p = 1; p < MAX_MB_PLANE; ++p)
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700105 av1_alloc_restoration_struct(cm, &cm->rst_info[p],
106 ROUND_POWER_OF_TWO(width, cm->subsampling_x),
107 ROUND_POWER_OF_TWO(height, cm->subsampling_y));
Alex Converse7f094f12017-02-23 17:29:40 -0800108 aom_free(cm->rst_internal.tmpbuf);
109 CHECK_MEM_ERROR(cm, cm->rst_internal.tmpbuf,
Yaowu Xubcf25cd2017-03-10 10:27:22 -0800110 (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800111}
112
Yaowu Xuf883b422016-08-30 14:01:10 -0700113void av1_free_restoration_buffers(AV1_COMMON *cm) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800114 int p;
115 for (p = 0; p < MAX_MB_PLANE; ++p)
116 av1_free_restoration_struct(&cm->rst_info[p]);
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800117 aom_free(cm->rst_internal.tmpbuf);
118 cm->rst_internal.tmpbuf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700119}
120#endif // CONFIG_LOOP_RESTORATION
121
Yaowu Xuf883b422016-08-30 14:01:10 -0700122void av1_free_context_buffers(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700123 int i;
124 cm->free_mi(cm);
125 free_seg_map(cm);
126 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700127 aom_free(cm->above_context[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 cm->above_context[i] = NULL;
129 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700130 aom_free(cm->above_seg_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700131 cm->above_seg_context = NULL;
132#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700133 aom_free(cm->above_txfm_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134 cm->above_txfm_context = NULL;
Jingning Han6e4955d2017-05-30 22:54:48 -0700135
136 for (i = 0; i < MAX_MB_PLANE; ++i) {
137 aom_free(cm->top_txfm_context[i]);
138 cm->top_txfm_context[i] = NULL;
139 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140#endif
141}
142
Yaowu Xuf883b422016-08-30 14:01:10 -0700143int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700144 int new_mi_size;
145
Yaowu Xuf883b422016-08-30 14:01:10 -0700146 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700147 new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
148 if (cm->mi_alloc_size < new_mi_size) {
149 cm->free_mi(cm);
150 if (cm->alloc_mi(cm, new_mi_size)) goto fail;
151 }
152
153 if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
154 // Create the segmentation map structure and set to 0.
155 free_seg_map(cm);
156 if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
157 }
158
159 if (cm->above_context_alloc_cols < cm->mi_cols) {
160 // TODO(geza.lore): These are bigger than they need to be.
161 // cm->tile_width would be enough but it complicates indexing a
162 // little elsewhere.
163 const int aligned_mi_cols =
164 ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
165 int i;
166
167 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700168 aom_free(cm->above_context[i]);
169 cm->above_context[i] = (ENTROPY_CONTEXT *)aom_calloc(
Timothy B. Terriberry5e816432017-05-05 13:58:32 -0700170 aligned_mi_cols << (MI_SIZE_LOG2 - tx_size_wide_log2[0]),
171 sizeof(*cm->above_context[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172 if (!cm->above_context[i]) goto fail;
173 }
174
Yaowu Xuf883b422016-08-30 14:01:10 -0700175 aom_free(cm->above_seg_context);
176 cm->above_seg_context = (PARTITION_CONTEXT *)aom_calloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177 aligned_mi_cols, sizeof(*cm->above_seg_context));
178 if (!cm->above_seg_context) goto fail;
179
180#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700181 aom_free(cm->above_txfm_context);
182 cm->above_txfm_context = (TXFM_CONTEXT *)aom_calloc(
Jingning Han331662e2017-05-30 17:03:32 -0700183 aligned_mi_cols << TX_UNIT_WIDE_LOG2, sizeof(*cm->above_txfm_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 if (!cm->above_txfm_context) goto fail;
Jingning Han6e4955d2017-05-30 22:54:48 -0700185
186 for (i = 0; i < MAX_MB_PLANE; ++i) {
187 aom_free(cm->top_txfm_context[i]);
188 cm->top_txfm_context[i] =
189 (TXFM_CONTEXT *)aom_calloc(aligned_mi_cols << TX_UNIT_WIDE_LOG2,
190 sizeof(*cm->top_txfm_context[0]));
191 if (!cm->top_txfm_context[i]) goto fail;
192 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193#endif
194
195 cm->above_context_alloc_cols = aligned_mi_cols;
196 }
197
198 return 0;
199
200fail:
201 // clear the mi_* values to force a realloc on resync
Yaowu Xuf883b422016-08-30 14:01:10 -0700202 av1_set_mb_mi(cm, 0, 0);
203 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204 return 1;
205}
206
Yaowu Xuf883b422016-08-30 14:01:10 -0700207void av1_remove_common(AV1_COMMON *cm) {
208 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209
Yaowu Xuf883b422016-08-30 14:01:10 -0700210 aom_free(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211 cm->fc = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700212 aom_free(cm->frame_contexts);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700213 cm->frame_contexts = NULL;
214}
215
Yaowu Xuf883b422016-08-30 14:01:10 -0700216void av1_init_context_buffers(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700217 cm->setup_mi(cm);
218 if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
219 memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
220}
221
Yaowu Xuf883b422016-08-30 14:01:10 -0700222void av1_swap_current_and_last_seg_map(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223 // Swap indices.
224 const int tmp = cm->seg_map_idx;
225 cm->seg_map_idx = cm->prev_seg_map_idx;
226 cm->prev_seg_map_idx = tmp;
227
228 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
229 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
230}