blob: 1a8246c1f91b2754cf6ed5ff4369ec0cb8524604 [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;
David Barkerbefcc422017-01-31 09:42:10 +000096 av1_alloc_restoration_struct(cm, &cm->rst_info[0], cm->width, cm->height);
Debargha Mukherjeed7489142017-01-05 13:58:16 -080097 for (p = 1; p < MAX_MB_PLANE; ++p)
Debargha Mukherjee1a0ae842017-01-26 16:45:22 -080098 av1_alloc_restoration_struct(
David Barkerbefcc422017-01-31 09:42:10 +000099 cm, &cm->rst_info[p], ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
Debargha Mukherjee1a0ae842017-01-26 16:45:22 -0800100 ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y));
Alex Converse7f094f12017-02-23 17:29:40 -0800101 aom_free(cm->rst_internal.tmpbuf);
102 CHECK_MEM_ERROR(cm, cm->rst_internal.tmpbuf,
Yaowu Xubcf25cd2017-03-10 10:27:22 -0800103 (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800104}
105
Yaowu Xuf883b422016-08-30 14:01:10 -0700106void av1_free_restoration_buffers(AV1_COMMON *cm) {
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800107 int p;
108 for (p = 0; p < MAX_MB_PLANE; ++p)
109 av1_free_restoration_struct(&cm->rst_info[p]);
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800110 aom_free(cm->rst_internal.tmpbuf);
111 cm->rst_internal.tmpbuf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700112}
113#endif // CONFIG_LOOP_RESTORATION
114
Yaowu Xuf883b422016-08-30 14:01:10 -0700115void av1_free_context_buffers(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700116 int i;
117 cm->free_mi(cm);
118 free_seg_map(cm);
119 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700120 aom_free(cm->above_context[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700121 cm->above_context[i] = NULL;
122 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700123 aom_free(cm->above_seg_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700124 cm->above_seg_context = NULL;
125#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700126 aom_free(cm->above_txfm_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700127 cm->above_txfm_context = NULL;
128#endif
129}
130
Yaowu Xuf883b422016-08-30 14:01:10 -0700131int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132 int new_mi_size;
133
Yaowu Xuf883b422016-08-30 14:01:10 -0700134 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700135 new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
136 if (cm->mi_alloc_size < new_mi_size) {
137 cm->free_mi(cm);
138 if (cm->alloc_mi(cm, new_mi_size)) goto fail;
139 }
140
141 if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
142 // Create the segmentation map structure and set to 0.
143 free_seg_map(cm);
144 if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
145 }
146
147 if (cm->above_context_alloc_cols < cm->mi_cols) {
148 // TODO(geza.lore): These are bigger than they need to be.
149 // cm->tile_width would be enough but it complicates indexing a
150 // little elsewhere.
151 const int aligned_mi_cols =
152 ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
153 int i;
154
155 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700156 aom_free(cm->above_context[i]);
157 cm->above_context[i] = (ENTROPY_CONTEXT *)aom_calloc(
Timothy B. Terriberry5e816432017-05-05 13:58:32 -0700158 aligned_mi_cols << (MI_SIZE_LOG2 - tx_size_wide_log2[0]),
159 sizeof(*cm->above_context[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160 if (!cm->above_context[i]) goto fail;
161 }
162
Yaowu Xuf883b422016-08-30 14:01:10 -0700163 aom_free(cm->above_seg_context);
164 cm->above_seg_context = (PARTITION_CONTEXT *)aom_calloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 aligned_mi_cols, sizeof(*cm->above_seg_context));
166 if (!cm->above_seg_context) goto fail;
167
168#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700169 aom_free(cm->above_txfm_context);
170 cm->above_txfm_context = (TXFM_CONTEXT *)aom_calloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171 aligned_mi_cols, sizeof(*cm->above_txfm_context));
172 if (!cm->above_txfm_context) goto fail;
173#endif
174
175 cm->above_context_alloc_cols = aligned_mi_cols;
176 }
177
178 return 0;
179
180fail:
181 // clear the mi_* values to force a realloc on resync
Yaowu Xuf883b422016-08-30 14:01:10 -0700182 av1_set_mb_mi(cm, 0, 0);
183 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 return 1;
185}
186
Yaowu Xuf883b422016-08-30 14:01:10 -0700187void av1_remove_common(AV1_COMMON *cm) {
188 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189
Yaowu Xuf883b422016-08-30 14:01:10 -0700190 aom_free(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700191 cm->fc = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700192 aom_free(cm->frame_contexts);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 cm->frame_contexts = NULL;
194}
195
Yaowu Xuf883b422016-08-30 14:01:10 -0700196void av1_init_context_buffers(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197 cm->setup_mi(cm);
198 if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
199 memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
200}
201
Yaowu Xuf883b422016-08-30 14:01:10 -0700202void av1_swap_current_and_last_seg_map(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 // Swap indices.
204 const int tmp = cm->seg_map_idx;
205 cm->seg_map_idx = cm->prev_seg_map_idx;
206 cm->prev_seg_map_idx = tmp;
207
208 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
209 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
210}