• 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 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
dealloc_context_buffers_ext(MBMIExtFrameBufferInfo * mbmi_ext_info)24 static AOM_INLINE void dealloc_context_buffers_ext(
25     MBMIExtFrameBufferInfo *mbmi_ext_info) {
26   if (mbmi_ext_info->frame_base) {
27     aom_free(mbmi_ext_info->frame_base);
28     mbmi_ext_info->frame_base = NULL;
29     mbmi_ext_info->alloc_size = 0;
30   }
31 }
32 
alloc_context_buffers_ext(AV1_COMMON * cm,MBMIExtFrameBufferInfo * mbmi_ext_info)33 static AOM_INLINE void alloc_context_buffers_ext(
34     AV1_COMMON *cm, MBMIExtFrameBufferInfo *mbmi_ext_info) {
35   const CommonModeInfoParams *const mi_params = &cm->mi_params;
36 
37   const int mi_alloc_size_1d = mi_size_wide[mi_params->mi_alloc_bsize];
38   const int mi_alloc_rows =
39       (mi_params->mi_rows + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
40   const int mi_alloc_cols =
41       (mi_params->mi_cols + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
42   const int new_ext_mi_size = mi_alloc_rows * mi_alloc_cols;
43 
44   if (new_ext_mi_size > mbmi_ext_info->alloc_size) {
45     dealloc_context_buffers_ext(mbmi_ext_info);
46     CHECK_MEM_ERROR(
47         cm, mbmi_ext_info->frame_base,
48         aom_calloc(new_ext_mi_size, sizeof(*mbmi_ext_info->frame_base)));
49     mbmi_ext_info->alloc_size = new_ext_mi_size;
50   }
51   // The stride needs to be updated regardless of whether new allocation
52   // happened or not.
53   mbmi_ext_info->stride = mi_alloc_cols;
54 }
55 
alloc_compressor_data(AV1_COMP * cpi)56 static AOM_INLINE void alloc_compressor_data(AV1_COMP *cpi) {
57   AV1_COMMON *cm = &cpi->common;
58   TokenInfo *token_info = &cpi->token_info;
59 
60   if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
61     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
62                        "Failed to allocate context buffers");
63   }
64 
65   if (!is_stat_generation_stage(cpi)) {
66     av1_alloc_txb_buf(cpi);
67 
68     alloc_context_buffers_ext(cm, &cpi->mbmi_ext_info);
69   }
70 
71   free_token_info(token_info);
72 
73   if (!is_stat_generation_stage(cpi)) {
74     alloc_token_info(cm, token_info);
75   }
76   if (cpi->td.mb.mv_costs) {
77     aom_free(cpi->td.mb.mv_costs);
78     cpi->td.mb.mv_costs = NULL;
79   }
80   CHECK_MEM_ERROR(cm, cpi->td.mb.mv_costs,
81                   (MvCosts *)aom_calloc(1, sizeof(MvCosts)));
82 
83   if (cpi->td.mb.dv_costs) {
84     aom_free(cpi->td.mb.dv_costs);
85     cpi->td.mb.dv_costs = NULL;
86   }
87   CHECK_MEM_ERROR(cm, cpi->td.mb.dv_costs,
88                   (IntraBCMVCosts *)aom_malloc(sizeof(*cpi->td.mb.dv_costs)));
89 
90   av1_setup_shared_coeff_buffer(cm->seq_params, &cpi->td.shared_coeff_buf,
91                                 cm->error);
92   av1_setup_sms_tree(cpi, &cpi->td);
93   cpi->td.firstpass_ctx =
94       av1_alloc_pmc(cpi, BLOCK_16X16, &cpi->td.shared_coeff_buf);
95 }
96 
realloc_segmentation_maps(AV1_COMP * cpi)97 static AOM_INLINE void realloc_segmentation_maps(AV1_COMP *cpi) {
98   AV1_COMMON *const cm = &cpi->common;
99   CommonModeInfoParams *const mi_params = &cm->mi_params;
100 
101   // Create the encoder segmentation map and set all entries to 0
102   aom_free(cpi->enc_seg.map);
103   CHECK_MEM_ERROR(cm, cpi->enc_seg.map,
104                   aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
105 
106   // Create a map used for cyclic background refresh.
107   if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
108   CHECK_MEM_ERROR(
109       cm, cpi->cyclic_refresh,
110       av1_cyclic_refresh_alloc(mi_params->mi_rows, mi_params->mi_cols));
111 
112   // Create a map used to mark inactive areas.
113   aom_free(cpi->active_map.map);
114   CHECK_MEM_ERROR(cm, cpi->active_map.map,
115                   aom_calloc(mi_params->mi_rows * mi_params->mi_cols, 1));
116 }
117 
alloc_obmc_buffers(OBMCBuffer * obmc_buffer,struct aom_internal_error_info * error)118 static AOM_INLINE void alloc_obmc_buffers(
119     OBMCBuffer *obmc_buffer, struct aom_internal_error_info *error) {
120   AOM_CHECK_MEM_ERROR(
121       error, obmc_buffer->wsrc,
122       (int32_t *)aom_memalign(16, MAX_SB_SQUARE * sizeof(*obmc_buffer->wsrc)));
123   AOM_CHECK_MEM_ERROR(
124       error, obmc_buffer->mask,
125       (int32_t *)aom_memalign(16, MAX_SB_SQUARE * sizeof(*obmc_buffer->mask)));
126   AOM_CHECK_MEM_ERROR(
127       error, obmc_buffer->above_pred,
128       (uint8_t *)aom_memalign(
129           16, MAX_MB_PLANE * MAX_SB_SQUARE * sizeof(*obmc_buffer->above_pred)));
130   AOM_CHECK_MEM_ERROR(
131       error, obmc_buffer->left_pred,
132       (uint8_t *)aom_memalign(
133           16, MAX_MB_PLANE * MAX_SB_SQUARE * sizeof(*obmc_buffer->left_pred)));
134 }
135 
release_obmc_buffers(OBMCBuffer * obmc_buffer)136 static AOM_INLINE void release_obmc_buffers(OBMCBuffer *obmc_buffer) {
137   aom_free(obmc_buffer->mask);
138   aom_free(obmc_buffer->above_pred);
139   aom_free(obmc_buffer->left_pred);
140   aom_free(obmc_buffer->wsrc);
141 
142   obmc_buffer->mask = NULL;
143   obmc_buffer->above_pred = NULL;
144   obmc_buffer->left_pred = NULL;
145   obmc_buffer->wsrc = NULL;
146 }
147 
alloc_compound_type_rd_buffers(struct aom_internal_error_info * error,CompoundTypeRdBuffers * const bufs)148 static AOM_INLINE void alloc_compound_type_rd_buffers(
149     struct aom_internal_error_info *error, CompoundTypeRdBuffers *const bufs) {
150   AOM_CHECK_MEM_ERROR(
151       error, bufs->pred0,
152       (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
153   AOM_CHECK_MEM_ERROR(
154       error, bufs->pred1,
155       (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
156   AOM_CHECK_MEM_ERROR(
157       error, bufs->residual1,
158       (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
159   AOM_CHECK_MEM_ERROR(
160       error, bufs->diff10,
161       (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
162   AOM_CHECK_MEM_ERROR(error, bufs->tmp_best_mask_buf,
163                       (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
164                                             sizeof(*bufs->tmp_best_mask_buf)));
165 }
166 
release_compound_type_rd_buffers(CompoundTypeRdBuffers * const bufs)167 static AOM_INLINE void release_compound_type_rd_buffers(
168     CompoundTypeRdBuffers *const bufs) {
169   aom_free(bufs->pred0);
170   aom_free(bufs->pred1);
171   aom_free(bufs->residual1);
172   aom_free(bufs->diff10);
173   aom_free(bufs->tmp_best_mask_buf);
174   av1_zero(*bufs);  // Set all pointers to NULL for safety.
175 }
176 
dealloc_compressor_data(AV1_COMP * cpi)177 static AOM_INLINE void dealloc_compressor_data(AV1_COMP *cpi) {
178   AV1_COMMON *const cm = &cpi->common;
179   TokenInfo *token_info = &cpi->token_info;
180 
181   dealloc_context_buffers_ext(&cpi->mbmi_ext_info);
182 
183   aom_free(cpi->tile_data);
184   cpi->tile_data = NULL;
185 
186   // Delete sementation map
187   aom_free(cpi->enc_seg.map);
188   cpi->enc_seg.map = NULL;
189 
190   av1_cyclic_refresh_free(cpi->cyclic_refresh);
191   cpi->cyclic_refresh = NULL;
192 
193   aom_free(cpi->active_map.map);
194   cpi->active_map.map = NULL;
195 
196   aom_free(cpi->ssim_rdmult_scaling_factors);
197   cpi->ssim_rdmult_scaling_factors = NULL;
198 
199 #if CONFIG_TUNE_VMAF
200   aom_free(cpi->vmaf_info.rdmult_scaling_factors);
201   cpi->vmaf_info.rdmult_scaling_factors = NULL;
202   aom_close_vmaf_model(cpi->vmaf_info.vmaf_model);
203 #endif
204 
205 #if CONFIG_TUNE_BUTTERAUGLI
206   aom_free(cpi->butteraugli_info.rdmult_scaling_factors);
207   cpi->butteraugli_info.rdmult_scaling_factors = NULL;
208   aom_free_frame_buffer(&cpi->butteraugli_info.source);
209   aom_free_frame_buffer(&cpi->butteraugli_info.resized_source);
210 #endif
211 
212   release_obmc_buffers(&cpi->td.mb.obmc_buffer);
213 
214   if (cpi->td.mb.mv_costs) {
215     aom_free(cpi->td.mb.mv_costs);
216     cpi->td.mb.mv_costs = NULL;
217   }
218 
219   if (cpi->td.mb.dv_costs) {
220     aom_free(cpi->td.mb.dv_costs);
221     cpi->td.mb.dv_costs = NULL;
222   }
223 
224   for (int i = 0; i < 2; i++)
225     for (int j = 0; j < 2; j++) {
226       aom_free(cpi->td.mb.intrabc_hash_info.hash_value_buffer[i][j]);
227       cpi->td.mb.intrabc_hash_info.hash_value_buffer[i][j] = NULL;
228     }
229 
230   aom_free(cm->tpl_mvs);
231   cm->tpl_mvs = NULL;
232 
233   if (cpi->td.pixel_gradient_info) {
234     aom_free(cpi->td.pixel_gradient_info);
235     cpi->td.pixel_gradient_info = NULL;
236   }
237 
238   if (cpi->td.vt64x64) {
239     aom_free(cpi->td.vt64x64);
240     cpi->td.vt64x64 = NULL;
241   }
242 
243   av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm));
244   cpi->td.firstpass_ctx = NULL;
245 
246   av1_free_txb_buf(cpi);
247   av1_free_context_buffers(cm);
248 
249   aom_free_frame_buffer(&cpi->last_frame_uf);
250 #if !CONFIG_REALTIME_ONLY
251   av1_free_restoration_buffers(cm);
252 #endif
253 
254   if (!is_stat_generation_stage(cpi)) {
255     int num_cdef_workers =
256         av1_get_num_mod_workers_for_alloc(&cpi->ppi->p_mt_info, MOD_CDEF);
257     av1_free_cdef_buffers(cm, &cpi->ppi->p_mt_info.cdef_worker,
258                           &cpi->mt_info.cdef_sync, num_cdef_workers);
259   }
260 
261   aom_free_frame_buffer(&cpi->trial_frame_rst);
262   aom_free_frame_buffer(&cpi->scaled_source);
263   aom_free_frame_buffer(&cpi->scaled_last_source);
264 
265   free_token_info(token_info);
266 
267   av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf);
268   av1_free_sms_tree(&cpi->td);
269 
270   aom_free(cpi->td.mb.palette_buffer);
271   release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
272   aom_free(cpi->td.mb.tmp_conv_dst);
273   for (int j = 0; j < 2; ++j) {
274     aom_free(cpi->td.mb.tmp_pred_bufs[j]);
275   }
276 
277 #if CONFIG_DENOISE
278   if (cpi->denoise_and_model) {
279     aom_denoise_and_model_free(cpi->denoise_and_model);
280     cpi->denoise_and_model = NULL;
281   }
282 #endif
283   if (cpi->film_grain_table) {
284     aom_film_grain_table_free(cpi->film_grain_table);
285     cpi->film_grain_table = NULL;
286   }
287 
288   if (cpi->ppi->use_svc) av1_free_svc_cyclic_refresh(cpi);
289 
290   if (cpi->consec_zero_mv) {
291     aom_free(cpi->consec_zero_mv);
292     cpi->consec_zero_mv = NULL;
293   }
294 
295   aom_free(cpi->mb_weber_stats);
296   cpi->mb_weber_stats = NULL;
297 
298   aom_free(cpi->mb_delta_q);
299   cpi->mb_delta_q = NULL;
300 }
301 
allocate_gradient_info_for_hog(PixelLevelGradientInfo ** pixel_gradient_info,AV1_COMP * cpi)302 static AOM_INLINE void allocate_gradient_info_for_hog(
303     PixelLevelGradientInfo **pixel_gradient_info, AV1_COMP *cpi) {
304   const AV1_COMMON *const cm = &cpi->common;
305 
306   if (!*pixel_gradient_info) {
307     const int plane_types = PLANE_TYPES >> cm->seq_params->monochrome;
308     CHECK_MEM_ERROR(cm, *pixel_gradient_info,
309                     aom_malloc(sizeof(**pixel_gradient_info) * plane_types *
310                                MAX_SB_SQUARE));
311   }
312 
313   cpi->td.mb.pixel_gradient_info = *pixel_gradient_info;
314 }
315 
variance_partition_alloc(AV1_COMP * cpi)316 static AOM_INLINE void variance_partition_alloc(AV1_COMP *cpi) {
317   AV1_COMMON *const cm = &cpi->common;
318   const int num_64x64_blocks = (cm->seq_params->sb_size == BLOCK_64X64) ? 1 : 4;
319   if (cpi->td.vt64x64) {
320     if (num_64x64_blocks != cpi->td.num_64x64_blocks) {
321       aom_free(cpi->td.vt64x64);
322       cpi->td.vt64x64 = NULL;
323     }
324   }
325   if (!cpi->td.vt64x64) {
326     CHECK_MEM_ERROR(cm, cpi->td.vt64x64,
327                     aom_malloc(sizeof(*cpi->td.vt64x64) * num_64x64_blocks));
328     cpi->td.num_64x64_blocks = num_64x64_blocks;
329   }
330 }
331 
alloc_altref_frame_buffer(AV1_COMP * cpi)332 static AOM_INLINE void alloc_altref_frame_buffer(AV1_COMP *cpi) {
333   AV1_COMMON *cm = &cpi->common;
334   const SequenceHeader *const seq_params = cm->seq_params;
335   const AV1EncoderConfig *oxcf = &cpi->oxcf;
336 
337   // When lag_in_frames <= 1, alt-ref frames are not enabled. In this case,
338   // temporal filtering of key frames is disabled as well. Hence alt_ref_buffer
339   // allocation is avoided.
340   if (oxcf->gf_cfg.lag_in_frames <= 1) return;
341 
342   // TODO(agrange) Check if ARF is enabled and skip allocation if not.
343   if (aom_realloc_frame_buffer(
344           &cpi->ppi->alt_ref_buffer, oxcf->frm_dim_cfg.width,
345           oxcf->frm_dim_cfg.height, seq_params->subsampling_x,
346           seq_params->subsampling_y, seq_params->use_highbitdepth,
347           cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL,
348           NULL, cpi->oxcf.tool_cfg.enable_global_motion))
349     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
350                        "Failed to allocate altref buffer");
351 }
352 
alloc_util_frame_buffers(AV1_COMP * cpi)353 static AOM_INLINE void alloc_util_frame_buffers(AV1_COMP *cpi) {
354   AV1_COMMON *const cm = &cpi->common;
355   const SequenceHeader *const seq_params = cm->seq_params;
356   const int byte_alignment = cm->features.byte_alignment;
357   if (aom_realloc_frame_buffer(
358           &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
359           seq_params->subsampling_y, seq_params->use_highbitdepth,
360           cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL, 0))
361     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
362                        "Failed to allocate last frame buffer");
363 
364   // The frame buffer trial_frame_rst is used during loop restoration filter
365   // search. Hence it is allocated only when loop restoration is used.
366   const int use_restoration = cm->seq_params->enable_restoration &&
367                               !cm->features.all_lossless &&
368                               !cm->tiles.large_scale;
369   if (use_restoration) {
370     if (aom_realloc_frame_buffer(
371             &cpi->trial_frame_rst, cm->superres_upscaled_width,
372             cm->superres_upscaled_height, seq_params->subsampling_x,
373             seq_params->subsampling_y, seq_params->use_highbitdepth,
374             AOM_RESTORATION_FRAME_BORDER, byte_alignment, NULL, NULL, NULL, 0))
375       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
376                          "Failed to allocate trial restored frame buffer");
377   }
378 
379   if (aom_realloc_frame_buffer(
380           &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
381           seq_params->subsampling_y, seq_params->use_highbitdepth,
382           cpi->oxcf.border_in_pixels, byte_alignment, NULL, NULL, NULL,
383           cpi->oxcf.tool_cfg.enable_global_motion))
384     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
385                        "Failed to allocate scaled source buffer");
386 
387   // The frame buffer cpi->scaled_last_source is used to hold the previous
388   // source frame information. As the previous source frame buffer allocation in
389   // the lookahead queue is avoided for all-intra frame encoding,
390   // cpi->unscaled_last_source will be NULL in such cases. As
391   // cpi->unscaled_last_source is NULL, cpi->scaled_last_source will not be used
392   // for all-intra frame encoding. Hence, the buffer is allocated conditionally.
393   if (cpi->oxcf.kf_cfg.key_freq_max > 0) {
394     if (aom_realloc_frame_buffer(
395             &cpi->scaled_last_source, cm->width, cm->height,
396             seq_params->subsampling_x, seq_params->subsampling_y,
397             seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
398             byte_alignment, NULL, NULL, NULL,
399             cpi->oxcf.tool_cfg.enable_global_motion))
400       aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
401                          "Failed to allocate scaled last source buffer");
402   }
403 }
404 
realloc_and_scale_source(AV1_COMP * cpi,int scaled_width,int scaled_height)405 static AOM_INLINE YV12_BUFFER_CONFIG *realloc_and_scale_source(
406     AV1_COMP *cpi, int scaled_width, int scaled_height) {
407   AV1_COMMON *cm = &cpi->common;
408   const int num_planes = av1_num_planes(cm);
409 
410   if (scaled_width == cpi->unscaled_source->y_crop_width &&
411       scaled_height == cpi->unscaled_source->y_crop_height) {
412     return cpi->unscaled_source;
413   }
414 
415   if (aom_realloc_frame_buffer(
416           &cpi->scaled_source, scaled_width, scaled_height,
417           cm->seq_params->subsampling_x, cm->seq_params->subsampling_y,
418           cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
419           cm->features.byte_alignment, NULL, NULL, NULL,
420           cpi->oxcf.tool_cfg.enable_global_motion))
421     aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR,
422                        "Failed to reallocate scaled source buffer");
423   assert(cpi->scaled_source.y_crop_width == scaled_width);
424   assert(cpi->scaled_source.y_crop_height == scaled_height);
425   av1_resize_and_extend_frame_nonnormative(
426       cpi->unscaled_source, &cpi->scaled_source, (int)cm->seq_params->bit_depth,
427       num_planes);
428   return &cpi->scaled_source;
429 }
430 
431 #ifdef __cplusplus
432 }  // extern "C"
433 #endif
434 
435 #endif  // AOM_AV1_ENCODER_ENCODER_ALLOC_H_
436