/src/aom/av1/encoder/encoder.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 2016, 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 | | #include <assert.h> |
13 | | #include <float.h> |
14 | | #include <limits.h> |
15 | | #include <math.h> |
16 | | #include <stdbool.h> |
17 | | #include <stdio.h> |
18 | | #include <stdlib.h> |
19 | | #include <time.h> |
20 | | |
21 | | #include "av1/common/scale.h" |
22 | | #include "config/aom_config.h" |
23 | | #include "config/aom_dsp_rtcd.h" |
24 | | |
25 | | #include "aom/aomcx.h" |
26 | | |
27 | | #if CONFIG_DENOISE |
28 | | #include "aom_dsp/grain_table.h" |
29 | | #include "aom_dsp/noise_util.h" |
30 | | #include "aom_dsp/noise_model.h" |
31 | | #endif |
32 | | #include "aom_dsp/flow_estimation/corner_detect.h" |
33 | | #include "aom_dsp/psnr.h" |
34 | | #if CONFIG_INTERNAL_STATS |
35 | | #include "aom_dsp/ssim.h" |
36 | | #endif |
37 | | #include "aom_ports/aom_timer.h" |
38 | | #include "aom_ports/mem.h" |
39 | | #include "aom_scale/aom_scale.h" |
40 | | #include "aom_util/aom_pthread.h" |
41 | | #if CONFIG_BITSTREAM_DEBUG |
42 | | #include "aom_util/debug_util.h" |
43 | | #endif // CONFIG_BITSTREAM_DEBUG |
44 | | |
45 | | #include "av1/common/alloccommon.h" |
46 | | #include "av1/common/debugmodes.h" |
47 | | #include "av1/common/filter.h" |
48 | | #include "av1/common/idct.h" |
49 | | #include "av1/common/reconinter.h" |
50 | | #include "av1/common/reconintra.h" |
51 | | #include "av1/common/resize.h" |
52 | | #include "av1/common/tile_common.h" |
53 | | |
54 | | #include "av1/encoder/allintra_vis.h" |
55 | | #include "av1/encoder/aq_complexity.h" |
56 | | #include "av1/encoder/aq_cyclicrefresh.h" |
57 | | #include "av1/encoder/aq_variance.h" |
58 | | #include "av1/encoder/bitstream.h" |
59 | | #if CONFIG_INTERNAL_STATS |
60 | | #include "av1/encoder/blockiness.h" |
61 | | #endif |
62 | | #include "av1/encoder/context_tree.h" |
63 | | #include "av1/encoder/dwt.h" |
64 | | #include "av1/encoder/encodeframe.h" |
65 | | #include "av1/encoder/encodemv.h" |
66 | | #include "av1/encoder/encode_strategy.h" |
67 | | #include "av1/encoder/encoder.h" |
68 | | #include "av1/encoder/encoder_alloc.h" |
69 | | #include "av1/encoder/encoder_utils.h" |
70 | | #include "av1/encoder/encodetxb.h" |
71 | | #include "av1/encoder/ethread.h" |
72 | | #include "av1/encoder/firstpass.h" |
73 | | #include "av1/encoder/hash_motion.h" |
74 | | #include "av1/encoder/hybrid_fwd_txfm.h" |
75 | | #include "av1/encoder/intra_mode_search.h" |
76 | | #include "av1/encoder/mv_prec.h" |
77 | | #include "av1/encoder/pass2_strategy.h" |
78 | | #include "av1/encoder/pickcdef.h" |
79 | | #include "av1/encoder/picklpf.h" |
80 | | #include "av1/encoder/pickrst.h" |
81 | | #include "av1/encoder/random.h" |
82 | | #include "av1/encoder/ratectrl.h" |
83 | | #include "av1/encoder/rc_utils.h" |
84 | | #include "av1/encoder/rd.h" |
85 | | #include "av1/encoder/rdopt.h" |
86 | | #if CONFIG_SALIENCY_MAP |
87 | | #include "av1/encoder/saliency_map.h" |
88 | | #endif |
89 | | #include "av1/encoder/segmentation.h" |
90 | | #include "av1/encoder/speed_features.h" |
91 | | #include "av1/encoder/superres_scale.h" |
92 | | #include "av1/encoder/thirdpass.h" |
93 | | #include "av1/encoder/tpl_model.h" |
94 | | #include "av1/encoder/reconinter_enc.h" |
95 | | #include "av1/encoder/var_based_part.h" |
96 | | |
97 | 0 | #define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7 |
98 | | |
99 | | // #define OUTPUT_YUV_REC |
100 | | #ifdef OUTPUT_YUV_REC |
101 | | FILE *yuv_rec_file; |
102 | | #define FILE_NAME_LEN 100 |
103 | | #endif |
104 | | |
105 | | #ifdef OUTPUT_YUV_DENOISED |
106 | | FILE *yuv_denoised_file = NULL; |
107 | | #endif |
108 | | |
109 | 0 | static INLINE void Scale2Ratio(AOM_SCALING_MODE mode, int *hr, int *hs) { |
110 | 0 | switch (mode) { |
111 | 0 | case AOME_NORMAL: |
112 | 0 | *hr = 1; |
113 | 0 | *hs = 1; |
114 | 0 | break; |
115 | 0 | case AOME_FOURFIVE: |
116 | 0 | *hr = 4; |
117 | 0 | *hs = 5; |
118 | 0 | break; |
119 | 0 | case AOME_THREEFIVE: |
120 | 0 | *hr = 3; |
121 | 0 | *hs = 5; |
122 | 0 | break; |
123 | 0 | case AOME_THREEFOUR: |
124 | 0 | *hr = 3; |
125 | 0 | *hs = 4; |
126 | 0 | break; |
127 | 0 | case AOME_ONEFOUR: |
128 | 0 | *hr = 1; |
129 | 0 | *hs = 4; |
130 | 0 | break; |
131 | 0 | case AOME_ONEEIGHT: |
132 | 0 | *hr = 1; |
133 | 0 | *hs = 8; |
134 | 0 | break; |
135 | 0 | case AOME_ONETWO: |
136 | 0 | *hr = 1; |
137 | 0 | *hs = 2; |
138 | 0 | break; |
139 | 0 | case AOME_TWOTHREE: |
140 | 0 | *hr = 2; |
141 | 0 | *hs = 3; |
142 | 0 | break; |
143 | 0 | case AOME_ONETHREE: |
144 | 0 | *hr = 1; |
145 | 0 | *hs = 3; |
146 | 0 | break; |
147 | 0 | default: |
148 | 0 | *hr = 1; |
149 | 0 | *hs = 1; |
150 | 0 | assert(0); |
151 | 0 | break; |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | | int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows, |
156 | 0 | int cols) { |
157 | 0 | const CommonModeInfoParams *const mi_params = &cpi->common.mi_params; |
158 | 0 | if (rows == mi_params->mb_rows && cols == mi_params->mb_cols) { |
159 | 0 | unsigned char *const active_map_4x4 = cpi->active_map.map; |
160 | 0 | const int mi_rows = mi_params->mi_rows; |
161 | 0 | const int mi_cols = mi_params->mi_cols; |
162 | 0 | cpi->active_map.update = 0; |
163 | 0 | cpi->rc.percent_blocks_inactive = 0; |
164 | 0 | assert(mi_rows % 2 == 0 && mi_rows > 0); |
165 | 0 | assert(mi_cols % 2 == 0 && mi_cols > 0); |
166 | 0 | if (new_map_16x16) { |
167 | 0 | int num_samples = 0; |
168 | 0 | int num_blocks_inactive = 0; |
169 | 0 | for (int r = 0; r < mi_rows; r += 4) { |
170 | 0 | for (int c = 0; c < mi_cols; c += 4) { |
171 | 0 | const uint8_t val = new_map_16x16[(r >> 2) * cols + (c >> 2)] |
172 | 0 | ? AM_SEGMENT_ID_ACTIVE |
173 | 0 | : AM_SEGMENT_ID_INACTIVE; |
174 | 0 | num_samples++; |
175 | 0 | if (val == AM_SEGMENT_ID_INACTIVE) num_blocks_inactive++; |
176 | 0 | const int row_max = AOMMIN(4, mi_rows - r); |
177 | 0 | const int col_max = AOMMIN(4, mi_cols - c); |
178 | 0 | for (int x = 0; x < row_max; ++x) { |
179 | 0 | for (int y = 0; y < col_max; ++y) { |
180 | 0 | active_map_4x4[(r + x) * mi_cols + (c + y)] = val; |
181 | 0 | } |
182 | 0 | } |
183 | 0 | } |
184 | 0 | } |
185 | 0 | cpi->active_map.enabled = 1; |
186 | 0 | cpi->active_map.update = 1; |
187 | 0 | assert(num_samples); |
188 | 0 | cpi->rc.percent_blocks_inactive = |
189 | 0 | (num_blocks_inactive * 100) / num_samples; |
190 | 0 | } |
191 | 0 | return 0; |
192 | 0 | } |
193 | | |
194 | 0 | return -1; |
195 | 0 | } |
196 | | |
197 | | int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows, |
198 | 0 | int cols) { |
199 | 0 | const CommonModeInfoParams *const mi_params = &cpi->common.mi_params; |
200 | 0 | if (rows == mi_params->mb_rows && cols == mi_params->mb_cols && |
201 | 0 | new_map_16x16) { |
202 | 0 | unsigned char *const seg_map_8x8 = cpi->enc_seg.map; |
203 | 0 | const int mi_rows = mi_params->mi_rows; |
204 | 0 | const int mi_cols = mi_params->mi_cols; |
205 | 0 | const int row_scale = mi_size_high_log2[BLOCK_16X16]; |
206 | 0 | const int col_scale = mi_size_wide_log2[BLOCK_16X16]; |
207 | 0 | assert(mi_rows % 2 == 0); |
208 | 0 | assert(mi_cols % 2 == 0); |
209 | |
|
210 | 0 | memset(new_map_16x16, !cpi->active_map.enabled, rows * cols); |
211 | 0 | if (cpi->active_map.enabled) { |
212 | 0 | for (int r = 0; r < (mi_rows >> row_scale); ++r) { |
213 | 0 | for (int c = 0; c < (mi_cols >> col_scale); ++c) { |
214 | | // Cyclic refresh segments are considered active despite not having |
215 | | // AM_SEGMENT_ID_ACTIVE |
216 | 0 | uint8_t temp = 0; |
217 | 0 | temp |= seg_map_8x8[(2 * r + 0) * mi_cols + (2 * c + 0)] != |
218 | 0 | AM_SEGMENT_ID_INACTIVE; |
219 | 0 | temp |= seg_map_8x8[(2 * r + 0) * mi_cols + (2 * c + 1)] != |
220 | 0 | AM_SEGMENT_ID_INACTIVE; |
221 | 0 | temp |= seg_map_8x8[(2 * r + 1) * mi_cols + (2 * c + 0)] != |
222 | 0 | AM_SEGMENT_ID_INACTIVE; |
223 | 0 | temp |= seg_map_8x8[(2 * r + 1) * mi_cols + (2 * c + 1)] != |
224 | 0 | AM_SEGMENT_ID_INACTIVE; |
225 | 0 | new_map_16x16[r * cols + c] |= temp; |
226 | 0 | } |
227 | 0 | } |
228 | 0 | } |
229 | 0 | return 0; |
230 | 0 | } |
231 | | |
232 | 0 | return -1; |
233 | 0 | } |
234 | | |
235 | 0 | void av1_initialize_enc(unsigned int usage, enum aom_rc_mode end_usage) { |
236 | 0 | bool is_allintra = usage == ALLINTRA; |
237 | |
|
238 | 0 | av1_rtcd(); |
239 | 0 | aom_dsp_rtcd(); |
240 | 0 | aom_scale_rtcd(); |
241 | 0 | av1_init_intra_predictors(); |
242 | 0 | av1_init_me_luts(); |
243 | 0 | if (!is_allintra) av1_init_wedge_masks(); |
244 | 0 | if (!is_allintra || end_usage != AOM_Q) av1_rc_init_minq_luts(); |
245 | 0 | } |
246 | | |
247 | 0 | void av1_new_framerate(AV1_COMP *cpi, double framerate) { |
248 | 0 | cpi->framerate = framerate < 0.1 ? 30 : framerate; |
249 | 0 | av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height); |
250 | 0 | } |
251 | | |
252 | | double av1_get_compression_ratio(const AV1_COMMON *const cm, |
253 | 0 | size_t encoded_frame_size) { |
254 | 0 | const int upscaled_width = cm->superres_upscaled_width; |
255 | 0 | const int height = cm->height; |
256 | 0 | const int64_t luma_pic_size = (int64_t)upscaled_width * height; |
257 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
258 | 0 | const BITSTREAM_PROFILE profile = seq_params->profile; |
259 | 0 | const int pic_size_profile_factor = |
260 | 0 | profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36); |
261 | 0 | encoded_frame_size = |
262 | 0 | (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1); |
263 | 0 | const int64_t uncompressed_frame_size = |
264 | 0 | (luma_pic_size * pic_size_profile_factor) >> 3; |
265 | 0 | return (double)uncompressed_frame_size / encoded_frame_size; |
266 | 0 | } |
267 | | |
268 | | static void auto_tile_size_balancing(AV1_COMMON *const cm, int num_sbs, |
269 | 0 | int num_tiles_lg, int tile_col_row) { |
270 | 0 | CommonTileParams *const tiles = &cm->tiles; |
271 | 0 | int i, start_sb; |
272 | 0 | int size_sb = num_sbs >> num_tiles_lg; |
273 | 0 | int res_sbs = num_sbs - (size_sb << num_tiles_lg); |
274 | 0 | int num_tiles = 1 << num_tiles_lg; |
275 | 0 | int inc_index = num_tiles - res_sbs; |
276 | |
|
277 | 0 | tiles->uniform_spacing = 0; |
278 | |
|
279 | 0 | for (i = 0, start_sb = 0; start_sb < num_sbs && i < MAX_TILE_COLS; ++i) { |
280 | 0 | if (i == inc_index) ++size_sb; |
281 | 0 | if (tile_col_row) |
282 | 0 | tiles->col_start_sb[i] = start_sb; |
283 | 0 | else |
284 | 0 | tiles->row_start_sb[i] = start_sb; |
285 | |
|
286 | 0 | start_sb += AOMMIN(size_sb, tiles->max_width_sb); |
287 | 0 | } |
288 | |
|
289 | 0 | if (tile_col_row) { |
290 | 0 | tiles->cols = i; |
291 | 0 | tiles->col_start_sb[i] = num_sbs; |
292 | 0 | } else { |
293 | 0 | tiles->rows = i; |
294 | 0 | tiles->row_start_sb[i] = num_sbs; |
295 | 0 | } |
296 | 0 | } |
297 | | |
298 | | static void set_tile_info(AV1_COMMON *const cm, |
299 | 0 | const TileConfig *const tile_cfg) { |
300 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
301 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
302 | 0 | CommonTileParams *const tiles = &cm->tiles; |
303 | 0 | int i, start_sb; |
304 | |
|
305 | 0 | av1_get_tile_limits(cm); |
306 | |
|
307 | 0 | int sb_cols = |
308 | 0 | CEIL_POWER_OF_TWO(mi_params->mi_cols, seq_params->mib_size_log2); |
309 | | // configure tile columns |
310 | 0 | if (tile_cfg->tile_width_count == 0 || tile_cfg->tile_height_count == 0) { |
311 | 0 | tiles->uniform_spacing = 1; |
312 | 0 | tiles->log2_cols = AOMMAX(tile_cfg->tile_columns, tiles->min_log2_cols); |
313 | | // Add a special case to handle super resolution |
314 | 0 | sb_cols = coded_to_superres_mi(sb_cols, cm->superres_scale_denominator); |
315 | 0 | int min_log2_cols = 0; |
316 | 0 | for (; (tiles->max_width_sb << min_log2_cols) <= sb_cols; ++min_log2_cols) { |
317 | 0 | } |
318 | 0 | tiles->log2_cols = AOMMAX(tiles->log2_cols, min_log2_cols); |
319 | |
|
320 | 0 | tiles->log2_cols = AOMMIN(tiles->log2_cols, tiles->max_log2_cols); |
321 | 0 | } else if (tile_cfg->tile_widths[0] < 0) { |
322 | 0 | auto_tile_size_balancing(cm, sb_cols, tile_cfg->tile_columns, 1); |
323 | 0 | } else { |
324 | 0 | int size_sb, j = 0; |
325 | 0 | tiles->uniform_spacing = 0; |
326 | 0 | for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) { |
327 | 0 | tiles->col_start_sb[i] = start_sb; |
328 | 0 | size_sb = tile_cfg->tile_widths[j++]; |
329 | 0 | if (j >= tile_cfg->tile_width_count) j = 0; |
330 | 0 | start_sb += AOMMIN(size_sb, tiles->max_width_sb); |
331 | 0 | } |
332 | 0 | tiles->cols = i; |
333 | 0 | tiles->col_start_sb[i] = sb_cols; |
334 | 0 | } |
335 | 0 | av1_calculate_tile_cols(seq_params, mi_params->mi_rows, mi_params->mi_cols, |
336 | 0 | tiles); |
337 | | |
338 | | // configure tile rows |
339 | 0 | int sb_rows = |
340 | 0 | CEIL_POWER_OF_TWO(mi_params->mi_rows, seq_params->mib_size_log2); |
341 | 0 | if (tiles->uniform_spacing) { |
342 | 0 | tiles->log2_rows = AOMMAX(tile_cfg->tile_rows, tiles->min_log2_rows); |
343 | 0 | tiles->log2_rows = AOMMIN(tiles->log2_rows, tiles->max_log2_rows); |
344 | 0 | } else if (tile_cfg->tile_heights[0] < 0) { |
345 | 0 | auto_tile_size_balancing(cm, sb_rows, tile_cfg->tile_rows, 0); |
346 | 0 | } else { |
347 | 0 | int size_sb, j = 0; |
348 | 0 | for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) { |
349 | 0 | tiles->row_start_sb[i] = start_sb; |
350 | 0 | size_sb = tile_cfg->tile_heights[j++]; |
351 | 0 | if (j >= tile_cfg->tile_height_count) j = 0; |
352 | 0 | start_sb += AOMMIN(size_sb, tiles->max_height_sb); |
353 | 0 | } |
354 | 0 | tiles->rows = i; |
355 | 0 | tiles->row_start_sb[i] = sb_rows; |
356 | 0 | } |
357 | 0 | av1_calculate_tile_rows(seq_params, mi_params->mi_rows, tiles); |
358 | 0 | } |
359 | | |
360 | 0 | void av1_update_frame_size(AV1_COMP *cpi) { |
361 | 0 | AV1_COMMON *const cm = &cpi->common; |
362 | 0 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
363 | | |
364 | | // Setup mi_params here in case we need more mi's. |
365 | 0 | CommonModeInfoParams *const mi_params = &cm->mi_params; |
366 | 0 | mi_params->set_mb_mi(mi_params, cm->width, cm->height, |
367 | 0 | cpi->sf.part_sf.default_min_partition_size); |
368 | |
|
369 | 0 | av1_init_macroblockd(cm, xd); |
370 | |
|
371 | 0 | if (!cpi->ppi->seq_params_locked) |
372 | 0 | set_sb_size(cm->seq_params, |
373 | 0 | av1_select_sb_size(&cpi->oxcf, cm->width, cm->height, |
374 | 0 | cpi->ppi->number_spatial_layers)); |
375 | |
|
376 | 0 | set_tile_info(cm, &cpi->oxcf.tile_cfg); |
377 | 0 | } |
378 | | |
379 | | static INLINE int does_level_match(int width, int height, double fps, |
380 | | int lvl_width, int lvl_height, |
381 | 0 | double lvl_fps, int lvl_dim_mult) { |
382 | 0 | const int64_t lvl_luma_pels = (int64_t)lvl_width * lvl_height; |
383 | 0 | const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps; |
384 | 0 | const int64_t luma_pels = (int64_t)width * height; |
385 | 0 | const double display_sample_rate = luma_pels * fps; |
386 | 0 | return luma_pels <= lvl_luma_pels && |
387 | 0 | display_sample_rate <= lvl_display_sample_rate && |
388 | 0 | width <= lvl_width * lvl_dim_mult && |
389 | 0 | height <= lvl_height * lvl_dim_mult; |
390 | 0 | } |
391 | | |
392 | | static void set_bitstream_level_tier(AV1_PRIMARY *const ppi, int width, |
393 | 0 | int height, double init_framerate) { |
394 | 0 | SequenceHeader *const seq_params = &ppi->seq_params; |
395 | 0 | const AV1LevelParams *const level_params = &ppi->level_params; |
396 | | // TODO(any): This is a placeholder function that only addresses dimensions |
397 | | // and max display sample rates. |
398 | | // Need to add checks for max bit rate, max decoded luma sample rate, header |
399 | | // rate, etc. that are not covered by this function. |
400 | 0 | AV1_LEVEL level = SEQ_LEVEL_MAX; |
401 | 0 | if (does_level_match(width, height, init_framerate, 512, 288, 30.0, 4)) { |
402 | 0 | level = SEQ_LEVEL_2_0; |
403 | 0 | } else if (does_level_match(width, height, init_framerate, 704, 396, 30.0, |
404 | 0 | 4)) { |
405 | 0 | level = SEQ_LEVEL_2_1; |
406 | 0 | } else if (does_level_match(width, height, init_framerate, 1088, 612, 30.0, |
407 | 0 | 4)) { |
408 | 0 | level = SEQ_LEVEL_3_0; |
409 | 0 | } else if (does_level_match(width, height, init_framerate, 1376, 774, 30.0, |
410 | 0 | 4)) { |
411 | 0 | level = SEQ_LEVEL_3_1; |
412 | 0 | } else if (does_level_match(width, height, init_framerate, 2048, 1152, 30.0, |
413 | 0 | 3)) { |
414 | 0 | level = SEQ_LEVEL_4_0; |
415 | 0 | } else if (does_level_match(width, height, init_framerate, 2048, 1152, 60.0, |
416 | 0 | 3)) { |
417 | 0 | level = SEQ_LEVEL_4_1; |
418 | 0 | } else if (does_level_match(width, height, init_framerate, 4096, 2176, 30.0, |
419 | 0 | 2)) { |
420 | 0 | level = SEQ_LEVEL_5_0; |
421 | 0 | } else if (does_level_match(width, height, init_framerate, 4096, 2176, 60.0, |
422 | 0 | 2)) { |
423 | 0 | level = SEQ_LEVEL_5_1; |
424 | 0 | } else if (does_level_match(width, height, init_framerate, 4096, 2176, 120.0, |
425 | 0 | 2)) { |
426 | 0 | level = SEQ_LEVEL_5_2; |
427 | 0 | } else if (does_level_match(width, height, init_framerate, 8192, 4352, 30.0, |
428 | 0 | 2)) { |
429 | 0 | level = SEQ_LEVEL_6_0; |
430 | 0 | } else if (does_level_match(width, height, init_framerate, 8192, 4352, 60.0, |
431 | 0 | 2)) { |
432 | 0 | level = SEQ_LEVEL_6_1; |
433 | 0 | } else if (does_level_match(width, height, init_framerate, 8192, 4352, 120.0, |
434 | 0 | 2)) { |
435 | 0 | level = SEQ_LEVEL_6_2; |
436 | 0 | } |
437 | | #if CONFIG_CWG_C013 |
438 | | // TODO(bohanli): currently target level is only working for the 0th operating |
439 | | // point, so scalable coding is not supported. |
440 | | else if (level_params->target_seq_level_idx[0] >= SEQ_LEVEL_7_0 && |
441 | | level_params->target_seq_level_idx[0] <= SEQ_LEVEL_8_3) { |
442 | | // Only use level 7.x to 8.x when explicitly asked to. |
443 | | if (does_level_match(width, height, init_framerate, 16384, 8704, 30.0, 2)) { |
444 | | level = SEQ_LEVEL_7_0; |
445 | | } else if (does_level_match(width, height, init_framerate, 16384, 8704, |
446 | | 60.0, 2)) { |
447 | | level = SEQ_LEVEL_7_1; |
448 | | } else if (does_level_match(width, height, init_framerate, 16384, 8704, |
449 | | 120.0, 2)) { |
450 | | level = SEQ_LEVEL_7_2; |
451 | | } else if (does_level_match(width, height, init_framerate, 32768, 17408, |
452 | | 30.0, 2)) { |
453 | | level = SEQ_LEVEL_8_0; |
454 | | } else if (does_level_match(width, height, init_framerate, 32768, 17408, |
455 | | 60.0, 2)) { |
456 | | level = SEQ_LEVEL_8_1; |
457 | | } else if (does_level_match(width, height, init_framerate, 32768, 17408, |
458 | | 120.0, 2)) { |
459 | | level = SEQ_LEVEL_8_2; |
460 | | } |
461 | | } |
462 | | #endif |
463 | |
|
464 | 0 | for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) { |
465 | 0 | assert(is_valid_seq_level_idx(level_params->target_seq_level_idx[i]) || |
466 | 0 | level_params->target_seq_level_idx[i] == SEQ_LEVEL_KEEP_STATS); |
467 | | // If a higher target level is specified, it is then used rather than the |
468 | | // inferred one from resolution and framerate. |
469 | 0 | seq_params->seq_level_idx[i] = |
470 | 0 | level_params->target_seq_level_idx[i] < SEQ_LEVELS && |
471 | 0 | level_params->target_seq_level_idx[i] > level |
472 | 0 | ? level_params->target_seq_level_idx[i] |
473 | 0 | : level; |
474 | | // Set the maximum parameters for bitrate and buffer size for this profile, |
475 | | // level, and tier |
476 | 0 | seq_params->op_params[i].bitrate = av1_max_level_bitrate( |
477 | 0 | seq_params->profile, seq_params->seq_level_idx[i], seq_params->tier[i]); |
478 | | // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the |
479 | | // check |
480 | 0 | if (seq_params->op_params[i].bitrate == 0) |
481 | 0 | aom_internal_error( |
482 | 0 | &ppi->error, AOM_CODEC_UNSUP_BITSTREAM, |
483 | 0 | "AV1 does not support this combination of profile, level, and tier."); |
484 | | // Buffer size in bits/s is bitrate in bits/s * 1 s |
485 | 0 | seq_params->op_params[i].buffer_size = seq_params->op_params[i].bitrate; |
486 | 0 | } |
487 | 0 | } |
488 | | |
489 | | void av1_init_seq_coding_tools(AV1_PRIMARY *const ppi, |
490 | | const AV1EncoderConfig *oxcf, |
491 | 0 | int disable_frame_id_numbers) { |
492 | 0 | SequenceHeader *const seq = &ppi->seq_params; |
493 | 0 | const FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg; |
494 | 0 | const ToolCfg *const tool_cfg = &oxcf->tool_cfg; |
495 | |
|
496 | 0 | seq->still_picture = |
497 | 0 | !tool_cfg->force_video_mode && (oxcf->input_cfg.limit == 1); |
498 | 0 | seq->reduced_still_picture_hdr = |
499 | 0 | seq->still_picture && !tool_cfg->full_still_picture_hdr; |
500 | 0 | seq->force_screen_content_tools = 2; |
501 | 0 | seq->force_integer_mv = 2; |
502 | 0 | seq->order_hint_info.enable_order_hint = tool_cfg->enable_order_hint; |
503 | 0 | seq->frame_id_numbers_present_flag = |
504 | 0 | !seq->reduced_still_picture_hdr && |
505 | 0 | !oxcf->tile_cfg.enable_large_scale_tile && |
506 | 0 | tool_cfg->error_resilient_mode && !disable_frame_id_numbers; |
507 | 0 | if (seq->reduced_still_picture_hdr) { |
508 | 0 | seq->order_hint_info.enable_order_hint = 0; |
509 | 0 | seq->force_screen_content_tools = 2; |
510 | 0 | seq->force_integer_mv = 2; |
511 | 0 | } |
512 | 0 | seq->order_hint_info.order_hint_bits_minus_1 = |
513 | 0 | seq->order_hint_info.enable_order_hint |
514 | 0 | ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1 |
515 | 0 | : -1; |
516 | |
|
517 | 0 | seq->max_frame_width = frm_dim_cfg->forced_max_frame_width |
518 | 0 | ? frm_dim_cfg->forced_max_frame_width |
519 | 0 | : frm_dim_cfg->width; |
520 | 0 | seq->max_frame_height = frm_dim_cfg->forced_max_frame_height |
521 | 0 | ? frm_dim_cfg->forced_max_frame_height |
522 | 0 | : frm_dim_cfg->height; |
523 | 0 | seq->num_bits_width = |
524 | 0 | (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1; |
525 | 0 | seq->num_bits_height = |
526 | 0 | (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1; |
527 | 0 | assert(seq->num_bits_width <= 16); |
528 | 0 | assert(seq->num_bits_height <= 16); |
529 | |
|
530 | 0 | seq->frame_id_length = FRAME_ID_LENGTH; |
531 | 0 | seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH; |
532 | |
|
533 | 0 | seq->enable_dual_filter = tool_cfg->enable_dual_filter; |
534 | 0 | seq->order_hint_info.enable_dist_wtd_comp = |
535 | 0 | oxcf->comp_type_cfg.enable_dist_wtd_comp; |
536 | 0 | seq->order_hint_info.enable_dist_wtd_comp &= |
537 | 0 | seq->order_hint_info.enable_order_hint; |
538 | 0 | seq->order_hint_info.enable_ref_frame_mvs = tool_cfg->ref_frame_mvs_present; |
539 | 0 | seq->order_hint_info.enable_ref_frame_mvs &= |
540 | 0 | seq->order_hint_info.enable_order_hint; |
541 | 0 | seq->enable_superres = oxcf->superres_cfg.enable_superres; |
542 | 0 | seq->enable_cdef = tool_cfg->cdef_control != CDEF_NONE ? 1 : 0; |
543 | 0 | seq->enable_restoration = tool_cfg->enable_restoration; |
544 | 0 | seq->enable_warped_motion = oxcf->motion_mode_cfg.enable_warped_motion; |
545 | 0 | seq->enable_interintra_compound = tool_cfg->enable_interintra_comp; |
546 | 0 | seq->enable_masked_compound = oxcf->comp_type_cfg.enable_masked_comp; |
547 | 0 | seq->enable_intra_edge_filter = oxcf->intra_mode_cfg.enable_intra_edge_filter; |
548 | 0 | seq->enable_filter_intra = oxcf->intra_mode_cfg.enable_filter_intra; |
549 | |
|
550 | 0 | set_bitstream_level_tier(ppi, frm_dim_cfg->width, frm_dim_cfg->height, |
551 | 0 | oxcf->input_cfg.init_framerate); |
552 | |
|
553 | 0 | if (seq->operating_points_cnt_minus_1 == 0) { |
554 | 0 | seq->operating_point_idc[0] = 0; |
555 | 0 | seq->has_nonzero_operating_point_idc = false; |
556 | 0 | } else { |
557 | | // Set operating_point_idc[] such that the i=0 point corresponds to the |
558 | | // highest quality operating point (all layers), and subsequent |
559 | | // operarting points (i > 0) are lower quality corresponding to |
560 | | // skip decoding enhancement layers (temporal first). |
561 | 0 | int i = 0; |
562 | 0 | assert(seq->operating_points_cnt_minus_1 == |
563 | 0 | (int)(ppi->number_spatial_layers * ppi->number_temporal_layers - 1)); |
564 | 0 | for (unsigned int sl = 0; sl < ppi->number_spatial_layers; sl++) { |
565 | 0 | for (unsigned int tl = 0; tl < ppi->number_temporal_layers; tl++) { |
566 | 0 | seq->operating_point_idc[i] = |
567 | 0 | (~(~0u << (ppi->number_spatial_layers - sl)) << 8) | |
568 | 0 | ~(~0u << (ppi->number_temporal_layers - tl)); |
569 | 0 | assert(seq->operating_point_idc[i] != 0); |
570 | 0 | i++; |
571 | 0 | } |
572 | 0 | } |
573 | 0 | seq->has_nonzero_operating_point_idc = true; |
574 | 0 | } |
575 | 0 | } |
576 | | |
577 | | static void init_config_sequence(struct AV1_PRIMARY *ppi, |
578 | 0 | const AV1EncoderConfig *oxcf) { |
579 | 0 | SequenceHeader *const seq_params = &ppi->seq_params; |
580 | 0 | const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg; |
581 | 0 | const ColorCfg *const color_cfg = &oxcf->color_cfg; |
582 | |
|
583 | 0 | ppi->use_svc = 0; |
584 | 0 | ppi->number_spatial_layers = 1; |
585 | 0 | ppi->number_temporal_layers = 1; |
586 | |
|
587 | 0 | seq_params->profile = oxcf->profile; |
588 | 0 | seq_params->bit_depth = oxcf->tool_cfg.bit_depth; |
589 | 0 | seq_params->use_highbitdepth = oxcf->use_highbitdepth; |
590 | 0 | seq_params->color_primaries = color_cfg->color_primaries; |
591 | 0 | seq_params->transfer_characteristics = color_cfg->transfer_characteristics; |
592 | 0 | seq_params->matrix_coefficients = color_cfg->matrix_coefficients; |
593 | 0 | seq_params->monochrome = oxcf->tool_cfg.enable_monochrome; |
594 | 0 | seq_params->chroma_sample_position = color_cfg->chroma_sample_position; |
595 | 0 | seq_params->color_range = color_cfg->color_range; |
596 | 0 | seq_params->timing_info_present = dec_model_cfg->timing_info_present; |
597 | 0 | seq_params->timing_info.num_units_in_display_tick = |
598 | 0 | dec_model_cfg->timing_info.num_units_in_display_tick; |
599 | 0 | seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale; |
600 | 0 | seq_params->timing_info.equal_picture_interval = |
601 | 0 | dec_model_cfg->timing_info.equal_picture_interval; |
602 | 0 | seq_params->timing_info.num_ticks_per_picture = |
603 | 0 | dec_model_cfg->timing_info.num_ticks_per_picture; |
604 | |
|
605 | 0 | seq_params->display_model_info_present_flag = |
606 | 0 | dec_model_cfg->display_model_info_present_flag; |
607 | 0 | seq_params->decoder_model_info_present_flag = |
608 | 0 | dec_model_cfg->decoder_model_info_present_flag; |
609 | 0 | if (dec_model_cfg->decoder_model_info_present_flag) { |
610 | | // set the decoder model parameters in schedule mode |
611 | 0 | seq_params->decoder_model_info.num_units_in_decoding_tick = |
612 | 0 | dec_model_cfg->num_units_in_decoding_tick; |
613 | 0 | ppi->buffer_removal_time_present = 1; |
614 | 0 | av1_set_aom_dec_model_info(&seq_params->decoder_model_info); |
615 | 0 | av1_set_dec_model_op_parameters(&seq_params->op_params[0]); |
616 | 0 | } else if (seq_params->timing_info_present && |
617 | 0 | seq_params->timing_info.equal_picture_interval && |
618 | 0 | !seq_params->decoder_model_info_present_flag) { |
619 | | // set the decoder model parameters in resource availability mode |
620 | 0 | av1_set_resource_availability_parameters(&seq_params->op_params[0]); |
621 | 0 | } else { |
622 | 0 | seq_params->op_params[0].initial_display_delay = |
623 | 0 | 10; // Default value (not signaled) |
624 | 0 | } |
625 | |
|
626 | 0 | if (seq_params->monochrome) { |
627 | 0 | seq_params->subsampling_x = 1; |
628 | 0 | seq_params->subsampling_y = 1; |
629 | 0 | } else if (seq_params->color_primaries == AOM_CICP_CP_BT_709 && |
630 | 0 | seq_params->transfer_characteristics == AOM_CICP_TC_SRGB && |
631 | 0 | seq_params->matrix_coefficients == AOM_CICP_MC_IDENTITY) { |
632 | 0 | seq_params->subsampling_x = 0; |
633 | 0 | seq_params->subsampling_y = 0; |
634 | 0 | } else { |
635 | 0 | if (seq_params->profile == 0) { |
636 | 0 | seq_params->subsampling_x = 1; |
637 | 0 | seq_params->subsampling_y = 1; |
638 | 0 | } else if (seq_params->profile == 1) { |
639 | 0 | seq_params->subsampling_x = 0; |
640 | 0 | seq_params->subsampling_y = 0; |
641 | 0 | } else { |
642 | 0 | if (seq_params->bit_depth == AOM_BITS_12) { |
643 | 0 | seq_params->subsampling_x = oxcf->input_cfg.chroma_subsampling_x; |
644 | 0 | seq_params->subsampling_y = oxcf->input_cfg.chroma_subsampling_y; |
645 | 0 | } else { |
646 | 0 | seq_params->subsampling_x = 1; |
647 | 0 | seq_params->subsampling_y = 0; |
648 | 0 | } |
649 | 0 | } |
650 | 0 | } |
651 | 0 | av1_change_config_seq(ppi, oxcf, NULL); |
652 | 0 | } |
653 | | |
654 | 0 | static void init_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) { |
655 | 0 | AV1_COMMON *const cm = &cpi->common; |
656 | 0 | ResizePendingParams *resize_pending_params = &cpi->resize_pending_params; |
657 | |
|
658 | 0 | cpi->oxcf = *oxcf; |
659 | 0 | cpi->framerate = oxcf->input_cfg.init_framerate; |
660 | |
|
661 | 0 | cm->width = oxcf->frm_dim_cfg.width; |
662 | 0 | cm->height = oxcf->frm_dim_cfg.height; |
663 | 0 | cpi->is_dropped_frame = false; |
664 | |
|
665 | 0 | alloc_compressor_data(cpi); |
666 | |
|
667 | 0 | cpi->data_alloc_width = cm->width; |
668 | 0 | cpi->data_alloc_height = cm->height; |
669 | 0 | cpi->frame_size_related_setup_done = false; |
670 | | |
671 | | // Single thread case: use counts in common. |
672 | 0 | cpi->td.counts = &cpi->counts; |
673 | | |
674 | | // Init SVC parameters. |
675 | 0 | cpi->svc.number_spatial_layers = 1; |
676 | 0 | cpi->svc.number_temporal_layers = 1; |
677 | 0 | cm->spatial_layer_id = 0; |
678 | 0 | cm->temporal_layer_id = 0; |
679 | | // Init rtc_ref parameters. |
680 | 0 | cpi->ppi->rtc_ref.set_ref_frame_config = 0; |
681 | 0 | cpi->ppi->rtc_ref.non_reference_frame = 0; |
682 | 0 | cpi->ppi->rtc_ref.ref_frame_comp[0] = 0; |
683 | 0 | cpi->ppi->rtc_ref.ref_frame_comp[1] = 0; |
684 | 0 | cpi->ppi->rtc_ref.ref_frame_comp[2] = 0; |
685 | | |
686 | | // change includes all joint functionality |
687 | 0 | av1_change_config(cpi, oxcf, false); |
688 | |
|
689 | 0 | cpi->ref_frame_flags = 0; |
690 | | |
691 | | // Reset resize pending flags |
692 | 0 | resize_pending_params->width = 0; |
693 | 0 | resize_pending_params->height = 0; |
694 | | |
695 | | // Setup identity scale factor |
696 | 0 | av1_setup_scale_factors_for_frame(&cm->sf_identity, 1, 1, 1, 1); |
697 | |
|
698 | 0 | init_buffer_indices(&cpi->force_intpel_info, cm->remapped_ref_idx); |
699 | |
|
700 | 0 | av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height); |
701 | 0 | } |
702 | | |
703 | | void av1_change_config_seq(struct AV1_PRIMARY *ppi, |
704 | | const AV1EncoderConfig *oxcf, |
705 | 0 | bool *is_sb_size_changed) { |
706 | 0 | SequenceHeader *const seq_params = &ppi->seq_params; |
707 | 0 | const FrameDimensionCfg *const frm_dim_cfg = &oxcf->frm_dim_cfg; |
708 | 0 | const DecoderModelCfg *const dec_model_cfg = &oxcf->dec_model_cfg; |
709 | 0 | const ColorCfg *const color_cfg = &oxcf->color_cfg; |
710 | |
|
711 | 0 | if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile; |
712 | 0 | seq_params->bit_depth = oxcf->tool_cfg.bit_depth; |
713 | 0 | seq_params->color_primaries = color_cfg->color_primaries; |
714 | 0 | seq_params->transfer_characteristics = color_cfg->transfer_characteristics; |
715 | 0 | seq_params->matrix_coefficients = color_cfg->matrix_coefficients; |
716 | 0 | seq_params->monochrome = oxcf->tool_cfg.enable_monochrome; |
717 | 0 | seq_params->chroma_sample_position = color_cfg->chroma_sample_position; |
718 | 0 | seq_params->color_range = color_cfg->color_range; |
719 | |
|
720 | 0 | assert(IMPLIES(seq_params->profile <= PROFILE_1, |
721 | 0 | seq_params->bit_depth <= AOM_BITS_10)); |
722 | |
|
723 | 0 | seq_params->timing_info_present = dec_model_cfg->timing_info_present; |
724 | 0 | seq_params->timing_info.num_units_in_display_tick = |
725 | 0 | dec_model_cfg->timing_info.num_units_in_display_tick; |
726 | 0 | seq_params->timing_info.time_scale = dec_model_cfg->timing_info.time_scale; |
727 | 0 | seq_params->timing_info.equal_picture_interval = |
728 | 0 | dec_model_cfg->timing_info.equal_picture_interval; |
729 | 0 | seq_params->timing_info.num_ticks_per_picture = |
730 | 0 | dec_model_cfg->timing_info.num_ticks_per_picture; |
731 | |
|
732 | 0 | seq_params->display_model_info_present_flag = |
733 | 0 | dec_model_cfg->display_model_info_present_flag; |
734 | 0 | seq_params->decoder_model_info_present_flag = |
735 | 0 | dec_model_cfg->decoder_model_info_present_flag; |
736 | 0 | if (dec_model_cfg->decoder_model_info_present_flag) { |
737 | | // set the decoder model parameters in schedule mode |
738 | 0 | seq_params->decoder_model_info.num_units_in_decoding_tick = |
739 | 0 | dec_model_cfg->num_units_in_decoding_tick; |
740 | 0 | ppi->buffer_removal_time_present = 1; |
741 | 0 | av1_set_aom_dec_model_info(&seq_params->decoder_model_info); |
742 | 0 | av1_set_dec_model_op_parameters(&seq_params->op_params[0]); |
743 | 0 | } else if (seq_params->timing_info_present && |
744 | 0 | seq_params->timing_info.equal_picture_interval && |
745 | 0 | !seq_params->decoder_model_info_present_flag) { |
746 | | // set the decoder model parameters in resource availability mode |
747 | 0 | av1_set_resource_availability_parameters(&seq_params->op_params[0]); |
748 | 0 | } else { |
749 | 0 | seq_params->op_params[0].initial_display_delay = |
750 | 0 | 10; // Default value (not signaled) |
751 | 0 | } |
752 | |
|
753 | 0 | av1_update_film_grain_parameters_seq(ppi, oxcf); |
754 | |
|
755 | 0 | int sb_size = seq_params->sb_size; |
756 | | // Superblock size should not be updated after the first key frame. |
757 | 0 | if (!ppi->seq_params_locked) { |
758 | 0 | set_sb_size(seq_params, av1_select_sb_size(oxcf, frm_dim_cfg->width, |
759 | 0 | frm_dim_cfg->height, |
760 | 0 | ppi->number_spatial_layers)); |
761 | 0 | for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) |
762 | 0 | seq_params->tier[i] = (oxcf->tier_mask >> i) & 1; |
763 | 0 | } |
764 | 0 | if (is_sb_size_changed != NULL && sb_size != seq_params->sb_size) |
765 | 0 | *is_sb_size_changed = true; |
766 | | |
767 | | // Init sequence level coding tools |
768 | | // This should not be called after the first key frame. |
769 | 0 | if (!ppi->seq_params_locked) { |
770 | 0 | seq_params->operating_points_cnt_minus_1 = |
771 | 0 | (ppi->number_spatial_layers > 1 || ppi->number_temporal_layers > 1) |
772 | 0 | ? ppi->number_spatial_layers * ppi->number_temporal_layers - 1 |
773 | 0 | : 0; |
774 | 0 | av1_init_seq_coding_tools( |
775 | 0 | ppi, oxcf, ppi->use_svc || ppi->rtc_ref.set_ref_frame_config); |
776 | 0 | } |
777 | 0 | seq_params->timing_info_present &= !seq_params->reduced_still_picture_hdr; |
778 | |
|
779 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
780 | 0 | highbd_set_var_fns(ppi); |
781 | 0 | #endif |
782 | |
|
783 | 0 | set_primary_rc_buffer_sizes(oxcf, ppi); |
784 | 0 | } |
785 | | |
786 | | void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf, |
787 | 0 | bool is_sb_size_changed) { |
788 | 0 | AV1_COMMON *const cm = &cpi->common; |
789 | 0 | SequenceHeader *const seq_params = cm->seq_params; |
790 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
791 | 0 | PRIMARY_RATE_CONTROL *const p_rc = &cpi->ppi->p_rc; |
792 | 0 | MACROBLOCK *const x = &cpi->td.mb; |
793 | 0 | AV1LevelParams *const level_params = &cpi->ppi->level_params; |
794 | 0 | RefreshFrameInfo *const refresh_frame = &cpi->refresh_frame; |
795 | 0 | const FrameDimensionCfg *const frm_dim_cfg = &cpi->oxcf.frm_dim_cfg; |
796 | 0 | const RateControlCfg *const rc_cfg = &oxcf->rc_cfg; |
797 | 0 | FeatureFlags *const features = &cm->features; |
798 | | |
799 | | // in case of LAP, lag in frames is set according to number of lap buffers |
800 | | // calculated at init time. This stores and restores LAP's lag in frames to |
801 | | // prevent override by new cfg. |
802 | 0 | int lap_lag_in_frames = -1; |
803 | 0 | if (cpi->ppi->lap_enabled && cpi->compressor_stage == LAP_STAGE) { |
804 | 0 | lap_lag_in_frames = cpi->oxcf.gf_cfg.lag_in_frames; |
805 | 0 | } |
806 | |
|
807 | 0 | cpi->oxcf = *oxcf; |
808 | |
|
809 | 0 | av1_update_film_grain_parameters(cpi, oxcf); |
810 | | |
811 | | // When user provides superres_mode = AOM_SUPERRES_AUTO, we still initialize |
812 | | // superres mode for current encoding = AOM_SUPERRES_NONE. This is to ensure |
813 | | // that any analysis (e.g. TPL) happening outside the main encoding loop still |
814 | | // happens at full resolution. |
815 | | // This value will later be set appropriately just before main encoding loop. |
816 | 0 | cpi->superres_mode = oxcf->superres_cfg.superres_mode == AOM_SUPERRES_AUTO |
817 | 0 | ? AOM_SUPERRES_NONE |
818 | 0 | : oxcf->superres_cfg.superres_mode; // default |
819 | 0 | x->e_mbd.bd = (int)seq_params->bit_depth; |
820 | 0 | x->e_mbd.global_motion = cm->global_motion; |
821 | |
|
822 | 0 | memcpy(level_params->target_seq_level_idx, cpi->oxcf.target_seq_level_idx, |
823 | 0 | sizeof(level_params->target_seq_level_idx)); |
824 | 0 | level_params->keep_level_stats = 0; |
825 | 0 | for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) { |
826 | 0 | if (level_params->target_seq_level_idx[i] < SEQ_LEVELS || |
827 | 0 | level_params->target_seq_level_idx[i] == SEQ_LEVEL_KEEP_STATS) { |
828 | 0 | level_params->keep_level_stats |= 1u << i; |
829 | 0 | if (!level_params->level_info[i]) { |
830 | 0 | CHECK_MEM_ERROR(cm, level_params->level_info[i], |
831 | 0 | aom_calloc(1, sizeof(*level_params->level_info[i]))); |
832 | 0 | } |
833 | 0 | } |
834 | 0 | } |
835 | | |
836 | | // TODO(huisu@): level targeting currently only works for the 0th operating |
837 | | // point, so scalable coding is not supported yet. |
838 | 0 | if (level_params->target_seq_level_idx[0] < SEQ_LEVELS) { |
839 | | // Adjust encoder config in order to meet target level. |
840 | 0 | config_target_level(cpi, level_params->target_seq_level_idx[0], |
841 | 0 | seq_params->tier[0]); |
842 | 0 | } |
843 | |
|
844 | 0 | if (has_no_stats_stage(cpi) && (rc_cfg->mode == AOM_Q)) { |
845 | 0 | p_rc->baseline_gf_interval = FIXED_GF_INTERVAL; |
846 | 0 | } else if (!is_one_pass_rt_params(cpi) || |
847 | 0 | cm->current_frame.frame_number == 0) { |
848 | | // For rtc mode: logic for setting the baseline_gf_interval is done |
849 | | // in av1_get_one_pass_rt_params(), and it should not be reset here in |
850 | | // change_config(), unless after init_config (first frame). |
851 | 0 | p_rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2; |
852 | 0 | } |
853 | |
|
854 | 0 | refresh_frame->golden_frame = false; |
855 | 0 | refresh_frame->bwd_ref_frame = false; |
856 | |
|
857 | 0 | features->refresh_frame_context = |
858 | 0 | (oxcf->tool_cfg.frame_parallel_decoding_mode) |
859 | 0 | ? REFRESH_FRAME_CONTEXT_DISABLED |
860 | 0 | : REFRESH_FRAME_CONTEXT_BACKWARD; |
861 | 0 | if (oxcf->tile_cfg.enable_large_scale_tile) |
862 | 0 | features->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED; |
863 | |
|
864 | 0 | if (x->palette_buffer == NULL) { |
865 | 0 | CHECK_MEM_ERROR(cm, x->palette_buffer, |
866 | 0 | aom_memalign(16, sizeof(*x->palette_buffer))); |
867 | 0 | } |
868 | |
|
869 | 0 | if (x->tmp_conv_dst == NULL) { |
870 | 0 | CHECK_MEM_ERROR( |
871 | 0 | cm, x->tmp_conv_dst, |
872 | 0 | aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst))); |
873 | 0 | x->e_mbd.tmp_conv_dst = x->tmp_conv_dst; |
874 | 0 | } |
875 | | // The buffers 'tmp_pred_bufs[]' and 'comp_rd_buffer' are used in inter frames |
876 | | // to store intermediate inter mode prediction results and are not required |
877 | | // for allintra encoding mode. Hence, the memory allocations for these buffers |
878 | | // are avoided for allintra encoding mode. |
879 | 0 | if (cpi->oxcf.kf_cfg.key_freq_max != 0) { |
880 | 0 | if (x->comp_rd_buffer.pred0 == NULL) |
881 | 0 | alloc_compound_type_rd_buffers(cm->error, &x->comp_rd_buffer); |
882 | |
|
883 | 0 | for (int i = 0; i < 2; ++i) { |
884 | 0 | if (x->tmp_pred_bufs[i] == NULL) { |
885 | 0 | CHECK_MEM_ERROR(cm, x->tmp_pred_bufs[i], |
886 | 0 | aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE * |
887 | 0 | sizeof(*x->tmp_pred_bufs[i]))); |
888 | 0 | x->e_mbd.tmp_obmc_bufs[i] = x->tmp_pred_bufs[i]; |
889 | 0 | } |
890 | 0 | } |
891 | 0 | } |
892 | |
|
893 | 0 | av1_reset_segment_features(cm); |
894 | |
|
895 | 0 | av1_set_high_precision_mv(cpi, 1, 0); |
896 | | |
897 | | // Under a configuration change, where maximum_buffer_size may change, |
898 | | // keep buffer level clipped to the maximum allowed buffer size. |
899 | 0 | p_rc->bits_off_target = |
900 | 0 | AOMMIN(p_rc->bits_off_target, p_rc->maximum_buffer_size); |
901 | 0 | p_rc->buffer_level = AOMMIN(p_rc->buffer_level, p_rc->maximum_buffer_size); |
902 | | |
903 | | // Set up frame rate and related parameters rate control values. |
904 | 0 | av1_new_framerate(cpi, cpi->framerate); |
905 | | |
906 | | // Set absolute upper and lower quality limits |
907 | 0 | rc->worst_quality = rc_cfg->worst_allowed_q; |
908 | 0 | rc->best_quality = rc_cfg->best_allowed_q; |
909 | | |
910 | | // If lossless has been requested make sure average Q accumulators are reset. |
911 | 0 | if (is_lossless_requested(&cpi->oxcf.rc_cfg)) { |
912 | 0 | int i; |
913 | 0 | for (i = 0; i < FRAME_TYPES; ++i) { |
914 | 0 | p_rc->avg_frame_qindex[i] = 0; |
915 | 0 | } |
916 | 0 | } |
917 | |
|
918 | 0 | features->interp_filter = |
919 | 0 | oxcf->tile_cfg.enable_large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE; |
920 | 0 | features->switchable_motion_mode = is_switchable_motion_mode_allowed( |
921 | 0 | features->allow_warped_motion, oxcf->motion_mode_cfg.enable_obmc); |
922 | |
|
923 | 0 | if (frm_dim_cfg->render_width > 0 && frm_dim_cfg->render_height > 0) { |
924 | 0 | cm->render_width = frm_dim_cfg->render_width; |
925 | 0 | cm->render_height = frm_dim_cfg->render_height; |
926 | 0 | } else { |
927 | 0 | cm->render_width = frm_dim_cfg->width; |
928 | 0 | cm->render_height = frm_dim_cfg->height; |
929 | 0 | } |
930 | 0 | cm->width = frm_dim_cfg->width; |
931 | 0 | cm->height = frm_dim_cfg->height; |
932 | |
|
933 | 0 | if (cm->width > cpi->data_alloc_width || |
934 | 0 | cm->height > cpi->data_alloc_height || is_sb_size_changed) { |
935 | 0 | av1_free_context_buffers(cm); |
936 | 0 | av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf); |
937 | 0 | av1_free_sms_tree(&cpi->td); |
938 | 0 | av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm)); |
939 | 0 | cpi->td.firstpass_ctx = NULL; |
940 | 0 | alloc_compressor_data(cpi); |
941 | 0 | realloc_segmentation_maps(cpi); |
942 | 0 | cpi->data_alloc_width = cm->width; |
943 | 0 | cpi->data_alloc_height = cm->height; |
944 | 0 | cpi->frame_size_related_setup_done = false; |
945 | 0 | } |
946 | 0 | av1_update_frame_size(cpi); |
947 | |
|
948 | 0 | rc->is_src_frame_alt_ref = 0; |
949 | |
|
950 | 0 | if (!cpi->ppi->rtc_ref.set_ref_frame_config) |
951 | 0 | cpi->ext_flags.refresh_frame.update_pending = 0; |
952 | 0 | cpi->ext_flags.refresh_frame_context_pending = 0; |
953 | |
|
954 | 0 | if (cpi->ppi->use_svc) |
955 | 0 | av1_update_layer_context_change_config(cpi, rc_cfg->target_bandwidth); |
956 | |
|
957 | 0 | check_reset_rc_flag(cpi); |
958 | | |
959 | | // restore the value of lag_in_frame for LAP stage. |
960 | 0 | if (lap_lag_in_frames != -1) { |
961 | 0 | cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames; |
962 | 0 | } |
963 | |
|
964 | | #if CONFIG_REALTIME_ONLY |
965 | | assert(!oxcf->tool_cfg.enable_global_motion); |
966 | | cpi->alloc_pyramid = false; |
967 | | #else |
968 | 0 | cpi->alloc_pyramid = oxcf->tool_cfg.enable_global_motion; |
969 | 0 | #endif // CONFIG_REALTIME_ONLY |
970 | 0 | } |
971 | | |
972 | | static INLINE void init_frame_info(FRAME_INFO *frame_info, |
973 | 0 | const AV1_COMMON *const cm) { |
974 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
975 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
976 | 0 | frame_info->frame_width = cm->width; |
977 | 0 | frame_info->frame_height = cm->height; |
978 | 0 | frame_info->mi_cols = mi_params->mi_cols; |
979 | 0 | frame_info->mi_rows = mi_params->mi_rows; |
980 | 0 | frame_info->mb_cols = mi_params->mb_cols; |
981 | 0 | frame_info->mb_rows = mi_params->mb_rows; |
982 | 0 | frame_info->num_mbs = mi_params->MBs; |
983 | 0 | frame_info->bit_depth = seq_params->bit_depth; |
984 | 0 | frame_info->subsampling_x = seq_params->subsampling_x; |
985 | 0 | frame_info->subsampling_y = seq_params->subsampling_y; |
986 | 0 | } |
987 | | |
988 | 0 | static INLINE void init_frame_index_set(FRAME_INDEX_SET *frame_index_set) { |
989 | 0 | frame_index_set->show_frame_count = 0; |
990 | 0 | } |
991 | | |
992 | 0 | static INLINE void update_counters_for_show_frame(AV1_COMP *const cpi) { |
993 | 0 | assert(cpi->common.show_frame); |
994 | 0 | cpi->frame_index_set.show_frame_count++; |
995 | 0 | cpi->common.current_frame.frame_number++; |
996 | 0 | } |
997 | | |
998 | | AV1_PRIMARY *av1_create_primary_compressor( |
999 | | struct aom_codec_pkt_list *pkt_list_head, int num_lap_buffers, |
1000 | 0 | const AV1EncoderConfig *oxcf) { |
1001 | 0 | AV1_PRIMARY *volatile const ppi = aom_memalign(32, sizeof(AV1_PRIMARY)); |
1002 | 0 | if (!ppi) return NULL; |
1003 | 0 | av1_zero(*ppi); |
1004 | | |
1005 | | // The jmp_buf is valid only for the duration of the function that calls |
1006 | | // setjmp(). Therefore, this function must reset the 'setjmp' field to 0 |
1007 | | // before it returns. |
1008 | 0 | if (setjmp(ppi->error.jmp)) { |
1009 | 0 | ppi->error.setjmp = 0; |
1010 | 0 | av1_remove_primary_compressor(ppi); |
1011 | 0 | return 0; |
1012 | 0 | } |
1013 | 0 | ppi->error.setjmp = 1; |
1014 | |
|
1015 | 0 | ppi->seq_params_locked = 0; |
1016 | 0 | ppi->lap_enabled = num_lap_buffers > 0; |
1017 | 0 | ppi->output_pkt_list = pkt_list_head; |
1018 | 0 | ppi->b_calculate_psnr = CONFIG_INTERNAL_STATS; |
1019 | 0 | ppi->frames_left = oxcf->input_cfg.limit; |
1020 | 0 | ppi->num_fp_contexts = 1; |
1021 | |
|
1022 | 0 | init_config_sequence(ppi, oxcf); |
1023 | |
|
1024 | | #if CONFIG_ENTROPY_STATS |
1025 | | av1_zero(ppi->aggregate_fc); |
1026 | | #endif // CONFIG_ENTROPY_STATS |
1027 | |
|
1028 | 0 | av1_primary_rc_init(oxcf, &ppi->p_rc); |
1029 | | |
1030 | | // For two pass and lag_in_frames > 33 in LAP. |
1031 | 0 | ppi->p_rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_2; |
1032 | 0 | if (ppi->lap_enabled) { |
1033 | 0 | if ((num_lap_buffers < |
1034 | 0 | (MAX_GF_LENGTH_LAP + SCENE_CUT_KEY_TEST_INTERVAL + 1)) && |
1035 | 0 | num_lap_buffers >= (MAX_GF_LENGTH_LAP + 3)) { |
1036 | | /* |
1037 | | * For lag in frames >= 19 and <33, enable scenecut |
1038 | | * with limited future frame prediction. |
1039 | | */ |
1040 | 0 | ppi->p_rc.enable_scenecut_detection = ENABLE_SCENECUT_MODE_1; |
1041 | 0 | } else if (num_lap_buffers < (MAX_GF_LENGTH_LAP + 3)) { |
1042 | | // Disable scenecut when lag_in_frames < 19. |
1043 | 0 | ppi->p_rc.enable_scenecut_detection = DISABLE_SCENECUT; |
1044 | 0 | } |
1045 | 0 | } |
1046 | |
|
1047 | 0 | #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, SDX3DF, JSDAF, JSVAF) \ |
1048 | 0 | ppi->fn_ptr[BT].sdf = SDF; \ |
1049 | 0 | ppi->fn_ptr[BT].sdaf = SDAF; \ |
1050 | 0 | ppi->fn_ptr[BT].vf = VF; \ |
1051 | 0 | ppi->fn_ptr[BT].svf = SVF; \ |
1052 | 0 | ppi->fn_ptr[BT].svaf = SVAF; \ |
1053 | 0 | ppi->fn_ptr[BT].sdx4df = SDX4DF; \ |
1054 | 0 | ppi->fn_ptr[BT].jsdaf = JSDAF; \ |
1055 | 0 | ppi->fn_ptr[BT].jsvaf = JSVAF; \ |
1056 | 0 | ppi->fn_ptr[BT].sdx3df = SDX3DF; |
1057 | | |
1058 | | // Realtime mode doesn't use 4x rectangular blocks. |
1059 | 0 | #if !CONFIG_REALTIME_ONLY |
1060 | 0 | BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16, |
1061 | 0 | aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, |
1062 | 0 | aom_sad4x16x4d, aom_sad4x16x3d, aom_dist_wtd_sad4x16_avg, |
1063 | 0 | aom_dist_wtd_sub_pixel_avg_variance4x16) |
1064 | |
|
1065 | 0 | BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4, |
1066 | 0 | aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, |
1067 | 0 | aom_sad16x4x4d, aom_sad16x4x3d, aom_dist_wtd_sad16x4_avg, |
1068 | 0 | aom_dist_wtd_sub_pixel_avg_variance16x4) |
1069 | |
|
1070 | 0 | BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32, |
1071 | 0 | aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, |
1072 | 0 | aom_sad8x32x4d, aom_sad8x32x3d, aom_dist_wtd_sad8x32_avg, |
1073 | 0 | aom_dist_wtd_sub_pixel_avg_variance8x32) |
1074 | |
|
1075 | 0 | BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8, |
1076 | 0 | aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, |
1077 | 0 | aom_sad32x8x4d, aom_sad32x8x3d, aom_dist_wtd_sad32x8_avg, |
1078 | 0 | aom_dist_wtd_sub_pixel_avg_variance32x8) |
1079 | |
|
1080 | 0 | BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64, |
1081 | 0 | aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, |
1082 | 0 | aom_sad16x64x4d, aom_sad16x64x3d, aom_dist_wtd_sad16x64_avg, |
1083 | 0 | aom_dist_wtd_sub_pixel_avg_variance16x64) |
1084 | |
|
1085 | 0 | BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16, |
1086 | 0 | aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, |
1087 | 0 | aom_sad64x16x4d, aom_sad64x16x3d, aom_dist_wtd_sad64x16_avg, |
1088 | 0 | aom_dist_wtd_sub_pixel_avg_variance64x16) |
1089 | 0 | #endif // !CONFIG_REALTIME_ONLY |
1090 | |
|
1091 | 0 | BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128, |
1092 | 0 | aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128, |
1093 | 0 | aom_sad128x128x4d, aom_sad128x128x3d, aom_dist_wtd_sad128x128_avg, |
1094 | 0 | aom_dist_wtd_sub_pixel_avg_variance128x128) |
1095 | |
|
1096 | 0 | BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64, |
1097 | 0 | aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, |
1098 | 0 | aom_sad128x64x4d, aom_sad128x64x3d, aom_dist_wtd_sad128x64_avg, |
1099 | 0 | aom_dist_wtd_sub_pixel_avg_variance128x64) |
1100 | |
|
1101 | 0 | BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128, |
1102 | 0 | aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, |
1103 | 0 | aom_sad64x128x4d, aom_sad64x128x3d, aom_dist_wtd_sad64x128_avg, |
1104 | 0 | aom_dist_wtd_sub_pixel_avg_variance64x128) |
1105 | |
|
1106 | 0 | BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16, |
1107 | 0 | aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, |
1108 | 0 | aom_sad32x16x4d, aom_sad32x16x3d, aom_dist_wtd_sad32x16_avg, |
1109 | 0 | aom_dist_wtd_sub_pixel_avg_variance32x16) |
1110 | |
|
1111 | 0 | BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32, |
1112 | 0 | aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, |
1113 | 0 | aom_sad16x32x4d, aom_sad16x32x3d, aom_dist_wtd_sad16x32_avg, |
1114 | 0 | aom_dist_wtd_sub_pixel_avg_variance16x32) |
1115 | |
|
1116 | 0 | BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32, |
1117 | 0 | aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, |
1118 | 0 | aom_sad64x32x4d, aom_sad64x32x3d, aom_dist_wtd_sad64x32_avg, |
1119 | 0 | aom_dist_wtd_sub_pixel_avg_variance64x32) |
1120 | |
|
1121 | 0 | BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64, |
1122 | 0 | aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, |
1123 | 0 | aom_sad32x64x4d, aom_sad32x64x3d, aom_dist_wtd_sad32x64_avg, |
1124 | 0 | aom_dist_wtd_sub_pixel_avg_variance32x64) |
1125 | |
|
1126 | 0 | BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32, |
1127 | 0 | aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32, |
1128 | 0 | aom_sad32x32x4d, aom_sad32x32x3d, aom_dist_wtd_sad32x32_avg, |
1129 | 0 | aom_dist_wtd_sub_pixel_avg_variance32x32) |
1130 | |
|
1131 | 0 | BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64, |
1132 | 0 | aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64, |
1133 | 0 | aom_sad64x64x4d, aom_sad64x64x3d, aom_dist_wtd_sad64x64_avg, |
1134 | 0 | aom_dist_wtd_sub_pixel_avg_variance64x64) |
1135 | |
|
1136 | 0 | BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16, |
1137 | 0 | aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16, |
1138 | 0 | aom_sad16x16x4d, aom_sad16x16x3d, aom_dist_wtd_sad16x16_avg, |
1139 | 0 | aom_dist_wtd_sub_pixel_avg_variance16x16) |
1140 | |
|
1141 | 0 | BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8, |
1142 | 0 | aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, |
1143 | 0 | aom_sad16x8x4d, aom_sad16x8x3d, aom_dist_wtd_sad16x8_avg, |
1144 | 0 | aom_dist_wtd_sub_pixel_avg_variance16x8) |
1145 | |
|
1146 | 0 | BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16, |
1147 | 0 | aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, |
1148 | 0 | aom_sad8x16x4d, aom_sad8x16x3d, aom_dist_wtd_sad8x16_avg, |
1149 | 0 | aom_dist_wtd_sub_pixel_avg_variance8x16) |
1150 | |
|
1151 | 0 | BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8, |
1152 | 0 | aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d, |
1153 | 0 | aom_sad8x8x3d, aom_dist_wtd_sad8x8_avg, |
1154 | 0 | aom_dist_wtd_sub_pixel_avg_variance8x8) |
1155 | |
|
1156 | 0 | BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4, |
1157 | 0 | aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d, |
1158 | 0 | aom_sad8x4x3d, aom_dist_wtd_sad8x4_avg, |
1159 | 0 | aom_dist_wtd_sub_pixel_avg_variance8x4) |
1160 | |
|
1161 | 0 | BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8, |
1162 | 0 | aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d, |
1163 | 0 | aom_sad4x8x3d, aom_dist_wtd_sad4x8_avg, |
1164 | 0 | aom_dist_wtd_sub_pixel_avg_variance4x8) |
1165 | |
|
1166 | 0 | BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4, |
1167 | 0 | aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d, |
1168 | 0 | aom_sad4x4x3d, aom_dist_wtd_sad4x4_avg, |
1169 | 0 | aom_dist_wtd_sub_pixel_avg_variance4x4) |
1170 | |
|
1171 | 0 | #if !CONFIG_REALTIME_ONLY |
1172 | 0 | #define OBFP(BT, OSDF, OVF, OSVF) \ |
1173 | 0 | ppi->fn_ptr[BT].osdf = OSDF; \ |
1174 | 0 | ppi->fn_ptr[BT].ovf = OVF; \ |
1175 | 0 | ppi->fn_ptr[BT].osvf = OSVF; |
1176 | |
|
1177 | 0 | OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128, |
1178 | 0 | aom_obmc_sub_pixel_variance128x128) |
1179 | 0 | OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64, |
1180 | 0 | aom_obmc_sub_pixel_variance128x64) |
1181 | 0 | OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128, |
1182 | 0 | aom_obmc_sub_pixel_variance64x128) |
1183 | 0 | OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64, |
1184 | 0 | aom_obmc_sub_pixel_variance64x64) |
1185 | 0 | OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32, |
1186 | 0 | aom_obmc_sub_pixel_variance64x32) |
1187 | 0 | OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64, |
1188 | 0 | aom_obmc_sub_pixel_variance32x64) |
1189 | 0 | OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32, |
1190 | 0 | aom_obmc_sub_pixel_variance32x32) |
1191 | 0 | OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16, |
1192 | 0 | aom_obmc_sub_pixel_variance32x16) |
1193 | 0 | OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32, |
1194 | 0 | aom_obmc_sub_pixel_variance16x32) |
1195 | 0 | OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16, |
1196 | 0 | aom_obmc_sub_pixel_variance16x16) |
1197 | 0 | OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8, |
1198 | 0 | aom_obmc_sub_pixel_variance16x8) |
1199 | 0 | OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16, |
1200 | 0 | aom_obmc_sub_pixel_variance8x16) |
1201 | 0 | OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8, |
1202 | 0 | aom_obmc_sub_pixel_variance8x8) |
1203 | 0 | OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8, |
1204 | 0 | aom_obmc_sub_pixel_variance4x8) |
1205 | 0 | OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4, |
1206 | 0 | aom_obmc_sub_pixel_variance8x4) |
1207 | 0 | OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4, |
1208 | 0 | aom_obmc_sub_pixel_variance4x4) |
1209 | 0 | OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16, |
1210 | 0 | aom_obmc_sub_pixel_variance4x16) |
1211 | 0 | OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4, |
1212 | 0 | aom_obmc_sub_pixel_variance16x4) |
1213 | 0 | OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32, |
1214 | 0 | aom_obmc_sub_pixel_variance8x32) |
1215 | 0 | OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8, |
1216 | 0 | aom_obmc_sub_pixel_variance32x8) |
1217 | 0 | OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64, |
1218 | 0 | aom_obmc_sub_pixel_variance16x64) |
1219 | 0 | OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16, |
1220 | 0 | aom_obmc_sub_pixel_variance64x16) |
1221 | 0 | #endif // !CONFIG_REALTIME_ONLY |
1222 | |
|
1223 | 0 | #define MBFP(BT, MCSDF, MCSVF) \ |
1224 | 0 | ppi->fn_ptr[BT].msdf = MCSDF; \ |
1225 | 0 | ppi->fn_ptr[BT].msvf = MCSVF; |
1226 | |
|
1227 | 0 | MBFP(BLOCK_128X128, aom_masked_sad128x128, |
1228 | 0 | aom_masked_sub_pixel_variance128x128) |
1229 | 0 | MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64) |
1230 | 0 | MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128) |
1231 | 0 | MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64) |
1232 | 0 | MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32) |
1233 | 0 | MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64) |
1234 | 0 | MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32) |
1235 | 0 | MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16) |
1236 | 0 | MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32) |
1237 | 0 | MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16) |
1238 | 0 | MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8) |
1239 | 0 | MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16) |
1240 | 0 | MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8) |
1241 | 0 | MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8) |
1242 | 0 | MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4) |
1243 | 0 | MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4) |
1244 | |
|
1245 | 0 | #if !CONFIG_REALTIME_ONLY |
1246 | 0 | MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16) |
1247 | 0 | MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4) |
1248 | 0 | MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32) |
1249 | 0 | MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8) |
1250 | 0 | MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64) |
1251 | 0 | MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16) |
1252 | 0 | #endif |
1253 | |
|
1254 | 0 | #define SDSFP(BT, SDSF, SDSX4DF) \ |
1255 | 0 | ppi->fn_ptr[BT].sdsf = SDSF; \ |
1256 | 0 | ppi->fn_ptr[BT].sdsx4df = SDSX4DF; |
1257 | |
|
1258 | 0 | SDSFP(BLOCK_128X128, aom_sad_skip_128x128, aom_sad_skip_128x128x4d) |
1259 | 0 | SDSFP(BLOCK_128X64, aom_sad_skip_128x64, aom_sad_skip_128x64x4d) |
1260 | 0 | SDSFP(BLOCK_64X128, aom_sad_skip_64x128, aom_sad_skip_64x128x4d) |
1261 | 0 | SDSFP(BLOCK_64X64, aom_sad_skip_64x64, aom_sad_skip_64x64x4d) |
1262 | 0 | SDSFP(BLOCK_64X32, aom_sad_skip_64x32, aom_sad_skip_64x32x4d) |
1263 | |
|
1264 | 0 | SDSFP(BLOCK_32X64, aom_sad_skip_32x64, aom_sad_skip_32x64x4d) |
1265 | 0 | SDSFP(BLOCK_32X32, aom_sad_skip_32x32, aom_sad_skip_32x32x4d) |
1266 | 0 | SDSFP(BLOCK_32X16, aom_sad_skip_32x16, aom_sad_skip_32x16x4d) |
1267 | |
|
1268 | 0 | SDSFP(BLOCK_16X32, aom_sad_skip_16x32, aom_sad_skip_16x32x4d) |
1269 | 0 | SDSFP(BLOCK_16X16, aom_sad_skip_16x16, aom_sad_skip_16x16x4d) |
1270 | 0 | SDSFP(BLOCK_16X8, aom_sad_skip_16x8, aom_sad_skip_16x8x4d) |
1271 | 0 | SDSFP(BLOCK_8X16, aom_sad_skip_8x16, aom_sad_skip_8x16x4d) |
1272 | 0 | SDSFP(BLOCK_8X8, aom_sad_skip_8x8, aom_sad_skip_8x8x4d) |
1273 | |
|
1274 | 0 | SDSFP(BLOCK_4X8, aom_sad_skip_4x8, aom_sad_skip_4x8x4d) |
1275 | |
|
1276 | 0 | #if !CONFIG_REALTIME_ONLY |
1277 | 0 | SDSFP(BLOCK_64X16, aom_sad_skip_64x16, aom_sad_skip_64x16x4d) |
1278 | 0 | SDSFP(BLOCK_16X64, aom_sad_skip_16x64, aom_sad_skip_16x64x4d) |
1279 | 0 | SDSFP(BLOCK_32X8, aom_sad_skip_32x8, aom_sad_skip_32x8x4d) |
1280 | 0 | SDSFP(BLOCK_8X32, aom_sad_skip_8x32, aom_sad_skip_8x32x4d) |
1281 | 0 | SDSFP(BLOCK_4X16, aom_sad_skip_4x16, aom_sad_skip_4x16x4d) |
1282 | 0 | #endif |
1283 | 0 | #undef SDSFP |
1284 | |
|
1285 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
1286 | 0 | highbd_set_var_fns(ppi); |
1287 | 0 | #endif |
1288 | |
|
1289 | 0 | { |
1290 | | // As cm->mi_params is a part of the frame level context (cpi), it is |
1291 | | // unavailable at this point. mi_params is created as a local temporary |
1292 | | // variable, to be passed into the functions used for allocating tpl |
1293 | | // buffers. The values in this variable are populated according to initial |
1294 | | // width and height of the frame. |
1295 | 0 | CommonModeInfoParams mi_params; |
1296 | 0 | enc_set_mb_mi(&mi_params, oxcf->frm_dim_cfg.width, oxcf->frm_dim_cfg.height, |
1297 | 0 | BLOCK_4X4); |
1298 | |
|
1299 | 0 | const BLOCK_SIZE bsize = BLOCK_16X16; |
1300 | 0 | const int w = mi_size_wide[bsize]; |
1301 | 0 | const int h = mi_size_high[bsize]; |
1302 | 0 | const int num_cols = (mi_params.mi_cols + w - 1) / w; |
1303 | 0 | const int num_rows = (mi_params.mi_rows + h - 1) / h; |
1304 | 0 | AOM_CHECK_MEM_ERROR( |
1305 | 0 | &ppi->error, ppi->tpl_sb_rdmult_scaling_factors, |
1306 | 0 | aom_calloc(num_rows * num_cols, |
1307 | 0 | sizeof(*ppi->tpl_sb_rdmult_scaling_factors))); |
1308 | |
|
1309 | | #if CONFIG_INTERNAL_STATS |
1310 | | ppi->b_calculate_blockiness = 1; |
1311 | | ppi->b_calculate_consistency = 1; |
1312 | | |
1313 | | for (int i = 0; i <= STAT_ALL; i++) { |
1314 | | ppi->psnr[0].stat[i] = 0; |
1315 | | ppi->psnr[1].stat[i] = 0; |
1316 | | |
1317 | | ppi->fastssim.stat[i] = 0; |
1318 | | ppi->psnrhvs.stat[i] = 0; |
1319 | | } |
1320 | | |
1321 | | ppi->psnr[0].worst = 100.0; |
1322 | | ppi->psnr[1].worst = 100.0; |
1323 | | ppi->worst_ssim = 100.0; |
1324 | | ppi->worst_ssim_hbd = 100.0; |
1325 | | |
1326 | | ppi->count[0] = 0; |
1327 | | ppi->count[1] = 0; |
1328 | | ppi->total_bytes = 0; |
1329 | | |
1330 | | if (ppi->b_calculate_psnr) { |
1331 | | ppi->total_sq_error[0] = 0; |
1332 | | ppi->total_samples[0] = 0; |
1333 | | ppi->total_sq_error[1] = 0; |
1334 | | ppi->total_samples[1] = 0; |
1335 | | ppi->total_recode_hits = 0; |
1336 | | ppi->summed_quality = 0; |
1337 | | ppi->summed_weights = 0; |
1338 | | ppi->summed_quality_hbd = 0; |
1339 | | ppi->summed_weights_hbd = 0; |
1340 | | } |
1341 | | |
1342 | | ppi->fastssim.worst = 100.0; |
1343 | | ppi->psnrhvs.worst = 100.0; |
1344 | | |
1345 | | if (ppi->b_calculate_blockiness) { |
1346 | | ppi->total_blockiness = 0; |
1347 | | ppi->worst_blockiness = 0.0; |
1348 | | } |
1349 | | |
1350 | | ppi->total_inconsistency = 0; |
1351 | | ppi->worst_consistency = 100.0; |
1352 | | if (ppi->b_calculate_consistency) { |
1353 | | AOM_CHECK_MEM_ERROR(&ppi->error, ppi->ssim_vars, |
1354 | | aom_malloc(sizeof(*ppi->ssim_vars) * 4 * |
1355 | | mi_params.mi_rows * mi_params.mi_cols)); |
1356 | | } |
1357 | | #endif |
1358 | 0 | } |
1359 | |
|
1360 | 0 | ppi->error.setjmp = 0; |
1361 | 0 | return ppi; |
1362 | 0 | } |
1363 | | |
1364 | | AV1_COMP *av1_create_compressor(AV1_PRIMARY *ppi, const AV1EncoderConfig *oxcf, |
1365 | | BufferPool *const pool, COMPRESSOR_STAGE stage, |
1366 | 0 | int lap_lag_in_frames) { |
1367 | 0 | AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP)); |
1368 | |
|
1369 | 0 | if (!cpi) return NULL; |
1370 | | |
1371 | 0 | av1_zero(*cpi); |
1372 | |
|
1373 | 0 | cpi->ppi = ppi; |
1374 | |
|
1375 | 0 | AV1_COMMON *volatile const cm = &cpi->common; |
1376 | 0 | cm->seq_params = &ppi->seq_params; |
1377 | 0 | cm->error = |
1378 | 0 | (struct aom_internal_error_info *)aom_calloc(1, sizeof(*cm->error)); |
1379 | 0 | if (!cm->error) { |
1380 | 0 | aom_free(cpi); |
1381 | 0 | return NULL; |
1382 | 0 | } |
1383 | | |
1384 | | // The jmp_buf is valid only for the duration of the function that calls |
1385 | | // setjmp(). Therefore, this function must reset the 'setjmp' field to 0 |
1386 | | // before it returns. |
1387 | 0 | if (setjmp(cm->error->jmp)) { |
1388 | 0 | cm->error->setjmp = 0; |
1389 | 0 | av1_remove_compressor(cpi); |
1390 | 0 | return NULL; |
1391 | 0 | } |
1392 | | |
1393 | 0 | cm->error->setjmp = 1; |
1394 | 0 | cpi->compressor_stage = stage; |
1395 | |
|
1396 | 0 | cpi->do_frame_data_update = true; |
1397 | |
|
1398 | 0 | CommonModeInfoParams *const mi_params = &cm->mi_params; |
1399 | 0 | mi_params->free_mi = enc_free_mi; |
1400 | 0 | mi_params->setup_mi = enc_setup_mi; |
1401 | 0 | mi_params->set_mb_mi = |
1402 | 0 | (oxcf->pass == AOM_RC_FIRST_PASS || cpi->compressor_stage == LAP_STAGE) |
1403 | 0 | ? stat_stage_set_mb_mi |
1404 | 0 | : enc_set_mb_mi; |
1405 | |
|
1406 | 0 | mi_params->mi_alloc_bsize = BLOCK_4X4; |
1407 | |
|
1408 | 0 | CHECK_MEM_ERROR(cm, cm->fc, |
1409 | 0 | (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc))); |
1410 | 0 | CHECK_MEM_ERROR( |
1411 | 0 | cm, cm->default_frame_context, |
1412 | 0 | (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context))); |
1413 | 0 | memset(cm->fc, 0, sizeof(*cm->fc)); |
1414 | 0 | memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context)); |
1415 | |
|
1416 | 0 | cpi->common.buffer_pool = pool; |
1417 | |
|
1418 | 0 | init_config(cpi, oxcf); |
1419 | 0 | if (cpi->compressor_stage == LAP_STAGE) { |
1420 | 0 | cpi->oxcf.gf_cfg.lag_in_frames = lap_lag_in_frames; |
1421 | 0 | } |
1422 | |
|
1423 | 0 | av1_rc_init(&cpi->oxcf, &cpi->rc); |
1424 | |
|
1425 | 0 | init_frame_info(&cpi->frame_info, cm); |
1426 | 0 | init_frame_index_set(&cpi->frame_index_set); |
1427 | |
|
1428 | 0 | cm->current_frame.frame_number = 0; |
1429 | 0 | cpi->rc.frame_number_encoded = 0; |
1430 | 0 | cpi->rc.prev_frame_is_dropped = 0; |
1431 | 0 | cpi->rc.max_consec_drop = INT_MAX; |
1432 | 0 | cpi->rc.drop_count_consec = 0; |
1433 | 0 | cm->current_frame_id = -1; |
1434 | 0 | cpi->tile_data = NULL; |
1435 | 0 | cpi->last_show_frame_buf = NULL; |
1436 | 0 | realloc_segmentation_maps(cpi); |
1437 | |
|
1438 | 0 | cpi->refresh_frame.alt_ref_frame = false; |
1439 | |
|
1440 | | #if CONFIG_SPEED_STATS |
1441 | | cpi->tx_search_count = 0; |
1442 | | #endif // CONFIG_SPEED_STATS |
1443 | |
|
1444 | 0 | cpi->time_stamps.first_ts_start = INT64_MAX; |
1445 | |
|
1446 | | #ifdef OUTPUT_YUV_REC |
1447 | | yuv_rec_file = fopen("rec.yuv", "wb"); |
1448 | | #endif |
1449 | | #ifdef OUTPUT_YUV_DENOISED |
1450 | | yuv_denoised_file = fopen("denoised.yuv", "wb"); |
1451 | | #endif |
1452 | |
|
1453 | 0 | #if !CONFIG_REALTIME_ONLY |
1454 | 0 | if (is_stat_consumption_stage(cpi)) { |
1455 | 0 | const size_t packet_sz = sizeof(FIRSTPASS_STATS); |
1456 | 0 | const int packets = (int)(oxcf->twopass_stats_in.sz / packet_sz); |
1457 | |
|
1458 | 0 | if (!cpi->ppi->lap_enabled) { |
1459 | | /*Re-initialize to stats buffer, populated by application in the case of |
1460 | | * two pass*/ |
1461 | 0 | cpi->ppi->twopass.stats_buf_ctx->stats_in_start = |
1462 | 0 | oxcf->twopass_stats_in.buf; |
1463 | 0 | cpi->twopass_frame.stats_in = |
1464 | 0 | cpi->ppi->twopass.stats_buf_ctx->stats_in_start; |
1465 | 0 | cpi->ppi->twopass.stats_buf_ctx->stats_in_end = |
1466 | 0 | &cpi->ppi->twopass.stats_buf_ctx->stats_in_start[packets - 1]; |
1467 | | |
1468 | | // The buffer size is packets - 1 because the last packet is total_stats. |
1469 | 0 | av1_firstpass_info_init(&cpi->ppi->twopass.firstpass_info, |
1470 | 0 | oxcf->twopass_stats_in.buf, packets - 1); |
1471 | 0 | av1_init_second_pass(cpi); |
1472 | 0 | } else { |
1473 | 0 | av1_firstpass_info_init(&cpi->ppi->twopass.firstpass_info, NULL, 0); |
1474 | 0 | av1_init_single_pass_lap(cpi); |
1475 | 0 | } |
1476 | 0 | } |
1477 | 0 | #endif |
1478 | | |
1479 | | // The buffer "obmc_buffer" is used in inter frames for fast obmc search. |
1480 | | // Hence, the memory allocation for the same is avoided for allintra encoding |
1481 | | // mode. |
1482 | 0 | if (cpi->oxcf.kf_cfg.key_freq_max != 0) |
1483 | 0 | alloc_obmc_buffers(&cpi->td.mb.obmc_buffer, cm->error); |
1484 | |
|
1485 | 0 | for (int x = 0; x < 2; x++) |
1486 | 0 | for (int y = 0; y < 2; y++) |
1487 | 0 | CHECK_MEM_ERROR( |
1488 | 0 | cm, cpi->td.mb.intrabc_hash_info.hash_value_buffer[x][y], |
1489 | 0 | (uint32_t *)aom_malloc( |
1490 | 0 | AOM_BUFFER_SIZE_FOR_BLOCK_HASH * |
1491 | 0 | sizeof(*cpi->td.mb.intrabc_hash_info.hash_value_buffer[0][0]))); |
1492 | |
|
1493 | 0 | cpi->td.mb.intrabc_hash_info.g_crc_initialized = 0; |
1494 | |
|
1495 | 0 | av1_set_speed_features_framesize_independent(cpi, oxcf->speed); |
1496 | 0 | av1_set_speed_features_framesize_dependent(cpi, oxcf->speed); |
1497 | |
|
1498 | 0 | int max_mi_cols = mi_params->mi_cols; |
1499 | 0 | int max_mi_rows = mi_params->mi_rows; |
1500 | 0 | if (oxcf->frm_dim_cfg.forced_max_frame_width) { |
1501 | 0 | max_mi_cols = size_in_mi(oxcf->frm_dim_cfg.forced_max_frame_width); |
1502 | 0 | } |
1503 | 0 | if (oxcf->frm_dim_cfg.forced_max_frame_height) { |
1504 | 0 | max_mi_rows = size_in_mi(oxcf->frm_dim_cfg.forced_max_frame_height); |
1505 | 0 | } |
1506 | |
|
1507 | 0 | const int consec_zero_mv_alloc_size = (max_mi_rows * max_mi_cols) >> 2; |
1508 | 0 | CHECK_MEM_ERROR( |
1509 | 0 | cm, cpi->consec_zero_mv, |
1510 | 0 | aom_calloc(consec_zero_mv_alloc_size, sizeof(*cpi->consec_zero_mv))); |
1511 | 0 | cpi->consec_zero_mv_alloc_size = consec_zero_mv_alloc_size; |
1512 | |
|
1513 | 0 | cpi->mb_weber_stats = NULL; |
1514 | 0 | cpi->mb_delta_q = NULL; |
1515 | 0 | cpi->palette_pixel_num = 0; |
1516 | 0 | cpi->scaled_last_source_available = 0; |
1517 | |
|
1518 | 0 | { |
1519 | 0 | const BLOCK_SIZE bsize = BLOCK_16X16; |
1520 | 0 | const int w = mi_size_wide[bsize]; |
1521 | 0 | const int h = mi_size_high[bsize]; |
1522 | 0 | const int num_cols = (max_mi_cols + w - 1) / w; |
1523 | 0 | const int num_rows = (max_mi_rows + h - 1) / h; |
1524 | 0 | CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors, |
1525 | 0 | aom_calloc(num_rows * num_cols, |
1526 | 0 | sizeof(*cpi->ssim_rdmult_scaling_factors))); |
1527 | 0 | CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors, |
1528 | 0 | aom_calloc(num_rows * num_cols, |
1529 | 0 | sizeof(*cpi->tpl_rdmult_scaling_factors))); |
1530 | 0 | } |
1531 | |
|
1532 | | #if CONFIG_TUNE_VMAF |
1533 | | { |
1534 | | const BLOCK_SIZE bsize = BLOCK_64X64; |
1535 | | const int w = mi_size_wide[bsize]; |
1536 | | const int h = mi_size_high[bsize]; |
1537 | | const int num_cols = (mi_params->mi_cols + w - 1) / w; |
1538 | | const int num_rows = (mi_params->mi_rows + h - 1) / h; |
1539 | | CHECK_MEM_ERROR(cm, cpi->vmaf_info.rdmult_scaling_factors, |
1540 | | aom_calloc(num_rows * num_cols, |
1541 | | sizeof(*cpi->vmaf_info.rdmult_scaling_factors))); |
1542 | | for (int i = 0; i < MAX_ARF_LAYERS; i++) { |
1543 | | cpi->vmaf_info.last_frame_unsharp_amount[i] = -1.0; |
1544 | | cpi->vmaf_info.last_frame_ysse[i] = -1.0; |
1545 | | cpi->vmaf_info.last_frame_vmaf[i] = -1.0; |
1546 | | } |
1547 | | cpi->vmaf_info.original_qindex = -1; |
1548 | | cpi->vmaf_info.vmaf_model = NULL; |
1549 | | } |
1550 | | #endif |
1551 | |
|
1552 | | #if CONFIG_TUNE_BUTTERAUGLI |
1553 | | { |
1554 | | const int w = mi_size_wide[butteraugli_rdo_bsize]; |
1555 | | const int h = mi_size_high[butteraugli_rdo_bsize]; |
1556 | | const int num_cols = (mi_params->mi_cols + w - 1) / w; |
1557 | | const int num_rows = (mi_params->mi_rows + h - 1) / h; |
1558 | | CHECK_MEM_ERROR( |
1559 | | cm, cpi->butteraugli_info.rdmult_scaling_factors, |
1560 | | aom_malloc(num_rows * num_cols * |
1561 | | sizeof(*cpi->butteraugli_info.rdmult_scaling_factors))); |
1562 | | memset(&cpi->butteraugli_info.source, 0, |
1563 | | sizeof(cpi->butteraugli_info.source)); |
1564 | | memset(&cpi->butteraugli_info.resized_source, 0, |
1565 | | sizeof(cpi->butteraugli_info.resized_source)); |
1566 | | cpi->butteraugli_info.recon_set = false; |
1567 | | } |
1568 | | #endif |
1569 | |
|
1570 | | #if CONFIG_SALIENCY_MAP |
1571 | | { |
1572 | | CHECK_MEM_ERROR(cm, cpi->saliency_map, |
1573 | | (uint8_t *)aom_calloc(cm->height * cm->width, |
1574 | | sizeof(*cpi->saliency_map))); |
1575 | | // Buffer initialization based on MIN_MIB_SIZE_LOG2 to ensure that |
1576 | | // cpi->sm_scaling_factor buffer is allocated big enough, since we have no |
1577 | | // idea of the actual superblock size we are going to use yet. |
1578 | | const int min_mi_w_sb = (1 << MIN_MIB_SIZE_LOG2); |
1579 | | const int min_mi_h_sb = (1 << MIN_MIB_SIZE_LOG2); |
1580 | | const int max_sb_cols = |
1581 | | (cm->mi_params.mi_cols + min_mi_w_sb - 1) / min_mi_w_sb; |
1582 | | const int max_sb_rows = |
1583 | | (cm->mi_params.mi_rows + min_mi_h_sb - 1) / min_mi_h_sb; |
1584 | | CHECK_MEM_ERROR(cm, cpi->sm_scaling_factor, |
1585 | | (double *)aom_calloc(max_sb_rows * max_sb_cols, |
1586 | | sizeof(*cpi->sm_scaling_factor))); |
1587 | | } |
1588 | | #endif |
1589 | |
|
1590 | | #if CONFIG_COLLECT_PARTITION_STATS |
1591 | | av1_zero(cpi->partition_stats); |
1592 | | #endif // CONFIG_COLLECT_PARTITION_STATS |
1593 | | |
1594 | | // Initialize the members of DeltaQuantParams with INT_MAX to ensure that |
1595 | | // the quantizer tables are correctly initialized using the default deltaq |
1596 | | // parameters when av1_init_quantizer is called for the first time. |
1597 | 0 | DeltaQuantParams *const prev_deltaq_params = |
1598 | 0 | &cpi->enc_quant_dequant_params.prev_deltaq_params; |
1599 | 0 | prev_deltaq_params->y_dc_delta_q = INT_MAX; |
1600 | 0 | prev_deltaq_params->u_dc_delta_q = INT_MAX; |
1601 | 0 | prev_deltaq_params->v_dc_delta_q = INT_MAX; |
1602 | 0 | prev_deltaq_params->u_ac_delta_q = INT_MAX; |
1603 | 0 | prev_deltaq_params->v_ac_delta_q = INT_MAX; |
1604 | |
|
1605 | 0 | av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params, |
1606 | 0 | cm->seq_params->bit_depth); |
1607 | 0 | av1_qm_init(&cm->quant_params, av1_num_planes(cm)); |
1608 | |
|
1609 | 0 | av1_loop_filter_init(cm); |
1610 | 0 | cm->superres_scale_denominator = SCALE_NUMERATOR; |
1611 | 0 | cm->superres_upscaled_width = oxcf->frm_dim_cfg.width; |
1612 | 0 | cm->superres_upscaled_height = oxcf->frm_dim_cfg.height; |
1613 | 0 | #if !CONFIG_REALTIME_ONLY |
1614 | 0 | av1_loop_restoration_precal(); |
1615 | 0 | #endif |
1616 | |
|
1617 | 0 | cpi->third_pass_ctx = NULL; |
1618 | 0 | if (cpi->oxcf.pass == AOM_RC_THIRD_PASS) { |
1619 | 0 | av1_init_thirdpass_ctx(cm, &cpi->third_pass_ctx, NULL); |
1620 | 0 | } |
1621 | |
|
1622 | 0 | cpi->second_pass_log_stream = NULL; |
1623 | 0 | cpi->use_ducky_encode = 0; |
1624 | |
|
1625 | 0 | cm->error->setjmp = 0; |
1626 | 0 | return cpi; |
1627 | 0 | } |
1628 | | |
1629 | | #if CONFIG_INTERNAL_STATS |
1630 | | #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T)) |
1631 | | |
1632 | | #define SNPRINT2(H, T, V) \ |
1633 | | snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V)) |
1634 | | #endif // CONFIG_INTERNAL_STATS |
1635 | | |
1636 | 0 | void av1_remove_primary_compressor(AV1_PRIMARY *ppi) { |
1637 | 0 | if (!ppi) return; |
1638 | 0 | #if !CONFIG_REALTIME_ONLY |
1639 | 0 | av1_tf_info_free(&ppi->tf_info); |
1640 | 0 | #endif // !CONFIG_REALTIME_ONLY |
1641 | |
|
1642 | 0 | for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) { |
1643 | 0 | aom_free(ppi->level_params.level_info[i]); |
1644 | 0 | } |
1645 | 0 | av1_lookahead_destroy(ppi->lookahead); |
1646 | |
|
1647 | 0 | aom_free(ppi->tpl_sb_rdmult_scaling_factors); |
1648 | 0 | ppi->tpl_sb_rdmult_scaling_factors = NULL; |
1649 | |
|
1650 | 0 | TplParams *const tpl_data = &ppi->tpl_data; |
1651 | 0 | aom_free(tpl_data->txfm_stats_list); |
1652 | |
|
1653 | 0 | for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) { |
1654 | 0 | aom_free(tpl_data->tpl_stats_pool[frame]); |
1655 | 0 | aom_free_frame_buffer(&tpl_data->tpl_rec_pool[frame]); |
1656 | 0 | tpl_data->tpl_stats_pool[frame] = NULL; |
1657 | 0 | } |
1658 | |
|
1659 | 0 | #if !CONFIG_REALTIME_ONLY |
1660 | 0 | av1_tpl_dealloc(&tpl_data->tpl_mt_sync); |
1661 | 0 | #endif |
1662 | |
|
1663 | 0 | av1_terminate_workers(ppi); |
1664 | 0 | free_thread_data(ppi); |
1665 | |
|
1666 | 0 | aom_free(ppi->p_mt_info.tile_thr_data); |
1667 | 0 | ppi->p_mt_info.tile_thr_data = NULL; |
1668 | 0 | aom_free(ppi->p_mt_info.workers); |
1669 | 0 | ppi->p_mt_info.workers = NULL; |
1670 | 0 | ppi->p_mt_info.num_workers = 0; |
1671 | |
|
1672 | 0 | aom_free(ppi); |
1673 | 0 | } |
1674 | | |
1675 | 0 | void av1_remove_compressor(AV1_COMP *cpi) { |
1676 | 0 | if (!cpi) return; |
1677 | | #if CONFIG_RATECTRL_LOG |
1678 | | if (cpi->oxcf.pass == 3) { |
1679 | | rc_log_show(&cpi->rc_log); |
1680 | | } |
1681 | | #endif // CONFIG_RATECTRL_LOG |
1682 | | |
1683 | 0 | AV1_COMMON *cm = &cpi->common; |
1684 | 0 | if (cm->current_frame.frame_number > 0) { |
1685 | | #if CONFIG_SPEED_STATS |
1686 | | if (!is_stat_generation_stage(cpi)) { |
1687 | | fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count); |
1688 | | } |
1689 | | #endif // CONFIG_SPEED_STATS |
1690 | |
|
1691 | | #if CONFIG_COLLECT_PARTITION_STATS == 2 |
1692 | | if (!is_stat_generation_stage(cpi)) { |
1693 | | av1_print_fr_partition_timing_stats(&cpi->partition_stats, |
1694 | | "fr_part_timing_data.csv"); |
1695 | | } |
1696 | | #endif |
1697 | 0 | } |
1698 | |
|
1699 | | #if CONFIG_AV1_TEMPORAL_DENOISING |
1700 | | av1_denoiser_free(&(cpi->denoiser)); |
1701 | | #endif |
1702 | |
|
1703 | 0 | if (cm->error) { |
1704 | | // Help detect use after free of the error detail string. |
1705 | 0 | memset(cm->error->detail, 'A', sizeof(cm->error->detail) - 1); |
1706 | 0 | cm->error->detail[sizeof(cm->error->detail) - 1] = '\0'; |
1707 | 0 | aom_free(cm->error); |
1708 | 0 | } |
1709 | 0 | aom_free(cpi->td.tctx); |
1710 | 0 | MultiThreadInfo *const mt_info = &cpi->mt_info; |
1711 | 0 | #if CONFIG_MULTITHREAD |
1712 | 0 | pthread_mutex_t *const enc_row_mt_mutex_ = mt_info->enc_row_mt.mutex_; |
1713 | 0 | pthread_cond_t *const enc_row_mt_cond_ = mt_info->enc_row_mt.cond_; |
1714 | 0 | pthread_mutex_t *const gm_mt_mutex_ = mt_info->gm_sync.mutex_; |
1715 | 0 | pthread_mutex_t *const tpl_error_mutex_ = mt_info->tpl_row_mt.mutex_; |
1716 | 0 | pthread_mutex_t *const pack_bs_mt_mutex_ = mt_info->pack_bs_sync.mutex_; |
1717 | 0 | if (enc_row_mt_mutex_ != NULL) { |
1718 | 0 | pthread_mutex_destroy(enc_row_mt_mutex_); |
1719 | 0 | aom_free(enc_row_mt_mutex_); |
1720 | 0 | } |
1721 | 0 | if (enc_row_mt_cond_ != NULL) { |
1722 | 0 | pthread_cond_destroy(enc_row_mt_cond_); |
1723 | 0 | aom_free(enc_row_mt_cond_); |
1724 | 0 | } |
1725 | 0 | if (gm_mt_mutex_ != NULL) { |
1726 | 0 | pthread_mutex_destroy(gm_mt_mutex_); |
1727 | 0 | aom_free(gm_mt_mutex_); |
1728 | 0 | } |
1729 | 0 | if (tpl_error_mutex_ != NULL) { |
1730 | 0 | pthread_mutex_destroy(tpl_error_mutex_); |
1731 | 0 | aom_free(tpl_error_mutex_); |
1732 | 0 | } |
1733 | 0 | if (pack_bs_mt_mutex_ != NULL) { |
1734 | 0 | pthread_mutex_destroy(pack_bs_mt_mutex_); |
1735 | 0 | aom_free(pack_bs_mt_mutex_); |
1736 | 0 | } |
1737 | 0 | #endif |
1738 | 0 | av1_row_mt_mem_dealloc(cpi); |
1739 | |
|
1740 | 0 | if (mt_info->num_workers > 1) { |
1741 | 0 | av1_row_mt_sync_mem_dealloc(&cpi->ppi->intra_row_mt_sync); |
1742 | 0 | av1_loop_filter_dealloc(&mt_info->lf_row_sync); |
1743 | 0 | av1_cdef_mt_dealloc(&mt_info->cdef_sync); |
1744 | 0 | #if !CONFIG_REALTIME_ONLY |
1745 | 0 | av1_loop_restoration_dealloc(&mt_info->lr_row_sync); |
1746 | 0 | av1_tf_mt_dealloc(&mt_info->tf_sync); |
1747 | 0 | #endif |
1748 | 0 | } |
1749 | |
|
1750 | 0 | av1_free_thirdpass_ctx(cpi->third_pass_ctx); |
1751 | |
|
1752 | 0 | av1_close_second_pass_log(cpi); |
1753 | |
|
1754 | 0 | dealloc_compressor_data(cpi); |
1755 | |
|
1756 | 0 | av1_ext_part_delete(&cpi->ext_part_controller); |
1757 | |
|
1758 | 0 | av1_remove_common(cm); |
1759 | |
|
1760 | 0 | aom_free(cpi); |
1761 | |
|
1762 | | #ifdef OUTPUT_YUV_REC |
1763 | | fclose(yuv_rec_file); |
1764 | | #endif |
1765 | |
|
1766 | | #ifdef OUTPUT_YUV_DENOISED |
1767 | | fclose(yuv_denoised_file); |
1768 | | #endif |
1769 | 0 | } |
1770 | | |
1771 | 0 | static void generate_psnr_packet(AV1_COMP *cpi) { |
1772 | 0 | struct aom_codec_cx_pkt pkt; |
1773 | 0 | int i; |
1774 | 0 | PSNR_STATS psnr; |
1775 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
1776 | 0 | const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth; |
1777 | 0 | const uint32_t bit_depth = cpi->td.mb.e_mbd.bd; |
1778 | 0 | aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr, |
1779 | 0 | bit_depth, in_bit_depth); |
1780 | | #else |
1781 | | aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr); |
1782 | | #endif |
1783 | |
|
1784 | 0 | for (i = 0; i < 4; ++i) { |
1785 | 0 | pkt.data.psnr.samples[i] = psnr.samples[i]; |
1786 | 0 | pkt.data.psnr.sse[i] = psnr.sse[i]; |
1787 | 0 | pkt.data.psnr.psnr[i] = psnr.psnr[i]; |
1788 | 0 | } |
1789 | |
|
1790 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
1791 | 0 | if ((cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) && |
1792 | 0 | (in_bit_depth < bit_depth)) { |
1793 | 0 | for (i = 0; i < 4; ++i) { |
1794 | 0 | pkt.data.psnr.samples_hbd[i] = psnr.samples_hbd[i]; |
1795 | 0 | pkt.data.psnr.sse_hbd[i] = psnr.sse_hbd[i]; |
1796 | 0 | pkt.data.psnr.psnr_hbd[i] = psnr.psnr_hbd[i]; |
1797 | 0 | } |
1798 | 0 | } |
1799 | 0 | #endif |
1800 | |
|
1801 | 0 | pkt.kind = AOM_CODEC_PSNR_PKT; |
1802 | 0 | aom_codec_pkt_list_add(cpi->ppi->output_pkt_list, &pkt); |
1803 | 0 | } |
1804 | | |
1805 | 0 | int av1_use_as_reference(int *ext_ref_frame_flags, int ref_frame_flags) { |
1806 | 0 | if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1; |
1807 | | |
1808 | 0 | *ext_ref_frame_flags = ref_frame_flags; |
1809 | 0 | return 0; |
1810 | 0 | } |
1811 | | |
1812 | 0 | int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) { |
1813 | 0 | AV1_COMMON *const cm = &cpi->common; |
1814 | 0 | const int num_planes = av1_num_planes(cm); |
1815 | 0 | YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx); |
1816 | 0 | if (cfg) { |
1817 | 0 | aom_yv12_copy_frame(cfg, sd, num_planes); |
1818 | 0 | return 0; |
1819 | 0 | } else { |
1820 | 0 | return -1; |
1821 | 0 | } |
1822 | 0 | } |
1823 | | |
1824 | 0 | int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) { |
1825 | 0 | AV1_COMMON *const cm = &cpi->common; |
1826 | 0 | const int num_planes = av1_num_planes(cm); |
1827 | 0 | YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx); |
1828 | 0 | if (cfg) { |
1829 | 0 | aom_yv12_copy_frame(sd, cfg, num_planes); |
1830 | 0 | return 0; |
1831 | 0 | } else { |
1832 | 0 | return -1; |
1833 | 0 | } |
1834 | 0 | } |
1835 | | |
1836 | | #ifdef OUTPUT_YUV_REC |
1837 | | void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) { |
1838 | | uint8_t *src = s->y_buffer; |
1839 | | int h = cm->height; |
1840 | | if (yuv_rec_file == NULL) return; |
1841 | | if (s->flags & YV12_FLAG_HIGHBITDEPTH) { |
1842 | | uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer); |
1843 | | |
1844 | | do { |
1845 | | fwrite(src16, s->y_width, 2, yuv_rec_file); |
1846 | | src16 += s->y_stride; |
1847 | | } while (--h); |
1848 | | |
1849 | | src16 = CONVERT_TO_SHORTPTR(s->u_buffer); |
1850 | | h = s->uv_height; |
1851 | | |
1852 | | do { |
1853 | | fwrite(src16, s->uv_width, 2, yuv_rec_file); |
1854 | | src16 += s->uv_stride; |
1855 | | } while (--h); |
1856 | | |
1857 | | src16 = CONVERT_TO_SHORTPTR(s->v_buffer); |
1858 | | h = s->uv_height; |
1859 | | |
1860 | | do { |
1861 | | fwrite(src16, s->uv_width, 2, yuv_rec_file); |
1862 | | src16 += s->uv_stride; |
1863 | | } while (--h); |
1864 | | |
1865 | | fflush(yuv_rec_file); |
1866 | | return; |
1867 | | } |
1868 | | |
1869 | | do { |
1870 | | fwrite(src, s->y_width, 1, yuv_rec_file); |
1871 | | src += s->y_stride; |
1872 | | } while (--h); |
1873 | | |
1874 | | src = s->u_buffer; |
1875 | | h = s->uv_height; |
1876 | | |
1877 | | do { |
1878 | | fwrite(src, s->uv_width, 1, yuv_rec_file); |
1879 | | src += s->uv_stride; |
1880 | | } while (--h); |
1881 | | |
1882 | | src = s->v_buffer; |
1883 | | h = s->uv_height; |
1884 | | |
1885 | | do { |
1886 | | fwrite(src, s->uv_width, 1, yuv_rec_file); |
1887 | | src += s->uv_stride; |
1888 | | } while (--h); |
1889 | | |
1890 | | fflush(yuv_rec_file); |
1891 | | } |
1892 | | #endif // OUTPUT_YUV_REC |
1893 | | |
1894 | 0 | void av1_set_mv_search_params(AV1_COMP *cpi) { |
1895 | 0 | const AV1_COMMON *const cm = &cpi->common; |
1896 | 0 | MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params; |
1897 | 0 | const int max_mv_def = AOMMAX(cm->width, cm->height); |
1898 | | |
1899 | | // Default based on max resolution. |
1900 | 0 | mv_search_params->mv_step_param = av1_init_search_range(max_mv_def); |
1901 | |
|
1902 | 0 | if (cpi->sf.mv_sf.auto_mv_step_size) { |
1903 | 0 | if (frame_is_intra_only(cm)) { |
1904 | | // Initialize max_mv_magnitude for use in the first INTER frame |
1905 | | // after a key/intra-only frame. |
1906 | 0 | mv_search_params->max_mv_magnitude = max_mv_def; |
1907 | 0 | } else { |
1908 | | // Use adaptive mv steps based on previous frame stats for show frames and |
1909 | | // internal arfs. |
1910 | 0 | FRAME_UPDATE_TYPE cur_update_type = |
1911 | 0 | cpi->ppi->gf_group.update_type[cpi->gf_frame_index]; |
1912 | 0 | int use_auto_mv_step = |
1913 | 0 | (cm->show_frame || cur_update_type == INTNL_ARF_UPDATE) && |
1914 | 0 | mv_search_params->max_mv_magnitude != -1 && |
1915 | 0 | cpi->sf.mv_sf.auto_mv_step_size >= 2; |
1916 | 0 | if (use_auto_mv_step) { |
1917 | | // Allow mv_steps to correspond to twice the max mv magnitude found |
1918 | | // in the previous frame, capped by the default max_mv_magnitude based |
1919 | | // on resolution. |
1920 | 0 | mv_search_params->mv_step_param = av1_init_search_range( |
1921 | 0 | AOMMIN(max_mv_def, 2 * mv_search_params->max_mv_magnitude)); |
1922 | 0 | } |
1923 | | // Reset max_mv_magnitude based on update flag. |
1924 | 0 | if (cpi->do_frame_data_update) mv_search_params->max_mv_magnitude = -1; |
1925 | 0 | } |
1926 | 0 | } |
1927 | 0 | } |
1928 | | |
1929 | 0 | void av1_set_screen_content_options(AV1_COMP *cpi, FeatureFlags *features) { |
1930 | 0 | const AV1_COMMON *const cm = &cpi->common; |
1931 | 0 | const MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
1932 | |
|
1933 | 0 | if (cm->seq_params->force_screen_content_tools != 2) { |
1934 | 0 | features->allow_screen_content_tools = features->allow_intrabc = |
1935 | 0 | cm->seq_params->force_screen_content_tools; |
1936 | 0 | return; |
1937 | 0 | } |
1938 | | |
1939 | 0 | if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_SCREEN) { |
1940 | 0 | features->allow_screen_content_tools = 1; |
1941 | 0 | features->allow_intrabc = cpi->oxcf.mode == REALTIME ? 0 : 1; |
1942 | 0 | cpi->is_screen_content_type = 1; |
1943 | 0 | cpi->use_screen_content_tools = 1; |
1944 | 0 | return; |
1945 | 0 | } |
1946 | | |
1947 | 0 | if (cpi->oxcf.mode == REALTIME) { |
1948 | 0 | features->allow_screen_content_tools = features->allow_intrabc = 0; |
1949 | 0 | return; |
1950 | 0 | } |
1951 | | |
1952 | | // Screen content tools are not evaluated in non-RD encoding mode unless |
1953 | | // content type is not set explicitly, i.e., when |
1954 | | // cpi->oxcf.tune_cfg.content != AOM_CONTENT_SCREEN, use_nonrd_pick_mode = 1 |
1955 | | // and hybrid_intra_pickmode = 0. Hence, screen content detection is |
1956 | | // disabled. |
1957 | 0 | if (cpi->sf.rt_sf.use_nonrd_pick_mode && |
1958 | 0 | !cpi->sf.rt_sf.hybrid_intra_pickmode) { |
1959 | 0 | features->allow_screen_content_tools = features->allow_intrabc = 0; |
1960 | 0 | return; |
1961 | 0 | } |
1962 | | |
1963 | | // Estimate if the source frame is screen content, based on the portion of |
1964 | | // blocks that have few luma colors. |
1965 | 0 | const uint8_t *src = cpi->unfiltered_source->y_buffer; |
1966 | 0 | assert(src != NULL); |
1967 | 0 | const int use_hbd = cpi->unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH; |
1968 | 0 | const int stride = cpi->unfiltered_source->y_stride; |
1969 | 0 | const int width = cpi->unfiltered_source->y_width; |
1970 | 0 | const int height = cpi->unfiltered_source->y_height; |
1971 | 0 | const int64_t area = (int64_t)width * height; |
1972 | 0 | const int bd = cm->seq_params->bit_depth; |
1973 | 0 | const int blk_w = 16; |
1974 | 0 | const int blk_h = 16; |
1975 | | // These threshold values are selected experimentally. |
1976 | 0 | const int color_thresh = 4; |
1977 | 0 | const unsigned int var_thresh = 0; |
1978 | | // Counts of blocks with no more than color_thresh colors. |
1979 | 0 | int64_t counts_1 = 0; |
1980 | | // Counts of blocks with no more than color_thresh colors and variance larger |
1981 | | // than var_thresh. |
1982 | 0 | int64_t counts_2 = 0; |
1983 | |
|
1984 | 0 | for (int r = 0; r + blk_h <= height; r += blk_h) { |
1985 | 0 | for (int c = 0; c + blk_w <= width; c += blk_w) { |
1986 | 0 | int count_buf[1 << 8]; // Maximum (1 << 8) bins for hbd path. |
1987 | 0 | const uint8_t *const this_src = src + r * stride + c; |
1988 | 0 | int n_colors; |
1989 | 0 | if (use_hbd) |
1990 | 0 | av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd, NULL, |
1991 | 0 | count_buf, &n_colors, NULL); |
1992 | 0 | else |
1993 | 0 | av1_count_colors(this_src, stride, blk_w, blk_h, count_buf, &n_colors); |
1994 | 0 | if (n_colors > 1 && n_colors <= color_thresh) { |
1995 | 0 | ++counts_1; |
1996 | 0 | struct buf_2d buf; |
1997 | 0 | buf.stride = stride; |
1998 | 0 | buf.buf = (uint8_t *)this_src; |
1999 | 0 | const unsigned int var = av1_get_perpixel_variance( |
2000 | 0 | cpi, xd, &buf, BLOCK_16X16, AOM_PLANE_Y, use_hbd); |
2001 | 0 | if (var > var_thresh) ++counts_2; |
2002 | 0 | } |
2003 | 0 | } |
2004 | 0 | } |
2005 | | |
2006 | | // The threshold values are selected experimentally. |
2007 | 0 | features->allow_screen_content_tools = counts_1 * blk_h * blk_w * 10 > area; |
2008 | | // IntraBC would force loop filters off, so we use more strict rules that also |
2009 | | // requires that the block has high variance. |
2010 | 0 | features->allow_intrabc = features->allow_screen_content_tools && |
2011 | 0 | counts_2 * blk_h * blk_w * 12 > area; |
2012 | 0 | cpi->use_screen_content_tools = features->allow_screen_content_tools; |
2013 | 0 | cpi->is_screen_content_type = |
2014 | 0 | features->allow_intrabc || (counts_1 * blk_h * blk_w * 10 > area * 4 && |
2015 | 0 | counts_2 * blk_h * blk_w * 30 > area); |
2016 | 0 | } |
2017 | | |
2018 | 0 | static void init_motion_estimation(AV1_COMP *cpi) { |
2019 | 0 | AV1_COMMON *const cm = &cpi->common; |
2020 | 0 | MotionVectorSearchParams *const mv_search_params = &cpi->mv_search_params; |
2021 | 0 | const int aligned_width = (cm->width + 7) & ~7; |
2022 | 0 | const int y_stride = |
2023 | 0 | aom_calc_y_stride(aligned_width, cpi->oxcf.border_in_pixels); |
2024 | 0 | const int y_stride_src = ((cpi->oxcf.frm_dim_cfg.width != cm->width || |
2025 | 0 | cpi->oxcf.frm_dim_cfg.height != cm->height) || |
2026 | 0 | av1_superres_scaled(cm)) |
2027 | 0 | ? y_stride |
2028 | 0 | : cpi->ppi->lookahead->buf->img.y_stride; |
2029 | 0 | int fpf_y_stride = |
2030 | 0 | cm->cur_frame != NULL ? cm->cur_frame->buf.y_stride : y_stride; |
2031 | | |
2032 | | // Update if search_site_cfg is uninitialized or the current frame has a new |
2033 | | // stride |
2034 | 0 | const int should_update = |
2035 | 0 | !mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride || |
2036 | 0 | !mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][DIAMOND].stride || |
2037 | 0 | (y_stride != |
2038 | 0 | mv_search_params->search_site_cfg[SS_CFG_SRC][DIAMOND].stride); |
2039 | |
|
2040 | 0 | if (!should_update) { |
2041 | 0 | return; |
2042 | 0 | } |
2043 | | |
2044 | | // Initialization of search_site_cfg for NUM_DISTINCT_SEARCH_METHODS. |
2045 | 0 | for (SEARCH_METHODS i = DIAMOND; i < NUM_DISTINCT_SEARCH_METHODS; i++) { |
2046 | 0 | const int level = ((i == NSTEP_8PT) || (i == CLAMPED_DIAMOND)) ? 1 : 0; |
2047 | 0 | av1_init_motion_compensation[i]( |
2048 | 0 | &mv_search_params->search_site_cfg[SS_CFG_SRC][i], y_stride, level); |
2049 | 0 | av1_init_motion_compensation[i]( |
2050 | 0 | &mv_search_params->search_site_cfg[SS_CFG_LOOKAHEAD][i], y_stride_src, |
2051 | 0 | level); |
2052 | 0 | } |
2053 | | |
2054 | | // First pass search site config initialization. |
2055 | 0 | av1_init_motion_fpf(&mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND], |
2056 | 0 | fpf_y_stride); |
2057 | 0 | for (SEARCH_METHODS i = NSTEP; i < NUM_DISTINCT_SEARCH_METHODS; i++) { |
2058 | 0 | memcpy(&mv_search_params->search_site_cfg[SS_CFG_FPF][i], |
2059 | 0 | &mv_search_params->search_site_cfg[SS_CFG_FPF][DIAMOND], |
2060 | 0 | sizeof(search_site_config)); |
2061 | 0 | } |
2062 | 0 | } |
2063 | | |
2064 | 0 | static void init_ref_frame_bufs(AV1_COMP *cpi) { |
2065 | 0 | AV1_COMMON *const cm = &cpi->common; |
2066 | 0 | int i; |
2067 | 0 | if (cm->cur_frame) { |
2068 | 0 | cm->cur_frame->ref_count--; |
2069 | 0 | cm->cur_frame = NULL; |
2070 | 0 | } |
2071 | 0 | for (i = 0; i < REF_FRAMES; ++i) { |
2072 | 0 | if (cm->ref_frame_map[i]) { |
2073 | 0 | cm->ref_frame_map[i]->ref_count--; |
2074 | 0 | cm->ref_frame_map[i] = NULL; |
2075 | 0 | } |
2076 | 0 | } |
2077 | | #ifndef NDEBUG |
2078 | | BufferPool *const pool = cm->buffer_pool; |
2079 | | for (i = 0; i < pool->num_frame_bufs; ++i) { |
2080 | | assert(pool->frame_bufs[i].ref_count == 0); |
2081 | | } |
2082 | | #endif |
2083 | 0 | } |
2084 | | |
2085 | | // TODO(chengchen): consider renaming this function as it is necessary |
2086 | | // for the encoder to setup critical parameters, and it does not |
2087 | | // deal with initial width any longer. |
2088 | | aom_codec_err_t av1_check_initial_width(AV1_COMP *cpi, int use_highbitdepth, |
2089 | 0 | int subsampling_x, int subsampling_y) { |
2090 | 0 | AV1_COMMON *const cm = &cpi->common; |
2091 | 0 | SequenceHeader *const seq_params = cm->seq_params; |
2092 | |
|
2093 | 0 | if (!cpi->frame_size_related_setup_done || |
2094 | 0 | seq_params->use_highbitdepth != use_highbitdepth || |
2095 | 0 | seq_params->subsampling_x != subsampling_x || |
2096 | 0 | seq_params->subsampling_y != subsampling_y) { |
2097 | 0 | seq_params->subsampling_x = subsampling_x; |
2098 | 0 | seq_params->subsampling_y = subsampling_y; |
2099 | 0 | seq_params->use_highbitdepth = use_highbitdepth; |
2100 | |
|
2101 | 0 | av1_set_speed_features_framesize_independent(cpi, cpi->oxcf.speed); |
2102 | 0 | av1_set_speed_features_framesize_dependent(cpi, cpi->oxcf.speed); |
2103 | |
|
2104 | 0 | if (!is_stat_generation_stage(cpi)) { |
2105 | 0 | #if !CONFIG_REALTIME_ONLY |
2106 | 0 | if (!av1_tf_info_alloc(&cpi->ppi->tf_info, cpi)) |
2107 | 0 | return AOM_CODEC_MEM_ERROR; |
2108 | 0 | #endif // !CONFIG_REALTIME_ONLY |
2109 | 0 | } |
2110 | 0 | init_ref_frame_bufs(cpi); |
2111 | |
|
2112 | 0 | init_motion_estimation(cpi); // TODO(agrange) This can be removed. |
2113 | |
|
2114 | 0 | cpi->initial_mbs = cm->mi_params.MBs; |
2115 | 0 | cpi->frame_size_related_setup_done = true; |
2116 | 0 | } |
2117 | 0 | return AOM_CODEC_OK; |
2118 | 0 | } |
2119 | | |
2120 | | #if CONFIG_AV1_TEMPORAL_DENOISING |
2121 | | static void setup_denoiser_buffer(AV1_COMP *cpi) { |
2122 | | AV1_COMMON *const cm = &cpi->common; |
2123 | | if (cpi->oxcf.noise_sensitivity > 0 && |
2124 | | !cpi->denoiser.frame_buffer_initialized) { |
2125 | | if (av1_denoiser_alloc( |
2126 | | cm, &cpi->svc, &cpi->denoiser, cpi->ppi->use_svc, |
2127 | | cpi->oxcf.noise_sensitivity, cm->width, cm->height, |
2128 | | cm->seq_params->subsampling_x, cm->seq_params->subsampling_y, |
2129 | | cm->seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS)) |
2130 | | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
2131 | | "Failed to allocate denoiser"); |
2132 | | } |
2133 | | } |
2134 | | #endif |
2135 | | |
2136 | | // Returns 1 if the assigned width or height was <= 0. |
2137 | 0 | static int set_size_literal(AV1_COMP *cpi, int width, int height) { |
2138 | 0 | AV1_COMMON *cm = &cpi->common; |
2139 | 0 | aom_codec_err_t err = av1_check_initial_width( |
2140 | 0 | cpi, cm->seq_params->use_highbitdepth, cm->seq_params->subsampling_x, |
2141 | 0 | cm->seq_params->subsampling_y); |
2142 | 0 | if (err != AOM_CODEC_OK) { |
2143 | 0 | aom_internal_error(cm->error, err, "av1_check_initial_width() failed"); |
2144 | 0 | } |
2145 | |
|
2146 | 0 | if (width <= 0 || height <= 0) return 1; |
2147 | | |
2148 | 0 | cm->width = width; |
2149 | 0 | cm->height = height; |
2150 | |
|
2151 | | #if CONFIG_AV1_TEMPORAL_DENOISING |
2152 | | setup_denoiser_buffer(cpi); |
2153 | | #endif |
2154 | |
|
2155 | 0 | if (cm->width > cpi->data_alloc_width || |
2156 | 0 | cm->height > cpi->data_alloc_height) { |
2157 | 0 | av1_free_context_buffers(cm); |
2158 | 0 | av1_free_shared_coeff_buffer(&cpi->td.shared_coeff_buf); |
2159 | 0 | av1_free_sms_tree(&cpi->td); |
2160 | 0 | av1_free_pmc(cpi->td.firstpass_ctx, av1_num_planes(cm)); |
2161 | 0 | cpi->td.firstpass_ctx = NULL; |
2162 | 0 | alloc_compressor_data(cpi); |
2163 | 0 | realloc_segmentation_maps(cpi); |
2164 | 0 | cpi->data_alloc_width = cm->width; |
2165 | 0 | cpi->data_alloc_height = cm->height; |
2166 | 0 | cpi->frame_size_related_setup_done = false; |
2167 | 0 | } |
2168 | 0 | alloc_mb_mode_info_buffers(cpi); |
2169 | 0 | av1_update_frame_size(cpi); |
2170 | |
|
2171 | 0 | return 0; |
2172 | 0 | } |
2173 | | |
2174 | 0 | void av1_set_frame_size(AV1_COMP *cpi, int width, int height) { |
2175 | 0 | AV1_COMMON *const cm = &cpi->common; |
2176 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
2177 | 0 | const int num_planes = av1_num_planes(cm); |
2178 | 0 | MACROBLOCKD *const xd = &cpi->td.mb.e_mbd; |
2179 | 0 | int ref_frame; |
2180 | |
|
2181 | 0 | if (width != cm->width || height != cm->height) { |
2182 | | // There has been a change in the encoded frame size |
2183 | 0 | set_size_literal(cpi, width, height); |
2184 | | // Recalculate 'all_lossless' in case super-resolution was (un)selected. |
2185 | 0 | cm->features.all_lossless = |
2186 | 0 | cm->features.coded_lossless && !av1_superres_scaled(cm); |
2187 | |
|
2188 | 0 | av1_noise_estimate_init(&cpi->noise_estimate, cm->width, cm->height); |
2189 | | #if CONFIG_AV1_TEMPORAL_DENOISING |
2190 | | // Reset the denoiser on the resized frame. |
2191 | | if (cpi->oxcf.noise_sensitivity > 0) { |
2192 | | av1_denoiser_free(&(cpi->denoiser)); |
2193 | | setup_denoiser_buffer(cpi); |
2194 | | } |
2195 | | #endif |
2196 | 0 | } |
2197 | 0 | if (is_stat_consumption_stage(cpi)) { |
2198 | 0 | av1_set_target_rate(cpi, cm->width, cm->height); |
2199 | 0 | } |
2200 | |
|
2201 | 0 | alloc_frame_mvs(cm, cm->cur_frame); |
2202 | | |
2203 | | // Allocate above context buffers |
2204 | 0 | CommonContexts *const above_contexts = &cm->above_contexts; |
2205 | 0 | if (above_contexts->num_planes < av1_num_planes(cm) || |
2206 | 0 | above_contexts->num_mi_cols < cm->mi_params.mi_cols || |
2207 | 0 | above_contexts->num_tile_rows < cm->tiles.rows) { |
2208 | 0 | av1_free_above_context_buffers(above_contexts); |
2209 | 0 | if (av1_alloc_above_context_buffers(above_contexts, cm->tiles.rows, |
2210 | 0 | cm->mi_params.mi_cols, |
2211 | 0 | av1_num_planes(cm))) |
2212 | 0 | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
2213 | 0 | "Failed to allocate context buffers"); |
2214 | 0 | } |
2215 | |
|
2216 | 0 | AV1EncoderConfig *oxcf = &cpi->oxcf; |
2217 | 0 | oxcf->border_in_pixels = av1_get_enc_border_size( |
2218 | 0 | av1_is_resize_needed(oxcf), oxcf->kf_cfg.key_freq_max == 0, |
2219 | 0 | cm->seq_params->sb_size); |
2220 | | |
2221 | | // Reset the frame pointers to the current frame size. |
2222 | 0 | if (aom_realloc_frame_buffer( |
2223 | 0 | &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x, |
2224 | 0 | seq_params->subsampling_y, seq_params->use_highbitdepth, |
2225 | 0 | cpi->oxcf.border_in_pixels, cm->features.byte_alignment, NULL, NULL, |
2226 | 0 | NULL, cpi->alloc_pyramid, 0)) |
2227 | 0 | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
2228 | 0 | "Failed to allocate frame buffer"); |
2229 | |
|
2230 | 0 | if (!is_stat_generation_stage(cpi)) av1_init_cdef_worker(cpi); |
2231 | |
|
2232 | 0 | #if !CONFIG_REALTIME_ONLY |
2233 | 0 | if (is_restoration_used(cm)) { |
2234 | 0 | for (int i = 0; i < num_planes; ++i) |
2235 | 0 | cm->rst_info[i].frame_restoration_type = RESTORE_NONE; |
2236 | |
|
2237 | 0 | const bool is_sgr_enabled = !cpi->sf.lpf_sf.disable_sgr_filter; |
2238 | 0 | av1_alloc_restoration_buffers(cm, is_sgr_enabled); |
2239 | | // Store the allocated restoration buffers in MT object. |
2240 | 0 | if (cpi->ppi->p_mt_info.num_workers > 1) { |
2241 | 0 | av1_init_lr_mt_buffers(cpi); |
2242 | 0 | } |
2243 | 0 | } |
2244 | 0 | #endif |
2245 | |
|
2246 | 0 | init_motion_estimation(cpi); |
2247 | |
|
2248 | 0 | int has_valid_ref_frame = 0; |
2249 | 0 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
2250 | 0 | RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame); |
2251 | 0 | if (buf != NULL) { |
2252 | 0 | struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame); |
2253 | 0 | av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width, |
2254 | 0 | buf->buf.y_crop_height, cm->width, |
2255 | 0 | cm->height); |
2256 | 0 | has_valid_ref_frame |= av1_is_valid_scale(sf); |
2257 | 0 | if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes); |
2258 | 0 | } |
2259 | 0 | } |
2260 | 0 | if (!frame_is_intra_only(cm) && !has_valid_ref_frame) { |
2261 | 0 | aom_internal_error( |
2262 | 0 | cm->error, AOM_CODEC_CORRUPT_FRAME, |
2263 | 0 | "Can't find at least one reference frame with valid size"); |
2264 | 0 | } |
2265 | |
|
2266 | 0 | av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height, |
2267 | 0 | cm->width, cm->height); |
2268 | |
|
2269 | 0 | set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME); |
2270 | 0 | } |
2271 | | |
2272 | | static INLINE int extend_borders_mt(const AV1_COMP *cpi, |
2273 | 0 | MULTI_THREADED_MODULES stage, int plane) { |
2274 | 0 | const AV1_COMMON *const cm = &cpi->common; |
2275 | 0 | if (cpi->mt_info.num_mod_workers[stage] < 2) return 0; |
2276 | 0 | switch (stage) { |
2277 | | // TODO(deepa.kg@ittiam.com): When cdef and loop-restoration are disabled, |
2278 | | // multi-thread frame border extension along with loop filter frame. |
2279 | | // As loop-filtering of a superblock row modifies the pixels of the |
2280 | | // above superblock row, border extension requires that loop filtering |
2281 | | // of the current and above superblock row is complete. |
2282 | 0 | case MOD_LPF: return 0; |
2283 | 0 | case MOD_CDEF: |
2284 | 0 | return is_cdef_used(cm) && !cpi->ppi->rtc_ref.non_reference_frame && |
2285 | 0 | !is_restoration_used(cm) && !av1_superres_scaled(cm); |
2286 | 0 | case MOD_LR: |
2287 | 0 | return is_restoration_used(cm) && |
2288 | 0 | (cm->rst_info[plane].frame_restoration_type != RESTORE_NONE); |
2289 | 0 | default: assert(0); |
2290 | 0 | } |
2291 | 0 | return 0; |
2292 | 0 | } |
2293 | | |
2294 | | /*!\brief Select and apply cdef filters and switchable restoration filters |
2295 | | * |
2296 | | * \ingroup high_level_algo |
2297 | | */ |
2298 | | static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm, |
2299 | | MACROBLOCKD *xd, int use_restoration, |
2300 | | int use_cdef, |
2301 | 0 | unsigned int skip_apply_postproc_filters) { |
2302 | 0 | #if !CONFIG_REALTIME_ONLY |
2303 | 0 | if (use_restoration) |
2304 | 0 | av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0); |
2305 | | #else |
2306 | | (void)use_restoration; |
2307 | | #endif |
2308 | |
|
2309 | 0 | if (use_cdef) { |
2310 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2311 | | start_timing(cpi, cdef_time); |
2312 | | #endif |
2313 | 0 | const int num_workers = cpi->mt_info.num_mod_workers[MOD_CDEF]; |
2314 | | // Find CDEF parameters |
2315 | 0 | av1_cdef_search(cpi); |
2316 | | |
2317 | | // Apply the filter |
2318 | 0 | if ((skip_apply_postproc_filters & SKIP_APPLY_CDEF) == 0) { |
2319 | 0 | assert(!cpi->ppi->rtc_ref.non_reference_frame); |
2320 | 0 | if (num_workers > 1) { |
2321 | | // Extension of frame borders is multi-threaded along with cdef. |
2322 | 0 | const int do_extend_border = |
2323 | 0 | extend_borders_mt(cpi, MOD_CDEF, /* plane */ 0); |
2324 | 0 | av1_cdef_frame_mt(cm, xd, cpi->mt_info.cdef_worker, |
2325 | 0 | cpi->mt_info.workers, &cpi->mt_info.cdef_sync, |
2326 | 0 | num_workers, av1_cdef_init_fb_row_mt, |
2327 | 0 | do_extend_border); |
2328 | 0 | } else { |
2329 | 0 | av1_cdef_frame(&cm->cur_frame->buf, cm, xd, av1_cdef_init_fb_row); |
2330 | 0 | } |
2331 | 0 | } |
2332 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2333 | | end_timing(cpi, cdef_time); |
2334 | | #endif |
2335 | 0 | } |
2336 | |
|
2337 | 0 | const int use_superres = av1_superres_scaled(cm); |
2338 | 0 | if (use_superres) { |
2339 | 0 | if ((skip_apply_postproc_filters & SKIP_APPLY_SUPERRES) == 0) { |
2340 | 0 | av1_superres_post_encode(cpi); |
2341 | 0 | } |
2342 | 0 | } |
2343 | |
|
2344 | 0 | #if !CONFIG_REALTIME_ONLY |
2345 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2346 | | start_timing(cpi, loop_restoration_time); |
2347 | | #endif |
2348 | 0 | if (use_restoration) { |
2349 | 0 | MultiThreadInfo *const mt_info = &cpi->mt_info; |
2350 | 0 | const int num_workers = mt_info->num_mod_workers[MOD_LR]; |
2351 | 0 | av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1); |
2352 | 0 | av1_pick_filter_restoration(cpi->source, cpi); |
2353 | 0 | if ((skip_apply_postproc_filters & SKIP_APPLY_RESTORATION) == 0 && |
2354 | 0 | (cm->rst_info[0].frame_restoration_type != RESTORE_NONE || |
2355 | 0 | cm->rst_info[1].frame_restoration_type != RESTORE_NONE || |
2356 | 0 | cm->rst_info[2].frame_restoration_type != RESTORE_NONE)) { |
2357 | 0 | if (num_workers > 1) { |
2358 | | // Extension of frame borders is multi-threaded along with loop |
2359 | | // restoration filter. |
2360 | 0 | const int do_extend_border = 1; |
2361 | 0 | av1_loop_restoration_filter_frame_mt( |
2362 | 0 | &cm->cur_frame->buf, cm, 0, mt_info->workers, num_workers, |
2363 | 0 | &mt_info->lr_row_sync, &cpi->lr_ctxt, do_extend_border); |
2364 | 0 | } else { |
2365 | 0 | av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0, |
2366 | 0 | &cpi->lr_ctxt); |
2367 | 0 | } |
2368 | 0 | } |
2369 | 0 | } |
2370 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2371 | | end_timing(cpi, loop_restoration_time); |
2372 | | #endif |
2373 | 0 | #endif // !CONFIG_REALTIME_ONLY |
2374 | 0 | } |
2375 | | |
2376 | 0 | static void extend_frame_borders(AV1_COMP *cpi) { |
2377 | 0 | const AV1_COMMON *const cm = &cpi->common; |
2378 | | // TODO(debargha): Fix mv search range on encoder side |
2379 | 0 | for (int plane = 0; plane < av1_num_planes(cm); ++plane) { |
2380 | 0 | const bool extend_border_done = extend_borders_mt(cpi, MOD_CDEF, plane) || |
2381 | 0 | extend_borders_mt(cpi, MOD_LR, plane); |
2382 | 0 | if (!extend_border_done) { |
2383 | 0 | const YV12_BUFFER_CONFIG *const ybf = &cm->cur_frame->buf; |
2384 | 0 | aom_extend_frame_borders_plane_row(ybf, plane, 0, |
2385 | 0 | ybf->crop_heights[plane > 0]); |
2386 | 0 | } |
2387 | 0 | } |
2388 | 0 | } |
2389 | | |
2390 | | /*!\brief Select and apply deblocking filters, cdef filters, and restoration |
2391 | | * filters. |
2392 | | * |
2393 | | * \ingroup high_level_algo |
2394 | | */ |
2395 | 0 | static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) { |
2396 | 0 | MultiThreadInfo *const mt_info = &cpi->mt_info; |
2397 | 0 | const int num_workers = mt_info->num_mod_workers[MOD_LPF]; |
2398 | 0 | const int num_planes = av1_num_planes(cm); |
2399 | 0 | MACROBLOCKD *xd = &cpi->td.mb.e_mbd; |
2400 | 0 | cpi->td.mb.rdmult = cpi->rd.RDMULT; |
2401 | |
|
2402 | 0 | assert(IMPLIES(is_lossless_requested(&cpi->oxcf.rc_cfg), |
2403 | 0 | cm->features.coded_lossless && cm->features.all_lossless)); |
2404 | |
|
2405 | 0 | const int use_loopfilter = |
2406 | 0 | is_loopfilter_used(cm) && !cpi->mt_info.pipeline_lpf_mt_with_enc; |
2407 | 0 | const int use_cdef = |
2408 | 0 | is_cdef_used(cm) && (!cpi->active_map.enabled || |
2409 | 0 | cpi->rc.percent_blocks_inactive <= |
2410 | 0 | cpi->sf.rt_sf.thresh_active_maps_skip_lf_cdef); |
2411 | 0 | const int use_superres = av1_superres_scaled(cm); |
2412 | 0 | const int use_restoration = is_restoration_used(cm); |
2413 | |
|
2414 | 0 | const unsigned int skip_apply_postproc_filters = |
2415 | 0 | derive_skip_apply_postproc_filters(cpi, use_loopfilter, use_cdef, |
2416 | 0 | use_superres, use_restoration); |
2417 | |
|
2418 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2419 | | start_timing(cpi, loop_filter_time); |
2420 | | #endif |
2421 | 0 | if (use_loopfilter) { |
2422 | 0 | av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_sf.lpf_pick); |
2423 | 0 | struct loopfilter *lf = &cm->lf; |
2424 | 0 | if ((lf->filter_level[0] || lf->filter_level[1]) && |
2425 | 0 | (skip_apply_postproc_filters & SKIP_APPLY_LOOPFILTER) == 0) { |
2426 | 0 | assert(!cpi->ppi->rtc_ref.non_reference_frame); |
2427 | | // lpf_opt_level = 1 : Enables dual/quad loop-filtering. |
2428 | | // lpf_opt_level is set to 1 if transform size search depth in inter |
2429 | | // blocks is limited to one as quad loop filtering assumes that all the |
2430 | | // transform blocks within a 16x8/8x16/16x16 prediction block are of the |
2431 | | // same size. lpf_opt_level = 2 : Filters both chroma planes together, in |
2432 | | // addition to enabling dual/quad loop-filtering. This is enabled when lpf |
2433 | | // pick method is LPF_PICK_FROM_Q as u and v plane filter levels are |
2434 | | // equal. |
2435 | 0 | int lpf_opt_level = get_lpf_opt_level(&cpi->sf); |
2436 | 0 | av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0, |
2437 | 0 | mt_info->workers, num_workers, |
2438 | 0 | &mt_info->lf_row_sync, lpf_opt_level); |
2439 | 0 | } |
2440 | 0 | } |
2441 | |
|
2442 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2443 | | end_timing(cpi, loop_filter_time); |
2444 | | #endif |
2445 | |
|
2446 | 0 | cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef, |
2447 | 0 | skip_apply_postproc_filters); |
2448 | 0 | } |
2449 | | |
2450 | 0 | static void update_motion_stat(AV1_COMP *const cpi) { |
2451 | 0 | AV1_COMMON *const cm = &cpi->common; |
2452 | 0 | const CommonModeInfoParams *const mi_params = &cm->mi_params; |
2453 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2454 | 0 | SVC *const svc = &cpi->svc; |
2455 | 0 | const int avg_cnt_zeromv = |
2456 | 0 | 100 * cpi->rc.cnt_zeromv / (mi_params->mi_rows * mi_params->mi_cols); |
2457 | 0 | if (!cpi->ppi->use_svc || |
2458 | 0 | (cpi->ppi->use_svc && |
2459 | 0 | !cpi->svc.layer_context[cpi->svc.temporal_layer_id].is_key_frame && |
2460 | 0 | cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1)) { |
2461 | 0 | rc->avg_frame_low_motion = |
2462 | 0 | (rc->avg_frame_low_motion == 0) |
2463 | 0 | ? avg_cnt_zeromv |
2464 | 0 | : (3 * rc->avg_frame_low_motion + avg_cnt_zeromv) / 4; |
2465 | | // For SVC: set avg_frame_low_motion (only computed on top spatial layer) |
2466 | | // to all lower spatial layers. |
2467 | 0 | if (cpi->ppi->use_svc && |
2468 | 0 | svc->spatial_layer_id == svc->number_spatial_layers - 1) { |
2469 | 0 | for (int i = 0; i < svc->number_spatial_layers - 1; ++i) { |
2470 | 0 | const int layer = LAYER_IDS_TO_IDX(i, svc->temporal_layer_id, |
2471 | 0 | svc->number_temporal_layers); |
2472 | 0 | LAYER_CONTEXT *const lc = &svc->layer_context[layer]; |
2473 | 0 | RATE_CONTROL *const lrc = &lc->rc; |
2474 | 0 | lrc->avg_frame_low_motion = rc->avg_frame_low_motion; |
2475 | 0 | } |
2476 | 0 | } |
2477 | 0 | } |
2478 | 0 | } |
2479 | | |
2480 | | /*!\brief Encode a frame without the recode loop, usually used in one-pass |
2481 | | * encoding and realtime coding. |
2482 | | * |
2483 | | * \ingroup high_level_algo |
2484 | | * |
2485 | | * \param[in] cpi Top-level encoder structure |
2486 | | * |
2487 | | * \return Returns a value to indicate if the encoding is done successfully. |
2488 | | * \retval #AOM_CODEC_OK |
2489 | | * \retval #AOM_CODEC_ERROR |
2490 | | */ |
2491 | 0 | static int encode_without_recode(AV1_COMP *cpi) { |
2492 | 0 | AV1_COMMON *const cm = &cpi->common; |
2493 | 0 | const QuantizationCfg *const q_cfg = &cpi->oxcf.q_cfg; |
2494 | 0 | SVC *const svc = &cpi->svc; |
2495 | 0 | const int resize_pending = is_frame_resize_pending(cpi); |
2496 | 0 | int top_index = 0, bottom_index = 0, q = 0; |
2497 | 0 | YV12_BUFFER_CONFIG *unscaled = cpi->unscaled_source; |
2498 | 0 | InterpFilter filter_scaler = |
2499 | 0 | cpi->ppi->use_svc ? svc->downsample_filter_type[svc->spatial_layer_id] |
2500 | 0 | : EIGHTTAP_SMOOTH; |
2501 | 0 | int phase_scaler = cpi->ppi->use_svc |
2502 | 0 | ? svc->downsample_filter_phase[svc->spatial_layer_id] |
2503 | 0 | : 0; |
2504 | |
|
2505 | 0 | if (cpi->rc.postencode_drop && allow_postencode_drop_rtc(cpi)) |
2506 | 0 | av1_save_all_coding_context(cpi); |
2507 | |
|
2508 | 0 | set_size_independent_vars(cpi); |
2509 | 0 | av1_setup_frame_size(cpi); |
2510 | 0 | cm->prev_frame = get_primary_ref_frame_buf(cm); |
2511 | 0 | av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index); |
2512 | 0 | av1_set_mv_search_params(cpi); |
2513 | |
|
2514 | 0 | if (cm->current_frame.frame_number == 0 && |
2515 | 0 | (cpi->ppi->use_svc || cpi->oxcf.rc_cfg.drop_frames_water_mark > 0) && |
2516 | 0 | cpi->svc.temporal_layer_id == 0) { |
2517 | 0 | const SequenceHeader *seq_params = cm->seq_params; |
2518 | 0 | if (aom_alloc_frame_buffer( |
2519 | 0 | &cpi->svc.source_last_TL0, cpi->oxcf.frm_dim_cfg.width, |
2520 | 0 | cpi->oxcf.frm_dim_cfg.height, seq_params->subsampling_x, |
2521 | 0 | seq_params->subsampling_y, seq_params->use_highbitdepth, |
2522 | 0 | cpi->oxcf.border_in_pixels, cm->features.byte_alignment, false, |
2523 | 0 | 0)) { |
2524 | 0 | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
2525 | 0 | "Failed to allocate buffer for source_last_TL0"); |
2526 | 0 | } |
2527 | 0 | } |
2528 | |
|
2529 | 0 | if (!cpi->ppi->use_svc) { |
2530 | 0 | phase_scaler = 8; |
2531 | | // 2:1 scaling. |
2532 | 0 | if ((cm->width << 1) == unscaled->y_crop_width && |
2533 | 0 | (cm->height << 1) == unscaled->y_crop_height) { |
2534 | 0 | filter_scaler = BILINEAR; |
2535 | | // For lower resolutions use eighttap_smooth. |
2536 | 0 | if (cm->width * cm->height <= 320 * 180) filter_scaler = EIGHTTAP_SMOOTH; |
2537 | 0 | } else if ((cm->width << 2) == unscaled->y_crop_width && |
2538 | 0 | (cm->height << 2) == unscaled->y_crop_height) { |
2539 | | // 4:1 scaling. |
2540 | 0 | filter_scaler = EIGHTTAP_SMOOTH; |
2541 | 0 | } else if ((cm->width << 2) == 3 * unscaled->y_crop_width && |
2542 | 0 | (cm->height << 2) == 3 * unscaled->y_crop_height) { |
2543 | | // 4:3 scaling. |
2544 | 0 | filter_scaler = EIGHTTAP_REGULAR; |
2545 | 0 | } |
2546 | 0 | } |
2547 | |
|
2548 | 0 | allocate_gradient_info_for_hog(cpi); |
2549 | |
|
2550 | 0 | allocate_src_var_of_4x4_sub_block_buf(cpi); |
2551 | |
|
2552 | 0 | const SPEED_FEATURES *sf = &cpi->sf; |
2553 | 0 | if (sf->part_sf.partition_search_type == VAR_BASED_PARTITION) |
2554 | 0 | variance_partition_alloc(cpi); |
2555 | |
|
2556 | 0 | if (cm->current_frame.frame_type == KEY_FRAME || |
2557 | 0 | ((sf->inter_sf.extra_prune_warped && cpi->refresh_frame.golden_frame))) |
2558 | 0 | copy_frame_prob_info(cpi); |
2559 | |
|
2560 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2561 | | printf("\n Encoding a frame: \n"); |
2562 | | #endif |
2563 | |
|
2564 | | #if CONFIG_TUNE_BUTTERAUGLI |
2565 | | if (cpi->oxcf.tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) { |
2566 | | av1_setup_butteraugli_rdmult(cpi); |
2567 | | } |
2568 | | #endif |
2569 | |
|
2570 | 0 | cpi->source = av1_realloc_and_scale_if_required( |
2571 | 0 | cm, unscaled, &cpi->scaled_source, filter_scaler, phase_scaler, true, |
2572 | 0 | false, cpi->oxcf.border_in_pixels, cpi->alloc_pyramid); |
2573 | 0 | if (frame_is_intra_only(cm) || resize_pending != 0) { |
2574 | 0 | const int current_size = |
2575 | 0 | (cm->mi_params.mi_rows * cm->mi_params.mi_cols) >> 2; |
2576 | 0 | if (cpi->consec_zero_mv && |
2577 | 0 | (cpi->consec_zero_mv_alloc_size < current_size)) { |
2578 | 0 | aom_free(cpi->consec_zero_mv); |
2579 | 0 | cpi->consec_zero_mv_alloc_size = 0; |
2580 | 0 | CHECK_MEM_ERROR(cm, cpi->consec_zero_mv, |
2581 | 0 | aom_malloc(current_size * sizeof(*cpi->consec_zero_mv))); |
2582 | 0 | cpi->consec_zero_mv_alloc_size = current_size; |
2583 | 0 | } |
2584 | 0 | assert(cpi->consec_zero_mv != NULL); |
2585 | 0 | memset(cpi->consec_zero_mv, 0, current_size * sizeof(*cpi->consec_zero_mv)); |
2586 | 0 | } |
2587 | |
|
2588 | 0 | if (cpi->scaled_last_source_available) { |
2589 | 0 | cpi->last_source = &cpi->scaled_last_source; |
2590 | 0 | cpi->scaled_last_source_available = 0; |
2591 | 0 | } else if (cpi->unscaled_last_source != NULL) { |
2592 | 0 | cpi->last_source = av1_realloc_and_scale_if_required( |
2593 | 0 | cm, cpi->unscaled_last_source, &cpi->scaled_last_source, filter_scaler, |
2594 | 0 | phase_scaler, true, false, cpi->oxcf.border_in_pixels, |
2595 | 0 | cpi->alloc_pyramid); |
2596 | 0 | } |
2597 | |
|
2598 | 0 | if (cpi->sf.rt_sf.use_temporal_noise_estimate) { |
2599 | 0 | av1_update_noise_estimate(cpi); |
2600 | 0 | } |
2601 | |
|
2602 | | #if CONFIG_AV1_TEMPORAL_DENOISING |
2603 | | if (cpi->oxcf.noise_sensitivity > 0 && cpi->ppi->use_svc) |
2604 | | av1_denoiser_reset_on_first_frame(cpi); |
2605 | | #endif |
2606 | | |
2607 | | // For 1 spatial layer encoding: if the (non-LAST) reference has different |
2608 | | // resolution from the source then disable that reference. This is to avoid |
2609 | | // significant increase in encode time from scaling the references in |
2610 | | // av1_scale_references. Note GOLDEN is forced to update on the (first/tigger) |
2611 | | // resized frame and ALTREF will be refreshed ~4 frames later, so both |
2612 | | // references become available again after few frames. |
2613 | | // For superres: don't disable golden reference. |
2614 | 0 | if (svc->number_spatial_layers == 1) { |
2615 | 0 | if (!cpi->oxcf.superres_cfg.enable_superres) { |
2616 | 0 | if (cpi->ref_frame_flags & av1_ref_frame_flag_list[GOLDEN_FRAME]) { |
2617 | 0 | const YV12_BUFFER_CONFIG *const ref = |
2618 | 0 | get_ref_frame_yv12_buf(cm, GOLDEN_FRAME); |
2619 | 0 | if (ref == NULL || ref->y_crop_width != cm->width || |
2620 | 0 | ref->y_crop_height != cm->height) { |
2621 | 0 | cpi->ref_frame_flags ^= AOM_GOLD_FLAG; |
2622 | 0 | } |
2623 | 0 | } |
2624 | 0 | } |
2625 | 0 | if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ALTREF_FRAME]) { |
2626 | 0 | const YV12_BUFFER_CONFIG *const ref = |
2627 | 0 | get_ref_frame_yv12_buf(cm, ALTREF_FRAME); |
2628 | 0 | if (ref == NULL || ref->y_crop_width != cm->width || |
2629 | 0 | ref->y_crop_height != cm->height) { |
2630 | 0 | cpi->ref_frame_flags ^= AOM_ALT_FLAG; |
2631 | 0 | } |
2632 | 0 | } |
2633 | 0 | } |
2634 | |
|
2635 | 0 | int scale_references = 0; |
2636 | | #if CONFIG_FPMT_TEST |
2637 | | scale_references = |
2638 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE ? 1 : 0; |
2639 | | #endif // CONFIG_FPMT_TEST |
2640 | 0 | if (scale_references || |
2641 | 0 | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) { |
2642 | 0 | if (!frame_is_intra_only(cm)) { |
2643 | 0 | av1_scale_references(cpi, filter_scaler, phase_scaler, 1); |
2644 | 0 | } |
2645 | 0 | } |
2646 | |
|
2647 | 0 | av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q, |
2648 | 0 | q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq); |
2649 | 0 | av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed); |
2650 | 0 | av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params, |
2651 | 0 | cm->seq_params->bit_depth); |
2652 | 0 | av1_set_variance_partition_thresholds(cpi, q, 0); |
2653 | 0 | av1_setup_frame(cpi); |
2654 | | |
2655 | | // Check if this high_source_sad (scene/slide change) frame should be |
2656 | | // encoded at high/max QP, and if so, set the q and adjust some rate |
2657 | | // control parameters. |
2658 | 0 | if (cpi->sf.rt_sf.overshoot_detection_cbr == FAST_DETECTION_MAXQ && |
2659 | 0 | cpi->rc.high_source_sad) { |
2660 | 0 | if (av1_encodedframe_overshoot_cbr(cpi, &q)) { |
2661 | 0 | av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q, |
2662 | 0 | q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq); |
2663 | 0 | av1_set_speed_features_qindex_dependent(cpi, cpi->oxcf.speed); |
2664 | 0 | av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params, |
2665 | 0 | cm->seq_params->bit_depth); |
2666 | 0 | av1_set_variance_partition_thresholds(cpi, q, 0); |
2667 | 0 | if (frame_is_intra_only(cm) || cm->features.error_resilient_mode || |
2668 | 0 | cm->features.primary_ref_frame == PRIMARY_REF_NONE) |
2669 | 0 | av1_setup_frame(cpi); |
2670 | 0 | } |
2671 | 0 | } |
2672 | 0 | av1_apply_active_map(cpi); |
2673 | 0 | if (q_cfg->aq_mode == CYCLIC_REFRESH_AQ) av1_cyclic_refresh_setup(cpi); |
2674 | 0 | if (cm->seg.enabled) { |
2675 | 0 | if (!cm->seg.update_data && cm->prev_frame) { |
2676 | 0 | segfeatures_copy(&cm->seg, &cm->prev_frame->seg); |
2677 | 0 | cm->seg.enabled = cm->prev_frame->seg.enabled; |
2678 | 0 | } else { |
2679 | 0 | av1_calculate_segdata(&cm->seg); |
2680 | 0 | } |
2681 | 0 | } else { |
2682 | 0 | memset(&cm->seg, 0, sizeof(cm->seg)); |
2683 | 0 | } |
2684 | 0 | segfeatures_copy(&cm->cur_frame->seg, &cm->seg); |
2685 | 0 | cm->cur_frame->seg.enabled = cm->seg.enabled; |
2686 | | |
2687 | | // This is for rtc temporal filtering case. |
2688 | 0 | if (is_psnr_calc_enabled(cpi) && cpi->sf.rt_sf.use_rtc_tf) { |
2689 | 0 | const SequenceHeader *seq_params = cm->seq_params; |
2690 | |
|
2691 | 0 | if (cpi->orig_source.buffer_alloc_sz == 0 || |
2692 | 0 | cpi->rc.prev_coded_width != cpi->oxcf.frm_dim_cfg.width || |
2693 | 0 | cpi->rc.prev_coded_height != cpi->oxcf.frm_dim_cfg.height) { |
2694 | | // Allocate a source buffer to store the true source for psnr calculation. |
2695 | 0 | if (aom_alloc_frame_buffer( |
2696 | 0 | &cpi->orig_source, cpi->oxcf.frm_dim_cfg.width, |
2697 | 0 | cpi->oxcf.frm_dim_cfg.height, seq_params->subsampling_x, |
2698 | 0 | seq_params->subsampling_y, seq_params->use_highbitdepth, |
2699 | 0 | cpi->oxcf.border_in_pixels, cm->features.byte_alignment, false, |
2700 | 0 | 0)) |
2701 | 0 | aom_internal_error(cm->error, AOM_CODEC_MEM_ERROR, |
2702 | 0 | "Failed to allocate scaled buffer"); |
2703 | 0 | } |
2704 | |
|
2705 | 0 | aom_yv12_copy_y(cpi->source, &cpi->orig_source, 1); |
2706 | 0 | aom_yv12_copy_u(cpi->source, &cpi->orig_source, 1); |
2707 | 0 | aom_yv12_copy_v(cpi->source, &cpi->orig_source, 1); |
2708 | 0 | } |
2709 | |
|
2710 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2711 | | start_timing(cpi, av1_encode_frame_time); |
2712 | | #endif |
2713 | | |
2714 | | // Set the motion vector precision based on mv stats from the last coded |
2715 | | // frame. |
2716 | 0 | if (!frame_is_intra_only(cm)) av1_pick_and_set_high_precision_mv(cpi, q); |
2717 | | |
2718 | | // transform / motion compensation build reconstruction frame |
2719 | 0 | av1_encode_frame(cpi); |
2720 | |
|
2721 | 0 | if (!cpi->rc.rtc_external_ratectrl && !frame_is_intra_only(cm)) |
2722 | 0 | update_motion_stat(cpi); |
2723 | | |
2724 | | // Adjust the refresh of the golden (longer-term) reference based on QP |
2725 | | // selected for this frame. This is for CBR real-time mode, and only |
2726 | | // for single layer without usage of the set_ref_frame_config (so |
2727 | | // reference structure for 1 layer is set internally). |
2728 | 0 | if (!frame_is_intra_only(cm) && cpi->oxcf.rc_cfg.mode == AOM_CBR && |
2729 | 0 | cpi->oxcf.mode == REALTIME && svc->number_spatial_layers == 1 && |
2730 | 0 | svc->number_temporal_layers == 1 && !cpi->rc.rtc_external_ratectrl && |
2731 | 0 | !cpi->ppi->rtc_ref.set_ref_frame_config && |
2732 | 0 | sf->rt_sf.gf_refresh_based_on_qp) |
2733 | 0 | av1_adjust_gf_refresh_qp_one_pass_rt(cpi); |
2734 | | |
2735 | | // For non-svc: if scaling is required, copy scaled_source |
2736 | | // into scaled_last_source. |
2737 | 0 | if (cm->current_frame.frame_number > 1 && !cpi->ppi->use_svc && |
2738 | 0 | cpi->scaled_source.y_buffer != NULL && |
2739 | 0 | cpi->scaled_last_source.y_buffer != NULL && |
2740 | 0 | cpi->scaled_source.y_crop_width == cpi->scaled_last_source.y_crop_width && |
2741 | 0 | cpi->scaled_source.y_crop_height == |
2742 | 0 | cpi->scaled_last_source.y_crop_height && |
2743 | 0 | (cm->width != cpi->unscaled_source->y_crop_width || |
2744 | 0 | cm->height != cpi->unscaled_source->y_crop_height)) { |
2745 | 0 | cpi->scaled_last_source_available = 1; |
2746 | 0 | aom_yv12_copy_y(&cpi->scaled_source, &cpi->scaled_last_source, 1); |
2747 | 0 | aom_yv12_copy_u(&cpi->scaled_source, &cpi->scaled_last_source, 1); |
2748 | 0 | aom_yv12_copy_v(&cpi->scaled_source, &cpi->scaled_last_source, 1); |
2749 | 0 | } |
2750 | |
|
2751 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2752 | | end_timing(cpi, av1_encode_frame_time); |
2753 | | #endif |
2754 | | #if CONFIG_INTERNAL_STATS |
2755 | | ++cpi->frame_recode_hits; |
2756 | | #endif |
2757 | |
|
2758 | 0 | return AOM_CODEC_OK; |
2759 | 0 | } |
2760 | | |
2761 | | #if !CONFIG_REALTIME_ONLY |
2762 | | |
2763 | | /*!\brief Recode loop for encoding one frame. the purpose of encoding one frame |
2764 | | * for multiple times can be approaching a target bitrate or adjusting the usage |
2765 | | * of global motions. |
2766 | | * |
2767 | | * \ingroup high_level_algo |
2768 | | * |
2769 | | * \param[in] cpi Top-level encoder structure |
2770 | | * \param[in] size Bitstream size |
2771 | | * \param[in] dest Bitstream output |
2772 | | * |
2773 | | * \return Returns a value to indicate if the encoding is done successfully. |
2774 | | * \retval #AOM_CODEC_OK |
2775 | | * \retval -1 |
2776 | | * \retval #AOM_CODEC_ERROR |
2777 | | */ |
2778 | 0 | static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) { |
2779 | 0 | AV1_COMMON *const cm = &cpi->common; |
2780 | 0 | RATE_CONTROL *const rc = &cpi->rc; |
2781 | 0 | GlobalMotionInfo *const gm_info = &cpi->gm_info; |
2782 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
2783 | 0 | const QuantizationCfg *const q_cfg = &oxcf->q_cfg; |
2784 | 0 | const int allow_recode = (cpi->sf.hl_sf.recode_loop != DISALLOW_RECODE); |
2785 | | // Must allow recode if minimum compression ratio is set. |
2786 | 0 | assert(IMPLIES(oxcf->rc_cfg.min_cr > 0, allow_recode)); |
2787 | |
|
2788 | 0 | set_size_independent_vars(cpi); |
2789 | 0 | if (is_stat_consumption_stage_twopass(cpi) && |
2790 | 0 | cpi->sf.interp_sf.adaptive_interp_filter_search) |
2791 | 0 | cpi->interp_search_flags.interp_filter_search_mask = |
2792 | 0 | av1_setup_interp_filter_search_mask(cpi); |
2793 | |
|
2794 | 0 | av1_setup_frame_size(cpi); |
2795 | |
|
2796 | 0 | if (av1_superres_in_recode_allowed(cpi) && |
2797 | 0 | cpi->superres_mode != AOM_SUPERRES_NONE && |
2798 | 0 | cm->superres_scale_denominator == SCALE_NUMERATOR) { |
2799 | | // Superres mode is currently enabled, but the denominator selected will |
2800 | | // disable superres. So no need to continue, as we will go through another |
2801 | | // recode loop for full-resolution after this anyway. |
2802 | 0 | return -1; |
2803 | 0 | } |
2804 | | |
2805 | 0 | int top_index = 0, bottom_index = 0; |
2806 | 0 | int q = 0, q_low = 0, q_high = 0; |
2807 | 0 | av1_set_size_dependent_vars(cpi, &q, &bottom_index, &top_index); |
2808 | 0 | q_low = bottom_index; |
2809 | 0 | q_high = top_index; |
2810 | |
|
2811 | 0 | av1_set_mv_search_params(cpi); |
2812 | |
|
2813 | 0 | allocate_gradient_info_for_hog(cpi); |
2814 | |
|
2815 | 0 | allocate_src_var_of_4x4_sub_block_buf(cpi); |
2816 | |
|
2817 | 0 | if (cpi->sf.part_sf.partition_search_type == VAR_BASED_PARTITION) |
2818 | 0 | variance_partition_alloc(cpi); |
2819 | |
|
2820 | 0 | if (cm->current_frame.frame_type == KEY_FRAME) copy_frame_prob_info(cpi); |
2821 | |
|
2822 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
2823 | | printf("\n Encoding a frame: \n"); |
2824 | | #endif |
2825 | |
|
2826 | 0 | #if !CONFIG_RD_COMMAND |
2827 | | // Determine whether to use screen content tools using two fast encoding. |
2828 | 0 | if (!cpi->sf.hl_sf.disable_extra_sc_testing && !cpi->use_ducky_encode) |
2829 | 0 | av1_determine_sc_tools_with_encoding(cpi, q); |
2830 | 0 | #endif // !CONFIG_RD_COMMAND |
2831 | |
|
2832 | | #if CONFIG_TUNE_VMAF |
2833 | | if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) { |
2834 | | av1_vmaf_neg_preprocessing(cpi, cpi->unscaled_source); |
2835 | | } |
2836 | | #endif |
2837 | |
|
2838 | | #if CONFIG_TUNE_BUTTERAUGLI |
2839 | | cpi->butteraugli_info.recon_set = false; |
2840 | | int original_q = 0; |
2841 | | #endif |
2842 | |
|
2843 | 0 | cpi->num_frame_recode = 0; |
2844 | | |
2845 | | // Loop variables |
2846 | 0 | int loop = 0; |
2847 | 0 | int loop_count = 0; |
2848 | 0 | int overshoot_seen = 0; |
2849 | 0 | int undershoot_seen = 0; |
2850 | 0 | int low_cr_seen = 0; |
2851 | 0 | int last_loop_allow_hp = 0; |
2852 | |
|
2853 | 0 | do { |
2854 | 0 | loop = 0; |
2855 | 0 | int do_mv_stats_collection = 1; |
2856 | | |
2857 | | // if frame was scaled calculate global_motion_search again if already |
2858 | | // done |
2859 | 0 | if (loop_count > 0 && cpi->source && gm_info->search_done) { |
2860 | 0 | if (cpi->source->y_crop_width != cm->width || |
2861 | 0 | cpi->source->y_crop_height != cm->height) { |
2862 | 0 | gm_info->search_done = 0; |
2863 | 0 | } |
2864 | 0 | } |
2865 | 0 | cpi->source = av1_realloc_and_scale_if_required( |
2866 | 0 | cm, cpi->unscaled_source, &cpi->scaled_source, EIGHTTAP_REGULAR, 0, |
2867 | 0 | false, false, cpi->oxcf.border_in_pixels, cpi->alloc_pyramid); |
2868 | |
|
2869 | | #if CONFIG_TUNE_BUTTERAUGLI |
2870 | | if (oxcf->tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) { |
2871 | | if (loop_count == 0) { |
2872 | | original_q = q; |
2873 | | // TODO(sdeng): different q here does not make big difference. Use a |
2874 | | // faster pass instead. |
2875 | | q = 96; |
2876 | | av1_setup_butteraugli_source(cpi); |
2877 | | } else { |
2878 | | q = original_q; |
2879 | | } |
2880 | | } |
2881 | | #endif |
2882 | |
|
2883 | 0 | if (cpi->unscaled_last_source != NULL) { |
2884 | 0 | cpi->last_source = av1_realloc_and_scale_if_required( |
2885 | 0 | cm, cpi->unscaled_last_source, &cpi->scaled_last_source, |
2886 | 0 | EIGHTTAP_REGULAR, 0, false, false, cpi->oxcf.border_in_pixels, |
2887 | 0 | cpi->alloc_pyramid); |
2888 | 0 | } |
2889 | |
|
2890 | 0 | int scale_references = 0; |
2891 | | #if CONFIG_FPMT_TEST |
2892 | | scale_references = |
2893 | | cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE ? 1 : 0; |
2894 | | #endif // CONFIG_FPMT_TEST |
2895 | 0 | if (scale_references || |
2896 | 0 | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) { |
2897 | 0 | if (!frame_is_intra_only(cm)) { |
2898 | 0 | if (loop_count > 0) { |
2899 | 0 | release_scaled_references(cpi); |
2900 | 0 | } |
2901 | 0 | av1_scale_references(cpi, EIGHTTAP_REGULAR, 0, 0); |
2902 | 0 | } |
2903 | 0 | } |
2904 | |
|
2905 | | #if CONFIG_TUNE_VMAF |
2906 | | if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING && |
2907 | | oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) { |
2908 | | cpi->vmaf_info.original_qindex = q; |
2909 | | q = av1_get_vmaf_base_qindex(cpi, q); |
2910 | | } |
2911 | | #endif |
2912 | |
|
2913 | | #if CONFIG_RD_COMMAND |
2914 | | RD_COMMAND *rd_command = &cpi->rd_command; |
2915 | | RD_OPTION option = rd_command->option_ls[rd_command->frame_index]; |
2916 | | if (option == RD_OPTION_SET_Q || option == RD_OPTION_SET_Q_RDMULT) { |
2917 | | q = rd_command->q_index_ls[rd_command->frame_index]; |
2918 | | } |
2919 | | #endif // CONFIG_RD_COMMAND |
2920 | |
|
2921 | | #if CONFIG_BITRATE_ACCURACY |
2922 | | #if CONFIG_THREE_PASS |
2923 | | if (oxcf->pass == AOM_RC_THIRD_PASS && cpi->vbr_rc_info.ready == 1) { |
2924 | | int frame_coding_idx = |
2925 | | av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, cpi->gf_frame_index); |
2926 | | if (frame_coding_idx < cpi->vbr_rc_info.total_frame_count) { |
2927 | | q = cpi->vbr_rc_info.q_index_list[frame_coding_idx]; |
2928 | | } else { |
2929 | | // TODO(angiebird): Investigate why sometimes there is an extra frame |
2930 | | // after the last GOP. |
2931 | | q = cpi->vbr_rc_info.base_q_index; |
2932 | | } |
2933 | | } |
2934 | | #else |
2935 | | if (cpi->vbr_rc_info.q_index_list_ready) { |
2936 | | q = cpi->vbr_rc_info.q_index_list[cpi->gf_frame_index]; |
2937 | | } |
2938 | | #endif // CONFIG_THREE_PASS |
2939 | | #endif // CONFIG_BITRATE_ACCURACY |
2940 | |
|
2941 | | #if CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY |
2942 | | // TODO(angiebird): Move this into a function. |
2943 | | if (oxcf->pass == AOM_RC_THIRD_PASS) { |
2944 | | int frame_coding_idx = |
2945 | | av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, cpi->gf_frame_index); |
2946 | | double qstep_ratio = cpi->vbr_rc_info.qstep_ratio_list[frame_coding_idx]; |
2947 | | FRAME_UPDATE_TYPE update_type = |
2948 | | cpi->vbr_rc_info.update_type_list[frame_coding_idx]; |
2949 | | rc_log_frame_encode_param(&cpi->rc_log, frame_coding_idx, qstep_ratio, q, |
2950 | | update_type); |
2951 | | } |
2952 | | #endif // CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY |
2953 | |
|
2954 | 0 | if (cpi->use_ducky_encode) { |
2955 | 0 | const DuckyEncodeFrameInfo *frame_info = |
2956 | 0 | &cpi->ducky_encode_info.frame_info; |
2957 | 0 | if (frame_info->qp_mode == DUCKY_ENCODE_FRAME_MODE_QINDEX) { |
2958 | 0 | q = frame_info->q_index; |
2959 | 0 | cm->delta_q_info.delta_q_present_flag = frame_info->delta_q_enabled; |
2960 | 0 | } |
2961 | 0 | } |
2962 | |
|
2963 | 0 | av1_set_quantizer(cm, q_cfg->qm_minlevel, q_cfg->qm_maxlevel, q, |
2964 | 0 | q_cfg->enable_chroma_deltaq, q_cfg->enable_hdr_deltaq); |
2965 | 0 | av1_set_speed_features_qindex_dependent(cpi, oxcf->speed); |
2966 | 0 | av1_init_quantizer(&cpi->enc_quant_dequant_params, &cm->quant_params, |
2967 | 0 | cm->seq_params->bit_depth); |
2968 | |
|
2969 | 0 | av1_set_variance_partition_thresholds(cpi, q, 0); |
2970 | | |
2971 | | // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n", |
2972 | | // cm->current_frame.frame_number, cm->show_frame, q, |
2973 | | // cm->current_frame.frame_type, cm->superres_scale_denominator); |
2974 | |
|
2975 | 0 | if (loop_count == 0) { |
2976 | 0 | av1_setup_frame(cpi); |
2977 | 0 | } else if (get_primary_ref_frame_buf(cm) == NULL) { |
2978 | | // Base q-index may have changed, so we need to assign proper default coef |
2979 | | // probs before every iteration. |
2980 | 0 | av1_default_coef_probs(cm); |
2981 | 0 | av1_setup_frame_contexts(cm); |
2982 | 0 | } |
2983 | |
|
2984 | 0 | if (q_cfg->aq_mode == VARIANCE_AQ) { |
2985 | 0 | av1_vaq_frame_setup(cpi); |
2986 | 0 | } else if (q_cfg->aq_mode == COMPLEXITY_AQ) { |
2987 | 0 | av1_setup_in_frame_q_adj(cpi); |
2988 | 0 | } |
2989 | |
|
2990 | 0 | if (cm->seg.enabled) { |
2991 | 0 | if (!cm->seg.update_data && cm->prev_frame) { |
2992 | 0 | segfeatures_copy(&cm->seg, &cm->prev_frame->seg); |
2993 | 0 | cm->seg.enabled = cm->prev_frame->seg.enabled; |
2994 | 0 | } else { |
2995 | 0 | av1_calculate_segdata(&cm->seg); |
2996 | 0 | } |
2997 | 0 | } else { |
2998 | 0 | memset(&cm->seg, 0, sizeof(cm->seg)); |
2999 | 0 | } |
3000 | 0 | segfeatures_copy(&cm->cur_frame->seg, &cm->seg); |
3001 | 0 | cm->cur_frame->seg.enabled = cm->seg.enabled; |
3002 | |
|
3003 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3004 | | start_timing(cpi, av1_encode_frame_time); |
3005 | | #endif |
3006 | | // Set the motion vector precision based on mv stats from the last coded |
3007 | | // frame. |
3008 | 0 | if (!frame_is_intra_only(cm)) { |
3009 | 0 | av1_pick_and_set_high_precision_mv(cpi, q); |
3010 | | |
3011 | | // If the precision has changed during different iteration of the loop, |
3012 | | // then we need to reset the global motion vectors |
3013 | 0 | if (loop_count > 0 && |
3014 | 0 | cm->features.allow_high_precision_mv != last_loop_allow_hp) { |
3015 | 0 | gm_info->search_done = 0; |
3016 | 0 | } |
3017 | 0 | last_loop_allow_hp = cm->features.allow_high_precision_mv; |
3018 | 0 | } |
3019 | | |
3020 | | // transform / motion compensation build reconstruction frame |
3021 | 0 | av1_encode_frame(cpi); |
3022 | | |
3023 | | // Disable mv_stats collection for parallel frames based on update flag. |
3024 | 0 | if (!cpi->do_frame_data_update) do_mv_stats_collection = 0; |
3025 | | |
3026 | | // Reset the mv_stats in case we are interrupted by an intraframe or an |
3027 | | // overlay frame. |
3028 | 0 | if (cpi->mv_stats.valid && do_mv_stats_collection) av1_zero(cpi->mv_stats); |
3029 | | |
3030 | | // Gather the mv_stats for the next frame |
3031 | 0 | if (cpi->sf.hl_sf.high_precision_mv_usage == LAST_MV_DATA && |
3032 | 0 | av1_frame_allows_smart_mv(cpi) && do_mv_stats_collection) { |
3033 | 0 | av1_collect_mv_stats(cpi, q); |
3034 | 0 | } |
3035 | |
|
3036 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3037 | | end_timing(cpi, av1_encode_frame_time); |
3038 | | #endif |
3039 | |
|
3040 | | #if CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND |
3041 | | const int do_dummy_pack = 1; |
3042 | | #else // CONFIG_BITRATE_ACCURACY |
3043 | | // Dummy pack of the bitstream using up to date stats to get an |
3044 | | // accurate estimate of output frame size to determine if we need |
3045 | | // to recode. |
3046 | 0 | const int do_dummy_pack = |
3047 | 0 | (cpi->sf.hl_sf.recode_loop >= ALLOW_RECODE_KFARFGF && |
3048 | 0 | oxcf->rc_cfg.mode != AOM_Q) || |
3049 | 0 | oxcf->rc_cfg.min_cr > 0; |
3050 | 0 | #endif // CONFIG_BITRATE_ACCURACY |
3051 | 0 | if (do_dummy_pack) { |
3052 | 0 | av1_finalize_encoded_frame(cpi); |
3053 | 0 | int largest_tile_id = 0; // Output from bitstream: unused here |
3054 | 0 | rc->coefficient_size = 0; |
3055 | 0 | if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != |
3056 | 0 | AOM_CODEC_OK) { |
3057 | 0 | return AOM_CODEC_ERROR; |
3058 | 0 | } |
3059 | | |
3060 | | // bits used for this frame |
3061 | 0 | rc->projected_frame_size = (int)(*size) << 3; |
3062 | | #if CONFIG_RD_COMMAND |
3063 | | PSNR_STATS psnr; |
3064 | | aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr); |
3065 | | printf("q %d rdmult %d rate %d dist %" PRIu64 "\n", q, cpi->rd.RDMULT, |
3066 | | rc->projected_frame_size, psnr.sse[0]); |
3067 | | ++rd_command->frame_index; |
3068 | | if (rd_command->frame_index == rd_command->frame_count) { |
3069 | | return AOM_CODEC_ERROR; |
3070 | | } |
3071 | | #endif // CONFIG_RD_COMMAND |
3072 | |
|
3073 | | #if CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY |
3074 | | if (oxcf->pass == AOM_RC_THIRD_PASS) { |
3075 | | int frame_coding_idx = |
3076 | | av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, cpi->gf_frame_index); |
3077 | | rc_log_frame_entropy(&cpi->rc_log, frame_coding_idx, |
3078 | | rc->projected_frame_size, rc->coefficient_size); |
3079 | | } |
3080 | | #endif // CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY |
3081 | 0 | } |
3082 | | |
3083 | | #if CONFIG_TUNE_VMAF |
3084 | | if (oxcf->tune_cfg.tuning >= AOM_TUNE_VMAF_WITH_PREPROCESSING && |
3085 | | oxcf->tune_cfg.tuning <= AOM_TUNE_VMAF_NEG_MAX_GAIN) { |
3086 | | q = cpi->vmaf_info.original_qindex; |
3087 | | } |
3088 | | #endif |
3089 | 0 | if (allow_recode) { |
3090 | | // Update q and decide whether to do a recode loop |
3091 | 0 | recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index, |
3092 | 0 | bottom_index, &undershoot_seen, &overshoot_seen, |
3093 | 0 | &low_cr_seen, loop_count); |
3094 | 0 | } |
3095 | |
|
3096 | | #if CONFIG_TUNE_BUTTERAUGLI |
3097 | | if (loop_count == 0 && oxcf->tune_cfg.tuning == AOM_TUNE_BUTTERAUGLI) { |
3098 | | loop = 1; |
3099 | | av1_setup_butteraugli_rdmult_and_restore_source(cpi, 0.4); |
3100 | | } |
3101 | | #endif |
3102 | |
|
3103 | 0 | if (cpi->use_ducky_encode) { |
3104 | | // Ducky encode currently does not support recode loop. |
3105 | 0 | loop = 0; |
3106 | 0 | } |
3107 | | #if CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND |
3108 | | loop = 0; // turn off recode loop when CONFIG_BITRATE_ACCURACY is on |
3109 | | #endif // CONFIG_BITRATE_ACCURACY || CONFIG_RD_COMMAND |
3110 | |
|
3111 | 0 | if (loop) { |
3112 | 0 | ++loop_count; |
3113 | 0 | cpi->num_frame_recode = |
3114 | 0 | (cpi->num_frame_recode < (NUM_RECODES_PER_FRAME - 1)) |
3115 | 0 | ? (cpi->num_frame_recode + 1) |
3116 | 0 | : (NUM_RECODES_PER_FRAME - 1); |
3117 | | #if CONFIG_INTERNAL_STATS |
3118 | | ++cpi->frame_recode_hits; |
3119 | | #endif |
3120 | 0 | } |
3121 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3122 | | if (loop) printf("\n Recoding:"); |
3123 | | #endif |
3124 | 0 | } while (loop); |
3125 | | |
3126 | 0 | return AOM_CODEC_OK; |
3127 | 0 | } |
3128 | | #endif // !CONFIG_REALTIME_ONLY |
3129 | | |
3130 | | // TODO(jingning, paulwilkins): Set up high grain level to test |
3131 | | // hardware decoders. Need to adapt the actual noise variance |
3132 | | // according to the difference between reconstructed frame and the |
3133 | | // source signal. |
3134 | 0 | static void set_grain_syn_params(AV1_COMMON *cm) { |
3135 | 0 | aom_film_grain_t *film_grain_params = &cm->film_grain_params; |
3136 | 0 | film_grain_params->apply_grain = 1; |
3137 | 0 | film_grain_params->update_parameters = 1; |
3138 | 0 | film_grain_params->random_seed = rand() & 0xffff; |
3139 | |
|
3140 | 0 | film_grain_params->num_y_points = 1; |
3141 | 0 | film_grain_params->scaling_points_y[0][0] = 128; |
3142 | 0 | film_grain_params->scaling_points_y[0][1] = 100; |
3143 | |
|
3144 | 0 | if (!cm->seq_params->monochrome) { |
3145 | 0 | film_grain_params->num_cb_points = 1; |
3146 | 0 | film_grain_params->scaling_points_cb[0][0] = 128; |
3147 | 0 | film_grain_params->scaling_points_cb[0][1] = 100; |
3148 | |
|
3149 | 0 | film_grain_params->num_cr_points = 1; |
3150 | 0 | film_grain_params->scaling_points_cr[0][0] = 128; |
3151 | 0 | film_grain_params->scaling_points_cr[0][1] = 100; |
3152 | 0 | } else { |
3153 | 0 | film_grain_params->num_cb_points = 0; |
3154 | 0 | film_grain_params->num_cr_points = 0; |
3155 | 0 | } |
3156 | |
|
3157 | 0 | film_grain_params->chroma_scaling_from_luma = 0; |
3158 | |
|
3159 | 0 | film_grain_params->scaling_shift = 1; |
3160 | 0 | film_grain_params->ar_coeff_lag = 0; |
3161 | 0 | film_grain_params->ar_coeff_shift = 1; |
3162 | 0 | film_grain_params->overlap_flag = 1; |
3163 | 0 | film_grain_params->grain_scale_shift = 0; |
3164 | 0 | } |
3165 | | |
3166 | | /*!\brief Recode loop or a single loop for encoding one frame, followed by |
3167 | | * in-loop deblocking filters, CDEF filters, and restoration filters. |
3168 | | * |
3169 | | * \ingroup high_level_algo |
3170 | | * \callgraph |
3171 | | * \callergraph |
3172 | | * |
3173 | | * \param[in] cpi Top-level encoder structure |
3174 | | * \param[in] size Bitstream size |
3175 | | * \param[in] dest Bitstream output |
3176 | | * \param[in] sse Total distortion of the frame |
3177 | | * \param[in] rate Total rate of the frame |
3178 | | * \param[in] largest_tile_id Tile id of the last tile |
3179 | | * |
3180 | | * \return Returns a value to indicate if the encoding is done successfully. |
3181 | | * \retval #AOM_CODEC_OK |
3182 | | * \retval #AOM_CODEC_ERROR |
3183 | | */ |
3184 | | static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size, |
3185 | | uint8_t *dest, int64_t *sse, |
3186 | | int64_t *rate, |
3187 | 0 | int *largest_tile_id) { |
3188 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3189 | | start_timing(cpi, encode_with_or_without_recode_time); |
3190 | | #endif |
3191 | 0 | for (int i = 0; i < NUM_RECODES_PER_FRAME; i++) { |
3192 | 0 | cpi->do_update_frame_probs_txtype[i] = 0; |
3193 | 0 | cpi->do_update_frame_probs_obmc[i] = 0; |
3194 | 0 | cpi->do_update_frame_probs_warp[i] = 0; |
3195 | 0 | cpi->do_update_frame_probs_interpfilter[i] = 0; |
3196 | 0 | } |
3197 | |
|
3198 | 0 | cpi->do_update_vbr_bits_off_target_fast = 0; |
3199 | 0 | int err; |
3200 | | #if CONFIG_REALTIME_ONLY |
3201 | | err = encode_without_recode(cpi); |
3202 | | #else |
3203 | 0 | if (cpi->sf.hl_sf.recode_loop == DISALLOW_RECODE) |
3204 | 0 | err = encode_without_recode(cpi); |
3205 | 0 | else |
3206 | 0 | err = encode_with_recode_loop(cpi, size, dest); |
3207 | 0 | #endif |
3208 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3209 | | end_timing(cpi, encode_with_or_without_recode_time); |
3210 | | #endif |
3211 | 0 | if (err != AOM_CODEC_OK) { |
3212 | 0 | if (err == -1) { |
3213 | | // special case as described in encode_with_recode_loop(). |
3214 | | // Encoding was skipped. |
3215 | 0 | err = AOM_CODEC_OK; |
3216 | 0 | if (sse != NULL) *sse = INT64_MAX; |
3217 | 0 | if (rate != NULL) *rate = INT64_MAX; |
3218 | 0 | *largest_tile_id = 0; |
3219 | 0 | } |
3220 | 0 | return err; |
3221 | 0 | } |
3222 | | |
3223 | | #ifdef OUTPUT_YUV_DENOISED |
3224 | | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
3225 | | if (oxcf->noise_sensitivity > 0 && denoise_svc(cpi)) { |
3226 | | aom_write_yuv_frame(yuv_denoised_file, |
3227 | | &cpi->denoiser.running_avg_y[INTRA_FRAME]); |
3228 | | } |
3229 | | #endif |
3230 | | |
3231 | 0 | AV1_COMMON *const cm = &cpi->common; |
3232 | 0 | SequenceHeader *const seq_params = cm->seq_params; |
3233 | | |
3234 | | // Special case code to reduce pulsing when key frames are forced at a |
3235 | | // fixed interval. Note the reconstruction error if it is the frame before |
3236 | | // the force key frame |
3237 | 0 | if (cpi->ppi->p_rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) { |
3238 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
3239 | 0 | if (seq_params->use_highbitdepth) { |
3240 | 0 | cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf); |
3241 | 0 | } else { |
3242 | 0 | cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf); |
3243 | 0 | } |
3244 | | #else |
3245 | | cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf); |
3246 | | #endif |
3247 | 0 | } |
3248 | |
|
3249 | 0 | cm->cur_frame->buf.color_primaries = seq_params->color_primaries; |
3250 | 0 | cm->cur_frame->buf.transfer_characteristics = |
3251 | 0 | seq_params->transfer_characteristics; |
3252 | 0 | cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients; |
3253 | 0 | cm->cur_frame->buf.monochrome = seq_params->monochrome; |
3254 | 0 | cm->cur_frame->buf.chroma_sample_position = |
3255 | 0 | seq_params->chroma_sample_position; |
3256 | 0 | cm->cur_frame->buf.color_range = seq_params->color_range; |
3257 | 0 | cm->cur_frame->buf.render_width = cm->render_width; |
3258 | 0 | cm->cur_frame->buf.render_height = cm->render_height; |
3259 | |
|
3260 | 0 | if (!cpi->mt_info.pipeline_lpf_mt_with_enc) |
3261 | 0 | set_postproc_filter_default_params(&cpi->common); |
3262 | |
|
3263 | 0 | if (!cm->features.allow_intrabc) { |
3264 | 0 | loopfilter_frame(cpi, cm); |
3265 | 0 | } |
3266 | |
|
3267 | 0 | if (cpi->oxcf.mode != ALLINTRA && !cpi->ppi->rtc_ref.non_reference_frame) { |
3268 | 0 | extend_frame_borders(cpi); |
3269 | 0 | } |
3270 | |
|
3271 | | #ifdef OUTPUT_YUV_REC |
3272 | | aom_write_one_yuv_frame(cm, &cm->cur_frame->buf); |
3273 | | #endif |
3274 | |
|
3275 | 0 | if (cpi->oxcf.tune_cfg.content == AOM_CONTENT_FILM) { |
3276 | 0 | set_grain_syn_params(cm); |
3277 | 0 | } |
3278 | |
|
3279 | 0 | av1_finalize_encoded_frame(cpi); |
3280 | | // Build the bitstream |
3281 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3282 | | start_timing(cpi, av1_pack_bitstream_final_time); |
3283 | | #endif |
3284 | 0 | cpi->rc.coefficient_size = 0; |
3285 | 0 | if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK) |
3286 | 0 | return AOM_CODEC_ERROR; |
3287 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3288 | | end_timing(cpi, av1_pack_bitstream_final_time); |
3289 | | #endif |
3290 | | |
3291 | 0 | if (cpi->rc.postencode_drop && allow_postencode_drop_rtc(cpi) && |
3292 | 0 | av1_postencode_drop_cbr(cpi, size)) { |
3293 | 0 | return AOM_CODEC_OK; |
3294 | 0 | } |
3295 | | |
3296 | | // Compute sse and rate. |
3297 | 0 | if (sse != NULL) { |
3298 | 0 | #if CONFIG_AV1_HIGHBITDEPTH |
3299 | 0 | *sse = (seq_params->use_highbitdepth) |
3300 | 0 | ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf) |
3301 | 0 | : aom_get_y_sse(cpi->source, &cm->cur_frame->buf); |
3302 | | #else |
3303 | | *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf); |
3304 | | #endif |
3305 | 0 | } |
3306 | 0 | if (rate != NULL) { |
3307 | 0 | const int64_t bits = (*size << 3); |
3308 | 0 | *rate = (bits << 5); // To match scale. |
3309 | 0 | } |
3310 | |
|
3311 | 0 | #if !CONFIG_REALTIME_ONLY |
3312 | 0 | if (cpi->use_ducky_encode) { |
3313 | 0 | PSNR_STATS psnr; |
3314 | 0 | aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr); |
3315 | 0 | DuckyEncodeFrameResult *frame_result = &cpi->ducky_encode_info.frame_result; |
3316 | 0 | frame_result->global_order_idx = cm->cur_frame->display_order_hint; |
3317 | 0 | frame_result->q_index = cm->quant_params.base_qindex; |
3318 | 0 | frame_result->rdmult = cpi->rd.RDMULT; |
3319 | 0 | frame_result->rate = (int)(*size) * 8; |
3320 | 0 | frame_result->dist = psnr.sse[0]; |
3321 | 0 | frame_result->psnr = psnr.psnr[0]; |
3322 | 0 | } |
3323 | 0 | #endif // !CONFIG_REALTIME_ONLY |
3324 | |
|
3325 | 0 | return AOM_CODEC_OK; |
3326 | 0 | } |
3327 | | |
3328 | | static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size, |
3329 | | uint8_t *dest, |
3330 | 0 | int *largest_tile_id) { |
3331 | 0 | const AV1_COMMON *const cm = &cpi->common; |
3332 | 0 | assert(cm->seq_params->enable_superres); |
3333 | 0 | assert(av1_superres_in_recode_allowed(cpi)); |
3334 | 0 | aom_codec_err_t err = AOM_CODEC_OK; |
3335 | 0 | av1_save_all_coding_context(cpi); |
3336 | |
|
3337 | 0 | int64_t sse1 = INT64_MAX; |
3338 | 0 | int64_t rate1 = INT64_MAX; |
3339 | 0 | int largest_tile_id1 = 0; |
3340 | 0 | int64_t sse2 = INT64_MAX; |
3341 | 0 | int64_t rate2 = INT64_MAX; |
3342 | 0 | int largest_tile_id2; |
3343 | 0 | double proj_rdcost1 = DBL_MAX; |
3344 | 0 | const GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
3345 | 0 | const FRAME_UPDATE_TYPE update_type = |
3346 | 0 | gf_group->update_type[cpi->gf_frame_index]; |
3347 | 0 | const aom_bit_depth_t bit_depth = cm->seq_params->bit_depth; |
3348 | | |
3349 | | // Encode with superres. |
3350 | 0 | if (cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_ALL) { |
3351 | 0 | SuperResCfg *const superres_cfg = &cpi->oxcf.superres_cfg; |
3352 | 0 | int64_t superres_sses[SCALE_NUMERATOR]; |
3353 | 0 | int64_t superres_rates[SCALE_NUMERATOR]; |
3354 | 0 | int superres_largest_tile_ids[SCALE_NUMERATOR]; |
3355 | | // Use superres for Key-frames and Alt-ref frames only. |
3356 | 0 | if (update_type != OVERLAY_UPDATE && update_type != INTNL_OVERLAY_UPDATE) { |
3357 | 0 | for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR; |
3358 | 0 | ++denom) { |
3359 | 0 | superres_cfg->superres_scale_denominator = denom; |
3360 | 0 | superres_cfg->superres_kf_scale_denominator = denom; |
3361 | 0 | const int this_index = denom - (SCALE_NUMERATOR + 1); |
3362 | |
|
3363 | 0 | cpi->superres_mode = AOM_SUPERRES_AUTO; // Super-res on for this loop. |
3364 | 0 | err = encode_with_recode_loop_and_filter( |
3365 | 0 | cpi, size, dest, &superres_sses[this_index], |
3366 | 0 | &superres_rates[this_index], |
3367 | 0 | &superres_largest_tile_ids[this_index]); |
3368 | 0 | cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res). |
3369 | 0 | if (err != AOM_CODEC_OK) return err; |
3370 | 0 | restore_all_coding_context(cpi); |
3371 | 0 | } |
3372 | | // Reset. |
3373 | 0 | superres_cfg->superres_scale_denominator = SCALE_NUMERATOR; |
3374 | 0 | superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR; |
3375 | 0 | } else { |
3376 | 0 | for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR; |
3377 | 0 | ++denom) { |
3378 | 0 | const int this_index = denom - (SCALE_NUMERATOR + 1); |
3379 | 0 | superres_sses[this_index] = INT64_MAX; |
3380 | 0 | superres_rates[this_index] = INT64_MAX; |
3381 | 0 | } |
3382 | 0 | } |
3383 | | // Encode without superres. |
3384 | 0 | assert(cpi->superres_mode == AOM_SUPERRES_NONE); |
3385 | 0 | err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2, |
3386 | 0 | &largest_tile_id2); |
3387 | 0 | if (err != AOM_CODEC_OK) return err; |
3388 | | |
3389 | | // Note: Both use common rdmult based on base qindex of fullres. |
3390 | 0 | const int64_t rdmult = av1_compute_rd_mult_based_on_qindex( |
3391 | 0 | bit_depth, update_type, cm->quant_params.base_qindex); |
3392 | | |
3393 | | // Find the best rdcost among all superres denoms. |
3394 | 0 | int best_denom = -1; |
3395 | 0 | for (int denom = SCALE_NUMERATOR + 1; denom <= 2 * SCALE_NUMERATOR; |
3396 | 0 | ++denom) { |
3397 | 0 | const int this_index = denom - (SCALE_NUMERATOR + 1); |
3398 | 0 | const int64_t this_sse = superres_sses[this_index]; |
3399 | 0 | const int64_t this_rate = superres_rates[this_index]; |
3400 | 0 | const int this_largest_tile_id = superres_largest_tile_ids[this_index]; |
3401 | 0 | const double this_rdcost = RDCOST_DBL_WITH_NATIVE_BD_DIST( |
3402 | 0 | rdmult, this_rate, this_sse, bit_depth); |
3403 | 0 | if (this_rdcost < proj_rdcost1) { |
3404 | 0 | sse1 = this_sse; |
3405 | 0 | rate1 = this_rate; |
3406 | 0 | largest_tile_id1 = this_largest_tile_id; |
3407 | 0 | proj_rdcost1 = this_rdcost; |
3408 | 0 | best_denom = denom; |
3409 | 0 | } |
3410 | 0 | } |
3411 | 0 | const double proj_rdcost2 = |
3412 | 0 | RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate2, sse2, bit_depth); |
3413 | | // Re-encode with superres if it's better. |
3414 | 0 | if (proj_rdcost1 < proj_rdcost2) { |
3415 | 0 | restore_all_coding_context(cpi); |
3416 | | // TODO(urvang): We should avoid rerunning the recode loop by saving |
3417 | | // previous output+state, or running encode only for the selected 'q' in |
3418 | | // previous step. |
3419 | | // Again, temporarily force the best denom. |
3420 | 0 | superres_cfg->superres_scale_denominator = best_denom; |
3421 | 0 | superres_cfg->superres_kf_scale_denominator = best_denom; |
3422 | 0 | int64_t sse3 = INT64_MAX; |
3423 | 0 | int64_t rate3 = INT64_MAX; |
3424 | 0 | cpi->superres_mode = |
3425 | 0 | AOM_SUPERRES_AUTO; // Super-res on for this recode loop. |
3426 | 0 | err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3, |
3427 | 0 | largest_tile_id); |
3428 | 0 | cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res). |
3429 | 0 | assert(sse1 == sse3); |
3430 | 0 | assert(rate1 == rate3); |
3431 | 0 | assert(largest_tile_id1 == *largest_tile_id); |
3432 | | // Reset. |
3433 | 0 | superres_cfg->superres_scale_denominator = SCALE_NUMERATOR; |
3434 | 0 | superres_cfg->superres_kf_scale_denominator = SCALE_NUMERATOR; |
3435 | 0 | } else { |
3436 | 0 | *largest_tile_id = largest_tile_id2; |
3437 | 0 | } |
3438 | 0 | } else { |
3439 | 0 | assert(cpi->sf.hl_sf.superres_auto_search_type == SUPERRES_AUTO_DUAL); |
3440 | 0 | cpi->superres_mode = |
3441 | 0 | AOM_SUPERRES_AUTO; // Super-res on for this recode loop. |
3442 | 0 | err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1, |
3443 | 0 | &largest_tile_id1); |
3444 | 0 | cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res). |
3445 | 0 | if (err != AOM_CODEC_OK) return err; |
3446 | 0 | restore_all_coding_context(cpi); |
3447 | | // Encode without superres. |
3448 | 0 | assert(cpi->superres_mode == AOM_SUPERRES_NONE); |
3449 | 0 | err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2, |
3450 | 0 | &largest_tile_id2); |
3451 | 0 | if (err != AOM_CODEC_OK) return err; |
3452 | | |
3453 | | // Note: Both use common rdmult based on base qindex of fullres. |
3454 | 0 | const int64_t rdmult = av1_compute_rd_mult_based_on_qindex( |
3455 | 0 | bit_depth, update_type, cm->quant_params.base_qindex); |
3456 | 0 | proj_rdcost1 = |
3457 | 0 | RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate1, sse1, bit_depth); |
3458 | 0 | const double proj_rdcost2 = |
3459 | 0 | RDCOST_DBL_WITH_NATIVE_BD_DIST(rdmult, rate2, sse2, bit_depth); |
3460 | | // Re-encode with superres if it's better. |
3461 | 0 | if (proj_rdcost1 < proj_rdcost2) { |
3462 | 0 | restore_all_coding_context(cpi); |
3463 | | // TODO(urvang): We should avoid rerunning the recode loop by saving |
3464 | | // previous output+state, or running encode only for the selected 'q' in |
3465 | | // previous step. |
3466 | 0 | int64_t sse3 = INT64_MAX; |
3467 | 0 | int64_t rate3 = INT64_MAX; |
3468 | 0 | cpi->superres_mode = |
3469 | 0 | AOM_SUPERRES_AUTO; // Super-res on for this recode loop. |
3470 | 0 | err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3, |
3471 | 0 | largest_tile_id); |
3472 | 0 | cpi->superres_mode = AOM_SUPERRES_NONE; // Reset to default (full-res). |
3473 | 0 | assert(sse1 == sse3); |
3474 | 0 | assert(rate1 == rate3); |
3475 | 0 | assert(largest_tile_id1 == *largest_tile_id); |
3476 | 0 | } else { |
3477 | 0 | *largest_tile_id = largest_tile_id2; |
3478 | 0 | } |
3479 | 0 | } |
3480 | | |
3481 | 0 | return err; |
3482 | 0 | } |
3483 | | |
3484 | | // Conditions to disable cdf_update mode in selective mode for real-time. |
3485 | | // Handle case for layers, scene change, and resizing. |
3486 | 0 | static AOM_INLINE int selective_disable_cdf_rtc(const AV1_COMP *cpi) { |
3487 | 0 | const AV1_COMMON *const cm = &cpi->common; |
3488 | 0 | const RATE_CONTROL *const rc = &cpi->rc; |
3489 | | // For single layer. |
3490 | 0 | if (cpi->svc.number_spatial_layers == 1 && |
3491 | 0 | cpi->svc.number_temporal_layers == 1) { |
3492 | | // Don't disable on intra_only, scene change (high_source_sad = 1), |
3493 | | // or resized frame. To avoid quality loss force enable at |
3494 | | // for ~30 frames after key or scene/slide change, and |
3495 | | // after 8 frames since last update if frame_source_sad > 0. |
3496 | 0 | if (frame_is_intra_only(cm) || is_frame_resize_pending(cpi) || |
3497 | 0 | rc->high_source_sad || rc->frames_since_key < 30 || |
3498 | 0 | (cpi->oxcf.q_cfg.aq_mode == CYCLIC_REFRESH_AQ && |
3499 | 0 | cpi->cyclic_refresh->counter_encode_maxq_scene_change < 30) || |
3500 | 0 | (cpi->frames_since_last_update > 8 && cpi->rc.frame_source_sad > 0)) |
3501 | 0 | return 0; |
3502 | 0 | else |
3503 | 0 | return 1; |
3504 | 0 | } else if (cpi->svc.number_temporal_layers > 1) { |
3505 | | // Disable only on top temporal enhancement layer for now. |
3506 | 0 | return cpi->svc.temporal_layer_id == cpi->svc.number_temporal_layers - 1; |
3507 | 0 | } |
3508 | 0 | return 1; |
3509 | 0 | } |
3510 | | |
3511 | | #if !CONFIG_REALTIME_ONLY |
3512 | | static void subtract_stats(FIRSTPASS_STATS *section, |
3513 | 0 | const FIRSTPASS_STATS *frame) { |
3514 | 0 | section->frame -= frame->frame; |
3515 | 0 | section->weight -= frame->weight; |
3516 | 0 | section->intra_error -= frame->intra_error; |
3517 | 0 | section->frame_avg_wavelet_energy -= frame->frame_avg_wavelet_energy; |
3518 | 0 | section->coded_error -= frame->coded_error; |
3519 | 0 | section->sr_coded_error -= frame->sr_coded_error; |
3520 | 0 | section->pcnt_inter -= frame->pcnt_inter; |
3521 | 0 | section->pcnt_motion -= frame->pcnt_motion; |
3522 | 0 | section->pcnt_second_ref -= frame->pcnt_second_ref; |
3523 | 0 | section->pcnt_neutral -= frame->pcnt_neutral; |
3524 | 0 | section->intra_skip_pct -= frame->intra_skip_pct; |
3525 | 0 | section->inactive_zone_rows -= frame->inactive_zone_rows; |
3526 | 0 | section->inactive_zone_cols -= frame->inactive_zone_cols; |
3527 | 0 | section->MVr -= frame->MVr; |
3528 | 0 | section->mvr_abs -= frame->mvr_abs; |
3529 | 0 | section->MVc -= frame->MVc; |
3530 | 0 | section->mvc_abs -= frame->mvc_abs; |
3531 | 0 | section->MVrv -= frame->MVrv; |
3532 | 0 | section->MVcv -= frame->MVcv; |
3533 | 0 | section->mv_in_out_count -= frame->mv_in_out_count; |
3534 | 0 | section->new_mv_count -= frame->new_mv_count; |
3535 | 0 | section->count -= frame->count; |
3536 | 0 | section->duration -= frame->duration; |
3537 | 0 | } |
3538 | | |
3539 | 0 | static void calculate_frame_avg_haar_energy(AV1_COMP *cpi) { |
3540 | 0 | TWO_PASS *const twopass = &cpi->ppi->twopass; |
3541 | 0 | const FIRSTPASS_STATS *const total_stats = |
3542 | 0 | twopass->stats_buf_ctx->total_stats; |
3543 | |
|
3544 | 0 | if (is_one_pass_rt_params(cpi) || |
3545 | 0 | (cpi->oxcf.q_cfg.deltaq_mode != DELTA_Q_PERCEPTUAL) || |
3546 | 0 | (is_fp_wavelet_energy_invalid(total_stats) == 0)) |
3547 | 0 | return; |
3548 | | |
3549 | 0 | const int num_mbs = (cpi->oxcf.resize_cfg.resize_mode != RESIZE_NONE) |
3550 | 0 | ? cpi->initial_mbs |
3551 | 0 | : cpi->common.mi_params.MBs; |
3552 | 0 | const YV12_BUFFER_CONFIG *const unfiltered_source = cpi->unfiltered_source; |
3553 | 0 | const uint8_t *const src = unfiltered_source->y_buffer; |
3554 | 0 | const int hbd = unfiltered_source->flags & YV12_FLAG_HIGHBITDEPTH; |
3555 | 0 | const int stride = unfiltered_source->y_stride; |
3556 | 0 | const BLOCK_SIZE fp_block_size = |
3557 | 0 | get_fp_block_size(cpi->is_screen_content_type); |
3558 | 0 | const int fp_block_size_width = block_size_wide[fp_block_size]; |
3559 | 0 | const int fp_block_size_height = block_size_high[fp_block_size]; |
3560 | 0 | const int num_unit_cols = |
3561 | 0 | get_num_blocks(unfiltered_source->y_crop_width, fp_block_size_width); |
3562 | 0 | const int num_unit_rows = |
3563 | 0 | get_num_blocks(unfiltered_source->y_crop_height, fp_block_size_height); |
3564 | 0 | const int num_8x8_cols = num_unit_cols * (fp_block_size_width / 8); |
3565 | 0 | const int num_8x8_rows = num_unit_rows * (fp_block_size_height / 8); |
3566 | 0 | int64_t frame_avg_wavelet_energy = av1_haar_ac_sad_mxn_uint8_input( |
3567 | 0 | src, stride, hbd, num_8x8_rows, num_8x8_cols); |
3568 | |
|
3569 | 0 | cpi->twopass_frame.frame_avg_haar_energy = |
3570 | 0 | log1p((double)frame_avg_wavelet_energy / num_mbs); |
3571 | 0 | } |
3572 | | #endif |
3573 | | |
3574 | | /*!\brief Run the final pass encoding for 1-pass/2-pass encoding mode, and pack |
3575 | | * the bitstream |
3576 | | * |
3577 | | * \ingroup high_level_algo |
3578 | | * \callgraph |
3579 | | * \callergraph |
3580 | | * |
3581 | | * \param[in] cpi Top-level encoder structure |
3582 | | * \param[in] size Bitstream size |
3583 | | * \param[in] dest Bitstream output |
3584 | | * |
3585 | | * \return Returns a value to indicate if the encoding is done successfully. |
3586 | | * \retval #AOM_CODEC_OK |
3587 | | * \retval #AOM_CODEC_ERROR |
3588 | | */ |
3589 | | static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size, |
3590 | 0 | uint8_t *dest) { |
3591 | 0 | AV1_COMMON *const cm = &cpi->common; |
3592 | 0 | SequenceHeader *const seq_params = cm->seq_params; |
3593 | 0 | CurrentFrame *const current_frame = &cm->current_frame; |
3594 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
3595 | 0 | struct segmentation *const seg = &cm->seg; |
3596 | 0 | FeatureFlags *const features = &cm->features; |
3597 | 0 | const TileConfig *const tile_cfg = &oxcf->tile_cfg; |
3598 | 0 | assert(cpi->source != NULL); |
3599 | 0 | cpi->td.mb.e_mbd.cur_buf = cpi->source; |
3600 | |
|
3601 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3602 | | start_timing(cpi, encode_frame_to_data_rate_time); |
3603 | | #endif |
3604 | |
|
3605 | 0 | #if !CONFIG_REALTIME_ONLY |
3606 | 0 | calculate_frame_avg_haar_energy(cpi); |
3607 | 0 | #endif |
3608 | | |
3609 | | // frame type has been decided outside of this function call |
3610 | 0 | cm->cur_frame->frame_type = current_frame->frame_type; |
3611 | |
|
3612 | 0 | cm->tiles.large_scale = tile_cfg->enable_large_scale_tile; |
3613 | 0 | cm->tiles.single_tile_decoding = tile_cfg->enable_single_tile_decoding; |
3614 | |
|
3615 | 0 | features->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm); |
3616 | | // features->allow_ref_frame_mvs needs to be written into the frame header |
3617 | | // while cm->tiles.large_scale is 1, therefore, "cm->tiles.large_scale=1" case |
3618 | | // is separated from frame_might_allow_ref_frame_mvs(). |
3619 | 0 | features->allow_ref_frame_mvs &= !cm->tiles.large_scale; |
3620 | |
|
3621 | 0 | features->allow_warped_motion = oxcf->motion_mode_cfg.allow_warped_motion && |
3622 | 0 | frame_might_allow_warped_motion(cm); |
3623 | |
|
3624 | 0 | cpi->last_frame_type = current_frame->frame_type; |
3625 | |
|
3626 | 0 | if (frame_is_intra_only(cm)) { |
3627 | 0 | cpi->frames_since_last_update = 0; |
3628 | 0 | } |
3629 | |
|
3630 | 0 | if (frame_is_sframe(cm)) { |
3631 | 0 | GF_GROUP *gf_group = &cpi->ppi->gf_group; |
3632 | | // S frame will wipe out any previously encoded altref so we cannot place |
3633 | | // an overlay frame |
3634 | 0 | gf_group->update_type[gf_group->size] = GF_UPDATE; |
3635 | 0 | } |
3636 | |
|
3637 | 0 | if (encode_show_existing_frame(cm)) { |
3638 | | #if CONFIG_RATECTRL_LOG && CONFIG_THREE_PASS && CONFIG_BITRATE_ACCURACY |
3639 | | // TODO(angiebird): Move this into a function. |
3640 | | if (oxcf->pass == AOM_RC_THIRD_PASS) { |
3641 | | int frame_coding_idx = |
3642 | | av1_vbr_rc_frame_coding_idx(&cpi->vbr_rc_info, cpi->gf_frame_index); |
3643 | | rc_log_frame_encode_param( |
3644 | | &cpi->rc_log, frame_coding_idx, 1, 255, |
3645 | | cpi->ppi->gf_group.update_type[cpi->gf_frame_index]); |
3646 | | } |
3647 | | #endif |
3648 | 0 | av1_finalize_encoded_frame(cpi); |
3649 | | // Build the bitstream |
3650 | 0 | int largest_tile_id = 0; // Output from bitstream: unused here |
3651 | 0 | cpi->rc.coefficient_size = 0; |
3652 | 0 | if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK) |
3653 | 0 | return AOM_CODEC_ERROR; |
3654 | | |
3655 | 0 | if (seq_params->frame_id_numbers_present_flag && |
3656 | 0 | current_frame->frame_type == KEY_FRAME) { |
3657 | | // Displaying a forward key-frame, so reset the ref buffer IDs |
3658 | 0 | int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show]; |
3659 | 0 | for (int i = 0; i < REF_FRAMES; i++) |
3660 | 0 | cm->ref_frame_id[i] = display_frame_id; |
3661 | 0 | } |
3662 | |
|
3663 | | #if DUMP_RECON_FRAMES == 1 |
3664 | | // NOTE(zoeliu): For debug - Output the filtered reconstructed video. |
3665 | | av1_dump_filtered_recon_frames(cpi); |
3666 | | #endif // DUMP_RECON_FRAMES |
3667 | | |
3668 | | // NOTE: Save the new show frame buffer index for --test-code=warn, i.e., |
3669 | | // for the purpose to verify no mismatch between encoder and decoder. |
3670 | 0 | if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame; |
3671 | |
|
3672 | | #if CONFIG_AV1_TEMPORAL_DENOISING |
3673 | | av1_denoiser_update_ref_frame(cpi); |
3674 | | #endif |
3675 | | |
3676 | | // Since we allocate a spot for the OVERLAY frame in the gf group, we need |
3677 | | // to do post-encoding update accordingly. |
3678 | 0 | av1_set_target_rate(cpi, cm->width, cm->height); |
3679 | |
|
3680 | 0 | if (is_psnr_calc_enabled(cpi)) { |
3681 | 0 | cpi->source = |
3682 | 0 | realloc_and_scale_source(cpi, cm->cur_frame->buf.y_crop_width, |
3683 | 0 | cm->cur_frame->buf.y_crop_height); |
3684 | 0 | } |
3685 | |
|
3686 | 0 | #if !CONFIG_REALTIME_ONLY |
3687 | 0 | if (cpi->use_ducky_encode) { |
3688 | 0 | PSNR_STATS psnr; |
3689 | 0 | aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr); |
3690 | 0 | DuckyEncodeFrameResult *frame_result = |
3691 | 0 | &cpi->ducky_encode_info.frame_result; |
3692 | 0 | frame_result->global_order_idx = cm->cur_frame->display_order_hint; |
3693 | 0 | frame_result->q_index = cm->quant_params.base_qindex; |
3694 | 0 | frame_result->rdmult = cpi->rd.RDMULT; |
3695 | 0 | frame_result->rate = (int)(*size) * 8; |
3696 | 0 | frame_result->dist = psnr.sse[0]; |
3697 | 0 | frame_result->psnr = psnr.psnr[0]; |
3698 | 0 | } |
3699 | 0 | #endif // !CONFIG_REALTIME_ONLY |
3700 | |
|
3701 | 0 | update_counters_for_show_frame(cpi); |
3702 | 0 | return AOM_CODEC_OK; |
3703 | 0 | } |
3704 | | |
3705 | | // Work out whether to force_integer_mv this frame |
3706 | 0 | if (!is_stat_generation_stage(cpi) && |
3707 | 0 | cpi->common.features.allow_screen_content_tools && |
3708 | 0 | !frame_is_intra_only(cm) && !cpi->sf.rt_sf.use_nonrd_pick_mode) { |
3709 | 0 | if (cpi->common.seq_params->force_integer_mv == 2) { |
3710 | | // Adaptive mode: see what previous frame encoded did |
3711 | 0 | if (cpi->unscaled_last_source != NULL) { |
3712 | 0 | features->cur_frame_force_integer_mv = av1_is_integer_mv( |
3713 | 0 | cpi->source, cpi->unscaled_last_source, &cpi->force_intpel_info); |
3714 | 0 | } else { |
3715 | 0 | cpi->common.features.cur_frame_force_integer_mv = 0; |
3716 | 0 | } |
3717 | 0 | } else { |
3718 | 0 | cpi->common.features.cur_frame_force_integer_mv = |
3719 | 0 | cpi->common.seq_params->force_integer_mv; |
3720 | 0 | } |
3721 | 0 | } else { |
3722 | 0 | cpi->common.features.cur_frame_force_integer_mv = 0; |
3723 | 0 | } |
3724 | | |
3725 | | // This is used by av1_pack_bitstream. So this needs to be set in case of |
3726 | | // row-mt where the encoding code will use a temporary structure. |
3727 | 0 | cpi->td.mb.e_mbd.cur_frame_force_integer_mv = |
3728 | 0 | cpi->common.features.cur_frame_force_integer_mv; |
3729 | | |
3730 | | // Set default state for segment based loop filter update flags. |
3731 | 0 | cm->lf.mode_ref_delta_update = 0; |
3732 | | |
3733 | | // Set various flags etc to special state if it is a key frame. |
3734 | 0 | if (frame_is_intra_only(cm) || frame_is_sframe(cm)) { |
3735 | | // Reset the loop filter deltas and segmentation map. |
3736 | 0 | av1_reset_segment_features(cm); |
3737 | | |
3738 | | // If segmentation is enabled force a map update for key frames. |
3739 | 0 | if (seg->enabled) { |
3740 | 0 | seg->update_map = 1; |
3741 | 0 | seg->update_data = 1; |
3742 | 0 | } |
3743 | 0 | } |
3744 | 0 | if (tile_cfg->mtu == 0) { |
3745 | 0 | cpi->num_tg = tile_cfg->num_tile_groups; |
3746 | 0 | } else { |
3747 | | // Use a default value for the purposes of weighting costs in probability |
3748 | | // updates |
3749 | 0 | cpi->num_tg = DEFAULT_MAX_NUM_TG; |
3750 | 0 | } |
3751 | | |
3752 | | // For 1 pass CBR mode: check if we are dropping this frame. |
3753 | 0 | if (has_no_stats_stage(cpi) && oxcf->rc_cfg.mode == AOM_CBR) { |
3754 | | // Always drop for spatial enhancement layer if layer bandwidth is 0. |
3755 | | // Otherwise check for frame-dropping based on buffer level in |
3756 | | // av1_rc_drop_frame(). |
3757 | 0 | if ((cpi->svc.spatial_layer_id > 0 && |
3758 | 0 | cpi->oxcf.rc_cfg.target_bandwidth == 0) || |
3759 | 0 | av1_rc_drop_frame(cpi)) { |
3760 | 0 | cpi->is_dropped_frame = true; |
3761 | 0 | } |
3762 | 0 | if (cpi->is_dropped_frame) { |
3763 | 0 | av1_setup_frame_size(cpi); |
3764 | 0 | av1_set_mv_search_params(cpi); |
3765 | 0 | av1_rc_postencode_update_drop_frame(cpi); |
3766 | 0 | release_scaled_references(cpi); |
3767 | 0 | cpi->ppi->gf_group.is_frame_dropped[cpi->gf_frame_index] = true; |
3768 | | // A dropped frame might not be shown but it always takes a slot in the gf |
3769 | | // group. Therefore, even when it is not shown, we still need to update |
3770 | | // the relevant frame counters. |
3771 | 0 | if (cm->show_frame) { |
3772 | 0 | update_counters_for_show_frame(cpi); |
3773 | 0 | } |
3774 | 0 | return AOM_CODEC_OK; |
3775 | 0 | } |
3776 | 0 | } |
3777 | | |
3778 | 0 | if (oxcf->tune_cfg.tuning == AOM_TUNE_SSIM) { |
3779 | 0 | av1_set_mb_ssim_rdmult_scaling(cpi); |
3780 | 0 | } |
3781 | | #if CONFIG_SALIENCY_MAP |
3782 | | else if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_SALIENCY_MAP && |
3783 | | !(cpi->source->flags & YV12_FLAG_HIGHBITDEPTH)) { |
3784 | | if (av1_set_saliency_map(cpi) == 0) { |
3785 | | return AOM_CODEC_MEM_ERROR; |
3786 | | } |
3787 | | #if !CONFIG_REALTIME_ONLY |
3788 | | double motion_ratio = av1_setup_motion_ratio(cpi); |
3789 | | #else |
3790 | | double motion_ratio = 1.0; |
3791 | | #endif |
3792 | | if (av1_setup_sm_rdmult_scaling_factor(cpi, motion_ratio) == 0) { |
3793 | | return AOM_CODEC_MEM_ERROR; |
3794 | | } |
3795 | | } |
3796 | | #endif |
3797 | | #if CONFIG_TUNE_VMAF |
3798 | | else if (oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_WITHOUT_PREPROCESSING || |
3799 | | oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN || |
3800 | | oxcf->tune_cfg.tuning == AOM_TUNE_VMAF_NEG_MAX_GAIN) { |
3801 | | av1_set_mb_vmaf_rdmult_scaling(cpi); |
3802 | | } |
3803 | | #endif |
3804 | |
|
3805 | 0 | if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_PERCEPTUAL_AI && |
3806 | 0 | cpi->sf.rt_sf.use_nonrd_pick_mode == 0) { |
3807 | 0 | av1_init_mb_wiener_var_buffer(cpi); |
3808 | 0 | av1_set_mb_wiener_variance(cpi); |
3809 | 0 | } |
3810 | |
|
3811 | 0 | if (cpi->oxcf.q_cfg.deltaq_mode == DELTA_Q_USER_RATING_BASED) { |
3812 | 0 | av1_init_mb_ur_var_buffer(cpi); |
3813 | 0 | av1_set_mb_ur_variance(cpi); |
3814 | 0 | } |
3815 | |
|
3816 | | #if CONFIG_INTERNAL_STATS |
3817 | | memset(cpi->mode_chosen_counts, 0, |
3818 | | MAX_MODES * sizeof(*cpi->mode_chosen_counts)); |
3819 | | #endif |
3820 | |
|
3821 | 0 | if (seq_params->frame_id_numbers_present_flag) { |
3822 | | /* Non-normative definition of current_frame_id ("frame counter" with |
3823 | | * wraparound) */ |
3824 | 0 | if (cm->current_frame_id == -1) { |
3825 | 0 | int lsb, msb; |
3826 | | /* quasi-random initialization of current_frame_id for a key frame */ |
3827 | 0 | if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) { |
3828 | 0 | lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff; |
3829 | 0 | msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff; |
3830 | 0 | } else { |
3831 | 0 | lsb = cpi->source->y_buffer[0] & 0xff; |
3832 | 0 | msb = cpi->source->y_buffer[1] & 0xff; |
3833 | 0 | } |
3834 | 0 | cm->current_frame_id = |
3835 | 0 | ((msb << 8) + lsb) % (1 << seq_params->frame_id_length); |
3836 | | |
3837 | | // S_frame is meant for stitching different streams of different |
3838 | | // resolutions together, so current_frame_id must be the |
3839 | | // same across different streams of the same content current_frame_id |
3840 | | // should be the same and not random. 0x37 is a chosen number as start |
3841 | | // point |
3842 | 0 | if (oxcf->kf_cfg.sframe_dist != 0) cm->current_frame_id = 0x37; |
3843 | 0 | } else { |
3844 | 0 | cm->current_frame_id = |
3845 | 0 | (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) % |
3846 | 0 | (1 << seq_params->frame_id_length); |
3847 | 0 | } |
3848 | 0 | } |
3849 | |
|
3850 | 0 | switch (oxcf->algo_cfg.cdf_update_mode) { |
3851 | 0 | case 0: // No CDF update for any frames(4~6% compression loss). |
3852 | 0 | features->disable_cdf_update = 1; |
3853 | 0 | break; |
3854 | 0 | case 1: // Enable CDF update for all frames. |
3855 | 0 | if (cpi->sf.rt_sf.disable_cdf_update_non_reference_frame && |
3856 | 0 | cpi->ppi->rtc_ref.non_reference_frame && cpi->rc.frames_since_key > 2) |
3857 | 0 | features->disable_cdf_update = 1; |
3858 | 0 | else if (cpi->sf.rt_sf.selective_cdf_update) |
3859 | 0 | features->disable_cdf_update = selective_disable_cdf_rtc(cpi); |
3860 | 0 | else |
3861 | 0 | features->disable_cdf_update = 0; |
3862 | 0 | break; |
3863 | 0 | case 2: |
3864 | | // Strategically determine at which frames to do CDF update. |
3865 | | // Currently only enable CDF update for all-intra and no-show frames(1.5% |
3866 | | // compression loss) for good qualiy or allintra mode. |
3867 | 0 | if (oxcf->mode == GOOD || oxcf->mode == ALLINTRA) { |
3868 | 0 | features->disable_cdf_update = |
3869 | 0 | (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1; |
3870 | 0 | } else { |
3871 | 0 | features->disable_cdf_update = selective_disable_cdf_rtc(cpi); |
3872 | 0 | } |
3873 | 0 | break; |
3874 | 0 | } |
3875 | | |
3876 | | // Disable cdf update for the INTNL_ARF_UPDATE frame with |
3877 | | // frame_parallel_level 1. |
3878 | 0 | if (!cpi->do_frame_data_update && |
3879 | 0 | cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) { |
3880 | 0 | assert(cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 1); |
3881 | 0 | features->disable_cdf_update = 1; |
3882 | 0 | } |
3883 | |
|
3884 | 0 | #if !CONFIG_REALTIME_ONLY |
3885 | 0 | if (cpi->oxcf.tool_cfg.enable_global_motion && !frame_is_intra_only(cm)) { |
3886 | | // Flush any stale global motion information, which may be left over |
3887 | | // from a previous frame |
3888 | 0 | aom_invalidate_pyramid(cpi->source->y_pyramid); |
3889 | 0 | av1_invalidate_corner_list(cpi->source->corners); |
3890 | 0 | } |
3891 | 0 | #endif // !CONFIG_REALTIME_ONLY |
3892 | |
|
3893 | 0 | int largest_tile_id = 0; |
3894 | 0 | if (av1_superres_in_recode_allowed(cpi)) { |
3895 | 0 | if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) != |
3896 | 0 | AOM_CODEC_OK) { |
3897 | 0 | return AOM_CODEC_ERROR; |
3898 | 0 | } |
3899 | 0 | } else { |
3900 | 0 | const aom_superres_mode orig_superres_mode = cpi->superres_mode; // save |
3901 | 0 | cpi->superres_mode = cpi->oxcf.superres_cfg.superres_mode; |
3902 | 0 | if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL, |
3903 | 0 | &largest_tile_id) != AOM_CODEC_OK) { |
3904 | 0 | return AOM_CODEC_ERROR; |
3905 | 0 | } |
3906 | 0 | cpi->superres_mode = orig_superres_mode; // restore |
3907 | 0 | } |
3908 | | |
3909 | | // Update reference frame ids for reference frames this frame will overwrite |
3910 | 0 | if (seq_params->frame_id_numbers_present_flag) { |
3911 | 0 | for (int i = 0; i < REF_FRAMES; i++) { |
3912 | 0 | if ((current_frame->refresh_frame_flags >> i) & 1) { |
3913 | 0 | cm->ref_frame_id[i] = cm->current_frame_id; |
3914 | 0 | } |
3915 | 0 | } |
3916 | 0 | } |
3917 | |
|
3918 | 0 | if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) |
3919 | 0 | cpi->svc.num_encoded_top_layer++; |
3920 | |
|
3921 | | #if DUMP_RECON_FRAMES == 1 |
3922 | | // NOTE(zoeliu): For debug - Output the filtered reconstructed video. |
3923 | | av1_dump_filtered_recon_frames(cpi); |
3924 | | #endif // DUMP_RECON_FRAMES |
3925 | |
|
3926 | 0 | if (cm->seg.enabled) { |
3927 | 0 | if (cm->seg.update_map == 0 && cm->last_frame_seg_map) { |
3928 | 0 | memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map, |
3929 | 0 | cm->cur_frame->mi_cols * cm->cur_frame->mi_rows * |
3930 | 0 | sizeof(*cm->cur_frame->seg_map)); |
3931 | 0 | } |
3932 | 0 | } |
3933 | |
|
3934 | 0 | int release_scaled_refs = 0; |
3935 | | #if CONFIG_FPMT_TEST |
3936 | | release_scaled_refs = |
3937 | | (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 1 : 0; |
3938 | | #endif // CONFIG_FPMT_TEST |
3939 | 0 | if (release_scaled_refs || |
3940 | 0 | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 0) { |
3941 | 0 | if (frame_is_intra_only(cm) == 0) { |
3942 | 0 | release_scaled_references(cpi); |
3943 | 0 | } |
3944 | 0 | } |
3945 | | #if CONFIG_AV1_TEMPORAL_DENOISING |
3946 | | av1_denoiser_update_ref_frame(cpi); |
3947 | | #endif |
3948 | | |
3949 | | // NOTE: Save the new show frame buffer index for --test-code=warn, i.e., |
3950 | | // for the purpose to verify no mismatch between encoder and decoder. |
3951 | 0 | if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame; |
3952 | |
|
3953 | 0 | if (features->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) { |
3954 | 0 | *cm->fc = cpi->tile_data[largest_tile_id].tctx; |
3955 | 0 | av1_reset_cdf_symbol_counters(cm->fc); |
3956 | 0 | } |
3957 | 0 | if (!cm->tiles.large_scale) { |
3958 | 0 | cm->cur_frame->frame_context = *cm->fc; |
3959 | 0 | } |
3960 | |
|
3961 | 0 | if (tile_cfg->enable_ext_tile_debug) { |
3962 | | // (yunqing) This test ensures the correctness of large scale tile coding. |
3963 | 0 | if (cm->tiles.large_scale && is_stat_consumption_stage(cpi)) { |
3964 | 0 | char fn[20] = "./fc"; |
3965 | 0 | fn[4] = current_frame->frame_number / 100 + '0'; |
3966 | 0 | fn[5] = (current_frame->frame_number % 100) / 10 + '0'; |
3967 | 0 | fn[6] = (current_frame->frame_number % 10) + '0'; |
3968 | 0 | fn[7] = '\0'; |
3969 | 0 | av1_print_frame_contexts(cm->fc, fn); |
3970 | 0 | } |
3971 | 0 | } |
3972 | |
|
3973 | 0 | cpi->last_frame_type = current_frame->frame_type; |
3974 | |
|
3975 | 0 | if (cm->features.disable_cdf_update) { |
3976 | 0 | cpi->frames_since_last_update++; |
3977 | 0 | } else { |
3978 | 0 | cpi->frames_since_last_update = 1; |
3979 | 0 | } |
3980 | |
|
3981 | 0 | if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) |
3982 | 0 | cpi->svc.prev_number_spatial_layers = cpi->svc.number_spatial_layers; |
3983 | | |
3984 | | // Clear the one shot update flags for segmentation map and mode/ref loop |
3985 | | // filter deltas. |
3986 | 0 | cm->seg.update_map = 0; |
3987 | 0 | cm->seg.update_data = 0; |
3988 | 0 | cm->lf.mode_ref_delta_update = 0; |
3989 | |
|
3990 | 0 | if (cm->show_frame) { |
3991 | 0 | update_counters_for_show_frame(cpi); |
3992 | 0 | } |
3993 | |
|
3994 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
3995 | | end_timing(cpi, encode_frame_to_data_rate_time); |
3996 | | #endif |
3997 | |
|
3998 | 0 | return AOM_CODEC_OK; |
3999 | 0 | } |
4000 | | |
4001 | | int av1_encode(AV1_COMP *const cpi, uint8_t *const dest, |
4002 | | const EncodeFrameInput *const frame_input, |
4003 | | const EncodeFrameParams *const frame_params, |
4004 | 0 | EncodeFrameResults *const frame_results) { |
4005 | 0 | AV1_COMMON *const cm = &cpi->common; |
4006 | 0 | CurrentFrame *const current_frame = &cm->current_frame; |
4007 | |
|
4008 | 0 | cpi->unscaled_source = frame_input->source; |
4009 | 0 | cpi->source = frame_input->source; |
4010 | 0 | cpi->unscaled_last_source = frame_input->last_source; |
4011 | |
|
4012 | 0 | current_frame->refresh_frame_flags = frame_params->refresh_frame_flags; |
4013 | 0 | cm->features.error_resilient_mode = frame_params->error_resilient_mode; |
4014 | 0 | cm->features.primary_ref_frame = frame_params->primary_ref_frame; |
4015 | 0 | cm->current_frame.frame_type = frame_params->frame_type; |
4016 | 0 | cm->show_frame = frame_params->show_frame; |
4017 | 0 | cpi->ref_frame_flags = frame_params->ref_frame_flags; |
4018 | 0 | cpi->speed = frame_params->speed; |
4019 | 0 | cm->show_existing_frame = frame_params->show_existing_frame; |
4020 | 0 | cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show; |
4021 | |
|
4022 | 0 | memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx, |
4023 | 0 | REF_FRAMES * sizeof(*cm->remapped_ref_idx)); |
4024 | |
|
4025 | 0 | memcpy(&cpi->refresh_frame, &frame_params->refresh_frame, |
4026 | 0 | sizeof(cpi->refresh_frame)); |
4027 | |
|
4028 | 0 | if (current_frame->frame_type == KEY_FRAME && |
4029 | 0 | cpi->ppi->gf_group.refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) { |
4030 | 0 | current_frame->frame_number = 0; |
4031 | 0 | } |
4032 | |
|
4033 | 0 | current_frame->order_hint = |
4034 | 0 | current_frame->frame_number + frame_params->order_offset; |
4035 | |
|
4036 | 0 | current_frame->display_order_hint = current_frame->order_hint; |
4037 | 0 | current_frame->order_hint %= |
4038 | 0 | (1 << (cm->seq_params->order_hint_info.order_hint_bits_minus_1 + 1)); |
4039 | |
|
4040 | 0 | current_frame->pyramid_level = get_true_pyr_level( |
4041 | 0 | cpi->ppi->gf_group.layer_depth[cpi->gf_frame_index], |
4042 | 0 | current_frame->display_order_hint, cpi->ppi->gf_group.max_layer_depth); |
4043 | |
|
4044 | 0 | if (is_stat_generation_stage(cpi)) { |
4045 | 0 | #if !CONFIG_REALTIME_ONLY |
4046 | 0 | if (cpi->oxcf.q_cfg.use_fixed_qp_offsets) |
4047 | 0 | av1_noop_first_pass_frame(cpi, frame_input->ts_duration); |
4048 | 0 | else |
4049 | 0 | av1_first_pass(cpi, frame_input->ts_duration); |
4050 | 0 | #endif |
4051 | 0 | } else if (cpi->oxcf.pass == AOM_RC_ONE_PASS || |
4052 | 0 | cpi->oxcf.pass >= AOM_RC_SECOND_PASS) { |
4053 | 0 | if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) != |
4054 | 0 | AOM_CODEC_OK) { |
4055 | 0 | return AOM_CODEC_ERROR; |
4056 | 0 | } |
4057 | 0 | } else { |
4058 | 0 | return AOM_CODEC_ERROR; |
4059 | 0 | } |
4060 | | |
4061 | 0 | return AOM_CODEC_OK; |
4062 | 0 | } |
4063 | | |
4064 | | #if CONFIG_DENOISE |
4065 | | static int apply_denoise_2d(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *sd, |
4066 | | int block_size, float noise_level, |
4067 | 0 | int64_t time_stamp, int64_t end_time) { |
4068 | 0 | AV1_COMMON *const cm = &cpi->common; |
4069 | 0 | if (!cpi->denoise_and_model) { |
4070 | 0 | cpi->denoise_and_model = aom_denoise_and_model_alloc( |
4071 | 0 | cm->seq_params->bit_depth, block_size, noise_level); |
4072 | 0 | if (!cpi->denoise_and_model) { |
4073 | 0 | aom_set_error(cm->error, AOM_CODEC_MEM_ERROR, |
4074 | 0 | "Error allocating denoise and model"); |
4075 | 0 | return -1; |
4076 | 0 | } |
4077 | 0 | } |
4078 | 0 | if (!cpi->film_grain_table) { |
4079 | 0 | cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table)); |
4080 | 0 | if (!cpi->film_grain_table) { |
4081 | 0 | aom_set_error(cm->error, AOM_CODEC_MEM_ERROR, |
4082 | 0 | "Error allocating grain table"); |
4083 | 0 | return -1; |
4084 | 0 | } |
4085 | 0 | memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table)); |
4086 | 0 | } |
4087 | 0 | if (aom_denoise_and_model_run(cpi->denoise_and_model, sd, |
4088 | 0 | &cm->film_grain_params, |
4089 | 0 | cpi->oxcf.enable_dnl_denoising)) { |
4090 | 0 | if (cm->film_grain_params.apply_grain) { |
4091 | 0 | aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time, |
4092 | 0 | &cm->film_grain_params); |
4093 | 0 | } |
4094 | 0 | } |
4095 | 0 | return 0; |
4096 | 0 | } |
4097 | | #endif |
4098 | | |
4099 | | int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags, |
4100 | | const YV12_BUFFER_CONFIG *sd, int64_t time_stamp, |
4101 | 0 | int64_t end_time) { |
4102 | 0 | AV1_COMMON *const cm = &cpi->common; |
4103 | 0 | const SequenceHeader *const seq_params = cm->seq_params; |
4104 | 0 | int res = 0; |
4105 | 0 | const int subsampling_x = sd->subsampling_x; |
4106 | 0 | const int subsampling_y = sd->subsampling_y; |
4107 | 0 | const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0; |
4108 | |
|
4109 | | #if CONFIG_TUNE_VMAF |
4110 | | if (!is_stat_generation_stage(cpi) && |
4111 | | cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_WITH_PREPROCESSING) { |
4112 | | av1_vmaf_frame_preprocessing(cpi, sd); |
4113 | | } |
4114 | | if (!is_stat_generation_stage(cpi) && |
4115 | | cpi->oxcf.tune_cfg.tuning == AOM_TUNE_VMAF_MAX_GAIN) { |
4116 | | av1_vmaf_blk_preprocessing(cpi, sd); |
4117 | | } |
4118 | | #endif |
4119 | |
|
4120 | | #if CONFIG_INTERNAL_STATS |
4121 | | struct aom_usec_timer timer; |
4122 | | aom_usec_timer_start(&timer); |
4123 | | #endif |
4124 | |
|
4125 | | #if CONFIG_AV1_TEMPORAL_DENOISING |
4126 | | setup_denoiser_buffer(cpi); |
4127 | | #endif |
4128 | |
|
4129 | 0 | #if CONFIG_DENOISE |
4130 | | // even if denoise_noise_level is > 0, we don't need need to denoise on pass |
4131 | | // 1 of 2 if enable_dnl_denoising is disabled since the 2nd pass will be |
4132 | | // encoding the original (non-denoised) frame |
4133 | 0 | if (cpi->oxcf.noise_level > 0 && !(cpi->oxcf.pass == AOM_RC_FIRST_PASS && |
4134 | 0 | !cpi->oxcf.enable_dnl_denoising)) { |
4135 | 0 | #if !CONFIG_REALTIME_ONLY |
4136 | | // Choose a synthetic noise level for still images for enhanced perceptual |
4137 | | // quality based on an estimated noise level in the source, but only if |
4138 | | // the noise level is set on the command line to > 0. |
4139 | 0 | if (cpi->oxcf.mode == ALLINTRA) { |
4140 | | // No noise synthesis if source is very clean. |
4141 | | // Uses a low edge threshold to focus on smooth areas. |
4142 | | // Increase output noise setting a little compared to measured value. |
4143 | 0 | double y_noise_level = 0.0; |
4144 | 0 | av1_estimate_noise_level(sd, &y_noise_level, AOM_PLANE_Y, AOM_PLANE_Y, |
4145 | 0 | cm->seq_params->bit_depth, 16); |
4146 | 0 | cpi->oxcf.noise_level = (float)(y_noise_level - 0.1); |
4147 | 0 | cpi->oxcf.noise_level = (float)AOMMAX(0.0, cpi->oxcf.noise_level); |
4148 | 0 | if (cpi->oxcf.noise_level > 0.0) { |
4149 | 0 | cpi->oxcf.noise_level += (float)0.5; |
4150 | 0 | } |
4151 | 0 | cpi->oxcf.noise_level = (float)AOMMIN(5.0, cpi->oxcf.noise_level); |
4152 | 0 | } |
4153 | 0 | #endif |
4154 | |
|
4155 | 0 | if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size, |
4156 | 0 | cpi->oxcf.noise_level, time_stamp, end_time) < 0) |
4157 | 0 | res = -1; |
4158 | 0 | } |
4159 | 0 | #endif // CONFIG_DENOISE |
4160 | |
|
4161 | 0 | if (av1_lookahead_push(cpi->ppi->lookahead, sd, time_stamp, end_time, |
4162 | 0 | use_highbitdepth, cpi->alloc_pyramid, frame_flags)) { |
4163 | 0 | aom_set_error(cm->error, AOM_CODEC_ERROR, "av1_lookahead_push() failed"); |
4164 | 0 | res = -1; |
4165 | 0 | } |
4166 | | #if CONFIG_INTERNAL_STATS |
4167 | | aom_usec_timer_mark(&timer); |
4168 | | cpi->ppi->total_time_receive_data += aom_usec_timer_elapsed(&timer); |
4169 | | #endif |
4170 | | |
4171 | | // Note: Regarding profile setting, the following checks are added to help |
4172 | | // choose a proper profile for the input video. The criterion is that all |
4173 | | // bitstreams must be designated as the lowest profile that match its content. |
4174 | | // E.G. A bitstream that contains 4:4:4 video must be designated as High |
4175 | | // Profile in the seq header, and likewise a bitstream that contains 4:2:2 |
4176 | | // bitstream must be designated as Professional Profile in the sequence |
4177 | | // header. |
4178 | 0 | if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome && |
4179 | 0 | (subsampling_x != 1 || subsampling_y != 1)) { |
4180 | 0 | aom_set_error(cm->error, AOM_CODEC_INVALID_PARAM, |
4181 | 0 | "Non-4:2:0 color format requires profile 1 or 2"); |
4182 | 0 | res = -1; |
4183 | 0 | } |
4184 | 0 | if ((seq_params->profile == PROFILE_1) && |
4185 | 0 | !(subsampling_x == 0 && subsampling_y == 0)) { |
4186 | 0 | aom_set_error(cm->error, AOM_CODEC_INVALID_PARAM, |
4187 | 0 | "Profile 1 requires 4:4:4 color format"); |
4188 | 0 | res = -1; |
4189 | 0 | } |
4190 | 0 | if ((seq_params->profile == PROFILE_2) && |
4191 | 0 | (seq_params->bit_depth <= AOM_BITS_10) && |
4192 | 0 | !(subsampling_x == 1 && subsampling_y == 0)) { |
4193 | 0 | aom_set_error(cm->error, AOM_CODEC_INVALID_PARAM, |
4194 | 0 | "Profile 2 bit-depth <= 10 requires 4:2:2 color format"); |
4195 | 0 | res = -1; |
4196 | 0 | } |
4197 | |
|
4198 | 0 | return res; |
4199 | 0 | } |
4200 | | |
4201 | | #if CONFIG_ENTROPY_STATS |
4202 | | void print_entropy_stats(AV1_PRIMARY *const ppi) { |
4203 | | if (!ppi->cpi) return; |
4204 | | |
4205 | | if (ppi->cpi->oxcf.pass != 1 && |
4206 | | ppi->cpi->common.current_frame.frame_number > 0) { |
4207 | | fprintf(stderr, "Writing counts.stt\n"); |
4208 | | FILE *f = fopen("counts.stt", "wb"); |
4209 | | fwrite(&ppi->aggregate_fc, sizeof(ppi->aggregate_fc), 1, f); |
4210 | | fclose(f); |
4211 | | } |
4212 | | } |
4213 | | #endif // CONFIG_ENTROPY_STATS |
4214 | | |
4215 | | #if CONFIG_INTERNAL_STATS |
4216 | | static void adjust_image_stat(double y, double u, double v, double all, |
4217 | | ImageStat *s) { |
4218 | | s->stat[STAT_Y] += y; |
4219 | | s->stat[STAT_U] += u; |
4220 | | s->stat[STAT_V] += v; |
4221 | | s->stat[STAT_ALL] += all; |
4222 | | s->worst = AOMMIN(s->worst, all); |
4223 | | } |
4224 | | |
4225 | | static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) { |
4226 | | AV1_PRIMARY *const ppi = cpi->ppi; |
4227 | | AV1_COMMON *const cm = &cpi->common; |
4228 | | double samples = 0.0; |
4229 | | const uint32_t in_bit_depth = cpi->oxcf.input_cfg.input_bit_depth; |
4230 | | const uint32_t bit_depth = cpi->td.mb.e_mbd.bd; |
4231 | | |
4232 | | if (cpi->ppi->use_svc && |
4233 | | cpi->svc.spatial_layer_id < cpi->svc.number_spatial_layers - 1) |
4234 | | return; |
4235 | | |
4236 | | #if CONFIG_INTER_STATS_ONLY |
4237 | | if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame |
4238 | | #endif |
4239 | | cpi->bytes += frame_bytes; |
4240 | | if (cm->show_frame) { |
4241 | | const YV12_BUFFER_CONFIG *orig = cpi->source; |
4242 | | const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf; |
4243 | | double y, u, v, frame_all; |
4244 | | |
4245 | | ppi->count[0]++; |
4246 | | ppi->count[1]++; |
4247 | | if (cpi->ppi->b_calculate_psnr) { |
4248 | | PSNR_STATS psnr; |
4249 | | double weight[2] = { 0.0, 0.0 }; |
4250 | | double frame_ssim2[2] = { 0.0, 0.0 }; |
4251 | | #if CONFIG_AV1_HIGHBITDEPTH |
4252 | | aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth); |
4253 | | #else |
4254 | | aom_calc_psnr(orig, recon, &psnr); |
4255 | | #endif |
4256 | | adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0], |
4257 | | &(ppi->psnr[0])); |
4258 | | ppi->total_sq_error[0] += psnr.sse[0]; |
4259 | | ppi->total_samples[0] += psnr.samples[0]; |
4260 | | samples = psnr.samples[0]; |
4261 | | |
4262 | | aom_calc_ssim(orig, recon, bit_depth, in_bit_depth, |
4263 | | cm->seq_params->use_highbitdepth, weight, frame_ssim2); |
4264 | | |
4265 | | ppi->worst_ssim = AOMMIN(ppi->worst_ssim, frame_ssim2[0]); |
4266 | | ppi->summed_quality += frame_ssim2[0] * weight[0]; |
4267 | | ppi->summed_weights += weight[0]; |
4268 | | |
4269 | | #if CONFIG_AV1_HIGHBITDEPTH |
4270 | | // Compute PSNR based on stream bit depth |
4271 | | if ((cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) && |
4272 | | (in_bit_depth < bit_depth)) { |
4273 | | adjust_image_stat(psnr.psnr_hbd[1], psnr.psnr_hbd[2], psnr.psnr_hbd[3], |
4274 | | psnr.psnr_hbd[0], &ppi->psnr[1]); |
4275 | | ppi->total_sq_error[1] += psnr.sse_hbd[0]; |
4276 | | ppi->total_samples[1] += psnr.samples_hbd[0]; |
4277 | | |
4278 | | ppi->worst_ssim_hbd = AOMMIN(ppi->worst_ssim_hbd, frame_ssim2[1]); |
4279 | | ppi->summed_quality_hbd += frame_ssim2[1] * weight[1]; |
4280 | | ppi->summed_weights_hbd += weight[1]; |
4281 | | } |
4282 | | #endif |
4283 | | |
4284 | | #if 0 |
4285 | | { |
4286 | | FILE *f = fopen("q_used.stt", "a"); |
4287 | | double y2 = psnr.psnr[1]; |
4288 | | double u2 = psnr.psnr[2]; |
4289 | | double v2 = psnr.psnr[3]; |
4290 | | double frame_psnr2 = psnr.psnr[0]; |
4291 | | fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n", |
4292 | | cm->current_frame.frame_number, y2, u2, v2, |
4293 | | frame_psnr2, frame_ssim2); |
4294 | | fclose(f); |
4295 | | } |
4296 | | #endif |
4297 | | } |
4298 | | if (ppi->b_calculate_blockiness) { |
4299 | | if (!cm->seq_params->use_highbitdepth) { |
4300 | | const double frame_blockiness = |
4301 | | av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer, |
4302 | | recon->y_stride, orig->y_width, orig->y_height); |
4303 | | ppi->worst_blockiness = AOMMAX(ppi->worst_blockiness, frame_blockiness); |
4304 | | ppi->total_blockiness += frame_blockiness; |
4305 | | } |
4306 | | |
4307 | | if (ppi->b_calculate_consistency) { |
4308 | | if (!cm->seq_params->use_highbitdepth) { |
4309 | | const double this_inconsistency = aom_get_ssim_metrics( |
4310 | | orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride, |
4311 | | orig->y_width, orig->y_height, ppi->ssim_vars, &ppi->metrics, 1); |
4312 | | |
4313 | | const double peak = (double)((1 << in_bit_depth) - 1); |
4314 | | const double consistency = |
4315 | | aom_sse_to_psnr(samples, peak, ppi->total_inconsistency); |
4316 | | if (consistency > 0.0) |
4317 | | ppi->worst_consistency = |
4318 | | AOMMIN(ppi->worst_consistency, consistency); |
4319 | | ppi->total_inconsistency += this_inconsistency; |
4320 | | } |
4321 | | } |
4322 | | } |
4323 | | |
4324 | | frame_all = |
4325 | | aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth); |
4326 | | adjust_image_stat(y, u, v, frame_all, &ppi->fastssim); |
4327 | | frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth); |
4328 | | adjust_image_stat(y, u, v, frame_all, &ppi->psnrhvs); |
4329 | | } |
4330 | | } |
4331 | | |
4332 | | void print_internal_stats(AV1_PRIMARY *ppi) { |
4333 | | if (!ppi->cpi) return; |
4334 | | AV1_COMP *const cpi = ppi->cpi; |
4335 | | |
4336 | | if (ppi->cpi->oxcf.pass != 1 && |
4337 | | ppi->cpi->common.current_frame.frame_number > 0) { |
4338 | | char headings[512] = { 0 }; |
4339 | | char results[512] = { 0 }; |
4340 | | FILE *f = fopen("opsnr.stt", "a"); |
4341 | | double time_encoded = |
4342 | | (cpi->time_stamps.prev_ts_end - cpi->time_stamps.first_ts_start) / |
4343 | | 10000000.000; |
4344 | | double total_encode_time = |
4345 | | (ppi->total_time_receive_data + ppi->total_time_compress_data) / |
4346 | | 1000.000; |
4347 | | const double dr = |
4348 | | (double)ppi->total_bytes * (double)8 / (double)1000 / time_encoded; |
4349 | | const double peak = |
4350 | | (double)((1 << ppi->cpi->oxcf.input_cfg.input_bit_depth) - 1); |
4351 | | const double target_rate = |
4352 | | (double)ppi->cpi->oxcf.rc_cfg.target_bandwidth / 1000; |
4353 | | const double rate_err = ((100.0 * (dr - target_rate)) / target_rate); |
4354 | | |
4355 | | if (ppi->b_calculate_psnr) { |
4356 | | const double total_psnr = aom_sse_to_psnr( |
4357 | | (double)ppi->total_samples[0], peak, (double)ppi->total_sq_error[0]); |
4358 | | const double total_ssim = |
4359 | | 100 * pow(ppi->summed_quality / ppi->summed_weights, 8.0); |
4360 | | snprintf(headings, sizeof(headings), |
4361 | | "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t" |
4362 | | "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t" |
4363 | | "WstPsnr\tWstSsim\tWstFast\tWstHVS\t" |
4364 | | "AVPsrnY\tAPsnrCb\tAPsnrCr"); |
4365 | | snprintf(results, sizeof(results), |
4366 | | "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t" |
4367 | | "%7.3f\t%7.3f\t%7.3f\t%7.3f\t" |
4368 | | "%7.3f\t%7.3f\t%7.3f\t%7.3f\t" |
4369 | | "%7.3f\t%7.3f\t%7.3f", |
4370 | | dr, ppi->psnr[0].stat[STAT_ALL] / ppi->count[0], total_psnr, |
4371 | | ppi->psnr[0].stat[STAT_ALL] / ppi->count[0], total_psnr, |
4372 | | total_ssim, total_ssim, |
4373 | | ppi->fastssim.stat[STAT_ALL] / ppi->count[0], |
4374 | | ppi->psnrhvs.stat[STAT_ALL] / ppi->count[0], ppi->psnr[0].worst, |
4375 | | ppi->worst_ssim, ppi->fastssim.worst, ppi->psnrhvs.worst, |
4376 | | ppi->psnr[0].stat[STAT_Y] / ppi->count[0], |
4377 | | ppi->psnr[0].stat[STAT_U] / ppi->count[0], |
4378 | | ppi->psnr[0].stat[STAT_V] / ppi->count[0]); |
4379 | | |
4380 | | if (ppi->b_calculate_blockiness) { |
4381 | | SNPRINT(headings, "\t Block\tWstBlck"); |
4382 | | SNPRINT2(results, "\t%7.3f", ppi->total_blockiness / ppi->count[0]); |
4383 | | SNPRINT2(results, "\t%7.3f", ppi->worst_blockiness); |
4384 | | } |
4385 | | |
4386 | | if (ppi->b_calculate_consistency) { |
4387 | | double consistency = |
4388 | | aom_sse_to_psnr((double)ppi->total_samples[0], peak, |
4389 | | (double)ppi->total_inconsistency); |
4390 | | |
4391 | | SNPRINT(headings, "\tConsist\tWstCons"); |
4392 | | SNPRINT2(results, "\t%7.3f", consistency); |
4393 | | SNPRINT2(results, "\t%7.3f", ppi->worst_consistency); |
4394 | | } |
4395 | | |
4396 | | SNPRINT(headings, "\t Time\tRcErr\tAbsErr"); |
4397 | | SNPRINT2(results, "\t%8.0f", total_encode_time); |
4398 | | SNPRINT2(results, " %7.2f", rate_err); |
4399 | | SNPRINT2(results, " %7.2f", fabs(rate_err)); |
4400 | | |
4401 | | SNPRINT(headings, "\tAPsnr611"); |
4402 | | SNPRINT2(results, " %7.3f", |
4403 | | (6 * ppi->psnr[0].stat[STAT_Y] + ppi->psnr[0].stat[STAT_U] + |
4404 | | ppi->psnr[0].stat[STAT_V]) / |
4405 | | (ppi->count[0] * 8)); |
4406 | | |
4407 | | #if CONFIG_AV1_HIGHBITDEPTH |
4408 | | const uint32_t in_bit_depth = ppi->cpi->oxcf.input_cfg.input_bit_depth; |
4409 | | const uint32_t bit_depth = ppi->seq_params.bit_depth; |
4410 | | // Since cpi->source->flags is not available here, but total_samples[1] |
4411 | | // will be non-zero if cpi->source->flags & YV12_FLAG_HIGHBITDEPTH was |
4412 | | // true in compute_internal_stats |
4413 | | if ((ppi->total_samples[1] > 0) && (in_bit_depth < bit_depth)) { |
4414 | | const double peak_hbd = (double)((1 << bit_depth) - 1); |
4415 | | const double total_psnr_hbd = |
4416 | | aom_sse_to_psnr((double)ppi->total_samples[1], peak_hbd, |
4417 | | (double)ppi->total_sq_error[1]); |
4418 | | const double total_ssim_hbd = |
4419 | | 100 * pow(ppi->summed_quality_hbd / ppi->summed_weights_hbd, 8.0); |
4420 | | SNPRINT(headings, |
4421 | | "\t AVGPsnrH GLBPsnrH AVPsnrPH GLPsnrPH" |
4422 | | " AVPsnrYH APsnrCbH APsnrCrH WstPsnrH" |
4423 | | " AOMSSIMH VPSSIMPH WstSsimH"); |
4424 | | SNPRINT2(results, "\t%7.3f", |
4425 | | ppi->psnr[1].stat[STAT_ALL] / ppi->count[1]); |
4426 | | SNPRINT2(results, " %7.3f", total_psnr_hbd); |
4427 | | SNPRINT2(results, " %7.3f", |
4428 | | ppi->psnr[1].stat[STAT_ALL] / ppi->count[1]); |
4429 | | SNPRINT2(results, " %7.3f", total_psnr_hbd); |
4430 | | SNPRINT2(results, " %7.3f", ppi->psnr[1].stat[STAT_Y] / ppi->count[1]); |
4431 | | SNPRINT2(results, " %7.3f", ppi->psnr[1].stat[STAT_U] / ppi->count[1]); |
4432 | | SNPRINT2(results, " %7.3f", ppi->psnr[1].stat[STAT_V] / ppi->count[1]); |
4433 | | SNPRINT2(results, " %7.3f", ppi->psnr[1].worst); |
4434 | | SNPRINT2(results, " %7.3f", total_ssim_hbd); |
4435 | | SNPRINT2(results, " %7.3f", total_ssim_hbd); |
4436 | | SNPRINT2(results, " %7.3f", ppi->worst_ssim_hbd); |
4437 | | } |
4438 | | #endif |
4439 | | fprintf(f, "%s\n", headings); |
4440 | | fprintf(f, "%s\n", results); |
4441 | | } |
4442 | | |
4443 | | fclose(f); |
4444 | | |
4445 | | aom_free(ppi->ssim_vars); |
4446 | | ppi->ssim_vars = NULL; |
4447 | | } |
4448 | | } |
4449 | | #endif // CONFIG_INTERNAL_STATS |
4450 | | |
4451 | 0 | static AOM_INLINE void update_keyframe_counters(AV1_COMP *cpi) { |
4452 | 0 | if (cpi->common.show_frame && cpi->rc.frames_to_key) { |
4453 | 0 | #if !CONFIG_REALTIME_ONLY |
4454 | 0 | FIRSTPASS_INFO *firstpass_info = &cpi->ppi->twopass.firstpass_info; |
4455 | 0 | if (firstpass_info->past_stats_count > FIRSTPASS_INFO_STATS_PAST_MIN) { |
4456 | 0 | av1_firstpass_info_move_cur_index_and_pop(firstpass_info); |
4457 | 0 | } else { |
4458 | | // When there is not enough past stats, we move the current |
4459 | | // index without popping the past stats |
4460 | 0 | av1_firstpass_info_move_cur_index(firstpass_info); |
4461 | 0 | } |
4462 | 0 | #endif |
4463 | 0 | if (cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { |
4464 | 0 | cpi->rc.frames_since_key++; |
4465 | 0 | cpi->rc.frames_to_key--; |
4466 | 0 | cpi->rc.frames_to_fwd_kf--; |
4467 | 0 | } |
4468 | 0 | } |
4469 | 0 | } |
4470 | | |
4471 | 0 | static AOM_INLINE void update_frames_till_gf_update(AV1_COMP *cpi) { |
4472 | | // TODO(weitinglin): Updating this counter for is_frame_droppable |
4473 | | // is a work-around to handle the condition when a frame is drop. |
4474 | | // We should fix the cpi->common.show_frame flag |
4475 | | // instead of checking the other condition to update the counter properly. |
4476 | 0 | if (cpi->common.show_frame || |
4477 | 0 | is_frame_droppable(&cpi->ppi->rtc_ref, &cpi->ext_flags.refresh_frame)) { |
4478 | | // Decrement count down till next gf |
4479 | 0 | if (cpi->rc.frames_till_gf_update_due > 0) |
4480 | 0 | cpi->rc.frames_till_gf_update_due--; |
4481 | 0 | } |
4482 | 0 | } |
4483 | | |
4484 | 0 | static AOM_INLINE void update_gf_group_index(AV1_COMP *cpi) { |
4485 | | // Increment the gf group index ready for the next frame. |
4486 | 0 | if (is_one_pass_rt_params(cpi) && |
4487 | 0 | cpi->svc.spatial_layer_id == cpi->svc.number_spatial_layers - 1) { |
4488 | 0 | ++cpi->gf_frame_index; |
4489 | | // Reset gf_frame_index in case it reaches MAX_STATIC_GF_GROUP_LENGTH |
4490 | | // for real time encoding. |
4491 | 0 | if (cpi->gf_frame_index == MAX_STATIC_GF_GROUP_LENGTH) |
4492 | 0 | cpi->gf_frame_index = 0; |
4493 | 0 | } else { |
4494 | 0 | ++cpi->gf_frame_index; |
4495 | 0 | } |
4496 | 0 | } |
4497 | | |
4498 | | static void update_fb_of_context_type(const AV1_COMP *const cpi, |
4499 | 0 | int *const fb_of_context_type) { |
4500 | 0 | const AV1_COMMON *const cm = &cpi->common; |
4501 | 0 | const int current_frame_ref_type = get_current_frame_ref_type(cpi); |
4502 | |
|
4503 | 0 | if (frame_is_intra_only(cm) || cm->features.error_resilient_mode || |
4504 | 0 | cpi->ext_flags.use_primary_ref_none) { |
4505 | 0 | for (int i = 0; i < REF_FRAMES; i++) { |
4506 | 0 | fb_of_context_type[i] = -1; |
4507 | 0 | } |
4508 | 0 | fb_of_context_type[current_frame_ref_type] = |
4509 | 0 | cm->show_frame ? get_ref_frame_map_idx(cm, GOLDEN_FRAME) |
4510 | 0 | : get_ref_frame_map_idx(cm, ALTREF_FRAME); |
4511 | 0 | } |
4512 | |
|
4513 | 0 | if (!encode_show_existing_frame(cm)) { |
4514 | | // Refresh fb_of_context_type[]: see encoder.h for explanation |
4515 | 0 | if (cm->current_frame.frame_type == KEY_FRAME) { |
4516 | | // All ref frames are refreshed, pick one that will live long enough |
4517 | 0 | fb_of_context_type[current_frame_ref_type] = 0; |
4518 | 0 | } else { |
4519 | | // If more than one frame is refreshed, it doesn't matter which one we |
4520 | | // pick so pick the first. LST sometimes doesn't refresh any: this is ok |
4521 | |
|
4522 | 0 | for (int i = 0; i < REF_FRAMES; i++) { |
4523 | 0 | if (cm->current_frame.refresh_frame_flags & (1 << i)) { |
4524 | 0 | fb_of_context_type[current_frame_ref_type] = i; |
4525 | 0 | break; |
4526 | 0 | } |
4527 | 0 | } |
4528 | 0 | } |
4529 | 0 | } |
4530 | 0 | } |
4531 | | |
4532 | 0 | static void update_rc_counts(AV1_COMP *cpi) { |
4533 | 0 | update_keyframe_counters(cpi); |
4534 | 0 | update_frames_till_gf_update(cpi); |
4535 | 0 | update_gf_group_index(cpi); |
4536 | 0 | } |
4537 | | |
4538 | 0 | static void update_end_of_frame_stats(AV1_COMP *cpi) { |
4539 | 0 | if (cpi->do_frame_data_update) { |
4540 | | // Store current frame loopfilter levels in ppi, if update flag is set. |
4541 | 0 | if (!cpi->common.show_existing_frame) { |
4542 | 0 | AV1_COMMON *const cm = &cpi->common; |
4543 | 0 | struct loopfilter *const lf = &cm->lf; |
4544 | 0 | cpi->ppi->filter_level[0] = lf->filter_level[0]; |
4545 | 0 | cpi->ppi->filter_level[1] = lf->filter_level[1]; |
4546 | 0 | cpi->ppi->filter_level_u = lf->filter_level_u; |
4547 | 0 | cpi->ppi->filter_level_v = lf->filter_level_v; |
4548 | 0 | } |
4549 | 0 | } |
4550 | | // Store frame level mv_stats from cpi to ppi. |
4551 | 0 | cpi->ppi->mv_stats = cpi->mv_stats; |
4552 | 0 | } |
4553 | | |
4554 | | // Updates frame level stats related to global motion |
4555 | 0 | static AOM_INLINE void update_gm_stats(AV1_COMP *cpi) { |
4556 | 0 | FRAME_UPDATE_TYPE update_type = |
4557 | 0 | cpi->ppi->gf_group.update_type[cpi->gf_frame_index]; |
4558 | 0 | int i, is_gm_present = 0; |
4559 | | |
4560 | | // Check if the current frame has any valid global motion model across its |
4561 | | // reference frames |
4562 | 0 | for (i = 0; i < REF_FRAMES; i++) { |
4563 | 0 | if (cpi->common.global_motion[i].wmtype != IDENTITY) { |
4564 | 0 | is_gm_present = 1; |
4565 | 0 | break; |
4566 | 0 | } |
4567 | 0 | } |
4568 | 0 | int update_actual_stats = 1; |
4569 | | #if CONFIG_FPMT_TEST |
4570 | | update_actual_stats = |
4571 | | (cpi->ppi->fpmt_unit_test_cfg == PARALLEL_SIMULATION_ENCODE) ? 0 : 1; |
4572 | | if (!update_actual_stats) { |
4573 | | if (cpi->ppi->temp_valid_gm_model_found[update_type] == INT32_MAX) { |
4574 | | cpi->ppi->temp_valid_gm_model_found[update_type] = is_gm_present; |
4575 | | } else { |
4576 | | cpi->ppi->temp_valid_gm_model_found[update_type] |= is_gm_present; |
4577 | | } |
4578 | | int show_existing_between_parallel_frames = |
4579 | | (cpi->ppi->gf_group.update_type[cpi->gf_frame_index] == |
4580 | | INTNL_OVERLAY_UPDATE && |
4581 | | cpi->ppi->gf_group.frame_parallel_level[cpi->gf_frame_index + 1] == 2); |
4582 | | if (cpi->do_frame_data_update == 1 && |
4583 | | !show_existing_between_parallel_frames) { |
4584 | | for (i = 0; i < FRAME_UPDATE_TYPES; i++) { |
4585 | | cpi->ppi->valid_gm_model_found[i] = |
4586 | | cpi->ppi->temp_valid_gm_model_found[i]; |
4587 | | } |
4588 | | } |
4589 | | } |
4590 | | #endif |
4591 | 0 | if (update_actual_stats) { |
4592 | 0 | if (cpi->ppi->valid_gm_model_found[update_type] == INT32_MAX) { |
4593 | 0 | cpi->ppi->valid_gm_model_found[update_type] = is_gm_present; |
4594 | 0 | } else { |
4595 | 0 | cpi->ppi->valid_gm_model_found[update_type] |= is_gm_present; |
4596 | 0 | } |
4597 | 0 | } |
4598 | 0 | } |
4599 | | |
4600 | | void av1_post_encode_updates(AV1_COMP *const cpi, |
4601 | 0 | const AV1_COMP_DATA *const cpi_data) { |
4602 | 0 | AV1_PRIMARY *const ppi = cpi->ppi; |
4603 | 0 | AV1_COMMON *const cm = &cpi->common; |
4604 | |
|
4605 | 0 | update_gm_stats(cpi); |
4606 | |
|
4607 | 0 | #if !CONFIG_REALTIME_ONLY |
4608 | | // Update the total stats remaining structure. |
4609 | 0 | if (cpi->twopass_frame.this_frame != NULL && |
4610 | 0 | ppi->twopass.stats_buf_ctx->total_left_stats) { |
4611 | 0 | subtract_stats(ppi->twopass.stats_buf_ctx->total_left_stats, |
4612 | 0 | cpi->twopass_frame.this_frame); |
4613 | 0 | } |
4614 | 0 | #endif |
4615 | |
|
4616 | | #if CONFIG_OUTPUT_FRAME_SIZE |
4617 | | FILE *f = fopen("frame_sizes.csv", "a"); |
4618 | | fprintf(f, "%d,", 8 * (int)cpi_data->frame_size); |
4619 | | fprintf(f, "%d\n", cm->quant_params.base_qindex); |
4620 | | fclose(f); |
4621 | | #endif // CONFIG_OUTPUT_FRAME_SIZE |
4622 | |
|
4623 | 0 | if (!is_stat_generation_stage(cpi) && !cpi->is_dropped_frame) { |
4624 | | // Before calling refresh_reference_frames(), copy ppi->ref_frame_map_copy |
4625 | | // to cm->ref_frame_map for frame_parallel_level 2 frame in a parallel |
4626 | | // encode set of lower layer frames. |
4627 | | // TODO(Remya): Move ref_frame_map from AV1_COMMON to AV1_PRIMARY to avoid |
4628 | | // copy. |
4629 | 0 | if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 2 && |
4630 | 0 | ppi->gf_group.frame_parallel_level[cpi->gf_frame_index - 1] == 1 && |
4631 | 0 | ppi->gf_group.update_type[cpi->gf_frame_index - 1] == |
4632 | 0 | INTNL_ARF_UPDATE) { |
4633 | 0 | memcpy(cm->ref_frame_map, ppi->ref_frame_map_copy, |
4634 | 0 | sizeof(cm->ref_frame_map)); |
4635 | 0 | } |
4636 | 0 | refresh_reference_frames(cpi); |
4637 | | // For frame_parallel_level 1 frame in a parallel encode set of lower layer |
4638 | | // frames, store the updated cm->ref_frame_map in ppi->ref_frame_map_copy. |
4639 | 0 | if (ppi->gf_group.frame_parallel_level[cpi->gf_frame_index] == 1 && |
4640 | 0 | ppi->gf_group.update_type[cpi->gf_frame_index] == INTNL_ARF_UPDATE) { |
4641 | 0 | memcpy(ppi->ref_frame_map_copy, cm->ref_frame_map, |
4642 | 0 | sizeof(cm->ref_frame_map)); |
4643 | 0 | } |
4644 | 0 | av1_rc_postencode_update(cpi, cpi_data->frame_size); |
4645 | 0 | } |
4646 | |
|
4647 | 0 | if (cpi_data->pop_lookahead == 1) { |
4648 | 0 | av1_lookahead_pop(cpi->ppi->lookahead, cpi_data->flush, |
4649 | 0 | cpi->compressor_stage); |
4650 | 0 | } |
4651 | 0 | if (cpi->common.show_frame) { |
4652 | 0 | cpi->ppi->ts_start_last_show_frame = cpi_data->ts_frame_start; |
4653 | 0 | cpi->ppi->ts_end_last_show_frame = cpi_data->ts_frame_end; |
4654 | 0 | } |
4655 | 0 | if (ppi->level_params.keep_level_stats && !is_stat_generation_stage(cpi)) { |
4656 | | // Initialize level info. at the beginning of each sequence. |
4657 | 0 | if (cm->current_frame.frame_type == KEY_FRAME && |
4658 | 0 | ppi->gf_group.refbuf_state[cpi->gf_frame_index] == REFBUF_RESET) { |
4659 | 0 | av1_init_level_info(cpi); |
4660 | 0 | } |
4661 | 0 | av1_update_level_info(cpi, cpi_data->frame_size, cpi_data->ts_frame_start, |
4662 | 0 | cpi_data->ts_frame_end); |
4663 | 0 | } |
4664 | |
|
4665 | 0 | if (!is_stat_generation_stage(cpi)) { |
4666 | 0 | #if !CONFIG_REALTIME_ONLY |
4667 | 0 | if (!has_no_stats_stage(cpi)) av1_twopass_postencode_update(cpi); |
4668 | 0 | #endif |
4669 | 0 | update_fb_of_context_type(cpi, ppi->fb_of_context_type); |
4670 | 0 | update_rc_counts(cpi); |
4671 | 0 | update_end_of_frame_stats(cpi); |
4672 | 0 | } |
4673 | |
|
4674 | 0 | if (cpi->oxcf.pass == AOM_RC_THIRD_PASS && cpi->third_pass_ctx) { |
4675 | 0 | av1_pop_third_pass_info(cpi->third_pass_ctx); |
4676 | 0 | } |
4677 | |
|
4678 | 0 | if (ppi->rtc_ref.set_ref_frame_config) { |
4679 | 0 | av1_svc_update_buffer_slot_refreshed(cpi); |
4680 | 0 | av1_svc_set_reference_was_previous(cpi); |
4681 | 0 | } |
4682 | |
|
4683 | 0 | if (ppi->use_svc) av1_save_layer_context(cpi); |
4684 | | |
4685 | | // Note *size = 0 indicates a dropped frame for which psnr is not calculated |
4686 | 0 | if (ppi->b_calculate_psnr && cpi_data->frame_size > 0) { |
4687 | 0 | if (cm->show_existing_frame || |
4688 | 0 | (!is_stat_generation_stage(cpi) && cm->show_frame)) { |
4689 | 0 | generate_psnr_packet(cpi); |
4690 | 0 | } |
4691 | 0 | } |
4692 | |
|
4693 | | #if CONFIG_INTERNAL_STATS |
4694 | | if (!is_stat_generation_stage(cpi)) { |
4695 | | compute_internal_stats(cpi, (int)cpi_data->frame_size); |
4696 | | } |
4697 | | #endif // CONFIG_INTERNAL_STATS |
4698 | | |
4699 | | // Write frame info. Subtract 1 from frame index since if was incremented in |
4700 | | // update_rc_counts. |
4701 | 0 | av1_write_second_pass_per_frame_info(cpi, cpi->gf_frame_index - 1); |
4702 | 0 | } |
4703 | | |
4704 | 0 | int av1_get_compressed_data(AV1_COMP *cpi, AV1_COMP_DATA *const cpi_data) { |
4705 | 0 | const AV1EncoderConfig *const oxcf = &cpi->oxcf; |
4706 | 0 | AV1_COMMON *const cm = &cpi->common; |
4707 | | |
4708 | | // The jmp_buf is valid only for the duration of the function that calls |
4709 | | // setjmp(). Therefore, this function must reset the 'setjmp' field to 0 |
4710 | | // before it returns. |
4711 | 0 | if (setjmp(cm->error->jmp)) { |
4712 | 0 | cm->error->setjmp = 0; |
4713 | 0 | return cm->error->error_code; |
4714 | 0 | } |
4715 | 0 | cm->error->setjmp = 1; |
4716 | |
|
4717 | | #if CONFIG_INTERNAL_STATS |
4718 | | cpi->frame_recode_hits = 0; |
4719 | | cpi->time_compress_data = 0; |
4720 | | cpi->bytes = 0; |
4721 | | #endif |
4722 | | #if CONFIG_ENTROPY_STATS |
4723 | | if (cpi->compressor_stage == ENCODE_STAGE) { |
4724 | | av1_zero(cpi->counts); |
4725 | | } |
4726 | | #endif |
4727 | |
|
4728 | | #if CONFIG_BITSTREAM_DEBUG |
4729 | | assert(cpi->oxcf.max_threads <= 1 && |
4730 | | "bitstream debug tool does not support multithreading"); |
4731 | | bitstream_queue_record_write(); |
4732 | | |
4733 | | if (cm->seq_params->order_hint_info.enable_order_hint) { |
4734 | | aom_bitstream_queue_set_frame_write(cm->current_frame.order_hint * 2 + |
4735 | | cm->show_frame); |
4736 | | } else { |
4737 | | // This is currently used in RTC encoding. cm->show_frame is always 1. |
4738 | | aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number); |
4739 | | } |
4740 | | #endif |
4741 | 0 | if (cpi->ppi->use_svc) { |
4742 | 0 | av1_one_pass_cbr_svc_start_layer(cpi); |
4743 | 0 | } |
4744 | |
|
4745 | 0 | cpi->is_dropped_frame = false; |
4746 | 0 | cm->showable_frame = 0; |
4747 | 0 | cpi_data->frame_size = 0; |
4748 | 0 | cpi->available_bs_size = cpi_data->cx_data_sz; |
4749 | | #if CONFIG_INTERNAL_STATS |
4750 | | struct aom_usec_timer cmptimer; |
4751 | | aom_usec_timer_start(&cmptimer); |
4752 | | #endif |
4753 | 0 | av1_set_high_precision_mv(cpi, 1, 0); |
4754 | | |
4755 | | // Normal defaults |
4756 | 0 | cm->features.refresh_frame_context = |
4757 | 0 | oxcf->tool_cfg.frame_parallel_decoding_mode |
4758 | 0 | ? REFRESH_FRAME_CONTEXT_DISABLED |
4759 | 0 | : REFRESH_FRAME_CONTEXT_BACKWARD; |
4760 | 0 | if (oxcf->tile_cfg.enable_large_scale_tile) |
4761 | 0 | cm->features.refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED; |
4762 | |
|
4763 | 0 | if (assign_cur_frame_new_fb(cm) == NULL) { |
4764 | 0 | aom_internal_error(cpi->common.error, AOM_CODEC_ERROR, |
4765 | 0 | "Failed to allocate new cur_frame"); |
4766 | 0 | } |
4767 | |
|
4768 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
4769 | | // Accumulate 2nd pass time in 2-pass case or 1 pass time in 1-pass case. |
4770 | | if (cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0) |
4771 | | start_timing(cpi, av1_encode_strategy_time); |
4772 | | #endif |
4773 | |
|
4774 | 0 | const int result = av1_encode_strategy( |
4775 | 0 | cpi, &cpi_data->frame_size, cpi_data->cx_data, &cpi_data->lib_flags, |
4776 | 0 | &cpi_data->ts_frame_start, &cpi_data->ts_frame_end, |
4777 | 0 | cpi_data->timestamp_ratio, &cpi_data->pop_lookahead, cpi_data->flush); |
4778 | |
|
4779 | | #if CONFIG_COLLECT_COMPONENT_TIMING |
4780 | | if (cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0) |
4781 | | end_timing(cpi, av1_encode_strategy_time); |
4782 | | |
4783 | | // Print out timing information. |
4784 | | // Note: Use "cpi->frame_component_time[0] > 100 us" to avoid showing of |
4785 | | // show_existing_frame and lag-in-frames. |
4786 | | if ((cpi->oxcf.pass == 2 || cpi->oxcf.pass == 0) && |
4787 | | cpi->frame_component_time[0] > 100) { |
4788 | | int i; |
4789 | | uint64_t frame_total = 0, total = 0; |
4790 | | const GF_GROUP *const gf_group = &cpi->ppi->gf_group; |
4791 | | FRAME_UPDATE_TYPE frame_update_type = |
4792 | | get_frame_update_type(gf_group, cpi->gf_frame_index); |
4793 | | |
4794 | | fprintf(stderr, |
4795 | | "\n Frame number: %d, Frame type: %s, Show Frame: %d, Frame Update " |
4796 | | "Type: %d, Q: %d\n", |
4797 | | cm->current_frame.frame_number, |
4798 | | get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame, |
4799 | | frame_update_type, cm->quant_params.base_qindex); |
4800 | | for (i = 0; i < kTimingComponents; i++) { |
4801 | | cpi->component_time[i] += cpi->frame_component_time[i]; |
4802 | | // Use av1_encode_strategy_time (i = 0) as the total time. |
4803 | | if (i == 0) { |
4804 | | frame_total = cpi->frame_component_time[0]; |
4805 | | total = cpi->component_time[0]; |
4806 | | } |
4807 | | fprintf(stderr, |
4808 | | " %50s: %15" PRId64 " us [%6.2f%%] (total: %15" PRId64 |
4809 | | " us [%6.2f%%])\n", |
4810 | | get_component_name(i), cpi->frame_component_time[i], |
4811 | | (float)((float)cpi->frame_component_time[i] * 100.0 / |
4812 | | (float)frame_total), |
4813 | | cpi->component_time[i], |
4814 | | (float)((float)cpi->component_time[i] * 100.0 / (float)total)); |
4815 | | cpi->frame_component_time[i] = 0; |
4816 | | } |
4817 | | } |
4818 | | #endif |
4819 | | |
4820 | | // Reset the flag to 0 afer encoding. |
4821 | 0 | cpi->rc.use_external_qp_one_pass = 0; |
4822 | |
|
4823 | 0 | if (result == -1) { |
4824 | 0 | cm->error->setjmp = 0; |
4825 | | // Returning -1 indicates no frame encoded; more input is required |
4826 | 0 | return -1; |
4827 | 0 | } |
4828 | 0 | if (result != AOM_CODEC_OK) { |
4829 | 0 | aom_internal_error(cpi->common.error, AOM_CODEC_ERROR, |
4830 | 0 | "Failed to encode frame"); |
4831 | 0 | } |
4832 | | #if CONFIG_INTERNAL_STATS |
4833 | | aom_usec_timer_mark(&cmptimer); |
4834 | | cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer); |
4835 | | #endif // CONFIG_INTERNAL_STATS |
4836 | |
|
4837 | | #if CONFIG_SPEED_STATS |
4838 | | if (!is_stat_generation_stage(cpi) && !cm->show_existing_frame) { |
4839 | | cpi->tx_search_count += cpi->td.mb.txfm_search_info.tx_search_count; |
4840 | | cpi->td.mb.txfm_search_info.tx_search_count = 0; |
4841 | | } |
4842 | | #endif // CONFIG_SPEED_STATS |
4843 | |
|
4844 | 0 | cm->error->setjmp = 0; |
4845 | 0 | return AOM_CODEC_OK; |
4846 | 0 | } |
4847 | | |
4848 | | // Populates cpi->scaled_ref_buf corresponding to frames in a parallel encode |
4849 | | // set. Also sets the bitmask 'ref_buffers_used_map'. |
4850 | 0 | void av1_scale_references_fpmt(AV1_COMP *cpi, int *ref_buffers_used_map) { |
4851 | 0 | AV1_COMMON *cm = &cpi->common; |
4852 | 0 | MV_REFERENCE_FRAME ref_frame; |
4853 | |
|
4854 | 0 | for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) { |
4855 | | // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1). |
4856 | 0 | if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) { |
4857 | 0 | const YV12_BUFFER_CONFIG *const ref = |
4858 | 0 | get_ref_frame_yv12_buf(cm, ref_frame); |
4859 | |
|
4860 | 0 | if (ref == NULL) { |
4861 | 0 | cpi->scaled_ref_buf[ref_frame - 1] = NULL; |
4862 | 0 | continue; |
4863 | 0 | } |
4864 | | |
4865 | | // FPMT does not support scaling yet. |
4866 | 0 | assert(ref->y_crop_width == cm->width && |
4867 | 0 | ref->y_crop_height == cm->height); |
4868 | |
|
4869 | 0 | RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame); |
4870 | 0 | cpi->scaled_ref_buf[ref_frame - 1] = buf; |
4871 | 0 | for (int i = 0; i < cm->buffer_pool->num_frame_bufs; ++i) { |
4872 | 0 | if (&cm->buffer_pool->frame_bufs[i] == buf) { |
4873 | 0 | *ref_buffers_used_map |= (1 << i); |
4874 | 0 | } |
4875 | 0 | } |
4876 | 0 | } else { |
4877 | 0 | if (!has_no_stats_stage(cpi)) cpi->scaled_ref_buf[ref_frame - 1] = NULL; |
4878 | 0 | } |
4879 | 0 | } |
4880 | 0 | } |
4881 | | |
4882 | | // Increments the ref_count of frame buffers referenced by cpi->scaled_ref_buf |
4883 | | // corresponding to frames in a parallel encode set. |
4884 | | void av1_increment_scaled_ref_counts_fpmt(BufferPool *buffer_pool, |
4885 | 0 | int ref_buffers_used_map) { |
4886 | 0 | for (int i = 0; i < buffer_pool->num_frame_bufs; ++i) { |
4887 | 0 | if (ref_buffers_used_map & (1 << i)) { |
4888 | 0 | ++buffer_pool->frame_bufs[i].ref_count; |
4889 | 0 | } |
4890 | 0 | } |
4891 | 0 | } |
4892 | | |
4893 | | // Releases cpi->scaled_ref_buf corresponding to frames in a parallel encode |
4894 | | // set. |
4895 | 0 | void av1_release_scaled_references_fpmt(AV1_COMP *cpi) { |
4896 | | // TODO(isbs): only refresh the necessary frames, rather than all of them |
4897 | 0 | for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) { |
4898 | 0 | RefCntBuffer *const buf = cpi->scaled_ref_buf[i]; |
4899 | 0 | if (buf != NULL) { |
4900 | 0 | cpi->scaled_ref_buf[i] = NULL; |
4901 | 0 | } |
4902 | 0 | } |
4903 | 0 | } |
4904 | | |
4905 | | // Decrements the ref_count of frame buffers referenced by cpi->scaled_ref_buf |
4906 | | // corresponding to frames in a parallel encode set. |
4907 | | void av1_decrement_ref_counts_fpmt(BufferPool *buffer_pool, |
4908 | 0 | int ref_buffers_used_map) { |
4909 | 0 | for (int i = 0; i < buffer_pool->num_frame_bufs; ++i) { |
4910 | 0 | if (ref_buffers_used_map & (1 << i)) { |
4911 | 0 | --buffer_pool->frame_bufs[i].ref_count; |
4912 | 0 | } |
4913 | 0 | } |
4914 | 0 | } |
4915 | | |
4916 | | // Initialize parallel frame contexts with screen content decisions. |
4917 | 0 | void av1_init_sc_decisions(AV1_PRIMARY *const ppi) { |
4918 | 0 | AV1_COMP *const first_cpi = ppi->cpi; |
4919 | 0 | for (int i = 1; i < ppi->num_fp_contexts; ++i) { |
4920 | 0 | AV1_COMP *cur_cpi = ppi->parallel_cpi[i]; |
4921 | 0 | cur_cpi->common.features.allow_screen_content_tools = |
4922 | 0 | first_cpi->common.features.allow_screen_content_tools; |
4923 | 0 | cur_cpi->common.features.allow_intrabc = |
4924 | 0 | first_cpi->common.features.allow_intrabc; |
4925 | 0 | cur_cpi->use_screen_content_tools = first_cpi->use_screen_content_tools; |
4926 | 0 | cur_cpi->is_screen_content_type = first_cpi->is_screen_content_type; |
4927 | 0 | } |
4928 | 0 | } |
4929 | | |
4930 | | AV1_COMP *av1_get_parallel_frame_enc_data(AV1_PRIMARY *const ppi, |
4931 | 0 | AV1_COMP_DATA *const first_cpi_data) { |
4932 | 0 | int cpi_idx = 0; |
4933 | | |
4934 | | // Loop over parallel_cpi to find the cpi that processed the current |
4935 | | // gf_frame_index ahead of time. |
4936 | 0 | for (int i = 1; i < ppi->num_fp_contexts; i++) { |
4937 | 0 | if (ppi->cpi->gf_frame_index == ppi->parallel_cpi[i]->gf_frame_index) { |
4938 | 0 | cpi_idx = i; |
4939 | 0 | break; |
4940 | 0 | } |
4941 | 0 | } |
4942 | |
|
4943 | 0 | assert(cpi_idx > 0); |
4944 | 0 | assert(!ppi->parallel_cpi[cpi_idx]->common.show_existing_frame); |
4945 | | |
4946 | | // Release the previously-used frame-buffer. |
4947 | 0 | if (ppi->cpi->common.cur_frame != NULL) { |
4948 | 0 | --ppi->cpi->common.cur_frame->ref_count; |
4949 | 0 | ppi->cpi->common.cur_frame = NULL; |
4950 | 0 | } |
4951 | | |
4952 | | // Swap the appropriate parallel_cpi with the parallel_cpi[0]. |
4953 | 0 | ppi->cpi = ppi->parallel_cpi[cpi_idx]; |
4954 | 0 | ppi->parallel_cpi[cpi_idx] = ppi->parallel_cpi[0]; |
4955 | 0 | ppi->parallel_cpi[0] = ppi->cpi; |
4956 | | |
4957 | | // Copy appropriate parallel_frames_data to local data. |
4958 | 0 | { |
4959 | 0 | AV1_COMP_DATA *data = &ppi->parallel_frames_data[cpi_idx - 1]; |
4960 | 0 | assert(data->frame_size > 0); |
4961 | 0 | assert(first_cpi_data->cx_data_sz > data->frame_size); |
4962 | |
|
4963 | 0 | first_cpi_data->lib_flags = data->lib_flags; |
4964 | 0 | first_cpi_data->ts_frame_start = data->ts_frame_start; |
4965 | 0 | first_cpi_data->ts_frame_end = data->ts_frame_end; |
4966 | 0 | memcpy(first_cpi_data->cx_data, data->cx_data, data->frame_size); |
4967 | 0 | first_cpi_data->frame_size = data->frame_size; |
4968 | 0 | if (ppi->cpi->common.show_frame) { |
4969 | 0 | first_cpi_data->pop_lookahead = 1; |
4970 | 0 | } |
4971 | 0 | } |
4972 | |
|
4973 | 0 | return ppi->cpi; |
4974 | 0 | } |
4975 | | |
4976 | | // Initialises frames belonging to a parallel encode set. |
4977 | | int av1_init_parallel_frame_context(const AV1_COMP_DATA *const first_cpi_data, |
4978 | | AV1_PRIMARY *const ppi, |
4979 | 0 | int *ref_buffers_used_map) { |
4980 | 0 | AV1_COMP *const first_cpi = ppi->cpi; |
4981 | 0 | GF_GROUP *const gf_group = &ppi->gf_group; |
4982 | 0 | int gf_index_start = first_cpi->gf_frame_index; |
4983 | 0 | assert(gf_group->frame_parallel_level[gf_index_start] == 1); |
4984 | 0 | int parallel_frame_count = 0; |
4985 | 0 | int cur_frame_num = first_cpi->common.current_frame.frame_number; |
4986 | 0 | int show_frame_count = first_cpi->frame_index_set.show_frame_count; |
4987 | 0 | int frames_since_key = first_cpi->rc.frames_since_key; |
4988 | 0 | int frames_to_key = first_cpi->rc.frames_to_key; |
4989 | 0 | int frames_to_fwd_kf = first_cpi->rc.frames_to_fwd_kf; |
4990 | 0 | int cur_frame_disp = cur_frame_num + gf_group->arf_src_offset[gf_index_start]; |
4991 | 0 | const FIRSTPASS_STATS *stats_in = first_cpi->twopass_frame.stats_in; |
4992 | |
|
4993 | 0 | assert(*ref_buffers_used_map == 0); |
4994 | | |
4995 | | // Release the previously used frame-buffer by a frame_parallel_level 1 frame. |
4996 | 0 | if (first_cpi->common.cur_frame != NULL) { |
4997 | 0 | --first_cpi->common.cur_frame->ref_count; |
4998 | 0 | first_cpi->common.cur_frame = NULL; |
4999 | 0 | } |
5000 | |
|
5001 | 0 | RefFrameMapPair ref_frame_map_pairs[REF_FRAMES]; |
5002 | 0 | RefFrameMapPair first_ref_frame_map_pairs[REF_FRAMES]; |
5003 | 0 | init_ref_map_pair(first_cpi, first_ref_frame_map_pairs); |
5004 | 0 | memcpy(ref_frame_map_pairs, first_ref_frame_map_pairs, |
5005 | 0 | sizeof(RefFrameMapPair) * REF_FRAMES); |
5006 | | |
5007 | | // Store the reference refresh index of frame_parallel_level 1 frame in a |
5008 | | // parallel encode set of lower layer frames. |
5009 | 0 | if (gf_group->update_type[gf_index_start] == INTNL_ARF_UPDATE) { |
5010 | 0 | first_cpi->ref_refresh_index = av1_calc_refresh_idx_for_intnl_arf( |
5011 | 0 | first_cpi, ref_frame_map_pairs, gf_index_start); |
5012 | 0 | assert(first_cpi->ref_refresh_index != INVALID_IDX && |
5013 | 0 | first_cpi->ref_refresh_index < REF_FRAMES); |
5014 | 0 | first_cpi->refresh_idx_available = true; |
5015 | | // Update ref_frame_map_pairs. |
5016 | 0 | ref_frame_map_pairs[first_cpi->ref_refresh_index].disp_order = |
5017 | 0 | gf_group->display_idx[gf_index_start]; |
5018 | 0 | ref_frame_map_pairs[first_cpi->ref_refresh_index].pyr_level = |
5019 | 0 | gf_group->layer_depth[gf_index_start]; |
5020 | 0 | } |
5021 | | |
5022 | | // Set do_frame_data_update flag as false for frame_parallel_level 1 frame. |
5023 | 0 | first_cpi->do_frame_data_update = false; |
5024 | 0 | if (gf_group->arf_src_offset[gf_index_start] == 0) { |
5025 | 0 | first_cpi->time_stamps.prev_ts_start = ppi->ts_start_last_show_frame; |
5026 | 0 | first_cpi->time_stamps.prev_ts_end = ppi->ts_end_last_show_frame; |
5027 | 0 | } |
5028 | |
|
5029 | 0 | av1_get_ref_frames(first_ref_frame_map_pairs, cur_frame_disp, first_cpi, |
5030 | 0 | gf_index_start, 1, first_cpi->common.remapped_ref_idx); |
5031 | |
|
5032 | 0 | av1_scale_references_fpmt(first_cpi, ref_buffers_used_map); |
5033 | 0 | parallel_frame_count++; |
5034 | | |
5035 | | // Iterate through the GF_GROUP to find the remaining frame_parallel_level 2 |
5036 | | // frames which are part of the current parallel encode set and initialize the |
5037 | | // required cpi elements. |
5038 | 0 | for (int i = gf_index_start + 1; i < gf_group->size; i++) { |
5039 | | // Update frame counters if previous frame was show frame or show existing |
5040 | | // frame. |
5041 | 0 | if (gf_group->arf_src_offset[i - 1] == 0) { |
5042 | 0 | cur_frame_num++; |
5043 | 0 | show_frame_count++; |
5044 | 0 | if (frames_to_fwd_kf <= 0) |
5045 | 0 | frames_to_fwd_kf = first_cpi->oxcf.kf_cfg.fwd_kf_dist; |
5046 | 0 | if (frames_to_key) { |
5047 | 0 | frames_since_key++; |
5048 | 0 | frames_to_key--; |
5049 | 0 | frames_to_fwd_kf--; |
5050 | 0 | } |
5051 | 0 | stats_in++; |
5052 | 0 | } |
5053 | 0 | cur_frame_disp = cur_frame_num + gf_group->arf_src_offset[i]; |
5054 | 0 | if (gf_group->frame_parallel_level[i] == 2) { |
5055 | 0 | AV1_COMP *cur_cpi = ppi->parallel_cpi[parallel_frame_count]; |
5056 | 0 | AV1_COMP_DATA *cur_cpi_data = |
5057 | 0 | &ppi->parallel_frames_data[parallel_frame_count - 1]; |
5058 | 0 | cur_cpi->gf_frame_index = i; |
5059 | 0 | cur_cpi->framerate = first_cpi->framerate; |
5060 | 0 | cur_cpi->common.current_frame.frame_number = cur_frame_num; |
5061 | 0 | cur_cpi->common.current_frame.frame_type = gf_group->frame_type[i]; |
5062 | 0 | cur_cpi->frame_index_set.show_frame_count = show_frame_count; |
5063 | 0 | cur_cpi->rc.frames_since_key = frames_since_key; |
5064 | 0 | cur_cpi->rc.frames_to_key = frames_to_key; |
5065 | 0 | cur_cpi->rc.frames_to_fwd_kf = frames_to_fwd_kf; |
5066 | 0 | cur_cpi->rc.active_worst_quality = first_cpi->rc.active_worst_quality; |
5067 | 0 | cur_cpi->rc.avg_frame_bandwidth = first_cpi->rc.avg_frame_bandwidth; |
5068 | 0 | cur_cpi->rc.max_frame_bandwidth = first_cpi->rc.max_frame_bandwidth; |
5069 | 0 | cur_cpi->rc.min_frame_bandwidth = first_cpi->rc.min_frame_bandwidth; |
5070 | 0 | cur_cpi->rc.intervals_till_gf_calculate_due = |
5071 | 0 | first_cpi->rc.intervals_till_gf_calculate_due; |
5072 | 0 | cur_cpi->mv_search_params.max_mv_magnitude = |
5073 | 0 | first_cpi->mv_search_params.max_mv_magnitude; |
5074 | 0 | if (gf_group->update_type[cur_cpi->gf_frame_index] == INTNL_ARF_UPDATE) { |
5075 | 0 | cur_cpi->common.lf.mode_ref_delta_enabled = 1; |
5076 | 0 | } |
5077 | 0 | cur_cpi->do_frame_data_update = false; |
5078 | | // Initialize prev_ts_start and prev_ts_end for show frame(s) and show |
5079 | | // existing frame(s). |
5080 | 0 | if (gf_group->arf_src_offset[i] == 0) { |
5081 | | // Choose source of prev frame. |
5082 | 0 | int src_index = gf_group->src_offset[i]; |
5083 | 0 | struct lookahead_entry *prev_source = av1_lookahead_peek( |
5084 | 0 | ppi->lookahead, src_index - 1, cur_cpi->compressor_stage); |
5085 | | // Save timestamps of prev frame. |
5086 | 0 | cur_cpi->time_stamps.prev_ts_start = prev_source->ts_start; |
5087 | 0 | cur_cpi->time_stamps.prev_ts_end = prev_source->ts_end; |
5088 | 0 | } |
5089 | 0 | cur_cpi->time_stamps.first_ts_start = |
5090 | 0 | first_cpi->time_stamps.first_ts_start; |
5091 | |
|
5092 | 0 | memcpy(cur_cpi->common.ref_frame_map, first_cpi->common.ref_frame_map, |
5093 | 0 | sizeof(first_cpi->common.ref_frame_map)); |
5094 | 0 | cur_cpi_data->lib_flags = 0; |
5095 | 0 | cur_cpi_data->timestamp_ratio = first_cpi_data->timestamp_ratio; |
5096 | 0 | cur_cpi_data->flush = first_cpi_data->flush; |
5097 | 0 | cur_cpi_data->frame_size = 0; |
5098 | 0 | if (gf_group->update_type[gf_index_start] == INTNL_ARF_UPDATE) { |
5099 | | // If the first frame in a parallel encode set is INTNL_ARF_UPDATE |
5100 | | // frame, initialize lib_flags of frame_parallel_level 2 frame in the |
5101 | | // set with that of frame_parallel_level 1 frame. |
5102 | 0 | cur_cpi_data->lib_flags = first_cpi_data->lib_flags; |
5103 | | // Store the reference refresh index of frame_parallel_level 2 frame in |
5104 | | // a parallel encode set of lower layer frames. |
5105 | 0 | cur_cpi->ref_refresh_index = |
5106 | 0 | av1_calc_refresh_idx_for_intnl_arf(cur_cpi, ref_frame_map_pairs, i); |
5107 | 0 | cur_cpi->refresh_idx_available = true; |
5108 | | // Skip the reference frame which will be refreshed by |
5109 | | // frame_parallel_level 1 frame in a parallel encode set of lower layer |
5110 | | // frames. |
5111 | 0 | cur_cpi->ref_idx_to_skip = first_cpi->ref_refresh_index; |
5112 | 0 | } else { |
5113 | 0 | cur_cpi->ref_idx_to_skip = INVALID_IDX; |
5114 | 0 | cur_cpi->ref_refresh_index = INVALID_IDX; |
5115 | 0 | cur_cpi->refresh_idx_available = false; |
5116 | 0 | } |
5117 | 0 | cur_cpi->twopass_frame.stats_in = stats_in; |
5118 | |
|
5119 | 0 | av1_get_ref_frames(first_ref_frame_map_pairs, cur_frame_disp, cur_cpi, i, |
5120 | 0 | 1, cur_cpi->common.remapped_ref_idx); |
5121 | 0 | av1_scale_references_fpmt(cur_cpi, ref_buffers_used_map); |
5122 | 0 | parallel_frame_count++; |
5123 | 0 | } |
5124 | | |
5125 | | // Set do_frame_data_update to true for the last frame_parallel_level 2 |
5126 | | // frame in the current parallel encode set. |
5127 | 0 | if (i == (gf_group->size - 1) || |
5128 | 0 | (gf_group->frame_parallel_level[i + 1] == 0 && |
5129 | 0 | (gf_group->update_type[i + 1] == ARF_UPDATE || |
5130 | 0 | gf_group->update_type[i + 1] == INTNL_ARF_UPDATE)) || |
5131 | 0 | gf_group->frame_parallel_level[i + 1] == 1) { |
5132 | 0 | ppi->parallel_cpi[parallel_frame_count - 1]->do_frame_data_update = true; |
5133 | 0 | break; |
5134 | 0 | } |
5135 | 0 | } |
5136 | |
|
5137 | 0 | av1_increment_scaled_ref_counts_fpmt(first_cpi->common.buffer_pool, |
5138 | 0 | *ref_buffers_used_map); |
5139 | | |
5140 | | // Return the number of frames in the parallel encode set. |
5141 | 0 | return parallel_frame_count; |
5142 | 0 | } |
5143 | | |
5144 | 0 | int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) { |
5145 | 0 | AV1_COMMON *cm = &cpi->common; |
5146 | 0 | if (!cm->show_frame) { |
5147 | 0 | return -1; |
5148 | 0 | } else { |
5149 | 0 | int ret; |
5150 | 0 | if (cm->cur_frame != NULL && !cpi->oxcf.algo_cfg.skip_postproc_filtering) { |
5151 | 0 | *dest = cm->cur_frame->buf; |
5152 | 0 | dest->y_width = cm->width; |
5153 | 0 | dest->y_height = cm->height; |
5154 | 0 | dest->uv_width = cm->width >> cm->seq_params->subsampling_x; |
5155 | 0 | dest->uv_height = cm->height >> cm->seq_params->subsampling_y; |
5156 | 0 | ret = 0; |
5157 | 0 | } else { |
5158 | 0 | ret = -1; |
5159 | 0 | } |
5160 | 0 | return ret; |
5161 | 0 | } |
5162 | 0 | } |
5163 | | |
5164 | 0 | int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) { |
5165 | 0 | if (cpi->last_show_frame_buf == NULL || |
5166 | 0 | cpi->oxcf.algo_cfg.skip_postproc_filtering) |
5167 | 0 | return -1; |
5168 | | |
5169 | 0 | *frame = cpi->last_show_frame_buf->buf; |
5170 | 0 | return 0; |
5171 | 0 | } |
5172 | | |
5173 | | aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm, |
5174 | | YV12_BUFFER_CONFIG *new_frame, |
5175 | 0 | YV12_BUFFER_CONFIG *sd) { |
5176 | 0 | const int num_planes = av1_num_planes(cm); |
5177 | 0 | if (!equal_dimensions_and_border(new_frame, sd)) |
5178 | 0 | aom_internal_error(cm->error, AOM_CODEC_ERROR, |
5179 | 0 | "Incorrect buffer dimensions"); |
5180 | 0 | else |
5181 | 0 | aom_yv12_copy_frame(new_frame, sd, num_planes); |
5182 | |
|
5183 | 0 | return cm->error->error_code; |
5184 | 0 | } |
5185 | | |
5186 | | int av1_set_internal_size(AV1EncoderConfig *const oxcf, |
5187 | | ResizePendingParams *resize_pending_params, |
5188 | | AOM_SCALING_MODE horiz_mode, |
5189 | 0 | AOM_SCALING_MODE vert_mode) { |
5190 | 0 | int hr = 0, hs = 0, vr = 0, vs = 0; |
5191 | | |
5192 | | // Checks for invalid AOM_SCALING_MODE values. |
5193 | 0 | if (horiz_mode > AOME_ONETHREE || vert_mode > AOME_ONETHREE) return -1; |
5194 | | |
5195 | 0 | Scale2Ratio(horiz_mode, &hr, &hs); |
5196 | 0 | Scale2Ratio(vert_mode, &vr, &vs); |
5197 | | |
5198 | | // always go to the next whole number |
5199 | 0 | resize_pending_params->width = (hs - 1 + oxcf->frm_dim_cfg.width * hr) / hs; |
5200 | 0 | resize_pending_params->height = (vs - 1 + oxcf->frm_dim_cfg.height * vr) / vs; |
5201 | |
|
5202 | 0 | if (horiz_mode != AOME_NORMAL || vert_mode != AOME_NORMAL) { |
5203 | 0 | oxcf->resize_cfg.resize_mode = RESIZE_FIXED; |
5204 | 0 | oxcf->algo_cfg.enable_tpl_model = 0; |
5205 | 0 | } |
5206 | 0 | return 0; |
5207 | 0 | } |
5208 | | |
5209 | 0 | int av1_get_quantizer(AV1_COMP *cpi) { |
5210 | 0 | return cpi->common.quant_params.base_qindex; |
5211 | 0 | } |
5212 | | |
5213 | 0 | int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) { |
5214 | 0 | size_t output_size = 0; |
5215 | 0 | size_t total_bytes_read = 0; |
5216 | 0 | size_t remaining_size = *frame_size; |
5217 | 0 | uint8_t *buff_ptr = buffer; |
5218 | | |
5219 | | // go through each OBUs |
5220 | 0 | while (total_bytes_read < *frame_size) { |
5221 | 0 | uint8_t saved_obu_header[2]; |
5222 | 0 | uint64_t obu_payload_size; |
5223 | 0 | size_t length_of_payload_size; |
5224 | 0 | size_t length_of_obu_size; |
5225 | 0 | uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1; |
5226 | 0 | size_t obu_bytes_read = obu_header_size; // bytes read for current obu |
5227 | | |
5228 | | // save the obu header (1 or 2 bytes) |
5229 | 0 | memmove(saved_obu_header, buff_ptr, obu_header_size); |
5230 | | // clear the obu_has_size_field |
5231 | 0 | saved_obu_header[0] = saved_obu_header[0] & (~0x2); |
5232 | | |
5233 | | // get the payload_size and length of payload_size |
5234 | 0 | if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size, |
5235 | 0 | &obu_payload_size, &length_of_payload_size) != 0) { |
5236 | 0 | return AOM_CODEC_ERROR; |
5237 | 0 | } |
5238 | 0 | obu_bytes_read += length_of_payload_size; |
5239 | | |
5240 | | // calculate the length of size of the obu header plus payload |
5241 | 0 | length_of_obu_size = |
5242 | 0 | aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size)); |
5243 | | |
5244 | | // move the rest of data to new location |
5245 | 0 | memmove(buff_ptr + length_of_obu_size + obu_header_size, |
5246 | 0 | buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read); |
5247 | 0 | obu_bytes_read += (size_t)obu_payload_size; |
5248 | | |
5249 | | // write the new obu size |
5250 | 0 | const uint64_t obu_size = obu_header_size + obu_payload_size; |
5251 | 0 | size_t coded_obu_size; |
5252 | 0 | if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr, |
5253 | 0 | &coded_obu_size) != 0) { |
5254 | 0 | return AOM_CODEC_ERROR; |
5255 | 0 | } |
5256 | | |
5257 | | // write the saved (modified) obu_header following obu size |
5258 | 0 | memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size); |
5259 | |
|
5260 | 0 | total_bytes_read += obu_bytes_read; |
5261 | 0 | remaining_size -= obu_bytes_read; |
5262 | 0 | buff_ptr += length_of_obu_size + obu_size; |
5263 | 0 | output_size += length_of_obu_size + (size_t)obu_size; |
5264 | 0 | } |
5265 | | |
5266 | 0 | *frame_size = output_size; |
5267 | 0 | return AOM_CODEC_OK; |
5268 | 0 | } |
5269 | | |
5270 | | static void rtc_set_updates_ref_frame_config( |
5271 | | ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags, |
5272 | 0 | RTC_REF *const rtc_ref) { |
5273 | 0 | ext_refresh_frame_flags->update_pending = 1; |
5274 | 0 | ext_refresh_frame_flags->last_frame = rtc_ref->refresh[rtc_ref->ref_idx[0]]; |
5275 | 0 | ext_refresh_frame_flags->golden_frame = rtc_ref->refresh[rtc_ref->ref_idx[3]]; |
5276 | 0 | ext_refresh_frame_flags->bwd_ref_frame = |
5277 | 0 | rtc_ref->refresh[rtc_ref->ref_idx[4]]; |
5278 | 0 | ext_refresh_frame_flags->alt2_ref_frame = |
5279 | 0 | rtc_ref->refresh[rtc_ref->ref_idx[5]]; |
5280 | 0 | ext_refresh_frame_flags->alt_ref_frame = |
5281 | 0 | rtc_ref->refresh[rtc_ref->ref_idx[6]]; |
5282 | 0 | rtc_ref->non_reference_frame = 1; |
5283 | 0 | for (int i = 0; i < REF_FRAMES; i++) { |
5284 | 0 | if (rtc_ref->refresh[i] == 1) { |
5285 | 0 | rtc_ref->non_reference_frame = 0; |
5286 | 0 | break; |
5287 | 0 | } |
5288 | 0 | } |
5289 | 0 | } |
5290 | | |
5291 | 0 | static int rtc_set_references_external_ref_frame_config(AV1_COMP *cpi) { |
5292 | | // LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3), |
5293 | | // BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6). |
5294 | 0 | int ref = AOM_REFFRAME_ALL; |
5295 | 0 | for (int i = 0; i < INTER_REFS_PER_FRAME; i++) { |
5296 | 0 | if (!cpi->ppi->rtc_ref.reference[i]) ref ^= (1 << i); |
5297 | 0 | } |
5298 | 0 | return ref; |
5299 | 0 | } |
5300 | | |
5301 | 0 | void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) { |
5302 | | // TODO(yunqingwang): For what references to use, external encoding flags |
5303 | | // should be consistent with internal reference frame selection. Need to |
5304 | | // ensure that there is not conflict between the two. In AV1 encoder, the |
5305 | | // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3, |
5306 | | // GOLDEN, BWDREF, ALTREF2. |
5307 | |
|
5308 | 0 | ExternalFlags *const ext_flags = &cpi->ext_flags; |
5309 | 0 | ExtRefreshFrameFlagsInfo *const ext_refresh_frame_flags = |
5310 | 0 | &ext_flags->refresh_frame; |
5311 | 0 | ext_flags->ref_frame_flags = AOM_REFFRAME_ALL; |
5312 | 0 | if (flags & |
5313 | 0 | (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 | |
5314 | 0 | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD | |
5315 | 0 | AOM_EFLAG_NO_REF_ARF2)) { |
5316 | 0 | int ref = AOM_REFFRAME_ALL; |
5317 | |
|
5318 | 0 | if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG; |
5319 | 0 | if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG; |
5320 | 0 | if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG; |
5321 | |
|
5322 | 0 | if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG; |
5323 | |
|
5324 | 0 | if (flags & AOM_EFLAG_NO_REF_ARF) { |
5325 | 0 | ref ^= AOM_ALT_FLAG; |
5326 | 0 | ref ^= AOM_BWD_FLAG; |
5327 | 0 | ref ^= AOM_ALT2_FLAG; |
5328 | 0 | } else { |
5329 | 0 | if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG; |
5330 | 0 | if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG; |
5331 | 0 | } |
5332 | |
|
5333 | 0 | av1_use_as_reference(&ext_flags->ref_frame_flags, ref); |
5334 | 0 | } else { |
5335 | 0 | if (cpi->ppi->rtc_ref.set_ref_frame_config) { |
5336 | 0 | int ref = rtc_set_references_external_ref_frame_config(cpi); |
5337 | 0 | av1_use_as_reference(&ext_flags->ref_frame_flags, ref); |
5338 | 0 | } |
5339 | 0 | } |
5340 | |
|
5341 | 0 | if (flags & |
5342 | 0 | (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) { |
5343 | 0 | int upd = AOM_REFFRAME_ALL; |
5344 | | |
5345 | | // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag. |
5346 | 0 | if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG; |
5347 | |
|
5348 | 0 | if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG; |
5349 | |
|
5350 | 0 | if (flags & AOM_EFLAG_NO_UPD_ARF) { |
5351 | 0 | upd ^= AOM_ALT_FLAG; |
5352 | 0 | upd ^= AOM_BWD_FLAG; |
5353 | 0 | upd ^= AOM_ALT2_FLAG; |
5354 | 0 | } |
5355 | |
|
5356 | 0 | ext_refresh_frame_flags->last_frame = (upd & AOM_LAST_FLAG) != 0; |
5357 | 0 | ext_refresh_frame_flags->golden_frame = (upd & AOM_GOLD_FLAG) != 0; |
5358 | 0 | ext_refresh_frame_flags->alt_ref_frame = (upd & AOM_ALT_FLAG) != 0; |
5359 | 0 | ext_refresh_frame_flags->bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0; |
5360 | 0 | ext_refresh_frame_flags->alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0; |
5361 | 0 | ext_refresh_frame_flags->update_pending = 1; |
5362 | 0 | } else { |
5363 | 0 | if (cpi->ppi->rtc_ref.set_ref_frame_config) |
5364 | 0 | rtc_set_updates_ref_frame_config(ext_refresh_frame_flags, |
5365 | 0 | &cpi->ppi->rtc_ref); |
5366 | 0 | else |
5367 | 0 | ext_refresh_frame_flags->update_pending = 0; |
5368 | 0 | } |
5369 | |
|
5370 | 0 | ext_flags->use_ref_frame_mvs = cpi->oxcf.tool_cfg.enable_ref_frame_mvs & |
5371 | 0 | ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0); |
5372 | 0 | ext_flags->use_error_resilient = cpi->oxcf.tool_cfg.error_resilient_mode | |
5373 | 0 | ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0); |
5374 | 0 | ext_flags->use_s_frame = |
5375 | 0 | cpi->oxcf.kf_cfg.enable_sframe | ((flags & AOM_EFLAG_SET_S_FRAME) != 0); |
5376 | 0 | ext_flags->use_primary_ref_none = |
5377 | 0 | (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0; |
5378 | |
|
5379 | 0 | if (flags & AOM_EFLAG_NO_UPD_ENTROPY) { |
5380 | 0 | update_entropy(&ext_flags->refresh_frame_context, |
5381 | 0 | &ext_flags->refresh_frame_context_pending, 0); |
5382 | 0 | } |
5383 | 0 | } |
5384 | | |
5385 | 0 | aom_fixed_buf_t *av1_get_global_headers(AV1_PRIMARY *ppi) { |
5386 | 0 | if (!ppi) return NULL; |
5387 | | |
5388 | 0 | uint8_t header_buf[512] = { 0 }; |
5389 | 0 | const uint32_t sequence_header_size = |
5390 | 0 | av1_write_sequence_header_obu(&ppi->seq_params, &header_buf[0]); |
5391 | 0 | assert(sequence_header_size <= sizeof(header_buf)); |
5392 | 0 | if (sequence_header_size == 0) return NULL; |
5393 | | |
5394 | 0 | const size_t obu_header_size = 1; |
5395 | 0 | const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size); |
5396 | 0 | const size_t payload_offset = obu_header_size + size_field_size; |
5397 | |
|
5398 | 0 | if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL; |
5399 | 0 | memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size); |
5400 | |
|
5401 | 0 | if (av1_write_obu_header(&ppi->level_params, &ppi->cpi->frame_header_count, |
5402 | 0 | OBU_SEQUENCE_HEADER, |
5403 | 0 | ppi->seq_params.has_nonzero_operating_point_idc, 0, |
5404 | 0 | &header_buf[0]) != obu_header_size) { |
5405 | 0 | return NULL; |
5406 | 0 | } |
5407 | | |
5408 | 0 | size_t coded_size_field_size = 0; |
5409 | 0 | if (aom_uleb_encode(sequence_header_size, size_field_size, |
5410 | 0 | &header_buf[obu_header_size], |
5411 | 0 | &coded_size_field_size) != 0) { |
5412 | 0 | return NULL; |
5413 | 0 | } |
5414 | 0 | assert(coded_size_field_size == size_field_size); |
5415 | |
|
5416 | 0 | aom_fixed_buf_t *global_headers = |
5417 | 0 | (aom_fixed_buf_t *)malloc(sizeof(*global_headers)); |
5418 | 0 | if (!global_headers) return NULL; |
5419 | | |
5420 | 0 | const size_t global_header_buf_size = |
5421 | 0 | obu_header_size + size_field_size + sequence_header_size; |
5422 | |
|
5423 | 0 | global_headers->buf = malloc(global_header_buf_size); |
5424 | 0 | if (!global_headers->buf) { |
5425 | 0 | free(global_headers); |
5426 | 0 | return NULL; |
5427 | 0 | } |
5428 | | |
5429 | 0 | memcpy(global_headers->buf, &header_buf[0], global_header_buf_size); |
5430 | 0 | global_headers->sz = global_header_buf_size; |
5431 | 0 | return global_headers; |
5432 | 0 | } |