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 
av1_get_MBs(int width,int height)22 int 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 
av1_set_mb_mi(AV1_COMMON * cm,int width,int height)38 void av1_set_mb_mi(AV1_COMMON *cm, int width, int height) {
39   // 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.
44   const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
45   const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
46 
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 
51 #if CONFIG_CB4X4
52   cm->mb_cols = (cm->mi_cols + 2) >> 2;
53   cm->mb_rows = (cm->mi_rows + 2) >> 2;
54 #else
55   cm->mb_cols = (cm->mi_cols + 1) >> 1;
56   cm->mb_rows = (cm->mi_rows + 1) >> 1;
57 #endif
58   cm->MBs = cm->mb_rows * cm->mb_cols;
59 }
60 
alloc_seg_map(AV1_COMMON * cm,int seg_map_size)61 static int alloc_seg_map(AV1_COMMON *cm, int seg_map_size) {
62   int i;
63 
64   for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
65     cm->seg_map_array[i] = (uint8_t *)aom_calloc(seg_map_size, 1);
66     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 
free_seg_map(AV1_COMMON * cm)81 static void free_seg_map(AV1_COMMON *cm) {
82   int i;
83 
84   for (i = 0; i < NUM_PING_PONG_BUFFERS; ++i) {
85     aom_free(cm->seg_map_array[i]);
86     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   }
94   cm->seg_map_alloc_size = 0;
95 }
96 
free_scratch_buffers(AV1_COMMON * cm)97 static 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 
alloc_scratch_buffers(AV1_COMMON * cm)109 static 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 
av1_free_ref_frame_buffers(BufferPool * pool)126 void av1_free_ref_frame_buffers(BufferPool *pool) {
127   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     }
135     aom_free(pool->frame_bufs[i].mvs);
136     pool->frame_bufs[i].mvs = NULL;
137 #if CONFIG_MFMV
138     aom_free(pool->frame_bufs[i].tpl_mvs);
139     pool->frame_bufs[i].tpl_mvs = NULL;
140 #endif
141     aom_free_frame_buffer(&pool->frame_bufs[i].buf);
142 #if CONFIG_HASH_ME
143     av1_hash_table_destroy(&pool->frame_bufs[i].hash_table);
144 #endif
145   }
146 }
147 
148 #if CONFIG_LOOP_RESTORATION
149 // Assumes cm->rst_info[p].restoration_tilesize is already initialized
av1_alloc_restoration_buffers(AV1_COMMON * cm)150 void av1_alloc_restoration_buffers(AV1_COMMON *cm) {
151   int p;
152 #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);
160   for (p = 1; p < MAX_MB_PLANE; ++p)
161     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));
164   aom_free(cm->rst_internal.tmpbuf);
165   CHECK_MEM_ERROR(cm, cm->rst_internal.tmpbuf,
166                   (int32_t *)aom_memalign(16, RESTORATION_TMPBUF_SIZE));
167 
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
194 }
195 
av1_free_restoration_buffers(AV1_COMMON * cm)196 void av1_free_restoration_buffers(AV1_COMMON *cm) {
197   int p;
198   for (p = 0; p < MAX_MB_PLANE; ++p)
199     av1_free_restoration_struct(&cm->rst_info[p]);
200   aom_free(cm->rst_internal.tmpbuf);
201   cm->rst_internal.tmpbuf = NULL;
202 }
203 #endif  // CONFIG_LOOP_RESTORATION
204 
av1_free_context_buffers(AV1_COMMON * cm)205 void av1_free_context_buffers(AV1_COMMON *cm) {
206   int i;
207   cm->free_mi(cm);
208   free_seg_map(cm);
209   free_scratch_buffers(cm);
210   for (i = 0; i < MAX_MB_PLANE; i++) {
211     aom_free(cm->above_context[i]);
212     cm->above_context[i] = NULL;
213   }
214   aom_free(cm->above_seg_context);
215   cm->above_seg_context = NULL;
216   cm->above_context_alloc_cols = 0;
217 #if CONFIG_VAR_TX
218   aom_free(cm->above_txfm_context);
219   cm->above_txfm_context = NULL;
220 
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   }
225 #endif
226 }
227 
av1_alloc_context_buffers(AV1_COMMON * cm,int width,int height)228 int av1_alloc_context_buffers(AV1_COMMON *cm, int width, int height) {
229   int new_mi_size;
230 
231   av1_set_mb_mi(cm, width, height);
232   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   }
243   if (alloc_scratch_buffers(cm)) goto fail;
244 
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++) {
254       aom_free(cm->above_context[i]);
255       cm->above_context[i] = (ENTROPY_CONTEXT *)aom_calloc(
256           aligned_mi_cols << (MI_SIZE_LOG2 - tx_size_wide_log2[0]),
257           sizeof(*cm->above_context[0]));
258       if (!cm->above_context[i]) goto fail;
259     }
260 
261     aom_free(cm->above_seg_context);
262     cm->above_seg_context = (PARTITION_CONTEXT *)aom_calloc(
263         aligned_mi_cols, sizeof(*cm->above_seg_context));
264     if (!cm->above_seg_context) goto fail;
265 
266 #if CONFIG_VAR_TX
267     aom_free(cm->above_txfm_context);
268     cm->above_txfm_context = (TXFM_CONTEXT *)aom_calloc(
269         aligned_mi_cols << TX_UNIT_WIDE_LOG2, sizeof(*cm->above_txfm_context));
270     if (!cm->above_txfm_context) goto fail;
271 
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     }
279 #endif
280 
281     cm->above_context_alloc_cols = aligned_mi_cols;
282   }
283 
284   return 0;
285 
286 fail:
287   // clear the mi_* values to force a realloc on resync
288   av1_set_mb_mi(cm, 0, 0);
289   av1_free_context_buffers(cm);
290   return 1;
291 }
292 
av1_remove_common(AV1_COMMON * cm)293 void av1_remove_common(AV1_COMMON *cm) {
294   av1_free_context_buffers(cm);
295 
296   aom_free(cm->fc);
297   cm->fc = NULL;
298   aom_free(cm->frame_contexts);
299   cm->frame_contexts = NULL;
300 }
301 
av1_init_context_buffers(AV1_COMMON * cm)302 void av1_init_context_buffers(AV1_COMMON *cm) {
303   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 
av1_swap_current_and_last_seg_map(AV1_COMMON * cm)308 void av1_swap_current_and_last_seg_map(AV1_COMMON *cm) {
309   // 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 }
317