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