• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020, Alliance for Open Media. All rights reserved
3  *
4  * 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.
10  */
11 
12 #ifndef AOM_AV1_ENCODER_ENCODER_ALLOC_H_
13 #define AOM_AV1_ENCODER_ENCODER_ALLOC_H_
14 
15 #include "av1/encoder/block.h"
16 #include "av1/encoder/encoder.h"
17 #include "av1/encoder/encodetxb.h"
18 #include "av1/encoder/ethread.h"
19 #include "av1/encoder/intra_mode_search_utils.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
dealloc_context_buffers_ext(MBMIExtFrameBufferInfo * mbmi_ext_info)25 static AOM_INLINE void dealloc_context_buffers_ext(
26     MBMIExtFrameBufferInfo *mbmi_ext_info) {
27   if (mbmi_ext_info->frame_base) {
28     aom_free(mbmi_ext_info->frame_base);
29     mbmi_ext_info->frame_base = NULL;
30     mbmi_ext_info->alloc_size = 0;
31   }
32 }
33 
alloc_context_buffers_ext(AV1_COMMON * cm,MBMIExtFrameBufferInfo * mbmi_ext_info)34 static AOM_INLINE void alloc_context_buffers_ext(
35     AV1_COMMON *cm, MBMIExtFrameBufferInfo *mbmi_ext_info) {
36   const CommonModeInfoParams *const mi_params = &cm->mi_params;
37 
38   const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
39   const int mi_alloc_rows =
40       (mi_params->mi_rows + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
41   const int mi_alloc_cols =
42       (mi_params->mi_cols + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
43   const int new_ext_mi_size = mi_alloc_rows * mi_alloc_cols;
44 
45   if (new_ext_mi_size > mbmi_ext_info->alloc_size) {
46     dealloc_context_buffers_ext(mbmi_ext_info);
47     CHECK_MEM_ERROR(
48         cm, mbmi_ext_info->frame_base,
49         aom_malloc(new_ext_mi_size * sizeof(*mbmi_ext_info->frame_base)));
50     mbmi_ext_info->alloc_size = new_ext_mi_size;
51   }
52   // The stride needs to be updated regardless of whether new allocation
53   // happened or not.
54   mbmi_ext_info->stride = mi_alloc_cols;
55 }
56 
alloc_compressor_data(AV1_COMP * cpi)57 static AOM_INLINE void alloc_compressor_data(AV1_COMP *cpi) {
58   AV1_COMMON *cm = &cpi->common;
59   CommonModeInfoParams *const mi_params = &cm->mi_params;
60 
61   // Setup mi_params
62   mi_params->set_mb_mi(mi_params, cm->width, cm->height,
63                        cpi->sf.part_sf.default_min_partition_size);
64 
65   if (!is_stat_generation_stage(cpi)) av1_alloc_txb_buf(cpi);
66 
67   if (cpi->td.mb.mv_costs) {
68     aom_free(cpi->td.mb.mv_costs);
69     cpi->td.mb.mv_costs = NULL;
70   }
71   // Avoid the memory allocation of 'mv_costs' for allintra encoding mode.
72   if (cpi->oxcf.kf_cfg.key_freq_max != 0) {
73     CHECK_MEM_ERROR(cm, cpi->td.mb.mv_costs,
74                     (MvCosts *)aom_calloc(1, sizeof(MvCosts)));
75   }
76 
77   av1_setup_shared_coeff_buffer(cm->seq_params, &cpi->td.shared_coeff_buf,
78                                 cm->error);
79   av1_setup_sms_tree(cpi, &cpi->td);
80   cpi->td.firstpass_ctx =
81       av1_alloc_pmc(cpi, BLOCK_16X16, &cpi->td.shared_coeff_buf);
82 }
83 
84 // Allocate mbmi buffers which are used to store mode information at block
85 // level.
alloc_mb_mode_info_buffers(AV1_COMP * const cpi)86 static AOM_INLINE void alloc_mb_mode_info_buffers(AV1_COMP *const cpi) {
87   AV1_COMMON *const cm = &cpi->common;
88   if (av1_alloc_context_buffers(cm, cm->width, cm->height,
89                                 cpi->sf.part_sf.default_min_partition_size)) {
90     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
91                        "Failed to allocate context buffers");
92   }
93 
94   if (!is_stat_generation_stage(cpi))
95     alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
96 }
97 
realloc_segmentation_maps(AV1_COMP * cpi)98 static AOM_INLINE void realloc_segmentation_maps(AV1_COMP *cpi) {
99   AV1_COMMON *const cm = &cpi->common;
100   CommonModeInfoParams *const mi_params = &cm->mi_params;
101 
102   // Create the encoder segmentation map and set all entries to 0
103   aom_free(cpi->enc_seg.map);
104   CHECK_MEM_ERROR(cm, cpi->enc_seg.map,
105                   aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
106 
107   // Create a map used for cyclic background refresh.
108   if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
109   CHECK_MEM_ERROR(
110       cm, cpi->cyclic_refresh,
111       av1_cyclic_refresh_alloc(mi_params->mi_rows, mi_params->mi_cols));
112 
113   // Create a map used to mark inactive areas.
114   aom_free(cpi->active_map.map);
115   CHECK_MEM_ERROR(cm, cpi->active_map.map,
116                   aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
117 }
118 
alloc_obmc_buffers(OBMCBuffer * obmc_buffer,struct aom_internal_error_info * error)119 static AOM_INLINE void alloc_obmc_buffers(
120     OBMCBuffer *obmc_buffer, struct aom_internal_error_info *error) {
121   AOM_CHECK_MEM_ERROR(
122       error, obmc_buffer->wsrc,
123       (int32_t *)aom_memalign(16, MAX_SB_SQUARE * sizeof(*obmc_buffer->wsrc)));
124   AOM_CHECK_MEM_ERROR(
125       error, obmc_buffer->mask,
126       (int32_t *)aom_memalign(16, MAX_SB_SQUARE * sizeof(*obmc_buffer->mask)));
127   AOM_CHECK_MEM_ERROR(
128       error, obmc_buffer->above_pred,
129       (uint8_t *)aom_memalign(
130           16, MAX_MB_PLANE * MAX_SB_SQUARE * sizeof(*obmc_buffer->above_pred)));
131   AOM_CHECK_MEM_ERROR(
132       error, obmc_buffer->left_pred,
133       (uint8_t *)aom_memalign(
134           16, MAX_MB_PLANE * MAX_SB_SQUARE * sizeof(*obmc_buffer->left_pred)));
135 }
136 
release_obmc_buffers(OBMCBuffer * obmc_buffer)137 static AOM_INLINE void release_obmc_buffers(OBMCBuffer *obmc_buffer) {
138   aom_free(obmc_buffer->mask);
139   aom_free(obmc_buffer->above_pred);
140   aom_free(obmc_buffer->left_pred);
141   aom_free(obmc_buffer->wsrc);
142 
143   obmc_buffer->mask = NULL;
144   obmc_buffer->above_pred = NULL;
145   obmc_buffer->left_pred = NULL;
146   obmc_buffer->wsrc = NULL;
147 }
148 
alloc_compound_type_rd_buffers(struct aom_internal_error_info * error,CompoundTypeRdBuffers * const bufs)149 static AOM_INLINE void alloc_compound_type_rd_buffers(
150     struct aom_internal_error_info *error, CompoundTypeRdBuffers *const bufs) {
151   AOM_CHECK_MEM_ERROR(
152       error, bufs->pred0,
153       (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
154   AOM_CHECK_MEM_ERROR(
155       error, bufs->pred1,
156       (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
157   AOM_CHECK_MEM_ERROR(
158       error, bufs->residual1,
159       (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
160   AOM_CHECK_MEM_ERROR(
161       error, bufs->diff10,
162       (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
163   AOM_CHECK_MEM_ERROR(error, bufs->tmp_best_mask_buf,
164                       (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
165                                             sizeof(*bufs->tmp_best_mask_buf)));
166 }
167 
release_compound_type_rd_buffers(CompoundTypeRdBuffers * const bufs)168 static AOM_INLINE void release_compound_type_rd_buffers(
169     CompoundTypeRdBuffers *const bufs) {
170   aom_free(bufs->pred0);
171   aom_free(bufs->pred1);
172   aom_free(bufs->residual1);
173   aom_free(bufs->diff10);
174   aom_free(bufs->tmp_best_mask_buf);
175   av1_zero(*bufs);  // Set all pointers to NULL for safety.
176 }
177 
dealloc_compressor_data(AV1_COMP * cpi)178 static AOM_INLINE void dealloc_compressor_data(AV1_COMP *cpi) {
179   AV1_COMMON *const cm = &cpi->common;
180   TokenInfo *token_info = &cpi->token_info;
181 
182   dealloc_context_buffers_ext(&cpi->mbmi_ext_info);
183 
184   aom_free(cpi->tile_data);
185   cpi->tile_data = NULL;
186 
187   // Delete sementation map
188   aom_free(cpi->enc_seg.map);
189   cpi->enc_seg.map = NULL;
190 
191   av1_cyclic_refresh_free(cpi->cyclic_refresh);
192   cpi->cyclic_refresh = NULL;
193 
194   aom_free(cpi->active_map.map);
195   cpi->active_map.map = NULL;
196 
197   aom_free(cpi->ssim_rdmult_scaling_factors);
198   cpi->ssim_rdmult_scaling_factors = NULL;
199 
200   aom_free(cpi->tpl_rdmult_scaling_factors);
201   cpi->tpl_rdmult_scaling_factors = NULL;
202 
203 #if CONFIG_TUNE_VMAF
204   aom_free(cpi->vmaf_info.rdmult_scaling_factors);
205   cpi->vmaf_info.rdmult_scaling_factors = NULL;
206   aom_close_vmaf_model(cpi->vmaf_info.vmaf_model);
207 #endif
208 
209 #if CONFIG_TUNE_BUTTERAUGLI
210   aom_free(cpi->butteraugli_info.rdmult_scaling_factors);
211   cpi->butteraugli_info.rdmult_scaling_factors = NULL;
212   aom_free_frame_buffer(&cpi->butteraugli_info.source);
213   aom_free_frame_buffer(&cpi->butteraugli_info.resized_source);
214 #endif
215 
216   release_obmc_buffers(&cpi->td.mb.obmc_buffer);
217 
218   if (cpi->td.mb.mv_costs) {
219     aom_free(cpi->td.mb.mv_costs);
220     cpi->td.mb.mv_costs = NULL;
221   }
222 
223   if (cpi->td.mb.dv_costs) {
224     aom_free(cpi->td.mb.dv_costs);
225     cpi->td.mb.dv_costs = NULL;
226   }
227 
228   for (int i = 0; i < 2; i++)
229     for (int j = 0; j < 2; j++) {
230       aom_free(cpi->td.mb.intrabc_hash_info.hash_value_buffer[i][j]);
231       cpi->td.mb.intrabc_hash_info.hash_value_buffer[i][j] = NULL;
232     }
233 
234   aom_free(cm->tpl_mvs);
235   cm->tpl_mvs = NULL;
236 
237   if (cpi->td.pixel_gradient_info) {
238     aom_free(cpi->td.pixel_gradient_info);
239     cpi->td.pixel_gradient_info = NULL;
240   }
241 
242   if (cpi->td.src_var_info_of_4x4_sub_blocks) {
243     aom_free(cpi->td.src_var_info_of_4x4_sub_blocks);
244     cpi->td.src_var_info_of_4x4_sub_blocks = NULL;
245   }
246 
247   if (cpi->td.vt64x64) {
248     aom_free(cpi->td.vt64x64);
249     cpi->td.vt64x64 = NULL;
250   }
251 
252   av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
253   cpi->td.firstpass_ctx = NULL;
254 
255   av1_free_txb_buf(cpi);
256   av1_free_context_buffers(cm);
257 
258   aom_free_frame_buffer(&cpi->last_frame_uf);
259 #if !CONFIG_REALTIME_ONLY
260   av1_free_restoration_buffers(cm);
261 #endif
262 
263   if (!is_stat_generation_stage(cpi)) {
264     av1_free_cdef_buffers(cm, &cpi->ppi->p_mt_info.cdef_worker,
265                           &cpi->mt_info.cdef_sync);
266   }
267 
268   aom_free_frame_buffer(&cpi->trial_frame_rst);
269   aom_free_frame_buffer(&cpi->scaled_source);
270   aom_free_frame_buffer(&cpi->scaled_last_source);
271   aom_free_frame_buffer(&cpi->orig_source);
272   aom_free_frame_buffer(&cpi->svc.source_last_TL0);
273 
274   free_token_info(token_info);
275 
276   av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
277   av1_free_sms_tree(&cpi->td);
278 
279   aom_free(cpi->td.mb.palette_buffer);
280   release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
281   aom_free(cpi->td.mb.tmp_conv_dst);
282   for (int j = 0; j < 2; ++j) {
283     aom_free(cpi->td.mb.tmp_pred_bufs[j]);
284   }
285 
286 #if CONFIG_DENOISE
287   if (cpi->denoise_and_model) {
288     aom_denoise_and_model_free(cpi->denoise_and_model);
289     cpi->denoise_and_model = NULL;
290   }
291 #endif
292   if (cpi->film_grain_table) {
293     aom_film_grain_table_free(cpi->film_grain_table);
294     cpi->film_grain_table = NULL;
295   }
296 
297   if (cpi->ppi->use_svc) av1_free_svc_cyclic_refresh(cpi);
298   aom_free(cpi->svc.layer_context);
299   cpi->svc.layer_context = NULL;
300 
301   if (cpi->consec_zero_mv) {
302     aom_free(cpi->consec_zero_mv);
303     cpi->consec_zero_mv = NULL;
304   }
305 
306   if (cpi->src_sad_blk_64x64) {
307     aom_free(cpi->src_sad_blk_64x64);
308     cpi->src_sad_blk_64x64 = NULL;
309   }
310 
311   aom_free(cpi->mb_weber_stats);
312   cpi->mb_weber_stats = NULL;
313 
314   aom_free(cpi->mb_delta_q);
315   cpi->mb_delta_q = NULL;
316 }
317 
allocate_gradient_info_for_hog(AV1_COMP * cpi)318 static AOM_INLINE void allocate_gradient_info_for_hog(AV1_COMP *cpi) {
319   if (!is_gradient_caching_for_hog_enabled(cpi)) return;
320 
321   PixelLevelGradientInfo *pixel_gradient_info = cpi->td.pixel_gradient_info;
322   if (!pixel_gradient_info) {
323     const AV1_COMMON *const cm = &cpi->common;
324     const int plane_types = PLANE_TYPES >> cm->seq_params->monochrome;
325     CHECK_MEM_ERROR(
326         cm, pixel_gradient_info,
327         aom_malloc(sizeof(*pixel_gradient_info) * plane_types * MAX_SB_SQUARE));
328     cpi->td.pixel_gradient_info = pixel_gradient_info;
329   }
330 
331   cpi->td.mb.pixel_gradient_info = pixel_gradient_info;
332 }
333 
allocate_src_var_of_4x4_sub_block_buf(AV1_COMP * cpi)334 static AOM_INLINE void allocate_src_var_of_4x4_sub_block_buf(AV1_COMP *cpi) {
335   if (!is_src_var_for_4x4_sub_blocks_caching_enabled(cpi)) return;
336 
337   Block4x4VarInfo *source_variance_info =
338       cpi->td.src_var_info_of_4x4_sub_blocks;
339   if (!source_variance_info) {
340     const AV1_COMMON *const cm = &cpi->common;
341     const BLOCK_SIZE sb_size = cm->seq_params->sb_size;
342     const int mi_count_in_sb = mi_size_wide[sb_size] * mi_size_high[sb_size];
343     CHECK_MEM_ERROR(cm, source_variance_info,
344                     aom_malloc(sizeof(*source_variance_info) * mi_count_in_sb));
345     cpi->td.src_var_info_of_4x4_sub_blocks = source_variance_info;
346   }
347 
348   cpi->td.mb.src_var_info_of_4x4_sub_blocks = source_variance_info;
349 }
350 
variance_partition_alloc(AV1_COMP * cpi)351 static AOM_INLINE void variance_partition_alloc(AV1_COMP *cpi) {
352   AV1_COMMON *const cm = &cpi->common;
353   const int num_64x64_blocks = (cm->seq_params->sb_size == BLOCK_64X64) ? 1 : 4;
354   if (cpi->td.vt64x64) {
355     if (num_64x64_blocks != cpi->td.num_64x64_blocks) {
356       aom_free(cpi->td.vt64x64);
357       cpi->td.vt64x64 = NULL;
358     }
359   }
360   if (!cpi->td.vt64x64) {
361     CHECK_MEM_ERROR(cm, cpi->td.vt64x64,
362                     aom_malloc(sizeof(*cpi->td.vt64x64) * num_64x64_blocks));
363     cpi->td.num_64x64_blocks = num_64x64_blocks;
364   }
365 }
366 
realloc_and_scale_source(AV1_COMP * cpi,int scaled_width,int scaled_height)367 static AOM_INLINE YV12_BUFFER_CONFIG *realloc_and_scale_source(
368     AV1_COMP *cpi, int scaled_width, int scaled_height) {
369   AV1_COMMON *cm = &cpi->common;
370   const int num_planes = av1_num_planes(cm);
371 
372   if (scaled_width == cpi->unscaled_source->y_crop_width &&
373       scaled_height == cpi->unscaled_source->y_crop_height) {
374     return cpi->unscaled_source;
375   }
376 
377   if (aom_realloc_frame_buffer(
378           &cpi->scaled_source, scaled_width, scaled_height,
379           cm->seq_params->subsampling_x, cm->seq_params->subsampling_y,
380           cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
381           cm->features.byte_alignment, NULL, NULL, NULL,
382           cpi->oxcf.tool_cfg.enable_global_motion, 0))
383     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
384                        "Failed to reallocate scaled source buffer");
385   assert(cpi->scaled_source.y_crop_width == scaled_width);
386   assert(cpi->scaled_source.y_crop_height == scaled_height);
387   av1_resize_and_extend_frame_nonnormative(
388       cpi->unscaled_source, &cpi->scaled_source, (int)cm->seq_params->bit_depth,
389       num_planes);
390   return &cpi->scaled_source;
391 }
392 
393 #ifdef __cplusplus
394 }  // extern "C"
395 #endif
396 
397 #endif  // AOM_AV1_ENCODER_ENCODER_ALLOC_H_
398