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 <limits.h>
13 #include <math.h>
14 #include <stdio.h>
15
16 #include "config/aom_config.h"
17 #include "config/aom_dsp_rtcd.h"
18 #include "config/aom_scale_rtcd.h"
19 #include "config/av1_rtcd.h"
20
21 #include "aom_dsp/aom_dsp_common.h"
22 #include "aom_dsp/aom_filter.h"
23 #if CONFIG_DENOISE
24 #include "aom_dsp/grain_table.h"
25 #include "aom_dsp/noise_util.h"
26 #include "aom_dsp/noise_model.h"
27 #endif
28 #include "aom_dsp/psnr.h"
29 #if CONFIG_INTERNAL_STATS
30 #include "aom_dsp/ssim.h"
31 #endif
32 #include "aom_ports/aom_timer.h"
33 #include "aom_ports/mem.h"
34 #include "aom_ports/system_state.h"
35 #include "aom_scale/aom_scale.h"
36 #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
37 #include "aom_util/debug_util.h"
38 #endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
39
40 #include "av1/common/alloccommon.h"
41 #include "av1/common/cdef.h"
42 #include "av1/common/filter.h"
43 #include "av1/common/idct.h"
44 #include "av1/common/reconinter.h"
45 #include "av1/common/reconintra.h"
46 #include "av1/common/resize.h"
47 #include "av1/common/tile_common.h"
48
49 #include "av1/encoder/aq_complexity.h"
50 #include "av1/encoder/aq_cyclicrefresh.h"
51 #include "av1/encoder/aq_variance.h"
52 #include "av1/encoder/bitstream.h"
53 #include "av1/encoder/context_tree.h"
54 #include "av1/encoder/encodeframe.h"
55 #include "av1/encoder/encodemv.h"
56 #include "av1/encoder/encoder.h"
57 #include "av1/encoder/encodetxb.h"
58 #include "av1/encoder/ethread.h"
59 #include "av1/encoder/firstpass.h"
60 #include "av1/encoder/grain_test_vectors.h"
61 #include "av1/encoder/hash_motion.h"
62 #include "av1/encoder/mbgraph.h"
63 #include "av1/encoder/picklpf.h"
64 #include "av1/encoder/pickrst.h"
65 #include "av1/encoder/random.h"
66 #include "av1/encoder/ratectrl.h"
67 #include "av1/encoder/rd.h"
68 #include "av1/encoder/segmentation.h"
69 #include "av1/encoder/speed_features.h"
70 #include "av1/encoder/temporal_filter.h"
71
72 #define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
73
74 // av1 uses 10,000,000 ticks/second as time stamp
75 #define TICKS_PER_SEC 10000000LL
76
77 #if CONFIG_ENTROPY_STATS
78 FRAME_COUNTS aggregate_fc;
79 #endif // CONFIG_ENTROPY_STATS
80
81 #define AM_SEGMENT_ID_INACTIVE 7
82 #define AM_SEGMENT_ID_ACTIVE 0
83
84 // Whether to use high precision mv for altref computation.
85 #define ALTREF_HIGH_PRECISION_MV 1
86
87 // Q threshold for high precision mv. Choose a very high value for now so that
88 // HIGH_PRECISION is always chosen.
89 #define HIGH_PRECISION_MV_QTHRESH 200
90
91 // #define OUTPUT_YUV_REC
92 #ifdef OUTPUT_YUV_SKINMAP
93 FILE *yuv_skinmap_file = NULL;
94 #endif
95 #ifdef OUTPUT_YUV_REC
96 FILE *yuv_rec_file;
97 #define FILE_NAME_LEN 100
98 #endif
99
Scale2Ratio(AOM_SCALING mode,int * hr,int * hs)100 static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
101 switch (mode) {
102 case NORMAL:
103 *hr = 1;
104 *hs = 1;
105 break;
106 case FOURFIVE:
107 *hr = 4;
108 *hs = 5;
109 break;
110 case THREEFIVE:
111 *hr = 3;
112 *hs = 5;
113 break;
114 case ONETWO:
115 *hr = 1;
116 *hs = 2;
117 break;
118 default:
119 *hr = 1;
120 *hs = 1;
121 assert(0);
122 break;
123 }
124 }
125
126 // Mark all inactive blocks as active. Other segmentation features may be set
127 // so memset cannot be used, instead only inactive blocks should be reset.
suppress_active_map(AV1_COMP * cpi)128 static void suppress_active_map(AV1_COMP *cpi) {
129 unsigned char *const seg_map = cpi->segmentation_map;
130 int i;
131 if (cpi->active_map.enabled || cpi->active_map.update)
132 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
133 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
134 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
135 }
136
apply_active_map(AV1_COMP * cpi)137 static void apply_active_map(AV1_COMP *cpi) {
138 struct segmentation *const seg = &cpi->common.seg;
139 unsigned char *const seg_map = cpi->segmentation_map;
140 const unsigned char *const active_map = cpi->active_map.map;
141 int i;
142
143 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
144
145 if (frame_is_intra_only(&cpi->common)) {
146 cpi->active_map.enabled = 0;
147 cpi->active_map.update = 1;
148 }
149
150 if (cpi->active_map.update) {
151 if (cpi->active_map.enabled) {
152 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
153 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
154 av1_enable_segmentation(seg);
155 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
156 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
157 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
158 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
159 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
160
161 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
162 -MAX_LOOP_FILTER);
163 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
164 -MAX_LOOP_FILTER);
165 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
166 -MAX_LOOP_FILTER);
167 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
168 -MAX_LOOP_FILTER);
169 } else {
170 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
171 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
172 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
173 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
174 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
175 if (seg->enabled) {
176 seg->update_data = 1;
177 seg->update_map = 1;
178 }
179 }
180 cpi->active_map.update = 0;
181 }
182 }
183
av1_set_active_map(AV1_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)184 int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
185 int cols) {
186 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
187 unsigned char *const active_map_8x8 = cpi->active_map.map;
188 const int mi_rows = cpi->common.mi_rows;
189 const int mi_cols = cpi->common.mi_cols;
190 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
191 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
192 cpi->active_map.update = 1;
193 if (new_map_16x16) {
194 int r, c;
195 for (r = 0; r < mi_rows; ++r) {
196 for (c = 0; c < mi_cols; ++c) {
197 active_map_8x8[r * mi_cols + c] =
198 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
199 ? AM_SEGMENT_ID_ACTIVE
200 : AM_SEGMENT_ID_INACTIVE;
201 }
202 }
203 cpi->active_map.enabled = 1;
204 } else {
205 cpi->active_map.enabled = 0;
206 }
207 return 0;
208 } else {
209 return -1;
210 }
211 }
212
av1_get_active_map(AV1_COMP * cpi,unsigned char * new_map_16x16,int rows,int cols)213 int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
214 int cols) {
215 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
216 new_map_16x16) {
217 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
218 const int mi_rows = cpi->common.mi_rows;
219 const int mi_cols = cpi->common.mi_cols;
220 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
221 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
222
223 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
224 if (cpi->active_map.enabled) {
225 int r, c;
226 for (r = 0; r < mi_rows; ++r) {
227 for (c = 0; c < mi_cols; ++c) {
228 // Cyclic refresh segments are considered active despite not having
229 // AM_SEGMENT_ID_ACTIVE
230 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
231 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
232 }
233 }
234 }
235 return 0;
236 } else {
237 return -1;
238 }
239 }
240
set_high_precision_mv(AV1_COMP * cpi,int allow_high_precision_mv,int cur_frame_force_integer_mv)241 static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
242 int cur_frame_force_integer_mv) {
243 MACROBLOCK *const mb = &cpi->td.mb;
244 cpi->common.allow_high_precision_mv =
245 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
246 const int copy_hp =
247 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
248 int *(*src)[2] = copy_hp ? &mb->nmvcost_hp : &mb->nmvcost;
249 mb->mv_cost_stack = *src;
250 }
251
select_sb_size(const AV1_COMP * const cpi)252 static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
253 const AV1_COMMON *const cm = &cpi->common;
254
255 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
256 return BLOCK_64X64;
257 #if CONFIG_FILEOPTIONS
258 if (cm->options && cm->options->ext_partition)
259 #endif
260 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
261 return BLOCK_128X128;
262
263 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
264
265 // TODO(any): Possibly could improve this with a heuristic.
266 #if CONFIG_FILEOPTIONS
267 if (cm->options && !cm->options->ext_partition) return BLOCK_64X64;
268 #endif
269
270 // When superres / resize is on, 'cm->width / height' can change between
271 // calls, so we don't apply this heuristic there. Also, this heuristic gives
272 // compression gain for speed >= 2 only.
273 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
274 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 2) {
275 return (cm->width >= 480 && cm->height >= 360) ? BLOCK_128X128
276 : BLOCK_64X64;
277 }
278
279 return BLOCK_128X128;
280 }
281
setup_frame(AV1_COMP * cpi)282 static void setup_frame(AV1_COMP *cpi) {
283 AV1_COMMON *const cm = &cpi->common;
284 // Set up entropy context depending on frame type. The decoder mandates
285 // the use of the default context, index 0, for keyframes and inter
286 // frames where the error_resilient_mode or intra_only flag is set. For
287 // other inter-frames the encoder currently uses only two contexts;
288 // context 1 for ALTREF frames and context 0 for the others.
289
290 cm->primary_ref_frame = PRIMARY_REF_NONE;
291 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
292 cm->force_primary_ref_none) {
293 av1_setup_past_independence(cm);
294 for (int i = 0; i < REF_FRAMES; i++) {
295 cm->fb_of_context_type[i] = -1;
296 }
297 cm->fb_of_context_type[REGULAR_FRAME] =
298 cm->show_frame ? get_ref_frame_map_idx(cpi, GOLDEN_FRAME)
299 : get_ref_frame_map_idx(cpi, ALTREF_FRAME);
300 cm->frame_context_idx = REGULAR_FRAME;
301 } else {
302 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
303 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
304 cm->frame_context_idx = EXT_ARF_FRAME;
305 else if (cpi->refresh_alt_ref_frame)
306 cm->frame_context_idx = ARF_FRAME;
307 else if (cpi->rc.is_src_frame_alt_ref)
308 cm->frame_context_idx = OVERLAY_FRAME;
309 else if (cpi->refresh_golden_frame)
310 cm->frame_context_idx = GLD_FRAME;
311 else if (cpi->refresh_bwd_ref_frame)
312 cm->frame_context_idx = BRF_FRAME;
313 else
314 cm->frame_context_idx = REGULAR_FRAME;
315 int wanted_fb = cm->fb_of_context_type[cm->frame_context_idx];
316 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
317 int fb = get_ref_frame_map_idx(cpi, ref_frame);
318 if (fb == wanted_fb) {
319 cm->primary_ref_frame = ref_frame - LAST_FRAME;
320 }
321 }
322 }
323
324 if (cm->frame_type == KEY_FRAME && cm->show_frame) {
325 cpi->refresh_golden_frame = 1;
326 cpi->refresh_alt_ref_frame = 1;
327 av1_zero(cpi->interp_filter_selected);
328 set_sb_size(&cm->seq_params, select_sb_size(cpi));
329 set_use_reference_buffer(cm, 0);
330 } else if (frame_is_sframe(cm)) {
331 cpi->refresh_golden_frame = 1;
332 cpi->refresh_alt_ref_frame = 1;
333 av1_zero(cpi->interp_filter_selected);
334 set_sb_size(&cm->seq_params, select_sb_size(cpi));
335 } else {
336 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
337 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
338 av1_setup_past_independence(cm);
339 cm->seg.update_map = 1;
340 cm->seg.update_data = 1;
341 } else {
342 *cm->fc = cm->frame_contexts[cm->frame_refs[cm->primary_ref_frame].idx];
343 }
344 av1_zero(cpi->interp_filter_selected[0]);
345 }
346
347 cm->prev_frame = get_prev_frame(cm);
348 cpi->vaq_refresh = 0;
349 }
350
enc_setup_mi(AV1_COMMON * cm)351 static void enc_setup_mi(AV1_COMMON *cm) {
352 int i;
353 int mi_rows_sb_aligned = calc_mi_size(cm->mi_rows);
354 cm->mi = cm->mip;
355 memset(cm->mip, 0, cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mip));
356 cm->prev_mi = cm->prev_mip;
357 // Clear top border row
358 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
359 // Clear left border column
360 for (i = 0; i < mi_rows_sb_aligned; ++i)
361 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
362 cm->mi_grid_visible = cm->mi_grid_base;
363 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
364
365 memset(cm->mi_grid_base, 0,
366 cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi_grid_base));
367 }
368
enc_alloc_mi(AV1_COMMON * cm,int mi_size)369 static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
370 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
371 if (!cm->mip) return 1;
372 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
373 if (!cm->prev_mip) return 1;
374 cm->mi_alloc_size = mi_size;
375
376 cm->mi_grid_base =
377 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
378 if (!cm->mi_grid_base) return 1;
379 cm->prev_mi_grid_base =
380 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
381 if (!cm->prev_mi_grid_base) return 1;
382
383 return 0;
384 }
385
enc_free_mi(AV1_COMMON * cm)386 static void enc_free_mi(AV1_COMMON *cm) {
387 aom_free(cm->mip);
388 cm->mip = NULL;
389 aom_free(cm->prev_mip);
390 cm->prev_mip = NULL;
391 aom_free(cm->mi_grid_base);
392 cm->mi_grid_base = NULL;
393 aom_free(cm->prev_mi_grid_base);
394 cm->prev_mi_grid_base = NULL;
395 cm->mi_alloc_size = 0;
396 }
397
swap_mi_and_prev_mi(AV1_COMMON * cm)398 static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
399 // Current mip will be the prev_mip for the next frame.
400 MB_MODE_INFO **temp_base = cm->prev_mi_grid_base;
401 MB_MODE_INFO *temp = cm->prev_mip;
402 cm->prev_mip = cm->mip;
403 cm->mip = temp;
404
405 // Update the upper left visible macroblock ptrs.
406 cm->mi = cm->mip;
407 cm->prev_mi = cm->prev_mip;
408
409 cm->prev_mi_grid_base = cm->mi_grid_base;
410 cm->mi_grid_base = temp_base;
411 cm->mi_grid_visible = cm->mi_grid_base;
412 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
413 }
414
av1_initialize_enc(void)415 void av1_initialize_enc(void) {
416 av1_rtcd();
417 aom_dsp_rtcd();
418 aom_scale_rtcd();
419 av1_init_intra_predictors();
420 av1_init_me_luts();
421 av1_rc_init_minq_luts();
422 av1_init_wedge_masks();
423 }
424
dealloc_context_buffers_ext(AV1_COMP * cpi)425 static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
426 if (cpi->mbmi_ext_base) {
427 aom_free(cpi->mbmi_ext_base);
428 cpi->mbmi_ext_base = NULL;
429 }
430 }
431
alloc_context_buffers_ext(AV1_COMP * cpi)432 static void alloc_context_buffers_ext(AV1_COMP *cpi) {
433 AV1_COMMON *cm = &cpi->common;
434 int mi_size = cm->mi_cols * cm->mi_rows;
435
436 dealloc_context_buffers_ext(cpi);
437 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
438 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
439 }
440
update_film_grain_parameters(struct AV1_COMP * cpi,const AV1EncoderConfig * oxcf)441 static void update_film_grain_parameters(struct AV1_COMP *cpi,
442 const AV1EncoderConfig *oxcf) {
443 AV1_COMMON *const cm = &cpi->common;
444 cpi->oxcf = *oxcf;
445
446 if (cpi->film_grain_table) {
447 aom_film_grain_table_free(cpi->film_grain_table);
448 aom_free(cpi->film_grain_table);
449 cpi->film_grain_table = NULL;
450 }
451
452 if (oxcf->film_grain_test_vector) {
453 cm->seq_params.film_grain_params_present = 1;
454 if (cm->frame_type == KEY_FRAME) {
455 memcpy(&cm->film_grain_params,
456 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
457 sizeof(cm->film_grain_params));
458
459 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
460 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
461 cm->film_grain_params.clip_to_restricted_range = 0;
462 }
463 }
464 } else if (oxcf->film_grain_table_filename) {
465 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
466 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
467
468 aom_film_grain_table_read(cpi->film_grain_table,
469 oxcf->film_grain_table_filename, &cm->error);
470 } else {
471 cm->seq_params.film_grain_params_present = 0;
472 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
473 }
474 }
475
dealloc_compressor_data(AV1_COMP * cpi)476 static void dealloc_compressor_data(AV1_COMP *cpi) {
477 AV1_COMMON *const cm = &cpi->common;
478 const int num_planes = av1_num_planes(cm);
479
480 dealloc_context_buffers_ext(cpi);
481
482 aom_free(cpi->tile_data);
483 cpi->tile_data = NULL;
484
485 // Delete sementation map
486 aom_free(cpi->segmentation_map);
487 cpi->segmentation_map = NULL;
488
489 av1_cyclic_refresh_free(cpi->cyclic_refresh);
490 cpi->cyclic_refresh = NULL;
491
492 aom_free(cpi->active_map.map);
493 cpi->active_map.map = NULL;
494
495 aom_free(cpi->td.mb.above_pred_buf);
496 cpi->td.mb.above_pred_buf = NULL;
497
498 aom_free(cpi->td.mb.left_pred_buf);
499 cpi->td.mb.left_pred_buf = NULL;
500
501 aom_free(cpi->td.mb.wsrc_buf);
502 cpi->td.mb.wsrc_buf = NULL;
503
504 for (int i = 0; i < 2; i++)
505 for (int j = 0; j < 2; j++) {
506 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
507 cpi->td.mb.hash_value_buffer[i][j] = NULL;
508 }
509 aom_free(cpi->td.mb.mask_buf);
510 cpi->td.mb.mask_buf = NULL;
511
512 aom_free(cm->tpl_mvs);
513 cm->tpl_mvs = NULL;
514
515 av1_free_ref_frame_buffers(cm->buffer_pool);
516 av1_free_txb_buf(cpi);
517 av1_free_context_buffers(cm);
518
519 aom_free_frame_buffer(&cpi->last_frame_uf);
520 av1_free_restoration_buffers(cm);
521 aom_free_frame_buffer(&cpi->trial_frame_rst);
522 aom_free_frame_buffer(&cpi->scaled_source);
523 aom_free_frame_buffer(&cpi->scaled_last_source);
524 aom_free_frame_buffer(&cpi->alt_ref_buffer);
525 av1_lookahead_destroy(cpi->lookahead);
526
527 aom_free(cpi->tile_tok[0][0]);
528 cpi->tile_tok[0][0] = 0;
529
530 aom_free(cpi->tplist[0][0]);
531 cpi->tplist[0][0] = NULL;
532
533 av1_free_pc_tree(&cpi->td, num_planes);
534
535 aom_free(cpi->td.mb.palette_buffer);
536
537 aom_free(cpi->td.mb.tmp_conv_dst);
538 for (int j = 0; j < 2; ++j) {
539 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
540 }
541
542 #if CONFIG_DENOISE
543 if (cpi->denoise_and_model) {
544 aom_denoise_and_model_free(cpi->denoise_and_model);
545 cpi->denoise_and_model = NULL;
546 }
547 #endif
548 if (cpi->film_grain_table) {
549 aom_film_grain_table_free(cpi->film_grain_table);
550 cpi->film_grain_table = NULL;
551 }
552 }
553
save_coding_context(AV1_COMP * cpi)554 static void save_coding_context(AV1_COMP *cpi) {
555 CODING_CONTEXT *const cc = &cpi->coding_context;
556 AV1_COMMON *cm = &cpi->common;
557
558 // Stores a snapshot of key state variables which can subsequently be
559 // restored with a call to av1_restore_coding_context. These functions are
560 // intended for use in a re-code loop in av1_compress_frame where the
561 // quantizer value is adjusted between loop iterations.
562 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
563 av1_copy(cc->nmv_costs, cpi->nmv_costs);
564 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
565
566 cc->fc = *cm->fc;
567 }
568
restore_coding_context(AV1_COMP * cpi)569 static void restore_coding_context(AV1_COMP *cpi) {
570 CODING_CONTEXT *const cc = &cpi->coding_context;
571 AV1_COMMON *cm = &cpi->common;
572
573 // Restore key state variables to the snapshot state stored in the
574 // previous call to av1_save_coding_context.
575 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
576 av1_copy(cpi->nmv_costs, cc->nmv_costs);
577 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
578
579 *cm->fc = cc->fc;
580 }
581
configure_static_seg_features(AV1_COMP * cpi)582 static void configure_static_seg_features(AV1_COMP *cpi) {
583 AV1_COMMON *const cm = &cpi->common;
584 const RATE_CONTROL *const rc = &cpi->rc;
585 struct segmentation *const seg = &cm->seg;
586
587 int high_q = (int)(rc->avg_q > 48.0);
588 int qi_delta;
589
590 // Disable and clear down for KF
591 if (cm->frame_type == KEY_FRAME) {
592 // Clear down the global segmentation map
593 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
594 seg->update_map = 0;
595 seg->update_data = 0;
596 cpi->static_mb_pct = 0;
597
598 // Disable segmentation
599 av1_disable_segmentation(seg);
600
601 // Clear down the segment features.
602 av1_clearall_segfeatures(seg);
603 } else if (cpi->refresh_alt_ref_frame) {
604 // If this is an alt ref frame
605 // Clear down the global segmentation map
606 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
607 seg->update_map = 0;
608 seg->update_data = 0;
609 cpi->static_mb_pct = 0;
610
611 // Disable segmentation and individual segment features by default
612 av1_disable_segmentation(seg);
613 av1_clearall_segfeatures(seg);
614
615 // Scan frames from current to arf frame.
616 // This function re-enables segmentation if appropriate.
617 av1_update_mbgraph_stats(cpi);
618
619 // If segmentation was enabled set those features needed for the
620 // arf itself.
621 if (seg->enabled) {
622 seg->update_map = 1;
623 seg->update_data = 1;
624
625 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
626 cm->seq_params.bit_depth);
627 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
628 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
629 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
630 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
631 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
632
633 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
634 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
635 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
636 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
637
638 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
639 }
640 } else if (seg->enabled) {
641 // All other frames if segmentation has been enabled
642
643 // First normal frame in a valid gf or alt ref group
644 if (rc->frames_since_golden == 0) {
645 // Set up segment features for normal frames in an arf group
646 if (rc->source_alt_ref_active) {
647 seg->update_map = 0;
648 seg->update_data = 1;
649
650 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
651 cm->seq_params.bit_depth);
652 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
653 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
654
655 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
656 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
657 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
658 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
659
660 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
661 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
662 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
663 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
664
665 // Segment coding disabled for compred testing
666 if (high_q || (cpi->static_mb_pct == 100)) {
667 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
668 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
669 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
670 }
671 } else {
672 // Disable segmentation and clear down features if alt ref
673 // is not active for this group
674
675 av1_disable_segmentation(seg);
676
677 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
678
679 seg->update_map = 0;
680 seg->update_data = 0;
681
682 av1_clearall_segfeatures(seg);
683 }
684 } else if (rc->is_src_frame_alt_ref) {
685 // Special case where we are coding over the top of a previous
686 // alt ref frame.
687 // Segment coding disabled for compred testing
688
689 // Enable ref frame features for segment 0 as well
690 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
691 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
692
693 // All mbs should use ALTREF_FRAME
694 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
695 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
696 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
697 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
698
699 // Skip all MBs if high Q (0,0 mv and skip coeffs)
700 if (high_q) {
701 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
702 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
703 }
704 // Enable data update
705 seg->update_data = 1;
706 } else {
707 // All other frames.
708
709 // No updates.. leave things as they are.
710 seg->update_map = 0;
711 seg->update_data = 0;
712 }
713 }
714 }
715
update_reference_segmentation_map(AV1_COMP * cpi)716 static void update_reference_segmentation_map(AV1_COMP *cpi) {
717 AV1_COMMON *const cm = &cpi->common;
718 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
719 uint8_t *cache_ptr = cm->current_frame_seg_map;
720 int row, col;
721
722 for (row = 0; row < cm->mi_rows; row++) {
723 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
724 uint8_t *cache = cache_ptr;
725 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
726 cache[0] = mi_4x4[0]->segment_id;
727 mi_4x4_ptr += cm->mi_stride;
728 cache_ptr += cm->mi_cols;
729 }
730 }
731
alloc_raw_frame_buffers(AV1_COMP * cpi)732 static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
733 AV1_COMMON *cm = &cpi->common;
734 const SequenceHeader *const seq_params = &cm->seq_params;
735 const AV1EncoderConfig *oxcf = &cpi->oxcf;
736
737 if (!cpi->lookahead)
738 cpi->lookahead =
739 av1_lookahead_init(oxcf->width, oxcf->height, seq_params->subsampling_x,
740 seq_params->subsampling_y,
741 seq_params->use_highbitdepth, oxcf->lag_in_frames);
742 if (!cpi->lookahead)
743 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
744 "Failed to allocate lag buffers");
745
746 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
747 if (aom_realloc_frame_buffer(
748 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
749 seq_params->subsampling_x, seq_params->subsampling_y,
750 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
751 cm->byte_alignment, NULL, NULL, NULL))
752 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
753 "Failed to allocate altref buffer");
754 }
755
alloc_util_frame_buffers(AV1_COMP * cpi)756 static void alloc_util_frame_buffers(AV1_COMP *cpi) {
757 AV1_COMMON *const cm = &cpi->common;
758 const SequenceHeader *const seq_params = &cm->seq_params;
759 if (aom_realloc_frame_buffer(
760 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
761 seq_params->subsampling_y, seq_params->use_highbitdepth,
762 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
763 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
764 "Failed to allocate last frame buffer");
765
766 if (aom_realloc_frame_buffer(
767 &cpi->trial_frame_rst, cm->superres_upscaled_width,
768 cm->superres_upscaled_height, seq_params->subsampling_x,
769 seq_params->subsampling_y, seq_params->use_highbitdepth,
770 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
771 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
772 "Failed to allocate trial restored frame buffer");
773
774 if (aom_realloc_frame_buffer(
775 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
776 seq_params->subsampling_y, seq_params->use_highbitdepth,
777 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
778 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
779 "Failed to allocate scaled source buffer");
780
781 if (aom_realloc_frame_buffer(
782 &cpi->scaled_last_source, cm->width, cm->height,
783 seq_params->subsampling_x, seq_params->subsampling_y,
784 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
785 cm->byte_alignment, NULL, NULL, NULL))
786 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
787 "Failed to allocate scaled last source buffer");
788 }
789
alloc_compressor_data(AV1_COMP * cpi)790 static void alloc_compressor_data(AV1_COMP *cpi) {
791 AV1_COMMON *cm = &cpi->common;
792 const int num_planes = av1_num_planes(cm);
793
794 av1_alloc_context_buffers(cm, cm->width, cm->height);
795
796 int mi_rows_aligned_to_sb =
797 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
798 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
799
800 av1_alloc_txb_buf(cpi);
801
802 alloc_context_buffers_ext(cpi);
803
804 aom_free(cpi->tile_tok[0][0]);
805
806 {
807 unsigned int tokens =
808 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
809 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
810 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
811 }
812 aom_free(cpi->tplist[0][0]);
813
814 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
815 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
816 sizeof(*cpi->tplist[0][0])));
817
818 av1_setup_pc_tree(&cpi->common, &cpi->td);
819 }
820
av1_new_framerate(AV1_COMP * cpi,double framerate)821 void av1_new_framerate(AV1_COMP *cpi, double framerate) {
822 cpi->framerate = framerate < 0.1 ? 30 : framerate;
823 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
824 }
825
set_tile_info(AV1_COMP * cpi)826 static void set_tile_info(AV1_COMP *cpi) {
827 AV1_COMMON *const cm = &cpi->common;
828 int i, start_sb;
829
830 av1_get_tile_limits(cm);
831
832 // configure tile columns
833 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
834 cm->uniform_tile_spacing_flag = 1;
835 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
836 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
837 } else {
838 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
839 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
840 int size_sb, j = 0;
841 cm->uniform_tile_spacing_flag = 0;
842 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
843 cm->tile_col_start_sb[i] = start_sb;
844 size_sb = cpi->oxcf.tile_widths[j++];
845 if (j >= cpi->oxcf.tile_width_count) j = 0;
846 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
847 }
848 cm->tile_cols = i;
849 cm->tile_col_start_sb[i] = sb_cols;
850 }
851 av1_calculate_tile_cols(cm);
852
853 // configure tile rows
854 if (cm->uniform_tile_spacing_flag) {
855 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
856 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
857 } else {
858 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
859 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
860 int size_sb, j = 0;
861 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
862 cm->tile_row_start_sb[i] = start_sb;
863 size_sb = cpi->oxcf.tile_heights[j++];
864 if (j >= cpi->oxcf.tile_height_count) j = 0;
865 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
866 }
867 cm->tile_rows = i;
868 cm->tile_row_start_sb[i] = sb_rows;
869 }
870 av1_calculate_tile_rows(cm);
871 }
872
update_frame_size(AV1_COMP * cpi)873 static void update_frame_size(AV1_COMP *cpi) {
874 AV1_COMMON *const cm = &cpi->common;
875 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
876
877 av1_set_mb_mi(cm, cm->width, cm->height);
878 av1_init_context_buffers(cm);
879 av1_init_macroblockd(cm, xd, NULL);
880 memset(cpi->mbmi_ext_base, 0,
881 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
882 set_tile_info(cpi);
883 }
884
init_buffer_indices(AV1_COMP * cpi)885 static void init_buffer_indices(AV1_COMP *cpi) {
886 int fb_idx;
887 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
888 cpi->ref_fb_idx[fb_idx] = fb_idx;
889 cpi->rate_index = 0;
890 cpi->rate_size = 0;
891 cpi->cur_poc = -1;
892 }
893
does_level_match(int width,int height,double fps,int lvl_width,int lvl_height,double lvl_fps,int lvl_dim_mult)894 static INLINE int does_level_match(int width, int height, double fps,
895 int lvl_width, int lvl_height,
896 double lvl_fps, int lvl_dim_mult) {
897 const int64_t lvl_luma_pels = lvl_width * lvl_height;
898 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
899 const int64_t luma_pels = width * height;
900 const double display_sample_rate = luma_pels * fps;
901 return luma_pels <= lvl_luma_pels &&
902 display_sample_rate <= lvl_display_sample_rate &&
903 width <= lvl_width * lvl_dim_mult &&
904 height <= lvl_height * lvl_dim_mult;
905 }
906
set_bitstream_level_tier(SequenceHeader * seq,AV1_COMMON * cm,const AV1EncoderConfig * oxcf)907 static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
908 const AV1EncoderConfig *oxcf) {
909 // TODO(any): This is a placeholder function that only addresses dimensions
910 // and max display sample rates.
911 // Need to add checks for max bit rate, max decoded luma sample rate, header
912 // rate, etc. that are not covered by this function.
913 (void)oxcf;
914 BitstreamLevel bl = { 9, 3 };
915 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
916 288, 30.0, 4)) {
917 bl.major = 2;
918 bl.minor = 0;
919 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
920 704, 396, 30.0, 4)) {
921 bl.major = 2;
922 bl.minor = 1;
923 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
924 1088, 612, 30.0, 4)) {
925 bl.major = 3;
926 bl.minor = 0;
927 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
928 1376, 774, 30.0, 4)) {
929 bl.major = 3;
930 bl.minor = 1;
931 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
932 2048, 1152, 30.0, 3)) {
933 bl.major = 4;
934 bl.minor = 0;
935 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
936 2048, 1152, 60.0, 3)) {
937 bl.major = 4;
938 bl.minor = 1;
939 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
940 4096, 2176, 30.0, 2)) {
941 bl.major = 5;
942 bl.minor = 0;
943 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
944 4096, 2176, 60.0, 2)) {
945 bl.major = 5;
946 bl.minor = 1;
947 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
948 4096, 2176, 120.0, 2)) {
949 bl.major = 5;
950 bl.minor = 2;
951 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
952 8192, 4352, 30.0, 2)) {
953 bl.major = 6;
954 bl.minor = 0;
955 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
956 8192, 4352, 60.0, 2)) {
957 bl.major = 6;
958 bl.minor = 1;
959 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
960 8192, 4352, 120.0, 2)) {
961 bl.major = 6;
962 bl.minor = 2;
963 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
964 16384, 8704, 30.0, 2)) {
965 bl.major = 7;
966 bl.minor = 0;
967 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
968 16384, 8704, 60.0, 2)) {
969 bl.major = 7;
970 bl.minor = 1;
971 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
972 16384, 8704, 120.0, 2)) {
973 bl.major = 7;
974 bl.minor = 2;
975 }
976 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
977 seq->level[i] = bl;
978 seq->tier[i] = 0; // setting main tier by default
979 // Set the maximum parameters for bitrate and buffer size for this profile,
980 // level, and tier
981 cm->op_params[i].bitrate = max_level_bitrate(
982 cm->seq_params.profile, major_minor_to_seq_level_idx(seq->level[i]),
983 seq->tier[i]);
984 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
985 // check
986 if (cm->op_params[i].bitrate == 0)
987 aom_internal_error(
988 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
989 "AV1 does not support this combination of profile, level, and tier.");
990 // Buffer size in bits/s is bitrate in bits/s * 1 s
991 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
992 }
993 }
994
init_seq_coding_tools(SequenceHeader * seq,AV1_COMMON * cm,const AV1EncoderConfig * oxcf)995 static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
996 const AV1EncoderConfig *oxcf) {
997 seq->still_picture = (oxcf->limit == 1);
998 seq->reduced_still_picture_hdr = seq->still_picture;
999 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
1000 seq->force_screen_content_tools = 2;
1001 seq->force_integer_mv = 2;
1002 seq->enable_order_hint = oxcf->enable_order_hint;
1003 seq->frame_id_numbers_present_flag = oxcf->large_scale_tile;
1004 if (seq->still_picture && seq->reduced_still_picture_hdr) {
1005 seq->enable_order_hint = 0;
1006 seq->frame_id_numbers_present_flag = 0;
1007 seq->force_screen_content_tools = 2;
1008 seq->force_integer_mv = 2;
1009 }
1010 seq->order_hint_bits_minus_1 =
1011 seq->enable_order_hint ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1 : -1;
1012
1013 seq->enable_dual_filter = oxcf->enable_dual_filter;
1014 seq->enable_jnt_comp = oxcf->enable_jnt_comp;
1015 seq->enable_jnt_comp &= seq->enable_order_hint;
1016 seq->enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1017 seq->enable_ref_frame_mvs &= seq->enable_order_hint;
1018 seq->enable_superres = oxcf->enable_superres;
1019 seq->enable_cdef = oxcf->enable_cdef;
1020 seq->enable_restoration = oxcf->enable_restoration;
1021 seq->enable_warped_motion = oxcf->enable_warped_motion;
1022 seq->enable_interintra_compound = 1;
1023 seq->enable_masked_compound = 1;
1024 seq->enable_intra_edge_filter = 1;
1025 seq->enable_filter_intra = 1;
1026
1027 set_bitstream_level_tier(seq, cm, oxcf);
1028
1029 if (seq->operating_points_cnt_minus_1 == 0) {
1030 seq->operating_point_idc[0] = 0;
1031 } else {
1032 // Set operating_point_idc[] such that for the i-th operating point the
1033 // first (operating_points_cnt-i) spatial layers and the first temporal
1034 // layer are decoded Note that highest quality operating point should come
1035 // first
1036 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1037 seq->operating_point_idc[i] =
1038 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1039 }
1040 }
1041
init_config(struct AV1_COMP * cpi,AV1EncoderConfig * oxcf)1042 static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1043 AV1_COMMON *const cm = &cpi->common;
1044
1045 cpi->oxcf = *oxcf;
1046 cpi->framerate = oxcf->init_framerate;
1047
1048 cm->seq_params.profile = oxcf->profile;
1049 cm->seq_params.bit_depth = oxcf->bit_depth;
1050 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1051 cm->seq_params.color_primaries = oxcf->color_primaries;
1052 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1053 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
1054 cm->seq_params.monochrome = oxcf->monochrome;
1055 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1056 cm->seq_params.color_range = oxcf->color_range;
1057 cm->timing_info_present = oxcf->timing_info_present;
1058 cm->timing_info.num_units_in_display_tick =
1059 oxcf->timing_info.num_units_in_display_tick;
1060 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1061 cm->timing_info.equal_picture_interval =
1062 oxcf->timing_info.equal_picture_interval;
1063 cm->timing_info.num_ticks_per_picture =
1064 oxcf->timing_info.num_ticks_per_picture;
1065
1066 cm->seq_params.display_model_info_present_flag =
1067 oxcf->display_model_info_present_flag;
1068 cm->seq_params.decoder_model_info_present_flag =
1069 oxcf->decoder_model_info_present_flag;
1070 if (oxcf->decoder_model_info_present_flag) {
1071 // set the decoder model parameters in schedule mode
1072 cm->buffer_model.num_units_in_decoding_tick =
1073 oxcf->buffer_model.num_units_in_decoding_tick;
1074 cm->buffer_removal_time_present = 1;
1075 set_aom_dec_model_info(&cm->buffer_model);
1076 set_dec_model_op_parameters(&cm->op_params[0]);
1077 } else if (cm->timing_info_present &&
1078 cm->timing_info.equal_picture_interval &&
1079 !cm->seq_params.decoder_model_info_present_flag) {
1080 // set the decoder model parameters in resource availability mode
1081 set_resource_availability_parameters(&cm->op_params[0]);
1082 } else {
1083 cm->op_params[0].initial_display_delay =
1084 10; // Default value (not signaled)
1085 }
1086
1087 if (cm->seq_params.monochrome) {
1088 cm->seq_params.subsampling_x = 1;
1089 cm->seq_params.subsampling_y = 1;
1090 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1091 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1092 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1093 cm->seq_params.subsampling_x = 0;
1094 cm->seq_params.subsampling_y = 0;
1095 } else {
1096 if (cm->seq_params.profile == 0) {
1097 cm->seq_params.subsampling_x = 1;
1098 cm->seq_params.subsampling_y = 1;
1099 } else if (cm->seq_params.profile == 1) {
1100 cm->seq_params.subsampling_x = 0;
1101 cm->seq_params.subsampling_y = 0;
1102 } else {
1103 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1104 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1105 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1106 } else {
1107 cm->seq_params.subsampling_x = 1;
1108 cm->seq_params.subsampling_y = 0;
1109 }
1110 }
1111 }
1112
1113 cm->width = oxcf->width;
1114 cm->height = oxcf->height;
1115 set_sb_size(&cm->seq_params,
1116 select_sb_size(cpi)); // set sb size before allocations
1117 alloc_compressor_data(cpi);
1118
1119 update_film_grain_parameters(cpi, oxcf);
1120
1121 // Single thread case: use counts in common.
1122 cpi->td.counts = &cpi->counts;
1123
1124 // change includes all joint functionality
1125 av1_change_config(cpi, oxcf);
1126
1127 cpi->static_mb_pct = 0;
1128 cpi->ref_frame_flags = 0;
1129
1130 // Reset resize pending flags
1131 cpi->resize_pending_width = 0;
1132 cpi->resize_pending_height = 0;
1133
1134 init_buffer_indices(cpi);
1135 }
1136
set_rc_buffer_sizes(RATE_CONTROL * rc,const AV1EncoderConfig * oxcf)1137 static void set_rc_buffer_sizes(RATE_CONTROL *rc,
1138 const AV1EncoderConfig *oxcf) {
1139 const int64_t bandwidth = oxcf->target_bandwidth;
1140 const int64_t starting = oxcf->starting_buffer_level_ms;
1141 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1142 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1143
1144 rc->starting_buffer_level = starting * bandwidth / 1000;
1145 rc->optimal_buffer_level =
1146 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1147 rc->maximum_buffer_size =
1148 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1149 }
1150
1151 #define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1152 cpi->fn_ptr[BT].sdf = SDF; \
1153 cpi->fn_ptr[BT].sdaf = SDAF; \
1154 cpi->fn_ptr[BT].vf = VF; \
1155 cpi->fn_ptr[BT].svf = SVF; \
1156 cpi->fn_ptr[BT].svaf = SVAF; \
1157 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1158 cpi->fn_ptr[BT].jsdaf = JSDAF; \
1159 cpi->fn_ptr[BT].jsvaf = JSVAF;
1160
1161 #define MAKE_BFP_SAD_WRAPPER(fnname) \
1162 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1163 int source_stride, \
1164 const uint8_t *ref_ptr, int ref_stride) { \
1165 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1166 } \
1167 static unsigned int fnname##_bits10( \
1168 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1169 int ref_stride) { \
1170 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1171 } \
1172 static unsigned int fnname##_bits12( \
1173 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1174 int ref_stride) { \
1175 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1176 }
1177
1178 #define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1179 static unsigned int fnname##_bits8( \
1180 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1181 int ref_stride, const uint8_t *second_pred) { \
1182 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1183 } \
1184 static unsigned int fnname##_bits10( \
1185 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1186 int ref_stride, const uint8_t *second_pred) { \
1187 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1188 2; \
1189 } \
1190 static unsigned int fnname##_bits12( \
1191 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1192 int ref_stride, const uint8_t *second_pred) { \
1193 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1194 4; \
1195 }
1196
1197 #define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1198 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1199 const uint8_t *const ref_ptr[], int ref_stride, \
1200 unsigned int *sad_array) { \
1201 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1202 } \
1203 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1204 const uint8_t *const ref_ptr[], int ref_stride, \
1205 unsigned int *sad_array) { \
1206 int i; \
1207 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1208 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1209 } \
1210 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1211 const uint8_t *const ref_ptr[], int ref_stride, \
1212 unsigned int *sad_array) { \
1213 int i; \
1214 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1215 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1216 }
1217
1218 #define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1219 static unsigned int fnname##_bits8( \
1220 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1221 int ref_stride, const uint8_t *second_pred, \
1222 const JNT_COMP_PARAMS *jcp_param) { \
1223 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1224 jcp_param); \
1225 } \
1226 static unsigned int fnname##_bits10( \
1227 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1228 int ref_stride, const uint8_t *second_pred, \
1229 const JNT_COMP_PARAMS *jcp_param) { \
1230 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1231 jcp_param) >> \
1232 2; \
1233 } \
1234 static unsigned int fnname##_bits12( \
1235 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1236 int ref_stride, const uint8_t *second_pred, \
1237 const JNT_COMP_PARAMS *jcp_param) { \
1238 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1239 jcp_param) >> \
1240 4; \
1241 }
1242
1243 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)1244 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1245 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1246 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1247 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1248 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1249 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1250 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1251 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
1252 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1253 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1254 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1255 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1256 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1257 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1258 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1259 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1260 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1261 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1262 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1263 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1264 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1265 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1266 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1267 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1268 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1269 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1270 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1271 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1272 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1273 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1274 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1275 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1276 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1277 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1278 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1279 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1280 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1281 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1282 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1283 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1284 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1285 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1286 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1287 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1288 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1289 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1290 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
1291
1292 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1293 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1294 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1295 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1296 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1297 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1298 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1299 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1300 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1301 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1302 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1303 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
1304 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1305 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1306 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1307 MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1308 MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1309 MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
1310
1311 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x128_avg)
1312 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x64_avg)
1313 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x128_avg)
1314 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x16_avg)
1315 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x32_avg)
1316 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x32_avg)
1317 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x64_avg)
1318 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x32_avg)
1319 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x64_avg)
1320 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x16_avg)
1321 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x8_avg)
1322 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x16_avg)
1323 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x8_avg)
1324 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x4_avg)
1325 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x8_avg)
1326 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x4_avg)
1327 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x16_avg)
1328 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x4_avg)
1329 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x32_avg)
1330 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x8_avg)
1331 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x64_avg)
1332 MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x16_avg)
1333
1334 #define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
1335 cpi->fn_ptr[BT].msdf = MCSDF; \
1336 cpi->fn_ptr[BT].msvf = MCSVF;
1337
1338 #define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1339 static unsigned int fnname##_bits8( \
1340 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1341 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1342 int m_stride, int invert_mask) { \
1343 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1344 second_pred_ptr, m, m_stride, invert_mask); \
1345 } \
1346 static unsigned int fnname##_bits10( \
1347 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1348 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1349 int m_stride, int invert_mask) { \
1350 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1351 second_pred_ptr, m, m_stride, invert_mask) >> \
1352 2; \
1353 } \
1354 static unsigned int fnname##_bits12( \
1355 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1356 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1357 int m_stride, int invert_mask) { \
1358 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1359 second_pred_ptr, m, m_stride, invert_mask) >> \
1360 4; \
1361 }
1362
1363 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1364 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1365 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
1366 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1367 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1368 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1369 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1370 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1371 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1372 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1373 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1374 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1375 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1376 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1377 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1378 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
1379 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1380 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1381 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1382 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
1383 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1384 MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
1385
1386 #define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1387 cpi->fn_ptr[BT].osdf = OSDF; \
1388 cpi->fn_ptr[BT].ovf = OVF; \
1389 cpi->fn_ptr[BT].osvf = OSVF;
1390
1391 #define MAKE_OBFP_SAD_WRAPPER(fnname) \
1392 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1393 const int32_t *wsrc, \
1394 const int32_t *msk) { \
1395 return fnname(ref, ref_stride, wsrc, msk); \
1396 } \
1397 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1398 const int32_t *wsrc, \
1399 const int32_t *msk) { \
1400 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1401 } \
1402 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1403 const int32_t *wsrc, \
1404 const int32_t *msk) { \
1405 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1406 }
1407
1408 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1409 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1410 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
1411 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1412 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1413 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1414 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1415 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1416 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1417 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1418 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1419 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1420 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1421 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1422 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1423 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
1424 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1425 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1426 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1427 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
1428 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1429 MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
1430
1431 static void highbd_set_var_fns(AV1_COMP *const cpi) {
1432 AV1_COMMON *const cm = &cpi->common;
1433 if (cm->seq_params.use_highbitdepth) {
1434 switch (cm->seq_params.bit_depth) {
1435 case AOM_BITS_8:
1436 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1437 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1438 aom_highbd_8_sub_pixel_variance64x16,
1439 aom_highbd_8_sub_pixel_avg_variance64x16,
1440 aom_highbd_sad64x16x4d_bits8,
1441 aom_highbd_jnt_sad64x16_avg_bits8,
1442 aom_highbd_8_jnt_sub_pixel_avg_variance64x16)
1443
1444 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1445 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1446 aom_highbd_8_sub_pixel_variance16x64,
1447 aom_highbd_8_sub_pixel_avg_variance16x64,
1448 aom_highbd_sad16x64x4d_bits8,
1449 aom_highbd_jnt_sad16x64_avg_bits8,
1450 aom_highbd_8_jnt_sub_pixel_avg_variance16x64)
1451
1452 HIGHBD_BFP(
1453 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1454 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
1455 aom_highbd_8_sub_pixel_avg_variance32x8,
1456 aom_highbd_sad32x8x4d_bits8, aom_highbd_jnt_sad32x8_avg_bits8,
1457 aom_highbd_8_jnt_sub_pixel_avg_variance32x8)
1458
1459 HIGHBD_BFP(
1460 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1461 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
1462 aom_highbd_8_sub_pixel_avg_variance8x32,
1463 aom_highbd_sad8x32x4d_bits8, aom_highbd_jnt_sad8x32_avg_bits8,
1464 aom_highbd_8_jnt_sub_pixel_avg_variance8x32)
1465
1466 HIGHBD_BFP(
1467 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1468 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
1469 aom_highbd_8_sub_pixel_avg_variance16x4,
1470 aom_highbd_sad16x4x4d_bits8, aom_highbd_jnt_sad16x4_avg_bits8,
1471 aom_highbd_8_jnt_sub_pixel_avg_variance16x4)
1472
1473 HIGHBD_BFP(
1474 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1475 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
1476 aom_highbd_8_sub_pixel_avg_variance4x16,
1477 aom_highbd_sad4x16x4d_bits8, aom_highbd_jnt_sad4x16_avg_bits8,
1478 aom_highbd_8_jnt_sub_pixel_avg_variance4x16)
1479
1480 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1481 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1482 aom_highbd_8_sub_pixel_variance32x16,
1483 aom_highbd_8_sub_pixel_avg_variance32x16,
1484 aom_highbd_sad32x16x4d_bits8,
1485 aom_highbd_jnt_sad32x16_avg_bits8,
1486 aom_highbd_8_jnt_sub_pixel_avg_variance32x16)
1487
1488 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1489 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1490 aom_highbd_8_sub_pixel_variance16x32,
1491 aom_highbd_8_sub_pixel_avg_variance16x32,
1492 aom_highbd_sad16x32x4d_bits8,
1493 aom_highbd_jnt_sad16x32_avg_bits8,
1494 aom_highbd_8_jnt_sub_pixel_avg_variance16x32)
1495
1496 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1497 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1498 aom_highbd_8_sub_pixel_variance64x32,
1499 aom_highbd_8_sub_pixel_avg_variance64x32,
1500 aom_highbd_sad64x32x4d_bits8,
1501 aom_highbd_jnt_sad64x32_avg_bits8,
1502 aom_highbd_8_jnt_sub_pixel_avg_variance64x32)
1503
1504 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1505 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1506 aom_highbd_8_sub_pixel_variance32x64,
1507 aom_highbd_8_sub_pixel_avg_variance32x64,
1508 aom_highbd_sad32x64x4d_bits8,
1509 aom_highbd_jnt_sad32x64_avg_bits8,
1510 aom_highbd_8_jnt_sub_pixel_avg_variance32x64)
1511
1512 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1513 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1514 aom_highbd_8_sub_pixel_variance32x32,
1515 aom_highbd_8_sub_pixel_avg_variance32x32,
1516 aom_highbd_sad32x32x4d_bits8,
1517 aom_highbd_jnt_sad32x32_avg_bits8,
1518 aom_highbd_8_jnt_sub_pixel_avg_variance32x32)
1519
1520 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1521 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1522 aom_highbd_8_sub_pixel_variance64x64,
1523 aom_highbd_8_sub_pixel_avg_variance64x64,
1524 aom_highbd_sad64x64x4d_bits8,
1525 aom_highbd_jnt_sad64x64_avg_bits8,
1526 aom_highbd_8_jnt_sub_pixel_avg_variance64x64)
1527
1528 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1529 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1530 aom_highbd_8_sub_pixel_variance16x16,
1531 aom_highbd_8_sub_pixel_avg_variance16x16,
1532 aom_highbd_sad16x16x4d_bits8,
1533 aom_highbd_jnt_sad16x16_avg_bits8,
1534 aom_highbd_8_jnt_sub_pixel_avg_variance16x16)
1535
1536 HIGHBD_BFP(
1537 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1538 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1539 aom_highbd_8_sub_pixel_avg_variance16x8,
1540 aom_highbd_sad16x8x4d_bits8, aom_highbd_jnt_sad16x8_avg_bits8,
1541 aom_highbd_8_jnt_sub_pixel_avg_variance16x8)
1542
1543 HIGHBD_BFP(
1544 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1545 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1546 aom_highbd_8_sub_pixel_avg_variance8x16,
1547 aom_highbd_sad8x16x4d_bits8, aom_highbd_jnt_sad8x16_avg_bits8,
1548 aom_highbd_8_jnt_sub_pixel_avg_variance8x16)
1549
1550 HIGHBD_BFP(BLOCK_8X8, aom_highbd_sad8x8_bits8,
1551 aom_highbd_sad8x8_avg_bits8, aom_highbd_8_variance8x8,
1552 aom_highbd_8_sub_pixel_variance8x8,
1553 aom_highbd_8_sub_pixel_avg_variance8x8,
1554 aom_highbd_sad8x8x4d_bits8, aom_highbd_jnt_sad8x8_avg_bits8,
1555 aom_highbd_8_jnt_sub_pixel_avg_variance8x8)
1556
1557 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1558 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1559 aom_highbd_8_sub_pixel_variance8x4,
1560 aom_highbd_8_sub_pixel_avg_variance8x4,
1561 aom_highbd_sad8x4x4d_bits8, aom_highbd_jnt_sad8x4_avg_bits8,
1562 aom_highbd_8_jnt_sub_pixel_avg_variance8x4)
1563
1564 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1565 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1566 aom_highbd_8_sub_pixel_variance4x8,
1567 aom_highbd_8_sub_pixel_avg_variance4x8,
1568 aom_highbd_sad4x8x4d_bits8, aom_highbd_jnt_sad4x8_avg_bits8,
1569 aom_highbd_8_jnt_sub_pixel_avg_variance4x8)
1570
1571 HIGHBD_BFP(BLOCK_4X4, aom_highbd_sad4x4_bits8,
1572 aom_highbd_sad4x4_avg_bits8, aom_highbd_8_variance4x4,
1573 aom_highbd_8_sub_pixel_variance4x4,
1574 aom_highbd_8_sub_pixel_avg_variance4x4,
1575 aom_highbd_sad4x4x4d_bits8, aom_highbd_jnt_sad4x4_avg_bits8,
1576 aom_highbd_8_jnt_sub_pixel_avg_variance4x4)
1577
1578 HIGHBD_BFP(
1579 BLOCK_128X128, aom_highbd_sad128x128_bits8,
1580 aom_highbd_sad128x128_avg_bits8, aom_highbd_8_variance128x128,
1581 aom_highbd_8_sub_pixel_variance128x128,
1582 aom_highbd_8_sub_pixel_avg_variance128x128,
1583 aom_highbd_sad128x128x4d_bits8, aom_highbd_jnt_sad128x128_avg_bits8,
1584 aom_highbd_8_jnt_sub_pixel_avg_variance128x128)
1585
1586 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1587 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1588 aom_highbd_8_sub_pixel_variance128x64,
1589 aom_highbd_8_sub_pixel_avg_variance128x64,
1590 aom_highbd_sad128x64x4d_bits8,
1591 aom_highbd_jnt_sad128x64_avg_bits8,
1592 aom_highbd_8_jnt_sub_pixel_avg_variance128x64)
1593
1594 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1595 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1596 aom_highbd_8_sub_pixel_variance64x128,
1597 aom_highbd_8_sub_pixel_avg_variance64x128,
1598 aom_highbd_sad64x128x4d_bits8,
1599 aom_highbd_jnt_sad64x128_avg_bits8,
1600 aom_highbd_8_jnt_sub_pixel_avg_variance64x128)
1601
1602 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1603 aom_highbd_8_masked_sub_pixel_variance128x128)
1604 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1605 aom_highbd_8_masked_sub_pixel_variance128x64)
1606 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1607 aom_highbd_8_masked_sub_pixel_variance64x128)
1608 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1609 aom_highbd_8_masked_sub_pixel_variance64x64)
1610 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1611 aom_highbd_8_masked_sub_pixel_variance64x32)
1612 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1613 aom_highbd_8_masked_sub_pixel_variance32x64)
1614 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1615 aom_highbd_8_masked_sub_pixel_variance32x32)
1616 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1617 aom_highbd_8_masked_sub_pixel_variance32x16)
1618 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1619 aom_highbd_8_masked_sub_pixel_variance16x32)
1620 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1621 aom_highbd_8_masked_sub_pixel_variance16x16)
1622 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1623 aom_highbd_8_masked_sub_pixel_variance8x16)
1624 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1625 aom_highbd_8_masked_sub_pixel_variance16x8)
1626 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1627 aom_highbd_8_masked_sub_pixel_variance8x8)
1628 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1629 aom_highbd_8_masked_sub_pixel_variance4x8)
1630 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1631 aom_highbd_8_masked_sub_pixel_variance8x4)
1632 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1633 aom_highbd_8_masked_sub_pixel_variance4x4)
1634 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1635 aom_highbd_8_masked_sub_pixel_variance64x16)
1636 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1637 aom_highbd_8_masked_sub_pixel_variance16x64)
1638 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1639 aom_highbd_8_masked_sub_pixel_variance32x8)
1640 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1641 aom_highbd_8_masked_sub_pixel_variance8x32)
1642 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1643 aom_highbd_8_masked_sub_pixel_variance16x4)
1644 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1645 aom_highbd_8_masked_sub_pixel_variance4x16)
1646 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1647 aom_highbd_obmc_variance128x128,
1648 aom_highbd_obmc_sub_pixel_variance128x128)
1649 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1650 aom_highbd_obmc_variance128x64,
1651 aom_highbd_obmc_sub_pixel_variance128x64)
1652 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1653 aom_highbd_obmc_variance64x128,
1654 aom_highbd_obmc_sub_pixel_variance64x128)
1655 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1656 aom_highbd_obmc_variance64x64,
1657 aom_highbd_obmc_sub_pixel_variance64x64)
1658 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1659 aom_highbd_obmc_variance64x32,
1660 aom_highbd_obmc_sub_pixel_variance64x32)
1661 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1662 aom_highbd_obmc_variance32x64,
1663 aom_highbd_obmc_sub_pixel_variance32x64)
1664 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1665 aom_highbd_obmc_variance32x32,
1666 aom_highbd_obmc_sub_pixel_variance32x32)
1667 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1668 aom_highbd_obmc_variance32x16,
1669 aom_highbd_obmc_sub_pixel_variance32x16)
1670 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1671 aom_highbd_obmc_variance16x32,
1672 aom_highbd_obmc_sub_pixel_variance16x32)
1673 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1674 aom_highbd_obmc_variance16x16,
1675 aom_highbd_obmc_sub_pixel_variance16x16)
1676 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1677 aom_highbd_obmc_variance8x16,
1678 aom_highbd_obmc_sub_pixel_variance8x16)
1679 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1680 aom_highbd_obmc_variance16x8,
1681 aom_highbd_obmc_sub_pixel_variance16x8)
1682 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1683 aom_highbd_obmc_variance8x8,
1684 aom_highbd_obmc_sub_pixel_variance8x8)
1685 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1686 aom_highbd_obmc_variance4x8,
1687 aom_highbd_obmc_sub_pixel_variance4x8)
1688 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1689 aom_highbd_obmc_variance8x4,
1690 aom_highbd_obmc_sub_pixel_variance8x4)
1691 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1692 aom_highbd_obmc_variance4x4,
1693 aom_highbd_obmc_sub_pixel_variance4x4)
1694 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1695 aom_highbd_obmc_variance64x16,
1696 aom_highbd_obmc_sub_pixel_variance64x16)
1697 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1698 aom_highbd_obmc_variance16x64,
1699 aom_highbd_obmc_sub_pixel_variance16x64)
1700 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1701 aom_highbd_obmc_variance32x8,
1702 aom_highbd_obmc_sub_pixel_variance32x8)
1703 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1704 aom_highbd_obmc_variance8x32,
1705 aom_highbd_obmc_sub_pixel_variance8x32)
1706 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1707 aom_highbd_obmc_variance16x4,
1708 aom_highbd_obmc_sub_pixel_variance16x4)
1709 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1710 aom_highbd_obmc_variance4x16,
1711 aom_highbd_obmc_sub_pixel_variance4x16)
1712 break;
1713
1714 case AOM_BITS_10:
1715 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1716 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1717 aom_highbd_10_sub_pixel_variance64x16,
1718 aom_highbd_10_sub_pixel_avg_variance64x16,
1719 aom_highbd_sad64x16x4d_bits10,
1720 aom_highbd_jnt_sad64x16_avg_bits10,
1721 aom_highbd_10_jnt_sub_pixel_avg_variance64x16);
1722
1723 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1724 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1725 aom_highbd_10_sub_pixel_variance16x64,
1726 aom_highbd_10_sub_pixel_avg_variance16x64,
1727 aom_highbd_sad16x64x4d_bits10,
1728 aom_highbd_jnt_sad16x64_avg_bits10,
1729 aom_highbd_10_jnt_sub_pixel_avg_variance16x64);
1730
1731 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1732 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1733 aom_highbd_10_sub_pixel_variance32x8,
1734 aom_highbd_10_sub_pixel_avg_variance32x8,
1735 aom_highbd_sad32x8x4d_bits10,
1736 aom_highbd_jnt_sad32x8_avg_bits10,
1737 aom_highbd_10_jnt_sub_pixel_avg_variance32x8);
1738
1739 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1740 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1741 aom_highbd_10_sub_pixel_variance8x32,
1742 aom_highbd_10_sub_pixel_avg_variance8x32,
1743 aom_highbd_sad8x32x4d_bits10,
1744 aom_highbd_jnt_sad8x32_avg_bits10,
1745 aom_highbd_10_jnt_sub_pixel_avg_variance8x32);
1746
1747 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1748 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1749 aom_highbd_10_sub_pixel_variance16x4,
1750 aom_highbd_10_sub_pixel_avg_variance16x4,
1751 aom_highbd_sad16x4x4d_bits10,
1752 aom_highbd_jnt_sad16x4_avg_bits10,
1753 aom_highbd_10_jnt_sub_pixel_avg_variance16x4);
1754
1755 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1756 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1757 aom_highbd_10_sub_pixel_variance4x16,
1758 aom_highbd_10_sub_pixel_avg_variance4x16,
1759 aom_highbd_sad4x16x4d_bits10,
1760 aom_highbd_jnt_sad4x16_avg_bits10,
1761 aom_highbd_10_jnt_sub_pixel_avg_variance4x16);
1762
1763 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1764 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1765 aom_highbd_10_sub_pixel_variance32x16,
1766 aom_highbd_10_sub_pixel_avg_variance32x16,
1767 aom_highbd_sad32x16x4d_bits10,
1768 aom_highbd_jnt_sad32x16_avg_bits10,
1769 aom_highbd_10_jnt_sub_pixel_avg_variance32x16);
1770
1771 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1772 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1773 aom_highbd_10_sub_pixel_variance16x32,
1774 aom_highbd_10_sub_pixel_avg_variance16x32,
1775 aom_highbd_sad16x32x4d_bits10,
1776 aom_highbd_jnt_sad16x32_avg_bits10,
1777 aom_highbd_10_jnt_sub_pixel_avg_variance16x32);
1778
1779 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1780 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1781 aom_highbd_10_sub_pixel_variance64x32,
1782 aom_highbd_10_sub_pixel_avg_variance64x32,
1783 aom_highbd_sad64x32x4d_bits10,
1784 aom_highbd_jnt_sad64x32_avg_bits10,
1785 aom_highbd_10_jnt_sub_pixel_avg_variance64x32);
1786
1787 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1788 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1789 aom_highbd_10_sub_pixel_variance32x64,
1790 aom_highbd_10_sub_pixel_avg_variance32x64,
1791 aom_highbd_sad32x64x4d_bits10,
1792 aom_highbd_jnt_sad32x64_avg_bits10,
1793 aom_highbd_10_jnt_sub_pixel_avg_variance32x64);
1794
1795 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1796 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1797 aom_highbd_10_sub_pixel_variance32x32,
1798 aom_highbd_10_sub_pixel_avg_variance32x32,
1799 aom_highbd_sad32x32x4d_bits10,
1800 aom_highbd_jnt_sad32x32_avg_bits10,
1801 aom_highbd_10_jnt_sub_pixel_avg_variance32x32);
1802
1803 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1804 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1805 aom_highbd_10_sub_pixel_variance64x64,
1806 aom_highbd_10_sub_pixel_avg_variance64x64,
1807 aom_highbd_sad64x64x4d_bits10,
1808 aom_highbd_jnt_sad64x64_avg_bits10,
1809 aom_highbd_10_jnt_sub_pixel_avg_variance64x64);
1810
1811 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1812 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1813 aom_highbd_10_sub_pixel_variance16x16,
1814 aom_highbd_10_sub_pixel_avg_variance16x16,
1815 aom_highbd_sad16x16x4d_bits10,
1816 aom_highbd_jnt_sad16x16_avg_bits10,
1817 aom_highbd_10_jnt_sub_pixel_avg_variance16x16);
1818
1819 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1820 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1821 aom_highbd_10_sub_pixel_variance16x8,
1822 aom_highbd_10_sub_pixel_avg_variance16x8,
1823 aom_highbd_sad16x8x4d_bits10,
1824 aom_highbd_jnt_sad16x8_avg_bits10,
1825 aom_highbd_10_jnt_sub_pixel_avg_variance16x8);
1826
1827 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1828 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1829 aom_highbd_10_sub_pixel_variance8x16,
1830 aom_highbd_10_sub_pixel_avg_variance8x16,
1831 aom_highbd_sad8x16x4d_bits10,
1832 aom_highbd_jnt_sad8x16_avg_bits10,
1833 aom_highbd_10_jnt_sub_pixel_avg_variance8x16);
1834
1835 HIGHBD_BFP(
1836 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1837 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1838 aom_highbd_10_sub_pixel_avg_variance8x8,
1839 aom_highbd_sad8x8x4d_bits10, aom_highbd_jnt_sad8x8_avg_bits10,
1840 aom_highbd_10_jnt_sub_pixel_avg_variance8x8);
1841
1842 HIGHBD_BFP(
1843 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1844 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1845 aom_highbd_10_sub_pixel_avg_variance8x4,
1846 aom_highbd_sad8x4x4d_bits10, aom_highbd_jnt_sad8x4_avg_bits10,
1847 aom_highbd_10_jnt_sub_pixel_avg_variance8x4);
1848
1849 HIGHBD_BFP(
1850 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1851 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1852 aom_highbd_10_sub_pixel_avg_variance4x8,
1853 aom_highbd_sad4x8x4d_bits10, aom_highbd_jnt_sad4x8_avg_bits10,
1854 aom_highbd_10_jnt_sub_pixel_avg_variance4x8);
1855
1856 HIGHBD_BFP(
1857 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1858 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1859 aom_highbd_10_sub_pixel_avg_variance4x4,
1860 aom_highbd_sad4x4x4d_bits10, aom_highbd_jnt_sad4x4_avg_bits10,
1861 aom_highbd_10_jnt_sub_pixel_avg_variance4x4);
1862
1863 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1864 aom_highbd_sad128x128_avg_bits10,
1865 aom_highbd_10_variance128x128,
1866 aom_highbd_10_sub_pixel_variance128x128,
1867 aom_highbd_10_sub_pixel_avg_variance128x128,
1868 aom_highbd_sad128x128x4d_bits10,
1869 aom_highbd_jnt_sad128x128_avg_bits10,
1870 aom_highbd_10_jnt_sub_pixel_avg_variance128x128);
1871
1872 HIGHBD_BFP(
1873 BLOCK_128X64, aom_highbd_sad128x64_bits10,
1874 aom_highbd_sad128x64_avg_bits10, aom_highbd_10_variance128x64,
1875 aom_highbd_10_sub_pixel_variance128x64,
1876 aom_highbd_10_sub_pixel_avg_variance128x64,
1877 aom_highbd_sad128x64x4d_bits10, aom_highbd_jnt_sad128x64_avg_bits10,
1878 aom_highbd_10_jnt_sub_pixel_avg_variance128x64);
1879
1880 HIGHBD_BFP(
1881 BLOCK_64X128, aom_highbd_sad64x128_bits10,
1882 aom_highbd_sad64x128_avg_bits10, aom_highbd_10_variance64x128,
1883 aom_highbd_10_sub_pixel_variance64x128,
1884 aom_highbd_10_sub_pixel_avg_variance64x128,
1885 aom_highbd_sad64x128x4d_bits10, aom_highbd_jnt_sad64x128_avg_bits10,
1886 aom_highbd_10_jnt_sub_pixel_avg_variance64x128);
1887
1888 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1889 aom_highbd_10_masked_sub_pixel_variance128x128)
1890 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1891 aom_highbd_10_masked_sub_pixel_variance128x64)
1892 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1893 aom_highbd_10_masked_sub_pixel_variance64x128)
1894 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1895 aom_highbd_10_masked_sub_pixel_variance64x64)
1896 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1897 aom_highbd_10_masked_sub_pixel_variance64x32)
1898 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1899 aom_highbd_10_masked_sub_pixel_variance32x64)
1900 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1901 aom_highbd_10_masked_sub_pixel_variance32x32)
1902 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1903 aom_highbd_10_masked_sub_pixel_variance32x16)
1904 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1905 aom_highbd_10_masked_sub_pixel_variance16x32)
1906 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1907 aom_highbd_10_masked_sub_pixel_variance16x16)
1908 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1909 aom_highbd_10_masked_sub_pixel_variance8x16)
1910 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1911 aom_highbd_10_masked_sub_pixel_variance16x8)
1912 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1913 aom_highbd_10_masked_sub_pixel_variance8x8)
1914 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1915 aom_highbd_10_masked_sub_pixel_variance4x8)
1916 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1917 aom_highbd_10_masked_sub_pixel_variance8x4)
1918 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1919 aom_highbd_10_masked_sub_pixel_variance4x4)
1920 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1921 aom_highbd_10_masked_sub_pixel_variance64x16)
1922 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1923 aom_highbd_10_masked_sub_pixel_variance16x64)
1924 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1925 aom_highbd_10_masked_sub_pixel_variance32x8)
1926 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1927 aom_highbd_10_masked_sub_pixel_variance8x32)
1928 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1929 aom_highbd_10_masked_sub_pixel_variance16x4)
1930 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1931 aom_highbd_10_masked_sub_pixel_variance4x16)
1932 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1933 aom_highbd_10_obmc_variance128x128,
1934 aom_highbd_10_obmc_sub_pixel_variance128x128)
1935 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1936 aom_highbd_10_obmc_variance128x64,
1937 aom_highbd_10_obmc_sub_pixel_variance128x64)
1938 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1939 aom_highbd_10_obmc_variance64x128,
1940 aom_highbd_10_obmc_sub_pixel_variance64x128)
1941 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1942 aom_highbd_10_obmc_variance64x64,
1943 aom_highbd_10_obmc_sub_pixel_variance64x64)
1944 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1945 aom_highbd_10_obmc_variance64x32,
1946 aom_highbd_10_obmc_sub_pixel_variance64x32)
1947 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
1948 aom_highbd_10_obmc_variance32x64,
1949 aom_highbd_10_obmc_sub_pixel_variance32x64)
1950 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
1951 aom_highbd_10_obmc_variance32x32,
1952 aom_highbd_10_obmc_sub_pixel_variance32x32)
1953 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
1954 aom_highbd_10_obmc_variance32x16,
1955 aom_highbd_10_obmc_sub_pixel_variance32x16)
1956 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
1957 aom_highbd_10_obmc_variance16x32,
1958 aom_highbd_10_obmc_sub_pixel_variance16x32)
1959 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
1960 aom_highbd_10_obmc_variance16x16,
1961 aom_highbd_10_obmc_sub_pixel_variance16x16)
1962 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
1963 aom_highbd_10_obmc_variance8x16,
1964 aom_highbd_10_obmc_sub_pixel_variance8x16)
1965 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
1966 aom_highbd_10_obmc_variance16x8,
1967 aom_highbd_10_obmc_sub_pixel_variance16x8)
1968 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
1969 aom_highbd_10_obmc_variance8x8,
1970 aom_highbd_10_obmc_sub_pixel_variance8x8)
1971 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
1972 aom_highbd_10_obmc_variance4x8,
1973 aom_highbd_10_obmc_sub_pixel_variance4x8)
1974 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
1975 aom_highbd_10_obmc_variance8x4,
1976 aom_highbd_10_obmc_sub_pixel_variance8x4)
1977 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
1978 aom_highbd_10_obmc_variance4x4,
1979 aom_highbd_10_obmc_sub_pixel_variance4x4)
1980
1981 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
1982 aom_highbd_10_obmc_variance64x16,
1983 aom_highbd_10_obmc_sub_pixel_variance64x16)
1984
1985 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
1986 aom_highbd_10_obmc_variance16x64,
1987 aom_highbd_10_obmc_sub_pixel_variance16x64)
1988
1989 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
1990 aom_highbd_10_obmc_variance32x8,
1991 aom_highbd_10_obmc_sub_pixel_variance32x8)
1992
1993 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
1994 aom_highbd_10_obmc_variance8x32,
1995 aom_highbd_10_obmc_sub_pixel_variance8x32)
1996
1997 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
1998 aom_highbd_10_obmc_variance16x4,
1999 aom_highbd_10_obmc_sub_pixel_variance16x4)
2000
2001 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2002 aom_highbd_10_obmc_variance4x16,
2003 aom_highbd_10_obmc_sub_pixel_variance4x16)
2004 break;
2005
2006 case AOM_BITS_12:
2007 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2008 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2009 aom_highbd_12_sub_pixel_variance64x16,
2010 aom_highbd_12_sub_pixel_avg_variance64x16,
2011 aom_highbd_sad64x16x4d_bits12,
2012 aom_highbd_jnt_sad64x16_avg_bits12,
2013 aom_highbd_12_jnt_sub_pixel_avg_variance64x16);
2014
2015 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2016 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2017 aom_highbd_12_sub_pixel_variance16x64,
2018 aom_highbd_12_sub_pixel_avg_variance16x64,
2019 aom_highbd_sad16x64x4d_bits12,
2020 aom_highbd_jnt_sad16x64_avg_bits12,
2021 aom_highbd_12_jnt_sub_pixel_avg_variance16x64);
2022
2023 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2024 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2025 aom_highbd_12_sub_pixel_variance32x8,
2026 aom_highbd_12_sub_pixel_avg_variance32x8,
2027 aom_highbd_sad32x8x4d_bits12,
2028 aom_highbd_jnt_sad32x8_avg_bits12,
2029 aom_highbd_12_jnt_sub_pixel_avg_variance32x8);
2030
2031 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2032 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2033 aom_highbd_12_sub_pixel_variance8x32,
2034 aom_highbd_12_sub_pixel_avg_variance8x32,
2035 aom_highbd_sad8x32x4d_bits12,
2036 aom_highbd_jnt_sad8x32_avg_bits12,
2037 aom_highbd_12_jnt_sub_pixel_avg_variance8x32);
2038
2039 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2040 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2041 aom_highbd_12_sub_pixel_variance16x4,
2042 aom_highbd_12_sub_pixel_avg_variance16x4,
2043 aom_highbd_sad16x4x4d_bits12,
2044 aom_highbd_jnt_sad16x4_avg_bits12,
2045 aom_highbd_12_jnt_sub_pixel_avg_variance16x4);
2046
2047 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2048 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2049 aom_highbd_12_sub_pixel_variance4x16,
2050 aom_highbd_12_sub_pixel_avg_variance4x16,
2051 aom_highbd_sad4x16x4d_bits12,
2052 aom_highbd_jnt_sad4x16_avg_bits12,
2053 aom_highbd_12_jnt_sub_pixel_avg_variance4x16);
2054
2055 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2056 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2057 aom_highbd_12_sub_pixel_variance32x16,
2058 aom_highbd_12_sub_pixel_avg_variance32x16,
2059 aom_highbd_sad32x16x4d_bits12,
2060 aom_highbd_jnt_sad32x16_avg_bits12,
2061 aom_highbd_12_jnt_sub_pixel_avg_variance32x16);
2062
2063 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2064 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2065 aom_highbd_12_sub_pixel_variance16x32,
2066 aom_highbd_12_sub_pixel_avg_variance16x32,
2067 aom_highbd_sad16x32x4d_bits12,
2068 aom_highbd_jnt_sad16x32_avg_bits12,
2069 aom_highbd_12_jnt_sub_pixel_avg_variance16x32);
2070
2071 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2072 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2073 aom_highbd_12_sub_pixel_variance64x32,
2074 aom_highbd_12_sub_pixel_avg_variance64x32,
2075 aom_highbd_sad64x32x4d_bits12,
2076 aom_highbd_jnt_sad64x32_avg_bits12,
2077 aom_highbd_12_jnt_sub_pixel_avg_variance64x32);
2078
2079 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2080 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2081 aom_highbd_12_sub_pixel_variance32x64,
2082 aom_highbd_12_sub_pixel_avg_variance32x64,
2083 aom_highbd_sad32x64x4d_bits12,
2084 aom_highbd_jnt_sad32x64_avg_bits12,
2085 aom_highbd_12_jnt_sub_pixel_avg_variance32x64);
2086
2087 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2088 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2089 aom_highbd_12_sub_pixel_variance32x32,
2090 aom_highbd_12_sub_pixel_avg_variance32x32,
2091 aom_highbd_sad32x32x4d_bits12,
2092 aom_highbd_jnt_sad32x32_avg_bits12,
2093 aom_highbd_12_jnt_sub_pixel_avg_variance32x32);
2094
2095 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2096 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2097 aom_highbd_12_sub_pixel_variance64x64,
2098 aom_highbd_12_sub_pixel_avg_variance64x64,
2099 aom_highbd_sad64x64x4d_bits12,
2100 aom_highbd_jnt_sad64x64_avg_bits12,
2101 aom_highbd_12_jnt_sub_pixel_avg_variance64x64);
2102
2103 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2104 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2105 aom_highbd_12_sub_pixel_variance16x16,
2106 aom_highbd_12_sub_pixel_avg_variance16x16,
2107 aom_highbd_sad16x16x4d_bits12,
2108 aom_highbd_jnt_sad16x16_avg_bits12,
2109 aom_highbd_12_jnt_sub_pixel_avg_variance16x16);
2110
2111 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2112 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2113 aom_highbd_12_sub_pixel_variance16x8,
2114 aom_highbd_12_sub_pixel_avg_variance16x8,
2115 aom_highbd_sad16x8x4d_bits12,
2116 aom_highbd_jnt_sad16x8_avg_bits12,
2117 aom_highbd_12_jnt_sub_pixel_avg_variance16x8);
2118
2119 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2120 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2121 aom_highbd_12_sub_pixel_variance8x16,
2122 aom_highbd_12_sub_pixel_avg_variance8x16,
2123 aom_highbd_sad8x16x4d_bits12,
2124 aom_highbd_jnt_sad8x16_avg_bits12,
2125 aom_highbd_12_jnt_sub_pixel_avg_variance8x16);
2126
2127 HIGHBD_BFP(
2128 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2129 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
2130 aom_highbd_12_sub_pixel_avg_variance8x8,
2131 aom_highbd_sad8x8x4d_bits12, aom_highbd_jnt_sad8x8_avg_bits12,
2132 aom_highbd_12_jnt_sub_pixel_avg_variance8x8);
2133
2134 HIGHBD_BFP(
2135 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2136 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2137 aom_highbd_12_sub_pixel_avg_variance8x4,
2138 aom_highbd_sad8x4x4d_bits12, aom_highbd_jnt_sad8x4_avg_bits12,
2139 aom_highbd_12_jnt_sub_pixel_avg_variance8x4);
2140
2141 HIGHBD_BFP(
2142 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2143 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2144 aom_highbd_12_sub_pixel_avg_variance4x8,
2145 aom_highbd_sad4x8x4d_bits12, aom_highbd_jnt_sad4x8_avg_bits12,
2146 aom_highbd_12_jnt_sub_pixel_avg_variance4x8);
2147
2148 HIGHBD_BFP(
2149 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2150 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
2151 aom_highbd_12_sub_pixel_avg_variance4x4,
2152 aom_highbd_sad4x4x4d_bits12, aom_highbd_jnt_sad4x4_avg_bits12,
2153 aom_highbd_12_jnt_sub_pixel_avg_variance4x4);
2154
2155 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2156 aom_highbd_sad128x128_avg_bits12,
2157 aom_highbd_12_variance128x128,
2158 aom_highbd_12_sub_pixel_variance128x128,
2159 aom_highbd_12_sub_pixel_avg_variance128x128,
2160 aom_highbd_sad128x128x4d_bits12,
2161 aom_highbd_jnt_sad128x128_avg_bits12,
2162 aom_highbd_12_jnt_sub_pixel_avg_variance128x128);
2163
2164 HIGHBD_BFP(
2165 BLOCK_128X64, aom_highbd_sad128x64_bits12,
2166 aom_highbd_sad128x64_avg_bits12, aom_highbd_12_variance128x64,
2167 aom_highbd_12_sub_pixel_variance128x64,
2168 aom_highbd_12_sub_pixel_avg_variance128x64,
2169 aom_highbd_sad128x64x4d_bits12, aom_highbd_jnt_sad128x64_avg_bits12,
2170 aom_highbd_12_jnt_sub_pixel_avg_variance128x64);
2171
2172 HIGHBD_BFP(
2173 BLOCK_64X128, aom_highbd_sad64x128_bits12,
2174 aom_highbd_sad64x128_avg_bits12, aom_highbd_12_variance64x128,
2175 aom_highbd_12_sub_pixel_variance64x128,
2176 aom_highbd_12_sub_pixel_avg_variance64x128,
2177 aom_highbd_sad64x128x4d_bits12, aom_highbd_jnt_sad64x128_avg_bits12,
2178 aom_highbd_12_jnt_sub_pixel_avg_variance64x128);
2179
2180 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2181 aom_highbd_12_masked_sub_pixel_variance128x128)
2182 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2183 aom_highbd_12_masked_sub_pixel_variance128x64)
2184 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2185 aom_highbd_12_masked_sub_pixel_variance64x128)
2186 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2187 aom_highbd_12_masked_sub_pixel_variance64x64)
2188 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2189 aom_highbd_12_masked_sub_pixel_variance64x32)
2190 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2191 aom_highbd_12_masked_sub_pixel_variance32x64)
2192 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2193 aom_highbd_12_masked_sub_pixel_variance32x32)
2194 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2195 aom_highbd_12_masked_sub_pixel_variance32x16)
2196 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2197 aom_highbd_12_masked_sub_pixel_variance16x32)
2198 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2199 aom_highbd_12_masked_sub_pixel_variance16x16)
2200 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2201 aom_highbd_12_masked_sub_pixel_variance8x16)
2202 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2203 aom_highbd_12_masked_sub_pixel_variance16x8)
2204 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2205 aom_highbd_12_masked_sub_pixel_variance8x8)
2206 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2207 aom_highbd_12_masked_sub_pixel_variance4x8)
2208 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2209 aom_highbd_12_masked_sub_pixel_variance8x4)
2210 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2211 aom_highbd_12_masked_sub_pixel_variance4x4)
2212 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2213 aom_highbd_12_masked_sub_pixel_variance64x16)
2214 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2215 aom_highbd_12_masked_sub_pixel_variance16x64)
2216 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2217 aom_highbd_12_masked_sub_pixel_variance32x8)
2218 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2219 aom_highbd_12_masked_sub_pixel_variance8x32)
2220 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2221 aom_highbd_12_masked_sub_pixel_variance16x4)
2222 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2223 aom_highbd_12_masked_sub_pixel_variance4x16)
2224 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2225 aom_highbd_12_obmc_variance128x128,
2226 aom_highbd_12_obmc_sub_pixel_variance128x128)
2227 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2228 aom_highbd_12_obmc_variance128x64,
2229 aom_highbd_12_obmc_sub_pixel_variance128x64)
2230 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2231 aom_highbd_12_obmc_variance64x128,
2232 aom_highbd_12_obmc_sub_pixel_variance64x128)
2233 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2234 aom_highbd_12_obmc_variance64x64,
2235 aom_highbd_12_obmc_sub_pixel_variance64x64)
2236 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2237 aom_highbd_12_obmc_variance64x32,
2238 aom_highbd_12_obmc_sub_pixel_variance64x32)
2239 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2240 aom_highbd_12_obmc_variance32x64,
2241 aom_highbd_12_obmc_sub_pixel_variance32x64)
2242 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2243 aom_highbd_12_obmc_variance32x32,
2244 aom_highbd_12_obmc_sub_pixel_variance32x32)
2245 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2246 aom_highbd_12_obmc_variance32x16,
2247 aom_highbd_12_obmc_sub_pixel_variance32x16)
2248 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2249 aom_highbd_12_obmc_variance16x32,
2250 aom_highbd_12_obmc_sub_pixel_variance16x32)
2251 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2252 aom_highbd_12_obmc_variance16x16,
2253 aom_highbd_12_obmc_sub_pixel_variance16x16)
2254 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2255 aom_highbd_12_obmc_variance8x16,
2256 aom_highbd_12_obmc_sub_pixel_variance8x16)
2257 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2258 aom_highbd_12_obmc_variance16x8,
2259 aom_highbd_12_obmc_sub_pixel_variance16x8)
2260 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2261 aom_highbd_12_obmc_variance8x8,
2262 aom_highbd_12_obmc_sub_pixel_variance8x8)
2263 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2264 aom_highbd_12_obmc_variance4x8,
2265 aom_highbd_12_obmc_sub_pixel_variance4x8)
2266 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2267 aom_highbd_12_obmc_variance8x4,
2268 aom_highbd_12_obmc_sub_pixel_variance8x4)
2269 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2270 aom_highbd_12_obmc_variance4x4,
2271 aom_highbd_12_obmc_sub_pixel_variance4x4)
2272 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2273 aom_highbd_12_obmc_variance64x16,
2274 aom_highbd_12_obmc_sub_pixel_variance64x16)
2275 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2276 aom_highbd_12_obmc_variance16x64,
2277 aom_highbd_12_obmc_sub_pixel_variance16x64)
2278 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2279 aom_highbd_12_obmc_variance32x8,
2280 aom_highbd_12_obmc_sub_pixel_variance32x8)
2281 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2282 aom_highbd_12_obmc_variance8x32,
2283 aom_highbd_12_obmc_sub_pixel_variance8x32)
2284 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2285 aom_highbd_12_obmc_variance16x4,
2286 aom_highbd_12_obmc_sub_pixel_variance16x4)
2287 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2288 aom_highbd_12_obmc_variance4x16,
2289 aom_highbd_12_obmc_sub_pixel_variance4x16)
2290 break;
2291
2292 default:
2293 assert(0 &&
2294 "cm->seq_params.bit_depth should be AOM_BITS_8, "
2295 "AOM_BITS_10 or AOM_BITS_12");
2296 }
2297 }
2298 }
2299
realloc_segmentation_maps(AV1_COMP * cpi)2300 static void realloc_segmentation_maps(AV1_COMP *cpi) {
2301 AV1_COMMON *const cm = &cpi->common;
2302
2303 // Create the encoder segmentation map and set all entries to 0
2304 aom_free(cpi->segmentation_map);
2305 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
2306 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
2307
2308 // Create a map used for cyclic background refresh.
2309 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
2310 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
2311 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
2312
2313 // Create a map used to mark inactive areas.
2314 aom_free(cpi->active_map.map);
2315 CHECK_MEM_ERROR(cm, cpi->active_map.map,
2316 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
2317 }
2318
av1_change_config(struct AV1_COMP * cpi,const AV1EncoderConfig * oxcf)2319 void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2320 AV1_COMMON *const cm = &cpi->common;
2321 SequenceHeader *const seq_params = &cm->seq_params;
2322 const int num_planes = av1_num_planes(cm);
2323 RATE_CONTROL *const rc = &cpi->rc;
2324 MACROBLOCK *const x = &cpi->td.mb;
2325
2326 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2327 seq_params->bit_depth = oxcf->bit_depth;
2328 seq_params->color_primaries = oxcf->color_primaries;
2329 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2330 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2331 seq_params->monochrome = oxcf->monochrome;
2332 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2333 seq_params->color_range = oxcf->color_range;
2334
2335 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2336 seq_params->bit_depth <= AOM_BITS_10));
2337
2338 cm->timing_info_present = oxcf->timing_info_present;
2339 cm->timing_info.num_units_in_display_tick =
2340 oxcf->timing_info.num_units_in_display_tick;
2341 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2342 cm->timing_info.equal_picture_interval =
2343 oxcf->timing_info.equal_picture_interval;
2344 cm->timing_info.num_ticks_per_picture =
2345 oxcf->timing_info.num_ticks_per_picture;
2346
2347 seq_params->display_model_info_present_flag =
2348 oxcf->display_model_info_present_flag;
2349 seq_params->decoder_model_info_present_flag =
2350 oxcf->decoder_model_info_present_flag;
2351 if (oxcf->decoder_model_info_present_flag) {
2352 // set the decoder model parameters in schedule mode
2353 cm->buffer_model.num_units_in_decoding_tick =
2354 oxcf->buffer_model.num_units_in_decoding_tick;
2355 cm->buffer_removal_time_present = 1;
2356 set_aom_dec_model_info(&cm->buffer_model);
2357 set_dec_model_op_parameters(&cm->op_params[0]);
2358 } else if (cm->timing_info_present &&
2359 cm->timing_info.equal_picture_interval &&
2360 !seq_params->decoder_model_info_present_flag) {
2361 // set the decoder model parameters in resource availability mode
2362 set_resource_availability_parameters(&cm->op_params[0]);
2363 } else {
2364 cm->op_params[0].initial_display_delay =
2365 10; // Default value (not signaled)
2366 }
2367
2368 update_film_grain_parameters(cpi, oxcf);
2369
2370 cpi->oxcf = *oxcf;
2371 cpi->common.options = oxcf->cfg;
2372 cpi->row_mt = oxcf->row_mt;
2373 x->e_mbd.bd = (int)seq_params->bit_depth;
2374 x->e_mbd.global_motion = cm->global_motion;
2375
2376 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
2377 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2378 } else {
2379 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2380 }
2381
2382 cpi->refresh_last_frame = 1;
2383 cpi->refresh_golden_frame = 0;
2384 cpi->refresh_bwd_ref_frame = 0;
2385 cpi->refresh_alt2_ref_frame = 0;
2386
2387 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2388 ? REFRESH_FRAME_CONTEXT_DISABLED
2389 : REFRESH_FRAME_CONTEXT_BACKWARD;
2390 if (oxcf->large_scale_tile)
2391 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
2392
2393 if (x->palette_buffer == NULL) {
2394 CHECK_MEM_ERROR(cm, x->palette_buffer,
2395 aom_memalign(16, sizeof(*x->palette_buffer)));
2396 }
2397
2398 if (x->tmp_conv_dst == NULL) {
2399 CHECK_MEM_ERROR(
2400 cm, x->tmp_conv_dst,
2401 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
2402 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
2403 }
2404 for (int i = 0; i < 2; ++i) {
2405 if (x->tmp_obmc_bufs[i] == NULL) {
2406 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
2407 aom_memalign(16, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
2408 sizeof(*x->tmp_obmc_bufs[i])));
2409 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
2410 }
2411 }
2412
2413 av1_reset_segment_features(cm);
2414 set_high_precision_mv(cpi, 1, 0);
2415
2416 set_rc_buffer_sizes(rc, &cpi->oxcf);
2417
2418 // Under a configuration change, where maximum_buffer_size may change,
2419 // keep buffer level clipped to the maximum allowed buffer size.
2420 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2421 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
2422
2423 // Set up frame rate and related parameters rate control values.
2424 av1_new_framerate(cpi, cpi->framerate);
2425
2426 // Set absolute upper and lower quality limits
2427 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2428 rc->best_quality = cpi->oxcf.best_allowed_q;
2429
2430 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
2431 cm->switchable_motion_mode = 1;
2432
2433 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2434 cm->render_width = cpi->oxcf.render_width;
2435 cm->render_height = cpi->oxcf.render_height;
2436 } else {
2437 cm->render_width = cpi->oxcf.width;
2438 cm->render_height = cpi->oxcf.height;
2439 }
2440 cm->width = cpi->oxcf.width;
2441 cm->height = cpi->oxcf.height;
2442
2443 int sb_size = seq_params->sb_size;
2444 // Superblock size should not be updated after the first key frame.
2445 if (!cpi->seq_params_locked) {
2446 set_sb_size(&cm->seq_params, select_sb_size(cpi));
2447 }
2448
2449 if (cpi->initial_width || sb_size != seq_params->sb_size) {
2450 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
2451 seq_params->sb_size != sb_size) {
2452 av1_free_context_buffers(cm);
2453 av1_free_pc_tree(&cpi->td, num_planes);
2454 alloc_compressor_data(cpi);
2455 realloc_segmentation_maps(cpi);
2456 cpi->initial_width = cpi->initial_height = 0;
2457 }
2458 }
2459 update_frame_size(cpi);
2460
2461 cpi->alt_ref_source = NULL;
2462 rc->is_src_frame_alt_ref = 0;
2463
2464 rc->is_bwd_ref_frame = 0;
2465 rc->is_last_bipred_frame = 0;
2466 rc->is_bipred_frame = 0;
2467
2468 set_tile_info(cpi);
2469
2470 cpi->ext_refresh_frame_flags_pending = 0;
2471 cpi->ext_refresh_frame_context_pending = 0;
2472
2473 highbd_set_var_fns(cpi);
2474
2475 // Init sequence level coding tools
2476 // This should not be called after the first key frame.
2477 if (!cpi->seq_params_locked) {
2478 seq_params->operating_points_cnt_minus_1 =
2479 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
2480 init_seq_coding_tools(&cm->seq_params, cm, oxcf);
2481 }
2482 }
2483
av1_create_compressor(AV1EncoderConfig * oxcf,BufferPool * const pool)2484 AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2485 BufferPool *const pool) {
2486 unsigned int i;
2487 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2488 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
2489
2490 if (!cm) return NULL;
2491
2492 av1_zero(*cpi);
2493
2494 // The jmp_buf is valid only for the duration of the function that calls
2495 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2496 // before it returns.
2497 if (setjmp(cm->error.jmp)) {
2498 cm->error.setjmp = 0;
2499 av1_remove_compressor(cpi);
2500 return 0;
2501 }
2502
2503 cm->error.setjmp = 1;
2504 cm->alloc_mi = enc_alloc_mi;
2505 cm->free_mi = enc_free_mi;
2506 cm->setup_mi = enc_setup_mi;
2507
2508 CHECK_MEM_ERROR(cm, cm->fc,
2509 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2510 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2511 (FRAME_CONTEXT *)aom_memalign(
2512 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2513 memset(cm->fc, 0, sizeof(*cm->fc));
2514 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
2515
2516 cpi->resize_state = 0;
2517 cpi->resize_avg_qp = 0;
2518 cpi->resize_buffer_underflow = 0;
2519
2520 cpi->common.buffer_pool = pool;
2521
2522 init_config(cpi, oxcf);
2523 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
2524
2525 cm->current_video_frame = 0;
2526 cpi->seq_params_locked = 0;
2527 cpi->partition_search_skippable_frame = 0;
2528 cpi->tile_data = NULL;
2529 cpi->last_show_frame_buf_idx = INVALID_IDX;
2530
2531 realloc_segmentation_maps(cpi);
2532
2533 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2534 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
2535
2536 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2537 i++) {
2538 CHECK_MEM_ERROR(
2539 cm, cpi->mbgraph_stats[i].mb_stats,
2540 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
2541 }
2542
2543 #if CONFIG_FP_MB_STATS
2544 cpi->use_fp_mb_stats = 0;
2545 if (cpi->use_fp_mb_stats) {
2546 // a place holder used to store the first pass mb stats in the first pass
2547 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
2548 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
2549 } else {
2550 cpi->twopass.frame_mb_stats_buf = NULL;
2551 }
2552 #endif
2553
2554 cpi->refresh_alt_ref_frame = 0;
2555
2556 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2557 #if CONFIG_INTERNAL_STATS
2558 cpi->b_calculate_blockiness = 1;
2559 cpi->b_calculate_consistency = 1;
2560 cpi->total_inconsistency = 0;
2561 cpi->psnr.worst = 100.0;
2562 cpi->worst_ssim = 100.0;
2563
2564 cpi->count = 0;
2565 cpi->bytes = 0;
2566
2567 if (cpi->b_calculate_psnr) {
2568 cpi->total_sq_error = 0;
2569 cpi->total_samples = 0;
2570 cpi->tot_recode_hits = 0;
2571 cpi->summed_quality = 0;
2572 cpi->summed_weights = 0;
2573 }
2574
2575 cpi->fastssim.worst = 100.0;
2576 cpi->psnrhvs.worst = 100.0;
2577
2578 if (cpi->b_calculate_blockiness) {
2579 cpi->total_blockiness = 0;
2580 cpi->worst_blockiness = 0.0;
2581 }
2582
2583 if (cpi->b_calculate_consistency) {
2584 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
2585 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
2586 cpi->common.mi_rows * cpi->common.mi_cols));
2587 cpi->worst_consistency = 100.0;
2588 }
2589 #endif
2590 #if CONFIG_ENTROPY_STATS
2591 av1_zero(aggregate_fc);
2592 #endif // CONFIG_ENTROPY_STATS
2593
2594 cpi->first_time_stamp_ever = INT64_MAX;
2595
2596 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2597 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2598 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2599 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
2600
2601 #ifdef OUTPUT_YUV_SKINMAP
2602 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2603 #endif
2604 #ifdef OUTPUT_YUV_REC
2605 yuv_rec_file = fopen("rec.yuv", "wb");
2606 #endif
2607
2608 if (oxcf->pass == 1) {
2609 av1_init_first_pass(cpi);
2610 } else if (oxcf->pass == 2) {
2611 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2612 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2613
2614 #if CONFIG_FP_MB_STATS
2615 if (cpi->use_fp_mb_stats) {
2616 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2617 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2618
2619 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2620 oxcf->firstpass_mb_stats_in.buf;
2621 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2622 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2623 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2624 }
2625 #endif
2626
2627 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2628 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2629 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2630
2631 av1_init_second_pass(cpi);
2632 }
2633
2634 CHECK_MEM_ERROR(
2635 cm, cpi->td.mb.above_pred_buf,
2636 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
2637 sizeof(*cpi->td.mb.above_pred_buf)));
2638 CHECK_MEM_ERROR(
2639 cm, cpi->td.mb.left_pred_buf,
2640 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
2641 sizeof(*cpi->td.mb.left_pred_buf)));
2642
2643 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2644 (int32_t *)aom_memalign(
2645 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2646
2647 for (int x = 0; x < 2; x++)
2648 for (int y = 0; y < 2; y++)
2649 CHECK_MEM_ERROR(
2650 cm, cpi->td.mb.hash_value_buffer[x][y],
2651 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2652 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2653
2654 cpi->td.mb.g_crc_initialized = 0;
2655
2656 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2657 (int32_t *)aom_memalign(
2658 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2659
2660 av1_set_speed_features_framesize_independent(cpi);
2661 av1_set_speed_features_framesize_dependent(cpi);
2662
2663 #define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2664 cpi->fn_ptr[BT].sdf = SDF; \
2665 cpi->fn_ptr[BT].sdaf = SDAF; \
2666 cpi->fn_ptr[BT].vf = VF; \
2667 cpi->fn_ptr[BT].svf = SVF; \
2668 cpi->fn_ptr[BT].svaf = SVAF; \
2669 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2670 cpi->fn_ptr[BT].jsdaf = JSDAF; \
2671 cpi->fn_ptr[BT].jsvaf = JSVAF;
2672
2673 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2674 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
2675 aom_sad4x16x4d, aom_jnt_sad4x16_avg, aom_jnt_sub_pixel_avg_variance4x16)
2676
2677 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2678 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
2679 aom_sad16x4x4d, aom_jnt_sad16x4_avg, aom_jnt_sub_pixel_avg_variance16x4)
2680
2681 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2682 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
2683 aom_sad8x32x4d, aom_jnt_sad8x32_avg, aom_jnt_sub_pixel_avg_variance8x32)
2684
2685 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2686 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
2687 aom_sad32x8x4d, aom_jnt_sad32x8_avg, aom_jnt_sub_pixel_avg_variance32x8)
2688
2689 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2690 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
2691 aom_sad16x64x4d, aom_jnt_sad16x64_avg,
2692 aom_jnt_sub_pixel_avg_variance16x64)
2693
2694 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2695 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
2696 aom_sad64x16x4d, aom_jnt_sad64x16_avg,
2697 aom_jnt_sub_pixel_avg_variance64x16)
2698
2699 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2700 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2701 aom_sad128x128x4d, aom_jnt_sad128x128_avg,
2702 aom_jnt_sub_pixel_avg_variance128x128)
2703
2704 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2705 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
2706 aom_sad128x64x4d, aom_jnt_sad128x64_avg,
2707 aom_jnt_sub_pixel_avg_variance128x64)
2708
2709 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2710 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
2711 aom_sad64x128x4d, aom_jnt_sad64x128_avg,
2712 aom_jnt_sub_pixel_avg_variance64x128)
2713
2714 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2715 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
2716 aom_sad32x16x4d, aom_jnt_sad32x16_avg,
2717 aom_jnt_sub_pixel_avg_variance32x16)
2718
2719 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2720 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
2721 aom_sad16x32x4d, aom_jnt_sad16x32_avg,
2722 aom_jnt_sub_pixel_avg_variance16x32)
2723
2724 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2725 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
2726 aom_sad64x32x4d, aom_jnt_sad64x32_avg,
2727 aom_jnt_sub_pixel_avg_variance64x32)
2728
2729 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2730 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
2731 aom_sad32x64x4d, aom_jnt_sad32x64_avg,
2732 aom_jnt_sub_pixel_avg_variance32x64)
2733
2734 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2735 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2736 aom_sad32x32x4d, aom_jnt_sad32x32_avg,
2737 aom_jnt_sub_pixel_avg_variance32x32)
2738
2739 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2740 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2741 aom_sad64x64x4d, aom_jnt_sad64x64_avg,
2742 aom_jnt_sub_pixel_avg_variance64x64)
2743
2744 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2745 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2746 aom_sad16x16x4d, aom_jnt_sad16x16_avg,
2747 aom_jnt_sub_pixel_avg_variance16x16)
2748
2749 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2750 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
2751 aom_sad16x8x4d, aom_jnt_sad16x8_avg, aom_jnt_sub_pixel_avg_variance16x8)
2752
2753 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2754 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
2755 aom_sad8x16x4d, aom_jnt_sad8x16_avg, aom_jnt_sub_pixel_avg_variance8x16)
2756
2757 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2758 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
2759 aom_jnt_sad8x8_avg, aom_jnt_sub_pixel_avg_variance8x8)
2760
2761 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2762 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
2763 aom_jnt_sad8x4_avg, aom_jnt_sub_pixel_avg_variance8x4)
2764
2765 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2766 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
2767 aom_jnt_sad4x8_avg, aom_jnt_sub_pixel_avg_variance4x8)
2768
2769 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2770 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
2771 aom_jnt_sad4x4_avg, aom_jnt_sub_pixel_avg_variance4x4)
2772
2773 #define OBFP(BT, OSDF, OVF, OSVF) \
2774 cpi->fn_ptr[BT].osdf = OSDF; \
2775 cpi->fn_ptr[BT].ovf = OVF; \
2776 cpi->fn_ptr[BT].osvf = OSVF;
2777
2778 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2779 aom_obmc_sub_pixel_variance128x128)
2780 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2781 aom_obmc_sub_pixel_variance128x64)
2782 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2783 aom_obmc_sub_pixel_variance64x128)
2784 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2785 aom_obmc_sub_pixel_variance64x64)
2786 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2787 aom_obmc_sub_pixel_variance64x32)
2788 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2789 aom_obmc_sub_pixel_variance32x64)
2790 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2791 aom_obmc_sub_pixel_variance32x32)
2792 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2793 aom_obmc_sub_pixel_variance32x16)
2794 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2795 aom_obmc_sub_pixel_variance16x32)
2796 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2797 aom_obmc_sub_pixel_variance16x16)
2798 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2799 aom_obmc_sub_pixel_variance16x8)
2800 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2801 aom_obmc_sub_pixel_variance8x16)
2802 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2803 aom_obmc_sub_pixel_variance8x8)
2804 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2805 aom_obmc_sub_pixel_variance4x8)
2806 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2807 aom_obmc_sub_pixel_variance8x4)
2808 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2809 aom_obmc_sub_pixel_variance4x4)
2810 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2811 aom_obmc_sub_pixel_variance4x16)
2812 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2813 aom_obmc_sub_pixel_variance16x4)
2814 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2815 aom_obmc_sub_pixel_variance8x32)
2816 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2817 aom_obmc_sub_pixel_variance32x8)
2818 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2819 aom_obmc_sub_pixel_variance16x64)
2820 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2821 aom_obmc_sub_pixel_variance64x16)
2822
2823 #define MBFP(BT, MCSDF, MCSVF) \
2824 cpi->fn_ptr[BT].msdf = MCSDF; \
2825 cpi->fn_ptr[BT].msvf = MCSVF;
2826
2827 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2828 aom_masked_sub_pixel_variance128x128)
2829 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2830 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
2831 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2832 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2833 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2834 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2835 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2836 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2837 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2838 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2839 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2840 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2841 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2842 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2843 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
2844
2845 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2846
2847 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2848
2849 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2850
2851 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
2852
2853 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2854
2855 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
2856
2857 highbd_set_var_fns(cpi);
2858
2859 /* av1_init_quantizer() is first called here. Add check in
2860 * av1_frame_init_quantizer() so that av1_init_quantizer is only
2861 * called later when needed. This will avoid unnecessary calls of
2862 * av1_init_quantizer() for every frame.
2863 */
2864 av1_init_quantizer(cpi);
2865 av1_qm_init(cm);
2866
2867 av1_loop_filter_init(cm);
2868 cm->superres_scale_denominator = SCALE_NUMERATOR;
2869 cm->superres_upscaled_width = oxcf->width;
2870 cm->superres_upscaled_height = oxcf->height;
2871 av1_loop_restoration_precal();
2872
2873 cm->error.setjmp = 0;
2874
2875 return cpi;
2876 }
2877
2878 #if CONFIG_INTERNAL_STATS
2879 #define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2880
2881 #define SNPRINT2(H, T, V) \
2882 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2883 #endif // CONFIG_INTERNAL_STATS
2884
av1_remove_compressor(AV1_COMP * cpi)2885 void av1_remove_compressor(AV1_COMP *cpi) {
2886 AV1_COMMON *cm;
2887 unsigned int i;
2888 int t;
2889
2890 if (!cpi) return;
2891
2892 cm = &cpi->common;
2893 const int num_planes = av1_num_planes(cm);
2894
2895 if (cm->current_video_frame > 0) {
2896 #if CONFIG_ENTROPY_STATS
2897 if (cpi->oxcf.pass != 1) {
2898 fprintf(stderr, "Writing counts.stt\n");
2899 FILE *f = fopen("counts.stt", "wb");
2900 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
2901 fclose(f);
2902 }
2903 #endif // CONFIG_ENTROPY_STATS
2904 #if CONFIG_INTERNAL_STATS
2905 aom_clear_system_state();
2906
2907 if (cpi->oxcf.pass != 1) {
2908 char headings[512] = { 0 };
2909 char results[512] = { 0 };
2910 FILE *f = fopen("opsnr.stt", "a");
2911 double time_encoded =
2912 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2913 10000000.000;
2914 double total_encode_time =
2915 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2916 const double dr =
2917 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2918 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2919 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2920 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2921
2922 if (cpi->b_calculate_psnr) {
2923 const double total_psnr = aom_sse_to_psnr(
2924 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2925 const double total_ssim =
2926 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2927 snprintf(headings, sizeof(headings),
2928 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
2929 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
2930 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
2931 "AVPsrnY\tAPsnrCb\tAPsnrCr");
2932 snprintf(results, sizeof(results),
2933 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2934 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2935 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2936 "%7.3f\t%7.3f\t%7.3f",
2937 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
2938 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
2939 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
2940 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
2941 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
2942 cpi->psnr.stat[STAT_Y] / cpi->count,
2943 cpi->psnr.stat[STAT_U] / cpi->count,
2944 cpi->psnr.stat[STAT_V] / cpi->count);
2945
2946 if (cpi->b_calculate_blockiness) {
2947 SNPRINT(headings, "\t Block\tWstBlck");
2948 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2949 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2950 }
2951
2952 if (cpi->b_calculate_consistency) {
2953 double consistency =
2954 aom_sse_to_psnr((double)cpi->total_samples, peak,
2955 (double)cpi->total_inconsistency);
2956
2957 SNPRINT(headings, "\tConsist\tWstCons");
2958 SNPRINT2(results, "\t%7.3f", consistency);
2959 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2960 }
2961 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2962 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
2963 rate_err, fabs(rate_err));
2964 }
2965
2966 fclose(f);
2967 }
2968 #endif // CONFIG_INTERNAL_STATS
2969 }
2970
2971 for (t = 0; t < cpi->num_workers; ++t) {
2972 AVxWorker *const worker = &cpi->workers[t];
2973 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2974
2975 // Deallocate allocated threads.
2976 aom_get_worker_interface()->end(worker);
2977
2978 // Deallocate allocated thread data.
2979 if (t < cpi->num_workers - 1) {
2980 aom_free(thread_data->td->palette_buffer);
2981 aom_free(thread_data->td->tmp_conv_dst);
2982 for (int j = 0; j < 2; ++j) {
2983 aom_free(thread_data->td->tmp_obmc_bufs[j]);
2984 }
2985 aom_free(thread_data->td->above_pred_buf);
2986 aom_free(thread_data->td->left_pred_buf);
2987 aom_free(thread_data->td->wsrc_buf);
2988 for (int x = 0; x < 2; x++) {
2989 for (int y = 0; y < 2; y++) {
2990 aom_free(thread_data->td->hash_value_buffer[x][y]);
2991 thread_data->td->hash_value_buffer[x][y] = NULL;
2992 }
2993 }
2994 aom_free(thread_data->td->mask_buf);
2995 aom_free(thread_data->td->counts);
2996 av1_free_pc_tree(thread_data->td, num_planes);
2997 aom_free(thread_data->td);
2998 }
2999 }
3000 aom_free(cpi->tile_thr_data);
3001 aom_free(cpi->workers);
3002
3003 if (cpi->num_workers > 1) {
3004 av1_loop_filter_dealloc(&cpi->lf_row_sync);
3005 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
3006 }
3007
3008 dealloc_compressor_data(cpi);
3009
3010 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3011 ++i) {
3012 aom_free(cpi->mbgraph_stats[i].mb_stats);
3013 }
3014
3015 #if CONFIG_FP_MB_STATS
3016 if (cpi->use_fp_mb_stats) {
3017 aom_free(cpi->twopass.frame_mb_stats_buf);
3018 cpi->twopass.frame_mb_stats_buf = NULL;
3019 }
3020 #endif
3021 #if CONFIG_INTERNAL_STATS
3022 aom_free(cpi->ssim_vars);
3023 cpi->ssim_vars = NULL;
3024 #endif // CONFIG_INTERNAL_STATS
3025
3026 av1_remove_common(cm);
3027 for (i = 0; i < FRAME_BUFFERS; ++i) {
3028 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3029 }
3030 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
3031 av1_free_ref_frame_buffers(cm->buffer_pool);
3032 aom_free(cpi);
3033
3034 #ifdef OUTPUT_YUV_SKINMAP
3035 fclose(yuv_skinmap_file);
3036 #endif
3037 #ifdef OUTPUT_YUV_REC
3038 fclose(yuv_rec_file);
3039 #endif
3040 }
3041
generate_psnr_packet(AV1_COMP * cpi)3042 static void generate_psnr_packet(AV1_COMP *cpi) {
3043 struct aom_codec_cx_pkt pkt;
3044 int i;
3045 PSNR_STATS psnr;
3046 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
3047 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3048
3049 for (i = 0; i < 4; ++i) {
3050 pkt.data.psnr.samples[i] = psnr.samples[i];
3051 pkt.data.psnr.sse[i] = psnr.sse[i];
3052 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3053 }
3054 pkt.kind = AOM_CODEC_PSNR_PKT;
3055 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
3056 }
3057
av1_use_as_reference(AV1_COMP * cpi,int ref_frame_flags)3058 int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
3059 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3060
3061 cpi->ext_ref_frame_flags = ref_frame_flags;
3062 return 0;
3063 }
3064
av1_update_reference(AV1_COMP * cpi,int ref_frame_upd_flags)3065 void av1_update_reference(AV1_COMP *cpi, int ref_frame_upd_flags) {
3066 cpi->ext_refresh_last_frame = (ref_frame_upd_flags & AOM_LAST_FLAG) != 0;
3067 cpi->ext_refresh_golden_frame = (ref_frame_upd_flags & AOM_GOLD_FLAG) != 0;
3068 cpi->ext_refresh_alt_ref_frame = (ref_frame_upd_flags & AOM_ALT_FLAG) != 0;
3069 cpi->ext_refresh_bwd_ref_frame = (ref_frame_upd_flags & AOM_BWD_FLAG) != 0;
3070 cpi->ext_refresh_alt2_ref_frame = (ref_frame_upd_flags & AOM_ALT2_FLAG) != 0;
3071 cpi->ext_refresh_frame_flags_pending = 1;
3072 }
3073
av1_copy_reference_enc(AV1_COMP * cpi,int idx,YV12_BUFFER_CONFIG * sd)3074 int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3075 AV1_COMMON *const cm = &cpi->common;
3076 const int num_planes = av1_num_planes(cm);
3077 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
3078 if (cfg) {
3079 aom_yv12_copy_frame(cfg, sd, num_planes);
3080 return 0;
3081 } else {
3082 return -1;
3083 }
3084 }
3085
av1_set_reference_enc(AV1_COMP * cpi,int idx,YV12_BUFFER_CONFIG * sd)3086 int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3087 AV1_COMMON *const cm = &cpi->common;
3088 const int num_planes = av1_num_planes(cm);
3089 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
3090 if (cfg) {
3091 aom_yv12_copy_frame(sd, cfg, num_planes);
3092 return 0;
3093 } else {
3094 return -1;
3095 }
3096 }
3097
av1_update_entropy(AV1_COMP * cpi,int update)3098 int av1_update_entropy(AV1_COMP *cpi, int update) {
3099 cpi->ext_refresh_frame_context = update;
3100 cpi->ext_refresh_frame_context_pending = 1;
3101 return 0;
3102 }
3103
3104 #if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3105 // The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3106 // as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3107 // not denoise the UV channels at this time. If ever we implement UV channel
3108 // denoising we will have to modify this.
aom_write_yuv_frame_420(YV12_BUFFER_CONFIG * s,FILE * f)3109 void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
3110 uint8_t *src = s->y_buffer;
3111 int h = s->y_height;
3112
3113 do {
3114 fwrite(src, s->y_width, 1, f);
3115 src += s->y_stride;
3116 } while (--h);
3117
3118 src = s->u_buffer;
3119 h = s->uv_height;
3120
3121 do {
3122 fwrite(src, s->uv_width, 1, f);
3123 src += s->uv_stride;
3124 } while (--h);
3125
3126 src = s->v_buffer;
3127 h = s->uv_height;
3128
3129 do {
3130 fwrite(src, s->uv_width, 1, f);
3131 src += s->uv_stride;
3132 } while (--h);
3133 }
3134 #endif
3135
check_show_existing_frame(AV1_COMP * cpi)3136 static void check_show_existing_frame(AV1_COMP *cpi) {
3137 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3138 AV1_COMMON *const cm = &cpi->common;
3139 const FRAME_UPDATE_TYPE next_frame_update_type =
3140 gf_group->update_type[gf_group->index];
3141 #if USE_SYMM_MULTI_LAYER
3142 const int which_arf = (cpi->new_bwdref_update_rule == 1)
3143 ? gf_group->arf_update_idx[gf_group->index] > 0
3144 : gf_group->arf_update_idx[gf_group->index];
3145 #else
3146 const int which_arf = gf_group->arf_update_idx[gf_group->index];
3147 #endif
3148
3149 if (cm->show_existing_frame == 1) {
3150 cm->show_existing_frame = 0;
3151 } else if (cpi->rc.is_last_bipred_frame) {
3152 #if USE_SYMM_MULTI_LAYER
3153 // NOTE: When new structure is used, every bwdref will have one overlay
3154 // frame. Therefore, there is no need to find out which frame to
3155 // show in advance.
3156 if (cpi->new_bwdref_update_rule == 0) {
3157 #endif
3158 // NOTE: If the current frame is a last bi-predictive frame, it is
3159 // needed next to show the BWDREF_FRAME, which is pointed by
3160 // the last_fb_idxes[0] after reference frame buffer update
3161 cpi->rc.is_last_bipred_frame = 0;
3162 cm->show_existing_frame = 1;
3163 cpi->existing_fb_idx_to_show = cpi->ref_fb_idx[0];
3164 #if USE_SYMM_MULTI_LAYER
3165 }
3166 #endif
3167 } else if (cpi->is_arf_filter_off[which_arf] &&
3168 (next_frame_update_type == OVERLAY_UPDATE ||
3169 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
3170 #if USE_SYMM_MULTI_LAYER
3171 const int bwdref_to_show =
3172 (cpi->new_bwdref_update_rule == 1) ? BWDREF_FRAME : ALTREF2_FRAME;
3173 #else
3174 const int bwdref_to_show = ALTREF2_FRAME;
3175 #endif
3176 // Other parameters related to OVERLAY_UPDATE will be taken care of
3177 // in av1_rc_get_second_pass_params(cpi)
3178 cm->show_existing_frame = 1;
3179 cpi->rc.is_src_frame_alt_ref = 1;
3180 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3181 ? cpi->ref_fb_idx[ALTREF_FRAME - 1]
3182 : cpi->ref_fb_idx[bwdref_to_show - 1];
3183 #if USE_SYMM_MULTI_LAYER
3184 if (cpi->new_bwdref_update_rule == 0)
3185 #endif
3186 cpi->is_arf_filter_off[which_arf] = 0;
3187 }
3188 cpi->rc.is_src_frame_ext_arf = 0;
3189 }
3190
3191 #ifdef OUTPUT_YUV_REC
aom_write_one_yuv_frame(AV1_COMMON * cm,YV12_BUFFER_CONFIG * s)3192 void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
3193 uint8_t *src = s->y_buffer;
3194 int h = cm->height;
3195 if (yuv_rec_file == NULL) return;
3196 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3197 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3198
3199 do {
3200 fwrite(src16, s->y_width, 2, yuv_rec_file);
3201 src16 += s->y_stride;
3202 } while (--h);
3203
3204 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3205 h = s->uv_height;
3206
3207 do {
3208 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3209 src16 += s->uv_stride;
3210 } while (--h);
3211
3212 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3213 h = s->uv_height;
3214
3215 do {
3216 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3217 src16 += s->uv_stride;
3218 } while (--h);
3219
3220 fflush(yuv_rec_file);
3221 return;
3222 }
3223
3224 do {
3225 fwrite(src, s->y_width, 1, yuv_rec_file);
3226 src += s->y_stride;
3227 } while (--h);
3228
3229 src = s->u_buffer;
3230 h = s->uv_height;
3231
3232 do {
3233 fwrite(src, s->uv_width, 1, yuv_rec_file);
3234 src += s->uv_stride;
3235 } while (--h);
3236
3237 src = s->v_buffer;
3238 h = s->uv_height;
3239
3240 do {
3241 fwrite(src, s->uv_width, 1, yuv_rec_file);
3242 src += s->uv_stride;
3243 } while (--h);
3244
3245 fflush(yuv_rec_file);
3246 }
3247 #endif // OUTPUT_YUV_REC
3248
3249 #define GM_RECODE_LOOP_NUM4X4_FACTOR 192
recode_loop_test_global_motion(AV1_COMP * cpi)3250 static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3251 int i;
3252 int recode = 0;
3253 RD_COUNTS *const rdc = &cpi->td.rd_counts;
3254 AV1_COMMON *const cm = &cpi->common;
3255 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3256 if (cm->global_motion[i].wmtype != IDENTITY &&
3257 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
3258 cpi->gmparams_cost[i]) {
3259 cm->global_motion[i] = default_warp_params;
3260 assert(cm->global_motion[i].wmtype == IDENTITY);
3261 cpi->gmparams_cost[i] = 0;
3262 recode = 1;
3263 // TODO(sarahparker): The earlier condition for recoding here was:
3264 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3265 // similar to that back to speed up global motion?
3266 }
3267 }
3268 return recode;
3269 }
3270
3271 // Function to test for conditions that indicate we should loop
3272 // back and recode a frame.
recode_loop_test(AV1_COMP * cpi,int high_limit,int low_limit,int q,int maxq,int minq)3273 static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3274 int maxq, int minq) {
3275 const RATE_CONTROL *const rc = &cpi->rc;
3276 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3277 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3278 int force_recode = 0;
3279
3280 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3281 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3282 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
3283 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3284 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3285 (rc->projected_frame_size < low_limit && q > minq)) {
3286 force_recode = 1;
3287 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
3288 // Deal with frame undershoot and whether or not we are
3289 // below the automatically set cq level.
3290 if (q > oxcf->cq_level &&
3291 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3292 force_recode = 1;
3293 }
3294 }
3295 }
3296 return force_recode;
3297 }
3298
3299 #define DUMP_REF_FRAME_IMAGES 0
3300
3301 #if DUMP_REF_FRAME_IMAGES == 1
dump_one_image(AV1_COMMON * cm,const YV12_BUFFER_CONFIG * const ref_buf,char * file_name)3302 static int dump_one_image(AV1_COMMON *cm,
3303 const YV12_BUFFER_CONFIG *const ref_buf,
3304 char *file_name) {
3305 int h;
3306 FILE *f_ref = NULL;
3307
3308 if (ref_buf == NULL) {
3309 printf("Frame data buffer is NULL.\n");
3310 return AOM_CODEC_MEM_ERROR;
3311 }
3312
3313 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3314 printf("Unable to open file %s to write.\n", file_name);
3315 return AOM_CODEC_MEM_ERROR;
3316 }
3317
3318 // --- Y ---
3319 for (h = 0; h < cm->height; ++h) {
3320 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3321 }
3322 // --- U ---
3323 for (h = 0; h < (cm->height >> 1); ++h) {
3324 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3325 f_ref);
3326 }
3327 // --- V ---
3328 for (h = 0; h < (cm->height >> 1); ++h) {
3329 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3330 f_ref);
3331 }
3332
3333 fclose(f_ref);
3334
3335 return AOM_CODEC_OK;
3336 }
3337
dump_ref_frame_images(AV1_COMP * cpi)3338 static void dump_ref_frame_images(AV1_COMP *cpi) {
3339 AV1_COMMON *const cm = &cpi->common;
3340 MV_REFERENCE_FRAME ref_frame;
3341
3342 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3343 char file_name[256] = "";
3344 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3345 cm->current_video_frame, ref_frame);
3346 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3347 }
3348 }
3349 #endif // DUMP_REF_FRAME_IMAGES == 1
3350
3351 // This function is used to shift the virtual indices of last reference frames
3352 // as follows:
3353 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3354 // when the LAST_FRAME is updated.
shift_last_ref_frames(AV1_COMP * cpi)3355 static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
3356 // TODO(isbs): shift the scaled indices as well
3357 int ref_frame;
3358 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
3359 cpi->ref_fb_idx[ref_frame] = cpi->ref_fb_idx[ref_frame - 1];
3360
3361 // [0] is allocated to the current coded frame. The statistics for the
3362 // reference frames start at [LAST_FRAME], i.e. [1].
3363 if (!cpi->rc.is_src_frame_alt_ref) {
3364 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3365 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3366 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
3367 }
3368 }
3369 }
3370
3371 #if USE_SYMM_MULTI_LAYER
3372 // This function is used to shift the virtual indices of bwd reference
3373 // frames as follows:
3374 // BWD_REF -> ALT2_REF -> EXT_REF
3375 // to clear a space to store the closest bwdref
rshift_bwd_ref_frames(AV1_COMP * cpi)3376 static INLINE void rshift_bwd_ref_frames(AV1_COMP *cpi) {
3377 // TODO(isbs): shift the scaled indices as well
3378 static const int ordered_bwd[3] = { BWDREF_FRAME - 1, ALTREF2_FRAME - 1,
3379 EXTREF_FRAME - 1 };
3380
3381 for (int i = 2; i > 0; --i) {
3382 // [0] is allocated to the current coded frame, i.e. bwdref
3383 memcpy(
3384 cpi->interp_filter_selected[ordered_bwd[i] + LAST_FRAME],
3385 cpi->interp_filter_selected[ordered_bwd[i - 1] + LAST_FRAME],
3386 sizeof(cpi->interp_filter_selected[ordered_bwd[i - 1] + LAST_FRAME]));
3387
3388 cpi->ref_fb_idx[ordered_bwd[i]] = cpi->ref_fb_idx[ordered_bwd[i - 1]];
3389 }
3390 }
3391
3392 // This function is used to shift the virtual indices of bwd reference
3393 // frames as follows:
3394 // BWD_REF <- ALT2_REF <- EXT_REF
3395 // to update the bwd reference frame for coding the next frame.
lshift_bwd_ref_frames(AV1_COMP * cpi)3396 static INLINE void lshift_bwd_ref_frames(AV1_COMP *cpi) {
3397 // TODO(isbs): shift the scaled indices as well
3398 static const int ordered_bwd[3] = { BWDREF_FRAME - 1, ALTREF2_FRAME - 1,
3399 EXTREF_FRAME - 1 };
3400
3401 for (int i = 0; i < 2; ++i) {
3402 // [0] is allocated to the current coded frame, i.e. bwdref
3403 memcpy(
3404 cpi->interp_filter_selected[ordered_bwd[i] + LAST_FRAME],
3405 cpi->interp_filter_selected[ordered_bwd[i + 1] + LAST_FRAME],
3406 sizeof(cpi->interp_filter_selected[ordered_bwd[i + 1] + LAST_FRAME]));
3407
3408 cpi->ref_fb_idx[ordered_bwd[i]] = cpi->ref_fb_idx[ordered_bwd[i + 1]];
3409 }
3410 }
3411 #endif // USE_SYMM_MULTI_LAYER
3412
update_reference_frames(AV1_COMP * cpi)3413 static void update_reference_frames(AV1_COMP *cpi) {
3414 AV1_COMMON *const cm = &cpi->common;
3415
3416 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3417 // for the purpose to verify no mismatch between encoder and decoder.
3418 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3419
3420 // In the case of show_existing frame, we will not send fresh flag
3421 // to decoder. Any change in the reference frame buffer can be done by
3422 // switching the virtual indices.
3423 if (cm->show_existing_frame) {
3424 cpi->refresh_last_frame = 0;
3425 cpi->refresh_golden_frame = 0;
3426 cpi->refresh_bwd_ref_frame = 0;
3427 cpi->refresh_alt2_ref_frame = 0;
3428 cpi->refresh_alt_ref_frame = 0;
3429
3430 cpi->rc.is_bwd_ref_frame = 0;
3431 cpi->rc.is_last_bipred_frame = 0;
3432 cpi->rc.is_bipred_frame = 0;
3433 }
3434
3435 BufferPool *const pool = cm->buffer_pool;
3436
3437 // At this point the new frame has been encoded.
3438 // If any buffer copy / swapping is signaled it should be done here.
3439
3440 // Only update all of the reference buffers if a KEY_FRAME is also a
3441 // show_frame. This ensures a fwd keyframe does not update all of the buffers
3442 if ((cm->frame_type == KEY_FRAME && cm->show_frame) || frame_is_sframe(cm)) {
3443 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame) {
3444 ref_cnt_fb(pool->frame_bufs,
3445 &cm->ref_frame_map[cpi->ref_fb_idx[ref_frame]],
3446 cm->new_fb_idx);
3447 }
3448 return;
3449 }
3450
3451 if (av1_preserve_existing_gf(cpi)) {
3452 // We have decided to preserve the previously existing golden frame as our
3453 // new ARF frame. However, in the short term in function
3454 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
3455 // we're updating the GF with the current decoded frame, we save it to the
3456 // ARF slot instead.
3457 // We now have to update the ARF with the current frame and swap gld_fb_idx
3458 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3459 // slot and, if we're updating the GF, the current frame becomes the new GF.
3460 int tmp;
3461
3462 // ARF in general is a better reference than overlay. We shouldkeep ARF as
3463 // reference instead of replacing it with overlay.
3464
3465 if (!cpi->preserve_arf_as_gld) {
3466 ref_cnt_fb(pool->frame_bufs,
3467 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF_FRAME - 1]],
3468 cm->new_fb_idx);
3469 }
3470
3471 tmp = cpi->ref_fb_idx[ALTREF_FRAME - 1];
3472 cpi->ref_fb_idx[ALTREF_FRAME - 1] = cpi->ref_fb_idx[GOLDEN_FRAME - 1];
3473 cpi->ref_fb_idx[GOLDEN_FRAME - 1] = tmp;
3474
3475 // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3476 // cpi->interp_filter_selected[GOLDEN_FRAME]?
3477 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3478 #if CONFIG_DEBUG
3479 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3480 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
3481 #endif
3482 #if USE_SYMM_MULTI_LAYER
3483 const int bwdref_to_show =
3484 (cpi->new_bwdref_update_rule == 1) ? BWDREF_FRAME : ALTREF2_FRAME;
3485 #else
3486 const int bwdref_to_show = ALTREF2_FRAME;
3487 #endif
3488 // Deal with the special case for showing existing internal ALTREF_FRAME
3489 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3490 // by updating the virtual indices.
3491 const int tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
3492 shift_last_ref_frames(cpi);
3493
3494 cpi->ref_fb_idx[LAST_FRAME - 1] = cpi->ref_fb_idx[bwdref_to_show - 1];
3495
3496 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3497 cpi->interp_filter_selected[bwdref_to_show],
3498 sizeof(cpi->interp_filter_selected[bwdref_to_show]));
3499 #if USE_SYMM_MULTI_LAYER
3500 if (cpi->new_bwdref_update_rule == 1) {
3501 lshift_bwd_ref_frames(cpi);
3502 // pass outdated forward reference frame (previous LAST3) to the
3503 // spared space
3504 cpi->ref_fb_idx[EXTREF_FRAME - 1] = tmp;
3505 } else {
3506 #endif
3507 cpi->ref_fb_idx[bwdref_to_show - 1] = tmp;
3508 #if USE_SYMM_MULTI_LAYER
3509 }
3510 #endif
3511 } else { /* For non key/golden frames */
3512 // === ALTREF_FRAME ===
3513 if (cpi->refresh_alt_ref_frame) {
3514 int arf_idx = cpi->ref_fb_idx[ALTREF_FRAME - 1];
3515 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
3516
3517 memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
3518 cpi->interp_filter_selected[0],
3519 sizeof(cpi->interp_filter_selected[0]));
3520 }
3521
3522 // === GOLDEN_FRAME ===
3523 if (cpi->refresh_golden_frame) {
3524 ref_cnt_fb(pool->frame_bufs,
3525 &cm->ref_frame_map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]],
3526 cm->new_fb_idx);
3527
3528 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3529 cpi->interp_filter_selected[0],
3530 sizeof(cpi->interp_filter_selected[0]));
3531 }
3532
3533 // === BWDREF_FRAME ===
3534 if (cpi->refresh_bwd_ref_frame) {
3535 #if USE_SYMM_MULTI_LAYER
3536 if (cpi->new_bwdref_update_rule) {
3537 // We shift the backward reference frame as follows:
3538 // BWDREF -> ALTREF2 -> EXTREF
3539 // and assign the newly coded frame to BWDREF so that it always
3540 // keeps the nearest future frame
3541 int tmp = cpi->ref_fb_idx[EXTREF_FRAME - 1];
3542 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[tmp], cm->new_fb_idx);
3543
3544 rshift_bwd_ref_frames(cpi);
3545 cpi->ref_fb_idx[BWDREF_FRAME - 1] = tmp;
3546 } else {
3547 #endif // USE_SYMM_MULTI_LAYER
3548 ref_cnt_fb(pool->frame_bufs,
3549 &cm->ref_frame_map[cpi->ref_fb_idx[BWDREF_FRAME - 1]],
3550 cm->new_fb_idx);
3551 #if USE_SYMM_MULTI_LAYER
3552 }
3553 #endif
3554 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3555 cpi->interp_filter_selected[0],
3556 sizeof(cpi->interp_filter_selected[0]));
3557 }
3558
3559 // === ALTREF2_FRAME ===
3560 if (cpi->refresh_alt2_ref_frame) {
3561 ref_cnt_fb(pool->frame_bufs,
3562 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]],
3563 cm->new_fb_idx);
3564
3565 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3566 cpi->interp_filter_selected[0],
3567 sizeof(cpi->interp_filter_selected[0]));
3568 }
3569 }
3570
3571 if (cpi->refresh_last_frame) {
3572 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3573 // reference to the reference frame buffer virtual index; and then (2) from
3574 // the virtual index to the reference frame buffer physical index:
3575 //
3576 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3577 // | | |
3578 // v v v
3579 // ref_fb_idx[0], ..., ref_fb_idx[2], ..., ref_fb_idx[ALTREF_FRAME-1]
3580 // | | |
3581 // v v v
3582 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3583 //
3584 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3585 // have the other 2 LAST reference frames shifted as follows:
3586 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3587 // , and then have LAST_FRAME refreshed by the newly coded frame.
3588 //
3589 // To fulfill it, the decoder will be notified to execute following 2 steps:
3590 //
3591 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3592 // to point to the newly coded frame, i.e.
3593 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3594 //
3595 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3596 // original virtual index of LAST3_FRAME and have the other mappings
3597 // shifted as follows:
3598 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3599 // | | |
3600 // v v v
3601 // ref_fb_idx[2], ref_fb_idx[0], ref_fb_idx[1]
3602 int tmp;
3603
3604 ref_cnt_fb(pool->frame_bufs,
3605 &cm->ref_frame_map[cpi->ref_fb_idx[LAST_REF_FRAMES - 1]],
3606 cm->new_fb_idx);
3607
3608 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
3609
3610 shift_last_ref_frames(cpi);
3611 cpi->ref_fb_idx[0] = tmp;
3612
3613 assert(cm->show_existing_frame == 0);
3614 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3615 cpi->interp_filter_selected[0],
3616 sizeof(cpi->interp_filter_selected[0]));
3617
3618 // If the new structure is used, we will always have overlay frames coupled
3619 // with bwdref frames. Therefore, we won't have to perform this update
3620 // in advance (we do this update when the overlay frame shows up).
3621 #if USE_SYMM_MULTI_LAYER
3622 if (cpi->new_bwdref_update_rule == 0 && cpi->rc.is_last_bipred_frame) {
3623 #else
3624 if (cpi->rc.is_last_bipred_frame) {
3625 #endif
3626 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3627 // LAST3_FRAME by updating the virtual indices.
3628 //
3629 // NOTE: The source frame for BWDREF does not have a holding position as
3630 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3631 // virtual index reshuffling for BWDREF, the encoder always
3632 // specifies a LAST_BIPRED right before BWDREF and completes the
3633 // reshuffling job accordingly.
3634 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
3635
3636 shift_last_ref_frames(cpi);
3637 cpi->ref_fb_idx[0] = cpi->ref_fb_idx[BWDREF_FRAME - 1];
3638 cpi->ref_fb_idx[BWDREF_FRAME - 1] = tmp;
3639
3640 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3641 cpi->interp_filter_selected[BWDREF_FRAME],
3642 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
3643 }
3644 }
3645
3646 #if DUMP_REF_FRAME_IMAGES == 1
3647 // Dump out all reference frame images.
3648 dump_ref_frame_images(cpi);
3649 #endif // DUMP_REF_FRAME_IMAGES
3650 }
3651
3652 static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
3653 assert(buffer_idx != INVALID_IDX);
3654 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
3655 ensure_mv_buffer(new_fb_ptr, cm);
3656 new_fb_ptr->width = cm->width;
3657 new_fb_ptr->height = cm->height;
3658 }
3659
3660 static void scale_references(AV1_COMP *cpi) {
3661 AV1_COMMON *cm = &cpi->common;
3662 const int num_planes = av1_num_planes(cm);
3663 MV_REFERENCE_FRAME ref_frame;
3664 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
3665 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3666 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
3667 };
3668
3669 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3670 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
3671 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3672 BufferPool *const pool = cm->buffer_pool;
3673 const YV12_BUFFER_CONFIG *const ref =
3674 get_ref_frame_buffer(cpi, ref_frame);
3675
3676 if (ref == NULL) {
3677 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3678 continue;
3679 }
3680
3681 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3682 RefCntBuffer *new_fb_ptr = NULL;
3683 int force_scaling = 0;
3684 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3685 if (new_fb == INVALID_IDX) {
3686 new_fb = get_free_fb(cm);
3687 force_scaling = 1;
3688 }
3689 if (new_fb == INVALID_IDX) return;
3690 new_fb_ptr = &pool->frame_bufs[new_fb];
3691 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3692 new_fb_ptr->buf.y_crop_height != cm->height) {
3693 if (aom_realloc_frame_buffer(
3694 &new_fb_ptr->buf, cm->width, cm->height,
3695 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
3696 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
3697 cm->byte_alignment, NULL, NULL, NULL))
3698 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3699 "Failed to allocate frame buffer");
3700 av1_resize_and_extend_frame(
3701 ref, &new_fb_ptr->buf, (int)cm->seq_params.bit_depth, num_planes);
3702 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3703 alloc_frame_mvs(cm, new_fb);
3704 }
3705 } else {
3706 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3707 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3708 buf->buf.y_crop_width = ref->y_crop_width;
3709 buf->buf.y_crop_height = ref->y_crop_height;
3710 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3711 ++buf->ref_count;
3712 }
3713 } else {
3714 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3715 }
3716 }
3717 }
3718
3719 static void release_scaled_references(AV1_COMP *cpi) {
3720 AV1_COMMON *cm = &cpi->common;
3721 int i;
3722 // TODO(isbs): only refresh the necessary frames, rather than all of them
3723 for (i = 0; i < REF_FRAMES; ++i) {
3724 const int idx = cpi->scaled_ref_idx[i];
3725 RefCntBuffer *const buf =
3726 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3727 if (buf != NULL) {
3728 --buf->ref_count;
3729 cpi->scaled_ref_idx[i] = INVALID_IDX;
3730 }
3731 }
3732 }
3733
3734 static void set_mv_search_params(AV1_COMP *cpi) {
3735 const AV1_COMMON *const cm = &cpi->common;
3736 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
3737
3738 // Default based on max resolution.
3739 cpi->mv_step_param = av1_init_search_range(max_mv_def);
3740
3741 if (cpi->sf.mv.auto_mv_step_size) {
3742 if (frame_is_intra_only(cm)) {
3743 // Initialize max_mv_magnitude for use in the first INTER frame
3744 // after a key/intra-only frame.
3745 cpi->max_mv_magnitude = max_mv_def;
3746 } else {
3747 if (cm->show_frame) {
3748 // Allow mv_steps to correspond to twice the max mv magnitude found
3749 // in the previous frame, capped by the default max_mv_magnitude based
3750 // on resolution.
3751 cpi->mv_step_param = av1_init_search_range(
3752 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
3753 }
3754 cpi->max_mv_magnitude = 0;
3755 }
3756 }
3757 }
3758
3759 static void set_size_independent_vars(AV1_COMP *cpi) {
3760 int i;
3761 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3762 cpi->common.global_motion[i] = default_warp_params;
3763 }
3764 cpi->global_motion_search_done = 0;
3765 av1_set_speed_features_framesize_independent(cpi);
3766 av1_set_rd_speed_thresholds(cpi);
3767 av1_set_rd_speed_thresholds_sub8x8(cpi);
3768 cpi->common.interp_filter = SWITCHABLE;
3769 cpi->common.switchable_motion_mode = 1;
3770 }
3771
3772 static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
3773 int *top_index) {
3774 AV1_COMMON *const cm = &cpi->common;
3775 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
3776
3777 // Setup variables that depend on the dimensions of the frame.
3778 av1_set_speed_features_framesize_dependent(cpi);
3779
3780 // Decide q and q bounds.
3781 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3782 top_index);
3783
3784 if (!frame_is_intra_only(cm)) {
3785 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
3786 cpi->common.cur_frame_force_integer_mv);
3787 }
3788
3789 // Configure experimental use of segmentation for enhanced coding of
3790 // static regions if indicated.
3791 // Only allowed in the second pass of a two pass encode, as it requires
3792 // lagged coding, and if the relevant speed feature flag is set.
3793 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3794 configure_static_seg_features(cpi);
3795 }
3796
3797 static void init_motion_estimation(AV1_COMP *cpi) {
3798 int y_stride = cpi->scaled_source.y_stride;
3799
3800 if (cpi->sf.mv.search_method == NSTEP) {
3801 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
3802 } else if (cpi->sf.mv.search_method == DIAMOND) {
3803 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
3804 }
3805 }
3806
3807 #define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
3808 static void set_restoration_unit_size(int width, int height, int sx, int sy,
3809 RestorationInfo *rst) {
3810 (void)width;
3811 (void)height;
3812 (void)sx;
3813 (void)sy;
3814 #if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3815 int s = AOMMIN(sx, sy);
3816 #else
3817 int s = 0;
3818 #endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3819
3820 if (width * height > 352 * 288)
3821 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
3822 else
3823 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
3824 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3825 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
3826 }
3827
3828 static void init_ref_frame_bufs(AV1_COMP *cpi) {
3829 AV1_COMMON *const cm = &cpi->common;
3830 int i;
3831 BufferPool *const pool = cm->buffer_pool;
3832 cm->new_fb_idx = INVALID_IDX;
3833 for (i = 0; i < REF_FRAMES; ++i) {
3834 cm->ref_frame_map[i] = INVALID_IDX;
3835 pool->frame_bufs[i].ref_count = 0;
3836 }
3837 if (cm->seq_params.force_screen_content_tools) {
3838 for (i = 0; i < FRAME_BUFFERS; ++i) {
3839 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
3840 }
3841 }
3842 }
3843
3844 static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
3845 int subsampling_x, int subsampling_y) {
3846 AV1_COMMON *const cm = &cpi->common;
3847 SequenceHeader *const seq_params = &cm->seq_params;
3848
3849 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3850 seq_params->subsampling_x != subsampling_x ||
3851 seq_params->subsampling_y != subsampling_y) {
3852 seq_params->subsampling_x = subsampling_x;
3853 seq_params->subsampling_y = subsampling_y;
3854 seq_params->use_highbitdepth = use_highbitdepth;
3855
3856 alloc_raw_frame_buffers(cpi);
3857 init_ref_frame_bufs(cpi);
3858 alloc_util_frame_buffers(cpi);
3859
3860 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3861
3862 cpi->initial_width = cm->width;
3863 cpi->initial_height = cm->height;
3864 cpi->initial_mbs = cm->MBs;
3865 }
3866 }
3867
3868 // Returns 1 if the assigned width or height was <= 0.
3869 static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3870 AV1_COMMON *cm = &cpi->common;
3871 const int num_planes = av1_num_planes(cm);
3872 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3873 cm->seq_params.subsampling_x,
3874 cm->seq_params.subsampling_y);
3875
3876 if (width <= 0 || height <= 0) return 1;
3877
3878 cm->width = width;
3879 cm->height = height;
3880
3881 if (cpi->initial_width && cpi->initial_height &&
3882 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3883 av1_free_context_buffers(cm);
3884 av1_free_pc_tree(&cpi->td, num_planes);
3885 alloc_compressor_data(cpi);
3886 realloc_segmentation_maps(cpi);
3887 cpi->initial_width = cpi->initial_height = 0;
3888 }
3889 update_frame_size(cpi);
3890
3891 return 0;
3892 }
3893
3894 static void set_frame_size(AV1_COMP *cpi, int width, int height) {
3895 AV1_COMMON *const cm = &cpi->common;
3896 const SequenceHeader *const seq_params = &cm->seq_params;
3897 const int num_planes = av1_num_planes(cm);
3898 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
3899 int ref_frame;
3900
3901 if (width != cm->width || height != cm->height) {
3902 // There has been a change in the encoded frame size
3903 set_size_literal(cpi, width, height);
3904 set_mv_search_params(cpi);
3905 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
3906 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
3907 }
3908
3909 if (cpi->oxcf.pass == 2) {
3910 av1_set_target_rate(cpi, cm->width, cm->height);
3911 }
3912
3913 alloc_frame_mvs(cm, cm->new_fb_idx);
3914
3915 // Allocate above context buffers
3916 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3917 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
3918 cm->num_allocated_above_contexts < cm->tile_rows) {
3919 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3920 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3921 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3922 "Failed to allocate context buffers");
3923 }
3924
3925 // Reset the frame pointers to the current frame size.
3926 if (aom_realloc_frame_buffer(
3927 get_frame_new_buffer(cm), cm->width, cm->height,
3928 seq_params->subsampling_x, seq_params->subsampling_y,
3929 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3930 cm->byte_alignment, NULL, NULL, NULL))
3931 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3932 "Failed to allocate frame buffer");
3933
3934 const int frame_width = cm->superres_upscaled_width;
3935 const int frame_height = cm->superres_upscaled_height;
3936 set_restoration_unit_size(frame_width, frame_height,
3937 seq_params->subsampling_x,
3938 seq_params->subsampling_y, cm->rst_info);
3939 for (int i = 0; i < num_planes; ++i)
3940 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
3941
3942 av1_alloc_restoration_buffers(cm);
3943 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
3944 init_motion_estimation(cpi);
3945
3946 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3947 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3948 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3949
3950 ref_buf->idx = buf_idx;
3951
3952 if (buf_idx != INVALID_IDX) {
3953 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3954 ref_buf->buf = buf;
3955 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3956 buf->y_crop_height, cm->width,
3957 cm->height);
3958 if (av1_is_scaled(&ref_buf->sf))
3959 aom_extend_frame_borders(buf, num_planes);
3960 } else {
3961 ref_buf->buf = NULL;
3962 }
3963 }
3964
3965 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
3966 cm->width, cm->height);
3967
3968 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3969 }
3970
3971 static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3972 // Choose an arbitrary random number
3973 static unsigned int seed = 56789;
3974 const AV1EncoderConfig *oxcf = &cpi->oxcf;
3975 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3976 uint8_t new_denom = SCALE_NUMERATOR;
3977
3978 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
3979 switch (oxcf->resize_mode) {
3980 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
3981 case RESIZE_FIXED:
3982 if (cpi->common.frame_type == KEY_FRAME)
3983 new_denom = oxcf->resize_kf_scale_denominator;
3984 else
3985 new_denom = oxcf->resize_scale_denominator;
3986 break;
3987 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
3988 default: assert(0);
3989 }
3990 return new_denom;
3991 }
3992
3993 static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
3994 // Choose an arbitrary random number
3995 static unsigned int seed = 34567;
3996 const AV1EncoderConfig *oxcf = &cpi->oxcf;
3997 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3998 uint8_t new_denom = SCALE_NUMERATOR;
3999
4000 // Make sure that superres mode of the frame is consistent with the
4001 // sequence-level flag.
4002 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4003 cpi->common.seq_params.enable_superres));
4004 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4005 oxcf->superres_mode == SUPERRES_NONE));
4006
4007 switch (oxcf->superres_mode) {
4008 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
4009 case SUPERRES_FIXED:
4010 if (cpi->common.frame_type == KEY_FRAME)
4011 new_denom = oxcf->superres_kf_scale_denominator;
4012 else
4013 new_denom = oxcf->superres_scale_denominator;
4014 break;
4015 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
4016 case SUPERRES_QTHRESH: {
4017 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4018 const RATE_FACTOR_LEVEL rf_level = gf_group->rf_level[gf_group->index];
4019 const double rate_factor_delta = rate_factor_deltas[rf_level];
4020 const int qthresh = (rate_factor_delta <= 1.0)
4021 ? oxcf->superres_qthresh
4022 : oxcf->superres_kf_qthresh;
4023 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
4024 int bottom_index, top_index;
4025 const int q = av1_rc_pick_q_and_bounds(
4026 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
4027 if (q < qthresh) {
4028 new_denom = SCALE_NUMERATOR;
4029 } else {
4030 const uint8_t min_denom = SCALE_NUMERATOR + 1;
4031 const uint8_t denom_step = (MAXQ - qthresh + 1) >> 3;
4032
4033 if (q == qthresh) {
4034 new_denom = min_denom;
4035 } else if (denom_step == 0) {
4036 new_denom = SCALE_NUMERATOR << 1;
4037 } else {
4038 const uint8_t additional_denom = (q - qthresh) / denom_step;
4039 new_denom =
4040 AOMMIN(min_denom + additional_denom, SCALE_NUMERATOR << 1);
4041 }
4042 }
4043 break;
4044 }
4045 default: assert(0);
4046 }
4047 return new_denom;
4048 }
4049
4050 static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4051 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4052 }
4053
4054 static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
4055 // Only need to check the width, as scaling is horizontal only.
4056 (void)oheight;
4057 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
4058 }
4059
4060 static int validate_size_scales(RESIZE_MODE resize_mode,
4061 SUPERRES_MODE superres_mode, int owidth,
4062 int oheight, size_params_type *rsz) {
4063 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
4064 return 1;
4065 }
4066
4067 // Calculate current resize scale.
4068 int resize_denom =
4069 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4070 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4071
4072 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
4073 // Alter superres scale as needed to enforce conformity.
4074 rsz->superres_denom =
4075 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4076 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4077 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
4078 }
4079 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
4080 // Alter resize scale as needed to enforce conformity.
4081 resize_denom =
4082 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
4083 rsz->resize_width = owidth;
4084 rsz->resize_height = oheight;
4085 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4086 resize_denom);
4087 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4088 if (resize_denom > SCALE_NUMERATOR) {
4089 --resize_denom;
4090 rsz->resize_width = owidth;
4091 rsz->resize_height = oheight;
4092 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4093 resize_denom);
4094 }
4095 }
4096 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
4097 // Alter both resize and superres scales as needed to enforce conformity.
4098 do {
4099 if (resize_denom > rsz->superres_denom)
4100 --resize_denom;
4101 else
4102 --rsz->superres_denom;
4103 rsz->resize_width = owidth;
4104 rsz->resize_height = oheight;
4105 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4106 resize_denom);
4107 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4108 (resize_denom > SCALE_NUMERATOR ||
4109 rsz->superres_denom > SCALE_NUMERATOR));
4110 } else { // We are allowed to alter neither resize scale nor superres
4111 // scale.
4112 return 0;
4113 }
4114 return dimensions_are_ok(owidth, oheight, rsz);
4115 }
4116
4117 // Calculates resize and superres params for next frame
4118 size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
4119 const AV1EncoderConfig *oxcf = &cpi->oxcf;
4120 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
4121 int resize_denom;
4122 if (oxcf->pass == 1) return rsz;
4123 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4124 rsz.resize_width = cpi->resize_pending_width;
4125 rsz.resize_height = cpi->resize_pending_height;
4126 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4127 } else {
4128 resize_denom = calculate_next_resize_scale(cpi);
4129 rsz.resize_width = cpi->oxcf.width;
4130 rsz.resize_height = cpi->oxcf.height;
4131 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
4132 resize_denom);
4133 }
4134 rsz.superres_denom = calculate_next_superres_scale(cpi);
4135 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4136 oxcf->height, &rsz))
4137 assert(0 && "Invalid scale parameters");
4138 return rsz;
4139 }
4140
4141 static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
4142 int encode_width = rsz->resize_width;
4143 int encode_height = rsz->resize_height;
4144
4145 AV1_COMMON *cm = &cpi->common;
4146 cm->superres_upscaled_width = encode_width;
4147 cm->superres_upscaled_height = encode_height;
4148 cm->superres_scale_denominator = rsz->superres_denom;
4149 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4150 rsz->superres_denom);
4151 set_frame_size(cpi, encode_width, encode_height);
4152 }
4153
4154 static void setup_frame_size(AV1_COMP *cpi) {
4155 size_params_type rsz = av1_calculate_next_size_params(cpi);
4156 setup_frame_size_from_params(cpi, &rsz);
4157 }
4158
4159 static void superres_post_encode(AV1_COMP *cpi) {
4160 AV1_COMMON *cm = &cpi->common;
4161 const int num_planes = av1_num_planes(cm);
4162
4163 if (!av1_superres_scaled(cm)) return;
4164
4165 assert(cpi->oxcf.enable_superres);
4166 assert(!is_lossless_requested(&cpi->oxcf));
4167 assert(!cm->all_lossless);
4168
4169 av1_superres_upscale(cm, NULL);
4170
4171 // If regular resizing is occurring the source will need to be downscaled to
4172 // match the upscaled superres resolution. Otherwise the original source is
4173 // used.
4174 if (!av1_resize_scaled(cm)) {
4175 cpi->source = cpi->unscaled_source;
4176 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4177 } else {
4178 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4179 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
4180 // Do downscale. cm->(width|height) has been updated by
4181 // av1_superres_upscale
4182 if (aom_realloc_frame_buffer(
4183 &cpi->scaled_source, cm->superres_upscaled_width,
4184 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4185 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4186 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
4187 aom_internal_error(
4188 &cm->error, AOM_CODEC_MEM_ERROR,
4189 "Failed to reallocate scaled source buffer for superres");
4190 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4191 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
4192 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
4193 (int)cm->seq_params.bit_depth, num_planes);
4194 cpi->source = &cpi->scaled_source;
4195 }
4196 }
4197
4198 static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4199 const int num_planes = av1_num_planes(cm);
4200 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4201
4202 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4203 cm->coded_lossless && cm->all_lossless));
4204
4205 const int no_loopfilter = cm->coded_lossless || cm->large_scale_tile;
4206 const int no_cdef =
4207 !cm->seq_params.enable_cdef || cm->coded_lossless || cm->large_scale_tile;
4208 const int no_restoration = !cm->seq_params.enable_restoration ||
4209 cm->all_lossless || cm->large_scale_tile;
4210
4211 struct loopfilter *lf = &cm->lf;
4212
4213 if (no_loopfilter) {
4214 lf->filter_level[0] = 0;
4215 lf->filter_level[1] = 0;
4216 } else {
4217 struct aom_usec_timer timer;
4218
4219 aom_clear_system_state();
4220
4221 aom_usec_timer_start(&timer);
4222
4223 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4224
4225 aom_usec_timer_mark(&timer);
4226 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4227 }
4228
4229 if (lf->filter_level[0] || lf->filter_level[1]) {
4230 #if LOOP_FILTER_BITMASK
4231 av1_loop_filter_frame(cm->frame_to_show, cm, xd, 0, 0, num_planes, 0);
4232 #else
4233 if (cpi->num_workers > 1)
4234 av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd, 0, num_planes, 0,
4235 cpi->workers, cpi->num_workers,
4236 &cpi->lf_row_sync);
4237 else
4238 av1_loop_filter_frame(cm->frame_to_show, cm, xd, 0, num_planes, 0);
4239 #endif
4240 }
4241
4242 if (!no_restoration)
4243 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 0);
4244
4245 if (no_cdef) {
4246 cm->cdef_bits = 0;
4247 cm->cdef_strengths[0] = 0;
4248 cm->nb_cdef_strengths = 1;
4249 cm->cdef_uv_strengths[0] = 0;
4250 } else {
4251 // Find CDEF parameters
4252 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
4253 cpi->sf.fast_cdef_search);
4254
4255 // Apply the filter
4256 av1_cdef_frame(cm->frame_to_show, cm, xd);
4257 }
4258
4259 superres_post_encode(cpi);
4260
4261 if (no_restoration) {
4262 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4263 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4264 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4265 } else {
4266 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 1);
4267 av1_pick_filter_restoration(cpi->source, cpi);
4268 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4269 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4270 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4271 if (cpi->num_workers > 1)
4272 av1_loop_restoration_filter_frame_mt(cm->frame_to_show, cm, 0,
4273 cpi->workers, cpi->num_workers,
4274 &cpi->lr_row_sync, &cpi->lr_ctxt);
4275 else
4276 av1_loop_restoration_filter_frame(cm->frame_to_show, cm, 0,
4277 &cpi->lr_ctxt);
4278 }
4279 }
4280 }
4281
4282 static int encode_without_recode_loop(AV1_COMP *cpi) {
4283 AV1_COMMON *const cm = &cpi->common;
4284 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
4285
4286 aom_clear_system_state();
4287
4288 set_size_independent_vars(cpi);
4289
4290 setup_frame_size(cpi);
4291
4292 assert(cm->width == cpi->scaled_source.y_crop_width);
4293 assert(cm->height == cpi->scaled_source.y_crop_height);
4294
4295 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4296
4297 cpi->source =
4298 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4299 if (cpi->unscaled_last_source != NULL)
4300 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4301 &cpi->scaled_last_source);
4302 cpi->source->buf_8bit_valid = 0;
4303 if (frame_is_intra_only(cm) == 0) {
4304 scale_references(cpi);
4305 }
4306
4307 av1_set_quantizer(cm, q);
4308 setup_frame(cpi);
4309 suppress_active_map(cpi);
4310
4311 // Variance adaptive and in frame q adjustment experiments are mutually
4312 // exclusive.
4313 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
4314 av1_vaq_frame_setup(cpi);
4315 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
4316 av1_setup_in_frame_q_adj(cpi);
4317 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
4318 av1_cyclic_refresh_setup(cpi);
4319 }
4320 apply_active_map(cpi);
4321 if (cm->seg.enabled) {
4322 if (!cm->seg.update_data && cm->prev_frame) {
4323 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
4324 } else {
4325 calculate_segdata(&cm->seg);
4326 }
4327 } else {
4328 memset(&cm->seg, 0, sizeof(cm->seg));
4329 }
4330 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
4331
4332 // transform / motion compensation build reconstruction frame
4333 av1_encode_frame(cpi);
4334
4335 // Update some stats from cyclic refresh, and check if we should not update
4336 // golden reference, for 1 pass CBR.
4337 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
4338 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4339 av1_cyclic_refresh_check_golden_update(cpi);
4340
4341 // Update the skip mb flag probabilities based on the distribution
4342 // seen in the last encoder iteration.
4343 // update_base_skip_probs(cpi);
4344 aom_clear_system_state();
4345 return AOM_CODEC_OK;
4346 }
4347
4348 static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
4349 AV1_COMMON *const cm = &cpi->common;
4350 RATE_CONTROL *const rc = &cpi->rc;
4351 int bottom_index, top_index;
4352 int loop_count = 0;
4353 int loop_at_this_size = 0;
4354 int loop = 0;
4355 int overshoot_seen = 0;
4356 int undershoot_seen = 0;
4357 int frame_over_shoot_limit;
4358 int frame_under_shoot_limit;
4359 int q = 0, q_low = 0, q_high = 0;
4360
4361 set_size_independent_vars(cpi);
4362
4363 cpi->source->buf_8bit_valid = 0;
4364
4365 aom_clear_system_state();
4366 setup_frame_size(cpi);
4367 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4368
4369 do {
4370 aom_clear_system_state();
4371
4372 if (loop_count == 0) {
4373 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4374 set_mv_search_params(cpi);
4375
4376 // Reset the loop state for new frame size.
4377 overshoot_seen = 0;
4378 undershoot_seen = 0;
4379
4380 q_low = bottom_index;
4381 q_high = top_index;
4382
4383 loop_at_this_size = 0;
4384
4385 // Decide frame size bounds first time through.
4386 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4387 &frame_under_shoot_limit,
4388 &frame_over_shoot_limit);
4389 }
4390
4391 // if frame was scaled calculate global_motion_search again if already
4392 // done
4393 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4394 if (cpi->source->y_crop_width != cm->width ||
4395 cpi->source->y_crop_height != cm->height)
4396 cpi->global_motion_search_done = 0;
4397 cpi->source =
4398 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4399 if (cpi->unscaled_last_source != NULL)
4400 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4401 &cpi->scaled_last_source);
4402
4403 if (frame_is_intra_only(cm) == 0) {
4404 if (loop_count > 0) {
4405 release_scaled_references(cpi);
4406 }
4407 scale_references(cpi);
4408 }
4409 av1_set_quantizer(cm, q);
4410 // printf("Frame %d/%d: q = %d, frame_type = %d\n", cm->current_video_frame,
4411 // cm->show_frame, q, cm->frame_type);
4412
4413 if (loop_count == 0) setup_frame(cpi);
4414
4415 // Base q-index may have changed, so we need to assign proper default coef
4416 // probs before every iteration.
4417 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
4418 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
4419 av1_default_coef_probs(cm);
4420 av1_setup_frame_contexts(cm);
4421 }
4422
4423 // Variance adaptive and in frame q adjustment experiments are mutually
4424 // exclusive.
4425 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
4426 av1_vaq_frame_setup(cpi);
4427 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
4428 av1_setup_in_frame_q_adj(cpi);
4429 }
4430 if (cm->seg.enabled) {
4431 if (!cm->seg.update_data && cm->prev_frame) {
4432 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
4433 } else {
4434 calculate_segdata(&cm->seg);
4435 }
4436 } else {
4437 memset(&cm->seg, 0, sizeof(cm->seg));
4438 }
4439 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
4440
4441 // transform / motion compensation build reconstruction frame
4442 save_coding_context(cpi);
4443 av1_encode_frame(cpi);
4444
4445 // Update the skip mb flag probabilities based on the distribution
4446 // seen in the last encoder iteration.
4447 // update_base_skip_probs(cpi);
4448
4449 aom_clear_system_state();
4450
4451 // Dummy pack of the bitstream using up to date stats to get an
4452 // accurate estimate of output frame size to determine if we need
4453 // to recode.
4454 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
4455 restore_coding_context(cpi);
4456
4457 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4458 return AOM_CODEC_ERROR;
4459
4460 rc->projected_frame_size = (int)(*size) << 3;
4461 restore_coding_context(cpi);
4462
4463 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4464 }
4465
4466 if (cpi->oxcf.rc_mode == AOM_Q) {
4467 loop = 0;
4468 } else {
4469 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4470 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4471 int last_q = q;
4472 int64_t kf_err;
4473
4474 int64_t high_err_target = cpi->ambient_err;
4475 int64_t low_err_target = cpi->ambient_err >> 1;
4476
4477 if (cm->seq_params.use_highbitdepth) {
4478 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
4479 } else {
4480 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
4481 }
4482 // Prevent possible divide by zero error below for perfect KF
4483 kf_err += !kf_err;
4484
4485 // The key frame is not good enough or we can afford
4486 // to make it better without undue risk of popping.
4487 if ((kf_err > high_err_target &&
4488 rc->projected_frame_size <= frame_over_shoot_limit) ||
4489 (kf_err > low_err_target &&
4490 rc->projected_frame_size <= frame_under_shoot_limit)) {
4491 // Lower q_high
4492 q_high = q > q_low ? q - 1 : q_low;
4493
4494 // Adjust Q
4495 q = (int)((q * high_err_target) / kf_err);
4496 q = AOMMIN(q, (q_high + q_low) >> 1);
4497 } else if (kf_err < low_err_target &&
4498 rc->projected_frame_size >= frame_under_shoot_limit) {
4499 // The key frame is much better than the previous frame
4500 // Raise q_low
4501 q_low = q < q_high ? q + 1 : q_high;
4502
4503 // Adjust Q
4504 q = (int)((q * low_err_target) / kf_err);
4505 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
4506 }
4507
4508 // Clamp Q to upper and lower limits:
4509 q = clamp(q, q_low, q_high);
4510
4511 loop = q != last_q;
4512 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4513 frame_under_shoot_limit, q,
4514 AOMMAX(q_high, top_index), bottom_index)) {
4515 // Is the projected frame size out of range and are we allowed
4516 // to attempt to recode.
4517 int last_q = q;
4518 int retries = 0;
4519
4520 // Frame size out of permitted range:
4521 // Update correction factor & compute new Q to try...
4522 // Frame is too large
4523 if (rc->projected_frame_size > rc->this_frame_target) {
4524 // Special case if the projected size is > the max allowed.
4525 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4526 q_high = rc->worst_quality;
4527
4528 // Raise Qlow as to at least the current value
4529 q_low = q < q_high ? q + 1 : q_high;
4530
4531 if (undershoot_seen || loop_at_this_size > 1) {
4532 // Update rate_correction_factor unless
4533 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4534
4535 q = (q_high + q_low + 1) / 2;
4536 } else {
4537 // Update rate_correction_factor unless
4538 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4539
4540 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4541 AOMMAX(q_high, top_index), cm->width,
4542 cm->height);
4543
4544 while (q < q_low && retries < 10) {
4545 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4546 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4547 AOMMAX(q_high, top_index), cm->width,
4548 cm->height);
4549 retries++;
4550 }
4551 }
4552
4553 overshoot_seen = 1;
4554 } else {
4555 // Frame is too small
4556 q_high = q > q_low ? q - 1 : q_low;
4557
4558 if (overshoot_seen || loop_at_this_size > 1) {
4559 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4560 q = (q_high + q_low) / 2;
4561 } else {
4562 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4563 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4564 top_index, cm->width, cm->height);
4565 // Special case reset for qlow for constrained quality.
4566 // This should only trigger where there is very substantial
4567 // undershoot on a frame and the auto cq level is above
4568 // the user passsed in value.
4569 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
4570 q_low = q;
4571 }
4572
4573 while (q > q_high && retries < 10) {
4574 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4575 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4576 top_index, cm->width, cm->height);
4577 retries++;
4578 }
4579 }
4580
4581 undershoot_seen = 1;
4582 }
4583
4584 // Clamp Q to upper and lower limits:
4585 q = clamp(q, q_low, q_high);
4586
4587 loop = (q != last_q);
4588 } else {
4589 loop = 0;
4590 }
4591 }
4592
4593 // Special case for overlay frame.
4594 if (rc->is_src_frame_alt_ref &&
4595 rc->projected_frame_size < rc->max_frame_bandwidth)
4596 loop = 0;
4597
4598 if (!cpi->sf.gm_disable_recode) {
4599 if (recode_loop_test_global_motion(cpi)) loop = 1;
4600 }
4601
4602 if (loop) {
4603 ++loop_count;
4604 ++loop_at_this_size;
4605
4606 #if CONFIG_INTERNAL_STATS
4607 ++cpi->tot_recode_hits;
4608 #endif
4609 }
4610 } while (loop);
4611
4612 return AOM_CODEC_OK;
4613 }
4614
4615 static int get_ref_frame_flags(const AV1_COMP *cpi) {
4616 const int *const map = cpi->common.ref_frame_map;
4617
4618 // No.1 Priority: LAST_FRAME
4619 const int last2_is_last = map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[0]];
4620 const int last3_is_last = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[0]];
4621 const int gld_is_last =
4622 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4623 const int bwd_is_last =
4624 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4625 const int alt2_is_last =
4626 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4627 const int alt_is_last =
4628 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4629
4630 // No.2 Priority: ALTREF_FRAME
4631 const int last2_is_alt =
4632 map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4633 const int last3_is_alt =
4634 map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4635 const int gld_is_alt = map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] ==
4636 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4637 const int bwd_is_alt = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4638 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4639 const int alt2_is_alt = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4640 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4641
4642 // No.3 Priority: LAST2_FRAME
4643 const int last3_is_last2 = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[1]];
4644 const int gld_is_last2 =
4645 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4646 const int bwd_is_last2 =
4647 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4648 const int alt2_is_last2 =
4649 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4650
4651 // No.4 Priority: LAST3_FRAME
4652 const int gld_is_last3 =
4653 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4654 const int bwd_is_last3 =
4655 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4656 const int alt2_is_last3 =
4657 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4658
4659 // No.5 Priority: GOLDEN_FRAME
4660 const int bwd_is_gld = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4661 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
4662 const int alt2_is_gld = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4663 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
4664
4665 // No.6 Priority: BWDREF_FRAME
4666 const int alt2_is_bwd = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4667 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]];
4668
4669 // No.7 Priority: ALTREF2_FRAME
4670
4671 // After av1_apply_encoding_flags() is called, cpi->ref_frame_flags might be
4672 // adjusted according to external encoder flags.
4673 int flags = cpi->ext_ref_frame_flags;
4674
4675 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
4676
4677 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
4678
4679 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
4680
4681 if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
4682
4683 if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
4684 flags &= ~AOM_GOLD_FLAG;
4685
4686 if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 ||
4687 bwd_is_gld) &&
4688 (flags & AOM_BWD_FLAG))
4689 flags &= ~AOM_BWD_FLAG;
4690
4691 if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
4692 alt2_is_gld || alt2_is_bwd) &&
4693 (flags & AOM_ALT2_FLAG))
4694 flags &= ~AOM_ALT2_FLAG;
4695
4696 return flags;
4697 }
4698
4699 static void set_ext_overrides(AV1_COMP *cpi) {
4700 // Overrides the defaults with the externally supplied values with
4701 // av1_update_reference() and av1_update_entropy() calls
4702 // Note: The overrides are valid only for the next frame passed
4703 // to encode_frame_to_data_rate() function
4704 if (cpi->ext_use_s_frame) cpi->common.frame_type = S_FRAME;
4705 cpi->common.force_primary_ref_none = cpi->ext_use_primary_ref_none;
4706
4707 if (cpi->ext_refresh_frame_context_pending) {
4708 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4709 cpi->ext_refresh_frame_context_pending = 0;
4710 }
4711 if (cpi->ext_refresh_frame_flags_pending) {
4712 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4713 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4714 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
4715 cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
4716 cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
4717 cpi->ext_refresh_frame_flags_pending = 0;
4718 }
4719 cpi->common.allow_ref_frame_mvs = cpi->ext_use_ref_frame_mvs;
4720 // A keyframe is already error resilient and keyframes with
4721 // error_resilient_mode interferes with the use of show_existing_frame
4722 // when forward reference keyframes are enabled.
4723 cpi->common.error_resilient_mode =
4724 cpi->ext_use_error_resilient && cpi->common.frame_type != KEY_FRAME;
4725 }
4726
4727 #define DUMP_RECON_FRAMES 0
4728
4729 #if DUMP_RECON_FRAMES == 1
4730 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4731 static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4732 AV1_COMMON *const cm = &cpi->common;
4733 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
4734
4735 if (recon_buf == NULL) {
4736 printf("Frame %d is not ready.\n", cm->current_video_frame);
4737 return;
4738 }
4739
4740 static const int flag_list[REF_FRAMES] = { 0,
4741 AOM_LAST_FLAG,
4742 AOM_LAST2_FLAG,
4743 AOM_LAST3_FLAG,
4744 AOM_GOLD_FLAG,
4745 AOM_BWD_FLAG,
4746 AOM_ALT2_FLAG,
4747 AOM_ALT_FLAG };
4748 printf(
4749 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4750 "show_existing_frame=%d) "
4751 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
4752 cm->current_video_frame, cm->frame_offset, cm->show_frame,
4753 cm->show_existing_frame);
4754 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4755 const int buf_idx = cm->frame_refs[ref_frame - LAST_FRAME].idx;
4756 const int ref_offset =
4757 (buf_idx >= 0)
4758 ? (int)cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset
4759 : -1;
4760 printf(
4761 " %d(%c-%d-%4.2f)", ref_offset,
4762 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N',
4763 (buf_idx >= 0) ? (int)cpi->frame_rf_level[buf_idx] : -1,
4764 (buf_idx >= 0) ? rate_factor_deltas[cpi->frame_rf_level[buf_idx]] : -1);
4765 }
4766 printf(" ]\n");
4767
4768 if (!cm->show_frame) {
4769 printf("Frame %d is a no show frame, so no image dump.\n",
4770 cm->current_video_frame);
4771 return;
4772 }
4773
4774 int h;
4775 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4776 FILE *f_recon = NULL;
4777
4778 if (cm->current_video_frame == 0) {
4779 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4780 printf("Unable to open file %s to write.\n", file_name);
4781 return;
4782 }
4783 } else {
4784 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4785 printf("Unable to open file %s to append.\n", file_name);
4786 return;
4787 }
4788 }
4789 printf(
4790 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4791 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
4792 "refresh_alt_ref_frame=%d, rf_level=%d, "
4793 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
4794 cm->current_video_frame, cpi->twopass.gf_group.index,
4795 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
4796 cm->frame_offset, cm->show_frame, cm->show_existing_frame,
4797 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
4798 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4799 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
4800 #if 0
4801 int ref_frame;
4802 printf("get_ref_frame_map_idx: [");
4803 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
4804 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
4805 printf(" ]\n");
4806 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
4807 printf("cm->ref_frame_map = [");
4808 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4809 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
4810 }
4811 printf(" ]\n");
4812 #endif // 0
4813
4814 // --- Y ---
4815 for (h = 0; h < cm->height; ++h) {
4816 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4817 f_recon);
4818 }
4819 // --- U ---
4820 for (h = 0; h < (cm->height >> 1); ++h) {
4821 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4822 f_recon);
4823 }
4824 // --- V ---
4825 for (h = 0; h < (cm->height >> 1); ++h) {
4826 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4827 f_recon);
4828 }
4829
4830 fclose(f_recon);
4831 }
4832 #endif // DUMP_RECON_FRAMES
4833
4834 static INLINE int is_frame_droppable(AV1_COMP *cpi) {
4835 return !(cpi->refresh_alt_ref_frame || cpi->refresh_alt2_ref_frame ||
4836 cpi->refresh_bwd_ref_frame || cpi->refresh_golden_frame ||
4837 cpi->refresh_last_frame);
4838 }
4839
4840 static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size, uint8_t *dest,
4841 int skip_adapt,
4842 unsigned int *frame_flags) {
4843 AV1_COMMON *const cm = &cpi->common;
4844 SequenceHeader *const seq_params = &cm->seq_params;
4845 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4846 struct segmentation *const seg = &cm->seg;
4847
4848 set_ext_overrides(cpi);
4849 aom_clear_system_state();
4850
4851 // frame type has been decided outside of this function call
4852 cm->cur_frame->intra_only = frame_is_intra_only(cm);
4853 cm->cur_frame->frame_type = cm->frame_type;
4854
4855 // S_FRAMEs are always error resilient
4856 cm->error_resilient_mode |= frame_is_sframe(cm);
4857
4858 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
4859 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
4860 if (cm->large_scale_tile) seq_params->frame_id_numbers_present_flag = 0;
4861
4862 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
4863 // cm->allow_ref_frame_mvs needs to be written into the frame header while
4864 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
4865 // separated from frame_might_allow_ref_frame_mvs().
4866 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
4867
4868 cm->allow_warped_motion =
4869 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
4870
4871 // Reset the frame packet stamp index.
4872 if (cm->frame_type == KEY_FRAME && cm->show_frame)
4873 cm->current_video_frame = 0;
4874
4875 // NOTE:
4876 // (1) Move the setup of the ref_frame_flags upfront as it would be
4877 // determined by the current frame properties;
4878 // (2) The setup of the ref_frame_flags applies to both
4879 // show_existing_frame's
4880 // and the other cases.
4881 if (cm->current_video_frame > 0)
4882 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4883
4884 if (encode_show_existing_frame(cm)) {
4885 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
4886 // BWDREF_FRAME in the reference frame buffer.
4887 if (cm->frame_type == KEY_FRAME) {
4888 cm->reset_decoder_state = 1;
4889 } else {
4890 cm->frame_type = INTER_FRAME;
4891 }
4892 cm->show_frame = 1;
4893 cpi->frame_flags = *frame_flags;
4894
4895 restore_coding_context(cpi);
4896
4897 // Build the bitstream
4898 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4899 return AOM_CODEC_ERROR;
4900
4901 cpi->seq_params_locked = 1;
4902
4903 // Set up frame to show to get ready for stats collection.
4904 cm->frame_to_show = get_frame_new_buffer(cm);
4905
4906 // Update current frame offset.
4907 cm->frame_offset =
4908 cm->buffer_pool->frame_bufs[cm->new_fb_idx].cur_frame_offset;
4909
4910 #if DUMP_RECON_FRAMES == 1
4911 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4912 dump_filtered_recon_frames(cpi);
4913 #endif // DUMP_RECON_FRAMES
4914
4915 // Update the LAST_FRAME in the reference frame buffer.
4916 // NOTE:
4917 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
4918 // update has been done previously when handling the LAST_BIPRED_FRAME
4919 // right before BWDREF_FRAME (in the display order);
4920 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
4921 // update will be done when the following is called, which will
4922 // exchange
4923 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
4924 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2,
4925 // and
4926 // ALTREF2_FRAME will serve as the new LAST_FRAME.
4927 update_reference_frames(cpi);
4928
4929 // Update frame flags
4930 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4931 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4932 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4933
4934 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4935
4936 // Update the frame type
4937 cm->last_frame_type = cm->frame_type;
4938
4939 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4940 // to do post-encoding update accordingly.
4941 if (cpi->rc.is_src_frame_alt_ref) {
4942 av1_set_target_rate(cpi, cm->width, cm->height);
4943 av1_rc_postencode_update(cpi, *size);
4944 }
4945
4946 ++cm->current_video_frame;
4947
4948 return AOM_CODEC_OK;
4949 }
4950
4951 // Set default state for segment based loop filter update flags.
4952 cm->lf.mode_ref_delta_update = 0;
4953
4954 // Set various flags etc to special state if it is a key frame.
4955 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
4956 // Reset the loop filter deltas and segmentation map.
4957 av1_reset_segment_features(cm);
4958
4959 // If segmentation is enabled force a map update for key frames.
4960 if (seg->enabled) {
4961 seg->update_map = 1;
4962 seg->update_data = 1;
4963 }
4964
4965 // The alternate reference frame cannot be active for a key frame.
4966 cpi->rc.source_alt_ref_active = 0;
4967 }
4968 if (cpi->oxcf.mtu == 0) {
4969 cm->num_tg = cpi->oxcf.num_tile_groups;
4970 } else {
4971 // Use a default value for the purposes of weighting costs in probability
4972 // updates
4973 cm->num_tg = DEFAULT_MAX_NUM_TG;
4974 }
4975
4976 // For 1 pass CBR, check if we are dropping this frame.
4977 // Never drop on key frame.
4978 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
4979 cm->frame_type != KEY_FRAME) {
4980 if (av1_rc_drop_frame(cpi)) {
4981 av1_rc_postencode_update_drop_frame(cpi);
4982 return AOM_CODEC_OK;
4983 }
4984 }
4985
4986 aom_clear_system_state();
4987
4988 #if CONFIG_INTERNAL_STATS
4989 memset(cpi->mode_chosen_counts, 0,
4990 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4991 #endif
4992
4993 if (seq_params->frame_id_numbers_present_flag) {
4994 /* Non-normative definition of current_frame_id ("frame counter" with
4995 * wraparound) */
4996 const int frame_id_length = FRAME_ID_LENGTH;
4997 if (cm->current_frame_id == -1) {
4998 int lsb, msb;
4999 /* quasi-random initialization of current_frame_id for a key frame */
5000 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5001 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5002 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
5003 } else {
5004 lsb = cpi->source->y_buffer[0] & 0xff;
5005 msb = cpi->source->y_buffer[1] & 0xff;
5006 }
5007 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
5008
5009 // S_frame is meant for stitching different streams of different
5010 // resolutions together, so current_frame_id must be the
5011 // same across different streams of the same content current_frame_id
5012 // should be the same and not random. 0x37 is a chosen number as start
5013 // point
5014 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
5015 } else {
5016 cm->current_frame_id =
5017 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
5018 (1 << frame_id_length);
5019 }
5020 }
5021
5022 switch (cpi->oxcf.cdf_update_mode) {
5023 case 0: // No CDF update for any frames(4~6% compression loss).
5024 cm->disable_cdf_update = 1;
5025 break;
5026 case 1: // Enable CDF update for all frames.
5027 cm->disable_cdf_update = 0;
5028 break;
5029 case 2:
5030 // Strategically determine at which frames to do CDF update.
5031 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5032 // compression loss).
5033 // TODO(huisu@google.com): design schemes for various trade-offs between
5034 // compression quality and decoding speed.
5035 cm->disable_cdf_update =
5036 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
5037 break;
5038 }
5039 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
5040
5041 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
5042 if (encode_without_recode_loop(cpi) != AOM_CODEC_OK) return AOM_CODEC_ERROR;
5043 } else {
5044 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
5045 return AOM_CODEC_ERROR;
5046 }
5047
5048 cm->last_tile_cols = cm->tile_cols;
5049 cm->last_tile_rows = cm->tile_rows;
5050
5051 #ifdef OUTPUT_YUV_SKINMAP
5052 if (cpi->common.current_video_frame > 1) {
5053 av1_compute_skin_map(cpi, yuv_skinmap_file);
5054 }
5055 #endif // OUTPUT_YUV_SKINMAP
5056
5057 // Special case code to reduce pulsing when key frames are forced at a
5058 // fixed interval. Note the reconstruction error if it is the frame before
5059 // the force key frame
5060 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
5061 if (seq_params->use_highbitdepth) {
5062 cpi->ambient_err =
5063 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
5064 } else {
5065 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
5066 }
5067 }
5068
5069 // If the encoder forced a KEY_FRAME decision or if frame is an S_FRAME
5070 if ((cm->frame_type == KEY_FRAME && cm->show_frame) || frame_is_sframe(cm)) {
5071 cpi->refresh_last_frame = 1;
5072 }
5073
5074 cm->frame_to_show = get_frame_new_buffer(cm);
5075 cm->frame_to_show->color_primaries = seq_params->color_primaries;
5076 cm->frame_to_show->transfer_characteristics =
5077 seq_params->transfer_characteristics;
5078 cm->frame_to_show->matrix_coefficients = seq_params->matrix_coefficients;
5079 cm->frame_to_show->monochrome = seq_params->monochrome;
5080 cm->frame_to_show->chroma_sample_position =
5081 seq_params->chroma_sample_position;
5082 cm->frame_to_show->color_range = seq_params->color_range;
5083 cm->frame_to_show->render_width = cm->render_width;
5084 cm->frame_to_show->render_height = cm->render_height;
5085
5086 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5087 // off.
5088
5089 // Pick the loop filter level for the frame.
5090 if (!cm->allow_intrabc) {
5091 loopfilter_frame(cpi, cm);
5092 } else {
5093 cm->lf.filter_level[0] = 0;
5094 cm->lf.filter_level[1] = 0;
5095 cm->cdef_bits = 0;
5096 cm->cdef_strengths[0] = 0;
5097 cm->nb_cdef_strengths = 1;
5098 cm->cdef_uv_strengths[0] = 0;
5099 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5100 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5101 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5102 }
5103
5104 // TODO(debargha): Fix mv search range on encoder side
5105 // aom_extend_frame_inner_borders(cm->frame_to_show, av1_num_planes(cm));
5106 aom_extend_frame_borders(cm->frame_to_show, av1_num_planes(cm));
5107
5108 #ifdef OUTPUT_YUV_REC
5109 aom_write_one_yuv_frame(cm, cm->frame_to_show);
5110 #endif
5111
5112 // Build the bitstream
5113 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
5114 return AOM_CODEC_ERROR;
5115
5116 cpi->seq_params_locked = 1;
5117
5118 if (skip_adapt) return AOM_CODEC_OK;
5119
5120 if (seq_params->frame_id_numbers_present_flag) {
5121 int i;
5122 // Update reference frame id values based on the value of refresh_frame_mask
5123 for (i = 0; i < REF_FRAMES; i++) {
5124 if ((cpi->refresh_frame_mask >> i) & 1) {
5125 cm->ref_frame_id[i] = cm->current_frame_id;
5126 }
5127 }
5128 }
5129
5130 #if DUMP_RECON_FRAMES == 1
5131 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5132 dump_filtered_recon_frames(cpi);
5133 #endif // DUMP_RECON_FRAMES
5134
5135 if (cm->seg.enabled) {
5136 if (cm->seg.update_map) {
5137 update_reference_segmentation_map(cpi);
5138 } else if (cm->last_frame_seg_map) {
5139 memcpy(cm->current_frame_seg_map, cm->last_frame_seg_map,
5140 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5141 }
5142 }
5143
5144 if (frame_is_intra_only(cm) == 0) {
5145 release_scaled_references(cpi);
5146 }
5147
5148 update_reference_frames(cpi);
5149
5150 #if CONFIG_ENTROPY_STATS
5151 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
5152 #endif // CONFIG_ENTROPY_STATS
5153
5154 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
5155 *cm->fc = cpi->tile_data[cm->largest_tile_id].tctx;
5156 av1_reset_cdf_symbol_counters(cm->fc);
5157 }
5158
5159 if (cpi->refresh_golden_frame == 1)
5160 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5161 else
5162 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5163
5164 if (cpi->refresh_alt_ref_frame == 1)
5165 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5166 else
5167 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5168
5169 if (cpi->refresh_bwd_ref_frame == 1)
5170 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5171 else
5172 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
5173
5174 cm->last_frame_type = cm->frame_type;
5175
5176 av1_rc_postencode_update(cpi, *size);
5177
5178 if (cm->frame_type == KEY_FRAME) {
5179 // Tell the caller that the frame was coded as a key frame
5180 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5181 } else {
5182 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5183 }
5184
5185 // Clear the one shot update flags for segmentation map and mode/ref loop
5186 // filter deltas.
5187 cm->seg.update_map = 0;
5188 cm->seg.update_data = 0;
5189 cm->lf.mode_ref_delta_update = 0;
5190
5191 // A droppable frame might not be shown but it always
5192 // takes a space in the gf group. Therefore, even when
5193 // it is not shown, we still need update the count down.
5194
5195 if (cm->show_frame) {
5196 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5197 // are
5198 // being used as reference.
5199 swap_mi_and_prev_mi(cm);
5200 // Don't increment frame counters if this was an altref buffer
5201 // update not a real frame
5202
5203 ++cm->current_video_frame;
5204 }
5205
5206 // NOTE: Shall not refer to any frame not used as reference.
5207 if (cm->is_reference_frame) {
5208 // keep track of the last coded dimensions
5209 cm->last_width = cm->width;
5210 cm->last_height = cm->height;
5211
5212 // reset to normal state now that we are done.
5213 cm->last_show_frame = cm->show_frame;
5214 }
5215
5216 return AOM_CODEC_OK;
5217 }
5218
5219 static INLINE void update_keyframe_counters(AV1_COMP *cpi) {
5220 // TODO(zoeliu): To investigate whether we should treat BWDREF_FRAME
5221 // differently here for rc->avg_frame_bandwidth.
5222 if (cpi->common.show_frame || cpi->rc.is_bwd_ref_frame) {
5223 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref ||
5224 cpi->common.frame_type == KEY_FRAME) {
5225 // If this is a show_existing_frame with a source other than altref,
5226 // or if it is not a displayed forward keyframe, the keyframe update
5227 // counters were incremented when it was originally encoded.
5228 cpi->rc.frames_since_key++;
5229 cpi->rc.frames_to_key--;
5230 }
5231 }
5232 }
5233
5234 static INLINE void update_frames_till_gf_update(AV1_COMP *cpi) {
5235 // TODO(weitinglin): Updating this counter for is_frame_droppable
5236 // is a work-around to handle the condition when a frame is drop.
5237 // We should fix the cpi->common.show_frame flag
5238 // instead of checking the other condition to update the counter properly.
5239 if (cpi->common.show_frame || is_frame_droppable(cpi)) {
5240 // Decrement count down till next gf
5241 if (cpi->rc.frames_till_gf_update_due > 0)
5242 cpi->rc.frames_till_gf_update_due--;
5243 }
5244 }
5245
5246 static INLINE void update_twopass_gf_group_index(AV1_COMP *cpi) {
5247 // Increment the gf group index ready for the next frame. If this is
5248 // a show_existing_frame with a source other than altref, or if it is not
5249 // a displayed forward keyframe, the index was incremented when it was
5250 // originally encoded.
5251 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref ||
5252 cpi->common.frame_type == KEY_FRAME) {
5253 ++cpi->twopass.gf_group.index;
5254 }
5255 }
5256
5257 static void update_rc_counts(AV1_COMP *cpi) {
5258 update_keyframe_counters(cpi);
5259 update_frames_till_gf_update(cpi);
5260 if (cpi->oxcf.pass == 2) update_twopass_gf_group_index(cpi);
5261 }
5262
5263 static int Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5264 int skip_adapt, unsigned int *frame_flags) {
5265 if (cpi->oxcf.rc_mode == AOM_CBR) {
5266 av1_rc_get_one_pass_cbr_params(cpi);
5267 } else {
5268 av1_rc_get_one_pass_vbr_params(cpi);
5269 }
5270 if (encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags) !=
5271 AOM_CODEC_OK) {
5272 return AOM_CODEC_ERROR;
5273 }
5274 update_rc_counts(cpi);
5275 check_show_existing_frame(cpi);
5276 return AOM_CODEC_OK;
5277 }
5278
5279 static int Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5280 unsigned int *frame_flags) {
5281 #if CONFIG_MISMATCH_DEBUG
5282 mismatch_move_frame_idx_w();
5283 #endif
5284 #if TXCOEFF_COST_TIMER
5285 AV1_COMMON *cm = &cpi->common;
5286 cm->txcoeff_cost_timer = 0;
5287 cm->txcoeff_cost_count = 0;
5288 #endif
5289
5290 if (encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags) !=
5291 AOM_CODEC_OK) {
5292 return AOM_CODEC_ERROR;
5293 }
5294
5295 #if TXCOEFF_COST_TIMER
5296 cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer;
5297 fprintf(stderr,
5298 "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld "
5299 "in us\n",
5300 cm->txcoeff_cost_count, cm->txcoeff_cost_timer,
5301 cm->cum_txcoeff_cost_timer);
5302 #endif
5303
5304 av1_twopass_postencode_update(cpi);
5305 update_rc_counts(cpi);
5306 check_show_existing_frame(cpi);
5307 return AOM_CODEC_OK;
5308 }
5309
5310 #if CONFIG_DENOISE
5311 static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5312 int block_size, float noise_level,
5313 int64_t time_stamp, int64_t end_time) {
5314 AV1_COMMON *const cm = &cpi->common;
5315 if (!cpi->denoise_and_model) {
5316 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5317 cm->seq_params.bit_depth, block_size, noise_level);
5318 if (!cpi->denoise_and_model) {
5319 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5320 "Error allocating denoise and model");
5321 return -1;
5322 }
5323 }
5324 if (!cpi->film_grain_table) {
5325 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5326 if (!cpi->film_grain_table) {
5327 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5328 "Error allocating grain table");
5329 return -1;
5330 }
5331 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5332 }
5333 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5334 &cm->film_grain_params)) {
5335 if (cm->film_grain_params.apply_grain) {
5336 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5337 &cm->film_grain_params);
5338 }
5339 }
5340 return 0;
5341 }
5342 #endif
5343
5344 int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
5345 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5346 int64_t end_time) {
5347 AV1_COMMON *const cm = &cpi->common;
5348 const SequenceHeader *const seq_params = &cm->seq_params;
5349 struct aom_usec_timer timer;
5350 int res = 0;
5351 const int subsampling_x = sd->subsampling_x;
5352 const int subsampling_y = sd->subsampling_y;
5353 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5354
5355 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5356
5357 aom_usec_timer_start(&timer);
5358
5359 #if CONFIG_DENOISE
5360 if (cpi->oxcf.noise_level > 0)
5361 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5362 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5363 res = -1;
5364 #endif // CONFIG_DENOISE
5365
5366 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
5367 use_highbitdepth, frame_flags))
5368 res = -1;
5369 aom_usec_timer_mark(&timer);
5370 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
5371
5372 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
5373 (subsampling_x != 1 || subsampling_y != 1)) {
5374 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5375 "Non-4:2:0 color format requires profile 1 or 2");
5376 res = -1;
5377 }
5378 if ((seq_params->profile == PROFILE_1) &&
5379 !(subsampling_x == 0 && subsampling_y == 0)) {
5380 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5381 "Profile 1 requires 4:4:4 color format");
5382 res = -1;
5383 }
5384 if ((seq_params->profile == PROFILE_2) &&
5385 (seq_params->bit_depth <= AOM_BITS_10) &&
5386 !(subsampling_x == 1 && subsampling_y == 0)) {
5387 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5388 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
5389 res = -1;
5390 }
5391
5392 return res;
5393 }
5394
5395 static int frame_is_reference(const AV1_COMP *cpi) {
5396 const AV1_COMMON *cm = &cpi->common;
5397
5398 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
5399 cpi->refresh_golden_frame || cpi->refresh_bwd_ref_frame ||
5400 cpi->refresh_alt2_ref_frame || cpi->refresh_alt_ref_frame ||
5401 !cm->error_resilient_mode || cm->lf.mode_ref_delta_update ||
5402 cm->seg.update_map || cm->seg.update_data;
5403 }
5404
5405 static void adjust_frame_rate(AV1_COMP *cpi,
5406 const struct lookahead_entry *source) {
5407 int64_t this_duration;
5408 int step = 0;
5409
5410 if (source->ts_start == cpi->first_time_stamp_ever) {
5411 this_duration = source->ts_end - source->ts_start;
5412 step = 1;
5413 } else {
5414 int64_t last_duration =
5415 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5416
5417 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5418
5419 // do a step update if the duration changes by 10%
5420 if (last_duration)
5421 step = (int)((this_duration - last_duration) * 10 / last_duration);
5422 }
5423
5424 if (this_duration) {
5425 if (step) {
5426 av1_new_framerate(cpi, 10000000.0 / this_duration);
5427 } else {
5428 // Average this frame's rate into the last second's average
5429 // frame rate. If we haven't seen 1 second yet, then average
5430 // over the whole interval seen.
5431 const double interval = AOMMIN(
5432 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5433 double avg_duration = 10000000.0 / cpi->framerate;
5434 avg_duration *= (interval - avg_duration + this_duration);
5435 avg_duration /= interval;
5436
5437 av1_new_framerate(cpi, 10000000.0 / avg_duration);
5438 }
5439 }
5440 cpi->last_time_stamp_seen = source->ts_start;
5441 cpi->last_end_time_stamp_seen = source->ts_end;
5442 }
5443
5444 // Returns 0 if this is not an alt ref else the offset of the source frame
5445 // used as the arf midpoint.
5446 static int get_arf_src_index(AV1_COMP *cpi) {
5447 RATE_CONTROL *const rc = &cpi->rc;
5448 int arf_src_index = 0;
5449 if (is_altref_enabled(cpi)) {
5450 if (cpi->oxcf.pass == 2) {
5451 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5452 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5453 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5454 }
5455 } else if (rc->source_alt_ref_pending) {
5456 arf_src_index = rc->frames_till_gf_update_due;
5457 }
5458 }
5459 return arf_src_index;
5460 }
5461
5462 static int get_brf_src_index(AV1_COMP *cpi) {
5463 int brf_src_index = 0;
5464 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5465
5466 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5467 // flag.
5468 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5469 if (cpi->oxcf.pass == 2) {
5470 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5471 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5472 } else {
5473 // TODO(zoeliu): To re-visit the setup for this scenario
5474 brf_src_index = cpi->rc.bipred_group_interval - 1;
5475 }
5476 }
5477
5478 return brf_src_index;
5479 }
5480
5481 // Returns 0 if this is not an alt ref else the offset of the source frame
5482 // used as the arf midpoint.
5483 static int get_arf2_src_index(AV1_COMP *cpi) {
5484 int arf2_src_index = 0;
5485 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5486 if (cpi->oxcf.pass == 2) {
5487 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5488 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5489 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5490 }
5491 }
5492 }
5493 return arf2_src_index;
5494 }
5495
5496 static void check_src_altref(AV1_COMP *cpi,
5497 const struct lookahead_entry *source) {
5498 RATE_CONTROL *const rc = &cpi->rc;
5499
5500 // If pass == 2, the parameters set here will be reset in
5501 // av1_rc_get_second_pass_params()
5502
5503 if (cpi->oxcf.pass == 2) {
5504 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5505 rc->is_src_frame_alt_ref =
5506 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
5507 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
5508 rc->is_src_frame_ext_arf =
5509 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
5510 } else {
5511 rc->is_src_frame_alt_ref =
5512 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5513 }
5514
5515 if (rc->is_src_frame_alt_ref) {
5516 // Current frame is an ARF overlay frame.
5517 cpi->alt_ref_source = NULL;
5518
5519 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
5520 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
5521 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
5522 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
5523 cpi->refresh_last_frame = 1;
5524 } else {
5525 // Don't refresh the last buffer for an ARF overlay frame. It will
5526 // become the GF so preserve last as an alternative prediction option.
5527 cpi->refresh_last_frame = 0;
5528 }
5529 }
5530 }
5531
5532 #if CONFIG_INTERNAL_STATS
5533 extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5534 const unsigned char *img2, int img2_pitch,
5535 int width, int height);
5536
5537 static void adjust_image_stat(double y, double u, double v, double all,
5538 ImageStat *s) {
5539 s->stat[STAT_Y] += y;
5540 s->stat[STAT_U] += u;
5541 s->stat[STAT_V] += v;
5542 s->stat[STAT_ALL] += all;
5543 s->worst = AOMMIN(s->worst, all);
5544 }
5545
5546 static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
5547 AV1_COMMON *const cm = &cpi->common;
5548 double samples = 0.0;
5549 uint32_t in_bit_depth = 8;
5550 uint32_t bit_depth = 8;
5551
5552 #if CONFIG_INTER_STATS_ONLY
5553 if (cm->frame_type == KEY_FRAME) return; // skip key frame
5554 #endif
5555 cpi->bytes += frame_bytes;
5556
5557 if (cm->seq_params.use_highbitdepth) {
5558 in_bit_depth = cpi->oxcf.input_bit_depth;
5559 bit_depth = cm->seq_params.bit_depth;
5560 }
5561 if (cm->show_frame) {
5562 const YV12_BUFFER_CONFIG *orig = cpi->source;
5563 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5564 double y, u, v, frame_all;
5565
5566 cpi->count++;
5567 if (cpi->b_calculate_psnr) {
5568 PSNR_STATS psnr;
5569 double frame_ssim2 = 0.0, weight = 0.0;
5570 aom_clear_system_state();
5571 // TODO(yaowu): unify these two versions into one.
5572 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
5573
5574 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5575 &cpi->psnr);
5576 cpi->total_sq_error += psnr.sse[0];
5577 cpi->total_samples += psnr.samples[0];
5578 samples = psnr.samples[0];
5579 // TODO(yaowu): unify these two versions into one.
5580 if (cm->seq_params.use_highbitdepth)
5581 frame_ssim2 =
5582 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
5583 else
5584 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
5585
5586 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
5587 cpi->summed_quality += frame_ssim2 * weight;
5588 cpi->summed_weights += weight;
5589
5590 #if 0
5591 {
5592 FILE *f = fopen("q_used.stt", "a");
5593 double y2 = psnr.psnr[1];
5594 double u2 = psnr.psnr[2];
5595 double v2 = psnr.psnr[3];
5596 double frame_psnr2 = psnr.psnr[0];
5597 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5598 cm->current_video_frame, y2, u2, v2,
5599 frame_psnr2, frame_ssim2);
5600 fclose(f);
5601 }
5602 #endif
5603 }
5604 if (cpi->b_calculate_blockiness) {
5605 if (!cm->seq_params.use_highbitdepth) {
5606 const double frame_blockiness =
5607 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5608 recon->y_stride, orig->y_width, orig->y_height);
5609 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
5610 cpi->total_blockiness += frame_blockiness;
5611 }
5612
5613 if (cpi->b_calculate_consistency) {
5614 if (!cm->seq_params.use_highbitdepth) {
5615 const double this_inconsistency = aom_get_ssim_metrics(
5616 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5617 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5618
5619 const double peak = (double)((1 << in_bit_depth) - 1);
5620 const double consistency =
5621 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
5622 if (consistency > 0.0)
5623 cpi->worst_consistency =
5624 AOMMIN(cpi->worst_consistency, consistency);
5625 cpi->total_inconsistency += this_inconsistency;
5626 }
5627 }
5628 }
5629
5630 frame_all =
5631 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
5632 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
5633 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
5634 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5635 }
5636 }
5637 #endif // CONFIG_INTERNAL_STATS
5638
5639 static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5640 const YV12_BUFFER_CONFIG *last_picture,
5641 hash_table *last_hash_table) {
5642 aom_clear_system_state();
5643 // check use hash ME
5644 int k;
5645 uint32_t hash_value_1;
5646 uint32_t hash_value_2;
5647
5648 const int block_size = 8;
5649 const double threshold_current = 0.8;
5650 const double threshold_average = 0.95;
5651 const int max_history_size = 32;
5652 int T = 0; // total block
5653 int C = 0; // match with collocated block
5654 int S = 0; // smooth region but not match with collocated block
5655 int M = 0; // match with other block
5656
5657 const int pic_width = cur_picture->y_width;
5658 const int pic_height = cur_picture->y_height;
5659 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5660 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5661 const int x_pos = j;
5662 const int y_pos = i;
5663 int match = 1;
5664 T++;
5665
5666 // check whether collocated block match with current
5667 uint8_t *p_cur = cur_picture->y_buffer;
5668 uint8_t *p_ref = last_picture->y_buffer;
5669 int stride_cur = cur_picture->y_stride;
5670 int stride_ref = last_picture->y_stride;
5671 p_cur += (y_pos * stride_cur + x_pos);
5672 p_ref += (y_pos * stride_ref + x_pos);
5673
5674 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5675 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5676 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5677 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5678 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5679 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5680 match = 0;
5681 }
5682 }
5683 p16_cur += stride_cur;
5684 p16_ref += stride_ref;
5685 }
5686 } else {
5687 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5688 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5689 if (p_cur[tmpX] != p_ref[tmpX]) {
5690 match = 0;
5691 }
5692 }
5693 p_cur += stride_cur;
5694 p_ref += stride_ref;
5695 }
5696 }
5697
5698 if (match) {
5699 C++;
5700 continue;
5701 }
5702
5703 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5704 y_pos) ||
5705 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5706 S++;
5707 continue;
5708 }
5709
5710 av1_get_block_hash_value(
5711 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
5712 block_size, &hash_value_1, &hash_value_2,
5713 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
5714 // Hashing does not work for highbitdepth currently.
5715 // TODO(Roger): Make it work for highbitdepth.
5716 if (av1_use_hash_me(&cpi->common)) {
5717 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5718 M++;
5719 }
5720 }
5721 }
5722 }
5723
5724 assert(T > 0);
5725 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5726 double m_rate = ((double)(M)) / ((double)(T));
5727
5728 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5729 cpi->m_rate_array[cpi->rate_index] = m_rate;
5730
5731 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5732 cpi->rate_size++;
5733 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5734
5735 if (csm_rate < threshold_current) {
5736 return 0;
5737 }
5738
5739 if (C == T) {
5740 return 1;
5741 }
5742
5743 double csm_average = 0.0;
5744 double m_average = 0.0;
5745
5746 for (k = 0; k < cpi->rate_size; k++) {
5747 csm_average += cpi->csm_rate_array[k];
5748 m_average += cpi->m_rate_array[k];
5749 }
5750 csm_average /= cpi->rate_size;
5751 m_average /= cpi->rate_size;
5752
5753 if (csm_average < threshold_average) {
5754 return 0;
5755 }
5756
5757 if (M > (T - C - S) / 3) {
5758 return 1;
5759 }
5760
5761 if (csm_rate > 0.99 && m_rate > 0.01) {
5762 return 1;
5763 }
5764
5765 if (csm_average + m_average > 1.01) {
5766 return 1;
5767 }
5768
5769 return 0;
5770 }
5771
5772 int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5773 size_t *size, uint8_t *dest, int64_t *time_stamp,
5774 int64_t *time_end, int flush,
5775 const aom_rational_t *timebase) {
5776 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5777 AV1_COMMON *const cm = &cpi->common;
5778 const int num_planes = av1_num_planes(cm);
5779 BufferPool *const pool = cm->buffer_pool;
5780 RATE_CONTROL *const rc = &cpi->rc;
5781 struct aom_usec_timer cmptimer;
5782 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5783 struct lookahead_entry *last_source = NULL;
5784 struct lookahead_entry *source = NULL;
5785 int arf_src_index;
5786 int brf_src_index;
5787 int i;
5788
5789 #if CONFIG_BITSTREAM_DEBUG
5790 assert(cpi->oxcf.max_threads == 0 &&
5791 "bitstream debug tool does not support multithreading");
5792 bitstream_queue_record_write();
5793 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
5794 #endif
5795
5796 cm->showable_frame = 0;
5797 aom_usec_timer_start(&cmptimer);
5798
5799 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
5800
5801 // Normal defaults
5802 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
5803 ? REFRESH_FRAME_CONTEXT_DISABLED
5804 : REFRESH_FRAME_CONTEXT_BACKWARD;
5805 if (oxcf->large_scale_tile)
5806 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
5807
5808 // default reference buffers update config
5809 av1_configure_buffer_updates_firstpass(cpi, LF_UPDATE);
5810
5811 // Initialize fields related to forward keyframes
5812 cpi->no_show_kf = 0;
5813 cm->reset_decoder_state = 0;
5814
5815 // Don't allow a show_existing_frame to coincide with an error resilient or
5816 // S-Frame. An exception can be made in the case of a keyframe, since it
5817 // does not depend on any previous frames. We must make this exception here
5818 // because of the use of show_existing_frame with forward coded keyframes.
5819 struct lookahead_entry *lookahead_src = NULL;
5820 if (cm->current_video_frame > 0)
5821 lookahead_src = av1_lookahead_peek(cpi->lookahead, 0);
5822
5823 int use_show_existing = 1;
5824 if (lookahead_src != NULL) {
5825 const int is_error_resilient =
5826 cpi->oxcf.error_resilient_mode ||
5827 (lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT);
5828 const int is_s_frame = cpi->oxcf.s_frame_mode ||
5829 (lookahead_src->flags & AOM_EFLAG_SET_S_FRAME);
5830 const int is_key_frame =
5831 (rc->frames_to_key == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY);
5832 use_show_existing = !(is_error_resilient || is_s_frame) || is_key_frame;
5833 }
5834
5835 if (oxcf->pass == 2 && cm->show_existing_frame && use_show_existing) {
5836 // Manage the source buffer and flush out the source frame that has been
5837 // coded already; Also get prepared for PSNR calculation if needed.
5838 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
5839 *size = 0;
5840 return -1;
5841 }
5842 av1_apply_encoding_flags(cpi, source->flags);
5843 cpi->source = &source->img;
5844 // TODO(zoeliu): To track down to determine whether it's needed to adjust
5845 // the frame rate.
5846 *time_stamp = source->ts_start;
5847 *time_end = source->ts_end;
5848
5849 // We need to adjust frame rate for an overlay frame
5850 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
5851
5852 // Find a free buffer for the new frame, releasing the reference
5853 // previously
5854 // held.
5855 if (cm->new_fb_idx != INVALID_IDX) {
5856 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5857 }
5858 cm->new_fb_idx = get_free_fb(cm);
5859
5860 if (cm->new_fb_idx == INVALID_IDX) return -1;
5861
5862 // Clear down mmx registers
5863 aom_clear_system_state();
5864
5865 // Start with a 0 size frame.
5866 *size = 0;
5867
5868 // We need to update the gf_group for show_existing overlay frame
5869 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
5870
5871 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
5872 return AOM_CODEC_ERROR;
5873
5874 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
5875
5876 #if CONFIG_INTERNAL_STATS
5877 compute_internal_stats(cpi, (int)(*size));
5878 #endif // CONFIG_INTERNAL_STATS
5879
5880 // Clear down mmx registers
5881 aom_clear_system_state();
5882
5883 cm->show_existing_frame = 0;
5884 return 0;
5885 }
5886
5887 // Should we encode an arf frame.
5888 arf_src_index = get_arf_src_index(cpi);
5889 if (arf_src_index) {
5890 for (i = 0; i <= arf_src_index; ++i) {
5891 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
5892 // Avoid creating an alt-ref if there's a forced keyframe pending.
5893 if (e == NULL) {
5894 break;
5895 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
5896 arf_src_index = 0;
5897 flush = 1;
5898 break;
5899 }
5900 }
5901 }
5902
5903 if (arf_src_index) {
5904 assert(arf_src_index <= rc->frames_to_key);
5905
5906 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
5907 cm->showable_frame = 1;
5908 cpi->alt_ref_source = source;
5909 // When arf_src_index == rc->frames_to_key, it indicates a fwd_kf
5910 if (arf_src_index == rc->frames_to_key) {
5911 // Skip temporal filtering and mark as intra_only if we have a fwd_kf
5912 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5913 int which_arf = gf_group->arf_update_idx[gf_group->index];
5914 cpi->is_arf_filter_off[which_arf] = 1;
5915 cpi->no_show_kf = 1;
5916 } else {
5917 if (oxcf->arnr_max_frames > 0) {
5918 // Produce the filtered ARF frame.
5919 av1_temporal_filter(cpi, arf_src_index);
5920 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
5921 force_src_buffer = &cpi->alt_ref_buffer;
5922 }
5923 }
5924 cm->show_frame = 0;
5925 cm->intra_only = 0;
5926
5927 if (oxcf->pass < 2) {
5928 // In second pass, the buffer updates configure will be set
5929 // in the function av1_rc_get_second_pass_params
5930 av1_configure_buffer_updates_firstpass(cpi, ARF_UPDATE);
5931 }
5932 }
5933 rc->source_alt_ref_pending = 0;
5934 }
5935
5936 // Should we encode an arf2 frame.
5937 arf_src_index = get_arf2_src_index(cpi);
5938 if (arf_src_index) {
5939 for (i = 0; i <= arf_src_index; ++i) {
5940 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
5941 // Avoid creating an alt-ref if there's a forced keyframe pending.
5942 if (e == NULL) {
5943 break;
5944 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
5945 arf_src_index = 0;
5946 flush = 1;
5947 break;
5948 }
5949 }
5950 }
5951
5952 if (arf_src_index) {
5953 assert(arf_src_index <= rc->frames_to_key);
5954
5955 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
5956 cm->showable_frame = 1;
5957 cpi->alt_ref_source = source;
5958
5959 if (oxcf->arnr_max_frames > 0) {
5960 // Produce the filtered ARF frame.
5961 av1_temporal_filter(cpi, arf_src_index);
5962 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
5963 force_src_buffer = &cpi->alt_ref_buffer;
5964 }
5965
5966 cm->show_frame = 0;
5967 cm->intra_only = 0;
5968
5969 if (oxcf->pass < 2) {
5970 // In second pass, the buffer updates configure will be set
5971 // in the function av1_rc_get_second_pass_params
5972 av1_configure_buffer_updates_firstpass(cpi, INTNL_ARF_UPDATE);
5973 }
5974 }
5975 rc->source_alt_ref_pending = 0;
5976 }
5977
5978 rc->is_bwd_ref_frame = 0;
5979 brf_src_index = get_brf_src_index(cpi);
5980 if (brf_src_index) {
5981 assert(brf_src_index <= rc->frames_to_key);
5982 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
5983 cm->showable_frame = 1;
5984 cm->show_frame = 0;
5985 cm->intra_only = 0;
5986
5987 if (oxcf->pass < 2) {
5988 // In second pass, the buffer updates configure will be set
5989 // in the function av1_rc_get_second_pass_params
5990 av1_configure_buffer_updates_firstpass(cpi, BIPRED_UPDATE);
5991 }
5992 }
5993 }
5994
5995 if (!source) {
5996 // Get last frame source.
5997 if (cm->current_video_frame > 0) {
5998 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
5999 return -1;
6000 }
6001 if (cm->current_video_frame > 0) assert(last_source != NULL);
6002 // Read in the source frame.
6003 source = av1_lookahead_pop(cpi->lookahead, flush);
6004
6005 if (source != NULL) {
6006 cm->show_frame = 1;
6007 cm->intra_only = 0;
6008
6009 // Check to see if the frame should be encoded as an arf overlay.
6010 check_src_altref(cpi, source);
6011 }
6012 }
6013 if (source) {
6014 cpi->unscaled_source = cpi->source =
6015 force_src_buffer ? force_src_buffer : &source->img;
6016 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
6017
6018 *time_stamp = source->ts_start;
6019 *time_end = source->ts_end;
6020 av1_apply_encoding_flags(cpi, source->flags);
6021 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
6022
6023 } else {
6024 *size = 0;
6025 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
6026 av1_end_first_pass(cpi); /* get last stats packet */
6027 cpi->twopass.first_pass_done = 1;
6028 }
6029 return -1;
6030 }
6031
6032 if (source->ts_start < cpi->first_time_stamp_ever) {
6033 cpi->first_time_stamp_ever = source->ts_start;
6034 cpi->last_end_time_stamp_seen = source->ts_start;
6035 }
6036
6037 // Clear down mmx registers
6038 aom_clear_system_state();
6039
6040 // adjust frame rates based on timestamps given
6041 if (cm->show_frame) adjust_frame_rate(cpi, source);
6042
6043 // Find a free buffer for the new frame, releasing the reference previously
6044 // held.
6045 if (cm->new_fb_idx != INVALID_IDX) {
6046 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6047 }
6048 cm->new_fb_idx = get_free_fb(cm);
6049
6050 if (cm->new_fb_idx == INVALID_IDX) return -1;
6051
6052 // Retain the RF_LEVEL for the current newly coded frame.
6053 cpi->frame_rf_level[cm->new_fb_idx] =
6054 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
6055
6056 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
6057 cm->cur_frame->buf.buf_8bit_valid = 0;
6058
6059 if (cpi->film_grain_table) {
6060 cm->seq_params.film_grain_params_present = aom_film_grain_table_lookup(
6061 cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */,
6062 &cm->film_grain_params);
6063 }
6064 cm->cur_frame->film_grain_params_present =
6065 cm->seq_params.film_grain_params_present;
6066
6067 // only one operating point supported now
6068 const int64_t pts64 = ticks_to_timebase_units(timebase, *time_stamp);
6069 if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR;
6070 cpi->common.frame_presentation_time = (uint32_t)pts64;
6071
6072 // Start with a 0 size frame.
6073 *size = 0;
6074
6075 cpi->frame_flags = *frame_flags;
6076
6077 if (oxcf->pass == 2) {
6078 av1_rc_get_second_pass_params(cpi);
6079 } else if (oxcf->pass == 1) {
6080 setup_frame_size(cpi);
6081 }
6082
6083 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
6084 for (i = 0; i < REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
6085 }
6086
6087 cm->using_qmatrix = cpi->oxcf.using_qm;
6088 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
6089 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
6090
6091 if (cm->seq_params.frame_id_numbers_present_flag) {
6092 if (*time_stamp == 0) {
6093 cpi->common.current_frame_id = -1;
6094 }
6095 }
6096
6097 cpi->cur_poc++;
6098 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
6099 !frame_is_intra_only(cm)) {
6100 if (cpi->common.seq_params.force_integer_mv == 2) {
6101 struct lookahead_entry *previous_entry =
6102 av1_lookahead_peek(cpi->lookahead, cpi->previous_index);
6103 if (!previous_entry)
6104 cpi->common.cur_frame_force_integer_mv = 0;
6105 else
6106 cpi->common.cur_frame_force_integer_mv = is_integer_mv(
6107 cpi, cpi->source, &previous_entry->img, cpi->previous_hash_table);
6108 } else {
6109 cpi->common.cur_frame_force_integer_mv =
6110 cpi->common.seq_params.force_integer_mv;
6111 }
6112 } else {
6113 cpi->common.cur_frame_force_integer_mv = 0;
6114 }
6115
6116 if (oxcf->pass == 1) {
6117 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
6118 av1_first_pass(cpi, source);
6119 } else if (oxcf->pass == 2) {
6120 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
6121 return AOM_CODEC_ERROR;
6122 } else {
6123 // One pass encode
6124 if (Pass0Encode(cpi, size, dest, 0, frame_flags) != AOM_CODEC_OK)
6125 return AOM_CODEC_ERROR;
6126 }
6127 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
6128 cpi->previous_hash_table = &cm->cur_frame->hash_table;
6129 {
6130 int l;
6131 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
6132 if ((cpi->lookahead->buf + l) == source) {
6133 cpi->previous_index = l;
6134 break;
6135 }
6136 }
6137
6138 if (l == cpi->lookahead->max_sz) {
6139 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6140 "Failed to find last frame original buffer");
6141 }
6142 }
6143 }
6144
6145 if (!cm->large_scale_tile) {
6146 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
6147 }
6148
6149 #define EXT_TILE_DEBUG 0
6150 #if EXT_TILE_DEBUG
6151 if (cm->large_scale_tile && oxcf->pass == 2) {
6152 char fn[20] = "./fc";
6153 fn[4] = cm->current_video_frame / 100 + '0';
6154 fn[5] = (cm->current_video_frame % 100) / 10 + '0';
6155 fn[6] = (cm->current_video_frame % 10) + '0';
6156 fn[7] = '\0';
6157 av1_print_frame_contexts(cm->fc, fn);
6158 }
6159 #endif // EXT_TILE_DEBUG
6160 #undef EXT_TILE_DEBUG
6161
6162 cm->showable_frame = !cm->show_frame && cm->showable_frame;
6163
6164 // No frame encoded, or frame was dropped, release scaled references.
6165 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6166 release_scaled_references(cpi);
6167 }
6168
6169 if (*size > 0) {
6170 cpi->droppable = !frame_is_reference(cpi);
6171 }
6172
6173 aom_usec_timer_mark(&cmptimer);
6174 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
6175
6176 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6177 generate_psnr_packet(cpi);
6178
6179 #if CONFIG_INTERNAL_STATS
6180 if (oxcf->pass != 1) {
6181 compute_internal_stats(cpi, (int)(*size));
6182 }
6183 #endif // CONFIG_INTERNAL_STATS
6184
6185 aom_clear_system_state();
6186
6187 return 0;
6188 }
6189
6190 int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6191 AV1_COMMON *cm = &cpi->common;
6192 if (!cm->show_frame) {
6193 return -1;
6194 } else {
6195 int ret;
6196 if (cm->frame_to_show) {
6197 *dest = *cm->frame_to_show;
6198 dest->y_width = cm->width;
6199 dest->y_height = cm->height;
6200 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6201 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
6202 ret = 0;
6203 } else {
6204 ret = -1;
6205 }
6206 aom_clear_system_state();
6207 return ret;
6208 }
6209 }
6210
6211 int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
6212 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6213
6214 *frame =
6215 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6216 return 0;
6217 }
6218
6219 static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6220 const YV12_BUFFER_CONFIG *b) {
6221 return a->y_height == b->y_height && a->y_width == b->y_width &&
6222 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6223 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6224 a->border == b->border &&
6225 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6226 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6227 }
6228
6229 aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6230 YV12_BUFFER_CONFIG *new_frame,
6231 YV12_BUFFER_CONFIG *sd) {
6232 const int num_planes = av1_num_planes(cm);
6233 if (!equal_dimensions_and_border(new_frame, sd))
6234 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6235 "Incorrect buffer dimensions");
6236 else
6237 aom_yv12_copy_frame(new_frame, sd, num_planes);
6238
6239 return cm->error.error_code;
6240 }
6241
6242 int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6243 AOM_SCALING vert_mode) {
6244 int hr = 0, hs = 0, vr = 0, vs = 0;
6245
6246 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6247
6248 Scale2Ratio(horiz_mode, &hr, &hs);
6249 Scale2Ratio(vert_mode, &vr, &vs);
6250
6251 // always go to the next whole number
6252 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6253 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
6254
6255 return 0;
6256 }
6257
6258 int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
6259
6260 int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6261 size_t output_size = 0;
6262 size_t total_bytes_read = 0;
6263 size_t remaining_size = *frame_size;
6264 uint8_t *buff_ptr = buffer;
6265
6266 // go through each OBUs
6267 while (total_bytes_read < *frame_size) {
6268 uint8_t saved_obu_header[2];
6269 uint64_t obu_payload_size;
6270 size_t length_of_payload_size;
6271 size_t length_of_obu_size;
6272 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6273 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6274
6275 // save the obu header (1 or 2 bytes)
6276 memmove(saved_obu_header, buff_ptr, obu_header_size);
6277 // clear the obu_has_size_field
6278 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6279
6280 // get the payload_size and length of payload_size
6281 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6282 &obu_payload_size, &length_of_payload_size) != 0) {
6283 return AOM_CODEC_ERROR;
6284 }
6285 obu_bytes_read += length_of_payload_size;
6286
6287 // calculate the length of size of the obu header plus payload
6288 length_of_obu_size =
6289 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
6290
6291 // move the rest of data to new location
6292 memmove(buff_ptr + length_of_obu_size + obu_header_size,
6293 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
6294 obu_bytes_read += (size_t)obu_payload_size;
6295
6296 // write the new obu size
6297 const uint64_t obu_size = obu_header_size + obu_payload_size;
6298 size_t coded_obu_size;
6299 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
6300 &coded_obu_size) != 0) {
6301 return AOM_CODEC_ERROR;
6302 }
6303
6304 // write the saved (modified) obu_header following obu size
6305 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
6306
6307 total_bytes_read += obu_bytes_read;
6308 remaining_size -= obu_bytes_read;
6309 buff_ptr += length_of_obu_size + obu_size;
6310 output_size += length_of_obu_size + (size_t)obu_size;
6311 }
6312
6313 *frame_size = output_size;
6314 return AOM_CODEC_OK;
6315 }
6316
6317 void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
6318 // TODO(yunqingwang): For what references to use, external encoding flags
6319 // should be consistent with internal reference frame selection. Need to
6320 // ensure that there is not conflict between the two. In AV1 encoder, the
6321 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
6322 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
6323 // LAST.
6324 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
6325 if (flags &
6326 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6327 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6328 AOM_EFLAG_NO_REF_ARF2)) {
6329 if (flags & AOM_EFLAG_NO_REF_LAST) {
6330 cpi->ext_ref_frame_flags = 0;
6331 } else {
6332 int ref = AOM_REFFRAME_ALL;
6333
6334 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6335 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6336
6337 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6338
6339 if (flags & AOM_EFLAG_NO_REF_ARF) {
6340 ref ^= AOM_ALT_FLAG;
6341 ref ^= AOM_BWD_FLAG;
6342 ref ^= AOM_ALT2_FLAG;
6343 } else {
6344 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6345 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
6346 }
6347
6348 av1_use_as_reference(cpi, ref);
6349 }
6350 }
6351
6352 if (flags &
6353 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
6354 int upd = AOM_REFFRAME_ALL;
6355
6356 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6357 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
6358
6359 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
6360
6361 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6362 upd ^= AOM_ALT_FLAG;
6363 upd ^= AOM_BWD_FLAG;
6364 upd ^= AOM_ALT2_FLAG;
6365 }
6366
6367 av1_update_reference(cpi, upd);
6368 }
6369
6370 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
6371 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
6372 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
6373 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
6374 cpi->ext_use_s_frame =
6375 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
6376 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
6377
6378 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6379 av1_update_entropy(cpi, 0);
6380 }
6381 }
6382
6383 int64_t timebase_units_to_ticks(const aom_rational_t *timebase, int64_t n) {
6384 return n * TICKS_PER_SEC * timebase->num / timebase->den;
6385 }
6386
6387 int64_t ticks_to_timebase_units(const aom_rational_t *timebase, int64_t n) {
6388 const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
6389 return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
6390 }
6391
6392 aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
6393 if (!cpi) return NULL;
6394
6395 uint8_t header_buf[512] = { 0 };
6396 const uint32_t sequence_header_size =
6397 write_sequence_header_obu(cpi, &header_buf[0]);
6398 assert(sequence_header_size <= sizeof(header_buf));
6399 if (sequence_header_size == 0) return NULL;
6400
6401 const size_t obu_header_size = 1;
6402 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
6403 const size_t payload_offset = obu_header_size + size_field_size;
6404
6405 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
6406 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
6407
6408 if (write_obu_header(OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
6409 obu_header_size) {
6410 return NULL;
6411 }
6412
6413 size_t coded_size_field_size = 0;
6414 if (aom_uleb_encode(sequence_header_size, size_field_size,
6415 &header_buf[obu_header_size],
6416 &coded_size_field_size) != 0) {
6417 return NULL;
6418 }
6419 assert(coded_size_field_size == size_field_size);
6420
6421 aom_fixed_buf_t *global_headers =
6422 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
6423 if (!global_headers) return NULL;
6424
6425 const size_t global_header_buf_size =
6426 obu_header_size + size_field_size + sequence_header_size;
6427
6428 global_headers->buf = malloc(global_header_buf_size);
6429 if (!global_headers->buf) {
6430 free(global_headers);
6431 return NULL;
6432 }
6433
6434 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
6435 global_headers->sz = global_header_buf_size;
6436 return global_headers;
6437 }
6438