Line data Source code
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 "./aom_config.h"
14 : #include "aom_mem/aom_mem.h"
15 :
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 :
22 0 : void av1_set_mb_mi(AV1_COMMON *cm, int width, int height) {
23 : // TODO(jingning): Fine tune the loop filter operations and bring this
24 : // back to integer multiple of 4 for cb4x4.
25 0 : const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
26 0 : const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
27 :
28 0 : cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
29 0 : cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
30 0 : cm->mi_stride = calc_mi_size(cm->mi_cols);
31 :
32 : #if CONFIG_CB4X4
33 0 : cm->mb_cols = (cm->mi_cols + 2) >> 2;
34 0 : cm->mb_rows = (cm->mi_rows + 2) >> 2;
35 : #else
36 : cm->mb_cols = (cm->mi_cols + 1) >> 1;
37 : cm->mb_rows = (cm->mi_rows + 1) >> 1;
38 : #endif
39 0 : cm->MBs = cm->mb_rows * cm->mb_cols;
40 0 : }
41 :
42 0 : static int alloc_seg_map(AV1_COMMON *cm, int seg_map_size) {
43 : int i;
44 :
45 0 : for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
46 0 : cm->seg_map_array[i] = (uint8_t *)aom_calloc(seg_map_size, 1);
47 0 : if (cm->seg_map_array[i] == NULL) return 1;
48 : }
49 0 : cm->seg_map_alloc_size = seg_map_size;
50 :
51 : // Init the index.
52 0 : cm->seg_map_idx = 0;
53 0 : cm->prev_seg_map_idx = 1;
54 :
55 0 : cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
56 0 : if (!cm->frame_parallel_decode)
57 0 : cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
58 :
59 0 : return 0;
60 : }
61 :
62 0 : static void free_seg_map(AV1_COMMON *cm) {
63 : int i;
64 :
65 0 : for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
66 0 : aom_free(cm->seg_map_array[i]);
67 0 : cm->seg_map_array[i] = NULL;
68 : }
69 :
70 0 : cm->current_frame_seg_map = NULL;
71 :
72 0 : if (!cm->frame_parallel_decode) {
73 0 : cm->last_frame_seg_map = NULL;
74 : }
75 0 : }
76 :
77 0 : void av1_free_ref_frame_buffers(BufferPool *pool) {
78 : int i;
79 :
80 0 : for (i = 0; i < FRAME_BUFFERS; ++i) {
81 0 : if (pool->frame_bufs[i].ref_count > 0 &&
82 0 : pool->frame_bufs[i].raw_frame_buffer.data != NULL) {
83 0 : pool->release_fb_cb(pool->cb_priv, &pool->frame_bufs[i].raw_frame_buffer);
84 0 : pool->frame_bufs[i].ref_count = 0;
85 : }
86 0 : aom_free(pool->frame_bufs[i].mvs);
87 0 : pool->frame_bufs[i].mvs = NULL;
88 0 : aom_free_frame_buffer(&pool->frame_bufs[i].buf);
89 : }
90 0 : }
91 :
92 : #if CONFIG_LOOP_RESTORATION
93 : // Assumes cm->rst_info[p].restoration_tilesize is already initialized
94 : void av1_alloc_restoration_buffers(AV1_COMMON *cm) {
95 : int p;
96 : av1_alloc_restoration_struct(cm, &cm->rst_info[0], cm->width, cm->height);
97 : for (p = 1; p < MAX_MB_PLANE; ++p)
98 : av1_alloc_restoration_struct(
99 : cm, &cm->rst_info[p], ROUND_POWER_OF_TWO(cm->width, cm->subsampling_x),
100 : ROUND_POWER_OF_TWO(cm->height, cm->subsampling_y));
101 : aom_free(cm->rst_internal.tmpbuf);
102 : CHECK_MEM_ERROR(cm, cm->rst_internal.tmpbuf,
103 : (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
104 : }
105 :
106 : void av1_free_restoration_buffers(AV1_COMMON *cm) {
107 : int p;
108 : for (p = 0; p < MAX_MB_PLANE; ++p)
109 : av1_free_restoration_struct(&cm->rst_info[p]);
110 : aom_free(cm->rst_internal.tmpbuf);
111 : cm->rst_internal.tmpbuf = NULL;
112 : }
113 : #endif // CONFIG_LOOP_RESTORATION
114 :
115 0 : void av1_free_context_buffers(AV1_COMMON *cm) {
116 : int i;
117 0 : cm->free_mi(cm);
118 0 : free_seg_map(cm);
119 0 : for (i = 0; i < MAX_MB_PLANE; i++) {
120 0 : aom_free(cm->above_context[i]);
121 0 : cm->above_context[i] = NULL;
122 : }
123 0 : aom_free(cm->above_seg_context);
124 0 : cm->above_seg_context = NULL;
125 : #if CONFIG_VAR_TX
126 0 : aom_free(cm->above_txfm_context);
127 0 : cm->above_txfm_context = NULL;
128 :
129 0 : for (i = 0; i < MAX_MB_PLANE; ++i) {
130 0 : aom_free(cm->top_txfm_context[i]);
131 0 : cm->top_txfm_context[i] = NULL;
132 : }
133 : #endif
134 0 : }
135 :
136 0 : int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) {
137 : int new_mi_size;
138 :
139 0 : av1_set_mb_mi(cm, width, height);
140 0 : new_mi_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
141 0 : if (cm->mi_alloc_size < new_mi_size) {
142 0 : cm->free_mi(cm);
143 0 : if (cm->alloc_mi(cm, new_mi_size)) goto fail;
144 : }
145 :
146 0 : if (cm->seg_map_alloc_size < cm->mi_rows * cm->mi_cols) {
147 : // Create the segmentation map structure and set to 0.
148 0 : free_seg_map(cm);
149 0 : if (alloc_seg_map(cm, cm->mi_rows * cm->mi_cols)) goto fail;
150 : }
151 :
152 0 : if (cm->above_context_alloc_cols < cm->mi_cols) {
153 : // TODO(geza.lore): These are bigger than they need to be.
154 : // cm->tile_width would be enough but it complicates indexing a
155 : // little elsewhere.
156 0 : const int aligned_mi_cols =
157 0 : ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
158 : int i;
159 :
160 0 : for (i = 0; i < MAX_MB_PLANE; i++) {
161 0 : aom_free(cm->above_context[i]);
162 0 : cm->above_context[i] = (ENTROPY_CONTEXT *)aom_calloc(
163 0 : aligned_mi_cols << (MI_SIZE_LOG2 - tx_size_wide_log2[0]),
164 : sizeof(*cm->above_context[0]));
165 0 : if (!cm->above_context[i]) goto fail;
166 : }
167 :
168 0 : aom_free(cm->above_seg_context);
169 0 : cm->above_seg_context = (PARTITION_CONTEXT *)aom_calloc(
170 : aligned_mi_cols, sizeof(*cm->above_seg_context));
171 0 : if (!cm->above_seg_context) goto fail;
172 :
173 : #if CONFIG_VAR_TX
174 0 : aom_free(cm->above_txfm_context);
175 0 : cm->above_txfm_context = (TXFM_CONTEXT *)aom_calloc(
176 0 : aligned_mi_cols << TX_UNIT_WIDE_LOG2, sizeof(*cm->above_txfm_context));
177 0 : if (!cm->above_txfm_context) goto fail;
178 :
179 0 : for (i = 0; i < MAX_MB_PLANE; ++i) {
180 0 : aom_free(cm->top_txfm_context[i]);
181 0 : cm->top_txfm_context[i] =
182 0 : (TXFM_CONTEXT *)aom_calloc(aligned_mi_cols << TX_UNIT_WIDE_LOG2,
183 : sizeof(*cm->top_txfm_context[0]));
184 0 : if (!cm->top_txfm_context[i]) goto fail;
185 : }
186 : #endif
187 :
188 0 : cm->above_context_alloc_cols = aligned_mi_cols;
189 : }
190 :
191 0 : return 0;
192 :
193 : fail:
194 : // clear the mi_* values to force a realloc on resync
195 0 : av1_set_mb_mi(cm, 0, 0);
196 0 : av1_free_context_buffers(cm);
197 0 : return 1;
198 : }
199 :
200 0 : void av1_remove_common(AV1_COMMON *cm) {
201 0 : av1_free_context_buffers(cm);
202 :
203 0 : aom_free(cm->fc);
204 0 : cm->fc = NULL;
205 0 : aom_free(cm->frame_contexts);
206 0 : cm->frame_contexts = NULL;
207 0 : }
208 :
209 0 : void av1_init_context_buffers(AV1_COMMON *cm) {
210 0 : cm->setup_mi(cm);
211 0 : if (cm->last_frame_seg_map && !cm->frame_parallel_decode)
212 0 : memset(cm->last_frame_seg_map, 0, cm->mi_rows * cm->mi_cols);
213 0 : }
214 :
215 0 : void av1_swap_current_and_last_seg_map(AV1_COMMON *cm) {
216 : // Swap indices.
217 0 : const int tmp = cm->seg_map_idx;
218 0 : cm->seg_map_idx = cm->prev_seg_map_idx;
219 0 : cm->prev_seg_map_idx = tmp;
220 :
221 0 : cm->current_frame_seg_map = cm->seg_map_array[cm->seg_map_idx];
222 0 : cm->last_frame_seg_map = cm->seg_map_array[cm->prev_seg_map_idx];
223 0 : }
|