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