blob: f15ad195ca6e88693b22bb61fca6df6024f1a680 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * 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.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
13#include <math.h>
14#include <stdio.h>
15
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "./aom_config.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
18#include "av1/common/alloccommon.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010019#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020#include "av1/common/filter.h"
21#include "av1/common/idct.h"
22#include "av1/common/reconinter.h"
23#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070024#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "av1/common/tile_common.h"
26
27#include "av1/encoder/aq_complexity.h"
28#include "av1/encoder/aq_cyclicrefresh.h"
29#include "av1/encoder/aq_variance.h"
30#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070031#include "av1/encoder/context_tree.h"
32#include "av1/encoder/encodeframe.h"
33#include "av1/encoder/encodemv.h"
34#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070035#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070036#include "av1/encoder/ethread.h"
37#include "av1/encoder/firstpass.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070038#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070039#include "av1/encoder/mbgraph.h"
40#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070041#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070042#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/encoder/ratectrl.h"
44#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045#include "av1/encoder/segmentation.h"
46#include "av1/encoder/speed_features.h"
47#include "av1/encoder/temporal_filter.h"
48
Yaowu Xuf883b422016-08-30 14:01:10 -070049#include "./av1_rtcd.h"
50#include "./aom_dsp_rtcd.h"
51#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070052#include "aom_dsp/psnr.h"
53#if CONFIG_INTERNAL_STATS
54#include "aom_dsp/ssim.h"
55#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -080056#include "av1/encoder/grain_test_vectors.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070057#include "aom_dsp/aom_dsp_common.h"
58#include "aom_dsp/aom_filter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070059#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070060#include "aom_ports/mem.h"
61#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070062#include "aom_scale/aom_scale.h"
Angie Chiang5b5f4df2017-12-06 10:41:12 -080063#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070064#include "aom_util/debug_util.h"
Angie Chiang5b5f4df2017-12-06 10:41:12 -080065#endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070066
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000067#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000068
Andrey Norkin795ba872018-03-06 13:24:14 -080069#if CONFIG_BUFFER_MODEL
70// av1 uses 10,000,000 ticks/second as time stamp
71#define TICKS_PER_SEC 10000000LL
72#endif
73
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080074#if CONFIG_ENTROPY_STATS
75FRAME_COUNTS aggregate_fc;
76#endif // CONFIG_ENTROPY_STATS
77
Yaowu Xuc27fc142016-08-22 16:08:15 -070078#define AM_SEGMENT_ID_INACTIVE 7
79#define AM_SEGMENT_ID_ACTIVE 0
80
Johannb0ef6ff2018-02-08 14:32:21 -080081// Whether to use high precision mv for altref computation.
82#define ALTREF_HIGH_PRECISION_MV 1
83
84// Q threshold for high precision mv. Choose a very high value for now so that
85// HIGH_PRECISION is always chosen.
86#define HIGH_PRECISION_MV_QTHRESH 200
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070087
Yaowu Xuc27fc142016-08-22 16:08:15 -070088// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070089#ifdef OUTPUT_YUV_SKINMAP
90FILE *yuv_skinmap_file = NULL;
91#endif
92#ifdef OUTPUT_YUV_REC
93FILE *yuv_rec_file;
94#define FILE_NAME_LEN 100
95#endif
96
Urvang Joshib5ed3502016-10-17 16:38:05 -070097#if CONFIG_INTERNAL_STATS
98typedef enum { Y, U, V, ALL } STAT_TYPE;
99#endif // CONFIG_INTERNAL_STATS
100
Yaowu Xuf883b422016-08-30 14:01:10 -0700101static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700102 switch (mode) {
103 case NORMAL:
104 *hr = 1;
105 *hs = 1;
106 break;
107 case FOURFIVE:
108 *hr = 4;
109 *hs = 5;
110 break;
111 case THREEFIVE:
112 *hr = 3;
113 *hs = 5;
114 break;
115 case ONETWO:
116 *hr = 1;
117 *hs = 2;
118 break;
119 default:
120 *hr = 1;
121 *hs = 1;
122 assert(0);
123 break;
124 }
125}
126
127// Mark all inactive blocks as active. Other segmentation features may be set
128// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700129static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700130 unsigned char *const seg_map = cpi->segmentation_map;
131 int i;
132 if (cpi->active_map.enabled || cpi->active_map.update)
133 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
134 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
135 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
136}
137
Yaowu Xuf883b422016-08-30 14:01:10 -0700138static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700139 struct segmentation *const seg = &cpi->common.seg;
140 unsigned char *const seg_map = cpi->segmentation_map;
141 const unsigned char *const active_map = cpi->active_map.map;
142 int i;
143
144 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
145
146 if (frame_is_intra_only(&cpi->common)) {
147 cpi->active_map.enabled = 0;
148 cpi->active_map.update = 1;
149 }
150
151 if (cpi->active_map.update) {
152 if (cpi->active_map.enabled) {
153 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
154 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700155 av1_enable_segmentation(seg);
156 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700157 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
158 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
159 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
160 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
161
162 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
163 -MAX_LOOP_FILTER);
164 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
165 -MAX_LOOP_FILTER);
166 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
167 -MAX_LOOP_FILTER);
168 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
169 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700170 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700171 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700172 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
173 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
174 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
175 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176 if (seg->enabled) {
177 seg->update_data = 1;
178 seg->update_map = 1;
179 }
180 }
181 cpi->active_map.update = 0;
182 }
183}
184
Yaowu Xuf883b422016-08-30 14:01:10 -0700185int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
186 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700187 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
188 unsigned char *const active_map_8x8 = cpi->active_map.map;
189 const int mi_rows = cpi->common.mi_rows;
190 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700191 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
192 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700193 cpi->active_map.update = 1;
194 if (new_map_16x16) {
195 int r, c;
196 for (r = 0; r < mi_rows; ++r) {
197 for (c = 0; c < mi_cols; ++c) {
198 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700199 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700200 ? AM_SEGMENT_ID_ACTIVE
201 : AM_SEGMENT_ID_INACTIVE;
202 }
203 }
204 cpi->active_map.enabled = 1;
205 } else {
206 cpi->active_map.enabled = 0;
207 }
208 return 0;
209 } else {
210 return -1;
211 }
212}
213
Yaowu Xuf883b422016-08-30 14:01:10 -0700214int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
215 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700216 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
217 new_map_16x16) {
218 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
219 const int mi_rows = cpi->common.mi_rows;
220 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700221 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
222 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
223
Yaowu Xuc27fc142016-08-22 16:08:15 -0700224 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
225 if (cpi->active_map.enabled) {
226 int r, c;
227 for (r = 0; r < mi_rows; ++r) {
228 for (c = 0; c < mi_cols; ++c) {
229 // Cyclic refresh segments are considered active despite not having
230 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700231 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700232 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
233 }
234 }
235 }
236 return 0;
237 } else {
238 return -1;
239 }
240}
241
Yaowu Xu45295c32018-03-29 12:06:10 -0700242static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
243 int cur_frame_force_integer_mv) {
James Zern01a9d702017-08-25 19:09:33 +0000244 MACROBLOCK *const mb = &cpi->td.mb;
Hui Su50361152018-03-02 11:01:42 -0800245 cpi->common.allow_high_precision_mv =
246 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Rupert Swarbricka84faf22017-12-11 13:56:40 +0000247 const int copy_hp =
248 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Jingning Hanf050fc12018-03-09 14:53:33 -0800249 int *(*src)[2] = copy_hp ? &mb->nmvcost_hp : &mb->nmvcost;
250 mb->mv_cost_stack = *src;
James Zern01a9d702017-08-25 19:09:33 +0000251}
252
Yaowu Xuf883b422016-08-30 14:01:10 -0700253static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700254 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700255 return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100256#if CONFIG_FILEOPTIONS
257 if (cpi->common.options && cpi->common.options->ext_partition)
258#endif
259 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
260 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100264// TODO(any): Possibly could improve this with a heuristic.
265#if CONFIG_FILEOPTIONS
266 if (cpi->common.options && !cpi->common.options->ext_partition)
267 return BLOCK_64X64;
268#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700269 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270}
271
Yaowu Xuf883b422016-08-30 14:01:10 -0700272static void setup_frame(AV1_COMP *cpi) {
273 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800274 // Set up entropy context depending on frame type. The decoder mandates
275 // the use of the default context, index 0, for keyframes and inter
276 // frames where the error_resilient_mode or intra_only flag is set. For
277 // other inter-frames the encoder currently uses only two contexts;
278 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500279
Thomas Daede51020e12017-12-14 20:12:44 -0800280 cm->primary_ref_frame = PRIMARY_REF_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700281 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700282 av1_setup_past_independence(cm);
Thomas Daede51020e12017-12-14 20:12:44 -0800283 for (int i = 0; i < REF_FRAMES; i++) {
284 cm->fb_of_context_type[i] = -1;
285 }
286 cm->fb_of_context_type[REGULAR_FRAME] =
287 get_ref_frame_map_idx(cpi, GOLDEN_FRAME);
Thomas Daede3f2853a2018-03-12 17:40:32 -0700288 } else if (!cm->large_scale_tile) {
289 // for large scale tile we leave the default PRIMARY_REF_NONE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -0700291 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700292 cm->frame_context_idx = EXT_ARF_FRAME;
293 else if (cpi->refresh_alt_ref_frame)
294 cm->frame_context_idx = ARF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700295 else if (cpi->rc.is_src_frame_alt_ref)
296 cm->frame_context_idx = OVERLAY_FRAME;
297 else if (cpi->refresh_golden_frame)
298 cm->frame_context_idx = GLD_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700299 else if (cpi->refresh_bwd_ref_frame)
300 cm->frame_context_idx = BRF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700301 else
302 cm->frame_context_idx = REGULAR_FRAME;
Thomas Daede51020e12017-12-14 20:12:44 -0800303 int wanted_fb = cm->fb_of_context_type[cm->frame_context_idx];
304 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
305 int fb = get_ref_frame_map_idx(cpi, ref_frame);
306 if (fb == wanted_fb) {
307 cm->primary_ref_frame = ref_frame - LAST_FRAME;
308 }
309 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310 }
311
312 if (cm->frame_type == KEY_FRAME) {
313 cpi->refresh_golden_frame = 1;
314 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700315 av1_zero(cpi->interp_filter_selected);
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000316 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700317 set_use_reference_buffer(cm, 0);
David Barker11eac7b2017-12-07 12:22:36 +0000318 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
Tarek AMARAc9813852018-03-05 18:40:18 -0500319 } else if (frame_is_sframe(cm)) {
320 cpi->refresh_golden_frame = 1;
321 cpi->refresh_alt_ref_frame = 1;
322 av1_zero(cpi->interp_filter_selected);
323 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700324 } else {
Thomas Daede51020e12017-12-14 20:12:44 -0800325 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
326 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
David Barkercc615a82018-03-19 14:38:51 +0000327 av1_setup_past_independence(cm);
328 cm->seg.update_map = 1;
329 cm->seg.update_data = 1;
David Barker11eac7b2017-12-07 12:22:36 +0000330 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700331 } else {
Thomas Daede51020e12017-12-14 20:12:44 -0800332 *cm->fc = cm->frame_contexts[cm->frame_refs[cm->primary_ref_frame].idx];
333 cm->pre_fc =
334 &cm->frame_contexts[cm->frame_refs[cm->primary_ref_frame].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700335 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700336 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700337 }
338
Yue Chend90d3432018-03-16 11:28:42 -0700339 cm->prev_frame = get_prev_frame(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700340 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341}
342
Cheng Chen46f30c72017-09-07 11:13:33 -0700343static void enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344 int i;
Yunqing Wang19b9f722018-02-20 16:22:01 -0800345 cm->mi = cm->mip;
346 memset(cm->mip, 0, cm->mi_stride * cm->mi_rows * sizeof(*cm->mip));
347 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700348 // Clear top border row
349 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
350 // Clear left border column
Yunqing Wang19b9f722018-02-20 16:22:01 -0800351 for (i = 0; i < cm->mi_rows; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700352 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yunqing Wang19b9f722018-02-20 16:22:01 -0800353 cm->mi_grid_visible = cm->mi_grid_base;
354 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700355
356 memset(cm->mi_grid_base, 0,
Yunqing Wang19b9f722018-02-20 16:22:01 -0800357 cm->mi_stride * cm->mi_rows * sizeof(*cm->mi_grid_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700358}
359
Cheng Chen46f30c72017-09-07 11:13:33 -0700360static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700361 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700363 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364 if (!cm->prev_mip) return 1;
365 cm->mi_alloc_size = mi_size;
366
Yue Chen53b53f02018-03-29 14:31:23 -0700367 cm->mi_grid_base =
368 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700369 if (!cm->mi_grid_base) return 1;
370 cm->prev_mi_grid_base =
Yue Chen53b53f02018-03-29 14:31:23 -0700371 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700372 if (!cm->prev_mi_grid_base) return 1;
373
374 return 0;
375}
376
Cheng Chen46f30c72017-09-07 11:13:33 -0700377static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700378 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700379 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700380 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700382 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700384 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700385 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700386 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700387}
388
Cheng Chen46f30c72017-09-07 11:13:33 -0700389static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700390 // Current mip will be the prev_mip for the next frame.
Yue Chen53b53f02018-03-29 14:31:23 -0700391 MB_MODE_INFO **temp_base = cm->prev_mi_grid_base;
392 MB_MODE_INFO *temp = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700393 cm->prev_mip = cm->mip;
394 cm->mip = temp;
395
396 // Update the upper left visible macroblock ptrs.
Yunqing Wang19b9f722018-02-20 16:22:01 -0800397 cm->mi = cm->mip;
398 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700399
400 cm->prev_mi_grid_base = cm->mi_grid_base;
401 cm->mi_grid_base = temp_base;
Yunqing Wang19b9f722018-02-20 16:22:01 -0800402 cm->mi_grid_visible = cm->mi_grid_base;
403 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404}
405
Yaowu Xuf883b422016-08-30 14:01:10 -0700406void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700407 static volatile int init_done = 0;
408
409 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700410 av1_rtcd();
411 aom_dsp_rtcd();
412 aom_scale_rtcd();
413 av1_init_intra_predictors();
414 av1_init_me_luts();
415 av1_rc_init_minq_luts();
Yaowu Xuf883b422016-08-30 14:01:10 -0700416 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417 init_done = 1;
418 }
419}
420
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700421static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
422 if (cpi->mbmi_ext_base) {
423 aom_free(cpi->mbmi_ext_base);
424 cpi->mbmi_ext_base = NULL;
425 }
426}
427
428static void alloc_context_buffers_ext(AV1_COMP *cpi) {
429 AV1_COMMON *cm = &cpi->common;
430 int mi_size = cm->mi_cols * cm->mi_rows;
431
432 dealloc_context_buffers_ext(cpi);
433 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
434 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
435}
436
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800437static void update_film_grain_parameters(struct AV1_COMP *cpi,
438 const AV1EncoderConfig *oxcf) {
439 AV1_COMMON *const cm = &cpi->common;
440 cpi->oxcf = *oxcf;
441
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700442 if (cm->film_grain_table) {
443 aom_film_grain_table_free(cm->film_grain_table);
444 aom_free(cm->film_grain_table);
445 }
446 cm->film_grain_table = 0;
447
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800448 if (oxcf->film_grain_test_vector) {
449 cm->film_grain_params_present = 1;
450 if (cm->frame_type == KEY_FRAME) {
451 memcpy(&cm->film_grain_params,
452 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
453 sizeof(cm->film_grain_params));
454
455 cm->film_grain_params.bit_depth = cm->bit_depth;
456 if (cm->color_range == AOM_CR_FULL_RANGE) {
457 cm->film_grain_params.clip_to_restricted_range = 0;
458 }
459 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700460 } else if (oxcf->film_grain_table_filename) {
461 cm->film_grain_table = aom_malloc(sizeof(*cm->film_grain_table));
462 memset(cm->film_grain_table, 0, sizeof(aom_film_grain_table_t));
463
464 aom_film_grain_table_read(cm->film_grain_table,
465 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800466 } else {
467 cm->film_grain_params_present = 0;
468 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
469 }
470}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800471
Yaowu Xuf883b422016-08-30 14:01:10 -0700472static void dealloc_compressor_data(AV1_COMP *cpi) {
473 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000474 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700476 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477
Yaowu Xuf883b422016-08-30 14:01:10 -0700478 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700479 cpi->tile_data = NULL;
480
481 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700482 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484
Yaowu Xuf883b422016-08-30 14:01:10 -0700485 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700486 cpi->cyclic_refresh = NULL;
487
Yaowu Xuf883b422016-08-30 14:01:10 -0700488 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700489 cpi->active_map.map = NULL;
490
Jingning Hand064cf02017-06-01 10:00:39 -0700491 aom_free(cpi->td.mb.above_pred_buf);
492 cpi->td.mb.above_pred_buf = NULL;
493
494 aom_free(cpi->td.mb.left_pred_buf);
495 cpi->td.mb.left_pred_buf = NULL;
496
497 aom_free(cpi->td.mb.wsrc_buf);
498 cpi->td.mb.wsrc_buf = NULL;
499
500 aom_free(cpi->td.mb.mask_buf);
501 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700502
Jingning Han6cc1fd32017-10-13 09:05:36 -0700503 aom_free(cm->tpl_mvs);
504 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700505
Yaowu Xuf883b422016-08-30 14:01:10 -0700506 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700507 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700508 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700509
Yaowu Xuf883b422016-08-30 14:01:10 -0700510 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700511 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800512 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700513 aom_free_frame_buffer(&cpi->scaled_source);
514 aom_free_frame_buffer(&cpi->scaled_last_source);
515 aom_free_frame_buffer(&cpi->alt_ref_buffer);
516 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700517
Yaowu Xuf883b422016-08-30 14:01:10 -0700518 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700519 cpi->tile_tok[0][0] = 0;
520
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000521 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522
hui sud9a812b2017-07-06 14:34:37 -0700523 aom_free(cpi->td.mb.palette_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700524}
525
Yaowu Xuf883b422016-08-30 14:01:10 -0700526static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700527 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700528 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200530 // Stores a snapshot of key state variables which can subsequently be
531 // restored with a call to av1_restore_coding_context. These functions are
532 // intended for use in a re-code loop in av1_compress_frame where the
533 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800534 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
535 av1_copy(cc->nmv_costs, cpi->nmv_costs);
536 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000537
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538 cc->fc = *cm->fc;
539}
540
Yaowu Xuf883b422016-08-30 14:01:10 -0700541static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700543 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200545 // Restore key state variables to the snapshot state stored in the
546 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800547 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
548 av1_copy(cpi->nmv_costs, cc->nmv_costs);
549 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000550
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551 *cm->fc = cc->fc;
552}
553
Yaowu Xuf883b422016-08-30 14:01:10 -0700554static void configure_static_seg_features(AV1_COMP *cpi) {
555 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700556 const RATE_CONTROL *const rc = &cpi->rc;
557 struct segmentation *const seg = &cm->seg;
558
559 int high_q = (int)(rc->avg_q > 48.0);
560 int qi_delta;
561
562 // Disable and clear down for KF
563 if (cm->frame_type == KEY_FRAME) {
564 // Clear down the global segmentation map
565 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
566 seg->update_map = 0;
567 seg->update_data = 0;
568 cpi->static_mb_pct = 0;
569
570 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700571 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700572
573 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700574 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700575 } else if (cpi->refresh_alt_ref_frame) {
576 // If this is an alt ref frame
577 // Clear down the global segmentation map
578 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
579 seg->update_map = 0;
580 seg->update_data = 0;
581 cpi->static_mb_pct = 0;
582
583 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700584 av1_disable_segmentation(seg);
585 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700586
587 // Scan frames from current to arf frame.
588 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700589 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700590
591 // If segmentation was enabled set those features needed for the
592 // arf itself.
593 if (seg->enabled) {
594 seg->update_map = 1;
595 seg->update_data = 1;
596
597 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700598 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
599 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700600 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
601 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
602 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
603 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
604
605 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
606 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
607 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
608 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609
Yaowu Xuf883b422016-08-30 14:01:10 -0700610 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700611 }
612 } else if (seg->enabled) {
613 // All other frames if segmentation has been enabled
614
615 // First normal frame in a valid gf or alt ref group
616 if (rc->frames_since_golden == 0) {
617 // Set up segment features for normal frames in an arf group
618 if (rc->source_alt_ref_active) {
619 seg->update_map = 0;
620 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621
Yaowu Xuf883b422016-08-30 14:01:10 -0700622 qi_delta =
623 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
624 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
625 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626
Cheng Chend8184da2017-09-26 18:15:22 -0700627 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
628 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
629 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
630 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
631
632 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
633 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
634 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
635 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636
637 // Segment coding disabled for compred testing
638 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700639 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
640 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
641 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700642 }
643 } else {
644 // Disable segmentation and clear down features if alt ref
645 // is not active for this group
646
Yaowu Xuf883b422016-08-30 14:01:10 -0700647 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700648
649 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
650
651 seg->update_map = 0;
652 seg->update_data = 0;
653
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655 }
656 } else if (rc->is_src_frame_alt_ref) {
657 // Special case where we are coding over the top of a previous
658 // alt ref frame.
659 // Segment coding disabled for compred testing
660
661 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700662 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
663 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664
665 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700666 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
667 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
668 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
669 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700670
671 // Skip all MBs if high Q (0,0 mv and skip coeffs)
672 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700673 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
674 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700675 }
676 // Enable data update
677 seg->update_data = 1;
678 } else {
679 // All other frames.
680
681 // No updates.. leave things as they are.
682 seg->update_map = 0;
683 seg->update_data = 0;
684 }
685 }
686}
687
Yaowu Xuf883b422016-08-30 14:01:10 -0700688static void update_reference_segmentation_map(AV1_COMP *cpi) {
689 AV1_COMMON *const cm = &cpi->common;
Yushin Choa7f65922018-04-04 16:06:11 -0700690 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
Soo-Chul Han934af352017-10-15 15:21:51 -0400691 uint8_t *cache_ptr = cm->current_frame_seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700692 int row, col;
693
694 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700695 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700697 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
698 cache[0] = mi_4x4[0]->segment_id;
699 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 cache_ptr += cm->mi_cols;
701 }
702}
703
Yaowu Xuf883b422016-08-30 14:01:10 -0700704static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
705 AV1_COMMON *cm = &cpi->common;
706 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700707
708 if (!cpi->lookahead)
Yaowu Xud3e7c682017-12-21 14:08:25 -0800709 cpi->lookahead = av1_lookahead_init(
710 oxcf->width, oxcf->height, cm->subsampling_x, cm->subsampling_y,
711 cm->use_highbitdepth, oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700712 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700713 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700714 "Failed to allocate lag buffers");
715
716 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700717 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -0800719 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
720 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700721 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722 "Failed to allocate altref buffer");
723}
724
Yaowu Xuf883b422016-08-30 14:01:10 -0700725static void alloc_util_frame_buffers(AV1_COMP *cpi) {
726 AV1_COMMON *const cm = &cpi->common;
727 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -0800729 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
730 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700731 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 "Failed to allocate last frame buffer");
733
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700734 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800735 &cpi->trial_frame_rst, cm->superres_upscaled_width,
736 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
737 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
738 NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800739 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800740 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741
Yaowu Xuf883b422016-08-30 14:01:10 -0700742 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -0800744 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
745 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700746 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700747 "Failed to allocate scaled source buffer");
748
Yaowu Xuf883b422016-08-30 14:01:10 -0700749 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -0800751 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
752 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700753 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754 "Failed to allocate scaled last source buffer");
755}
756
Cheng Chen46f30c72017-09-07 11:13:33 -0700757static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700758 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000759 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760
Yaowu Xuf883b422016-08-30 14:01:10 -0700761 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700762
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700763 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700764
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 alloc_context_buffers_ext(cpi);
766
Yaowu Xuf883b422016-08-30 14:01:10 -0700767 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768
769 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000770 unsigned int tokens =
771 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700772 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700773 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774 }
775
Yaowu Xuf883b422016-08-30 14:01:10 -0700776 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700777}
778
Yaowu Xuf883b422016-08-30 14:01:10 -0700779void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700781 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700782}
783
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200784static void set_tile_info_max_tile(AV1_COMP *cpi) {
785 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200786 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200787
788 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200789
790 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200791 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200792 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200793 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
794 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200795 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000796 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
797 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200798 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200799 cm->uniform_tile_spacing_flag = 0;
800 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
801 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200802 size_sb = cpi->oxcf.tile_widths[j++];
803 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000804 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200805 }
806 cm->tile_cols = i;
807 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200808 }
809 av1_calculate_tile_cols(cm);
810
811 // configure tile rows
812 if (cm->uniform_tile_spacing_flag) {
813 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
814 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200815 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000816 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
817 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200818 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200819 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
820 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200821 size_sb = cpi->oxcf.tile_heights[j++];
822 if (j >= cpi->oxcf.tile_height_count) j = 0;
823 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200824 }
825 cm->tile_rows = i;
826 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200827 }
828 av1_calculate_tile_rows(cm);
829}
830
Yaowu Xuf883b422016-08-30 14:01:10 -0700831static void set_tile_info(AV1_COMP *cpi) {
832 AV1_COMMON *const cm = &cpi->common;
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700833 if (cpi->oxcf.large_scale_tile) {
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700834 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
835 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
836 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
837 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
838 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
839 } else {
840 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
841 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
842 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
843 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
844 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700846 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
847 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700848
Yaowu Xu8d0039c2017-10-13 07:51:46 -0700849 assert(cm->tile_width >> MAX_MIB_SIZE_LOG2 <= 32);
850 assert(cm->tile_height >> MAX_MIB_SIZE_LOG2 <= 32);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700851
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700852 // Get the number of tiles
853 cm->tile_cols = 1;
854 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700856 cm->tile_rows = 1;
857 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
Dominic Symes917d6c02017-10-11 18:00:52 +0200858 int i;
859 for (i = 0; i <= cm->tile_cols; i++) {
860 cm->tile_col_start_sb[i] =
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000861 ((i * cm->tile_width - 1) >> cm->seq_params.mib_size_log2) + 1;
Dominic Symes917d6c02017-10-11 18:00:52 +0200862 }
863 for (i = 0; i <= cm->tile_rows; i++) {
864 cm->tile_row_start_sb[i] =
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000865 ((i * cm->tile_height - 1) >> cm->seq_params.mib_size_log2) + 1;
Dominic Symes917d6c02017-10-11 18:00:52 +0200866 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700867 } else {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200868 set_tile_info_max_tile(cpi);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700869 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870}
871
Yaowu Xuf883b422016-08-30 14:01:10 -0700872static void update_frame_size(AV1_COMP *cpi) {
873 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700874 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
875
Yaowu Xuf883b422016-08-30 14:01:10 -0700876 av1_set_mb_mi(cm, cm->width, cm->height);
877 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500878 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700879 memset(cpi->mbmi_ext_base, 0,
880 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700881 set_tile_info(cpi);
882}
883
Yaowu Xuf883b422016-08-30 14:01:10 -0700884static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700885 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700886 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
887 cpi->ref_fb_idx[fb_idx] = fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700888 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
889 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700890 cpi->rate_index = 0;
891 cpi->rate_size = 0;
892 cpi->cur_poc = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700893}
894
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700895void init_seq_coding_tools(SequenceHeader *seq, const AV1EncoderConfig *oxcf) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700896 seq->still_picture = (oxcf->limit == 1);
897 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -0700898 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700899 seq->force_screen_content_tools = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700900 seq->force_integer_mv = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700901 seq->enable_order_hint = oxcf->enable_order_hint;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700902 seq->frame_id_numbers_present_flag = oxcf->large_scale_tile;
903 if (seq->still_picture && seq->reduced_still_picture_hdr) {
904 seq->enable_order_hint = 0;
905 seq->frame_id_numbers_present_flag = 0;
906 seq->force_screen_content_tools = 2;
907 seq->force_integer_mv = 2;
908 }
909 seq->order_hint_bits_minus1 =
910 seq->enable_order_hint ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1 : -1;
911
912 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700913 seq->enable_jnt_comp = oxcf->enable_jnt_comp;
914 seq->enable_jnt_comp &= seq->enable_order_hint;
915 seq->enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
916 seq->enable_ref_frame_mvs &= seq->enable_order_hint;
917 seq->enable_superres = oxcf->enable_superres;
918 seq->enable_cdef = oxcf->enable_cdef;
919 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -0700920 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee97095fb2018-03-26 07:51:48 -0700921 seq->enable_interintra_compound = 1;
922 seq->enable_masked_compound = 1;
Debargha Mukherjee365eae82018-03-26 19:19:55 -0700923 seq->enable_intra_edge_filter = 1;
924 seq->enable_filter_intra = 1;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700925}
926
Yaowu Xuf883b422016-08-30 14:01:10 -0700927static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
928 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929
930 cpi->oxcf = *oxcf;
931 cpi->framerate = oxcf->init_framerate;
932
933 cm->profile = oxcf->profile;
934 cm->bit_depth = oxcf->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935 cm->use_highbitdepth = oxcf->use_highbitdepth;
Andrey Norkin9e694632017-12-21 18:50:57 -0800936 cm->color_primaries = oxcf->color_primaries;
937 cm->transfer_characteristics = oxcf->transfer_characteristics;
938 cm->matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800939 cm->seq_params.monochrome = oxcf->monochrome;
anorkin76fb1262017-03-22 15:12:12 -0700940 cm->chroma_sample_position = oxcf->chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941 cm->color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800942 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -0800943#if !CONFIG_BUFFER_MODEL
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800944 cm->num_units_in_tick = oxcf->num_units_in_tick;
945 cm->time_scale = oxcf->time_scale;
946 cm->equal_picture_interval = oxcf->equal_picture_interval;
947 cm->num_ticks_per_picture = oxcf->num_ticks_per_picture;
Andrey Norkin795ba872018-03-06 13:24:14 -0800948#else
949 cm->timing_info.num_units_in_display_tick =
950 oxcf->timing_info.num_units_in_display_tick;
951 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
952 cm->timing_info.equal_picture_interval =
953 oxcf->timing_info.equal_picture_interval;
954 cm->timing_info.num_ticks_per_picture =
955 oxcf->timing_info.num_ticks_per_picture;
956
957 cm->decoder_model_info_present_flag = oxcf->decoder_model_info_present_flag;
958 cm->operating_points_decoder_model_cnt =
959 oxcf->operating_points_decoder_model_cnt;
960 if (oxcf->decoder_model_info_present_flag) {
961 cm->buffer_model.num_units_in_decoding_tick =
962 oxcf->buffer_model.num_units_in_decoding_tick;
963 cm->buffer_removal_delay_present = 1;
964 set_aom_dec_model_info(&cm->buffer_model);
965 set_dec_model_op_parameters(&cm->op_params[0], &cm->buffer_model,
966 oxcf->target_bandwidth, 0);
967 }
968#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700969 cm->width = oxcf->width;
970 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000971 set_sb_size(&cm->seq_params,
972 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -0700973 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -0700974
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800975 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700976
977 // Single thread case: use counts in common.
978 cpi->td.counts = &cm->counts;
979
980 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -0700981 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700982
983 cpi->static_mb_pct = 0;
984 cpi->ref_frame_flags = 0;
985
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700986 // Reset resize pending flags
987 cpi->resize_pending_width = 0;
988 cpi->resize_pending_height = 0;
989
Yaowu Xuc27fc142016-08-22 16:08:15 -0700990 init_buffer_indices(cpi);
991}
992
993static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -0700994 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700995 const int64_t bandwidth = oxcf->target_bandwidth;
996 const int64_t starting = oxcf->starting_buffer_level_ms;
997 const int64_t optimal = oxcf->optimal_buffer_level_ms;
998 const int64_t maximum = oxcf->maximum_buffer_size_ms;
999
1000 rc->starting_buffer_level = starting * bandwidth / 1000;
1001 rc->optimal_buffer_level =
1002 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1003 rc->maximum_buffer_size =
1004 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1005}
1006
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001007#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1008 cpi->fn_ptr[BT].sdf = SDF; \
1009 cpi->fn_ptr[BT].sdaf = SDAF; \
1010 cpi->fn_ptr[BT].vf = VF; \
1011 cpi->fn_ptr[BT].svf = SVF; \
1012 cpi->fn_ptr[BT].svaf = SVAF; \
1013 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1014 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001015 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001016
1017#define MAKE_BFP_SAD_WRAPPER(fnname) \
1018 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1019 int source_stride, \
1020 const uint8_t *ref_ptr, int ref_stride) { \
1021 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1022 } \
1023 static unsigned int fnname##_bits10( \
1024 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1025 int ref_stride) { \
1026 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1027 } \
1028 static unsigned int fnname##_bits12( \
1029 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1030 int ref_stride) { \
1031 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1032 }
1033
1034#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1035 static unsigned int fnname##_bits8( \
1036 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1037 int ref_stride, const uint8_t *second_pred) { \
1038 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1039 } \
1040 static unsigned int fnname##_bits10( \
1041 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1042 int ref_stride, const uint8_t *second_pred) { \
1043 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1044 2; \
1045 } \
1046 static unsigned int fnname##_bits12( \
1047 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1048 int ref_stride, const uint8_t *second_pred) { \
1049 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1050 4; \
1051 }
1052
Yaowu Xuc27fc142016-08-22 16:08:15 -07001053#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1054 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1055 const uint8_t *const ref_ptr[], int ref_stride, \
1056 unsigned int *sad_array) { \
1057 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1058 } \
1059 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1060 const uint8_t *const ref_ptr[], int ref_stride, \
1061 unsigned int *sad_array) { \
1062 int i; \
1063 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1064 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1065 } \
1066 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1067 const uint8_t *const ref_ptr[], int ref_stride, \
1068 unsigned int *sad_array) { \
1069 int i; \
1070 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1071 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1072 }
1073
Cheng Chenbf3d4962017-11-01 14:48:52 -07001074#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1075 static unsigned int fnname##_bits8( \
1076 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1077 int ref_stride, const uint8_t *second_pred, \
1078 const JNT_COMP_PARAMS *jcp_param) { \
1079 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1080 jcp_param); \
1081 } \
1082 static unsigned int fnname##_bits10( \
1083 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1084 int ref_stride, const uint8_t *second_pred, \
1085 const JNT_COMP_PARAMS *jcp_param) { \
1086 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1087 jcp_param) >> \
1088 2; \
1089 } \
1090 static unsigned int fnname##_bits12( \
1091 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1092 int ref_stride, const uint8_t *second_pred, \
1093 const JNT_COMP_PARAMS *jcp_param) { \
1094 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1095 jcp_param) >> \
1096 4; \
1097 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001098
Yaowu Xuf883b422016-08-30 14:01:10 -07001099MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1100MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001101MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1102MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1103MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1104MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1105MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1106MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1107MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001108MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1109MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1110MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1111MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1112MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1113MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1114MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1115MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1116MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1117MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1118MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1119MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1120MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1121MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001122MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1123MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1124MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001125MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1126MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1127MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001128MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1129MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1130MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001131MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1132MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1133MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001134MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1135MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1136MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001137MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1138MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1139MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001140MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1141MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1142MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001143MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1144MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1145MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001146MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001147
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001148MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1149MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1150MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1151MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1152MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1153MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1154MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1155MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1156MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1157MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1158MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1159MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001160MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1161MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1162MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1163MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1164MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1165MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001166
Cheng Chenbf3d4962017-11-01 14:48:52 -07001167MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x128_avg)
1168MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x64_avg)
1169MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x128_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001170MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x16_avg)
1171MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x32_avg)
1172MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x32_avg)
1173MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x64_avg)
1174MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x32_avg)
1175MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x64_avg)
1176MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x16_avg)
1177MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x8_avg)
1178MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x16_avg)
1179MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x8_avg)
1180MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x4_avg)
1181MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x8_avg)
1182MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x4_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001183MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x16_avg)
1184MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x4_avg)
1185MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x32_avg)
1186MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x8_avg)
1187MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x64_avg)
1188MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001189
David Barker0f3c94e2017-05-16 15:21:50 +01001190#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001191 cpi->fn_ptr[BT].msdf = MCSDF; \
1192 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001193
David Barkerc155e012017-05-11 13:54:54 +01001194#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1195 static unsigned int fnname##_bits8( \
1196 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1197 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1198 int m_stride, int invert_mask) { \
1199 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1200 second_pred_ptr, m, m_stride, invert_mask); \
1201 } \
1202 static unsigned int fnname##_bits10( \
1203 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1204 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1205 int m_stride, int invert_mask) { \
1206 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1207 second_pred_ptr, m, m_stride, invert_mask) >> \
1208 2; \
1209 } \
1210 static unsigned int fnname##_bits12( \
1211 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1212 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1213 int m_stride, int invert_mask) { \
1214 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1215 second_pred_ptr, m, m_stride, invert_mask) >> \
1216 4; \
1217 }
1218
David Barkerf19f35f2017-05-22 16:33:22 +01001219MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1220MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1221MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001222MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1223MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1224MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1225MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1226MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1227MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1228MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1229MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1230MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1231MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1232MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1233MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1234MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001235MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1236MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1237MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1238MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001239MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1240MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001241
Yaowu Xuc27fc142016-08-22 16:08:15 -07001242#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1243 cpi->fn_ptr[BT].osdf = OSDF; \
1244 cpi->fn_ptr[BT].ovf = OVF; \
1245 cpi->fn_ptr[BT].osvf = OSVF;
1246
1247#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1248 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1249 const int32_t *wsrc, \
1250 const int32_t *msk) { \
1251 return fnname(ref, ref_stride, wsrc, msk); \
1252 } \
1253 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1254 const int32_t *wsrc, \
1255 const int32_t *msk) { \
1256 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1257 } \
1258 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1259 const int32_t *wsrc, \
1260 const int32_t *msk) { \
1261 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1262 }
1263
Yaowu Xuf883b422016-08-30 14:01:10 -07001264MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1265MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1266MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001267MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1268MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1269MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1270MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1271MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1272MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1273MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1274MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1275MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1276MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1277MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1278MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1279MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001280MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1281MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1282MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1283MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001284MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1285MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001286
Yaowu Xuf883b422016-08-30 14:01:10 -07001287static void highbd_set_var_fns(AV1_COMP *const cpi) {
1288 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001289 if (cm->use_highbitdepth) {
1290 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001291 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001292 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1293 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1294 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001295 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001296 aom_highbd_sad64x16x4d_bits8,
1297 aom_highbd_jnt_sad64x16_avg_bits8,
1298 aom_highbd_8_jnt_sub_pixel_avg_variance64x16)
1299
1300 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1301 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1302 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001303 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001304 aom_highbd_sad16x64x4d_bits8,
1305 aom_highbd_jnt_sad16x64_avg_bits8,
1306 aom_highbd_8_jnt_sub_pixel_avg_variance16x64)
1307
1308 HIGHBD_BFP(
1309 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1310 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001311 aom_highbd_8_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001312 aom_highbd_sad32x8x4d_bits8, aom_highbd_jnt_sad32x8_avg_bits8,
1313 aom_highbd_8_jnt_sub_pixel_avg_variance32x8)
1314
1315 HIGHBD_BFP(
1316 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1317 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001318 aom_highbd_8_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001319 aom_highbd_sad8x32x4d_bits8, aom_highbd_jnt_sad8x32_avg_bits8,
1320 aom_highbd_8_jnt_sub_pixel_avg_variance8x32)
1321
1322 HIGHBD_BFP(
1323 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1324 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001325 aom_highbd_8_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001326 aom_highbd_sad16x4x4d_bits8, aom_highbd_jnt_sad16x4_avg_bits8,
1327 aom_highbd_8_jnt_sub_pixel_avg_variance16x4)
1328
1329 HIGHBD_BFP(
1330 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1331 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001332 aom_highbd_8_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001333 aom_highbd_sad4x16x4d_bits8, aom_highbd_jnt_sad4x16_avg_bits8,
1334 aom_highbd_8_jnt_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001335
1336 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1337 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1338 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001339 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001340 aom_highbd_sad32x16x4d_bits8,
1341 aom_highbd_jnt_sad32x16_avg_bits8,
1342 aom_highbd_8_jnt_sub_pixel_avg_variance32x16)
1343
1344 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1345 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1346 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001347 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001348 aom_highbd_sad16x32x4d_bits8,
1349 aom_highbd_jnt_sad16x32_avg_bits8,
1350 aom_highbd_8_jnt_sub_pixel_avg_variance16x32)
1351
1352 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1353 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1354 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001355 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001356 aom_highbd_sad64x32x4d_bits8,
1357 aom_highbd_jnt_sad64x32_avg_bits8,
1358 aom_highbd_8_jnt_sub_pixel_avg_variance64x32)
1359
1360 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1361 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1362 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001363 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001364 aom_highbd_sad32x64x4d_bits8,
1365 aom_highbd_jnt_sad32x64_avg_bits8,
1366 aom_highbd_8_jnt_sub_pixel_avg_variance32x64)
1367
1368 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1369 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1370 aom_highbd_8_sub_pixel_variance32x32,
1371 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001372 aom_highbd_sad32x32x4d_bits8,
1373 aom_highbd_jnt_sad32x32_avg_bits8,
1374 aom_highbd_8_jnt_sub_pixel_avg_variance32x32)
1375
1376 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1377 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1378 aom_highbd_8_sub_pixel_variance64x64,
1379 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001380 aom_highbd_sad64x64x4d_bits8,
1381 aom_highbd_jnt_sad64x64_avg_bits8,
1382 aom_highbd_8_jnt_sub_pixel_avg_variance64x64)
1383
1384 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1385 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1386 aom_highbd_8_sub_pixel_variance16x16,
1387 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001388 aom_highbd_sad16x16x4d_bits8,
1389 aom_highbd_jnt_sad16x16_avg_bits8,
1390 aom_highbd_8_jnt_sub_pixel_avg_variance16x16)
1391
1392 HIGHBD_BFP(
1393 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1394 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001395 aom_highbd_8_sub_pixel_avg_variance16x8,
1396 aom_highbd_sad16x8x4d_bits8, aom_highbd_jnt_sad16x8_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001397 aom_highbd_8_jnt_sub_pixel_avg_variance16x8)
1398
1399 HIGHBD_BFP(
1400 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1401 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001402 aom_highbd_8_sub_pixel_avg_variance8x16,
1403 aom_highbd_sad8x16x4d_bits8, aom_highbd_jnt_sad8x16_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001404 aom_highbd_8_jnt_sub_pixel_avg_variance8x16)
1405
1406 HIGHBD_BFP(BLOCK_8X8, aom_highbd_sad8x8_bits8,
1407 aom_highbd_sad8x8_avg_bits8, aom_highbd_8_variance8x8,
1408 aom_highbd_8_sub_pixel_variance8x8,
1409 aom_highbd_8_sub_pixel_avg_variance8x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001410 aom_highbd_sad8x8x4d_bits8, aom_highbd_jnt_sad8x8_avg_bits8,
1411 aom_highbd_8_jnt_sub_pixel_avg_variance8x8)
1412
1413 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1414 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1415 aom_highbd_8_sub_pixel_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001416 aom_highbd_8_sub_pixel_avg_variance8x4,
1417 aom_highbd_sad8x4x4d_bits8, aom_highbd_jnt_sad8x4_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001418 aom_highbd_8_jnt_sub_pixel_avg_variance8x4)
1419
1420 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1421 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1422 aom_highbd_8_sub_pixel_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001423 aom_highbd_8_sub_pixel_avg_variance4x8,
1424 aom_highbd_sad4x8x4d_bits8, aom_highbd_jnt_sad4x8_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001425 aom_highbd_8_jnt_sub_pixel_avg_variance4x8)
1426
1427 HIGHBD_BFP(BLOCK_4X4, aom_highbd_sad4x4_bits8,
1428 aom_highbd_sad4x4_avg_bits8, aom_highbd_8_variance4x4,
1429 aom_highbd_8_sub_pixel_variance4x4,
1430 aom_highbd_8_sub_pixel_avg_variance4x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001431 aom_highbd_sad4x4x4d_bits8, aom_highbd_jnt_sad4x4_avg_bits8,
1432 aom_highbd_8_jnt_sub_pixel_avg_variance4x4)
1433
Cheng Chenbf3d4962017-11-01 14:48:52 -07001434 HIGHBD_BFP(
1435 BLOCK_128X128, aom_highbd_sad128x128_bits8,
1436 aom_highbd_sad128x128_avg_bits8, aom_highbd_8_variance128x128,
1437 aom_highbd_8_sub_pixel_variance128x128,
1438 aom_highbd_8_sub_pixel_avg_variance128x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001439 aom_highbd_sad128x128x4d_bits8, aom_highbd_jnt_sad128x128_avg_bits8,
1440 aom_highbd_8_jnt_sub_pixel_avg_variance128x128)
1441
1442 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1443 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1444 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001445 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001446 aom_highbd_sad128x64x4d_bits8,
1447 aom_highbd_jnt_sad128x64_avg_bits8,
1448 aom_highbd_8_jnt_sub_pixel_avg_variance128x64)
1449
1450 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1451 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1452 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001453 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001454 aom_highbd_sad64x128x4d_bits8,
1455 aom_highbd_jnt_sad64x128_avg_bits8,
1456 aom_highbd_8_jnt_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001457
David Barkerf19f35f2017-05-22 16:33:22 +01001458 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1459 aom_highbd_8_masked_sub_pixel_variance128x128)
1460 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1461 aom_highbd_8_masked_sub_pixel_variance128x64)
1462 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1463 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001464 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1465 aom_highbd_8_masked_sub_pixel_variance64x64)
1466 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1467 aom_highbd_8_masked_sub_pixel_variance64x32)
1468 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1469 aom_highbd_8_masked_sub_pixel_variance32x64)
1470 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1471 aom_highbd_8_masked_sub_pixel_variance32x32)
1472 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1473 aom_highbd_8_masked_sub_pixel_variance32x16)
1474 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1475 aom_highbd_8_masked_sub_pixel_variance16x32)
1476 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1477 aom_highbd_8_masked_sub_pixel_variance16x16)
1478 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1479 aom_highbd_8_masked_sub_pixel_variance8x16)
1480 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1481 aom_highbd_8_masked_sub_pixel_variance16x8)
1482 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1483 aom_highbd_8_masked_sub_pixel_variance8x8)
1484 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1485 aom_highbd_8_masked_sub_pixel_variance4x8)
1486 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1487 aom_highbd_8_masked_sub_pixel_variance8x4)
1488 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1489 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001490 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1491 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001492 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1493 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001494 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1495 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001496 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1497 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001498 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1499 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001500 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1501 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001502 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1503 aom_highbd_obmc_variance128x128,
1504 aom_highbd_obmc_sub_pixel_variance128x128)
1505 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1506 aom_highbd_obmc_variance128x64,
1507 aom_highbd_obmc_sub_pixel_variance128x64)
1508 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1509 aom_highbd_obmc_variance64x128,
1510 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001511 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1512 aom_highbd_obmc_variance64x64,
1513 aom_highbd_obmc_sub_pixel_variance64x64)
1514 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1515 aom_highbd_obmc_variance64x32,
1516 aom_highbd_obmc_sub_pixel_variance64x32)
1517 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1518 aom_highbd_obmc_variance32x64,
1519 aom_highbd_obmc_sub_pixel_variance32x64)
1520 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1521 aom_highbd_obmc_variance32x32,
1522 aom_highbd_obmc_sub_pixel_variance32x32)
1523 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1524 aom_highbd_obmc_variance32x16,
1525 aom_highbd_obmc_sub_pixel_variance32x16)
1526 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1527 aom_highbd_obmc_variance16x32,
1528 aom_highbd_obmc_sub_pixel_variance16x32)
1529 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1530 aom_highbd_obmc_variance16x16,
1531 aom_highbd_obmc_sub_pixel_variance16x16)
1532 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1533 aom_highbd_obmc_variance8x16,
1534 aom_highbd_obmc_sub_pixel_variance8x16)
1535 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1536 aom_highbd_obmc_variance16x8,
1537 aom_highbd_obmc_sub_pixel_variance16x8)
1538 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1539 aom_highbd_obmc_variance8x8,
1540 aom_highbd_obmc_sub_pixel_variance8x8)
1541 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1542 aom_highbd_obmc_variance4x8,
1543 aom_highbd_obmc_sub_pixel_variance4x8)
1544 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1545 aom_highbd_obmc_variance8x4,
1546 aom_highbd_obmc_sub_pixel_variance8x4)
1547 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1548 aom_highbd_obmc_variance4x4,
1549 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001550 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1551 aom_highbd_obmc_variance64x16,
1552 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001553 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1554 aom_highbd_obmc_variance16x64,
1555 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001556 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1557 aom_highbd_obmc_variance32x8,
1558 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001559 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1560 aom_highbd_obmc_variance8x32,
1561 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001562 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1563 aom_highbd_obmc_variance16x4,
1564 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001565 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1566 aom_highbd_obmc_variance4x16,
1567 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001568 break;
1569
Yaowu Xuf883b422016-08-30 14:01:10 -07001570 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001571 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1572 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1573 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001574 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001575 aom_highbd_sad64x16x4d_bits10,
1576 aom_highbd_jnt_sad64x16_avg_bits10,
1577 aom_highbd_10_jnt_sub_pixel_avg_variance64x16);
1578
1579 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1580 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1581 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001582 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001583 aom_highbd_sad16x64x4d_bits10,
1584 aom_highbd_jnt_sad16x64_avg_bits10,
1585 aom_highbd_10_jnt_sub_pixel_avg_variance16x64);
1586
1587 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1588 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1589 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001590 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001591 aom_highbd_sad32x8x4d_bits10,
1592 aom_highbd_jnt_sad32x8_avg_bits10,
1593 aom_highbd_10_jnt_sub_pixel_avg_variance32x8);
1594
1595 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1596 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1597 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001598 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001599 aom_highbd_sad8x32x4d_bits10,
1600 aom_highbd_jnt_sad8x32_avg_bits10,
1601 aom_highbd_10_jnt_sub_pixel_avg_variance8x32);
1602
1603 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1604 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1605 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001606 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001607 aom_highbd_sad16x4x4d_bits10,
1608 aom_highbd_jnt_sad16x4_avg_bits10,
1609 aom_highbd_10_jnt_sub_pixel_avg_variance16x4);
1610
1611 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1612 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1613 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001614 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001615 aom_highbd_sad4x16x4d_bits10,
1616 aom_highbd_jnt_sad4x16_avg_bits10,
1617 aom_highbd_10_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001618
1619 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1620 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1621 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001622 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001623 aom_highbd_sad32x16x4d_bits10,
1624 aom_highbd_jnt_sad32x16_avg_bits10,
1625 aom_highbd_10_jnt_sub_pixel_avg_variance32x16);
1626
1627 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1628 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1629 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001630 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001631 aom_highbd_sad16x32x4d_bits10,
1632 aom_highbd_jnt_sad16x32_avg_bits10,
1633 aom_highbd_10_jnt_sub_pixel_avg_variance16x32);
1634
1635 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1636 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1637 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001638 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001639 aom_highbd_sad64x32x4d_bits10,
1640 aom_highbd_jnt_sad64x32_avg_bits10,
1641 aom_highbd_10_jnt_sub_pixel_avg_variance64x32);
1642
1643 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1644 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1645 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001646 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001647 aom_highbd_sad32x64x4d_bits10,
1648 aom_highbd_jnt_sad32x64_avg_bits10,
1649 aom_highbd_10_jnt_sub_pixel_avg_variance32x64);
1650
1651 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1652 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1653 aom_highbd_10_sub_pixel_variance32x32,
1654 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001655 aom_highbd_sad32x32x4d_bits10,
1656 aom_highbd_jnt_sad32x32_avg_bits10,
1657 aom_highbd_10_jnt_sub_pixel_avg_variance32x32);
1658
1659 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1660 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1661 aom_highbd_10_sub_pixel_variance64x64,
1662 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001663 aom_highbd_sad64x64x4d_bits10,
1664 aom_highbd_jnt_sad64x64_avg_bits10,
1665 aom_highbd_10_jnt_sub_pixel_avg_variance64x64);
1666
1667 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1668 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1669 aom_highbd_10_sub_pixel_variance16x16,
1670 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001671 aom_highbd_sad16x16x4d_bits10,
1672 aom_highbd_jnt_sad16x16_avg_bits10,
1673 aom_highbd_10_jnt_sub_pixel_avg_variance16x16);
1674
1675 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1676 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1677 aom_highbd_10_sub_pixel_variance16x8,
1678 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001679 aom_highbd_sad16x8x4d_bits10,
1680 aom_highbd_jnt_sad16x8_avg_bits10,
1681 aom_highbd_10_jnt_sub_pixel_avg_variance16x8);
1682
1683 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1684 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1685 aom_highbd_10_sub_pixel_variance8x16,
1686 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001687 aom_highbd_sad8x16x4d_bits10,
1688 aom_highbd_jnt_sad8x16_avg_bits10,
1689 aom_highbd_10_jnt_sub_pixel_avg_variance8x16);
1690
1691 HIGHBD_BFP(
1692 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1693 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001694 aom_highbd_10_sub_pixel_avg_variance8x8,
1695 aom_highbd_sad8x8x4d_bits10, aom_highbd_jnt_sad8x8_avg_bits10,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001696 aom_highbd_10_jnt_sub_pixel_avg_variance8x8);
1697
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001698 HIGHBD_BFP(
1699 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1700 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1701 aom_highbd_10_sub_pixel_avg_variance8x4,
1702 aom_highbd_sad8x4x4d_bits10, aom_highbd_jnt_sad8x4_avg_bits10,
1703 aom_highbd_10_jnt_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001704
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001705 HIGHBD_BFP(
1706 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1707 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1708 aom_highbd_10_sub_pixel_avg_variance4x8,
1709 aom_highbd_sad4x8x4d_bits10, aom_highbd_jnt_sad4x8_avg_bits10,
1710 aom_highbd_10_jnt_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001711
1712 HIGHBD_BFP(
1713 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1714 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001715 aom_highbd_10_sub_pixel_avg_variance4x4,
1716 aom_highbd_sad4x4x4d_bits10, aom_highbd_jnt_sad4x4_avg_bits10,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001717 aom_highbd_10_jnt_sub_pixel_avg_variance4x4);
1718
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001719 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1720 aom_highbd_sad128x128_avg_bits10,
1721 aom_highbd_10_variance128x128,
1722 aom_highbd_10_sub_pixel_variance128x128,
1723 aom_highbd_10_sub_pixel_avg_variance128x128,
1724 aom_highbd_sad128x128x4d_bits10,
1725 aom_highbd_jnt_sad128x128_avg_bits10,
1726 aom_highbd_10_jnt_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001727
1728 HIGHBD_BFP(
1729 BLOCK_128X64, aom_highbd_sad128x64_bits10,
1730 aom_highbd_sad128x64_avg_bits10, aom_highbd_10_variance128x64,
1731 aom_highbd_10_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001732 aom_highbd_10_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001733 aom_highbd_sad128x64x4d_bits10, aom_highbd_jnt_sad128x64_avg_bits10,
1734 aom_highbd_10_jnt_sub_pixel_avg_variance128x64);
1735
1736 HIGHBD_BFP(
1737 BLOCK_64X128, aom_highbd_sad64x128_bits10,
1738 aom_highbd_sad64x128_avg_bits10, aom_highbd_10_variance64x128,
1739 aom_highbd_10_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001740 aom_highbd_10_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001741 aom_highbd_sad64x128x4d_bits10, aom_highbd_jnt_sad64x128_avg_bits10,
1742 aom_highbd_10_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001743
David Barkerf19f35f2017-05-22 16:33:22 +01001744 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1745 aom_highbd_10_masked_sub_pixel_variance128x128)
1746 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1747 aom_highbd_10_masked_sub_pixel_variance128x64)
1748 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1749 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001750 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1751 aom_highbd_10_masked_sub_pixel_variance64x64)
1752 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1753 aom_highbd_10_masked_sub_pixel_variance64x32)
1754 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1755 aom_highbd_10_masked_sub_pixel_variance32x64)
1756 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1757 aom_highbd_10_masked_sub_pixel_variance32x32)
1758 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1759 aom_highbd_10_masked_sub_pixel_variance32x16)
1760 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1761 aom_highbd_10_masked_sub_pixel_variance16x32)
1762 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1763 aom_highbd_10_masked_sub_pixel_variance16x16)
1764 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1765 aom_highbd_10_masked_sub_pixel_variance8x16)
1766 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1767 aom_highbd_10_masked_sub_pixel_variance16x8)
1768 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1769 aom_highbd_10_masked_sub_pixel_variance8x8)
1770 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1771 aom_highbd_10_masked_sub_pixel_variance4x8)
1772 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1773 aom_highbd_10_masked_sub_pixel_variance8x4)
1774 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1775 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001776 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1777 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001778 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1779 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001780 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1781 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001782 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1783 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001784 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1785 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001786 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1787 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001788 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1789 aom_highbd_10_obmc_variance128x128,
1790 aom_highbd_10_obmc_sub_pixel_variance128x128)
1791 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1792 aom_highbd_10_obmc_variance128x64,
1793 aom_highbd_10_obmc_sub_pixel_variance128x64)
1794 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1795 aom_highbd_10_obmc_variance64x128,
1796 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001797 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1798 aom_highbd_10_obmc_variance64x64,
1799 aom_highbd_10_obmc_sub_pixel_variance64x64)
1800 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1801 aom_highbd_10_obmc_variance64x32,
1802 aom_highbd_10_obmc_sub_pixel_variance64x32)
1803 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
1804 aom_highbd_10_obmc_variance32x64,
1805 aom_highbd_10_obmc_sub_pixel_variance32x64)
1806 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
1807 aom_highbd_10_obmc_variance32x32,
1808 aom_highbd_10_obmc_sub_pixel_variance32x32)
1809 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
1810 aom_highbd_10_obmc_variance32x16,
1811 aom_highbd_10_obmc_sub_pixel_variance32x16)
1812 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
1813 aom_highbd_10_obmc_variance16x32,
1814 aom_highbd_10_obmc_sub_pixel_variance16x32)
1815 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
1816 aom_highbd_10_obmc_variance16x16,
1817 aom_highbd_10_obmc_sub_pixel_variance16x16)
1818 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
1819 aom_highbd_10_obmc_variance8x16,
1820 aom_highbd_10_obmc_sub_pixel_variance8x16)
1821 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
1822 aom_highbd_10_obmc_variance16x8,
1823 aom_highbd_10_obmc_sub_pixel_variance16x8)
1824 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
1825 aom_highbd_10_obmc_variance8x8,
1826 aom_highbd_10_obmc_sub_pixel_variance8x8)
1827 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
1828 aom_highbd_10_obmc_variance4x8,
1829 aom_highbd_10_obmc_sub_pixel_variance4x8)
1830 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
1831 aom_highbd_10_obmc_variance8x4,
1832 aom_highbd_10_obmc_sub_pixel_variance8x4)
1833 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
1834 aom_highbd_10_obmc_variance4x4,
1835 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001836
Rupert Swarbrick72678572017-08-02 12:05:26 +01001837 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
1838 aom_highbd_10_obmc_variance64x16,
1839 aom_highbd_10_obmc_sub_pixel_variance64x16)
1840
1841 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
1842 aom_highbd_10_obmc_variance16x64,
1843 aom_highbd_10_obmc_sub_pixel_variance16x64)
1844
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001845 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
1846 aom_highbd_10_obmc_variance32x8,
1847 aom_highbd_10_obmc_sub_pixel_variance32x8)
1848
1849 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
1850 aom_highbd_10_obmc_variance8x32,
1851 aom_highbd_10_obmc_sub_pixel_variance8x32)
1852
1853 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
1854 aom_highbd_10_obmc_variance16x4,
1855 aom_highbd_10_obmc_sub_pixel_variance16x4)
1856
1857 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
1858 aom_highbd_10_obmc_variance4x16,
1859 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001860 break;
1861
Yaowu Xuf883b422016-08-30 14:01:10 -07001862 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001863 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
1864 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
1865 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001866 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001867 aom_highbd_sad64x16x4d_bits12,
1868 aom_highbd_jnt_sad64x16_avg_bits12,
1869 aom_highbd_12_jnt_sub_pixel_avg_variance64x16);
1870
1871 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
1872 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
1873 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001874 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001875 aom_highbd_sad16x64x4d_bits12,
1876 aom_highbd_jnt_sad16x64_avg_bits12,
1877 aom_highbd_12_jnt_sub_pixel_avg_variance16x64);
1878
1879 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
1880 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
1881 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001882 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001883 aom_highbd_sad32x8x4d_bits12,
1884 aom_highbd_jnt_sad32x8_avg_bits12,
1885 aom_highbd_12_jnt_sub_pixel_avg_variance32x8);
1886
1887 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
1888 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
1889 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001890 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001891 aom_highbd_sad8x32x4d_bits12,
1892 aom_highbd_jnt_sad8x32_avg_bits12,
1893 aom_highbd_12_jnt_sub_pixel_avg_variance8x32);
1894
1895 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
1896 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
1897 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001898 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001899 aom_highbd_sad16x4x4d_bits12,
1900 aom_highbd_jnt_sad16x4_avg_bits12,
1901 aom_highbd_12_jnt_sub_pixel_avg_variance16x4);
1902
1903 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
1904 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
1905 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001906 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001907 aom_highbd_sad4x16x4d_bits12,
1908 aom_highbd_jnt_sad4x16_avg_bits12,
1909 aom_highbd_12_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001910
1911 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
1912 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
1913 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001914 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001915 aom_highbd_sad32x16x4d_bits12,
1916 aom_highbd_jnt_sad32x16_avg_bits12,
1917 aom_highbd_12_jnt_sub_pixel_avg_variance32x16);
1918
1919 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
1920 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
1921 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001922 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001923 aom_highbd_sad16x32x4d_bits12,
1924 aom_highbd_jnt_sad16x32_avg_bits12,
1925 aom_highbd_12_jnt_sub_pixel_avg_variance16x32);
1926
1927 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
1928 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
1929 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001930 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001931 aom_highbd_sad64x32x4d_bits12,
1932 aom_highbd_jnt_sad64x32_avg_bits12,
1933 aom_highbd_12_jnt_sub_pixel_avg_variance64x32);
1934
1935 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
1936 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
1937 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001938 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001939 aom_highbd_sad32x64x4d_bits12,
1940 aom_highbd_jnt_sad32x64_avg_bits12,
1941 aom_highbd_12_jnt_sub_pixel_avg_variance32x64);
1942
1943 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
1944 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
1945 aom_highbd_12_sub_pixel_variance32x32,
1946 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001947 aom_highbd_sad32x32x4d_bits12,
1948 aom_highbd_jnt_sad32x32_avg_bits12,
1949 aom_highbd_12_jnt_sub_pixel_avg_variance32x32);
1950
1951 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
1952 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
1953 aom_highbd_12_sub_pixel_variance64x64,
1954 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001955 aom_highbd_sad64x64x4d_bits12,
1956 aom_highbd_jnt_sad64x64_avg_bits12,
1957 aom_highbd_12_jnt_sub_pixel_avg_variance64x64);
1958
1959 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
1960 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
1961 aom_highbd_12_sub_pixel_variance16x16,
1962 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001963 aom_highbd_sad16x16x4d_bits12,
1964 aom_highbd_jnt_sad16x16_avg_bits12,
1965 aom_highbd_12_jnt_sub_pixel_avg_variance16x16);
1966
1967 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
1968 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
1969 aom_highbd_12_sub_pixel_variance16x8,
1970 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001971 aom_highbd_sad16x8x4d_bits12,
1972 aom_highbd_jnt_sad16x8_avg_bits12,
1973 aom_highbd_12_jnt_sub_pixel_avg_variance16x8);
1974
1975 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
1976 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
1977 aom_highbd_12_sub_pixel_variance8x16,
1978 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001979 aom_highbd_sad8x16x4d_bits12,
1980 aom_highbd_jnt_sad8x16_avg_bits12,
1981 aom_highbd_12_jnt_sub_pixel_avg_variance8x16);
1982
1983 HIGHBD_BFP(
1984 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
1985 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001986 aom_highbd_12_sub_pixel_avg_variance8x8,
1987 aom_highbd_sad8x8x4d_bits12, aom_highbd_jnt_sad8x8_avg_bits12,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001988 aom_highbd_12_jnt_sub_pixel_avg_variance8x8);
1989
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001990 HIGHBD_BFP(
1991 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
1992 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
1993 aom_highbd_12_sub_pixel_avg_variance8x4,
1994 aom_highbd_sad8x4x4d_bits12, aom_highbd_jnt_sad8x4_avg_bits12,
1995 aom_highbd_12_jnt_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001996
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001997 HIGHBD_BFP(
1998 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
1999 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2000 aom_highbd_12_sub_pixel_avg_variance4x8,
2001 aom_highbd_sad4x8x4d_bits12, aom_highbd_jnt_sad4x8_avg_bits12,
2002 aom_highbd_12_jnt_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002003
2004 HIGHBD_BFP(
2005 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2006 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002007 aom_highbd_12_sub_pixel_avg_variance4x4,
2008 aom_highbd_sad4x4x4d_bits12, aom_highbd_jnt_sad4x4_avg_bits12,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002009 aom_highbd_12_jnt_sub_pixel_avg_variance4x4);
2010
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002011 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2012 aom_highbd_sad128x128_avg_bits12,
2013 aom_highbd_12_variance128x128,
2014 aom_highbd_12_sub_pixel_variance128x128,
2015 aom_highbd_12_sub_pixel_avg_variance128x128,
2016 aom_highbd_sad128x128x4d_bits12,
2017 aom_highbd_jnt_sad128x128_avg_bits12,
2018 aom_highbd_12_jnt_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002019
2020 HIGHBD_BFP(
2021 BLOCK_128X64, aom_highbd_sad128x64_bits12,
2022 aom_highbd_sad128x64_avg_bits12, aom_highbd_12_variance128x64,
2023 aom_highbd_12_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002024 aom_highbd_12_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002025 aom_highbd_sad128x64x4d_bits12, aom_highbd_jnt_sad128x64_avg_bits12,
2026 aom_highbd_12_jnt_sub_pixel_avg_variance128x64);
2027
2028 HIGHBD_BFP(
2029 BLOCK_64X128, aom_highbd_sad64x128_bits12,
2030 aom_highbd_sad64x128_avg_bits12, aom_highbd_12_variance64x128,
2031 aom_highbd_12_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002032 aom_highbd_12_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002033 aom_highbd_sad64x128x4d_bits12, aom_highbd_jnt_sad64x128_avg_bits12,
2034 aom_highbd_12_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002035
David Barkerf19f35f2017-05-22 16:33:22 +01002036 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2037 aom_highbd_12_masked_sub_pixel_variance128x128)
2038 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2039 aom_highbd_12_masked_sub_pixel_variance128x64)
2040 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2041 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002042 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2043 aom_highbd_12_masked_sub_pixel_variance64x64)
2044 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2045 aom_highbd_12_masked_sub_pixel_variance64x32)
2046 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2047 aom_highbd_12_masked_sub_pixel_variance32x64)
2048 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2049 aom_highbd_12_masked_sub_pixel_variance32x32)
2050 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2051 aom_highbd_12_masked_sub_pixel_variance32x16)
2052 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2053 aom_highbd_12_masked_sub_pixel_variance16x32)
2054 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2055 aom_highbd_12_masked_sub_pixel_variance16x16)
2056 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2057 aom_highbd_12_masked_sub_pixel_variance8x16)
2058 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2059 aom_highbd_12_masked_sub_pixel_variance16x8)
2060 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2061 aom_highbd_12_masked_sub_pixel_variance8x8)
2062 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2063 aom_highbd_12_masked_sub_pixel_variance4x8)
2064 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2065 aom_highbd_12_masked_sub_pixel_variance8x4)
2066 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2067 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002068 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2069 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002070 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2071 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002072 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2073 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002074 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2075 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002076 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2077 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002078 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2079 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002080 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2081 aom_highbd_12_obmc_variance128x128,
2082 aom_highbd_12_obmc_sub_pixel_variance128x128)
2083 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2084 aom_highbd_12_obmc_variance128x64,
2085 aom_highbd_12_obmc_sub_pixel_variance128x64)
2086 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2087 aom_highbd_12_obmc_variance64x128,
2088 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002089 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2090 aom_highbd_12_obmc_variance64x64,
2091 aom_highbd_12_obmc_sub_pixel_variance64x64)
2092 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2093 aom_highbd_12_obmc_variance64x32,
2094 aom_highbd_12_obmc_sub_pixel_variance64x32)
2095 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2096 aom_highbd_12_obmc_variance32x64,
2097 aom_highbd_12_obmc_sub_pixel_variance32x64)
2098 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2099 aom_highbd_12_obmc_variance32x32,
2100 aom_highbd_12_obmc_sub_pixel_variance32x32)
2101 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2102 aom_highbd_12_obmc_variance32x16,
2103 aom_highbd_12_obmc_sub_pixel_variance32x16)
2104 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2105 aom_highbd_12_obmc_variance16x32,
2106 aom_highbd_12_obmc_sub_pixel_variance16x32)
2107 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2108 aom_highbd_12_obmc_variance16x16,
2109 aom_highbd_12_obmc_sub_pixel_variance16x16)
2110 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2111 aom_highbd_12_obmc_variance8x16,
2112 aom_highbd_12_obmc_sub_pixel_variance8x16)
2113 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2114 aom_highbd_12_obmc_variance16x8,
2115 aom_highbd_12_obmc_sub_pixel_variance16x8)
2116 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2117 aom_highbd_12_obmc_variance8x8,
2118 aom_highbd_12_obmc_sub_pixel_variance8x8)
2119 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2120 aom_highbd_12_obmc_variance4x8,
2121 aom_highbd_12_obmc_sub_pixel_variance4x8)
2122 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2123 aom_highbd_12_obmc_variance8x4,
2124 aom_highbd_12_obmc_sub_pixel_variance8x4)
2125 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2126 aom_highbd_12_obmc_variance4x4,
2127 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002128 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2129 aom_highbd_12_obmc_variance64x16,
2130 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002131 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2132 aom_highbd_12_obmc_variance16x64,
2133 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002134 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2135 aom_highbd_12_obmc_variance32x8,
2136 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002137 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2138 aom_highbd_12_obmc_variance8x32,
2139 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002140 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2141 aom_highbd_12_obmc_variance16x4,
2142 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002143 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2144 aom_highbd_12_obmc_variance4x16,
2145 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002146 break;
2147
2148 default:
2149 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07002150 "cm->bit_depth should be AOM_BITS_8, "
2151 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002152 }
2153 }
2154}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002155
Yaowu Xuf883b422016-08-30 14:01:10 -07002156static void realloc_segmentation_maps(AV1_COMP *cpi) {
2157 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002158
2159 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002160 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002161 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002162 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002163
2164 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002165 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002166 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002167 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002168
2169 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002170 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002171 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002172 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002173}
2174
Yaowu Xuf883b422016-08-30 14:01:10 -07002175void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2176 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002177 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002178 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002179 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002180
2181 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
2182 cm->bit_depth = oxcf->bit_depth;
Andrey Norkin9e694632017-12-21 18:50:57 -08002183 cm->color_primaries = oxcf->color_primaries;
2184 cm->transfer_characteristics = oxcf->transfer_characteristics;
2185 cm->matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08002186 cm->seq_params.monochrome = oxcf->monochrome;
anorkin76fb1262017-03-22 15:12:12 -07002187 cm->chroma_sample_position = oxcf->chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002188 cm->color_range = oxcf->color_range;
2189
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002190 assert(IMPLIES(cm->profile <= PROFILE_1, cm->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002191
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002192 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002193#if CONFIG_BUFFER_MODEL
2194 cm->timing_info.num_units_in_display_tick =
2195 oxcf->timing_info.num_units_in_display_tick;
2196 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2197 cm->timing_info.equal_picture_interval =
2198 oxcf->timing_info.equal_picture_interval;
2199 cm->timing_info.num_ticks_per_picture =
2200 oxcf->timing_info.num_ticks_per_picture;
2201
2202 cm->decoder_model_info_present_flag = oxcf->decoder_model_info_present_flag;
2203 cm->operating_points_decoder_model_cnt =
2204 oxcf->operating_points_decoder_model_cnt;
2205 if (oxcf->decoder_model_info_present_flag) {
2206 cm->buffer_model.num_units_in_decoding_tick =
2207 oxcf->buffer_model.num_units_in_decoding_tick;
2208 cm->buffer_removal_delay_present = 1;
2209 set_aom_dec_model_info(&cm->buffer_model);
2210 set_dec_model_op_parameters(&cm->op_params[0], &cm->buffer_model,
2211 oxcf->target_bandwidth, 0);
2212 }
2213#else
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002214 cm->num_units_in_tick = oxcf->num_units_in_tick;
2215 cm->time_scale = oxcf->time_scale;
2216 cm->equal_picture_interval = oxcf->equal_picture_interval;
2217 cm->num_ticks_per_picture = oxcf->num_ticks_per_picture;
Andrey Norkin795ba872018-03-06 13:24:14 -08002218#endif
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002219
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002220 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002221
Yaowu Xuc27fc142016-08-22 16:08:15 -07002222 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002223 cpi->common.options = oxcf->cfg;
hui sud9a812b2017-07-06 14:34:37 -07002224 x->e_mbd.bd = (int)cm->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002225 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002226
Yaowu Xuf883b422016-08-30 14:01:10 -07002227 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002228 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2229 } else {
2230 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2231 }
2232
2233 cpi->refresh_last_frame = 1;
2234 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002235 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002236 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002237
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002238 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2239 ? REFRESH_FRAME_CONTEXT_DISABLED
2240 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002241 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002242 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002243
Alex Converse74ad0912017-07-18 10:22:58 -07002244 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002245 CHECK_MEM_ERROR(cm, x->palette_buffer,
2246 aom_memalign(16, sizeof(*x->palette_buffer)));
2247 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002248 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002249 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002250
Yaowu Xuc27fc142016-08-22 16:08:15 -07002251 set_rc_buffer_sizes(rc, &cpi->oxcf);
2252
2253 // Under a configuration change, where maximum_buffer_size may change,
2254 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002255 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2256 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002257
2258 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002259 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002260
2261 // Set absolute upper and lower quality limits
2262 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2263 rc->best_quality = cpi->oxcf.best_allowed_q;
2264
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002265 if (!oxcf->large_scale_tile)
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002266 cm->interp_filter = cpi->sf.default_interp_filter;
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002267 else
2268 cm->interp_filter = EIGHTTAP_REGULAR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002269
Yue Chen5380cb52018-02-23 15:33:21 -08002270 cm->switchable_motion_mode = 1;
2271
Yaowu Xuc27fc142016-08-22 16:08:15 -07002272 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2273 cm->render_width = cpi->oxcf.render_width;
2274 cm->render_height = cpi->oxcf.render_height;
2275 } else {
2276 cm->render_width = cpi->oxcf.width;
2277 cm->render_height = cpi->oxcf.height;
2278 }
2279 cm->width = cpi->oxcf.width;
2280 cm->height = cpi->oxcf.height;
2281
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00002282 int sb_size = cm->seq_params.sb_size;
2283 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Dominic Symes917d6c02017-10-11 18:00:52 +02002284
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00002285 if (cpi->initial_width || sb_size != cm->seq_params.sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002286 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00002287 cm->seq_params.sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002288 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002289 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002290 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002291 realloc_segmentation_maps(cpi);
2292 cpi->initial_width = cpi->initial_height = 0;
2293 }
2294 }
2295 update_frame_size(cpi);
2296
2297 cpi->alt_ref_source = NULL;
2298 rc->is_src_frame_alt_ref = 0;
2299
Yaowu Xuc27fc142016-08-22 16:08:15 -07002300 rc->is_bwd_ref_frame = 0;
2301 rc->is_last_bipred_frame = 0;
2302 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002303
Yaowu Xuc27fc142016-08-22 16:08:15 -07002304 set_tile_info(cpi);
2305
2306 cpi->ext_refresh_frame_flags_pending = 0;
2307 cpi->ext_refresh_frame_context_pending = 0;
2308
Yaowu Xuc27fc142016-08-22 16:08:15 -07002309 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002310
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002311 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002312 // This should not be called after the first key frame.
2313 if (!cpi->seq_params_locked) {
2314 init_seq_coding_tools(&cm->seq_params, oxcf);
2315 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002316}
2317
Yaowu Xuf883b422016-08-30 14:01:10 -07002318AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2319 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002320 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002321 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2322 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002323
2324 if (!cm) return NULL;
2325
Yaowu Xuf883b422016-08-30 14:01:10 -07002326 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002327
2328 if (setjmp(cm->error.jmp)) {
2329 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002330 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002331 return 0;
2332 }
2333
2334 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002335 cm->alloc_mi = enc_alloc_mi;
2336 cm->free_mi = enc_free_mi;
2337 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002338
Angie Chianga5d96c42016-10-21 16:16:56 -07002339 CHECK_MEM_ERROR(cm, cm->fc,
2340 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2341 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2342 (FRAME_CONTEXT *)aom_memalign(
2343 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2344 memset(cm->fc, 0, sizeof(*cm->fc));
2345 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002346
2347 cpi->resize_state = 0;
2348 cpi->resize_avg_qp = 0;
2349 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002350
Yaowu Xuc27fc142016-08-22 16:08:15 -07002351 cpi->common.buffer_pool = pool;
2352
2353 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002354 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002355
2356 cm->current_video_frame = 0;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002357 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002358 cpi->partition_search_skippable_frame = 0;
2359 cpi->tile_data = NULL;
2360 cpi->last_show_frame_buf_idx = INVALID_IDX;
2361
2362 realloc_segmentation_maps(cpi);
2363
Jingning Hanf050fc12018-03-09 14:53:33 -08002364 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2365 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002366
Yaowu Xuc27fc142016-08-22 16:08:15 -07002367 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2368 i++) {
2369 CHECK_MEM_ERROR(
2370 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002371 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002372 }
2373
2374#if CONFIG_FP_MB_STATS
2375 cpi->use_fp_mb_stats = 0;
2376 if (cpi->use_fp_mb_stats) {
2377 // a place holder used to store the first pass mb stats in the first pass
2378 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002379 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002380 } else {
2381 cpi->twopass.frame_mb_stats_buf = NULL;
2382 }
2383#endif
2384
2385 cpi->refresh_alt_ref_frame = 0;
2386 cpi->multi_arf_last_grp_enabled = 0;
2387
2388 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2389#if CONFIG_INTERNAL_STATS
2390 cpi->b_calculate_blockiness = 1;
2391 cpi->b_calculate_consistency = 1;
2392 cpi->total_inconsistency = 0;
2393 cpi->psnr.worst = 100.0;
2394 cpi->worst_ssim = 100.0;
2395
2396 cpi->count = 0;
2397 cpi->bytes = 0;
2398
2399 if (cpi->b_calculate_psnr) {
2400 cpi->total_sq_error = 0;
2401 cpi->total_samples = 0;
2402 cpi->tot_recode_hits = 0;
2403 cpi->summed_quality = 0;
2404 cpi->summed_weights = 0;
2405 }
2406
2407 cpi->fastssim.worst = 100.0;
2408 cpi->psnrhvs.worst = 100.0;
2409
2410 if (cpi->b_calculate_blockiness) {
2411 cpi->total_blockiness = 0;
2412 cpi->worst_blockiness = 0.0;
2413 }
2414
2415 if (cpi->b_calculate_consistency) {
2416 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002417 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002418 cpi->common.mi_rows * cpi->common.mi_cols));
2419 cpi->worst_consistency = 100.0;
2420 }
2421#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002422#if CONFIG_ENTROPY_STATS
2423 av1_zero(aggregate_fc);
2424#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002425
2426 cpi->first_time_stamp_ever = INT64_MAX;
2427
Jingning Hanf050fc12018-03-09 14:53:33 -08002428 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2429 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2430 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2431 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002432
Yaowu Xuc27fc142016-08-22 16:08:15 -07002433#ifdef OUTPUT_YUV_SKINMAP
2434 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2435#endif
2436#ifdef OUTPUT_YUV_REC
2437 yuv_rec_file = fopen("rec.yuv", "wb");
2438#endif
2439
Yaowu Xuc27fc142016-08-22 16:08:15 -07002440 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002441 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002442 } else if (oxcf->pass == 2) {
2443 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2444 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2445
2446#if CONFIG_FP_MB_STATS
2447 if (cpi->use_fp_mb_stats) {
2448 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2449 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2450
2451 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2452 oxcf->firstpass_mb_stats_in.buf;
2453 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2454 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2455 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2456 }
2457#endif
2458
2459 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2460 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2461 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2462
Yaowu Xuf883b422016-08-30 14:01:10 -07002463 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002464 }
2465
Jingning Hand064cf02017-06-01 10:00:39 -07002466 CHECK_MEM_ERROR(
2467 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002468 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002469 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002470 CHECK_MEM_ERROR(
2471 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002472 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002473 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002474
2475 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2476 (int32_t *)aom_memalign(
2477 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2478
2479 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2480 (int32_t *)aom_memalign(
2481 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2482
Yaowu Xuf883b422016-08-30 14:01:10 -07002483 av1_set_speed_features_framesize_independent(cpi);
2484 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002485
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002486#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2487 cpi->fn_ptr[BT].sdf = SDF; \
2488 cpi->fn_ptr[BT].sdaf = SDAF; \
2489 cpi->fn_ptr[BT].vf = VF; \
2490 cpi->fn_ptr[BT].svf = SVF; \
2491 cpi->fn_ptr[BT].svaf = SVAF; \
2492 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2493 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002494 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002495
Cheng Chenf78632e2017-10-20 15:30:51 -07002496 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002497 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002498 aom_sad4x16x4d, aom_jnt_sad4x16_avg, aom_jnt_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002499
2500 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002501 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Cheng Chend0179a62017-11-16 17:02:53 -08002502 aom_sad16x4x4d, aom_jnt_sad16x4_avg, aom_jnt_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002503
2504 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002505 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002506 aom_sad8x32x4d, aom_jnt_sad8x32_avg, aom_jnt_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002507
2508 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002509 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Cheng Chend0179a62017-11-16 17:02:53 -08002510 aom_sad32x8x4d, aom_jnt_sad32x8_avg, aom_jnt_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002511
2512 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002513 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002514 aom_sad16x64x4d, aom_jnt_sad16x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002515 aom_jnt_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002516
2517 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002518 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002519 aom_sad64x16x4d, aom_jnt_sad64x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002520 aom_jnt_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002521
Cheng Chenf78632e2017-10-20 15:30:51 -07002522 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2523 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002524 aom_sad128x128x4d, aom_jnt_sad128x128_avg,
2525 aom_jnt_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002526
2527 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002528 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
2529 aom_sad128x64x4d, aom_jnt_sad128x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002530 aom_jnt_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002531
2532 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002533 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
2534 aom_sad64x128x4d, aom_jnt_sad64x128_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002535 aom_jnt_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002536
2537 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002538 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002539 aom_sad32x16x4d, aom_jnt_sad32x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002540 aom_jnt_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002541
2542 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002543 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002544 aom_sad16x32x4d, aom_jnt_sad16x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002545 aom_jnt_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002546
2547 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002548 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002549 aom_sad64x32x4d, aom_jnt_sad64x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002550 aom_jnt_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002551
2552 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002553 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002554 aom_sad32x64x4d, aom_jnt_sad32x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002555 aom_jnt_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002556
2557 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2558 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002559 aom_sad32x32x4d, aom_jnt_sad32x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002560 aom_jnt_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002561
2562 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2563 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002564 aom_sad64x64x4d, aom_jnt_sad64x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002565 aom_jnt_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002566
2567 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2568 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002569 aom_sad16x16x4d, aom_jnt_sad16x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002570 aom_jnt_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002571
2572 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002573 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
2574 aom_sad16x8x4d, aom_jnt_sad16x8_avg, aom_jnt_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002575
2576 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002577 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
2578 aom_sad8x16x4d, aom_jnt_sad8x16_avg, aom_jnt_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002579
2580 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002581 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
2582 aom_jnt_sad8x8_avg, aom_jnt_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002583
2584 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002585 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
2586 aom_jnt_sad8x4_avg, aom_jnt_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002587
2588 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002589 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
2590 aom_jnt_sad4x8_avg, aom_jnt_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002591
2592 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002593 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
2594 aom_jnt_sad4x4_avg, aom_jnt_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002595
Yaowu Xuc27fc142016-08-22 16:08:15 -07002596#define OBFP(BT, OSDF, OVF, OSVF) \
2597 cpi->fn_ptr[BT].osdf = OSDF; \
2598 cpi->fn_ptr[BT].ovf = OVF; \
2599 cpi->fn_ptr[BT].osvf = OSVF;
2600
Yaowu Xuf883b422016-08-30 14:01:10 -07002601 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2602 aom_obmc_sub_pixel_variance128x128)
2603 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2604 aom_obmc_sub_pixel_variance128x64)
2605 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2606 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002607 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2608 aom_obmc_sub_pixel_variance64x64)
2609 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2610 aom_obmc_sub_pixel_variance64x32)
2611 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2612 aom_obmc_sub_pixel_variance32x64)
2613 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2614 aom_obmc_sub_pixel_variance32x32)
2615 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2616 aom_obmc_sub_pixel_variance32x16)
2617 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2618 aom_obmc_sub_pixel_variance16x32)
2619 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2620 aom_obmc_sub_pixel_variance16x16)
2621 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2622 aom_obmc_sub_pixel_variance16x8)
2623 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2624 aom_obmc_sub_pixel_variance8x16)
2625 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2626 aom_obmc_sub_pixel_variance8x8)
2627 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2628 aom_obmc_sub_pixel_variance4x8)
2629 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2630 aom_obmc_sub_pixel_variance8x4)
2631 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2632 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002633 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2634 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002635 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2636 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002637 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2638 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002639 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2640 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002641 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2642 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002643 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2644 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002645
David Barkerf19f35f2017-05-22 16:33:22 +01002646#define MBFP(BT, MCSDF, MCSVF) \
2647 cpi->fn_ptr[BT].msdf = MCSDF; \
2648 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002649
David Barkerf19f35f2017-05-22 16:33:22 +01002650 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2651 aom_masked_sub_pixel_variance128x128)
2652 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2653 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002654 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2655 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2656 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2657 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2658 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2659 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2660 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2661 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2662 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2663 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2664 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2665 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2666 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002667
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002668 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2669
2670 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2671
2672 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2673
2674 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002675
2676 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2677
2678 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002679
Yaowu Xuc27fc142016-08-22 16:08:15 -07002680 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681
Yaowu Xuf883b422016-08-30 14:01:10 -07002682 /* av1_init_quantizer() is first called here. Add check in
2683 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002684 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002685 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002686 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002687 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08002688 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002689
Yaowu Xuf883b422016-08-30 14:01:10 -07002690 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07002691 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002692 cm->superres_upscaled_width = oxcf->width;
2693 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002694 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002695
2696 cm->error.setjmp = 0;
2697
2698 return cpi;
2699}
2700
2701#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2702
2703#define SNPRINT2(H, T, V) \
2704 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2705
Yaowu Xuf883b422016-08-30 14:01:10 -07002706void av1_remove_compressor(AV1_COMP *cpi) {
2707 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002708 unsigned int i;
2709 int t;
2710
2711 if (!cpi) return;
2712
2713 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002714 const int num_planes = av1_num_planes(cm);
2715
Yaowu Xuc27fc142016-08-22 16:08:15 -07002716 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002717#if CONFIG_ENTROPY_STATS
2718 if (cpi->oxcf.pass != 1) {
2719 fprintf(stderr, "Writing counts.stt\n");
2720 FILE *f = fopen("counts.stt", "wb");
2721 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
2722 fclose(f);
2723 }
2724#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002725#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07002726 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002727
2728 if (cpi->oxcf.pass != 1) {
2729 char headings[512] = { 0 };
2730 char results[512] = { 0 };
2731 FILE *f = fopen("opsnr.stt", "a");
2732 double time_encoded =
2733 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2734 10000000.000;
2735 double total_encode_time =
2736 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2737 const double dr =
2738 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2739 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2740 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2741 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2742
2743 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002744 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002745 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2746 const double total_ssim =
2747 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2748 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08002749 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07002750 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002751 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08002752 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002753 snprintf(results, sizeof(results),
2754 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2755 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002756 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08002757 "%7.3f\t%7.3f\t%7.3f",
Yaowu Xuc27fc142016-08-22 16:08:15 -07002758 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
Jingning Han87651b22017-11-28 20:02:26 -08002759 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002760 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2761 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002762 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Jingning Han87651b22017-11-28 20:02:26 -08002763 cpi->psnr.stat[Y] / cpi->count, cpi->psnr.stat[U] / cpi->count,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002764 cpi->psnr.stat[V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002765
2766 if (cpi->b_calculate_blockiness) {
2767 SNPRINT(headings, "\t Block\tWstBlck");
2768 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2769 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2770 }
2771
2772 if (cpi->b_calculate_consistency) {
2773 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07002774 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002775 (double)cpi->total_inconsistency);
2776
2777 SNPRINT(headings, "\tConsist\tWstCons");
2778 SNPRINT2(results, "\t%7.3f", consistency);
2779 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2780 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07002781 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2782 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002783 rate_err, fabs(rate_err));
2784 }
2785
2786 fclose(f);
2787 }
2788
2789#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002790 }
2791
2792 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002793 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002794 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2795
2796 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07002797 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002798
2799 // Deallocate allocated thread data.
2800 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07002801 aom_free(thread_data->td->palette_buffer);
Jingning Hand064cf02017-06-01 10:00:39 -07002802 aom_free(thread_data->td->above_pred_buf);
2803 aom_free(thread_data->td->left_pred_buf);
2804 aom_free(thread_data->td->wsrc_buf);
2805 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002806 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002807 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07002808 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002809 }
2810 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002811 aom_free(cpi->tile_thr_data);
2812 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002813
Yaowu Xuc27fc142016-08-22 16:08:15 -07002814 dealloc_compressor_data(cpi);
2815
2816 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2817 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002818 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002819 }
2820
2821#if CONFIG_FP_MB_STATS
2822 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002823 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002824 cpi->twopass.frame_mb_stats_buf = NULL;
2825 }
2826#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08002827#if CONFIG_INTERNAL_STATS
2828 aom_free(cpi->ssim_vars);
2829 cpi->ssim_vars = NULL;
2830#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002831
Yaowu Xuf883b422016-08-30 14:01:10 -07002832 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08002833 for (i = 0; i < FRAME_BUFFERS; ++i) {
2834 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
2835 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08002836 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xuf883b422016-08-30 14:01:10 -07002837 av1_free_ref_frame_buffers(cm->buffer_pool);
2838 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002839
2840#ifdef OUTPUT_YUV_SKINMAP
2841 fclose(yuv_skinmap_file);
2842#endif
2843#ifdef OUTPUT_YUV_REC
2844 fclose(yuv_rec_file);
2845#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002846}
2847
Yaowu Xuf883b422016-08-30 14:01:10 -07002848static void generate_psnr_packet(AV1_COMP *cpi) {
2849 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002850 int i;
2851 PSNR_STATS psnr;
Alex Conversef77fd0b2017-04-20 11:00:24 -07002852 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002853 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002854
2855 for (i = 0; i < 4; ++i) {
2856 pkt.data.psnr.samples[i] = psnr.samples[i];
2857 pkt.data.psnr.sse[i] = psnr.sse[i];
2858 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2859 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002860 pkt.kind = AOM_CODEC_PSNR_PKT;
2861 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002862}
2863
Yaowu Xuf883b422016-08-30 14:01:10 -07002864int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002865 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
2866
Yunqing Wangf2e7a392017-11-08 00:27:21 -08002867 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002868 return 0;
2869}
2870
Yunqing Wang9a50fec2017-11-02 17:02:00 -07002871void av1_update_reference(AV1_COMP *cpi, int ref_frame_upd_flags) {
2872 cpi->ext_refresh_last_frame = (ref_frame_upd_flags & AOM_LAST_FLAG) != 0;
2873 cpi->ext_refresh_golden_frame = (ref_frame_upd_flags & AOM_GOLD_FLAG) != 0;
2874 cpi->ext_refresh_alt_ref_frame = (ref_frame_upd_flags & AOM_ALT_FLAG) != 0;
2875 cpi->ext_refresh_bwd_ref_frame = (ref_frame_upd_flags & AOM_BWD_FLAG) != 0;
2876 cpi->ext_refresh_alt2_ref_frame = (ref_frame_upd_flags & AOM_ALT2_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002877 cpi->ext_refresh_frame_flags_pending = 1;
2878}
2879
Thomas Daede497d1952017-08-08 17:33:06 -07002880int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
2881 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002882 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07002883 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002884 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002885 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002886 return 0;
2887 } else {
2888 return -1;
2889 }
2890}
2891
Thomas Daede497d1952017-08-08 17:33:06 -07002892int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
2893 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002894 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07002895 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07002896 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002897 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07002898 return 0;
2899 } else {
2900 return -1;
2901 }
2902}
2903
2904int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002905 cpi->ext_refresh_frame_context = update;
2906 cpi->ext_refresh_frame_context_pending = 1;
2907 return 0;
2908}
2909
2910#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
2911// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2912// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2913// not denoise the UV channels at this time. If ever we implement UV channel
2914// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07002915void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002916 uint8_t *src = s->y_buffer;
2917 int h = s->y_height;
2918
2919 do {
2920 fwrite(src, s->y_width, 1, f);
2921 src += s->y_stride;
2922 } while (--h);
2923
2924 src = s->u_buffer;
2925 h = s->uv_height;
2926
2927 do {
2928 fwrite(src, s->uv_width, 1, f);
2929 src += s->uv_stride;
2930 } while (--h);
2931
2932 src = s->v_buffer;
2933 h = s->uv_height;
2934
2935 do {
2936 fwrite(src, s->uv_width, 1, f);
2937 src += s->uv_stride;
2938 } while (--h);
2939}
2940#endif
2941
Zoe Liu8dd1c982017-09-11 10:14:35 -07002942#if USE_GF16_MULTI_LAYER
2943static void check_show_existing_frame_gf16(AV1_COMP *cpi) {
2944 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2945 AV1_COMMON *const cm = &cpi->common;
2946 const FRAME_UPDATE_TYPE next_frame_update_type =
2947 gf_group->update_type[gf_group->index];
2948
2949 if (cm->show_existing_frame == 1) {
2950 cm->show_existing_frame = 0;
2951 } else if (cpi->rc.is_last_bipred_frame) {
2952 cpi->rc.is_last_bipred_frame = 0;
2953 cm->show_existing_frame = 1;
Zoe Liu5989a722018-03-29 13:37:36 -07002954 cpi->existing_fb_idx_to_show = cpi->ref_fb_idx[BWDREF_FRAME - 1];
Zoe Liu8dd1c982017-09-11 10:14:35 -07002955 } else if (next_frame_update_type == OVERLAY_UPDATE ||
2956 next_frame_update_type == INTNL_OVERLAY_UPDATE) {
2957 // Check the temporal filtering status for the next OVERLAY frame
2958 const int num_arfs_in_gf = cpi->num_extra_arfs + 1;
2959 int which_arf = 0, arf_idx;
2960 // Identify the index to the next overlay frame.
2961 for (arf_idx = 0; arf_idx < num_arfs_in_gf; arf_idx++) {
2962 if (gf_group->index == cpi->arf_pos_for_ovrly[arf_idx]) {
2963 which_arf = arf_idx;
2964 break;
2965 }
2966 }
2967 assert(arf_idx < num_arfs_in_gf);
2968 if (cpi->is_arf_filter_off[which_arf]) {
2969 cm->show_existing_frame = 1;
2970 cpi->rc.is_src_frame_alt_ref = 1;
2971 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
Zoe Liu5989a722018-03-29 13:37:36 -07002972 ? cpi->ref_fb_idx[ALTREF_FRAME - 1]
2973 : cpi->ref_fb_idx[BWDREF_FRAME - 1];
Zoe Liu8dd1c982017-09-11 10:14:35 -07002974 cpi->is_arf_filter_off[which_arf] = 0;
2975 }
2976 }
2977 cpi->rc.is_src_frame_ext_arf = 0;
2978}
2979#endif // USE_GF16_MULTI_LAYER
2980
Yaowu Xuf883b422016-08-30 14:01:10 -07002981static void check_show_existing_frame(AV1_COMP *cpi) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07002982#if USE_GF16_MULTI_LAYER
2983 if (cpi->rc.baseline_gf_interval == 16) {
2984 check_show_existing_frame_gf16(cpi);
2985 return;
2986 }
2987#endif // USE_GF16_MULTI_LAYER
2988
Yaowu Xuc27fc142016-08-22 16:08:15 -07002989 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07002990 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002991 const FRAME_UPDATE_TYPE next_frame_update_type =
2992 gf_group->update_type[gf_group->index];
2993 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07002994
2995 if (cm->show_existing_frame == 1) {
2996 cm->show_existing_frame = 0;
2997 } else if (cpi->rc.is_last_bipred_frame) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07002998 // NOTE: If the current frame is a last bi-predictive frame, it is
2999 // needed next to show the BWDREF_FRAME, which is pointed by
3000 // the last_fb_idxes[0] after reference frame buffer update
Yaowu Xuc27fc142016-08-22 16:08:15 -07003001 cpi->rc.is_last_bipred_frame = 0;
3002 cm->show_existing_frame = 1;
Zoe Liu5989a722018-03-29 13:37:36 -07003003 cpi->existing_fb_idx_to_show = cpi->ref_fb_idx[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003004 } else if (cpi->is_arf_filter_off[which_arf] &&
3005 (next_frame_update_type == OVERLAY_UPDATE ||
3006 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
3007 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003008 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003009 cm->show_existing_frame = 1;
3010 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003011 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
Zoe Liu5989a722018-03-29 13:37:36 -07003012 ? cpi->ref_fb_idx[ALTREF_FRAME - 1]
3013 : cpi->ref_fb_idx[ALTREF2_FRAME - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003014 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003015 }
3016 cpi->rc.is_src_frame_ext_arf = 0;
3017}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003018
3019#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003020void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003021 uint8_t *src = s->y_buffer;
3022 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003023 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003024 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3025 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3026
3027 do {
3028 fwrite(src16, s->y_width, 2, yuv_rec_file);
3029 src16 += s->y_stride;
3030 } while (--h);
3031
3032 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3033 h = s->uv_height;
3034
3035 do {
3036 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3037 src16 += s->uv_stride;
3038 } while (--h);
3039
3040 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3041 h = s->uv_height;
3042
3043 do {
3044 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3045 src16 += s->uv_stride;
3046 } while (--h);
3047
3048 fflush(yuv_rec_file);
3049 return;
3050 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003051
3052 do {
3053 fwrite(src, s->y_width, 1, yuv_rec_file);
3054 src += s->y_stride;
3055 } while (--h);
3056
3057 src = s->u_buffer;
3058 h = s->uv_height;
3059
3060 do {
3061 fwrite(src, s->uv_width, 1, yuv_rec_file);
3062 src += s->uv_stride;
3063 } while (--h);
3064
3065 src = s->v_buffer;
3066 h = s->uv_height;
3067
3068 do {
3069 fwrite(src, s->uv_width, 1, yuv_rec_file);
3070 src += s->uv_stride;
3071 } while (--h);
3072
3073 fflush(yuv_rec_file);
3074}
3075#endif // OUTPUT_YUV_REC
3076
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003077#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003078static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3079 int i;
3080 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003081 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003082 AV1_COMMON *const cm = &cpi->common;
3083 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3084 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003085 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003086 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003087 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003088 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003089 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003090 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003091 // TODO(sarahparker): The earlier condition for recoding here was:
3092 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3093 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003094 }
3095 }
3096 return recode;
3097}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003098
Yaowu Xuc27fc142016-08-22 16:08:15 -07003099// Function to test for conditions that indicate we should loop
3100// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003101static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3102 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003103 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003104 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003105 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3106 int force_recode = 0;
3107
3108 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3109 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3110 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003111 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3112 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3113 (rc->projected_frame_size < low_limit && q > minq)) {
3114 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003115 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003116 // Deal with frame undershoot and whether or not we are
3117 // below the automatically set cq level.
3118 if (q > oxcf->cq_level &&
3119 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3120 force_recode = 1;
3121 }
3122 }
3123 }
3124 return force_recode;
3125}
3126
Yaowu Xuc27fc142016-08-22 16:08:15 -07003127#define DUMP_REF_FRAME_IMAGES 0
3128
3129#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003130static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003131 const YV12_BUFFER_CONFIG *const ref_buf,
3132 char *file_name) {
3133 int h;
3134 FILE *f_ref = NULL;
3135
3136 if (ref_buf == NULL) {
3137 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003138 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003139 }
3140
3141 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3142 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003143 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003144 }
3145
3146 // --- Y ---
3147 for (h = 0; h < cm->height; ++h) {
3148 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3149 }
3150 // --- U ---
3151 for (h = 0; h < (cm->height >> 1); ++h) {
3152 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3153 f_ref);
3154 }
3155 // --- V ---
3156 for (h = 0; h < (cm->height >> 1); ++h) {
3157 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3158 f_ref);
3159 }
3160
3161 fclose(f_ref);
3162
Yaowu Xuf883b422016-08-30 14:01:10 -07003163 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003164}
3165
Yaowu Xuf883b422016-08-30 14:01:10 -07003166static void dump_ref_frame_images(AV1_COMP *cpi) {
3167 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003168 MV_REFERENCE_FRAME ref_frame;
3169
3170 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3171 char file_name[256] = "";
3172 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3173 cm->current_video_frame, ref_frame);
3174 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3175 }
3176}
3177#endif // DUMP_REF_FRAME_IMAGES == 1
3178
Yaowu Xuc27fc142016-08-22 16:08:15 -07003179// This function is used to shift the virtual indices of last reference frames
3180// as follows:
3181// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3182// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003183static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003184 // TODO(isbs): shift the scaled indices as well
Yaowu Xuc27fc142016-08-22 16:08:15 -07003185 int ref_frame;
3186 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003187 cpi->ref_fb_idx[ref_frame] = cpi->ref_fb_idx[ref_frame - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003188
3189 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003190 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003191 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003192 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3193 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3194 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003195 }
3196 }
3197}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003198
Zoe Liu8dd1c982017-09-11 10:14:35 -07003199#if USE_GF16_MULTI_LAYER
3200static void update_reference_frames_gf16(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003201 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003202 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003203
Zoe Liu8dd1c982017-09-11 10:14:35 -07003204 if (cm->frame_type == KEY_FRAME) {
Zoe Liu5989a722018-03-29 13:37:36 -07003205 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003206 ref_cnt_fb(pool->frame_bufs,
Zoe Liu5989a722018-03-29 13:37:36 -07003207 &cm->ref_frame_map[cpi->ref_fb_idx[ref_frame]],
Zoe Liu8dd1c982017-09-11 10:14:35 -07003208 cm->new_fb_idx);
3209 }
Zoe Liu8dd1c982017-09-11 10:14:35 -07003210 } else {
3211 if (cpi->refresh_last_frame || cpi->refresh_golden_frame ||
3212 cpi->refresh_bwd_ref_frame || cpi->refresh_alt2_ref_frame ||
3213 cpi->refresh_alt_ref_frame) {
3214 assert(cpi->refresh_fb_idx >= 0 && cpi->refresh_fb_idx < REF_FRAMES);
3215 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->refresh_fb_idx],
3216 cm->new_fb_idx);
3217 }
3218
3219 // TODO(zoeliu): To handle cpi->interp_filter_selected[].
3220
3221 // For GF of 16, an additional ref frame index mapping needs to be handled
3222 // if this is the last frame to encode in the current GF group.
3223 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3224 if (gf_group->update_type[gf_group->index + 1] == OVERLAY_UPDATE)
3225 av1_ref_frame_map_idx_updates(cpi, gf_group->index + 1);
3226 }
3227
3228#if DUMP_REF_FRAME_IMAGES == 1
3229 // Dump out all reference frame images.
3230 dump_ref_frame_images(cpi);
3231#endif // DUMP_REF_FRAME_IMAGES
3232}
3233#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003234
3235static void update_reference_frames(AV1_COMP *cpi) {
3236 AV1_COMMON *const cm = &cpi->common;
3237
Yaowu Xuc27fc142016-08-22 16:08:15 -07003238 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3239 // for the purpose to verify no mismatch between encoder and decoder.
3240 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3241
Zoe Liu8dd1c982017-09-11 10:14:35 -07003242#if USE_GF16_MULTI_LAYER
3243 if (cpi->rc.baseline_gf_interval == 16) {
3244 update_reference_frames_gf16(cpi);
3245 return;
3246 }
3247#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003248
3249 BufferPool *const pool = cm->buffer_pool;
Zoe Liubcef1e62018-04-06 20:56:11 -07003250
Yaowu Xuc27fc142016-08-22 16:08:15 -07003251 // At this point the new frame has been encoded.
3252 // If any buffer copy / swapping is signaled it should be done here.
Zoe Liubcef1e62018-04-06 20:56:11 -07003253
Tarek AMARAc9813852018-03-05 18:40:18 -05003254 if (cm->frame_type == KEY_FRAME || frame_is_sframe(cm)) {
Zoe Liubcef1e62018-04-06 20:56:11 -07003255 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame) {
3256 ref_cnt_fb(pool->frame_bufs,
3257 &cm->ref_frame_map[cpi->ref_fb_idx[ref_frame]],
3258 cm->new_fb_idx);
3259 }
3260 return;
3261 }
3262
3263 if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003264 // We have decided to preserve the previously existing golden frame as our
3265 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003266 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003267 // we're updating the GF with the current decoded frame, we save it to the
3268 // ARF slot instead.
3269 // We now have to update the ARF with the current frame and swap gld_fb_idx
3270 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3271 // slot and, if we're updating the GF, the current frame becomes the new GF.
3272 int tmp;
3273
Zoe Liu5989a722018-03-29 13:37:36 -07003274 ref_cnt_fb(pool->frame_bufs,
3275 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003276 cm->new_fb_idx);
Zoe Liu5989a722018-03-29 13:37:36 -07003277 tmp = cpi->ref_fb_idx[ALTREF_FRAME - 1];
3278 cpi->ref_fb_idx[ALTREF_FRAME - 1] = cpi->ref_fb_idx[GOLDEN_FRAME - 1];
3279 cpi->ref_fb_idx[GOLDEN_FRAME - 1] = tmp;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003280
Yaowu Xuc27fc142016-08-22 16:08:15 -07003281 // We need to modify the mapping accordingly
Zoe Liu5989a722018-03-29 13:37:36 -07003282 cpi->arf_map[0] = cpi->ref_fb_idx[ALTREF_FRAME - 1];
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003283 // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3284 // cpi->interp_filter_selected[GOLDEN_FRAME]?
Yaowu Xuc27fc142016-08-22 16:08:15 -07003285 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3286 // Deal with the special case for showing existing internal ALTREF_FRAME
3287 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3288 // by updating the virtual indices.
3289 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -07003290 const int which_arf = gf_group->arf_ref_idx[gf_group->index];
3291 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003292
Zoe Liu5989a722018-03-29 13:37:36 -07003293 const int tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003294 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003295
Zoe Liu5989a722018-03-29 13:37:36 -07003296 cpi->ref_fb_idx[LAST_FRAME - 1] = cpi->ref_fb_idx[ALTREF2_FRAME - 1];
3297 cpi->ref_fb_idx[ALTREF2_FRAME - 1] = tmp;
Zoe Liue9b15e22017-07-19 15:53:01 -07003298 // We need to modify the mapping accordingly
Zoe Liu5989a722018-03-29 13:37:36 -07003299 cpi->arf_map[which_arf] = cpi->ref_fb_idx[ALTREF2_FRAME - 1];
Zoe Liue9b15e22017-07-19 15:53:01 -07003300
3301 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3302 cpi->interp_filter_selected[ALTREF2_FRAME],
3303 sizeof(cpi->interp_filter_selected[ALTREF2_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003304 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003305 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003306 if (cpi->refresh_alt_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003307 int arf_idx = cpi->ref_fb_idx[ALTREF_FRAME - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003308 int which_arf = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003309 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003310
3311 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3312 cpi->interp_filter_selected[0],
3313 sizeof(cpi->interp_filter_selected[0]));
3314 }
3315
Zoe Liue9b15e22017-07-19 15:53:01 -07003316 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003317 if (cpi->refresh_golden_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003318 ref_cnt_fb(pool->frame_bufs,
3319 &cm->ref_frame_map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003320 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003321
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003322 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3323 cpi->interp_filter_selected[0],
3324 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003325 }
3326
Zoe Liue9b15e22017-07-19 15:53:01 -07003327 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003328 if (cpi->refresh_bwd_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003329 ref_cnt_fb(pool->frame_bufs,
3330 &cm->ref_frame_map[cpi->ref_fb_idx[BWDREF_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003331 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003332
3333 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3334 cpi->interp_filter_selected[0],
3335 sizeof(cpi->interp_filter_selected[0]));
3336 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003337
Zoe Liue9b15e22017-07-19 15:53:01 -07003338 // === ALTREF2_FRAME ===
3339 if (cpi->refresh_alt2_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003340 ref_cnt_fb(pool->frame_bufs,
3341 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]],
Zoe Liue9b15e22017-07-19 15:53:01 -07003342 cm->new_fb_idx);
3343
3344 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3345 cpi->interp_filter_selected[0],
3346 sizeof(cpi->interp_filter_selected[0]));
3347 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003348 }
3349
3350 if (cpi->refresh_last_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003351 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3352 // reference to the reference frame buffer virtual index; and then (2) from
3353 // the virtual index to the reference frame buffer physical index:
3354 //
3355 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3356 // | | |
3357 // v v v
Zoe Liu5989a722018-03-29 13:37:36 -07003358 // ref_fb_idx[0], ..., ref_fb_idx[2], ..., ref_fb_idx[ALTREF_FRAME-1]
Yaowu Xuc27fc142016-08-22 16:08:15 -07003359 // | | |
3360 // v v v
3361 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3362 //
3363 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3364 // have the other 2 LAST reference frames shifted as follows:
3365 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3366 // , and then have LAST_FRAME refreshed by the newly coded frame.
3367 //
3368 // To fulfill it, the decoder will be notified to execute following 2 steps:
3369 //
3370 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3371 // to point to the newly coded frame, i.e.
3372 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3373 //
3374 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3375 // original virtual index of LAST3_FRAME and have the other mappings
3376 // shifted as follows:
3377 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3378 // | | |
3379 // v v v
Zoe Liu5989a722018-03-29 13:37:36 -07003380 // ref_fb_idx[2], ref_fb_idx[0], ref_fb_idx[1]
Zoe Liubcef1e62018-04-06 20:56:11 -07003381 int tmp;
Zoe Liu5fca7242016-10-10 17:18:57 -07003382
Zoe Liubcef1e62018-04-06 20:56:11 -07003383 ref_cnt_fb(pool->frame_bufs,
3384 &cm->ref_frame_map[cpi->ref_fb_idx[LAST_REF_FRAMES - 1]],
3385 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386
Zoe Liubcef1e62018-04-06 20:56:11 -07003387 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003388
Zoe Liubcef1e62018-04-06 20:56:11 -07003389 shift_last_ref_frames(cpi);
3390 cpi->ref_fb_idx[0] = tmp;
3391
3392 assert(cm->show_existing_frame == 0);
3393 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3394 cpi->interp_filter_selected[0],
3395 sizeof(cpi->interp_filter_selected[0]));
3396
3397 if (cpi->rc.is_last_bipred_frame) {
3398 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3399 // LAST3_FRAME by updating the virtual indices.
3400 //
3401 // NOTE: The source frame for BWDREF does not have a holding position as
3402 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3403 // virtual index reshuffling for BWDREF, the encoder always
3404 // specifies a LAST_BIPRED right before BWDREF and completes the
3405 // reshuffling job accordingly.
Zoe Liu5989a722018-03-29 13:37:36 -07003406 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003407
3408 shift_last_ref_frames(cpi);
Zoe Liubcef1e62018-04-06 20:56:11 -07003409 cpi->ref_fb_idx[0] = cpi->ref_fb_idx[BWDREF_FRAME - 1];
3410 cpi->ref_fb_idx[BWDREF_FRAME - 1] = tmp;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411
Zoe Liuf0e46692016-10-12 12:31:43 -07003412 memcpy(cpi->interp_filter_selected[LAST_FRAME],
Zoe Liubcef1e62018-04-06 20:56:11 -07003413 cpi->interp_filter_selected[BWDREF_FRAME],
3414 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003415 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416 }
3417
3418#if DUMP_REF_FRAME_IMAGES == 1
3419 // Dump out all reference frame images.
3420 dump_ref_frame_images(cpi);
3421#endif // DUMP_REF_FRAME_IMAGES
3422}
3423
Yaowu Xuf883b422016-08-30 14:01:10 -07003424static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003425 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003426 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003427 ensure_mv_buffer(new_fb_ptr, cm);
3428 new_fb_ptr->width = cm->width;
3429 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003430}
3431
Cheng Chen46f30c72017-09-07 11:13:33 -07003432static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003433 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003434 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003435 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003436 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003437 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3438 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003439 };
3440
3441 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003442 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003443 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3444 BufferPool *const pool = cm->buffer_pool;
3445 const YV12_BUFFER_CONFIG *const ref =
3446 get_ref_frame_buffer(cpi, ref_frame);
3447
3448 if (ref == NULL) {
3449 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3450 continue;
3451 }
3452
Yaowu Xuc27fc142016-08-22 16:08:15 -07003453 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3454 RefCntBuffer *new_fb_ptr = NULL;
3455 int force_scaling = 0;
3456 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3457 if (new_fb == INVALID_IDX) {
3458 new_fb = get_free_fb(cm);
3459 force_scaling = 1;
3460 }
3461 if (new_fb == INVALID_IDX) return;
3462 new_fb_ptr = &pool->frame_bufs[new_fb];
3463 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3464 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003465 if (aom_realloc_frame_buffer(
3466 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3467 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3468 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003469 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003470 "Failed to allocate frame buffer");
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003471 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth,
3472 num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003473 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3474 alloc_frame_mvs(cm, new_fb);
3475 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003476 } else {
3477 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3478 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3479 buf->buf.y_crop_width = ref->y_crop_width;
3480 buf->buf.y_crop_height = ref->y_crop_height;
3481 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3482 ++buf->ref_count;
3483 }
3484 } else {
3485 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3486 }
3487 }
3488}
3489
Yaowu Xuf883b422016-08-30 14:01:10 -07003490static void release_scaled_references(AV1_COMP *cpi) {
3491 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003492 int i;
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003493 // TODO(isbs): only refresh the necessary frames, rather than all of them
Zoe Liu27deb382018-03-27 15:13:56 -07003494 for (i = 0; i < REF_FRAMES; ++i) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003495 const int idx = cpi->scaled_ref_idx[i];
3496 RefCntBuffer *const buf =
3497 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3498 if (buf != NULL) {
3499 --buf->ref_count;
3500 cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003501 }
3502 }
3503}
3504
Yaowu Xuf883b422016-08-30 14:01:10 -07003505static void set_mv_search_params(AV1_COMP *cpi) {
3506 const AV1_COMMON *const cm = &cpi->common;
3507 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003508
3509 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003510 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003511
3512 if (cpi->sf.mv.auto_mv_step_size) {
3513 if (frame_is_intra_only(cm)) {
3514 // Initialize max_mv_magnitude for use in the first INTER frame
3515 // after a key/intra-only frame.
3516 cpi->max_mv_magnitude = max_mv_def;
3517 } else {
3518 if (cm->show_frame) {
3519 // Allow mv_steps to correspond to twice the max mv magnitude found
3520 // in the previous frame, capped by the default max_mv_magnitude based
3521 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003522 cpi->mv_step_param = av1_init_search_range(
3523 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003524 }
3525 cpi->max_mv_magnitude = 0;
3526 }
3527 }
3528}
3529
Yaowu Xuf883b422016-08-30 14:01:10 -07003530static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003531 int i;
3532 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003533 cpi->common.global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003534 }
3535 cpi->global_motion_search_done = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003536 av1_set_speed_features_framesize_independent(cpi);
3537 av1_set_rd_speed_thresholds(cpi);
3538 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003539 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Yue Chen5380cb52018-02-23 15:33:21 -08003540 cpi->common.switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003541}
3542
Yaowu Xuf883b422016-08-30 14:01:10 -07003543static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003544 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003545 AV1_COMMON *const cm = &cpi->common;
3546 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003547
3548 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003549 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003550
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003551 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003552 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3553 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003554
James Zern01a9d702017-08-25 19:09:33 +00003555 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003556 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003557 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003558 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003559
3560 // Configure experimental use of segmentation for enhanced coding of
3561 // static regions if indicated.
3562 // Only allowed in the second pass of a two pass encode, as it requires
3563 // lagged coding, and if the relevant speed feature flag is set.
3564 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3565 configure_static_seg_features(cpi);
3566}
3567
Yaowu Xuf883b422016-08-30 14:01:10 -07003568static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569 int y_stride = cpi->scaled_source.y_stride;
3570
3571 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003572 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003573 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003574 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003575 }
3576}
3577
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003578#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003579static void set_restoration_unit_size(int width, int height, int sx, int sy,
3580 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003581 (void)width;
3582 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003583 (void)sx;
3584 (void)sy;
3585#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3586 int s = AOMMIN(sx, sy);
3587#else
3588 int s = 0;
3589#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3590
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003591 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003592 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003593 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003594 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003595 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3596 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003597}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003598
Cheng Chen46f30c72017-09-07 11:13:33 -07003599static void init_ref_frame_bufs(AV1_COMMON *cm) {
3600 int i;
3601 BufferPool *const pool = cm->buffer_pool;
3602 cm->new_fb_idx = INVALID_IDX;
3603 for (i = 0; i < REF_FRAMES; ++i) {
3604 cm->ref_frame_map[i] = INVALID_IDX;
3605 pool->frame_bufs[i].ref_count = 0;
3606 }
RogerZhou86902d02018-02-28 15:29:16 -08003607 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003608 for (i = 0; i < FRAME_BUFFERS; ++i) {
3609 av1_hash_table_init(&pool->frame_bufs[i].hash_table);
3610 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003611 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003612}
3613
Yaowu Xud3e7c682017-12-21 14:08:25 -08003614static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003615 int subsampling_x, int subsampling_y) {
3616 AV1_COMMON *const cm = &cpi->common;
3617
Yaowu Xud3e7c682017-12-21 14:08:25 -08003618 if (!cpi->initial_width || cm->use_highbitdepth != use_highbitdepth ||
Cheng Chen46f30c72017-09-07 11:13:33 -07003619 cm->subsampling_x != subsampling_x ||
3620 cm->subsampling_y != subsampling_y) {
3621 cm->subsampling_x = subsampling_x;
3622 cm->subsampling_y = subsampling_y;
Cheng Chen46f30c72017-09-07 11:13:33 -07003623 cm->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003624
3625 alloc_raw_frame_buffers(cpi);
3626 init_ref_frame_bufs(cm);
3627 alloc_util_frame_buffers(cpi);
3628
3629 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3630
3631 cpi->initial_width = cm->width;
3632 cpi->initial_height = cm->height;
3633 cpi->initial_mbs = cm->MBs;
3634 }
3635}
3636
3637// Returns 1 if the assigned width or height was <= 0.
3638static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3639 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003640 const int num_planes = av1_num_planes(cm);
Cheng Chen46f30c72017-09-07 11:13:33 -07003641 check_initial_width(cpi, cm->use_highbitdepth, cm->subsampling_x,
3642 cm->subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003643
3644 if (width <= 0 || height <= 0) return 1;
3645
3646 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003647 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003648
3649 if (cpi->initial_width && cpi->initial_height &&
3650 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3651 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003652 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003653 alloc_compressor_data(cpi);
3654 realloc_segmentation_maps(cpi);
3655 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003656 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003657 update_frame_size(cpi);
3658
3659 return 0;
3660}
3661
Fergus Simpsonbc189932017-05-16 17:02:39 -07003662static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003663 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003664 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003665 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003666 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003667
Fergus Simpsonbc189932017-05-16 17:02:39 -07003668 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003669 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003670 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003671 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003672 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
3673 cm->all_lossless = cm->coded_lossless && av1_superres_unscaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003674 }
3675
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003676 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003677 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003678 }
3679
3680 alloc_frame_mvs(cm, cm->new_fb_idx);
3681
3682 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07003683 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003684 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -08003685 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3686 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003687 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003688 "Failed to allocate frame buffer");
3689
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003690 const int frame_width = cm->superres_upscaled_width;
3691 const int frame_height = cm->superres_upscaled_height;
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003692 set_restoration_unit_size(frame_width, frame_height, cm->subsampling_x,
3693 cm->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003694 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003695 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003696
3697 av1_alloc_restoration_buffers(cm);
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003698 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003699 init_motion_estimation(cpi);
3700
3701 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3702 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3703 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3704
3705 ref_buf->idx = buf_idx;
3706
3707 if (buf_idx != INVALID_IDX) {
3708 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3709 ref_buf->buf = buf;
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003710 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3711 buf->y_crop_height, cm->width,
3712 cm->height);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003713 if (av1_is_scaled(&ref_buf->sf))
3714 aom_extend_frame_borders(buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715 } else {
3716 ref_buf->buf = NULL;
3717 }
3718 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003719
Hui Su5ebd8702018-01-08 18:09:20 -08003720 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003721 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003722
3723 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3724}
3725
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003726static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3727 // Choose an arbitrary random number
3728 static unsigned int seed = 56789;
3729 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003730 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3731 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003732
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07003733 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003734 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003735 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003736 case RESIZE_FIXED:
3737 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003738 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003739 else
Urvang Joshide71d142017-10-05 12:12:15 -07003740 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003741 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003742 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003743 default: assert(0);
3744 }
Urvang Joshide71d142017-10-05 12:12:15 -07003745 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003746}
3747
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003748static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
3749 // Choose an arbitrary random number
3750 static unsigned int seed = 34567;
3751 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003752 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3753 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07003754
3755 // Make sure that superres mode of the frame is consistent with the
3756 // sequence-level flag.
3757 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
3758 cpi->common.seq_params.enable_superres));
3759 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
3760 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003761
3762 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003763 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003764 case SUPERRES_FIXED:
3765 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003766 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003767 else
Urvang Joshide71d142017-10-05 12:12:15 -07003768 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003769 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003770 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003771 case SUPERRES_QTHRESH: {
3772 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3773 const RATE_FACTOR_LEVEL rf_level = gf_group->rf_level[gf_group->index];
3774 const double rate_factor_delta = rate_factor_deltas[rf_level];
Urvang Joshi2c92b072018-03-19 17:23:31 -07003775 const int qthresh = (rate_factor_delta <= 1.0)
3776 ? oxcf->superres_qthresh
3777 : oxcf->superres_kf_qthresh;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003778 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi2c92b072018-03-19 17:23:31 -07003779 int bottom_index, top_index;
3780 const int q = av1_rc_pick_q_and_bounds(
3781 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003782 if (q < qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07003783 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003784 } else {
Urvang Joshi28983f72017-10-25 14:41:06 -07003785 const uint8_t min_denom = SCALE_NUMERATOR + 1;
3786 const uint8_t denom_step = (MAXQ - qthresh + 1) >> 3;
Imdad Sardharwallaf6154fd2018-03-02 16:53:41 +00003787
3788 if (q == qthresh) {
3789 new_denom = min_denom;
3790 } else if (denom_step == 0) {
3791 new_denom = SCALE_NUMERATOR << 1;
3792 } else {
3793 const uint8_t additional_denom = (q - qthresh) / denom_step;
3794 new_denom =
3795 AOMMIN(min_denom + additional_denom, SCALE_NUMERATOR << 1);
3796 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003797 }
3798 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003799 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003800 default: assert(0);
3801 }
Urvang Joshide71d142017-10-05 12:12:15 -07003802 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003803}
3804
Urvang Joshide71d142017-10-05 12:12:15 -07003805static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
3806 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
3807}
3808
3809static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08003810 // Only need to check the width, as scaling is horizontal only.
3811 (void)oheight;
3812 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07003813}
3814
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003815static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003816 SUPERRES_MODE superres_mode, int owidth,
3817 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07003818 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003819 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07003820 }
3821
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003822 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07003823 int resize_denom =
3824 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
3825 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
3826
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003827 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003828 // Alter superres scale as needed to enforce conformity.
3829 rsz->superres_denom =
3830 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
3831 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3832 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003833 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003834 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003835 // Alter resize scale as needed to enforce conformity.
3836 resize_denom =
3837 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003838 rsz->resize_width = owidth;
3839 rsz->resize_height = oheight;
3840 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003841 resize_denom);
3842 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3843 if (resize_denom > SCALE_NUMERATOR) {
3844 --resize_denom;
3845 rsz->resize_width = owidth;
3846 rsz->resize_height = oheight;
3847 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
3848 resize_denom);
3849 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003850 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003851 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003852 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003853 do {
Urvang Joshide71d142017-10-05 12:12:15 -07003854 if (resize_denom > rsz->superres_denom)
3855 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003856 else
Urvang Joshide71d142017-10-05 12:12:15 -07003857 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003858 rsz->resize_width = owidth;
3859 rsz->resize_height = oheight;
3860 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003861 resize_denom);
3862 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
3863 (resize_denom > SCALE_NUMERATOR ||
3864 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08003865 } else { // We are allowed to alter neither resize scale nor superres
3866 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003867 return 0;
3868 }
Urvang Joshide71d142017-10-05 12:12:15 -07003869 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003870}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003871
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003872// Calculates resize and superres params for next frame
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003873size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
3874 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08003875 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshide71d142017-10-05 12:12:15 -07003876 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003877 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003878 if (cpi->resize_pending_width && cpi->resize_pending_height) {
3879 rsz.resize_width = cpi->resize_pending_width;
3880 rsz.resize_height = cpi->resize_pending_height;
3881 cpi->resize_pending_width = cpi->resize_pending_height = 0;
3882 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07003883 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003884 rsz.resize_width = cpi->oxcf.width;
3885 rsz.resize_height = cpi->oxcf.height;
3886 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003887 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003888 }
Urvang Joshide71d142017-10-05 12:12:15 -07003889 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003890 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
3891 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003892 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003893 return rsz;
3894}
3895
3896static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003897 int encode_width = rsz->resize_width;
3898 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003899
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003900 AV1_COMMON *cm = &cpi->common;
3901 cm->superres_upscaled_width = encode_width;
3902 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07003903 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003904 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
3905 rsz->superres_denom);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003906 set_frame_size(cpi, encode_width, encode_height);
3907}
3908
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003909static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003910 size_params_type rsz = av1_calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003911 setup_frame_size_from_params(cpi, &rsz);
3912}
3913
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003914static void superres_post_encode(AV1_COMP *cpi) {
3915 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003916 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003917
3918 if (av1_superres_unscaled(cm)) return;
3919
Urvang Joshid6b5d512018-03-20 13:34:38 -07003920 assert(cpi->oxcf.enable_superres);
3921 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07003922 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07003923
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003924 av1_superres_upscale(cm, NULL);
3925
3926 // If regular resizing is occurring the source will need to be downscaled to
3927 // match the upscaled superres resolution. Otherwise the original source is
3928 // used.
3929 if (av1_resize_unscaled(cm)) {
3930 cpi->source = cpi->unscaled_source;
3931 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
3932 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07003933 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
3934 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08003935 // Do downscale. cm->(width|height) has been updated by
3936 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003937 if (aom_realloc_frame_buffer(
3938 &cpi->scaled_source, cm->superres_upscaled_width,
3939 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -08003940 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3941 NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003942 aom_internal_error(
3943 &cm->error, AOM_CODEC_MEM_ERROR,
3944 "Failed to reallocate scaled source buffer for superres");
3945 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
3946 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003947 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003948 (int)cm->bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003949 cpi->source = &cpi->scaled_source;
3950 }
3951}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003952
3953static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003954 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003955 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
Urvang Joshi14072aa2018-03-21 17:43:36 -07003956
Urvang Joshic8b52d52018-03-23 13:16:51 -07003957 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
3958 cm->coded_lossless && cm->all_lossless));
3959
3960 const int no_loopfilter = cm->coded_lossless || cm->large_scale_tile;
3961 const int no_cdef =
Debargha Mukherjee98a311c2018-03-25 16:33:11 -07003962 !cm->seq_params.enable_cdef || cm->coded_lossless || cm->large_scale_tile;
3963 const int no_restoration = !cm->seq_params.enable_restoration ||
3964 cm->all_lossless || cm->large_scale_tile;
Urvang Joshi14072aa2018-03-21 17:43:36 -07003965
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003966 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07003967
3968 if (no_loopfilter) {
Cheng Chen179479f2017-08-04 10:56:39 -07003969 lf->filter_level[0] = 0;
3970 lf->filter_level[1] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003971 } else {
3972 struct aom_usec_timer timer;
3973
3974 aom_clear_system_state();
3975
3976 aom_usec_timer_start(&timer);
3977
3978 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
3979
3980 aom_usec_timer_mark(&timer);
3981 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
3982 }
3983
Debargha Mukherjee2382b142018-02-26 14:31:32 -08003984 if (lf->filter_level[0] || lf->filter_level[1]) {
Cheng Chen179479f2017-08-04 10:56:39 -07003985 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level[0],
3986 lf->filter_level[1], 0, 0);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003987 if (num_planes > 1) {
3988 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_u,
3989 lf->filter_level_u, 1, 0);
3990 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_v,
3991 lf->filter_level_v, 2, 0);
3992 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003993 }
Debargha Mukherjeee168a782017-08-31 12:30:10 -07003994
Yaowu Xu35ee2342017-11-08 11:50:46 -08003995 if (!no_restoration)
3996 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02003997
Yaowu Xu35ee2342017-11-08 11:50:46 -08003998 if (no_cdef) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003999 cm->cdef_bits = 0;
4000 cm->cdef_strengths[0] = 0;
4001 cm->nb_cdef_strengths = 1;
Yunqing Wangdad63d42017-12-08 12:45:07 -08004002 cm->cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004003 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004004 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004005 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004006 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004007
4008 // Apply the filter
4009 av1_cdef_frame(cm->frame_to_show, cm, xd);
4010 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004011
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004012 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004013
Yaowu Xu35ee2342017-11-08 11:50:46 -08004014 if (no_restoration) {
4015 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4016 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4017 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4018 } else {
4019 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 1);
4020 av1_pick_filter_restoration(cpi->source, cpi);
4021 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4022 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4023 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Rupert Swarbrick70c399e2017-12-12 09:47:49 +00004024 av1_loop_restoration_filter_frame(cm->frame_to_show, cm);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004025 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004026 }
Fergus Simpsonbc189932017-05-16 17:02:39 -07004027}
4028
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004029static int encode_without_recode_loop(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004030 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004031 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004032
Yaowu Xuf883b422016-08-30 14:01:10 -07004033 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004034
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004035 set_size_independent_vars(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004036
Fergus Simpsonbc189932017-05-16 17:02:39 -07004037 setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004038
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004039 assert(cm->width == cpi->scaled_source.y_crop_width);
4040 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004041
Yaowu Xuc27fc142016-08-22 16:08:15 -07004042 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4043
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004044 cpi->source =
4045 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4046 if (cpi->unscaled_last_source != NULL)
4047 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4048 &cpi->scaled_last_source);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004049 cpi->source->buf_8bit_valid = 0;
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004050 if (frame_is_intra_only(cm) == 0) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004051 scale_references(cpi);
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004052 }
4053
Yaowu Xuf883b422016-08-30 14:01:10 -07004054 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004055 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004056 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004057
Yaowu Xuc27fc142016-08-22 16:08:15 -07004058 // Variance adaptive and in frame q adjustment experiments are mutually
4059 // exclusive.
4060 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004061 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004062 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004063 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004064 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004065 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004066 }
4067 apply_active_map(cpi);
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004068 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004069 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004070 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004071 }
David Barkercab37552018-03-21 11:56:24 +00004072 } else {
4073 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004074 }
David Barkercab37552018-03-21 11:56:24 +00004075 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004076
4077 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004078 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004079
4080 // Update some stats from cyclic refresh, and check if we should not update
4081 // golden reference, for 1 pass CBR.
4082 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004083 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4084 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004085
4086 // Update the skip mb flag probabilities based on the distribution
4087 // seen in the last encoder iteration.
4088 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004089 aom_clear_system_state();
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004090 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004091}
4092
Tom Finegane4099e32018-01-23 12:01:51 -08004093static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004094 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004095 RATE_CONTROL *const rc = &cpi->rc;
4096 int bottom_index, top_index;
4097 int loop_count = 0;
4098 int loop_at_this_size = 0;
4099 int loop = 0;
4100 int overshoot_seen = 0;
4101 int undershoot_seen = 0;
4102 int frame_over_shoot_limit;
4103 int frame_under_shoot_limit;
4104 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004105
4106 set_size_independent_vars(cpi);
4107
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004108 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004109
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004110 aom_clear_system_state();
4111 setup_frame_size(cpi);
4112 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4113
Yaowu Xuc27fc142016-08-22 16:08:15 -07004114 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004115 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004116
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004117 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004118 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4119 set_mv_search_params(cpi);
4120
4121 // Reset the loop state for new frame size.
4122 overshoot_seen = 0;
4123 undershoot_seen = 0;
4124
Yaowu Xuc27fc142016-08-22 16:08:15 -07004125 q_low = bottom_index;
4126 q_high = top_index;
4127
4128 loop_at_this_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004129
Urvang Joshi2a74cf22017-12-18 17:21:41 -08004130 // Decide frame size bounds first time through.
Yaowu Xuf883b422016-08-30 14:01:10 -07004131 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4132 &frame_under_shoot_limit,
4133 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004134 }
4135
Urvang Joshif1fa6862018-01-08 16:39:33 -08004136 // if frame was scaled calculate global_motion_search again if already
4137 // done
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004138 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4139 if (cpi->source->y_crop_width != cm->width ||
4140 cpi->source->y_crop_height != cm->height)
4141 cpi->global_motion_search_done = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004142 cpi->source =
4143 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004144 if (cpi->unscaled_last_source != NULL)
4145 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4146 &cpi->scaled_last_source);
4147
Yaowu Xuc27fc142016-08-22 16:08:15 -07004148 if (frame_is_intra_only(cm) == 0) {
4149 if (loop_count > 0) {
4150 release_scaled_references(cpi);
4151 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004152 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004153 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004154 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004155
4156 if (loop_count == 0) setup_frame(cpi);
4157
Yaowu Xuc27fc142016-08-22 16:08:15 -07004158 // Base q-index may have changed, so we need to assign proper default coef
4159 // probs before every iteration.
David Barkercc615a82018-03-19 14:38:51 +00004160 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
4161 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004162 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004163 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004164 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004165
Yaowu Xuc27fc142016-08-22 16:08:15 -07004166 // Variance adaptive and in frame q adjustment experiments are mutually
4167 // exclusive.
4168 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004169 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004170 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004171 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004172 }
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004173 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004174 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004175 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004176 }
David Barkercab37552018-03-21 11:56:24 +00004177 } else {
4178 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004179 }
David Barkercab37552018-03-21 11:56:24 +00004180 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004181
4182 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004183 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004184 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004185
4186 // Update the skip mb flag probabilities based on the distribution
4187 // seen in the last encoder iteration.
4188 // update_base_skip_probs(cpi);
4189
Yaowu Xuf883b422016-08-30 14:01:10 -07004190 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004191
4192 // Dummy pack of the bitstream using up to date stats to get an
4193 // accurate estimate of output frame size to determine if we need
4194 // to recode.
4195 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004196 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004197
4198 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4199 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004200
4201 rc->projected_frame_size = (int)(*size) << 3;
4202 restore_coding_context(cpi);
4203
4204 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4205 }
4206
Yaowu Xuf883b422016-08-30 14:01:10 -07004207 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004208 loop = 0;
4209 } else {
4210 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4211 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4212 int last_q = q;
4213 int64_t kf_err;
4214
4215 int64_t high_err_target = cpi->ambient_err;
4216 int64_t low_err_target = cpi->ambient_err >> 1;
4217
Yaowu Xuc27fc142016-08-22 16:08:15 -07004218 if (cm->use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004219 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004220 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004221 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004222 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004223 // Prevent possible divide by zero error below for perfect KF
4224 kf_err += !kf_err;
4225
4226 // The key frame is not good enough or we can afford
4227 // to make it better without undue risk of popping.
4228 if ((kf_err > high_err_target &&
4229 rc->projected_frame_size <= frame_over_shoot_limit) ||
4230 (kf_err > low_err_target &&
4231 rc->projected_frame_size <= frame_under_shoot_limit)) {
4232 // Lower q_high
4233 q_high = q > q_low ? q - 1 : q_low;
4234
4235 // Adjust Q
4236 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004237 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004238 } else if (kf_err < low_err_target &&
4239 rc->projected_frame_size >= frame_under_shoot_limit) {
4240 // The key frame is much better than the previous frame
4241 // Raise q_low
4242 q_low = q < q_high ? q + 1 : q_high;
4243
4244 // Adjust Q
4245 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004246 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004247 }
4248
4249 // Clamp Q to upper and lower limits:
4250 q = clamp(q, q_low, q_high);
4251
4252 loop = q != last_q;
4253 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4254 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004255 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004256 // Is the projected frame size out of range and are we allowed
4257 // to attempt to recode.
4258 int last_q = q;
4259 int retries = 0;
4260
Yaowu Xuc27fc142016-08-22 16:08:15 -07004261 // Frame size out of permitted range:
4262 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004263 // Frame is too large
4264 if (rc->projected_frame_size > rc->this_frame_target) {
4265 // Special case if the projected size is > the max allowed.
4266 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4267 q_high = rc->worst_quality;
4268
4269 // Raise Qlow as to at least the current value
4270 q_low = q < q_high ? q + 1 : q_high;
4271
4272 if (undershoot_seen || loop_at_this_size > 1) {
4273 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004274 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004275
4276 q = (q_high + q_low + 1) / 2;
4277 } else {
4278 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004279 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004280
Yaowu Xuf883b422016-08-30 14:01:10 -07004281 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004282 AOMMAX(q_high, top_index), cm->width,
4283 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004284
4285 while (q < q_low && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004286 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004287 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004288 AOMMAX(q_high, top_index), cm->width,
4289 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004290 retries++;
4291 }
4292 }
4293
4294 overshoot_seen = 1;
4295 } else {
4296 // Frame is too small
4297 q_high = q > q_low ? q - 1 : q_low;
4298
4299 if (overshoot_seen || loop_at_this_size > 1) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004300 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004301 q = (q_high + q_low) / 2;
4302 } else {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004303 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004304 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004305 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004306 // Special case reset for qlow for constrained quality.
4307 // This should only trigger where there is very substantial
4308 // undershoot on a frame and the auto cq level is above
4309 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004310 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004311 q_low = q;
4312 }
4313
4314 while (q > q_high && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004315 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004316 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004317 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004318 retries++;
4319 }
4320 }
4321
4322 undershoot_seen = 1;
4323 }
4324
4325 // Clamp Q to upper and lower limits:
4326 q = clamp(q, q_low, q_high);
4327
4328 loop = (q != last_q);
4329 } else {
4330 loop = 0;
4331 }
4332 }
4333
4334 // Special case for overlay frame.
4335 if (rc->is_src_frame_alt_ref &&
4336 rc->projected_frame_size < rc->max_frame_bandwidth)
4337 loop = 0;
4338
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004339 if (recode_loop_test_global_motion(cpi)) {
4340 loop = 1;
4341 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004342
Yaowu Xuc27fc142016-08-22 16:08:15 -07004343 if (loop) {
4344 ++loop_count;
4345 ++loop_at_this_size;
4346
4347#if CONFIG_INTERNAL_STATS
4348 ++cpi->tot_recode_hits;
4349#endif
4350 }
4351 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004352
4353 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004354}
4355
Yaowu Xuf883b422016-08-30 14:01:10 -07004356static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004357 const int *const map = cpi->common.ref_frame_map;
4358
Zoe Liu368bf162017-11-03 20:10:19 -07004359 // No.1 Priority: LAST_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004360 const int last2_is_last = map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[0]];
4361 const int last3_is_last = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[0]];
4362 const int gld_is_last =
4363 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4364 const int bwd_is_last =
4365 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4366 const int alt2_is_last =
4367 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4368 const int alt_is_last =
4369 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004370
Zoe Liu368bf162017-11-03 20:10:19 -07004371 // No.2 Priority: ALTREF_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004372 const int last2_is_alt =
4373 map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4374 const int last3_is_alt =
4375 map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4376 const int gld_is_alt = map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] ==
4377 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4378 const int bwd_is_alt = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4379 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4380 const int alt2_is_alt = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4381 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004382
Zoe Liu368bf162017-11-03 20:10:19 -07004383 // No.3 Priority: LAST2_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004384 const int last3_is_last2 = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[1]];
4385 const int gld_is_last2 =
4386 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4387 const int bwd_is_last2 =
4388 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4389 const int alt2_is_last2 =
4390 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004391
Zoe Liu368bf162017-11-03 20:10:19 -07004392 // No.4 Priority: LAST3_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004393 const int gld_is_last3 =
4394 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4395 const int bwd_is_last3 =
4396 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4397 const int alt2_is_last3 =
4398 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
Zoe Liu368bf162017-11-03 20:10:19 -07004399
4400 // No.5 Priority: GOLDEN_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004401 const int bwd_is_gld = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4402 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
4403 const int alt2_is_gld = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4404 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
Zoe Liu368bf162017-11-03 20:10:19 -07004405
4406 // No.6 Priority: BWDREF_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004407 const int alt2_is_bwd = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4408 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]];
Zoe Liu368bf162017-11-03 20:10:19 -07004409
4410 // No.7 Priority: ALTREF2_FRAME
4411
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004412 // After av1_apply_encoding_flags() is called, cpi->ref_frame_flags might be
4413 // adjusted according to external encoder flags.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08004414 int flags = cpi->ext_ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004415
Yaowu Xuf883b422016-08-30 14:01:10 -07004416 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004417
Yaowu Xuf883b422016-08-30 14:01:10 -07004418 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004419
Yaowu Xuf883b422016-08-30 14:01:10 -07004420 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004421
Zoe Liu368bf162017-11-03 20:10:19 -07004422 if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004423
Zoe Liu368bf162017-11-03 20:10:19 -07004424 if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
4425 flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004426
Zoe Liu368bf162017-11-03 20:10:19 -07004427 if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 ||
4428 bwd_is_gld) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004429 (flags & AOM_BWD_FLAG))
4430 flags &= ~AOM_BWD_FLAG;
Zoe Liue9b15e22017-07-19 15:53:01 -07004431
Zoe Liu368bf162017-11-03 20:10:19 -07004432 if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
4433 alt2_is_gld || alt2_is_bwd) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004434 (flags & AOM_ALT2_FLAG))
4435 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004436
4437 return flags;
4438}
4439
Yaowu Xuf883b422016-08-30 14:01:10 -07004440static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004441 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07004442 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07004443 // Note: The overrides are valid only for the next frame passed
4444 // to encode_frame_to_data_rate() function
sarahparker9806fed2018-03-30 17:43:44 -07004445 if (cpi->ext_use_s_frame) cpi->common.frame_type = S_FRAME;
Debargha Mukherjee52fb0472018-03-29 15:48:11 -07004446
Yaowu Xuc27fc142016-08-22 16:08:15 -07004447 if (cpi->ext_refresh_frame_context_pending) {
4448 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4449 cpi->ext_refresh_frame_context_pending = 0;
4450 }
4451 if (cpi->ext_refresh_frame_flags_pending) {
4452 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4453 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4454 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004455 cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
4456 cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004457 cpi->ext_refresh_frame_flags_pending = 0;
4458 }
sarahparker21dbca42018-03-30 17:43:44 -07004459 cpi->common.allow_ref_frame_mvs = cpi->ext_use_ref_frame_mvs;
sarahparker27d686a2018-03-30 17:43:44 -07004460 cpi->common.error_resilient_mode = cpi->ext_use_error_resilient;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004461}
4462
Yaowu Xuf883b422016-08-30 14:01:10 -07004463static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07004464 InterpFilter ifilter;
Zoe Liu27deb382018-03-27 15:13:56 -07004465 int ref_total[REF_FRAMES] = { 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -07004466 MV_REFERENCE_FRAME ref;
4467 int mask = 0;
4468 int arf_idx = ALTREF_FRAME;
4469
Zoe Liue9b15e22017-07-19 15:53:01 -07004470 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
4471 cpi->refresh_alt2_ref_frame)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004472 return mask;
4473
Yaowu Xuc27fc142016-08-22 16:08:15 -07004474 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4475 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4476 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004477
4478 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
4479 if ((ref_total[LAST_FRAME] &&
4480 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004481 (ref_total[LAST2_FRAME] == 0 ||
4482 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
4483 ref_total[LAST2_FRAME]) &&
4484 (ref_total[LAST3_FRAME] == 0 ||
4485 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
4486 ref_total[LAST3_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004487 (ref_total[GOLDEN_FRAME] == 0 ||
4488 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4489 ref_total[GOLDEN_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004490 (ref_total[BWDREF_FRAME] == 0 ||
4491 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
4492 ref_total[BWDREF_FRAME]) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004493 (ref_total[ALTREF2_FRAME] == 0 ||
4494 cpi->interp_filter_selected[ALTREF2_FRAME][ifilter] * 50 <
4495 ref_total[ALTREF2_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004496 (ref_total[ALTREF_FRAME] == 0 ||
4497 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
4498 ref_total[ALTREF_FRAME]))
4499 mask |= 1 << ifilter;
4500 }
4501 return mask;
4502}
4503
4504#define DUMP_RECON_FRAMES 0
4505
4506#if DUMP_RECON_FRAMES == 1
4507// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004508static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4509 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004510 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004511
Zoe Liub4f31032017-11-03 23:48:35 -07004512 if (recon_buf == NULL) {
4513 printf("Frame %d is not ready.\n", cm->current_video_frame);
4514 return;
4515 }
4516
Zoe Liu27deb382018-03-27 15:13:56 -07004517 static const int flag_list[REF_FRAMES] = { 0,
4518 AOM_LAST_FLAG,
4519 AOM_LAST2_FLAG,
4520 AOM_LAST3_FLAG,
4521 AOM_GOLD_FLAG,
4522 AOM_BWD_FLAG,
4523 AOM_ALT2_FLAG,
4524 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004525 printf(
4526 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4527 "show_existing_frame=%d) "
4528 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
4529 cm->current_video_frame, cm->frame_offset, cm->show_frame,
4530 cm->show_existing_frame);
4531 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4532 const int buf_idx = cm->frame_refs[ref_frame - LAST_FRAME].idx;
4533 const int ref_offset =
4534 (buf_idx >= 0)
4535 ? (int)cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset
4536 : -1;
Zoe Liuf452fdf2017-11-02 23:08:12 -07004537 printf(
4538 " %d(%c-%d-%4.2f)", ref_offset,
4539 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N',
4540 (buf_idx >= 0) ? (int)cpi->frame_rf_level[buf_idx] : -1,
4541 (buf_idx >= 0) ? rate_factor_deltas[cpi->frame_rf_level[buf_idx]] : -1);
Zoe Liub4f31032017-11-03 23:48:35 -07004542 }
4543 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004544
4545 if (!cm->show_frame) {
4546 printf("Frame %d is a no show frame, so no image dump.\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004547 cm->current_video_frame);
4548 return;
4549 }
4550
Zoe Liub4f31032017-11-03 23:48:35 -07004551 int h;
4552 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4553 FILE *f_recon = NULL;
4554
Yaowu Xuc27fc142016-08-22 16:08:15 -07004555 if (cm->current_video_frame == 0) {
4556 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4557 printf("Unable to open file %s to write.\n", file_name);
4558 return;
4559 }
4560 } else {
4561 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4562 printf("Unable to open file %s to append.\n", file_name);
4563 return;
4564 }
4565 }
4566 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004567 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4568 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
4569 "refresh_alt_ref_frame=%d, rf_level=%d, "
4570 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004571 cm->current_video_frame, cpi->twopass.gf_group.index,
4572 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liuf40a9572017-10-13 12:37:19 -07004573 cm->frame_offset, cm->show_frame, cm->show_existing_frame,
4574 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
4575 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4576 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004577#if 0
4578 int ref_frame;
4579 printf("get_ref_frame_map_idx: [");
4580 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
4581 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
4582 printf(" ]\n");
4583 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
4584 printf("cm->ref_frame_map = [");
4585 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4586 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
4587 }
4588 printf(" ]\n");
4589#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004590
4591 // --- Y ---
4592 for (h = 0; h < cm->height; ++h) {
4593 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4594 f_recon);
4595 }
4596 // --- U ---
4597 for (h = 0; h < (cm->height >> 1); ++h) {
4598 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4599 f_recon);
4600 }
4601 // --- V ---
4602 for (h = 0; h < (cm->height >> 1); ++h) {
4603 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4604 f_recon);
4605 }
4606
4607 fclose(f_recon);
4608}
4609#endif // DUMP_RECON_FRAMES
4610
Tom Finegane4099e32018-01-23 12:01:51 -08004611static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size, uint8_t *dest,
4612 int skip_adapt,
4613 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004614 AV1_COMMON *const cm = &cpi->common;
4615 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004616 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01004617
Yaowu Xuc27fc142016-08-22 16:08:15 -07004618 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004619 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004620
Fangwen Fu8d164de2016-12-14 13:40:54 -08004621 // frame type has been decided outside of this function call
Zoe Liud78ce2d2018-02-24 05:50:57 -08004622 cm->cur_frame->intra_only = frame_is_intra_only(cm);
Zoe Liu2723a9d2018-02-22 20:17:00 -08004623 cm->cur_frame->frame_type = cm->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07004624
4625 // S_FRAMEs are always error resilient
sarahparker27d686a2018-03-30 17:43:44 -07004626 cm->error_resilient_mode |= frame_is_sframe(cm);
sarahparker21dbca42018-03-30 17:43:44 -07004627 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07004628 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07004629 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004630
Jingning Hand8a15a62017-10-30 10:53:42 -07004631 // Reset the frame packet stamp index.
4632 if (cm->frame_type == KEY_FRAME) cm->current_video_frame = 0;
4633
Yaowu Xuc27fc142016-08-22 16:08:15 -07004634 // NOTE:
4635 // (1) Move the setup of the ref_frame_flags upfront as it would be
4636 // determined by the current frame properties;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004637 // (2) The setup of the ref_frame_flags applies to both
4638 // show_existing_frame's
Yaowu Xuc27fc142016-08-22 16:08:15 -07004639 // and the other cases.
4640 if (cm->current_video_frame > 0)
4641 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4642
4643 if (cm->show_existing_frame) {
4644 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
4645 // BWDREF_FRAME in the reference frame buffer.
4646 cm->frame_type = INTER_FRAME;
4647 cm->show_frame = 1;
4648 cpi->frame_flags = *frame_flags;
4649
4650 // In the case of show_existing frame, we will not send fresh flag
4651 // to decoder. Any change in the reference frame buffer can be done by
4652 // switching the virtual indices.
4653
4654 cpi->refresh_last_frame = 0;
4655 cpi->refresh_golden_frame = 0;
4656 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07004657 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004658 cpi->refresh_alt_ref_frame = 0;
4659
4660 cpi->rc.is_bwd_ref_frame = 0;
4661 cpi->rc.is_last_bipred_frame = 0;
4662 cpi->rc.is_bipred_frame = 0;
4663
Jingning Han8f661602017-08-19 08:16:50 -07004664 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07004665
Yaowu Xuc27fc142016-08-22 16:08:15 -07004666 // Build the bitstream
Tom Finegane4099e32018-01-23 12:01:51 -08004667 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4668 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004669
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004670 cpi->seq_params_locked = 1;
4671
Yaowu Xuc27fc142016-08-22 16:08:15 -07004672 // Set up frame to show to get ready for stats collection.
4673 cm->frame_to_show = get_frame_new_buffer(cm);
4674
Zoe Liub4f31032017-11-03 23:48:35 -07004675 // Update current frame offset.
4676 cm->frame_offset =
4677 cm->buffer_pool->frame_bufs[cm->new_fb_idx].cur_frame_offset;
Zoe Liub4f31032017-11-03 23:48:35 -07004678
Yaowu Xuc27fc142016-08-22 16:08:15 -07004679#if DUMP_RECON_FRAMES == 1
4680 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4681 dump_filtered_recon_frames(cpi);
4682#endif // DUMP_RECON_FRAMES
4683
4684 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07004685 // NOTE:
4686 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
4687 // update has been done previously when handling the LAST_BIPRED_FRAME
4688 // right before BWDREF_FRAME (in the display order);
4689 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
Urvang Joshif1fa6862018-01-08 16:39:33 -08004690 // update will be done when the following is called, which will
4691 // exchange
Zoe Liue9b15e22017-07-19 15:53:01 -07004692 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
Urvang Joshif1fa6862018-01-08 16:39:33 -08004693 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2,
4694 // and
Zoe Liue9b15e22017-07-19 15:53:01 -07004695 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Cheng Chen46f30c72017-09-07 11:13:33 -07004696 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004697
4698 // Update frame flags
4699 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4700 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4701 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4702
4703 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4704
4705 // Update the frame type
4706 cm->last_frame_type = cm->frame_type;
4707
Yaowu Xuc27fc142016-08-22 16:08:15 -07004708 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4709 // to do post-encoding update accordingly.
4710 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004711 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004712 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004713 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004714
Yaowu Xuc27fc142016-08-22 16:08:15 -07004715 ++cm->current_video_frame;
4716
Tom Finegane4099e32018-01-23 12:01:51 -08004717 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004718 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004719
4720 // Set default state for segment based loop filter update flags.
4721 cm->lf.mode_ref_delta_update = 0;
4722
4723 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4724 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4725
4726 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05004727 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004728 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004729 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004730
4731 // If segmentation is enabled force a map update for key frames.
4732 if (seg->enabled) {
4733 seg->update_map = 1;
4734 seg->update_data = 1;
4735 }
4736
4737 // The alternate reference frame cannot be active for a key frame.
4738 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004739 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00004740 if (cpi->oxcf.mtu == 0) {
4741 cm->num_tg = cpi->oxcf.num_tile_groups;
4742 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08004743 // Use a default value for the purposes of weighting costs in probability
4744 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00004745 cm->num_tg = DEFAULT_MAX_NUM_TG;
4746 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004747
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004748 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
4749 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08004750 if (cm->large_scale_tile) cm->seq_params.frame_id_numbers_present_flag = 0;
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08004751
Yaowu Xuc27fc142016-08-22 16:08:15 -07004752 // For 1 pass CBR, check if we are dropping this frame.
4753 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004754 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004755 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004756 if (av1_rc_drop_frame(cpi)) {
4757 av1_rc_postencode_update_drop_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004758 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004759 }
4760 }
4761
Yaowu Xuf883b422016-08-30 14:01:10 -07004762 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004763
4764#if CONFIG_INTERNAL_STATS
4765 memset(cpi->mode_chosen_counts, 0,
4766 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4767#endif
4768
David Barker5e70a112017-10-03 14:28:17 +01004769 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004770 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08004771 * wraparound) */
Sebastien Alaiwand418f682017-10-19 15:06:52 +02004772 const int frame_id_length = FRAME_ID_LENGTH;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004773 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00004774 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08004775 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07004776 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
4777 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
4778 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004779 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004780 lsb = cpi->source->y_buffer[0] & 0xff;
4781 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004782 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004783 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05004784
4785 // S_frame is meant for stitching different streams of different
4786 // resolutions together, so current_frame_id must be the
4787 // same across different streams of the same content current_frame_id
4788 // should be the same and not random. 0x37 is a chosen number as start
4789 // point
4790 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004791 } else {
4792 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004793 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
4794 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004795 }
4796 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004797
Hui Su483a8452018-02-26 12:28:48 -08004798 switch (cpi->oxcf.cdf_update_mode) {
4799 case 0: // No CDF update for any frames(4~6% compression loss).
4800 cm->disable_cdf_update = 1;
4801 break;
4802 case 1: // Enable CDF update for all frames.
4803 cm->disable_cdf_update = 0;
4804 break;
4805 case 2:
4806 // Strategically determine at which frames to do CDF update.
4807 // Currently only enable CDF update for all-intra and no-show frames(1.5%
4808 // compression loss).
4809 // TODO(huisu@google.com): design schemes for various trade-offs between
4810 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08004811 cm->disable_cdf_update =
4812 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08004813 break;
4814 }
Debargha Mukherjeefe516e52018-04-10 07:10:04 -07004815 cm->disable_cdf_update |= cm->seq_params.reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08004816
Yaowu Xuc27fc142016-08-22 16:08:15 -07004817 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004818 if (encode_without_recode_loop(cpi) != AOM_CODEC_OK) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819 } else {
Tom Finegane4099e32018-01-23 12:01:51 -08004820 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
4821 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004822 }
4823
Yi Luo10e23002017-07-31 11:54:43 -07004824 cm->last_tile_cols = cm->tile_cols;
4825 cm->last_tile_rows = cm->tile_rows;
4826
Yaowu Xuc27fc142016-08-22 16:08:15 -07004827#ifdef OUTPUT_YUV_SKINMAP
4828 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004829 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004830 }
4831#endif // OUTPUT_YUV_SKINMAP
4832
4833 // Special case code to reduce pulsing when key frames are forced at a
4834 // fixed interval. Note the reconstruction error if it is the frame before
4835 // the force key frame
4836 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004837 if (cm->use_highbitdepth) {
4838 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07004839 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004840 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004841 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004842 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004843 }
4844
Tarek AMARAc9813852018-03-05 18:40:18 -05004845 // If the encoder forced a KEY_FRAME decision or if frame is an S_FRAME
4846 if (cm->frame_type == KEY_FRAME || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004847 cpi->refresh_last_frame = 1;
4848 }
4849
4850 cm->frame_to_show = get_frame_new_buffer(cm);
Andrey Norkin9e694632017-12-21 18:50:57 -08004851 cm->frame_to_show->color_primaries = cm->color_primaries;
4852 cm->frame_to_show->transfer_characteristics = cm->transfer_characteristics;
4853 cm->frame_to_show->matrix_coefficients = cm->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08004854 cm->frame_to_show->monochrome = cm->seq_params.monochrome;
anorkin76fb1262017-03-22 15:12:12 -07004855 cm->frame_to_show->chroma_sample_position = cm->chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004856 cm->frame_to_show->color_range = cm->color_range;
4857 cm->frame_to_show->render_width = cm->render_width;
4858 cm->frame_to_show->render_height = cm->render_height;
4859
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02004860 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
4861 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004862
4863 // Pick the loop filter level for the frame.
Hui Su06463e42018-02-23 22:17:36 -08004864 if (!(cm->allow_intrabc && NO_FILTER_FOR_IBC)) {
David Barker218556e2018-02-14 14:23:12 +00004865 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08004866 } else {
Hui Su06463e42018-02-23 22:17:36 -08004867 cm->lf.filter_level[0] = 0;
4868 cm->lf.filter_level[1] = 0;
Hui Su06463e42018-02-23 22:17:36 -08004869 cm->cdef_bits = 0;
4870 cm->cdef_strengths[0] = 0;
4871 cm->nb_cdef_strengths = 1;
4872 cm->cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08004873 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4874 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4875 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08004876 }
David Barker218556e2018-02-14 14:23:12 +00004877
4878 // TODO(debargha): Fix mv search range on encoder side
4879 // aom_extend_frame_inner_borders(cm->frame_to_show, av1_num_planes(cm));
4880 aom_extend_frame_borders(cm->frame_to_show, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004881
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07004882#ifdef OUTPUT_YUV_REC
4883 aom_write_one_yuv_frame(cm, cm->frame_to_show);
4884#endif
4885
Yaowu Xuc27fc142016-08-22 16:08:15 -07004886 // Build the bitstream
Tom Finegane4099e32018-01-23 12:01:51 -08004887 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4888 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004889
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004890 cpi->seq_params_locked = 1;
4891
Hui Sudc54be62018-03-14 19:14:28 -07004892 if (skip_adapt) return AOM_CODEC_OK;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004893
David Barker5e70a112017-10-03 14:28:17 +01004894 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004895 int i;
4896 /* Update reference frame id values based on the value of refresh_mask */
4897 for (i = 0; i < REF_FRAMES; i++) {
4898 if ((cm->refresh_mask >> i) & 1) {
4899 cm->ref_frame_id[i] = cm->current_frame_id;
4900 }
4901 }
4902 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004903
Yaowu Xuc27fc142016-08-22 16:08:15 -07004904#if DUMP_RECON_FRAMES == 1
4905 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07004906 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004907#endif // DUMP_RECON_FRAMES
4908
Soo-Chul Han934af352017-10-15 15:21:51 -04004909 if (cm->seg.enabled) {
4910 if (cm->seg.update_map) {
4911 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07004912 } else if (cm->last_frame_seg_map) {
Soo-Chul Han934af352017-10-15 15:21:51 -04004913 memcpy(cm->current_frame_seg_map, cm->last_frame_seg_map,
4914 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
4915 }
4916 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004917
4918 if (frame_is_intra_only(cm) == 0) {
4919 release_scaled_references(cpi);
4920 }
4921
Cheng Chen46f30c72017-09-07 11:13:33 -07004922 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004923
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08004924#if CONFIG_ENTROPY_STATS
4925 av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
4926#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004927
Hui Sudc54be62018-03-14 19:14:28 -07004928 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
4929 *cm->fc = cpi->tile_data[cm->largest_tile_id].tctx;
4930 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004931 }
4932
4933 if (cpi->refresh_golden_frame == 1)
4934 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
4935 else
4936 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4937
4938 if (cpi->refresh_alt_ref_frame == 1)
4939 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
4940 else
4941 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4942
Yaowu Xuc27fc142016-08-22 16:08:15 -07004943 if (cpi->refresh_bwd_ref_frame == 1)
4944 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
4945 else
4946 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004947
Yaowu Xuc27fc142016-08-22 16:08:15 -07004948 cm->last_frame_type = cm->frame_type;
4949
Yaowu Xuf883b422016-08-30 14:01:10 -07004950 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004951
Yaowu Xuc27fc142016-08-22 16:08:15 -07004952 if (cm->frame_type == KEY_FRAME) {
4953 // Tell the caller that the frame was coded as a key frame
4954 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
4955 } else {
4956 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4957 }
4958
4959 // Clear the one shot update flags for segmentation map and mode/ref loop
4960 // filter deltas.
4961 cm->seg.update_map = 0;
4962 cm->seg.update_data = 0;
4963 cm->lf.mode_ref_delta_update = 0;
4964
Yaowu Xuc27fc142016-08-22 16:08:15 -07004965 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08004966 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
4967 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02004968 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07004969 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004970 // Don't increment frame counters if this was an altref buffer
4971 // update not a real frame
4972 ++cm->current_video_frame;
4973 }
4974
Yaowu Xuc27fc142016-08-22 16:08:15 -07004975 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07004976 if (cm->is_reference_frame) {
Fergus Simpson2b4ea112017-06-19 11:33:59 -07004977 // keep track of the last coded dimensions
4978 cm->last_width = cm->width;
4979 cm->last_height = cm->height;
4980
4981 // reset to normal state now that we are done.
4982 cm->last_show_frame = cm->show_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07004983 }
Yi Luo10e23002017-07-31 11:54:43 -07004984
Tom Finegane4099e32018-01-23 12:01:51 -08004985 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004986}
4987
Tom Finegane4099e32018-01-23 12:01:51 -08004988static int Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
4989 int skip_adapt, unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004990 if (cpi->oxcf.rc_mode == AOM_CBR) {
4991 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004992 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004993 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004994 }
Debargha Mukherjeeff48c092018-04-04 23:53:40 -07004995 if (encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags) !=
4996 AOM_CODEC_OK) {
4997 return AOM_CODEC_ERROR;
4998 }
4999 check_show_existing_frame(cpi);
5000 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005001}
5002
Tom Finegane4099e32018-01-23 12:01:51 -08005003static int Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5004 unsigned int *frame_flags) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -08005005#if CONFIG_MISMATCH_DEBUG
5006 mismatch_move_frame_idx_w();
5007#endif
Angie Chiang4d55d762017-12-13 16:18:37 -08005008#if TXCOEFF_COST_TIMER
5009 AV1_COMMON *cm = &cpi->common;
5010 cm->txcoeff_cost_timer = 0;
5011 cm->txcoeff_cost_count = 0;
5012#endif
Tom Finegane4099e32018-01-23 12:01:51 -08005013
5014 if (encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags) !=
5015 AOM_CODEC_OK) {
5016 return AOM_CODEC_ERROR;
5017 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005018
Angie Chiang4d55d762017-12-13 16:18:37 -08005019#if TXCOEFF_COST_TIMER
5020 cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer;
5021 fprintf(stderr,
5022 "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld "
5023 "in us\n",
5024 cm->txcoeff_cost_count, cm->txcoeff_cost_timer,
5025 cm->cum_txcoeff_cost_timer);
5026#endif
5027
Urvang Joshif1fa6862018-01-08 16:39:33 -08005028 // Do not do post-encoding update for those frames that do not have a spot
5029 // in
5030 // a gf group, but note that an OVERLAY frame always has a spot in a gf
5031 // group,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005032 // even when show_existing_frame is used.
5033 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005034 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005035 }
5036 check_show_existing_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005037 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005038}
5039
James Zern3e2613b2017-03-30 23:14:40 -07005040int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005041 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5042 int64_t end_time) {
5043 AV1_COMMON *const cm = &cpi->common;
5044 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005045 int res = 0;
5046 const int subsampling_x = sd->subsampling_x;
5047 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005048 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005049
Yaowu Xuc27fc142016-08-22 16:08:15 -07005050 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005051
Yaowu Xuf883b422016-08-30 14:01:10 -07005052 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005053
Yaowu Xuf883b422016-08-30 14:01:10 -07005054 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005055 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005056 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005057 aom_usec_timer_mark(&timer);
5058 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005059
Yaowu Xuc1fdb4b2018-03-16 08:44:13 -07005060 if ((cm->profile == PROFILE_0) && !cm->seq_params.monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005061 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005062 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005063 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005064 res = -1;
5065 }
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005066 if ((cm->profile == PROFILE_1) &&
5067 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005068 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005069 "Profile 1 requires 4:4:4 color format");
5070 res = -1;
5071 }
5072 if ((cm->profile == PROFILE_2) && (cm->bit_depth <= AOM_BITS_10) &&
5073 !(subsampling_x == 1 && subsampling_y == 0)) {
5074 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5075 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005076 res = -1;
5077 }
5078
5079 return res;
5080}
5081
Yaowu Xuf883b422016-08-30 14:01:10 -07005082static int frame_is_reference(const AV1_COMP *cpi) {
5083 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005084
5085 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005086 cpi->refresh_golden_frame || cpi->refresh_bwd_ref_frame ||
5087 cpi->refresh_alt2_ref_frame || cpi->refresh_alt_ref_frame ||
5088 !cm->error_resilient_mode || cm->lf.mode_ref_delta_update ||
5089 cm->seg.update_map || cm->seg.update_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005090}
5091
Yaowu Xuf883b422016-08-30 14:01:10 -07005092static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005093 const struct lookahead_entry *source) {
5094 int64_t this_duration;
5095 int step = 0;
5096
5097 if (source->ts_start == cpi->first_time_stamp_ever) {
5098 this_duration = source->ts_end - source->ts_start;
5099 step = 1;
5100 } else {
5101 int64_t last_duration =
5102 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5103
5104 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5105
5106 // do a step update if the duration changes by 10%
5107 if (last_duration)
5108 step = (int)((this_duration - last_duration) * 10 / last_duration);
5109 }
5110
5111 if (this_duration) {
5112 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005113 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005114 } else {
5115 // Average this frame's rate into the last second's average
5116 // frame rate. If we haven't seen 1 second yet, then average
5117 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005118 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005119 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5120 double avg_duration = 10000000.0 / cpi->framerate;
5121 avg_duration *= (interval - avg_duration + this_duration);
5122 avg_duration /= interval;
5123
Yaowu Xuf883b422016-08-30 14:01:10 -07005124 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005125 }
5126 }
5127 cpi->last_time_stamp_seen = source->ts_start;
5128 cpi->last_end_time_stamp_seen = source->ts_end;
5129}
5130
5131// Returns 0 if this is not an alt ref else the offset of the source frame
5132// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005133static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005134 RATE_CONTROL *const rc = &cpi->rc;
5135 int arf_src_index = 0;
5136 if (is_altref_enabled(cpi)) {
5137 if (cpi->oxcf.pass == 2) {
5138 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5139 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5140 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5141 }
5142 } else if (rc->source_alt_ref_pending) {
5143 arf_src_index = rc->frames_till_gf_update_due;
5144 }
5145 }
5146 return arf_src_index;
5147}
5148
Yaowu Xuf883b422016-08-30 14:01:10 -07005149static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005150 int brf_src_index = 0;
5151 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5152
5153 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5154 // flag.
5155 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5156 if (cpi->oxcf.pass == 2) {
5157 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5158 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5159 } else {
5160 // TODO(zoeliu): To re-visit the setup for this scenario
5161 brf_src_index = cpi->rc.bipred_group_interval - 1;
5162 }
5163 }
5164
5165 return brf_src_index;
5166}
Zoe Liue9b15e22017-07-19 15:53:01 -07005167
Zoe Liue9b15e22017-07-19 15:53:01 -07005168// Returns 0 if this is not an alt ref else the offset of the source frame
5169// used as the arf midpoint.
5170static int get_arf2_src_index(AV1_COMP *cpi) {
5171 int arf2_src_index = 0;
5172 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5173 if (cpi->oxcf.pass == 2) {
5174 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5175 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5176 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5177 }
5178 }
5179 }
5180 return arf2_src_index;
5181}
Yaowu Xuc27fc142016-08-22 16:08:15 -07005182
Yaowu Xuf883b422016-08-30 14:01:10 -07005183static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005184 const struct lookahead_entry *source) {
5185 RATE_CONTROL *const rc = &cpi->rc;
5186
5187 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005188 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005189
5190 if (cpi->oxcf.pass == 2) {
5191 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5192 rc->is_src_frame_alt_ref =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005193 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07005194 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07005195 rc->is_src_frame_ext_arf =
5196 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005197 } else {
5198 rc->is_src_frame_alt_ref =
5199 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5200 }
5201
5202 if (rc->is_src_frame_alt_ref) {
5203 // Current frame is an ARF overlay frame.
5204 cpi->alt_ref_source = NULL;
5205
Zoe Liue9b15e22017-07-19 15:53:01 -07005206 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
5207 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
5208 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
5209 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
5210 cpi->refresh_last_frame = 1;
5211 } else {
Zoe Liue9b15e22017-07-19 15:53:01 -07005212 // Don't refresh the last buffer for an ARF overlay frame. It will
5213 // become the GF so preserve last as an alternative prediction option.
5214 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005215 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005216 }
5217}
5218
5219#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005220extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5221 const unsigned char *img2, int img2_pitch,
5222 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005223
5224static void adjust_image_stat(double y, double u, double v, double all,
5225 ImageStat *s) {
5226 s->stat[Y] += y;
5227 s->stat[U] += u;
5228 s->stat[V] += v;
5229 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005230 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005231}
5232
Angie Chiang08a22a62017-07-17 17:29:17 -07005233static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005234 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005235 double samples = 0.0;
5236 uint32_t in_bit_depth = 8;
5237 uint32_t bit_depth = 8;
5238
Angie Chiang08a22a62017-07-17 17:29:17 -07005239#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07005240 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005241#endif
5242 cpi->bytes += frame_bytes;
5243
Yaowu Xuc27fc142016-08-22 16:08:15 -07005244 if (cm->use_highbitdepth) {
5245 in_bit_depth = cpi->oxcf.input_bit_depth;
5246 bit_depth = cm->bit_depth;
5247 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005248 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005249 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005250 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5251 double y, u, v, frame_all;
5252
5253 cpi->count++;
5254 if (cpi->b_calculate_psnr) {
5255 PSNR_STATS psnr;
5256 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005257 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005258 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005259 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005260
5261 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5262 &cpi->psnr);
5263 cpi->total_sq_error += psnr.sse[0];
5264 cpi->total_samples += psnr.samples[0];
5265 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005266 // TODO(yaowu): unify these two versions into one.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005267 if (cm->use_highbitdepth)
5268 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005269 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005270 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005271 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005272
Yaowu Xuf883b422016-08-30 14:01:10 -07005273 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005274 cpi->summed_quality += frame_ssim2 * weight;
5275 cpi->summed_weights += weight;
5276
5277#if 0
5278 {
5279 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005280 double y2 = psnr.psnr[1];
5281 double u2 = psnr.psnr[2];
5282 double v2 = psnr.psnr[3];
5283 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005284 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
Zoe Liuee202be2017-11-17 12:14:33 -08005285 cm->current_video_frame, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005286 frame_psnr2, frame_ssim2);
5287 fclose(f);
5288 }
5289#endif
5290 }
5291 if (cpi->b_calculate_blockiness) {
Yaowu Xud3e7c682017-12-21 14:08:25 -08005292 if (!cm->use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005293 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005294 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5295 recon->y_stride, orig->y_width, orig->y_height);
5296 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005297 cpi->total_blockiness += frame_blockiness;
5298 }
5299
5300 if (cpi->b_calculate_consistency) {
Yaowu Xud3e7c682017-12-21 14:08:25 -08005301 if (!cm->use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005302 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005303 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5304 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5305
5306 const double peak = (double)((1 << in_bit_depth) - 1);
5307 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005308 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005309 if (consistency > 0.0)
5310 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005311 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005312 cpi->total_inconsistency += this_inconsistency;
5313 }
5314 }
5315 }
5316
5317 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005318 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005319 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005320 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005321 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5322 }
5323}
5324#endif // CONFIG_INTERNAL_STATS
5325
RogerZhou3b635242017-09-19 10:06:46 -07005326static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5327 const YV12_BUFFER_CONFIG *last_picture,
5328 hash_table *last_hash_table) {
5329 aom_clear_system_state();
5330 // check use hash ME
5331 int k;
5332 uint32_t hash_value_1;
5333 uint32_t hash_value_2;
5334
5335 const int block_size = 8;
5336 const double threshold_current = 0.8;
5337 const double threshold_average = 0.95;
5338 const int max_history_size = 32;
5339 int T = 0; // total block
5340 int C = 0; // match with collocated block
5341 int S = 0; // smooth region but not match with collocated block
5342 int M = 0; // match with other block
5343
5344 const int pic_width = cur_picture->y_width;
5345 const int pic_height = cur_picture->y_height;
5346 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5347 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5348 const int x_pos = j;
5349 const int y_pos = i;
5350 int match = 1;
5351 T++;
5352
5353 // check whether collocated block match with current
5354 uint8_t *p_cur = cur_picture->y_buffer;
5355 uint8_t *p_ref = last_picture->y_buffer;
5356 int stride_cur = cur_picture->y_stride;
5357 int stride_ref = last_picture->y_stride;
5358 p_cur += (y_pos * stride_cur + x_pos);
5359 p_ref += (y_pos * stride_ref + x_pos);
5360
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005361 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5362 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5363 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5364 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5365 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5366 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5367 match = 0;
5368 }
RogerZhou3b635242017-09-19 10:06:46 -07005369 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005370 p16_cur += stride_cur;
5371 p16_ref += stride_ref;
RogerZhou3b635242017-09-19 10:06:46 -07005372 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005373 } else {
5374 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5375 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5376 if (p_cur[tmpX] != p_ref[tmpX]) {
5377 match = 0;
5378 }
5379 }
5380 p_cur += stride_cur;
5381 p_ref += stride_ref;
5382 }
RogerZhou3b635242017-09-19 10:06:46 -07005383 }
5384
5385 if (match) {
5386 C++;
5387 continue;
5388 }
5389
5390 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5391 y_pos) ||
5392 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5393 S++;
5394 continue;
5395 }
5396
5397 av1_get_block_hash_value(
5398 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005399 block_size, &hash_value_1, &hash_value_2,
5400 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH));
5401 // Hashing does not work for highbitdepth currently.
5402 // TODO(Roger): Make it work for highbitdepth.
5403 if (av1_use_hash_me(&cpi->common)) {
5404 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5405 M++;
5406 }
RogerZhou3b635242017-09-19 10:06:46 -07005407 }
5408 }
5409 }
5410
5411 assert(T > 0);
5412 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5413 double m_rate = ((double)(M)) / ((double)(T));
5414
5415 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5416 cpi->m_rate_array[cpi->rate_index] = m_rate;
5417
5418 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5419 cpi->rate_size++;
5420 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5421
5422 if (csm_rate < threshold_current) {
5423 return 0;
5424 }
5425
5426 if (C == T) {
5427 return 1;
5428 }
5429
5430 double csm_average = 0.0;
5431 double m_average = 0.0;
5432
5433 for (k = 0; k < cpi->rate_size; k++) {
5434 csm_average += cpi->csm_rate_array[k];
5435 m_average += cpi->m_rate_array[k];
5436 }
5437 csm_average /= cpi->rate_size;
5438 m_average /= cpi->rate_size;
5439
5440 if (csm_average < threshold_average) {
5441 return 0;
5442 }
5443
5444 if (M > (T - C - S) / 3) {
5445 return 1;
5446 }
5447
5448 if (csm_rate > 0.99 && m_rate > 0.01) {
5449 return 1;
5450 }
5451
5452 if (csm_average + m_average > 1.01) {
5453 return 1;
5454 }
5455
5456 return 0;
5457}
RogerZhou3b635242017-09-19 10:06:46 -07005458
Andrey Norkin795ba872018-03-06 13:24:14 -08005459#if CONFIG_BUFFER_MODEL
5460int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5461 size_t *size, uint8_t *dest, int64_t *time_stamp,
5462 int64_t *time_end, int flush,
5463 const aom_rational_t *timebase) {
5464#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005465int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5466 size_t *size, uint8_t *dest, int64_t *time_stamp,
5467 int64_t *time_end, int flush) {
Andrey Norkin795ba872018-03-06 13:24:14 -08005468#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07005469 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5470 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005471 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005472 BufferPool *const pool = cm->buffer_pool;
5473 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07005474 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005475 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5476 struct lookahead_entry *last_source = NULL;
5477 struct lookahead_entry *source = NULL;
5478 int arf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005479 int brf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005480 int i;
5481
5482#if CONFIG_BITSTREAM_DEBUG
5483 assert(cpi->oxcf.max_threads == 0 &&
5484 "bitstream debug tool does not support multithreading");
5485 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07005486 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005487#endif
5488
Dominic Symesd4929012018-01-31 17:32:01 +01005489 cm->showable_frame = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005490 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005491
RogerZhou3b635242017-09-19 10:06:46 -07005492 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005493
5494 // Is multi-arf enabled.
5495 // Note that at the moment multi_arf is only configured for 2 pass VBR
5496 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
5497 cpi->multi_arf_allowed = 1;
5498 else
5499 cpi->multi_arf_allowed = 0;
5500
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07005501 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07005502 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
5503 ? REFRESH_FRAME_CONTEXT_DISABLED
5504 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005505 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08005506 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005507
5508 cpi->refresh_last_frame = 1;
5509 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005510 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005511 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005512 cpi->refresh_alt_ref_frame = 0;
5513
Zoe Liub4991202017-12-21 15:31:06 -08005514 // TODO(zoeliu@gmail.com): To support forward-KEY_FRAME and set up the
5515 // following flag accordingly.
5516 cm->reset_decoder_state = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005517
Yaowu Xuc27fc142016-08-22 16:08:15 -07005518 if (oxcf->pass == 2 && cm->show_existing_frame) {
5519 // Manage the source buffer and flush out the source frame that has been
5520 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07005521 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005522 *size = 0;
5523 return -1;
5524 }
sarahparker21dbca42018-03-30 17:43:44 -07005525 av1_apply_encoding_flags(cpi, source->flags);
Alex Conversef77fd0b2017-04-20 11:00:24 -07005526 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005527 // TODO(zoeliu): To track down to determine whether it's needed to adjust
5528 // the frame rate.
5529 *time_stamp = source->ts_start;
5530 *time_end = source->ts_end;
5531
5532 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005533 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005534
Urvang Joshif1fa6862018-01-08 16:39:33 -08005535 // Find a free buffer for the new frame, releasing the reference
5536 // previously
Yaowu Xuc27fc142016-08-22 16:08:15 -07005537 // held.
5538 if (cm->new_fb_idx != INVALID_IDX) {
5539 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5540 }
5541 cm->new_fb_idx = get_free_fb(cm);
5542
5543 if (cm->new_fb_idx == INVALID_IDX) return -1;
5544
5545 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005546 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005547
5548 // Start with a 0 size frame.
5549 *size = 0;
5550
5551 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005552 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005553
Tom Finegane4099e32018-01-23 12:01:51 -08005554 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
5555 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005556
5557 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
5558
5559#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07005560 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005561#endif // CONFIG_INTERNAL_STATS
5562
5563 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005564 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005565
5566 cm->show_existing_frame = 0;
5567 return 0;
5568 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005569
5570 // Should we encode an arf frame.
5571 arf_src_index = get_arf_src_index(cpi);
5572 if (arf_src_index) {
5573 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005574 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005575 // Avoid creating an alt-ref if there's a forced keyframe pending.
5576 if (e == NULL) {
5577 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07005578 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005579 arf_src_index = 0;
5580 flush = 1;
5581 break;
5582 }
5583 }
5584 }
5585
5586 if (arf_src_index) {
5587 assert(arf_src_index <= rc->frames_to_key);
5588
Yaowu Xuf883b422016-08-30 14:01:10 -07005589 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005590 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005591 cpi->alt_ref_source = source;
5592
5593 if (oxcf->arnr_max_frames > 0) {
Sebastien Alaiwan6697acf2018-02-21 16:59:17 +01005594 // Produce the filtered ARF frame.
5595 av1_temporal_filter(cpi, arf_src_index);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005596 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005597 force_src_buffer = &cpi->alt_ref_buffer;
5598 }
5599
5600 cm->show_frame = 0;
5601 cm->intra_only = 0;
5602 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005603 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005604 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005605 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005606 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005607 rc->is_src_frame_alt_ref = 0;
5608 }
5609 rc->source_alt_ref_pending = 0;
5610 }
5611
Zoe Liue9b15e22017-07-19 15:53:01 -07005612 // Should we encode an arf2 frame.
5613 arf_src_index = get_arf2_src_index(cpi);
5614 if (arf_src_index) {
5615 for (i = 0; i <= arf_src_index; ++i) {
5616 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
5617 // Avoid creating an alt-ref if there's a forced keyframe pending.
5618 if (e == NULL) {
5619 break;
5620 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
5621 arf_src_index = 0;
5622 flush = 1;
5623 break;
5624 }
5625 }
5626 }
5627
5628 if (arf_src_index) {
5629 assert(arf_src_index <= rc->frames_to_key);
5630
5631 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005632 cm->showable_frame = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07005633 cpi->alt_ref_source = source;
5634
5635 if (oxcf->arnr_max_frames > 0) {
5636 // Produce the filtered ARF frame.
Sebastien Alaiwan6697acf2018-02-21 16:59:17 +01005637 av1_temporal_filter(cpi, arf_src_index);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005638 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
Zoe Liue9b15e22017-07-19 15:53:01 -07005639 force_src_buffer = &cpi->alt_ref_buffer;
5640 }
5641
5642 cm->show_frame = 0;
5643 cm->intra_only = 0;
5644 cpi->refresh_alt2_ref_frame = 1;
5645 cpi->refresh_last_frame = 0;
5646 cpi->refresh_golden_frame = 0;
5647 cpi->refresh_bwd_ref_frame = 0;
5648 cpi->refresh_alt_ref_frame = 0;
5649 rc->is_src_frame_alt_ref = 0;
5650 rc->is_src_frame_ext_arf = 0;
5651 }
5652 rc->source_alt_ref_pending = 0;
5653 }
Zoe Liue9b15e22017-07-19 15:53:01 -07005654
Yaowu Xuc27fc142016-08-22 16:08:15 -07005655 rc->is_bwd_ref_frame = 0;
5656 brf_src_index = get_brf_src_index(cpi);
5657 if (brf_src_index) {
5658 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07005659 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005660 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005661 cm->show_frame = 0;
5662 cm->intra_only = 0;
5663
5664 cpi->refresh_bwd_ref_frame = 1;
5665 cpi->refresh_last_frame = 0;
5666 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005667 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005668 cpi->refresh_alt_ref_frame = 0;
5669
5670 rc->is_bwd_ref_frame = 1;
5671 }
5672 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005673
5674 if (!source) {
5675 // Get last frame source.
5676 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005677 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005678 return -1;
5679 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005680 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005681 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005682 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005683
5684 if (source != NULL) {
5685 cm->show_frame = 1;
5686 cm->intra_only = 0;
5687
5688 // Check to see if the frame should be encoded as an arf overlay.
5689 check_src_altref(cpi, source);
5690 }
5691 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005692 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005693 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005694 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005695 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
5696
5697 *time_stamp = source->ts_start;
5698 *time_end = source->ts_end;
Sarah Parker73556772018-03-28 18:28:05 -07005699 av1_apply_encoding_flags(cpi, source->flags);
Yaowu Xuf883b422016-08-30 14:01:10 -07005700 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005701
5702 } else {
5703 *size = 0;
5704 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005705 av1_end_first_pass(cpi); /* get last stats packet */
Yaowu Xuc27fc142016-08-22 16:08:15 -07005706 cpi->twopass.first_pass_done = 1;
5707 }
5708 return -1;
5709 }
5710
5711 if (source->ts_start < cpi->first_time_stamp_ever) {
5712 cpi->first_time_stamp_ever = source->ts_start;
5713 cpi->last_end_time_stamp_seen = source->ts_start;
5714 }
5715
5716 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005717 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005718
5719 // adjust frame rates based on timestamps given
5720 if (cm->show_frame) adjust_frame_rate(cpi, source);
5721
5722 // Find a free buffer for the new frame, releasing the reference previously
5723 // held.
5724 if (cm->new_fb_idx != INVALID_IDX) {
5725 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5726 }
5727 cm->new_fb_idx = get_free_fb(cm);
5728
5729 if (cm->new_fb_idx == INVALID_IDX) return -1;
5730
Zoe Liuf452fdf2017-11-02 23:08:12 -07005731 // Retain the RF_LEVEL for the current newly coded frame.
5732 cpi->frame_rf_level[cm->new_fb_idx] =
5733 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
Zoe Liuf452fdf2017-11-02 23:08:12 -07005734
Yaowu Xuc27fc142016-08-22 16:08:15 -07005735 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005736 cm->cur_frame->buf.buf_8bit_valid = 0;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07005737
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07005738 if (cm->film_grain_table) {
5739 cm->film_grain_params_present = aom_film_grain_table_lookup(
5740 cm->film_grain_table, *time_stamp, *time_end, 0 /* erase */,
5741 &cm->film_grain_params);
5742 }
Dominic Symesd4929012018-01-31 17:32:01 +01005743 cm->cur_frame->film_grain_params_present = cm->film_grain_params_present;
Zoe Liu6cfaff92016-10-18 17:12:11 -07005744
Andrey Norkin795ba872018-03-06 13:24:14 -08005745#if CONFIG_BUFFER_MODEL
5746 // only one operating point supported now
5747 cpi->common.tu_presentation_delay =
5748 ticks_to_timebase_units(timebase, *time_stamp);
5749#endif
5750
Yaowu Xuc27fc142016-08-22 16:08:15 -07005751 // Start with a 0 size frame.
5752 *size = 0;
5753
5754 cpi->frame_flags = *frame_flags;
5755
5756 if (oxcf->pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005757 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005758 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07005759 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005760 }
5761
5762 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
Zoe Liu27deb382018-03-27 15:13:56 -07005763 for (i = 0; i < REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005764 }
5765
Yaowu Xuc27fc142016-08-22 16:08:15 -07005766 cm->using_qmatrix = cpi->oxcf.using_qm;
5767 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
5768 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005769
David Barker5e70a112017-10-03 14:28:17 +01005770 if (cm->seq_params.frame_id_numbers_present_flag) {
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005771 if (*time_stamp == 0) {
5772 cpi->common.current_frame_id = -1;
5773 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005774 }
Zoe Liuca0cd3f2018-02-26 15:07:50 -08005775
RogerZhou3b635242017-09-19 10:06:46 -07005776 cpi->cur_poc++;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08005777 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
5778 !frame_is_intra_only(cm)) {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00005779 if (cpi->common.seq_params.force_integer_mv == 2) {
RogerZhou3b635242017-09-19 10:06:46 -07005780 struct lookahead_entry *previous_entry =
Debargha Mukherjeea71e3db2018-02-28 07:47:17 -08005781 av1_lookahead_peek(cpi->lookahead, cpi->previous_index);
5782 if (!previous_entry)
5783 cpi->common.cur_frame_force_integer_mv = 0;
5784 else
5785 cpi->common.cur_frame_force_integer_mv = is_integer_mv(
5786 cpi, cpi->source, &previous_entry->img, cpi->previous_hash_table);
RogerZhou3b635242017-09-19 10:06:46 -07005787 } else {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00005788 cpi->common.cur_frame_force_integer_mv =
5789 cpi->common.seq_params.force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07005790 }
5791 } else {
RogerZhou10a03802017-10-26 11:49:48 -07005792 cpi->common.cur_frame_force_integer_mv = 0;
RogerZhou3b635242017-09-19 10:06:46 -07005793 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005794
Yaowu Xuc27fc142016-08-22 16:08:15 -07005795 if (oxcf->pass == 1) {
5796 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07005797 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005798 } else if (oxcf->pass == 2) {
Tom Finegane4099e32018-01-23 12:01:51 -08005799 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
5800 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005801 } else {
5802 // One pass encode
Tom Finegane4099e32018-01-23 12:01:51 -08005803 if (Pass0Encode(cpi, size, dest, 0, frame_flags) != AOM_CODEC_OK)
5804 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005805 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07005806 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08005807 cpi->previous_hash_table = &cm->cur_frame->hash_table;
RogerZhou3b635242017-09-19 10:06:46 -07005808 {
5809 int l;
5810 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
5811 if ((cpi->lookahead->buf + l) == source) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08005812 cpi->previous_index = l;
RogerZhou3b635242017-09-19 10:06:46 -07005813 break;
5814 }
5815 }
5816
5817 if (l == cpi->lookahead->max_sz) {
5818 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5819 "Failed to find last frame original buffer");
5820 }
5821 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07005822 }
5823
Yunqing Wang267e3272017-11-09 14:23:22 -08005824 if (!cm->large_scale_tile) {
Yunqing Wang267e3272017-11-09 14:23:22 -08005825 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Yunqing Wang267e3272017-11-09 14:23:22 -08005826 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005827
Yunqing Wangb041d8a2017-11-15 12:31:18 -08005828#define EXT_TILE_DEBUG 0
5829#if EXT_TILE_DEBUG
5830 if (cm->large_scale_tile && oxcf->pass == 2) {
5831 char fn[20] = "./fc";
5832 fn[4] = cm->current_video_frame / 100 + '0';
5833 fn[5] = (cm->current_video_frame % 100) / 10 + '0';
5834 fn[6] = (cm->current_video_frame % 10) + '0';
5835 fn[7] = '\0';
5836 av1_print_frame_contexts(cm->fc, fn);
5837 }
5838#endif // EXT_TILE_DEBUG
5839#undef EXT_TILE_DEBUG
Yaowu Xuc7119a72018-03-29 09:59:37 -07005840
Dominic Symesd4929012018-01-31 17:32:01 +01005841 cm->showable_frame = !cm->show_frame && cm->showable_frame;
Yunqing Wangb041d8a2017-11-15 12:31:18 -08005842
Yaowu Xuc27fc142016-08-22 16:08:15 -07005843 // No frame encoded, or frame was dropped, release scaled references.
5844 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
5845 release_scaled_references(cpi);
5846 }
5847
5848 if (*size > 0) {
5849 cpi->droppable = !frame_is_reference(cpi);
5850 }
5851
Yaowu Xuf883b422016-08-30 14:01:10 -07005852 aom_usec_timer_mark(&cmptimer);
5853 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005854
5855 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
5856 generate_psnr_packet(cpi);
5857
5858#if CONFIG_INTERNAL_STATS
5859 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07005860 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005861 }
5862#endif // CONFIG_INTERNAL_STATS
5863
Yaowu Xuf883b422016-08-30 14:01:10 -07005864 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005865
5866 return 0;
5867}
5868
Yaowu Xuf883b422016-08-30 14:01:10 -07005869int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5870 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005871 if (!cm->show_frame) {
5872 return -1;
5873 } else {
5874 int ret;
5875 if (cm->frame_to_show) {
5876 *dest = *cm->frame_to_show;
5877 dest->y_width = cm->width;
5878 dest->y_height = cm->height;
5879 dest->uv_width = cm->width >> cm->subsampling_x;
5880 dest->uv_height = cm->height >> cm->subsampling_y;
5881 ret = 0;
5882 } else {
5883 ret = -1;
5884 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005885 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005886 return ret;
5887 }
5888}
5889
Yaowu Xuf883b422016-08-30 14:01:10 -07005890int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005891 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
5892
5893 *frame =
5894 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
5895 return 0;
5896}
5897
Yaowu Xuf883b422016-08-30 14:01:10 -07005898int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
5899 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005900 int hr = 0, hs = 0, vr = 0, vs = 0;
5901
5902 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5903
5904 Scale2Ratio(horiz_mode, &hr, &hs);
5905 Scale2Ratio(vert_mode, &vr, &vs);
5906
5907 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005908 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5909 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005910
5911 return 0;
5912}
5913
Yaowu Xuf883b422016-08-30 14:01:10 -07005914int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005915
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005916int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
5917 size_t output_size = 0;
5918 size_t total_bytes_read = 0;
5919 size_t remaining_size = *frame_size;
5920 uint8_t *buff_ptr = buffer;
5921
5922 // go through each OBUs
5923 while (total_bytes_read < *frame_size) {
5924 uint8_t saved_obu_header[2];
5925 uint64_t obu_payload_size;
5926 size_t length_of_payload_size;
5927 size_t length_of_obu_size;
5928 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
5929 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
5930
5931 // save the obu header (1 or 2 bytes)
5932 memmove(saved_obu_header, buff_ptr, obu_header_size);
5933 // clear the obu_has_size_field
5934 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
5935
5936 // get the payload_size and length of payload_size
5937 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
5938 &obu_payload_size, &length_of_payload_size) != 0) {
5939 return AOM_CODEC_ERROR;
5940 }
5941 obu_bytes_read += length_of_payload_size;
5942
5943 // calculate the length of size of the obu header plus payload
5944 length_of_obu_size =
5945 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
5946
5947 // move the rest of data to new location
5948 memmove(buff_ptr + length_of_obu_size + obu_header_size,
5949 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07005950 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005951
5952 // write the new obu size
5953 const uint64_t obu_size = obu_header_size + obu_payload_size;
5954 size_t coded_obu_size;
5955 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
5956 &coded_obu_size) != 0) {
5957 return AOM_CODEC_ERROR;
5958 }
5959
5960 // write the saved (modified) obu_header following obu size
5961 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
5962
5963 total_bytes_read += obu_bytes_read;
5964 remaining_size -= obu_bytes_read;
5965 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07005966 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005967 }
5968
5969 *frame_size = output_size;
5970 return AOM_CODEC_OK;
5971}
5972
Yaowu Xuf883b422016-08-30 14:01:10 -07005973void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005974 // TODO(yunqingwang): For what references to use, external encoding flags
5975 // should be consistent with internal reference frame selection. Need to
5976 // ensure that there is not conflict between the two. In AV1 encoder, the
5977 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
5978 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
5979 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005980 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005981 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005982 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
5983 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
5984 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005985 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005986 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005987 } else {
5988 int ref = AOM_REFFRAME_ALL;
5989
5990 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
5991 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
5992
5993 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
5994
5995 if (flags & AOM_EFLAG_NO_REF_ARF) {
5996 ref ^= AOM_ALT_FLAG;
5997 ref ^= AOM_BWD_FLAG;
5998 ref ^= AOM_ALT2_FLAG;
5999 } else {
6000 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6001 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
6002 }
6003
6004 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006005 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006006 }
6007
6008 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006009 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006010 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006011
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006012 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6013 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006014
Yaowu Xuf883b422016-08-30 14:01:10 -07006015 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006016
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006017 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6018 upd ^= AOM_ALT_FLAG;
6019 upd ^= AOM_BWD_FLAG;
6020 upd ^= AOM_ALT2_FLAG;
6021 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006022
Yaowu Xuf883b422016-08-30 14:01:10 -07006023 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006024 }
6025
sarahparker21dbca42018-03-30 17:43:44 -07006026 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
6027 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07006028 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
6029 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07006030 cpi->ext_use_s_frame =
6031 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
sarahparker21dbca42018-03-30 17:43:44 -07006032
Yaowu Xuf883b422016-08-30 14:01:10 -07006033 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6034 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006035 }
6036}
Andrey Norkin795ba872018-03-06 13:24:14 -08006037
6038#if CONFIG_BUFFER_MODEL
6039int64_t timebase_units_to_ticks(const aom_rational_t *timebase, int64_t n) {
6040 return n * TICKS_PER_SEC * timebase->num / timebase->den;
6041}
6042
6043int64_t ticks_to_timebase_units(const aom_rational_t *timebase, int64_t n) {
6044 const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
6045 return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
6046}
6047#endif