blob: dab01168f70fafd24ee412471decd8d223cbe2a4 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Yaowu Xu9c01aa12016-09-01 14:32:49 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Yaowu Xu9c01aa12016-09-01 14:32:49 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
Yaowu Xuf883b422016-08-30 14:01:10 -070012#include "./aom_config.h"
13#include "aom_mem/aom_mem.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070014
15#include "av1/common/alloccommon.h"
16#include "av1/common/blockd.h"
17#include "av1/common/entropymode.h"
18#include "av1/common/entropymv.h"
19#include "av1/common/onyxc_int.h"
20
Yaowu Xuf883b422016-08-30 14:01:10 -070021void av1_set_mb_mi(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070022 const int aligned_width = ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2);
23 const int aligned_height = ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2);
24
25 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
26 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
27 cm->mi_stride = calc_mi_size(cm->mi_cols);
28
29 cm->mb_cols = (cm->mi_cols + 1) >> 1;
30 cm->mb_rows = (cm->mi_rows + 1) >> 1;
31 cm->MBs = cm->mb_rows * cm->mb_cols;
32}
33
Yaowu Xuf883b422016-08-30 14:01:10 -070034static int alloc_seg_map(AV1_COMMON *cm, int seg_map_size) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070035 int i;
36
37 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -070038 cm->seg_map_array[i] = (uint8_t *)aom_calloc(seg_map_size, 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -070039 if (cm->seg_map_array[i] == NULL) return 1;
40 }
41 cm->seg_map_alloc_size = seg_map_size;
42
43 // Init the index.
44 cm->seg_map_idx = 0;
45 cm->prev_seg_map_idx = 1;
46
47 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
48 if (!cm->frame_parallel_decode)
49 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
50
51 return 0;
52}
53
Yaowu Xuf883b422016-08-30 14:01:10 -070054static void free_seg_map(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070055 int i;
56
57 for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -070058 aom_free(cm->seg_map_array[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -070059 cm->seg_map_array[i] = NULL;
60 }
61
62 cm->current_frame_seg_map = NULL;
63
64 if (!cm->frame_parallel_decode) {
65 cm->last_frame_seg_map = NULL;
66 }
67}
68
Yaowu Xuf883b422016-08-30 14:01:10 -070069void av1_free_ref_frame_buffers(BufferPool *pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070070 int i;
71
72 for (i = 0; i < FRAME_BUFFERS; ++i) {
73 if (pool->frame_bufs[i].ref_count > 0 &&
74 pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
75 pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
76 pool->frame_bufs[i].ref_count = 0;
77 }
Yaowu Xuf883b422016-08-30 14:01:10 -070078 aom_free(pool->frame_bufs[i].mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -070079 pool->frame_bufs[i].mvs = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -070080 aom_free_frame_buffer(&pool->frame_bufs[i].buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -070081 }
82}
83
84#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -070085void av1_free_restoration_buffers(AV1_COMMON *cm) {
86 aom_free(cm->rst_info.bilateral_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -070087 cm->rst_info.bilateral_level = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -070088 aom_free(cm->rst_info.vfilter);
Yaowu Xuc27fc142016-08-22 16:08:15 -070089 cm->rst_info.vfilter = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -070090 aom_free(cm->rst_info.hfilter);
Yaowu Xuc27fc142016-08-22 16:08:15 -070091 cm->rst_info.hfilter = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -070092 aom_free(cm->rst_info.wiener_level);
Yaowu Xuc27fc142016-08-22 16:08:15 -070093 cm->rst_info.wiener_level = NULL;
94}
95#endif // CONFIG_LOOP_RESTORATION
96
Yaowu Xuf883b422016-08-30 14:01:10 -070097void av1_free_context_buffers(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070098 int i;
99 cm->free_mi(cm);
100 free_seg_map(cm);
101 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700102 aom_free(cm->above_context[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700103 cm->above_context[i] = NULL;
104 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700105 aom_free(cm->above_seg_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700106 cm->above_seg_context = NULL;
107#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700108 aom_free(cm->above_txfm_context);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700109 cm->above_txfm_context = NULL;
110#endif
111}
112
Yaowu Xuf883b422016-08-30 14:01:10 -0700113int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700114 int new_mi_size;
115
Yaowu Xuf883b422016-08-30 14:01:10 -0700116 av1_set_mb_mi(cm, width, height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700117 new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
118 if (cm->mi_alloc_size < new_mi_size) {
119 cm->free_mi(cm);
120 if (cm->alloc_mi(cm, new_mi_size)) goto fail;
121 }
122
123 if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
124 // Create the segmentation map structure and set to 0.
125 free_seg_map(cm);
126 if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
127 }
128
129 if (cm->above_context_alloc_cols < cm->mi_cols) {
130 // TODO(geza.lore): These are bigger than they need to be.
131 // cm->tile_width would be enough but it complicates indexing a
132 // little elsewhere.
133 const int aligned_mi_cols =
134 ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
135 int i;
136
137 for (i = 0; i < MAX_MB_PLANE; i++) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700138 aom_free(cm->above_context[i]);
139 cm->above_context[i] = (ENTROPY_CONTEXT *)aom_calloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140 2 * aligned_mi_cols, sizeof(*cm->above_context[0]));
141 if (!cm->above_context[i]) goto fail;
142 }
143
Yaowu Xuf883b422016-08-30 14:01:10 -0700144 aom_free(cm->above_seg_context);
145 cm->above_seg_context = (PARTITION_CONTEXT *)aom_calloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700146 aligned_mi_cols, sizeof(*cm->above_seg_context));
147 if (!cm->above_seg_context) goto fail;
148
149#if CONFIG_VAR_TX
Yaowu Xuf883b422016-08-30 14:01:10 -0700150 aom_free(cm->above_txfm_context);
151 cm->above_txfm_context = (TXFM_CONTEXT *)aom_calloc(
Yaowu Xuc27fc142016-08-22 16:08:15 -0700152 aligned_mi_cols, sizeof(*cm->above_txfm_context));
153 if (!cm->above_txfm_context) goto fail;
154#endif
155
156 cm->above_context_alloc_cols = aligned_mi_cols;
157 }
158
159 return 0;
160
161fail:
162 // clear the mi_* values to force a realloc on resync
Yaowu Xuf883b422016-08-30 14:01:10 -0700163 av1_set_mb_mi(cm, 0, 0);
164 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 return 1;
166}
167
Yaowu Xuf883b422016-08-30 14:01:10 -0700168void av1_remove_common(AV1_COMMON *cm) {
169 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170
Yaowu Xuf883b422016-08-30 14:01:10 -0700171 aom_free(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172 cm->fc = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700173 aom_free(cm->frame_contexts);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700174 cm->frame_contexts = NULL;
175}
176
Yaowu Xuf883b422016-08-30 14:01:10 -0700177void av1_init_context_buffers(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178 cm->setup_mi(cm);
179 if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
180 memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
181}
182
Yaowu Xuf883b422016-08-30 14:01:10 -0700183void av1_swap_current_and_last_seg_map(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700184 // Swap indices.
185 const int tmp = cm->seg_map_idx;
186 cm->seg_map_idx = cm->prev_seg_map_idx;
187 cm->prev_seg_map_idx = tmp;
188
189 cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
190 cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
191}