blob: d37b3ff30dd2499f463e356a689c74693a1628de [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"
Jean-Marc Valin01435132017-02-18 14:12:53 -050019#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010020#include "av1/common/cdef.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050021#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -070022#include "av1/common/filter.h"
23#include "av1/common/idct.h"
24#include "av1/common/reconinter.h"
25#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070026#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/common/tile_common.h"
28
29#include "av1/encoder/aq_complexity.h"
30#include "av1/encoder/aq_cyclicrefresh.h"
31#include "av1/encoder/aq_variance.h"
32#include "av1/encoder/bitstream.h"
Todd Nguyen302d0972017-06-16 16:16:29 -070033#if CONFIG_BGSPRITE
34#include "av1/encoder/bgsprite.h"
35#endif // CONFIG_BGSPRITE
Yaowu Xuc27fc142016-08-22 16:08:15 -070036#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -070037#include "aom_dsp/buf_ans.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070038#endif
39#include "av1/encoder/context_tree.h"
40#include "av1/encoder/encodeframe.h"
41#include "av1/encoder/encodemv.h"
42#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070043#if CONFIG_LV_MAP
44#include "av1/encoder/encodetxb.h"
45#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070046#include "av1/encoder/ethread.h"
47#include "av1/encoder/firstpass.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070048#if CONFIG_HASH_ME
49#include "av1/encoder/hash_motion.h"
50#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070051#include "av1/encoder/mbgraph.h"
52#include "av1/encoder/picklpf.h"
53#if CONFIG_LOOP_RESTORATION
54#include "av1/encoder/pickrst.h"
55#endif // CONFIG_LOOP_RESTORATION
56#include "av1/encoder/ratectrl.h"
57#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070058#include "av1/encoder/segmentation.h"
59#include "av1/encoder/speed_features.h"
60#include "av1/encoder/temporal_filter.h"
61
Yaowu Xuf883b422016-08-30 14:01:10 -070062#include "./av1_rtcd.h"
63#include "./aom_dsp_rtcd.h"
64#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070065#include "aom_dsp/psnr.h"
66#if CONFIG_INTERNAL_STATS
67#include "aom_dsp/ssim.h"
68#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070069#include "aom_dsp/aom_dsp_common.h"
70#include "aom_dsp/aom_filter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070071#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070072#include "aom_ports/mem.h"
73#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070074#include "aom_scale/aom_scale.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070075#if CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070076#include "aom_util/debug_util.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070077#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070078
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080079#if CONFIG_ENTROPY_STATS
80FRAME_COUNTS aggregate_fc;
Zoe Liua56f9162017-06-21 22:49:57 -070081// Aggregate frame counts per frame context type
82FRAME_COUNTS aggregate_fc_per_type[FRAME_CONTEXTS];
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080083#endif // CONFIG_ENTROPY_STATS
84
Yaowu Xuc27fc142016-08-22 16:08:15 -070085#define AM_SEGMENT_ID_INACTIVE 7
86#define AM_SEGMENT_ID_ACTIVE 0
87
88#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
89
90#define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
91 // for altref computation.
92#define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
93 // mv. Choose a very high value for
94 // now so that HIGH_PRECISION is always
95 // chosen.
96// #define OUTPUT_YUV_REC
97#ifdef OUTPUT_YUV_DENOISED
98FILE *yuv_denoised_file = NULL;
99#endif
100#ifdef OUTPUT_YUV_SKINMAP
101FILE *yuv_skinmap_file = NULL;
102#endif
103#ifdef OUTPUT_YUV_REC
104FILE *yuv_rec_file;
105#define FILE_NAME_LEN 100
106#endif
107
108#if 0
109FILE *framepsnr;
110FILE *kf_list;
111FILE *keyfile;
112#endif
113
Luc Trudeauf8164152017-04-11 16:20:51 -0400114#if CONFIG_CFL
115CFL_CTX NULL_CFL;
116#endif
117
Urvang Joshib5ed3502016-10-17 16:38:05 -0700118#if CONFIG_INTERNAL_STATS
119typedef enum { Y, U, V, ALL } STAT_TYPE;
120#endif // CONFIG_INTERNAL_STATS
121
Yaowu Xuf883b422016-08-30 14:01:10 -0700122static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700123 switch (mode) {
124 case NORMAL:
125 *hr = 1;
126 *hs = 1;
127 break;
128 case FOURFIVE:
129 *hr = 4;
130 *hs = 5;
131 break;
132 case THREEFIVE:
133 *hr = 3;
134 *hs = 5;
135 break;
136 case ONETWO:
137 *hr = 1;
138 *hs = 2;
139 break;
140 default:
141 *hr = 1;
142 *hs = 1;
143 assert(0);
144 break;
145 }
146}
147
148// Mark all inactive blocks as active. Other segmentation features may be set
149// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700150static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 unsigned char *const seg_map = cpi->segmentation_map;
152 int i;
153 if (cpi->active_map.enabled || cpi->active_map.update)
154 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
155 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
156 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
157}
158
Yaowu Xuf883b422016-08-30 14:01:10 -0700159static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160 struct segmentation *const seg = &cpi->common.seg;
161 unsigned char *const seg_map = cpi->segmentation_map;
162 const unsigned char *const active_map = cpi->active_map.map;
163 int i;
164
165 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
166
167 if (frame_is_intra_only(&cpi->common)) {
168 cpi->active_map.enabled = 0;
169 cpi->active_map.update = 1;
170 }
171
172 if (cpi->active_map.update) {
173 if (cpi->active_map.enabled) {
174 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
175 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700176 av1_enable_segmentation(seg);
177 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
178 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179 // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
180 // filter level being zero regardless of the value of seg->abs_delta.
Yaowu Xuf883b422016-08-30 14:01:10 -0700181 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
182 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700184 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
185 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 if (seg->enabled) {
187 seg->update_data = 1;
188 seg->update_map = 1;
189 }
190 }
191 cpi->active_map.update = 0;
192 }
193}
194
Yaowu Xuf883b422016-08-30 14:01:10 -0700195int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
196 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
198 unsigned char *const active_map_8x8 = cpi->active_map.map;
199 const int mi_rows = cpi->common.mi_rows;
200 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700201 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
202 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 cpi->active_map.update = 1;
204 if (new_map_16x16) {
205 int r, c;
206 for (r = 0; r < mi_rows; ++r) {
207 for (c = 0; c < mi_cols; ++c) {
208 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700209 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210 ? AM_SEGMENT_ID_ACTIVE
211 : AM_SEGMENT_ID_INACTIVE;
212 }
213 }
214 cpi->active_map.enabled = 1;
215 } else {
216 cpi->active_map.enabled = 0;
217 }
218 return 0;
219 } else {
220 return -1;
221 }
222}
223
Yaowu Xuf883b422016-08-30 14:01:10 -0700224int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
225 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
227 new_map_16x16) {
228 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
229 const int mi_rows = cpi->common.mi_rows;
230 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700231 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
232 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
233
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
235 if (cpi->active_map.enabled) {
236 int r, c;
237 for (r = 0; r < mi_rows; ++r) {
238 for (c = 0; c < mi_cols; ++c) {
239 // Cyclic refresh segments are considered active despite not having
240 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700241 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
243 }
244 }
245 }
246 return 0;
247 } else {
248 return -1;
249 }
250}
251
Yaowu Xuf883b422016-08-30 14:01:10 -0700252void av1_set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253 MACROBLOCK *const mb = &cpi->td.mb;
254 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
255
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256 if (cpi->common.allow_high_precision_mv) {
257 int i;
258 for (i = 0; i < NMV_CONTEXTS; ++i) {
259 mb->mv_cost_stack[i] = mb->nmvcost_hp[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700260 }
261 } else {
262 int i;
263 for (i = 0; i < NMV_CONTEXTS; ++i) {
264 mb->mv_cost_stack[i] = mb->nmvcost[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265 }
266 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700267}
268
Yaowu Xuf883b422016-08-30 14:01:10 -0700269static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700271 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700272 return BLOCK_64X64;
273
Yaowu Xuf883b422016-08-30 14:01:10 -0700274 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700275 return BLOCK_128X128;
276
Yaowu Xuf883b422016-08-30 14:01:10 -0700277 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700278
279 assert(IMPLIES(cpi->common.tile_cols > 1,
280 cpi->common.tile_width % MAX_MIB_SIZE == 0));
281 assert(IMPLIES(cpi->common.tile_rows > 1,
282 cpi->common.tile_height % MAX_MIB_SIZE == 0));
283
284 // TODO(any): Possibly could improve this with a heuristic.
285 return BLOCK_128X128;
286#else
287 (void)cpi;
288 return BLOCK_64X64;
289#endif // CONFIG_EXT_PARTITION
290}
291
Yaowu Xuf883b422016-08-30 14:01:10 -0700292static void setup_frame(AV1_COMP *cpi) {
293 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 // Set up entropy context depending on frame type. The decoder mandates
295 // the use of the default context, index 0, for keyframes and inter
296 // frames where the error_resilient_mode or intra_only flag is set. For
297 // other inter-frames the encoder currently uses only two contexts;
298 // context 1 for ALTREF frames and context 0 for the others.
299 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700300 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700301 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700302#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
303// Just use frame context from first signaled reference frame.
304// This will always be LAST_FRAME for now.
305#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306#if CONFIG_EXT_REFS
307 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -0700308#if CONFIG_ALTREF2
309 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
310#else // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -0700311 if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
Zoe Liue9b15e22017-07-19 15:53:01 -0700312#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -0700313 cm->frame_context_idx = EXT_ARF_FRAME;
314 else if (cpi->refresh_alt_ref_frame)
315 cm->frame_context_idx = ARF_FRAME;
Zoe Liue9b15e22017-07-19 15:53:01 -0700316#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700317 if (cpi->refresh_alt_ref_frame) cm->frame_context_idx = ARF_FRAME;
Zoe Liu6cfaff92016-10-18 17:12:11 -0700318#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319 else if (cpi->rc.is_src_frame_alt_ref)
320 cm->frame_context_idx = OVERLAY_FRAME;
321 else if (cpi->refresh_golden_frame)
322 cm->frame_context_idx = GLD_FRAME;
323#if CONFIG_EXT_REFS
324 else if (cpi->refresh_bwd_ref_frame)
325 cm->frame_context_idx = BRF_FRAME;
Zoe Liu6cfaff92016-10-18 17:12:11 -0700326#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700327 else
328 cm->frame_context_idx = REGULAR_FRAME;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700329#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700330 }
331
332 if (cm->frame_type == KEY_FRAME) {
333 cpi->refresh_golden_frame = 1;
334 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700335 av1_zero(cpi->interp_filter_selected);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700336 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700337#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
338 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
339 cm->frame_refs[0].idx < 0) {
340 *cm->fc = cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
341 } else {
342 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
343 }
344#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700346#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuf883b422016-08-30 14:01:10 -0700347 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700348 }
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200349#if CONFIG_EXT_REFS
Zoe Liu5a978832017-08-15 16:33:34 -0700350#if CONFIG_ONE_SIDED_COMPOUND && \
351 !CONFIG_EXT_COMP_REFS // No change to bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200352 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
353 cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
354 cpi->rc.is_bipred_frame = 1;
355 }
Zoe Liu5a978832017-08-15 16:33:34 -0700356#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
357#endif // CONFIG_EXT_REFS
Thomas Daededa4d8b92017-06-05 15:44:14 -0700358#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -0700359 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
360 cm->frame_refs[0].idx < 0) {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700361 // use default frame context values
362 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
363 } else {
Thomas Daedea6a854b2017-06-22 17:49:11 -0700364 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700365 cm->pre_fc = &cm->frame_contexts[cm->frame_refs[0].idx];
366 }
367#else
Thomas Daede10e1da92017-04-26 13:22:21 -0700368 cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700369#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700370
371 cpi->vaq_refresh = 0;
372
373 set_sb_size(cm, select_sb_size(cpi));
374}
375
Yaowu Xuf883b422016-08-30 14:01:10 -0700376static void av1_enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377 int i;
378 cm->mi = cm->mip + cm->mi_stride + 1;
379 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
380 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
381 // Clear top border row
382 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
383 // Clear left border column
384 for (i = 1; i < cm->mi_rows + 1; ++i)
385 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700386 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
387 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
388
389 memset(cm->mi_grid_base, 0,
390 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
391}
392
Yaowu Xuf883b422016-08-30 14:01:10 -0700393static int av1_enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
394 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700395 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700396 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397 if (!cm->prev_mip) return 1;
398 cm->mi_alloc_size = mi_size;
399
Yaowu Xuf883b422016-08-30 14:01:10 -0700400 cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700401 if (!cm->mi_grid_base) return 1;
402 cm->prev_mi_grid_base =
Yaowu Xuf883b422016-08-30 14:01:10 -0700403 (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700404 if (!cm->prev_mi_grid_base) return 1;
405
406 return 0;
407}
408
Yaowu Xuf883b422016-08-30 14:01:10 -0700409static void av1_enc_free_mi(AV1_COMMON *cm) {
410 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700411 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700412 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700413 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700414 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700415 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700416 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417 cm->prev_mi_grid_base = NULL;
418}
419
Yaowu Xuf883b422016-08-30 14:01:10 -0700420static void av1_swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421 // Current mip will be the prev_mip for the next frame.
422 MODE_INFO **temp_base = cm->prev_mi_grid_base;
423 MODE_INFO *temp = cm->prev_mip;
424 cm->prev_mip = cm->mip;
425 cm->mip = temp;
426
427 // Update the upper left visible macroblock ptrs.
428 cm->mi = cm->mip + cm->mi_stride + 1;
429 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
430
431 cm->prev_mi_grid_base = cm->mi_grid_base;
432 cm->mi_grid_base = temp_base;
433 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
434 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
435}
436
Yaowu Xuf883b422016-08-30 14:01:10 -0700437void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700438 static volatile int init_done = 0;
439
440 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700441 av1_rtcd();
442 aom_dsp_rtcd();
443 aom_scale_rtcd();
444 av1_init_intra_predictors();
445 av1_init_me_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000446#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -0700447 av1_rc_init_minq_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000448#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700449 av1_entropy_mv_init();
450 av1_encode_token_init();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700451#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700452 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700453#endif
454 init_done = 1;
455 }
456}
457
Yaowu Xuf883b422016-08-30 14:01:10 -0700458static void dealloc_compressor_data(AV1_COMP *cpi) {
459 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700460
Yaowu Xuf883b422016-08-30 14:01:10 -0700461 aom_free(cpi->mbmi_ext_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700462 cpi->mbmi_ext_base = NULL;
463
Yushin Cho77bba8d2016-11-04 16:36:56 -0700464#if CONFIG_PVQ
465 if (cpi->oxcf.pass != 1) {
Yushin Cho749c0572017-04-07 10:36:47 -0700466 const int tile_cols = cm->tile_cols;
467 const int tile_rows = cm->tile_rows;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700468 int tile_col, tile_row;
469
470 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
471 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
472 TileDataEnc *tile_data =
473 &cpi->tile_data[tile_row * tile_cols + tile_col];
474 aom_free(tile_data->pvq_q.buf);
475 }
476 }
477#endif
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#if CONFIG_MOTION_VAR
492 aom_free(cpi->td.mb.above_pred_buf);
493 cpi->td.mb.above_pred_buf = NULL;
494
495 aom_free(cpi->td.mb.left_pred_buf);
496 cpi->td.mb.left_pred_buf = NULL;
497
498 aom_free(cpi->td.mb.wsrc_buf);
499 cpi->td.mb.wsrc_buf = NULL;
500
501 aom_free(cpi->td.mb.mask_buf);
502 cpi->td.mb.mask_buf = NULL;
503#endif
504
Yaowu Xuf883b422016-08-30 14:01:10 -0700505 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700506#if CONFIG_LV_MAP
507 av1_free_txb_buf(cpi);
508#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700509 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510
Yaowu Xuf883b422016-08-30 14:01:10 -0700511 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700512#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700513 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800514 aom_free_frame_buffer(&cpi->last_frame_db);
515 aom_free_frame_buffer(&cpi->trial_frame_rst);
516 aom_free(cpi->extra_rstbuf);
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -0700517 {
518 int i;
519 for (i = 0; i < MAX_MB_PLANE; ++i)
520 av1_free_restoration_struct(&cpi->rst_search[i]);
521 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700523 aom_free_frame_buffer(&cpi->scaled_source);
524 aom_free_frame_buffer(&cpi->scaled_last_source);
525 aom_free_frame_buffer(&cpi->alt_ref_buffer);
526 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700527
Yaowu Xuf883b422016-08-30 14:01:10 -0700528 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529 cpi->tile_tok[0][0] = 0;
530
Yaowu Xuf883b422016-08-30 14:01:10 -0700531 av1_free_pc_tree(&cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700532
hui sud9a812b2017-07-06 14:34:37 -0700533 aom_free(cpi->td.mb.palette_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534
Yaowu Xuc27fc142016-08-22 16:08:15 -0700535#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -0700536 aom_buf_ans_free(&cpi->buf_ans);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537#endif // CONFIG_ANS
538}
539
Yaowu Xuf883b422016-08-30 14:01:10 -0700540static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700541 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700542 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200545 // Stores a snapshot of key state variables which can subsequently be
546 // restored with a call to av1_restore_coding_context. These functions are
547 // intended for use in a re-code loop in av1_compress_frame where the
548 // quantizer value is adjusted between loop iterations.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549 for (i = 0; i < NMV_CONTEXTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700550 av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
Urvang Joshibffc0b52016-07-25 13:38:49 -0700551 av1_copy(cc->nmv_costs, cpi->nmv_costs);
552 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700553 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554
Yaowu Xuf883b422016-08-30 14:01:10 -0700555 av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
556 av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700557
558 cc->fc = *cm->fc;
559}
560
Yaowu Xuf883b422016-08-30 14:01:10 -0700561static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700563 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700565
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200566 // Restore key state variables to the snapshot state stored in the
567 // previous call to av1_save_coding_context.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700568 for (i = 0; i < NMV_CONTEXTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700569 av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
Urvang Joshibffc0b52016-07-25 13:38:49 -0700570 av1_copy(cpi->nmv_costs, cc->nmv_costs);
571 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700572 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700573
Yaowu Xuf883b422016-08-30 14:01:10 -0700574 av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
575 av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700576
577 *cm->fc = cc->fc;
578}
579
Yaowu Xuf883b422016-08-30 14:01:10 -0700580static void configure_static_seg_features(AV1_COMP *cpi) {
581 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582 const RATE_CONTROL *const rc = &cpi->rc;
583 struct segmentation *const seg = &cm->seg;
584
585 int high_q = (int)(rc->avg_q > 48.0);
586 int qi_delta;
587
588 // Disable and clear down for KF
589 if (cm->frame_type == KEY_FRAME) {
590 // Clear down the global segmentation map
591 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
592 seg->update_map = 0;
593 seg->update_data = 0;
594 cpi->static_mb_pct = 0;
595
596 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700597 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700598
599 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700600 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601 } else if (cpi->refresh_alt_ref_frame) {
602 // If this is an alt ref frame
603 // Clear down the global segmentation map
604 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
605 seg->update_map = 0;
606 seg->update_data = 0;
607 cpi->static_mb_pct = 0;
608
609 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700610 av1_disable_segmentation(seg);
611 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700612
613 // Scan frames from current to arf frame.
614 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700615 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700616
617 // If segmentation was enabled set those features needed for the
618 // arf itself.
619 if (seg->enabled) {
620 seg->update_map = 1;
621 seg->update_data = 1;
622
623 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700624 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
625 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
626 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700627
Yaowu Xuf883b422016-08-30 14:01:10 -0700628 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
629 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700630
631 // Where relevant assume segment data is delta data
632 seg->abs_delta = SEGMENT_DELTADATA;
633 }
634 } else if (seg->enabled) {
635 // All other frames if segmentation has been enabled
636
637 // First normal frame in a valid gf or alt ref group
638 if (rc->frames_since_golden == 0) {
639 // Set up segment features for normal frames in an arf group
640 if (rc->source_alt_ref_active) {
641 seg->update_map = 0;
642 seg->update_data = 1;
643 seg->abs_delta = SEGMENT_DELTADATA;
644
Yaowu Xuf883b422016-08-30 14:01:10 -0700645 qi_delta =
646 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
647 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
648 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700649
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
651 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700652
653 // Segment coding disabled for compred testing
654 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700655 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
656 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
657 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658 }
659 } else {
660 // Disable segmentation and clear down features if alt ref
661 // is not active for this group
662
Yaowu Xuf883b422016-08-30 14:01:10 -0700663 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664
665 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
666
667 seg->update_map = 0;
668 seg->update_data = 0;
669
Yaowu Xuf883b422016-08-30 14:01:10 -0700670 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700671 }
672 } else if (rc->is_src_frame_alt_ref) {
673 // Special case where we are coding over the top of a previous
674 // alt ref frame.
675 // Segment coding disabled for compred testing
676
677 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700678 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
679 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700680
681 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700682 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
683 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
684 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
685 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686
687 // Skip all MBs if high Q (0,0 mv and skip coeffs)
688 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700689 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
690 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700691 }
692 // Enable data update
693 seg->update_data = 1;
694 } else {
695 // All other frames.
696
697 // No updates.. leave things as they are.
698 seg->update_map = 0;
699 seg->update_data = 0;
700 }
701 }
702}
703
Yaowu Xuf883b422016-08-30 14:01:10 -0700704static void update_reference_segmentation_map(AV1_COMP *cpi) {
705 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
707 uint8_t *cache_ptr = cm->last_frame_seg_map;
708 int row, col;
709
710 for (row = 0; row < cm->mi_rows; row++) {
711 MODE_INFO **mi_8x8 = mi_8x8_ptr;
712 uint8_t *cache = cache_ptr;
713 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
714 cache[0] = mi_8x8[0]->mbmi.segment_id;
715 mi_8x8_ptr += cm->mi_stride;
716 cache_ptr += cm->mi_cols;
717 }
718}
719
Yaowu Xuf883b422016-08-30 14:01:10 -0700720static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
721 AV1_COMMON *cm = &cpi->common;
722 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723
724 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700725 cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
726 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200727#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700728 cm->use_highbitdepth,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700730 oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700731 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700732 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700733 "Failed to allocate lag buffers");
734
735 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700736 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200738#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739 cm->use_highbitdepth,
740#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700741 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
742 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700743 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744 "Failed to allocate altref buffer");
745}
746
Yaowu Xuf883b422016-08-30 14:01:10 -0700747static void alloc_util_frame_buffers(AV1_COMP *cpi) {
748 AV1_COMMON *const cm = &cpi->common;
749 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200751#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 cm->use_highbitdepth,
753#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700754 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
755 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700756 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700757 "Failed to allocate last frame buffer");
758
759#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700760 if (aom_realloc_frame_buffer(&cpi->last_frame_db, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700761 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200762#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763 cm->use_highbitdepth,
764#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700765 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
766 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700767 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768 "Failed to allocate last frame deblocked buffer");
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700769 if (aom_realloc_frame_buffer(
770 &cpi->trial_frame_rst,
771#if CONFIG_FRAME_SUPERRES
772 cm->superres_upscaled_width, cm->superres_upscaled_height,
773#else
774 cm->width, cm->height,
775#endif // CONFIG_FRAME_SUPERRES
776 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200777#if CONFIG_HIGHBITDEPTH
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700778 cm->use_highbitdepth,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800779#endif
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700780 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800781 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800782 "Failed to allocate trial restored frame buffer");
Alex Converse232e3842017-02-24 12:24:36 -0800783 int extra_rstbuf_sz = RESTORATION_EXTBUF_SIZE;
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -0800784 if (extra_rstbuf_sz > 0) {
Alex Converse7f094f12017-02-23 17:29:40 -0800785 aom_free(cpi->extra_rstbuf);
Alex Converse232e3842017-02-24 12:24:36 -0800786 CHECK_MEM_ERROR(cm, cpi->extra_rstbuf,
Alex Converse7f094f12017-02-23 17:29:40 -0800787 (uint8_t *)aom_malloc(extra_rstbuf_sz));
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -0800788 } else {
789 cpi->extra_rstbuf = NULL;
790 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791#endif // CONFIG_LOOP_RESTORATION
792
Yaowu Xuf883b422016-08-30 14:01:10 -0700793 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200795#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796 cm->use_highbitdepth,
797#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700798 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
799 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700800 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700801 "Failed to allocate scaled source buffer");
802
Yaowu Xuf883b422016-08-30 14:01:10 -0700803 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700804 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200805#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806 cm->use_highbitdepth,
807#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700808 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
809 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700810 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700811 "Failed to allocate scaled last source buffer");
812}
813
Yunqing Wangf5dc71e2017-05-04 18:20:17 -0700814static void alloc_context_buffers_ext(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816 int mi_size = cm->mi_cols * cm->mi_rows;
817
Yunqing Wangf5dc71e2017-05-04 18:20:17 -0700818 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
819 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820}
821
Yaowu Xuf883b422016-08-30 14:01:10 -0700822void av1_alloc_compressor_data(AV1_COMP *cpi) {
823 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700824
Yaowu Xuf883b422016-08-30 14:01:10 -0700825 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700827#if CONFIG_LV_MAP
828 av1_alloc_txb_buf(cpi);
829#endif
830
Yaowu Xuc27fc142016-08-22 16:08:15 -0700831 alloc_context_buffers_ext(cpi);
832
Yaowu Xuf883b422016-08-30 14:01:10 -0700833 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700834
835 {
836 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
837 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700838 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839 }
840
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842}
843
Yaowu Xuf883b422016-08-30 14:01:10 -0700844void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000846#if CONFIG_XIPHRC
847 if (!cpi->od_rc.cur_frame) return;
848 cpi->od_rc.framerate = cpi->framerate;
849 od_enc_rc_resize(&cpi->od_rc);
850#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700851 av1_rc_update_framerate(cpi);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000852#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853}
854
Yaowu Xuf883b422016-08-30 14:01:10 -0700855static void set_tile_info(AV1_COMP *cpi) {
856 AV1_COMMON *const cm = &cpi->common;
Thomas Daviesb25ba502017-07-18 10:18:24 +0100857#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -0800858 int tile_row, tile_col, num_tiles_in_tg;
859 int tg_row_start, tg_col_start;
860#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700861#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700862 if (cpi->oxcf.large_scale_tile) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863#if CONFIG_EXT_PARTITION
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700864 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
865 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
866 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
867 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
868 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
869 } else {
870 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
871 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
872 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
873 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
874 }
875#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700876 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
877 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700878 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
879 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880#endif // CONFIG_EXT_PARTITION
881
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700882 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
883 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700884
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700885 assert(cm->tile_width >> MAX_MIB_SIZE <= 32);
886 assert(cm->tile_height >> MAX_MIB_SIZE <= 32);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700888 // Get the number of tiles
889 cm->tile_cols = 1;
890 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700892 cm->tile_rows = 1;
893 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
894 } else {
895#endif // CONFIG_EXT_TILE
896 int min_log2_tile_cols, max_log2_tile_cols;
897 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700898
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700899 cm->log2_tile_cols =
900 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
901 cm->log2_tile_rows = cpi->oxcf.tile_rows;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700903 cm->tile_cols = 1 << cm->log2_tile_cols;
904 cm->tile_rows = 1 << cm->log2_tile_rows;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700905
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700906 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
907 cm->tile_width >>= cm->log2_tile_cols;
908 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
909 cm->tile_height >>= cm->log2_tile_rows;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700910
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700911 // round to integer multiples of max superblock size
912 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
913 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
914#if CONFIG_EXT_TILE
915 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916#endif // CONFIG_EXT_TILE
Ryan Lei7386eda2016-12-08 21:08:31 -0800917
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800918#if CONFIG_DEPENDENT_HORZTILES
919 cm->dependent_horz_tiles = cpi->oxcf.dependent_horz_tiles;
Fangwen Fu70bcb892017-05-06 17:05:19 -0700920#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700921 if (cm->large_scale_tile) {
922 // May not needed since cpi->oxcf.dependent_horz_tiles is already adjusted.
923 cm->dependent_horz_tiles = 0;
Fangwen Fu73126c02017-02-08 22:37:47 -0800924 } else {
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700925#endif // CONFIG_EXT_TILE
926 if (cm->log2_tile_rows == 0) cm->dependent_horz_tiles = 0;
927#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -0800928 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700929#endif // CONFIG_EXT_TILE
930
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700931#if CONFIG_EXT_TILE
932 if (!cm->large_scale_tile) {
933#endif // CONFIG_EXT_TILE
934 if (cpi->oxcf.mtu == 0) {
935 cm->num_tg = cpi->oxcf.num_tile_groups;
936 } else {
937 // Use a default value for the purposes of weighting costs in probability
938 // updates
939 cm->num_tg = DEFAULT_MAX_NUM_TG;
Fangwen Fu73126c02017-02-08 22:37:47 -0800940 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700941 num_tiles_in_tg =
942 (cm->tile_cols * cm->tile_rows + cm->num_tg - 1) / cm->num_tg;
943 tg_row_start = 0;
944 tg_col_start = 0;
945 for (tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
946 for (tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
947 if ((tile_row * cm->tile_cols + tile_col) % num_tiles_in_tg == 0) {
948 tg_row_start = tile_row;
949 tg_col_start = tile_col;
950 }
951 cm->tile_group_start_row[tile_row][tile_col] = tg_row_start;
952 cm->tile_group_start_col[tile_row][tile_col] = tg_col_start;
953 }
954 }
955#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -0800956 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700957#endif // CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -0800958#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800959
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800960#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800961 cm->loop_filter_across_tiles_enabled =
962 cpi->oxcf.loop_filter_across_tiles_enabled;
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800963#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -0700964}
965
Yaowu Xuf883b422016-08-30 14:01:10 -0700966static void update_frame_size(AV1_COMP *cpi) {
967 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700968 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
969
Yaowu Xuf883b422016-08-30 14:01:10 -0700970 av1_set_mb_mi(cm, cm->width, cm->height);
971 av1_init_context_buffers(cm);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700972 av1_init_macroblockd(cm, xd,
973#if CONFIG_PVQ
974 NULL,
975#endif
Luc Trudeauf8164152017-04-11 16:20:51 -0400976#if CONFIG_CFL
977 &NULL_CFL,
978#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700979 NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700980 memset(cpi->mbmi_ext_base, 0,
981 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
982
983 set_tile_info(cpi);
984}
985
Yaowu Xuf883b422016-08-30 14:01:10 -0700986static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700987#if CONFIG_EXT_REFS
988 int fb_idx;
989 for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
990 cpi->lst_fb_idxes[fb_idx] = fb_idx;
991 cpi->gld_fb_idx = LAST_REF_FRAMES;
992 cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
Zoe Liue9b15e22017-07-19 15:53:01 -0700993#if CONFIG_ALTREF2
994 cpi->alt2_fb_idx = LAST_REF_FRAMES + 2;
995 cpi->alt_fb_idx = LAST_REF_FRAMES + 3;
996#else // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -0700997 cpi->alt_fb_idx = LAST_REF_FRAMES + 2;
Zoe Liue9b15e22017-07-19 15:53:01 -0700998#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -0700999 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
1000 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
Zoe Liue9b15e22017-07-19 15:53:01 -07001001#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001002 cpi->lst_fb_idx = 0;
1003 cpi->gld_fb_idx = 1;
1004 cpi->alt_fb_idx = 2;
1005#endif // CONFIG_EXT_REFS
1006}
1007
Yaowu Xuf883b422016-08-30 14:01:10 -07001008static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1009 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001010
1011 cpi->oxcf = *oxcf;
1012 cpi->framerate = oxcf->init_framerate;
1013
1014 cm->profile = oxcf->profile;
1015 cm->bit_depth = oxcf->bit_depth;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001016#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001017 cm->use_highbitdepth = oxcf->use_highbitdepth;
1018#endif
1019 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07001020#if CONFIG_COLORSPACE_HEADERS
1021 cm->transfer_function = oxcf->transfer_function;
1022 cm->chroma_sample_position = oxcf->chroma_sample_position;
1023#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001024 cm->color_range = oxcf->color_range;
1025
1026 cm->width = oxcf->width;
1027 cm->height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001028 av1_alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001029
1030 // Single thread case: use counts in common.
1031 cpi->td.counts = &cm->counts;
1032
1033 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001034 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001035
1036 cpi->static_mb_pct = 0;
1037 cpi->ref_frame_flags = 0;
1038
1039 init_buffer_indices(cpi);
1040}
1041
1042static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001043 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001044 const int64_t bandwidth = oxcf->target_bandwidth;
1045 const int64_t starting = oxcf->starting_buffer_level_ms;
1046 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1047 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1048
1049 rc->starting_buffer_level = starting * bandwidth / 1000;
1050 rc->optimal_buffer_level =
1051 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1052 rc->maximum_buffer_size =
1053 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1054}
1055
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001056#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001057#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
1058 cpi->fn_ptr[BT].sdf = SDF; \
1059 cpi->fn_ptr[BT].sdaf = SDAF; \
1060 cpi->fn_ptr[BT].vf = VF; \
1061 cpi->fn_ptr[BT].svf = SVF; \
1062 cpi->fn_ptr[BT].svaf = SVAF; \
1063 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1064 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1065 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1066
1067#define MAKE_BFP_SAD_WRAPPER(fnname) \
1068 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1069 int source_stride, \
1070 const uint8_t *ref_ptr, int ref_stride) { \
1071 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1072 } \
1073 static unsigned int fnname##_bits10( \
1074 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1075 int ref_stride) { \
1076 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1077 } \
1078 static unsigned int fnname##_bits12( \
1079 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1080 int ref_stride) { \
1081 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1082 }
1083
1084#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1085 static unsigned int fnname##_bits8( \
1086 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1087 int ref_stride, const uint8_t *second_pred) { \
1088 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1089 } \
1090 static unsigned int fnname##_bits10( \
1091 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1092 int ref_stride, const uint8_t *second_pred) { \
1093 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1094 2; \
1095 } \
1096 static unsigned int fnname##_bits12( \
1097 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1098 int ref_stride, const uint8_t *second_pred) { \
1099 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1100 4; \
1101 }
1102
1103#define MAKE_BFP_SAD3_WRAPPER(fnname) \
1104 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1105 const uint8_t *ref_ptr, int ref_stride, \
1106 unsigned int *sad_array) { \
1107 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1108 } \
1109 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1110 const uint8_t *ref_ptr, int ref_stride, \
1111 unsigned int *sad_array) { \
1112 int i; \
1113 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1114 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1115 } \
1116 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1117 const uint8_t *ref_ptr, int ref_stride, \
1118 unsigned int *sad_array) { \
1119 int i; \
1120 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1121 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1122 }
1123
1124#define MAKE_BFP_SAD8_WRAPPER(fnname) \
1125 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1126 const uint8_t *ref_ptr, int ref_stride, \
1127 unsigned int *sad_array) { \
1128 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1129 } \
1130 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1131 const uint8_t *ref_ptr, int ref_stride, \
1132 unsigned int *sad_array) { \
1133 int i; \
1134 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1135 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1136 } \
1137 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1138 const uint8_t *ref_ptr, int ref_stride, \
1139 unsigned int *sad_array) { \
1140 int i; \
1141 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1142 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1143 }
1144#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1145 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1146 const uint8_t *const ref_ptr[], int ref_stride, \
1147 unsigned int *sad_array) { \
1148 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1149 } \
1150 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1151 const uint8_t *const ref_ptr[], int ref_stride, \
1152 unsigned int *sad_array) { \
1153 int i; \
1154 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1155 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1156 } \
1157 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1158 const uint8_t *const ref_ptr[], int ref_stride, \
1159 unsigned int *sad_array) { \
1160 int i; \
1161 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1162 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1163 }
1164
1165#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001166MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1167MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1168MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
1169MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
1170MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1171MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1172MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1173MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1174MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1175MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1176MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001177#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001178MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1179MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1180MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1181MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1182MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1183MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1184MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1185MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1186MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1187MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1188MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1189MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1190MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1191MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1192MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
1193MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
1194MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1195MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1196MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1197MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
1198MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
1199MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1200MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1201MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1202MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
1203MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
1204MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1205MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1206MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1207MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
1208MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
1209MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1210MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1211MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1212MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
1213MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
1214MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1215MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1216MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1217MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
1218MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
1219MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1220MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1221MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1222MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
1223MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1224MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1225MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1226MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
1227MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1228MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1229MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1230MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
1231MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
1232MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001233
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001234#if CONFIG_EXT_PARTITION_TYPES
1235MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1236MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1237MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1238MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1239MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1240MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1241MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1242MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1243MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1244MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1245MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1246MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001247MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1248MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1249MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1250MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1251MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1252MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001253#endif
1254
Yaowu Xuc27fc142016-08-22 16:08:15 -07001255#if CONFIG_EXT_INTER
David Barker0f3c94e2017-05-16 15:21:50 +01001256#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001257 cpi->fn_ptr[BT].msdf = MCSDF; \
1258 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001259
David Barkerc155e012017-05-11 13:54:54 +01001260#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1261 static unsigned int fnname##_bits8( \
1262 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1263 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1264 int m_stride, int invert_mask) { \
1265 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1266 second_pred_ptr, m, m_stride, invert_mask); \
1267 } \
1268 static unsigned int fnname##_bits10( \
1269 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1270 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1271 int m_stride, int invert_mask) { \
1272 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1273 second_pred_ptr, m, m_stride, invert_mask) >> \
1274 2; \
1275 } \
1276 static unsigned int fnname##_bits12( \
1277 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1278 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1279 int m_stride, int invert_mask) { \
1280 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1281 second_pred_ptr, m, m_stride, invert_mask) >> \
1282 4; \
1283 }
1284
Yaowu Xuc27fc142016-08-22 16:08:15 -07001285#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001286MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1287MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1288MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001289#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001290MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1291MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1292MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1293MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1294MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1295MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1296MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1297MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1298MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1299MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1300MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1301MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1302MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001303
1304#if CONFIG_EXT_PARTITION_TYPES
1305MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1306MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1307MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1308MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001309MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1310MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001311#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001312#endif // CONFIG_EXT_INTER
1313
Yue Chencb60b182016-10-13 15:18:22 -07001314#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001315#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1316 cpi->fn_ptr[BT].osdf = OSDF; \
1317 cpi->fn_ptr[BT].ovf = OVF; \
1318 cpi->fn_ptr[BT].osvf = OSVF;
1319
1320#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1321 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1322 const int32_t *wsrc, \
1323 const int32_t *msk) { \
1324 return fnname(ref, ref_stride, wsrc, msk); \
1325 } \
1326 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1327 const int32_t *wsrc, \
1328 const int32_t *msk) { \
1329 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1330 } \
1331 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1332 const int32_t *wsrc, \
1333 const int32_t *msk) { \
1334 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1335 }
1336
1337#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001338MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1339MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1340MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001341#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001342MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1343MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1344MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1345MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1346MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1347MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1348MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1349MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1350MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1351MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1352MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1353MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1354MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001355
1356#if CONFIG_EXT_PARTITION_TYPES
1357MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1358MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1359MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1360MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001361MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1362MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001363#endif
Yue Chencb60b182016-10-13 15:18:22 -07001364#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001365
Yaowu Xuf883b422016-08-30 14:01:10 -07001366static void highbd_set_var_fns(AV1_COMP *const cpi) {
1367 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001368 if (cm->use_highbitdepth) {
1369 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001370 case AOM_BITS_8:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001371#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001372 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1373 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1374 aom_highbd_8_sub_pixel_variance64x16,
1375 aom_highbd_8_sub_pixel_avg_variance64x16, NULL, NULL,
1376 aom_highbd_sad64x16x4d_bits8)
1377
1378 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1379 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1380 aom_highbd_8_sub_pixel_variance16x64,
1381 aom_highbd_8_sub_pixel_avg_variance16x64, NULL, NULL,
1382 aom_highbd_sad16x64x4d_bits8)
1383
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001384 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits8,
1385 aom_highbd_sad32x8_avg_bits8, aom_highbd_8_variance32x8,
1386 aom_highbd_8_sub_pixel_variance32x8,
1387 aom_highbd_8_sub_pixel_avg_variance32x8, NULL, NULL,
1388 aom_highbd_sad32x8x4d_bits8)
1389
1390 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits8,
1391 aom_highbd_sad8x32_avg_bits8, aom_highbd_8_variance8x32,
1392 aom_highbd_8_sub_pixel_variance8x32,
1393 aom_highbd_8_sub_pixel_avg_variance8x32, NULL, NULL,
1394 aom_highbd_sad8x32x4d_bits8)
1395
1396 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits8,
1397 aom_highbd_sad16x4_avg_bits8, aom_highbd_8_variance16x4,
1398 aom_highbd_8_sub_pixel_variance16x4,
1399 aom_highbd_8_sub_pixel_avg_variance16x4, NULL, NULL,
1400 aom_highbd_sad16x4x4d_bits8)
1401
1402 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits8,
1403 aom_highbd_sad4x16_avg_bits8, aom_highbd_8_variance4x16,
1404 aom_highbd_8_sub_pixel_variance4x16,
1405 aom_highbd_8_sub_pixel_avg_variance4x16, NULL, NULL,
1406 aom_highbd_sad4x16x4d_bits8)
1407#endif
1408
Yaowu Xuf883b422016-08-30 14:01:10 -07001409 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1410 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1411 aom_highbd_8_sub_pixel_variance32x16,
1412 aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1413 aom_highbd_sad32x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001414
Yaowu Xuf883b422016-08-30 14:01:10 -07001415 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1416 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1417 aom_highbd_8_sub_pixel_variance16x32,
1418 aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1419 aom_highbd_sad16x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001420
Yaowu Xuf883b422016-08-30 14:01:10 -07001421 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1422 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1423 aom_highbd_8_sub_pixel_variance64x32,
1424 aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1425 aom_highbd_sad64x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001426
Yaowu Xuf883b422016-08-30 14:01:10 -07001427 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1428 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1429 aom_highbd_8_sub_pixel_variance32x64,
1430 aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1431 aom_highbd_sad32x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001432
Yaowu Xuf883b422016-08-30 14:01:10 -07001433 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1434 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1435 aom_highbd_8_sub_pixel_variance32x32,
1436 aom_highbd_8_sub_pixel_avg_variance32x32,
1437 aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
1438 aom_highbd_sad32x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001439
Yaowu Xuf883b422016-08-30 14:01:10 -07001440 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1441 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1442 aom_highbd_8_sub_pixel_variance64x64,
1443 aom_highbd_8_sub_pixel_avg_variance64x64,
1444 aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
1445 aom_highbd_sad64x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001446
Yaowu Xuf883b422016-08-30 14:01:10 -07001447 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1448 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1449 aom_highbd_8_sub_pixel_variance16x16,
1450 aom_highbd_8_sub_pixel_avg_variance16x16,
1451 aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
1452 aom_highbd_sad16x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001453
1454 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001455 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1456 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1457 aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
1458 aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001459
1460 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001461 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1462 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1463 aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
1464 aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001465
1466 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001467 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1468 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1469 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits8,
1470 aom_highbd_sad8x8x8_bits8, aom_highbd_sad8x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001471
Yaowu Xuf883b422016-08-30 14:01:10 -07001472 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1473 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1474 aom_highbd_8_sub_pixel_variance8x4,
1475 aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
1476 aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001477
Yaowu Xuf883b422016-08-30 14:01:10 -07001478 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1479 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1480 aom_highbd_8_sub_pixel_variance4x8,
1481 aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
1482 aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001483
1484 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001485 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1486 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1487 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits8,
1488 aom_highbd_sad4x4x8_bits8, aom_highbd_sad4x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001489
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001490#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Hancc5bdf42016-12-19 11:14:30 -08001491 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_8_variance2x2, NULL, NULL,
1492 NULL, NULL, NULL)
1493 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_8_variance4x2, NULL, NULL,
1494 NULL, NULL, NULL)
1495 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_8_variance2x4, NULL, NULL,
1496 NULL, NULL, NULL)
1497#endif
1498
Yaowu Xuc27fc142016-08-22 16:08:15 -07001499#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001500 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1501 aom_highbd_sad128x128_avg_bits8,
1502 aom_highbd_8_variance128x128,
1503 aom_highbd_8_sub_pixel_variance128x128,
1504 aom_highbd_8_sub_pixel_avg_variance128x128,
1505 aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
1506 aom_highbd_sad128x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001507
Yaowu Xuf883b422016-08-30 14:01:10 -07001508 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1509 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1510 aom_highbd_8_sub_pixel_variance128x64,
1511 aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
1512 aom_highbd_sad128x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001513
Yaowu Xuf883b422016-08-30 14:01:10 -07001514 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1515 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1516 aom_highbd_8_sub_pixel_variance64x128,
1517 aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
1518 aom_highbd_sad64x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001519#endif // CONFIG_EXT_PARTITION
1520
1521#if CONFIG_EXT_INTER
1522#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001523 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1524 aom_highbd_8_masked_sub_pixel_variance128x128)
1525 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1526 aom_highbd_8_masked_sub_pixel_variance128x64)
1527 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1528 aom_highbd_8_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001529#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001530 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1531 aom_highbd_8_masked_sub_pixel_variance64x64)
1532 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1533 aom_highbd_8_masked_sub_pixel_variance64x32)
1534 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1535 aom_highbd_8_masked_sub_pixel_variance32x64)
1536 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1537 aom_highbd_8_masked_sub_pixel_variance32x32)
1538 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1539 aom_highbd_8_masked_sub_pixel_variance32x16)
1540 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1541 aom_highbd_8_masked_sub_pixel_variance16x32)
1542 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1543 aom_highbd_8_masked_sub_pixel_variance16x16)
1544 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1545 aom_highbd_8_masked_sub_pixel_variance8x16)
1546 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1547 aom_highbd_8_masked_sub_pixel_variance16x8)
1548 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1549 aom_highbd_8_masked_sub_pixel_variance8x8)
1550 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1551 aom_highbd_8_masked_sub_pixel_variance4x8)
1552 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1553 aom_highbd_8_masked_sub_pixel_variance8x4)
1554 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1555 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001556#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001557 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1558 aom_highbd_8_masked_sub_pixel_variance64x16)
1559
1560 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1561 aom_highbd_8_masked_sub_pixel_variance16x64)
1562
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001563 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1564 aom_highbd_8_masked_sub_pixel_variance32x8)
1565
1566 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1567 aom_highbd_8_masked_sub_pixel_variance8x32)
1568
1569 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1570 aom_highbd_8_masked_sub_pixel_variance16x4)
1571
1572 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1573 aom_highbd_8_masked_sub_pixel_variance4x16)
1574#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001575#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07001576#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001577#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001578 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1579 aom_highbd_obmc_variance128x128,
1580 aom_highbd_obmc_sub_pixel_variance128x128)
1581 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1582 aom_highbd_obmc_variance128x64,
1583 aom_highbd_obmc_sub_pixel_variance128x64)
1584 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1585 aom_highbd_obmc_variance64x128,
1586 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001587#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001588 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1589 aom_highbd_obmc_variance64x64,
1590 aom_highbd_obmc_sub_pixel_variance64x64)
1591 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1592 aom_highbd_obmc_variance64x32,
1593 aom_highbd_obmc_sub_pixel_variance64x32)
1594 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1595 aom_highbd_obmc_variance32x64,
1596 aom_highbd_obmc_sub_pixel_variance32x64)
1597 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1598 aom_highbd_obmc_variance32x32,
1599 aom_highbd_obmc_sub_pixel_variance32x32)
1600 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1601 aom_highbd_obmc_variance32x16,
1602 aom_highbd_obmc_sub_pixel_variance32x16)
1603 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1604 aom_highbd_obmc_variance16x32,
1605 aom_highbd_obmc_sub_pixel_variance16x32)
1606 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1607 aom_highbd_obmc_variance16x16,
1608 aom_highbd_obmc_sub_pixel_variance16x16)
1609 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1610 aom_highbd_obmc_variance8x16,
1611 aom_highbd_obmc_sub_pixel_variance8x16)
1612 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1613 aom_highbd_obmc_variance16x8,
1614 aom_highbd_obmc_sub_pixel_variance16x8)
1615 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1616 aom_highbd_obmc_variance8x8,
1617 aom_highbd_obmc_sub_pixel_variance8x8)
1618 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1619 aom_highbd_obmc_variance4x8,
1620 aom_highbd_obmc_sub_pixel_variance4x8)
1621 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1622 aom_highbd_obmc_variance8x4,
1623 aom_highbd_obmc_sub_pixel_variance8x4)
1624 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1625 aom_highbd_obmc_variance4x4,
1626 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001627#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001628 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1629 aom_highbd_obmc_variance64x16,
1630 aom_highbd_obmc_sub_pixel_variance64x16)
1631
1632 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1633 aom_highbd_obmc_variance16x64,
1634 aom_highbd_obmc_sub_pixel_variance16x64)
1635
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001636 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1637 aom_highbd_obmc_variance32x8,
1638 aom_highbd_obmc_sub_pixel_variance32x8)
1639
1640 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1641 aom_highbd_obmc_variance8x32,
1642 aom_highbd_obmc_sub_pixel_variance8x32)
1643
1644 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1645 aom_highbd_obmc_variance16x4,
1646 aom_highbd_obmc_sub_pixel_variance16x4)
1647
1648 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1649 aom_highbd_obmc_variance4x16,
1650 aom_highbd_obmc_sub_pixel_variance4x16)
1651#endif
Yue Chencb60b182016-10-13 15:18:22 -07001652#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001653 break;
1654
Yaowu Xuf883b422016-08-30 14:01:10 -07001655 case AOM_BITS_10:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001656#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001657 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1658 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1659 aom_highbd_10_sub_pixel_variance64x16,
1660 aom_highbd_10_sub_pixel_avg_variance64x16, NULL, NULL,
1661 aom_highbd_sad64x16x4d_bits10)
1662
1663 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1664 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1665 aom_highbd_10_sub_pixel_variance16x64,
1666 aom_highbd_10_sub_pixel_avg_variance16x64, NULL, NULL,
1667 aom_highbd_sad16x64x4d_bits10)
1668
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001669 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1670 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1671 aom_highbd_10_sub_pixel_variance32x8,
1672 aom_highbd_10_sub_pixel_avg_variance32x8, NULL, NULL,
1673 aom_highbd_sad32x8x4d_bits10)
1674
1675 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1676 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1677 aom_highbd_10_sub_pixel_variance8x32,
1678 aom_highbd_10_sub_pixel_avg_variance8x32, NULL, NULL,
1679 aom_highbd_sad8x32x4d_bits10)
1680
1681 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1682 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1683 aom_highbd_10_sub_pixel_variance16x4,
1684 aom_highbd_10_sub_pixel_avg_variance16x4, NULL, NULL,
1685 aom_highbd_sad16x4x4d_bits10)
1686
1687 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1688 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1689 aom_highbd_10_sub_pixel_variance4x16,
1690 aom_highbd_10_sub_pixel_avg_variance4x16, NULL, NULL,
1691 aom_highbd_sad4x16x4d_bits10)
1692#endif
1693
Yaowu Xuf883b422016-08-30 14:01:10 -07001694 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1695 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1696 aom_highbd_10_sub_pixel_variance32x16,
1697 aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1698 aom_highbd_sad32x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001699
Yaowu Xuf883b422016-08-30 14:01:10 -07001700 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1701 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1702 aom_highbd_10_sub_pixel_variance16x32,
1703 aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1704 aom_highbd_sad16x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001705
Yaowu Xuf883b422016-08-30 14:01:10 -07001706 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1707 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1708 aom_highbd_10_sub_pixel_variance64x32,
1709 aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1710 aom_highbd_sad64x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001711
Yaowu Xuf883b422016-08-30 14:01:10 -07001712 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1713 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1714 aom_highbd_10_sub_pixel_variance32x64,
1715 aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1716 aom_highbd_sad32x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001717
Yaowu Xuf883b422016-08-30 14:01:10 -07001718 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1719 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1720 aom_highbd_10_sub_pixel_variance32x32,
1721 aom_highbd_10_sub_pixel_avg_variance32x32,
1722 aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
1723 aom_highbd_sad32x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001724
Yaowu Xuf883b422016-08-30 14:01:10 -07001725 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1726 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1727 aom_highbd_10_sub_pixel_variance64x64,
1728 aom_highbd_10_sub_pixel_avg_variance64x64,
1729 aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
1730 aom_highbd_sad64x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001731
Yaowu Xuf883b422016-08-30 14:01:10 -07001732 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1733 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1734 aom_highbd_10_sub_pixel_variance16x16,
1735 aom_highbd_10_sub_pixel_avg_variance16x16,
1736 aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
1737 aom_highbd_sad16x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001738
Yaowu Xuf883b422016-08-30 14:01:10 -07001739 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1740 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1741 aom_highbd_10_sub_pixel_variance16x8,
1742 aom_highbd_10_sub_pixel_avg_variance16x8,
1743 aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
1744 aom_highbd_sad16x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001745
Yaowu Xuf883b422016-08-30 14:01:10 -07001746 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1747 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1748 aom_highbd_10_sub_pixel_variance8x16,
1749 aom_highbd_10_sub_pixel_avg_variance8x16,
1750 aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
1751 aom_highbd_sad8x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001752
1753 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001754 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1755 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1756 aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
1757 aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001758
Yaowu Xuf883b422016-08-30 14:01:10 -07001759 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
1760 aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
1761 aom_highbd_10_sub_pixel_variance8x4,
1762 aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
1763 aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001764
Yaowu Xuf883b422016-08-30 14:01:10 -07001765 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
1766 aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
1767 aom_highbd_10_sub_pixel_variance4x8,
1768 aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
1769 aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001770
1771 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001772 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1773 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1774 aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
1775 aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001776
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001777#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Hancc5bdf42016-12-19 11:14:30 -08001778 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_10_variance2x2, NULL, NULL,
1779 NULL, NULL, NULL)
1780 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_10_variance4x2, NULL, NULL,
1781 NULL, NULL, NULL)
1782 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_10_variance2x4, NULL, NULL,
1783 NULL, NULL, NULL)
1784#endif
1785
Yaowu Xuc27fc142016-08-22 16:08:15 -07001786#if CONFIG_EXT_PARTITION
1787 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001788 BLOCK_128X128, aom_highbd_sad128x128_bits10,
1789 aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
1790 aom_highbd_10_sub_pixel_variance128x128,
1791 aom_highbd_10_sub_pixel_avg_variance128x128,
1792 aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
1793 aom_highbd_sad128x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001794
Yaowu Xuf883b422016-08-30 14:01:10 -07001795 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1796 aom_highbd_sad128x64_avg_bits10,
1797 aom_highbd_10_variance128x64,
1798 aom_highbd_10_sub_pixel_variance128x64,
1799 aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
1800 aom_highbd_sad128x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001801
Yaowu Xuf883b422016-08-30 14:01:10 -07001802 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1803 aom_highbd_sad64x128_avg_bits10,
1804 aom_highbd_10_variance64x128,
1805 aom_highbd_10_sub_pixel_variance64x128,
1806 aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
1807 aom_highbd_sad64x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001808#endif // CONFIG_EXT_PARTITION
1809
1810#if CONFIG_EXT_INTER
1811#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001812 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1813 aom_highbd_10_masked_sub_pixel_variance128x128)
1814 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1815 aom_highbd_10_masked_sub_pixel_variance128x64)
1816 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1817 aom_highbd_10_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001818#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001819 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1820 aom_highbd_10_masked_sub_pixel_variance64x64)
1821 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1822 aom_highbd_10_masked_sub_pixel_variance64x32)
1823 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1824 aom_highbd_10_masked_sub_pixel_variance32x64)
1825 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1826 aom_highbd_10_masked_sub_pixel_variance32x32)
1827 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1828 aom_highbd_10_masked_sub_pixel_variance32x16)
1829 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1830 aom_highbd_10_masked_sub_pixel_variance16x32)
1831 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1832 aom_highbd_10_masked_sub_pixel_variance16x16)
1833 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1834 aom_highbd_10_masked_sub_pixel_variance8x16)
1835 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1836 aom_highbd_10_masked_sub_pixel_variance16x8)
1837 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1838 aom_highbd_10_masked_sub_pixel_variance8x8)
1839 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1840 aom_highbd_10_masked_sub_pixel_variance4x8)
1841 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1842 aom_highbd_10_masked_sub_pixel_variance8x4)
1843 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1844 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001845#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001846 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1847 aom_highbd_10_masked_sub_pixel_variance64x16)
1848
1849 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1850 aom_highbd_10_masked_sub_pixel_variance16x64)
1851
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001852 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1853 aom_highbd_10_masked_sub_pixel_variance32x8)
1854
1855 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1856 aom_highbd_10_masked_sub_pixel_variance8x32)
1857
1858 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1859 aom_highbd_10_masked_sub_pixel_variance16x4)
1860
1861 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1862 aom_highbd_10_masked_sub_pixel_variance4x16)
1863#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001864#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07001865#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001866#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001867 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1868 aom_highbd_10_obmc_variance128x128,
1869 aom_highbd_10_obmc_sub_pixel_variance128x128)
1870 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1871 aom_highbd_10_obmc_variance128x64,
1872 aom_highbd_10_obmc_sub_pixel_variance128x64)
1873 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1874 aom_highbd_10_obmc_variance64x128,
1875 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001876#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001877 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1878 aom_highbd_10_obmc_variance64x64,
1879 aom_highbd_10_obmc_sub_pixel_variance64x64)
1880 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1881 aom_highbd_10_obmc_variance64x32,
1882 aom_highbd_10_obmc_sub_pixel_variance64x32)
1883 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
1884 aom_highbd_10_obmc_variance32x64,
1885 aom_highbd_10_obmc_sub_pixel_variance32x64)
1886 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
1887 aom_highbd_10_obmc_variance32x32,
1888 aom_highbd_10_obmc_sub_pixel_variance32x32)
1889 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
1890 aom_highbd_10_obmc_variance32x16,
1891 aom_highbd_10_obmc_sub_pixel_variance32x16)
1892 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
1893 aom_highbd_10_obmc_variance16x32,
1894 aom_highbd_10_obmc_sub_pixel_variance16x32)
1895 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
1896 aom_highbd_10_obmc_variance16x16,
1897 aom_highbd_10_obmc_sub_pixel_variance16x16)
1898 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
1899 aom_highbd_10_obmc_variance8x16,
1900 aom_highbd_10_obmc_sub_pixel_variance8x16)
1901 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
1902 aom_highbd_10_obmc_variance16x8,
1903 aom_highbd_10_obmc_sub_pixel_variance16x8)
1904 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
1905 aom_highbd_10_obmc_variance8x8,
1906 aom_highbd_10_obmc_sub_pixel_variance8x8)
1907 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
1908 aom_highbd_10_obmc_variance4x8,
1909 aom_highbd_10_obmc_sub_pixel_variance4x8)
1910 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
1911 aom_highbd_10_obmc_variance8x4,
1912 aom_highbd_10_obmc_sub_pixel_variance8x4)
1913 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
1914 aom_highbd_10_obmc_variance4x4,
1915 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001916#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001917 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
1918 aom_highbd_10_obmc_variance64x16,
1919 aom_highbd_10_obmc_sub_pixel_variance64x16)
1920
1921 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
1922 aom_highbd_10_obmc_variance16x64,
1923 aom_highbd_10_obmc_sub_pixel_variance16x64)
1924
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001925 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
1926 aom_highbd_10_obmc_variance32x8,
1927 aom_highbd_10_obmc_sub_pixel_variance32x8)
1928
1929 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
1930 aom_highbd_10_obmc_variance8x32,
1931 aom_highbd_10_obmc_sub_pixel_variance8x32)
1932
1933 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
1934 aom_highbd_10_obmc_variance16x4,
1935 aom_highbd_10_obmc_sub_pixel_variance16x4)
1936
1937 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
1938 aom_highbd_10_obmc_variance4x16,
1939 aom_highbd_10_obmc_sub_pixel_variance4x16)
1940#endif
Yue Chencb60b182016-10-13 15:18:22 -07001941#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001942 break;
1943
Yaowu Xuf883b422016-08-30 14:01:10 -07001944 case AOM_BITS_12:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001945#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001946 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
1947 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
1948 aom_highbd_12_sub_pixel_variance64x16,
1949 aom_highbd_12_sub_pixel_avg_variance64x16, NULL, NULL,
1950 aom_highbd_sad64x16x4d_bits12)
1951
1952 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
1953 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
1954 aom_highbd_12_sub_pixel_variance16x64,
1955 aom_highbd_12_sub_pixel_avg_variance16x64, NULL, NULL,
1956 aom_highbd_sad16x64x4d_bits12)
1957
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001958 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
1959 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
1960 aom_highbd_12_sub_pixel_variance32x8,
1961 aom_highbd_12_sub_pixel_avg_variance32x8, NULL, NULL,
1962 aom_highbd_sad32x8x4d_bits12)
1963
1964 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
1965 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
1966 aom_highbd_12_sub_pixel_variance8x32,
1967 aom_highbd_12_sub_pixel_avg_variance8x32, NULL, NULL,
1968 aom_highbd_sad8x32x4d_bits12)
1969
1970 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
1971 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
1972 aom_highbd_12_sub_pixel_variance16x4,
1973 aom_highbd_12_sub_pixel_avg_variance16x4, NULL, NULL,
1974 aom_highbd_sad16x4x4d_bits12)
1975
1976 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
1977 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
1978 aom_highbd_12_sub_pixel_variance4x16,
1979 aom_highbd_12_sub_pixel_avg_variance4x16, NULL, NULL,
1980 aom_highbd_sad4x16x4d_bits12)
1981#endif
1982
Yaowu Xuf883b422016-08-30 14:01:10 -07001983 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
1984 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
1985 aom_highbd_12_sub_pixel_variance32x16,
1986 aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
1987 aom_highbd_sad32x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988
Yaowu Xuf883b422016-08-30 14:01:10 -07001989 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
1990 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
1991 aom_highbd_12_sub_pixel_variance16x32,
1992 aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
1993 aom_highbd_sad16x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001994
Yaowu Xuf883b422016-08-30 14:01:10 -07001995 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
1996 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
1997 aom_highbd_12_sub_pixel_variance64x32,
1998 aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
1999 aom_highbd_sad64x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002000
Yaowu Xuf883b422016-08-30 14:01:10 -07002001 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2002 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2003 aom_highbd_12_sub_pixel_variance32x64,
2004 aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
2005 aom_highbd_sad32x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002006
Yaowu Xuf883b422016-08-30 14:01:10 -07002007 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2008 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2009 aom_highbd_12_sub_pixel_variance32x32,
2010 aom_highbd_12_sub_pixel_avg_variance32x32,
2011 aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
2012 aom_highbd_sad32x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002013
Yaowu Xuf883b422016-08-30 14:01:10 -07002014 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2015 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2016 aom_highbd_12_sub_pixel_variance64x64,
2017 aom_highbd_12_sub_pixel_avg_variance64x64,
2018 aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
2019 aom_highbd_sad64x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002020
Yaowu Xuf883b422016-08-30 14:01:10 -07002021 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2022 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2023 aom_highbd_12_sub_pixel_variance16x16,
2024 aom_highbd_12_sub_pixel_avg_variance16x16,
2025 aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
2026 aom_highbd_sad16x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002027
Yaowu Xuf883b422016-08-30 14:01:10 -07002028 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2029 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2030 aom_highbd_12_sub_pixel_variance16x8,
2031 aom_highbd_12_sub_pixel_avg_variance16x8,
2032 aom_highbd_sad16x8x3_bits12, aom_highbd_sad16x8x8_bits12,
2033 aom_highbd_sad16x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002034
Yaowu Xuf883b422016-08-30 14:01:10 -07002035 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2036 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2037 aom_highbd_12_sub_pixel_variance8x16,
2038 aom_highbd_12_sub_pixel_avg_variance8x16,
2039 aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
2040 aom_highbd_sad8x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002041
2042 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002043 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2044 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
2045 aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
2046 aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002047
Yaowu Xuf883b422016-08-30 14:01:10 -07002048 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
2049 aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
2050 aom_highbd_12_sub_pixel_variance8x4,
2051 aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
2052 aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002053
Yaowu Xuf883b422016-08-30 14:01:10 -07002054 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
2055 aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
2056 aom_highbd_12_sub_pixel_variance4x8,
2057 aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
2058 aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002059
2060 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002061 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2062 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
2063 aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
2064 aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002065
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07002066#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Hancc5bdf42016-12-19 11:14:30 -08002067 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_12_variance2x2, NULL, NULL,
2068 NULL, NULL, NULL)
2069 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_12_variance4x2, NULL, NULL,
2070 NULL, NULL, NULL)
2071 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_12_variance2x4, NULL, NULL,
2072 NULL, NULL, NULL)
2073#endif
2074
Yaowu Xuc27fc142016-08-22 16:08:15 -07002075#if CONFIG_EXT_PARTITION
2076 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002077 BLOCK_128X128, aom_highbd_sad128x128_bits12,
2078 aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
2079 aom_highbd_12_sub_pixel_variance128x128,
2080 aom_highbd_12_sub_pixel_avg_variance128x128,
2081 aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
2082 aom_highbd_sad128x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002083
Yaowu Xuf883b422016-08-30 14:01:10 -07002084 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2085 aom_highbd_sad128x64_avg_bits12,
2086 aom_highbd_12_variance128x64,
2087 aom_highbd_12_sub_pixel_variance128x64,
2088 aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
2089 aom_highbd_sad128x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002090
Yaowu Xuf883b422016-08-30 14:01:10 -07002091 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2092 aom_highbd_sad64x128_avg_bits12,
2093 aom_highbd_12_variance64x128,
2094 aom_highbd_12_sub_pixel_variance64x128,
2095 aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
2096 aom_highbd_sad64x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002097#endif // CONFIG_EXT_PARTITION
2098
2099#if CONFIG_EXT_INTER
2100#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002101 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2102 aom_highbd_12_masked_sub_pixel_variance128x128)
2103 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2104 aom_highbd_12_masked_sub_pixel_variance128x64)
2105 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2106 aom_highbd_12_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002107#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002108 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2109 aom_highbd_12_masked_sub_pixel_variance64x64)
2110 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2111 aom_highbd_12_masked_sub_pixel_variance64x32)
2112 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2113 aom_highbd_12_masked_sub_pixel_variance32x64)
2114 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2115 aom_highbd_12_masked_sub_pixel_variance32x32)
2116 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2117 aom_highbd_12_masked_sub_pixel_variance32x16)
2118 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2119 aom_highbd_12_masked_sub_pixel_variance16x32)
2120 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2121 aom_highbd_12_masked_sub_pixel_variance16x16)
2122 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2123 aom_highbd_12_masked_sub_pixel_variance8x16)
2124 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2125 aom_highbd_12_masked_sub_pixel_variance16x8)
2126 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2127 aom_highbd_12_masked_sub_pixel_variance8x8)
2128 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2129 aom_highbd_12_masked_sub_pixel_variance4x8)
2130 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2131 aom_highbd_12_masked_sub_pixel_variance8x4)
2132 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2133 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002134#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01002135 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2136 aom_highbd_12_masked_sub_pixel_variance64x16)
2137
2138 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2139 aom_highbd_12_masked_sub_pixel_variance16x64)
2140
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002141 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2142 aom_highbd_12_masked_sub_pixel_variance32x8)
2143
2144 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2145 aom_highbd_12_masked_sub_pixel_variance8x32)
2146
2147 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2148 aom_highbd_12_masked_sub_pixel_variance16x4)
2149
2150 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2151 aom_highbd_12_masked_sub_pixel_variance4x16)
2152#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002153#endif // CONFIG_EXT_INTER
2154
Yue Chencb60b182016-10-13 15:18:22 -07002155#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002156#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002157 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2158 aom_highbd_12_obmc_variance128x128,
2159 aom_highbd_12_obmc_sub_pixel_variance128x128)
2160 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2161 aom_highbd_12_obmc_variance128x64,
2162 aom_highbd_12_obmc_sub_pixel_variance128x64)
2163 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2164 aom_highbd_12_obmc_variance64x128,
2165 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002166#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002167 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2168 aom_highbd_12_obmc_variance64x64,
2169 aom_highbd_12_obmc_sub_pixel_variance64x64)
2170 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2171 aom_highbd_12_obmc_variance64x32,
2172 aom_highbd_12_obmc_sub_pixel_variance64x32)
2173 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2174 aom_highbd_12_obmc_variance32x64,
2175 aom_highbd_12_obmc_sub_pixel_variance32x64)
2176 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2177 aom_highbd_12_obmc_variance32x32,
2178 aom_highbd_12_obmc_sub_pixel_variance32x32)
2179 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2180 aom_highbd_12_obmc_variance32x16,
2181 aom_highbd_12_obmc_sub_pixel_variance32x16)
2182 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2183 aom_highbd_12_obmc_variance16x32,
2184 aom_highbd_12_obmc_sub_pixel_variance16x32)
2185 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2186 aom_highbd_12_obmc_variance16x16,
2187 aom_highbd_12_obmc_sub_pixel_variance16x16)
2188 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2189 aom_highbd_12_obmc_variance8x16,
2190 aom_highbd_12_obmc_sub_pixel_variance8x16)
2191 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2192 aom_highbd_12_obmc_variance16x8,
2193 aom_highbd_12_obmc_sub_pixel_variance16x8)
2194 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2195 aom_highbd_12_obmc_variance8x8,
2196 aom_highbd_12_obmc_sub_pixel_variance8x8)
2197 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2198 aom_highbd_12_obmc_variance4x8,
2199 aom_highbd_12_obmc_sub_pixel_variance4x8)
2200 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2201 aom_highbd_12_obmc_variance8x4,
2202 aom_highbd_12_obmc_sub_pixel_variance8x4)
2203 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2204 aom_highbd_12_obmc_variance4x4,
2205 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002206#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01002207 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2208 aom_highbd_12_obmc_variance64x16,
2209 aom_highbd_12_obmc_sub_pixel_variance64x16)
2210
2211 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2212 aom_highbd_12_obmc_variance16x64,
2213 aom_highbd_12_obmc_sub_pixel_variance16x64)
2214
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002215 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2216 aom_highbd_12_obmc_variance32x8,
2217 aom_highbd_12_obmc_sub_pixel_variance32x8)
2218
2219 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2220 aom_highbd_12_obmc_variance8x32,
2221 aom_highbd_12_obmc_sub_pixel_variance8x32)
2222
2223 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2224 aom_highbd_12_obmc_variance16x4,
2225 aom_highbd_12_obmc_sub_pixel_variance16x4)
2226
2227 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2228 aom_highbd_12_obmc_variance4x16,
2229 aom_highbd_12_obmc_sub_pixel_variance4x16)
2230#endif
Yue Chencb60b182016-10-13 15:18:22 -07002231#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002232 break;
2233
2234 default:
2235 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07002236 "cm->bit_depth should be AOM_BITS_8, "
2237 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002238 }
2239 }
2240}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002241#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002242
Yaowu Xuf883b422016-08-30 14:01:10 -07002243static void realloc_segmentation_maps(AV1_COMP *cpi) {
2244 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002245
2246 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002247 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002248 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002249 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002250
2251 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002252 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002253 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002254 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002255
2256 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002257 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002258 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002259 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002260}
2261
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002262#if CONFIG_EXT_INTER
2263void set_compound_tools(AV1_COMMON *cm) {
2264 (void)cm;
2265#if CONFIG_INTERINTRA
2266 cm->allow_interintra_compound = 1;
2267#endif // CONFIG_INTERINTRA
2268#if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
2269 cm->allow_masked_compound = 1;
2270#endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
2271}
2272#endif // CONFIG_EXT_INTER
2273
Yaowu Xuf883b422016-08-30 14:01:10 -07002274void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2275 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002276 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002277 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002278
2279 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
2280 cm->bit_depth = oxcf->bit_depth;
2281 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07002282#if CONFIG_COLORSPACE_HEADERS
2283 cm->transfer_function = oxcf->transfer_function;
2284 cm->chroma_sample_position = oxcf->chroma_sample_position;
2285#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002286 cm->color_range = oxcf->color_range;
2287
2288 if (cm->profile <= PROFILE_1)
Yaowu Xuf883b422016-08-30 14:01:10 -07002289 assert(cm->bit_depth == AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002290 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002291 assert(cm->bit_depth > AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002292
2293 cpi->oxcf = *oxcf;
hui sud9a812b2017-07-06 14:34:37 -07002294 x->e_mbd.bd = (int)cm->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002295#if CONFIG_GLOBAL_MOTION
hui sud9a812b2017-07-06 14:34:37 -07002296 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002297#endif // CONFIG_GLOBAL_MOTION
2298
Yaowu Xuf883b422016-08-30 14:01:10 -07002299 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002300 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2301 } else {
2302 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2303 }
2304
2305 cpi->refresh_last_frame = 1;
2306 cpi->refresh_golden_frame = 0;
2307#if CONFIG_EXT_REFS
2308 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002309#if CONFIG_ALTREF2
2310 cpi->refresh_alt2_ref_frame = 0;
2311#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07002312#endif // CONFIG_EXT_REFS
2313
2314 cm->refresh_frame_context =
2315 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
2316 ? REFRESH_FRAME_CONTEXT_FORWARD
2317 : REFRESH_FRAME_CONTEXT_BACKWARD;
Thomas Daedea6a854b2017-06-22 17:49:11 -07002318#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07002319 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07002320#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002321
Alex Converse74ad0912017-07-18 10:22:58 -07002322 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002323 CHECK_MEM_ERROR(cm, x->palette_buffer,
2324 aom_memalign(16, sizeof(*x->palette_buffer)));
2325 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002326#if CONFIG_EXT_INTER
2327 set_compound_tools(cm);
2328#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002329 av1_reset_segment_features(cm);
2330 av1_set_high_precision_mv(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002331
Yaowu Xuc27fc142016-08-22 16:08:15 -07002332 set_rc_buffer_sizes(rc, &cpi->oxcf);
2333
2334 // Under a configuration change, where maximum_buffer_size may change,
2335 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002336 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2337 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002338
2339 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002340 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002341
2342 // Set absolute upper and lower quality limits
2343 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2344 rc->best_quality = cpi->oxcf.best_allowed_q;
2345
2346 cm->interp_filter = cpi->sf.default_interp_filter;
2347
2348 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2349 cm->render_width = cpi->oxcf.render_width;
2350 cm->render_height = cpi->oxcf.render_height;
2351 } else {
2352 cm->render_width = cpi->oxcf.width;
2353 cm->render_height = cpi->oxcf.height;
2354 }
2355 cm->width = cpi->oxcf.width;
2356 cm->height = cpi->oxcf.height;
2357
2358 if (cpi->initial_width) {
2359 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002360 av1_free_context_buffers(cm);
2361 av1_alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002362 realloc_segmentation_maps(cpi);
2363 cpi->initial_width = cpi->initial_height = 0;
2364 }
2365 }
2366 update_frame_size(cpi);
2367
2368 cpi->alt_ref_source = NULL;
2369 rc->is_src_frame_alt_ref = 0;
2370
2371#if CONFIG_EXT_REFS
2372 rc->is_bwd_ref_frame = 0;
2373 rc->is_last_bipred_frame = 0;
2374 rc->is_bipred_frame = 0;
2375#endif // CONFIG_EXT_REFS
2376
2377#if 0
2378 // Experimental RD Code
2379 cpi->frame_distortion = 0;
2380 cpi->last_frame_distortion = 0;
2381#endif
2382
2383 set_tile_info(cpi);
2384
2385 cpi->ext_refresh_frame_flags_pending = 0;
2386 cpi->ext_refresh_frame_context_pending = 0;
2387
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002388#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002389 highbd_set_var_fns(cpi);
2390#endif
Alex Converseeb780e72016-12-13 12:46:41 -08002391#if CONFIG_ANS && ANS_MAX_SYMBOLS
2392 cpi->common.ans_window_size_log2 = cpi->oxcf.ans_window_size_log2;
Alex Converseeb780e72016-12-13 12:46:41 -08002393#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002394}
2395
Yaowu Xuf883b422016-08-30 14:01:10 -07002396AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2397 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002398 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002399 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2400 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002401
2402 if (!cm) return NULL;
2403
Yaowu Xuf883b422016-08-30 14:01:10 -07002404 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002405
2406 if (setjmp(cm->error.jmp)) {
2407 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002408 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002409 return 0;
2410 }
2411
2412 cm->error.setjmp = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002413 cm->alloc_mi = av1_enc_alloc_mi;
2414 cm->free_mi = av1_enc_free_mi;
2415 cm->setup_mi = av1_enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002416
Angie Chianga5d96c42016-10-21 16:16:56 -07002417 CHECK_MEM_ERROR(cm, cm->fc,
2418 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2419 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2420 (FRAME_CONTEXT *)aom_memalign(
2421 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2422 memset(cm->fc, 0, sizeof(*cm->fc));
2423 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002424
2425 cpi->resize_state = 0;
2426 cpi->resize_avg_qp = 0;
2427 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002428
Yaowu Xuc27fc142016-08-22 16:08:15 -07002429 cpi->common.buffer_pool = pool;
2430
2431 init_config(cpi, oxcf);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002432#if CONFIG_XIPHRC
2433 cpi->od_rc.framerate = cpi->framerate;
2434 cpi->od_rc.frame_width = cm->render_width;
2435 cpi->od_rc.frame_height = cm->render_height;
2436 cpi->od_rc.keyframe_rate = oxcf->key_freq;
2437 cpi->od_rc.goldenframe_rate = FIXED_GF_INTERVAL;
2438 cpi->od_rc.altref_rate = 25;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002439 cpi->od_rc.firstpass_quant = 1;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002440 cpi->od_rc.bit_depth = cm->bit_depth;
2441 cpi->od_rc.minq = oxcf->best_allowed_q;
2442 cpi->od_rc.maxq = oxcf->worst_allowed_q;
2443 if (cpi->oxcf.rc_mode == AOM_CQ) cpi->od_rc.minq = cpi->od_rc.quality;
2444 cpi->od_rc.quality = cpi->oxcf.rc_mode == AOM_Q ? oxcf->cq_level : -1;
2445 cpi->od_rc.periodic_boosts = oxcf->frame_periodic_boost;
2446 od_enc_rc_init(&cpi->od_rc,
2447 cpi->oxcf.rc_mode == AOM_Q ? -1 : oxcf->target_bandwidth,
2448 oxcf->maximum_buffer_size_ms);
2449#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002450 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002451#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452
2453 cm->current_video_frame = 0;
2454 cpi->partition_search_skippable_frame = 0;
2455 cpi->tile_data = NULL;
2456 cpi->last_show_frame_buf_idx = INVALID_IDX;
2457
2458 realloc_segmentation_maps(cpi);
2459
Yaowu Xuc27fc142016-08-22 16:08:15 -07002460 for (i = 0; i < NMV_CONTEXTS; ++i) {
Urvang Joshibffc0b52016-07-25 13:38:49 -07002461 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2462 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002463 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002464
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2466 i++) {
2467 CHECK_MEM_ERROR(
2468 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002469 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002470 }
2471
2472#if CONFIG_FP_MB_STATS
2473 cpi->use_fp_mb_stats = 0;
2474 if (cpi->use_fp_mb_stats) {
2475 // a place holder used to store the first pass mb stats in the first pass
2476 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002477 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002478 } else {
2479 cpi->twopass.frame_mb_stats_buf = NULL;
2480 }
2481#endif
2482
2483 cpi->refresh_alt_ref_frame = 0;
2484 cpi->multi_arf_last_grp_enabled = 0;
2485
2486 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2487#if CONFIG_INTERNAL_STATS
2488 cpi->b_calculate_blockiness = 1;
2489 cpi->b_calculate_consistency = 1;
2490 cpi->total_inconsistency = 0;
2491 cpi->psnr.worst = 100.0;
2492 cpi->worst_ssim = 100.0;
2493
2494 cpi->count = 0;
2495 cpi->bytes = 0;
2496
2497 if (cpi->b_calculate_psnr) {
2498 cpi->total_sq_error = 0;
2499 cpi->total_samples = 0;
2500 cpi->tot_recode_hits = 0;
2501 cpi->summed_quality = 0;
2502 cpi->summed_weights = 0;
2503 }
2504
2505 cpi->fastssim.worst = 100.0;
2506 cpi->psnrhvs.worst = 100.0;
2507
2508 if (cpi->b_calculate_blockiness) {
2509 cpi->total_blockiness = 0;
2510 cpi->worst_blockiness = 0.0;
2511 }
2512
2513 if (cpi->b_calculate_consistency) {
2514 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002515 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002516 cpi->common.mi_rows * cpi->common.mi_cols));
2517 cpi->worst_consistency = 100.0;
2518 }
2519#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002520#if CONFIG_ENTROPY_STATS
2521 av1_zero(aggregate_fc);
Zoe Liua56f9162017-06-21 22:49:57 -07002522 av1_zero_array(aggregate_fc_per_type, FRAME_CONTEXTS);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002523#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002524
2525 cpi->first_time_stamp_ever = INT64_MAX;
2526
Yaowu Xuc27fc142016-08-22 16:08:15 -07002527 for (i = 0; i < NMV_CONTEXTS; ++i) {
2528 cpi->td.mb.nmvcost[i][0] = &cpi->nmv_costs[i][0][MV_MAX];
2529 cpi->td.mb.nmvcost[i][1] = &cpi->nmv_costs[i][1][MV_MAX];
2530 cpi->td.mb.nmvcost_hp[i][0] = &cpi->nmv_costs_hp[i][0][MV_MAX];
2531 cpi->td.mb.nmvcost_hp[i][1] = &cpi->nmv_costs_hp[i][1][MV_MAX];
2532 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002533
2534#ifdef OUTPUT_YUV_SKINMAP
2535 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2536#endif
2537#ifdef OUTPUT_YUV_REC
2538 yuv_rec_file = fopen("rec.yuv", "wb");
2539#endif
2540
2541#if 0
2542 framepsnr = fopen("framepsnr.stt", "a");
2543 kf_list = fopen("kf_list.stt", "w");
2544#endif
2545
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002546#if CONFIG_XIPHRC
2547 if (oxcf->pass == 2) {
2548 cpi->od_rc.twopass_allframes_buf = oxcf->two_pass_stats_in.buf;
2549 cpi->od_rc.twopass_allframes_buf_size = oxcf->two_pass_stats_in.sz;
2550 }
2551#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002552 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002553 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002554 } else if (oxcf->pass == 2) {
2555 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2556 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2557
2558#if CONFIG_FP_MB_STATS
2559 if (cpi->use_fp_mb_stats) {
2560 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2561 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2562
2563 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2564 oxcf->firstpass_mb_stats_in.buf;
2565 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2566 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2567 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2568 }
2569#endif
2570
2571 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2572 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2573 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2574
Yaowu Xuf883b422016-08-30 14:01:10 -07002575 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002576 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002577#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002578
Jingning Hand064cf02017-06-01 10:00:39 -07002579#if CONFIG_MOTION_VAR
2580#if CONFIG_HIGHBITDEPTH
2581 int buf_scaler = 2;
2582#else
2583 int buf_scaler = 1;
2584#endif
2585 CHECK_MEM_ERROR(
2586 cm, cpi->td.mb.above_pred_buf,
2587 (uint8_t *)aom_memalign(16, buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2588 sizeof(*cpi->td.mb.above_pred_buf)));
2589 CHECK_MEM_ERROR(
2590 cm, cpi->td.mb.left_pred_buf,
2591 (uint8_t *)aom_memalign(16, buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2592 sizeof(*cpi->td.mb.left_pred_buf)));
2593
2594 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2595 (int32_t *)aom_memalign(
2596 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2597
2598 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2599 (int32_t *)aom_memalign(
2600 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2601
2602#endif
2603
Yaowu Xuf883b422016-08-30 14:01:10 -07002604 av1_set_speed_features_framesize_independent(cpi);
2605 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002606
Yaowu Xuc27fc142016-08-22 16:08:15 -07002607#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
2608 cpi->fn_ptr[BT].sdf = SDF; \
2609 cpi->fn_ptr[BT].sdaf = SDAF; \
2610 cpi->fn_ptr[BT].vf = VF; \
2611 cpi->fn_ptr[BT].svf = SVF; \
2612 cpi->fn_ptr[BT].svaf = SVAF; \
2613 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2614 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2615 cpi->fn_ptr[BT].sdx4df = SDX4DF;
2616
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002617#if CONFIG_EXT_PARTITION_TYPES
2618 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2619 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, NULL, NULL,
2620 aom_sad4x16x4d)
2621
2622 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2623 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, NULL, NULL,
2624 aom_sad16x4x4d)
2625
2626 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2627 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, NULL, NULL,
2628 aom_sad8x32x4d)
2629
2630 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2631 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, NULL, NULL,
2632 aom_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002633
2634 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2635 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, NULL, NULL,
2636 aom_sad16x64x4d)
2637
2638 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2639 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, NULL, NULL,
2640 aom_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002641#endif
2642
Yaowu Xuc27fc142016-08-22 16:08:15 -07002643#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002644 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2645 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2646 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002647
Yaowu Xuf883b422016-08-30 14:01:10 -07002648 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2649 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
2650 NULL, aom_sad128x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002651
Yaowu Xuf883b422016-08-30 14:01:10 -07002652 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2653 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
2654 NULL, aom_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655#endif // CONFIG_EXT_PARTITION
2656
Yaowu Xuf883b422016-08-30 14:01:10 -07002657 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2658 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2659 aom_sad32x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002660
Yaowu Xuf883b422016-08-30 14:01:10 -07002661 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2662 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2663 aom_sad16x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002664
Yaowu Xuf883b422016-08-30 14:01:10 -07002665 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2666 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2667 aom_sad64x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002668
Yaowu Xuf883b422016-08-30 14:01:10 -07002669 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2670 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2671 aom_sad32x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672
Yaowu Xuf883b422016-08-30 14:01:10 -07002673 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2674 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2675 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002676
Yaowu Xuf883b422016-08-30 14:01:10 -07002677 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2678 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2679 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002680
Yaowu Xuf883b422016-08-30 14:01:10 -07002681 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2682 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2683 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002684
Yaowu Xuf883b422016-08-30 14:01:10 -07002685 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2686 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
2687 aom_sad16x8x8, aom_sad16x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002688
Yaowu Xuf883b422016-08-30 14:01:10 -07002689 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2690 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
2691 aom_sad8x16x8, aom_sad8x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002692
Yaowu Xuf883b422016-08-30 14:01:10 -07002693 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2694 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
2695 aom_sad8x8x8, aom_sad8x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002696
Yaowu Xuf883b422016-08-30 14:01:10 -07002697 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2698 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
2699 aom_sad8x4x8, aom_sad8x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002700
Yaowu Xuf883b422016-08-30 14:01:10 -07002701 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2702 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
2703 aom_sad4x8x8, aom_sad4x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002704
Yaowu Xuf883b422016-08-30 14:01:10 -07002705 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2706 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
2707 aom_sad4x4x8, aom_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002708
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07002709#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Han9e7c49f2016-12-06 11:20:10 -08002710 BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL)
Jingning Hane2ffaf82016-12-14 15:26:30 -08002711 BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL)
2712 BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL)
Jingning Han9e7c49f2016-12-06 11:20:10 -08002713#endif
2714
Yue Chencb60b182016-10-13 15:18:22 -07002715#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002716#define OBFP(BT, OSDF, OVF, OSVF) \
2717 cpi->fn_ptr[BT].osdf = OSDF; \
2718 cpi->fn_ptr[BT].ovf = OVF; \
2719 cpi->fn_ptr[BT].osvf = OSVF;
2720
2721#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002722 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2723 aom_obmc_sub_pixel_variance128x128)
2724 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2725 aom_obmc_sub_pixel_variance128x64)
2726 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2727 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002728#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002729 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2730 aom_obmc_sub_pixel_variance64x64)
2731 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2732 aom_obmc_sub_pixel_variance64x32)
2733 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2734 aom_obmc_sub_pixel_variance32x64)
2735 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2736 aom_obmc_sub_pixel_variance32x32)
2737 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2738 aom_obmc_sub_pixel_variance32x16)
2739 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2740 aom_obmc_sub_pixel_variance16x32)
2741 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2742 aom_obmc_sub_pixel_variance16x16)
2743 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2744 aom_obmc_sub_pixel_variance16x8)
2745 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2746 aom_obmc_sub_pixel_variance8x16)
2747 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2748 aom_obmc_sub_pixel_variance8x8)
2749 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2750 aom_obmc_sub_pixel_variance4x8)
2751 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2752 aom_obmc_sub_pixel_variance8x4)
2753 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2754 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002755
2756#if CONFIG_EXT_PARTITION_TYPES
2757 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2758 aom_obmc_sub_pixel_variance4x16)
2759
2760 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2761 aom_obmc_sub_pixel_variance16x4)
2762
2763 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2764 aom_obmc_sub_pixel_variance8x32)
2765
2766 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2767 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002768
2769 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2770 aom_obmc_sub_pixel_variance16x64)
2771
2772 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2773 aom_obmc_sub_pixel_variance64x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002774#endif
Yue Chencb60b182016-10-13 15:18:22 -07002775#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002776
2777#if CONFIG_EXT_INTER
David Barkerf19f35f2017-05-22 16:33:22 +01002778#define MBFP(BT, MCSDF, MCSVF) \
2779 cpi->fn_ptr[BT].msdf = MCSDF; \
2780 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002781
2782#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002783 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2784 aom_masked_sub_pixel_variance128x128)
2785 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2786 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002787#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002788 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2789 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2790 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2791 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2792 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2793 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2794 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2795 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2796 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2797 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2798 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2799 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2800 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002801
2802#if CONFIG_EXT_PARTITION_TYPES
2803 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2804
2805 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2806
2807 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2808
2809 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002810
2811 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2812
2813 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002814#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002815#endif // CONFIG_EXT_INTER
2816
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002817#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002818 highbd_set_var_fns(cpi);
2819#endif
2820
Yaowu Xuf883b422016-08-30 14:01:10 -07002821 /* av1_init_quantizer() is first called here. Add check in
2822 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002823 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002824 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002825 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002826 av1_init_quantizer(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002827#if CONFIG_AOM_QM
2828 aom_qm_init(cm);
2829#endif
2830
Yaowu Xuf883b422016-08-30 14:01:10 -07002831 av1_loop_filter_init(cm);
Fergus Simpsondac6aff2017-05-18 15:11:38 -07002832#if CONFIG_FRAME_SUPERRES
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07002833 cm->superres_scale_numerator = SCALE_DENOMINATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002834 cm->superres_upscaled_width = oxcf->width;
2835 cm->superres_upscaled_height = oxcf->height;
Fergus Simpsondac6aff2017-05-18 15:11:38 -07002836#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuc27fc142016-08-22 16:08:15 -07002837#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07002838 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002839#endif // CONFIG_LOOP_RESTORATION
2840
2841 cm->error.setjmp = 0;
2842
2843 return cpi;
2844}
2845
2846#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2847
2848#define SNPRINT2(H, T, V) \
2849 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2850
Yaowu Xuf883b422016-08-30 14:01:10 -07002851void av1_remove_compressor(AV1_COMP *cpi) {
2852 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002853 unsigned int i;
2854 int t;
2855
2856 if (!cpi) return;
2857
2858 cm = &cpi->common;
2859 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002860#if CONFIG_ENTROPY_STATS
2861 if (cpi->oxcf.pass != 1) {
2862 fprintf(stderr, "Writing counts.stt\n");
2863 FILE *f = fopen("counts.stt", "wb");
2864 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
Zoe Liua56f9162017-06-21 22:49:57 -07002865 fwrite(aggregate_fc_per_type, sizeof(aggregate_fc_per_type[0]),
2866 FRAME_CONTEXTS, f);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002867 fclose(f);
2868 }
2869#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002870#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07002871 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002872
2873 if (cpi->oxcf.pass != 1) {
2874 char headings[512] = { 0 };
2875 char results[512] = { 0 };
2876 FILE *f = fopen("opsnr.stt", "a");
2877 double time_encoded =
2878 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2879 10000000.000;
2880 double total_encode_time =
2881 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2882 const double dr =
2883 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2884 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2885 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2886 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2887
2888 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002889 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002890 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2891 const double total_ssim =
2892 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2893 snprintf(headings, sizeof(headings),
2894 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07002895 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Yaowu Xuc27fc142016-08-22 16:08:15 -07002896 "WstPsnr\tWstSsim\tWstFast\tWstHVS");
2897 snprintf(results, sizeof(results),
2898 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2899 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2900 "%7.3f\t%7.3f\t%7.3f\t%7.3f",
2901 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
2902 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
2903 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2904 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
2905 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
2906
2907 if (cpi->b_calculate_blockiness) {
2908 SNPRINT(headings, "\t Block\tWstBlck");
2909 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2910 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2911 }
2912
2913 if (cpi->b_calculate_consistency) {
2914 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07002915 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002916 (double)cpi->total_inconsistency);
2917
2918 SNPRINT(headings, "\tConsist\tWstCons");
2919 SNPRINT2(results, "\t%7.3f", consistency);
2920 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2921 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07002922 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2923 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924 rate_err, fabs(rate_err));
2925 }
2926
2927 fclose(f);
2928 }
2929
2930#endif
2931
2932#if 0
2933 {
2934 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2935 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2936 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2937 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2938 cpi->time_compress_data / 1000,
2939 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2940 }
2941#endif
2942 }
2943
2944 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002945 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002946 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2947
2948 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07002949 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002950
2951 // Deallocate allocated thread data.
2952 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07002953 aom_free(thread_data->td->palette_buffer);
Jingning Hand064cf02017-06-01 10:00:39 -07002954#if CONFIG_MOTION_VAR
2955 aom_free(thread_data->td->above_pred_buf);
2956 aom_free(thread_data->td->left_pred_buf);
2957 aom_free(thread_data->td->wsrc_buf);
2958 aom_free(thread_data->td->mask_buf);
2959#endif // CONFIG_MOTION_VAR
Yaowu Xuf883b422016-08-30 14:01:10 -07002960 aom_free(thread_data->td->counts);
2961 av1_free_pc_tree(thread_data->td);
Yaowu Xuf883b422016-08-30 14:01:10 -07002962 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963 }
2964 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002965 aom_free(cpi->tile_thr_data);
2966 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002967
Yaowu Xuf883b422016-08-30 14:01:10 -07002968 if (cpi->num_workers > 1) av1_loop_filter_dealloc(&cpi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969
2970 dealloc_compressor_data(cpi);
2971
2972 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2973 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002974 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002975 }
2976
2977#if CONFIG_FP_MB_STATS
2978 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002979 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002980 cpi->twopass.frame_mb_stats_buf = NULL;
2981 }
2982#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08002983#if CONFIG_INTERNAL_STATS
2984 aom_free(cpi->ssim_vars);
2985 cpi->ssim_vars = NULL;
2986#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002987
Yaowu Xuf883b422016-08-30 14:01:10 -07002988 av1_remove_common(cm);
2989 av1_free_ref_frame_buffers(cm->buffer_pool);
2990 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002991
2992#ifdef OUTPUT_YUV_SKINMAP
2993 fclose(yuv_skinmap_file);
2994#endif
2995#ifdef OUTPUT_YUV_REC
2996 fclose(yuv_rec_file);
2997#endif
2998
2999#if 0
3000
3001 if (keyfile)
3002 fclose(keyfile);
3003
3004 if (framepsnr)
3005 fclose(framepsnr);
3006
3007 if (kf_list)
3008 fclose(kf_list);
3009
3010#endif
3011}
3012
Yaowu Xuf883b422016-08-30 14:01:10 -07003013static void generate_psnr_packet(AV1_COMP *cpi) {
3014 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003015 int i;
3016 PSNR_STATS psnr;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003017#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07003018 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003019 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3020#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07003021 aom_calc_psnr(cpi->source, cpi->common.frame_to_show, &psnr);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003022#endif
3023
3024 for (i = 0; i < 4; ++i) {
3025 pkt.data.psnr.samples[i] = psnr.samples[i];
3026 pkt.data.psnr.sse[i] = psnr.sse[i];
3027 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3028 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003029 pkt.kind = AOM_CODEC_PSNR_PKT;
3030 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003031}
3032
Yaowu Xuf883b422016-08-30 14:01:10 -07003033int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003034 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3035
3036 cpi->ref_frame_flags = ref_frame_flags;
3037 return 0;
3038}
3039
Yaowu Xuf883b422016-08-30 14:01:10 -07003040void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags) {
3041 cpi->ext_refresh_golden_frame = (ref_frame_flags & AOM_GOLD_FLAG) != 0;
3042 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & AOM_ALT_FLAG) != 0;
3043 cpi->ext_refresh_last_frame = (ref_frame_flags & AOM_LAST_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044 cpi->ext_refresh_frame_flags_pending = 1;
3045}
3046
Thomas Daede497d1952017-08-08 17:33:06 -07003047int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3048 AV1_COMMON *const cm = &cpi->common;
3049 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003050 if (cfg) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003051 aom_yv12_copy_frame(cfg, sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003052 return 0;
3053 } else {
3054 return -1;
3055 }
3056}
3057
Thomas Daede497d1952017-08-08 17:33:06 -07003058int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3059 AV1_COMMON *const cm = &cpi->common;
3060 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003061 if (cfg) {
3062 aom_yv12_copy_frame(sd, cfg);
3063 return 0;
3064 } else {
3065 return -1;
3066 }
3067}
3068
3069int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003070 cpi->ext_refresh_frame_context = update;
3071 cpi->ext_refresh_frame_context_pending = 1;
3072 return 0;
3073}
3074
3075#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3076// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3077// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3078// not denoise the UV channels at this time. If ever we implement UV channel
3079// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003080void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003081 uint8_t *src = s->y_buffer;
3082 int h = s->y_height;
3083
3084 do {
3085 fwrite(src, s->y_width, 1, f);
3086 src += s->y_stride;
3087 } while (--h);
3088
3089 src = s->u_buffer;
3090 h = s->uv_height;
3091
3092 do {
3093 fwrite(src, s->uv_width, 1, f);
3094 src += s->uv_stride;
3095 } while (--h);
3096
3097 src = s->v_buffer;
3098 h = s->uv_height;
3099
3100 do {
3101 fwrite(src, s->uv_width, 1, f);
3102 src += s->uv_stride;
3103 } while (--h);
3104}
3105#endif
3106
Zoe Liue04abf72017-04-19 15:37:11 -07003107#if CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07003108static void check_show_existing_frame(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003109 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003110 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003111 const FRAME_UPDATE_TYPE next_frame_update_type =
3112 gf_group->update_type[gf_group->index];
3113 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07003114
3115 if (cm->show_existing_frame == 1) {
3116 cm->show_existing_frame = 0;
3117 } else if (cpi->rc.is_last_bipred_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003118 // NOTE(zoeliu): If the current frame is a last bi-predictive frame, it is
3119 // needed next to show the BWDREF_FRAME, which is pointed by
3120 // the last_fb_idxes[0] after reference frame buffer update
3121 cpi->rc.is_last_bipred_frame = 0;
3122 cm->show_existing_frame = 1;
3123 cpi->existing_fb_idx_to_show = cpi->lst_fb_idxes[0];
3124 } else if (cpi->is_arf_filter_off[which_arf] &&
3125 (next_frame_update_type == OVERLAY_UPDATE ||
3126 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
3127 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003128 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003129 cm->show_existing_frame = 1;
3130 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003131#if CONFIG_ALTREF2
3132 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3133 ? cpi->alt_fb_idx
3134 : cpi->alt2_fb_idx;
3135#else // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003136 cpi->existing_fb_idx_to_show = cpi->alt_fb_idx;
Zoe Liue9b15e22017-07-19 15:53:01 -07003137#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003138 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003139 }
3140 cpi->rc.is_src_frame_ext_arf = 0;
3141}
Zoe Liue04abf72017-04-19 15:37:11 -07003142#endif // CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07003143
3144#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003145void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003146 uint8_t *src = s->y_buffer;
3147 int h = cm->height;
3148
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003149#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003150 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3151 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3152
3153 do {
3154 fwrite(src16, s->y_width, 2, yuv_rec_file);
3155 src16 += s->y_stride;
3156 } while (--h);
3157
3158 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3159 h = s->uv_height;
3160
3161 do {
3162 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3163 src16 += s->uv_stride;
3164 } while (--h);
3165
3166 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3167 h = s->uv_height;
3168
3169 do {
3170 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3171 src16 += s->uv_stride;
3172 } while (--h);
3173
3174 fflush(yuv_rec_file);
3175 return;
3176 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003177#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003178
3179 do {
3180 fwrite(src, s->y_width, 1, yuv_rec_file);
3181 src += s->y_stride;
3182 } while (--h);
3183
3184 src = s->u_buffer;
3185 h = s->uv_height;
3186
3187 do {
3188 fwrite(src, s->uv_width, 1, yuv_rec_file);
3189 src += s->uv_stride;
3190 } while (--h);
3191
3192 src = s->v_buffer;
3193 h = s->uv_height;
3194
3195 do {
3196 fwrite(src, s->uv_width, 1, yuv_rec_file);
3197 src += s->uv_stride;
3198 } while (--h);
3199
3200 fflush(yuv_rec_file);
3201}
3202#endif // OUTPUT_YUV_REC
3203
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003204#if CONFIG_GLOBAL_MOTION
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003205#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003206static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3207 int i;
3208 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003209 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003210 AV1_COMMON *const cm = &cpi->common;
3211 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3212 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003213 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003214 cpi->gmparams_cost[i]) {
Sarah Parker4c10a3c2017-04-10 19:37:59 -07003215 set_default_warp_params(&cm->global_motion[i]);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003216 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003217 recode = 1;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003218 recode |= (rdc->global_motion_used[i] > 0);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003219 }
3220 }
3221 return recode;
3222}
3223#endif // CONFIG_GLOBAL_MOTION
3224
Yaowu Xuc27fc142016-08-22 16:08:15 -07003225// Function to test for conditions that indicate we should loop
3226// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003227static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3228 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003229 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003230 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003231 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3232 int force_recode = 0;
3233
3234 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3235 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3236 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003237 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3238 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3239 (rc->projected_frame_size < low_limit && q > minq)) {
3240 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003241 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003242 // Deal with frame undershoot and whether or not we are
3243 // below the automatically set cq level.
3244 if (q > oxcf->cq_level &&
3245 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3246 force_recode = 1;
3247 }
3248 }
3249 }
3250 return force_recode;
3251}
3252
Yaowu Xuc27fc142016-08-22 16:08:15 -07003253#define DUMP_REF_FRAME_IMAGES 0
3254
3255#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003256static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003257 const YV12_BUFFER_CONFIG *const ref_buf,
3258 char *file_name) {
3259 int h;
3260 FILE *f_ref = NULL;
3261
3262 if (ref_buf == NULL) {
3263 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003264 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003265 }
3266
3267 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3268 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003269 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003270 }
3271
3272 // --- Y ---
3273 for (h = 0; h < cm->height; ++h) {
3274 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3275 }
3276 // --- U ---
3277 for (h = 0; h < (cm->height >> 1); ++h) {
3278 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3279 f_ref);
3280 }
3281 // --- V ---
3282 for (h = 0; h < (cm->height >> 1); ++h) {
3283 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3284 f_ref);
3285 }
3286
3287 fclose(f_ref);
3288
Yaowu Xuf883b422016-08-30 14:01:10 -07003289 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003290}
3291
Yaowu Xuf883b422016-08-30 14:01:10 -07003292static void dump_ref_frame_images(AV1_COMP *cpi) {
3293 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003294 MV_REFERENCE_FRAME ref_frame;
3295
3296 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3297 char file_name[256] = "";
3298 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3299 cm->current_video_frame, ref_frame);
3300 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3301 }
3302}
3303#endif // DUMP_REF_FRAME_IMAGES == 1
3304
3305#if CONFIG_EXT_REFS
3306// This function is used to shift the virtual indices of last reference frames
3307// as follows:
3308// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3309// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003310static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003311 int ref_frame;
3312 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
3313 cpi->lst_fb_idxes[ref_frame] = cpi->lst_fb_idxes[ref_frame - 1];
3314
3315 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003316 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003317 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003318 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3319 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3320 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003321 }
3322 }
3323}
Zoe Liuf0e46692016-10-12 12:31:43 -07003324#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003325
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003326#if CONFIG_VAR_REFS
3327static void enc_check_valid_ref_frames(AV1_COMP *const cpi) {
3328 AV1_COMMON *const cm = &cpi->common;
3329 MV_REFERENCE_FRAME ref_frame;
3330
3331 // TODO(zoeliu): To handle ALTREF_FRAME the same way as do with other
3332 // reference frames. Current encoder invalid ALTREF when ALTREF
3333 // is the same as LAST, but invalid all the other references
3334 // when they are the same as ALTREF.
3335 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3336 int ref_buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3337 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3338
3339 if (ref_buf_idx != INVALID_IDX) {
3340 ref_buf->is_valid = 1;
3341
3342 MV_REFERENCE_FRAME ref;
3343 for (ref = LAST_FRAME; ref < ref_frame; ++ref) {
3344 int buf_idx = get_ref_frame_buf_idx(cpi, ref);
3345 RefBuffer *const buf = &cm->frame_refs[ref - LAST_FRAME];
3346 if (buf->is_valid && buf_idx == ref_buf_idx) {
3347 if (ref_frame != ALTREF_FRAME || ref == LAST_FRAME) {
3348 ref_buf->is_valid = 0;
3349 break;
3350 } else {
3351 buf->is_valid = 0;
3352 }
3353 }
3354 }
3355 } else {
3356 ref_buf->is_valid = 0;
3357 }
3358 }
3359}
3360#endif // CONFIG_VAR_REFS
3361
Yaowu Xuf883b422016-08-30 14:01:10 -07003362void av1_update_reference_frames(AV1_COMP *cpi) {
3363 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003364 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003365
3366 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3367 // for the purpose to verify no mismatch between encoder and decoder.
3368 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3369
Yaowu Xuc27fc142016-08-22 16:08:15 -07003370 // At this point the new frame has been encoded.
3371 // If any buffer copy / swapping is signaled it should be done here.
3372 if (cm->frame_type == KEY_FRAME) {
3373 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3374 cm->new_fb_idx);
3375#if CONFIG_EXT_REFS
3376 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3377 cm->new_fb_idx);
Zoe Liue9b15e22017-07-19 15:53:01 -07003378#if CONFIG_ALTREF2
3379 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3380 cm->new_fb_idx);
3381#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003382#endif // CONFIG_EXT_REFS
3383 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3384 cm->new_fb_idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003385 } else if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386 // We have decided to preserve the previously existing golden frame as our
3387 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003388 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003389 // we're updating the GF with the current decoded frame, we save it to the
3390 // ARF slot instead.
3391 // We now have to update the ARF with the current frame and swap gld_fb_idx
3392 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3393 // slot and, if we're updating the GF, the current frame becomes the new GF.
3394 int tmp;
3395
3396 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3397 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003398 tmp = cpi->alt_fb_idx;
3399 cpi->alt_fb_idx = cpi->gld_fb_idx;
3400 cpi->gld_fb_idx = tmp;
3401
3402#if CONFIG_EXT_REFS
3403 // We need to modify the mapping accordingly
3404 cpi->arf_map[0] = cpi->alt_fb_idx;
Zoe Liue9b15e22017-07-19 15:53:01 -07003405#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003406// TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3407// cpi->interp_filter_selected[GOLDEN_FRAME]?
3408#if CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003409 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3410 // Deal with the special case for showing existing internal ALTREF_FRAME
3411 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3412 // by updating the virtual indices.
3413 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -07003414 const int which_arf = gf_group->arf_ref_idx[gf_group->index];
3415 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416
Zoe Liue9b15e22017-07-19 15:53:01 -07003417 const int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003418 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003419
3420#if CONFIG_ALTREF2
3421 cpi->lst_fb_idxes[0] = cpi->alt2_fb_idx;
3422 cpi->alt2_fb_idx = tmp;
3423 // We need to modify the mapping accordingly
3424 cpi->arf_map[which_arf] = cpi->alt2_fb_idx;
3425
3426 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3427 cpi->interp_filter_selected[ALTREF2_FRAME],
3428 sizeof(cpi->interp_filter_selected[ALTREF2_FRAME]));
3429#else // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003430 cpi->lst_fb_idxes[0] = cpi->alt_fb_idx;
Zoe Liuf0e46692016-10-12 12:31:43 -07003431 cpi->alt_fb_idx = tmp;
Zoe Liuf0e46692016-10-12 12:31:43 -07003432 // We need to modify the mapping accordingly
3433 cpi->arf_map[which_arf] = cpi->alt_fb_idx;
3434
Yaowu Xuc27fc142016-08-22 16:08:15 -07003435 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3436 cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3437 sizeof(cpi->interp_filter_selected[ALTREF_FRAME + which_arf]));
Zoe Liue9b15e22017-07-19 15:53:01 -07003438#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003439#endif // CONFIG_EXT_REFS
3440 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003441 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003442 if (cpi->refresh_alt_ref_frame) {
3443 int arf_idx = cpi->alt_fb_idx;
3444 int which_arf = 0;
3445#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07003446#if !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003447 if (cpi->oxcf.pass == 2) {
3448 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3449 which_arf = gf_group->arf_update_idx[gf_group->index];
3450 arf_idx = cpi->arf_map[which_arf];
3451 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003452#endif // !CONFIG_ALTREF2
3453#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003454 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3455 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3456 arf_idx = gf_group->arf_update_idx[gf_group->index];
3457 }
3458#endif // CONFIG_EXT_REFS
3459 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003460
3461 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3462 cpi->interp_filter_selected[0],
3463 sizeof(cpi->interp_filter_selected[0]));
3464 }
3465
Zoe Liue9b15e22017-07-19 15:53:01 -07003466 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003467 if (cpi->refresh_golden_frame) {
3468 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3469 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003470
Zoe Liuf0e46692016-10-12 12:31:43 -07003471#if !CONFIG_EXT_REFS
3472 if (!cpi->rc.is_src_frame_alt_ref)
3473#endif // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003474 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3475 cpi->interp_filter_selected[0],
3476 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477 }
3478
3479#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07003480 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003481 if (cpi->refresh_bwd_ref_frame) {
Zoe Liue9b15e22017-07-19 15:53:01 -07003482#if !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003483 if (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs) {
3484 // We have swapped the virtual indices to allow bwd_ref_frame to use
3485 // ALT0 as reference frame. We need to swap them back.
3486 // NOTE: The ALT_REFs' are indexed reversely, and ALT0 refers to the
3487 // farthest ALT_REF from the first frame in the gf group.
3488 int tmp = cpi->arf_map[0];
3489 cpi->arf_map[0] = cpi->alt_fb_idx;
3490 cpi->alt_fb_idx = cpi->bwd_fb_idx;
3491 cpi->bwd_fb_idx = tmp;
3492 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003493#endif // !CONFIG_ALTREF2
Zoe Liuf0e46692016-10-12 12:31:43 -07003494
Yaowu Xuc27fc142016-08-22 16:08:15 -07003495 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3496 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003497
3498 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3499 cpi->interp_filter_selected[0],
3500 sizeof(cpi->interp_filter_selected[0]));
3501 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003502
3503#if CONFIG_ALTREF2
3504 // === ALTREF2_FRAME ===
3505 if (cpi->refresh_alt2_ref_frame) {
3506 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3507 cm->new_fb_idx);
3508
3509 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3510 cpi->interp_filter_selected[0],
3511 sizeof(cpi->interp_filter_selected[0]));
3512 }
3513#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003514#endif // CONFIG_EXT_REFS
3515 }
3516
3517 if (cpi->refresh_last_frame) {
3518#if CONFIG_EXT_REFS
3519 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3520 // reference to the reference frame buffer virtual index; and then (2) from
3521 // the virtual index to the reference frame buffer physical index:
3522 //
3523 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3524 // | | |
3525 // v v v
3526 // lst_fb_idxes[0], ..., lst_fb_idxes[2], ..., alt_fb_idx
3527 // | | |
3528 // v v v
3529 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3530 //
3531 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3532 // have the other 2 LAST reference frames shifted as follows:
3533 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3534 // , and then have LAST_FRAME refreshed by the newly coded frame.
3535 //
3536 // To fulfill it, the decoder will be notified to execute following 2 steps:
3537 //
3538 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3539 // to point to the newly coded frame, i.e.
3540 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3541 //
3542 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3543 // original virtual index of LAST3_FRAME and have the other mappings
3544 // shifted as follows:
3545 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3546 // | | |
3547 // v v v
3548 // lst_fb_idxes[2], lst_fb_idxes[0], lst_fb_idxes[1]
3549 int ref_frame;
Zoe Liu5fca7242016-10-10 17:18:57 -07003550
Zoe Liue9b15e22017-07-19 15:53:01 -07003551#if !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552 if (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs) {
3553 // We have swapped the virtual indices to use ALT0 as BWD_REF
3554 // and we need to swap them back.
3555 int tmp = cpi->arf_map[0];
3556 cpi->arf_map[0] = cpi->alt_fb_idx;
3557 cpi->alt_fb_idx = cpi->bwd_fb_idx;
3558 cpi->bwd_fb_idx = tmp;
3559 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003560#endif // !CONFIG_ALTREF2
Zoe Liu5fca7242016-10-10 17:18:57 -07003561
Yaowu Xuc27fc142016-08-22 16:08:15 -07003562 if (cm->frame_type == KEY_FRAME) {
3563 for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3564 ref_cnt_fb(pool->frame_bufs,
3565 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3566 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003567 }
3568 } else {
3569 int tmp;
3570
3571 ref_cnt_fb(pool->frame_bufs,
3572 &cm->ref_frame_map[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3573 cm->new_fb_idx);
3574
Yaowu Xuc27fc142016-08-22 16:08:15 -07003575 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3576
3577 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003578 cpi->lst_fb_idxes[0] = tmp;
3579
Zoe Liuf0e46692016-10-12 12:31:43 -07003580 assert(cm->show_existing_frame == 0);
Zoe Liuf0e46692016-10-12 12:31:43 -07003581 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3582 cpi->interp_filter_selected[0],
3583 sizeof(cpi->interp_filter_selected[0]));
Zoe Liuaff92d52017-07-11 21:35:08 -07003584
3585 if (cpi->rc.is_last_bipred_frame) {
3586 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3587 // LAST3_FRAME by updating the virtual indices.
3588 //
3589 // NOTE: The source frame for BWDREF does not have a holding position as
3590 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3591 // virtual index reshuffling for BWDREF, the encoder always
3592 // specifies a LAST_BIPRED right before BWDREF and completes the
3593 // reshuffling job accordingly.
3594 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3595
3596 shift_last_ref_frames(cpi);
3597 cpi->lst_fb_idxes[0] = cpi->bwd_fb_idx;
3598 cpi->bwd_fb_idx = tmp;
3599
3600 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3601 cpi->interp_filter_selected[BWDREF_FRAME],
3602 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
3603 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003604 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003605#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003606 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
3607 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003608 if (!cpi->rc.is_src_frame_alt_ref) {
3609 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3610 cpi->interp_filter_selected[0],
3611 sizeof(cpi->interp_filter_selected[0]));
3612 }
3613#endif // CONFIG_EXT_REFS
3614 }
3615
3616#if DUMP_REF_FRAME_IMAGES == 1
3617 // Dump out all reference frame images.
3618 dump_ref_frame_images(cpi);
3619#endif // DUMP_REF_FRAME_IMAGES
3620}
3621
Yaowu Xuf883b422016-08-30 14:01:10 -07003622static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003623 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003624 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003625 ensure_mv_buffer(new_fb_ptr, cm);
3626 new_fb_ptr->width = cm->width;
3627 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003628}
3629
Yaowu Xuf883b422016-08-30 14:01:10 -07003630void av1_scale_references(AV1_COMP *cpi) {
3631 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003632 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003633 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
3634 AOM_LAST_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003635#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003636 AOM_LAST2_FLAG,
3637 AOM_LAST3_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003638#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003639 AOM_GOLD_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003640#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003641 AOM_BWD_FLAG,
Zoe Liue9b15e22017-07-19 15:53:01 -07003642#if CONFIG_ALTREF2
3643 AOM_ALT2_FLAG,
3644#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003645#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003646 AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003647 };
3648
3649 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003650 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003651 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3652 BufferPool *const pool = cm->buffer_pool;
3653 const YV12_BUFFER_CONFIG *const ref =
3654 get_ref_frame_buffer(cpi, ref_frame);
3655
3656 if (ref == NULL) {
3657 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3658 continue;
3659 }
3660
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003661#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003662 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3663 RefCntBuffer *new_fb_ptr = NULL;
3664 int force_scaling = 0;
3665 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3666 if (new_fb == INVALID_IDX) {
3667 new_fb = get_free_fb(cm);
3668 force_scaling = 1;
3669 }
3670 if (new_fb == INVALID_IDX) return;
3671 new_fb_ptr = &pool->frame_bufs[new_fb];
3672 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3673 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003674 if (aom_realloc_frame_buffer(
3675 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3676 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3677 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003678 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003679 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003680 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf,
3681 (int)cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003682 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3683 alloc_frame_mvs(cm, new_fb);
3684 }
3685#else
3686 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3687 RefCntBuffer *new_fb_ptr = NULL;
3688 int force_scaling = 0;
3689 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3690 if (new_fb == INVALID_IDX) {
3691 new_fb = get_free_fb(cm);
3692 force_scaling = 1;
3693 }
3694 if (new_fb == INVALID_IDX) return;
3695 new_fb_ptr = &pool->frame_bufs[new_fb];
3696 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3697 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003698 if (aom_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003699 cm->subsampling_x, cm->subsampling_y,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003700 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3701 NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003702 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003704 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003705 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3706 alloc_frame_mvs(cm, new_fb);
3707 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003708#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003709 } else {
3710 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3711 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3712 buf->buf.y_crop_width = ref->y_crop_width;
3713 buf->buf.y_crop_height = ref->y_crop_height;
3714 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3715 ++buf->ref_count;
3716 }
3717 } else {
3718 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3719 }
3720 }
3721}
3722
Yaowu Xuf883b422016-08-30 14:01:10 -07003723static void release_scaled_references(AV1_COMP *cpi) {
3724 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003725 int i;
3726 if (cpi->oxcf.pass == 0) {
3727 // Only release scaled references under certain conditions:
3728 // if reference will be updated, or if scaled reference has same resolution.
3729 int refresh[INTER_REFS_PER_FRAME];
3730 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
3731#if CONFIG_EXT_REFS
3732 refresh[1] = refresh[2] = 0;
3733 refresh[3] = (cpi->refresh_golden_frame) ? 1 : 0;
3734 refresh[4] = (cpi->refresh_bwd_ref_frame) ? 1 : 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07003735#if CONFIG_ALTREF2
3736 refresh[5] = (cpi->refresh_alt2_ref_frame) ? 1 : 0;
3737 refresh[6] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3738#else // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07003739 refresh[5] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07003740#endif // CONFIG_ALTREF2
3741#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742 refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
3743 refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3744#endif // CONFIG_EXT_REFS
3745 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3746 const int idx = cpi->scaled_ref_idx[i - 1];
3747 RefCntBuffer *const buf =
3748 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3749 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
3750 if (buf != NULL &&
3751 (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
3752 buf->buf.y_crop_height == ref->y_crop_height))) {
3753 --buf->ref_count;
3754 cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
3755 }
3756 }
3757 } else {
3758 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) {
3759 const int idx = cpi->scaled_ref_idx[i];
3760 RefCntBuffer *const buf =
3761 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3762 if (buf != NULL) {
3763 --buf->ref_count;
3764 cpi->scaled_ref_idx[i] = INVALID_IDX;
3765 }
3766 }
3767 }
3768}
3769
3770static void full_to_model_count(unsigned int *model_count,
3771 unsigned int *full_count) {
3772 int n;
3773 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
3774 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
3775 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
3776 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
3777 model_count[TWO_TOKEN] += full_count[n];
3778 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
3779}
3780
Yaowu Xuf883b422016-08-30 14:01:10 -07003781void av1_full_to_model_counts(av1_coeff_count_model *model_count,
3782 av1_coeff_count *full_count) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003783 int i, j, k, l;
3784
3785 for (i = 0; i < PLANE_TYPES; ++i)
3786 for (j = 0; j < REF_TYPES; ++j)
3787 for (k = 0; k < COEF_BANDS; ++k)
3788 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
3789 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
3790}
3791
3792#if 0 && CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003793static void output_frame_level_debug_stats(AV1_COMP *cpi) {
3794 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003795 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
3796 int64_t recon_err;
3797
Yaowu Xuf883b422016-08-30 14:01:10 -07003798 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003799
Alex Conversef77fd0b2017-04-20 11:00:24 -07003800 recon_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003801
3802 if (cpi->twopass.total_left_stats.coded_error != 0.0)
Yunqing Wang8c1e57c2016-10-25 15:15:23 -07003803 fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
Yaowu Xuc27fc142016-08-22 16:08:15 -07003804 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
3805 "%10"PRId64" %10"PRId64" %10d "
3806 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
3807 "%6d %6d %5d %5d %5d "
3808 "%10"PRId64" %10.3lf"
3809 "%10lf %8u %10"PRId64" %10d %10d %10d\n",
3810 cpi->common.current_video_frame,
3811 cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003812 cpi->rc.source_alt_ref_pending,
3813 cpi->rc.source_alt_ref_active,
3814 cpi->rc.this_frame_target,
3815 cpi->rc.projected_frame_size,
3816 cpi->rc.projected_frame_size / cpi->common.MBs,
3817 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
3818 cpi->rc.vbr_bits_off_target,
3819 cpi->rc.vbr_bits_off_target_fast,
3820 cpi->twopass.extend_minq,
3821 cpi->twopass.extend_minq_fast,
3822 cpi->rc.total_target_vs_actual,
3823 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
3824 cpi->rc.total_actual_bits, cm->base_qindex,
Yaowu Xuf883b422016-08-30 14:01:10 -07003825 av1_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
3826 (double)av1_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
3827 av1_convert_qindex_to_q(cpi->twopass.active_worst_quality,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003828 cm->bit_depth),
3829 cpi->rc.avg_q,
Yaowu Xuf883b422016-08-30 14:01:10 -07003830 av1_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003831 cpi->refresh_last_frame, cpi->refresh_golden_frame,
3832 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
3833 cpi->twopass.bits_left,
3834 cpi->twopass.total_left_stats.coded_error,
3835 cpi->twopass.bits_left /
3836 (1 + cpi->twopass.total_left_stats.coded_error),
3837 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
3838 cpi->twopass.kf_zeromotion_pct,
3839 cpi->twopass.fr_content_type);
3840
3841 fclose(f);
3842
3843 if (0) {
3844 FILE *const fmodes = fopen("Modes.stt", "a");
3845 int i;
3846
3847 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
3848 cm->frame_type, cpi->refresh_golden_frame,
3849 cpi->refresh_alt_ref_frame);
3850
3851 for (i = 0; i < MAX_MODES; ++i)
3852 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
3853
3854 fprintf(fmodes, "\n");
3855
3856 fclose(fmodes);
3857 }
3858}
3859#endif
3860
Yaowu Xuf883b422016-08-30 14:01:10 -07003861static void set_mv_search_params(AV1_COMP *cpi) {
3862 const AV1_COMMON *const cm = &cpi->common;
3863 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003864
3865 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003866 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003867
3868 if (cpi->sf.mv.auto_mv_step_size) {
3869 if (frame_is_intra_only(cm)) {
3870 // Initialize max_mv_magnitude for use in the first INTER frame
3871 // after a key/intra-only frame.
3872 cpi->max_mv_magnitude = max_mv_def;
3873 } else {
3874 if (cm->show_frame) {
3875 // Allow mv_steps to correspond to twice the max mv magnitude found
3876 // in the previous frame, capped by the default max_mv_magnitude based
3877 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003878 cpi->mv_step_param = av1_init_search_range(
3879 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003880 }
3881 cpi->max_mv_magnitude = 0;
3882 }
3883 }
3884}
3885
Yaowu Xuf883b422016-08-30 14:01:10 -07003886static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003887#if CONFIG_GLOBAL_MOTION
3888 int i;
3889 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Sarah Parker4c10a3c2017-04-10 19:37:59 -07003890 set_default_warp_params(&cpi->common.global_motion[i]);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003891 }
3892 cpi->global_motion_search_done = 0;
3893#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuf883b422016-08-30 14:01:10 -07003894 av1_set_speed_features_framesize_independent(cpi);
3895 av1_set_rd_speed_thresholds(cpi);
3896 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003897 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07003898#if CONFIG_EXT_INTER
3899 if (!frame_is_intra_only(&cpi->common)) set_compound_tools(&cpi->common);
3900#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07003901}
3902
Yaowu Xuf883b422016-08-30 14:01:10 -07003903static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003904 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003905 AV1_COMMON *const cm = &cpi->common;
3906 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003907
3908 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003909 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003910
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00003911// Decide q and q bounds.
3912#if CONFIG_XIPHRC
3913 int frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
3914 *q = od_enc_rc_select_quantizers_and_lambdas(
3915 &cpi->od_rc, cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
3916 frame_type, bottom_index, top_index);
3917#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003918 *q = av1_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00003919#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003920
3921 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003922 av1_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003923 }
3924
3925 // Configure experimental use of segmentation for enhanced coding of
3926 // static regions if indicated.
3927 // Only allowed in the second pass of a two pass encode, as it requires
3928 // lagged coding, and if the relevant speed feature flag is set.
3929 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3930 configure_static_seg_features(cpi);
3931}
3932
Yaowu Xuf883b422016-08-30 14:01:10 -07003933static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003934 int y_stride = cpi->scaled_source.y_stride;
3935
3936 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003937 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003938 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003939 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003940 }
3941}
3942
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003943#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003944#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
3945static void set_restoration_tilesize(int width, int height, int sx, int sy,
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003946 RestorationInfo *rst) {
3947 (void)width;
3948 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003949 (void)sx;
3950 (void)sy;
3951#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3952 int s = AOMMIN(sx, sy);
3953#else
3954 int s = 0;
3955#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3956
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003957 rst[0].restoration_tilesize = (RESTORATION_TILESIZE_MAX >> 1);
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003958 rst[1].restoration_tilesize = rst[0].restoration_tilesize >> s;
3959 rst[2].restoration_tilesize = rst[1].restoration_tilesize;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003960}
3961#endif // CONFIG_LOOP_RESTORATION
3962
Fergus Simpsonbc189932017-05-16 17:02:39 -07003963static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003964 AV1_COMMON *const cm = &cpi->common;
Fergus Simpsonbc189932017-05-16 17:02:39 -07003965 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003966 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003967
Fergus Simpsonbc189932017-05-16 17:02:39 -07003968 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003969 // There has been a change in the encoded frame size
Fergus Simpsonbc189932017-05-16 17:02:39 -07003970 av1_set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003971 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003972 }
3973
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00003974#if !CONFIG_XIPHRC
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003975 if (cpi->oxcf.pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003976 av1_set_target_rate(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003977 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00003978#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003979
3980 alloc_frame_mvs(cm, cm->new_fb_idx);
3981
3982 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07003983 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003984 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003985#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003986 cm->use_highbitdepth,
3987#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003988 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
3989 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003990 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003991 "Failed to allocate frame buffer");
3992
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003993#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003994 set_restoration_tilesize(
3995#if CONFIG_FRAME_SUPERRES
3996 cm->superres_upscaled_width, cm->superres_upscaled_height,
3997#else
3998 cm->width, cm->height,
3999#endif // CONFIG_FRAME_SUPERRES
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004000 cm->subsampling_x, cm->subsampling_y, cm->rst_info);
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004001 for (int i = 0; i < MAX_MB_PLANE; ++i)
4002 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
4003 av1_alloc_restoration_buffers(cm);
4004 for (int i = 0; i < MAX_MB_PLANE; ++i) {
4005 cpi->rst_search[i].restoration_tilesize =
4006 cm->rst_info[i].restoration_tilesize;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004007 av1_alloc_restoration_struct(cm, &cpi->rst_search[i],
4008#if CONFIG_FRAME_SUPERRES
4009 cm->superres_upscaled_width,
4010 cm->superres_upscaled_height);
4011#else
4012 cm->width, cm->height);
4013#endif // CONFIG_FRAME_SUPERRES
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004014 }
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004015#endif // CONFIG_LOOP_RESTORATION
4016 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004017 init_motion_estimation(cpi);
4018
4019 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4020 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
4021 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4022
4023 ref_buf->idx = buf_idx;
4024
4025 if (buf_idx != INVALID_IDX) {
4026 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
4027 ref_buf->buf = buf;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004028#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004029 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004030 &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
4031 cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
4032#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004033 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
4034 buf->y_crop_height, cm->width,
4035 cm->height);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004036#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004037 if (av1_is_scaled(&ref_buf->sf)) aom_extend_frame_borders(buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004038 } else {
4039 ref_buf->buf = NULL;
4040 }
4041 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004042
4043#if CONFIG_VAR_REFS
4044 // Check duplicate reference frames
4045 enc_check_valid_ref_frames(cpi);
4046#endif // CONFIG_VAR_REFS
4047
Alex Conversee816b312017-05-01 09:51:24 -07004048#if CONFIG_INTRABC
4049#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan1dcb7072017-05-12 11:13:05 +02004050 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4051 cm->width, cm->height,
4052 cm->use_highbitdepth);
Alex Conversee816b312017-05-01 09:51:24 -07004053#else
4054 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4055 cm->width, cm->height);
4056#endif // CONFIG_HIGHBITDEPTH
4057#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004058
4059 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4060}
4061
Fergus Simpsonbc189932017-05-16 17:02:39 -07004062static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07004063 int encode_width = cpi->oxcf.width;
4064 int encode_height = cpi->oxcf.height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004065
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07004066 uint8_t resize_num = av1_calculate_next_resize_scale(cpi);
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07004067 av1_calculate_scaled_size(&encode_width, &encode_height, resize_num);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004068
4069#if CONFIG_FRAME_SUPERRES
4070 AV1_COMMON *cm = &cpi->common;
4071 cm->superres_upscaled_width = encode_width;
4072 cm->superres_upscaled_height = encode_height;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07004073 cm->superres_scale_numerator =
Urvang Joshi0b905402017-08-02 10:56:23 -07004074 av1_calculate_next_superres_scale(cpi, encode_width, encode_height);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07004075 av1_calculate_scaled_size(&encode_width, &encode_height,
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07004076 cm->superres_scale_numerator);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004077#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004078
4079 set_frame_size(cpi, encode_width, encode_height);
4080}
4081
4082#if CONFIG_FRAME_SUPERRES
4083static void superres_post_encode(AV1_COMP *cpi) {
4084 AV1_COMMON *cm = &cpi->common;
4085
4086 if (av1_superres_unscaled(cm)) return;
4087
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004088 av1_superres_upscale(cm, NULL);
4089
4090 // If regular resizing is occurring the source will need to be downscaled to
4091 // match the upscaled superres resolution. Otherwise the original source is
4092 // used.
4093 if (av1_resize_unscaled(cm)) {
4094 cpi->source = cpi->unscaled_source;
4095 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4096 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004097 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4098 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004099 // Do downscale. cm->(width|height) has been updated by av1_superres_upscale
4100 if (aom_realloc_frame_buffer(
4101 &cpi->scaled_source, cm->superres_upscaled_width,
4102 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
4103#if CONFIG_HIGHBITDEPTH
4104 cm->use_highbitdepth,
4105#endif // CONFIG_HIGHBITDEPTH
4106 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
4107 aom_internal_error(
4108 &cm->error, AOM_CODEC_MEM_ERROR,
4109 "Failed to reallocate scaled source buffer for superres");
4110 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4111 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
4112#if CONFIG_HIGHBITDEPTH
4113 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
4114 (int)cm->bit_depth);
4115#else
4116 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source);
4117#endif // CONFIG_HIGHBITDEPTH
4118 cpi->source = &cpi->scaled_source;
4119 }
4120}
4121#endif // CONFIG_FRAME_SUPERRES
4122
4123static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4124 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4125 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004126 int no_loopfilter = 0;
4127
4128 if (is_lossless_requested(&cpi->oxcf)) no_loopfilter = 1;
4129
4130#if CONFIG_EXT_TILE
4131 // 0 loopfilter level is only necessary if individual tile
4132 // decoding is required.
4133 if (cm->single_tile_decoding) no_loopfilter = 1;
4134#endif // CONFIG_EXT_TILE
4135
4136 if (no_loopfilter) {
Cheng Chen179479f2017-08-04 10:56:39 -07004137#if CONFIG_UV_LVL
4138 lf->filter_level[0] = 0;
4139 lf->filter_level[1] = 0;
4140#else
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004141 lf->filter_level = 0;
Cheng Chen179479f2017-08-04 10:56:39 -07004142#endif
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004143 } else {
4144 struct aom_usec_timer timer;
4145
4146 aom_clear_system_state();
4147
4148 aom_usec_timer_start(&timer);
4149
4150 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4151
4152 aom_usec_timer_mark(&timer);
4153 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4154 }
4155
Cheng Chen179479f2017-08-04 10:56:39 -07004156#if CONFIG_UV_LVL
4157 if (lf->filter_level[0] || lf->filter_level[1])
4158#else
4159 if (lf->filter_level > 0)
4160#endif
4161 {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004162#if CONFIG_VAR_TX || CONFIG_EXT_PARTITION || CONFIG_CB4X4
Cheng Chene94df5c2017-07-19 17:25:33 -07004163#if CONFIG_UV_LVL
Cheng Chen179479f2017-08-04 10:56:39 -07004164 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level[0],
4165 lf->filter_level[1], 0, 0);
4166 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_u,
4167 lf->filter_level_u, 1, 0);
4168 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_v,
4169 lf->filter_level_v, 2, 0);
4170
Cheng Chene94df5c2017-07-19 17:25:33 -07004171#else
4172 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
4173#endif // CONFIG_UV_LVL
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004174#else
4175 if (cpi->num_workers > 1)
4176 av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
4177 lf->filter_level, 0, 0, cpi->workers,
4178 cpi->num_workers, &cpi->lf_row_sync);
4179 else
4180 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
4181#endif
4182 }
4183#if CONFIG_CDEF
4184 if (is_lossless_requested(&cpi->oxcf)) {
4185 cm->cdef_bits = 0;
4186 cm->cdef_strengths[0] = 0;
4187 cm->nb_cdef_strengths = 1;
4188 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004189 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004190 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
4191 cpi->oxcf.speed > 0);
4192
4193 // Apply the filter
4194 av1_cdef_frame(cm->frame_to_show, cm, xd);
4195 }
4196#endif
4197
4198#if CONFIG_FRAME_SUPERRES
4199 superres_post_encode(cpi);
4200#endif // CONFIG_FRAME_SUPERRES
4201
4202#if CONFIG_LOOP_RESTORATION
4203 av1_pick_filter_restoration(cpi->source, cpi, cpi->sf.lpf_pick);
4204 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4205 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4206 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4207 av1_loop_restoration_frame(cm->frame_to_show, cm, cm->rst_info, 7, 0, NULL);
4208 }
4209#endif // CONFIG_LOOP_RESTORATION
4210 // TODO(debargha): Fix mv search range on encoder side
4211 // aom_extend_frame_inner_borders(cm->frame_to_show);
4212 aom_extend_frame_borders(cm->frame_to_show);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004213}
4214
Yaowu Xuf883b422016-08-30 14:01:10 -07004215static void encode_without_recode_loop(AV1_COMP *cpi) {
4216 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004217 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004218
Yaowu Xuf883b422016-08-30 14:01:10 -07004219 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004220
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004221 set_size_independent_vars(cpi);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004222 setup_frame_size(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004223 assert(cm->width == cpi->scaled_source.y_crop_width);
4224 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004225
Yaowu Xuc27fc142016-08-22 16:08:15 -07004226 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4227
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004228 cpi->source =
4229 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4230 if (cpi->unscaled_last_source != NULL)
4231 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4232 &cpi->scaled_last_source);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004233#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
4234 cpi->source->buf_8bit_valid = 0;
4235#endif
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004236
4237 if (frame_is_intra_only(cm) == 0) {
4238 av1_scale_references(cpi);
4239 }
4240
Yaowu Xuf883b422016-08-30 14:01:10 -07004241 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004242 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004243 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004244
Yaowu Xuc27fc142016-08-22 16:08:15 -07004245 // Variance adaptive and in frame q adjustment experiments are mutually
4246 // exclusive.
4247 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004248 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004249 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004250 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004251 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004252 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004253 }
4254 apply_active_map(cpi);
4255
4256 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004257 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004258
4259 // Update some stats from cyclic refresh, and check if we should not update
4260 // golden reference, for 1 pass CBR.
4261 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004262 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4263 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004264
4265 // Update the skip mb flag probabilities based on the distribution
4266 // seen in the last encoder iteration.
4267 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004268 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004269}
4270
Yaowu Xuf883b422016-08-30 14:01:10 -07004271static void encode_with_recode_loop(AV1_COMP *cpi, size_t *size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004272 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004273 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004274 RATE_CONTROL *const rc = &cpi->rc;
4275 int bottom_index, top_index;
4276 int loop_count = 0;
4277 int loop_at_this_size = 0;
4278 int loop = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004279#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004280 int overshoot_seen = 0;
4281 int undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004282#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004283 int frame_over_shoot_limit;
4284 int frame_under_shoot_limit;
4285 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004286
4287 set_size_independent_vars(cpi);
4288
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004289#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
4290 cpi->source->buf_8bit_valid = 0;
4291#endif
4292
Yaowu Xuc27fc142016-08-22 16:08:15 -07004293 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004294 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004295
Fergus Simpsonbc189932017-05-16 17:02:39 -07004296 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004297
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004298 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004299 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4300
4301 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4302 set_mv_search_params(cpi);
4303
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004304#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004305 // Reset the loop state for new frame size.
4306 overshoot_seen = 0;
4307 undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004308#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004309
Yaowu Xuc27fc142016-08-22 16:08:15 -07004310 q_low = bottom_index;
4311 q_high = top_index;
4312
4313 loop_at_this_size = 0;
4314 }
4315
4316 // Decide frame size bounds first time through.
4317 if (loop_count == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004318 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4319 &frame_under_shoot_limit,
4320 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004321 }
4322
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004323 cpi->source =
4324 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4325#if CONFIG_GLOBAL_MOTION
4326 // if frame was scaled calculate global_motion_search again if already done
4327 if (cpi->source != cpi->unscaled_source) cpi->global_motion_search_done = 0;
4328#endif // CONFIG_GLOBAL_MOTION
4329 if (cpi->unscaled_last_source != NULL)
4330 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4331 &cpi->scaled_last_source);
4332
Yaowu Xuc27fc142016-08-22 16:08:15 -07004333 if (frame_is_intra_only(cm) == 0) {
4334 if (loop_count > 0) {
4335 release_scaled_references(cpi);
4336 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004337 av1_scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004338 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004339 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004340
4341 if (loop_count == 0) setup_frame(cpi);
4342
hui su0d103572017-03-01 17:58:01 -08004343#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004344 // Base q-index may have changed, so we need to assign proper default coef
4345 // probs before every iteration.
4346 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
4347 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07004348 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004349 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4350 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4351 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4352 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
Thomas Daededa4d8b92017-06-05 15:44:14 -07004353#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
4354 if (cm->frame_refs[0].idx >= 0) {
4355 cm->frame_contexts[cm->frame_refs[0].idx] = *cm->fc;
4356 }
4357#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004358 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07004359#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004360 }
4361 }
hui su0d103572017-03-01 17:58:01 -08004362#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363
Yaowu Xuc27fc142016-08-22 16:08:15 -07004364 // Variance adaptive and in frame q adjustment experiments are mutually
4365 // exclusive.
4366 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004367 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004368 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004369 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004370 }
4371
4372 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004373 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004374
4375 // Update the skip mb flag probabilities based on the distribution
4376 // seen in the last encoder iteration.
4377 // update_base_skip_probs(cpi);
4378
Yaowu Xuf883b422016-08-30 14:01:10 -07004379 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004380
4381 // Dummy pack of the bitstream using up to date stats to get an
4382 // accurate estimate of output frame size to determine if we need
4383 // to recode.
4384 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
4385 save_coding_context(cpi);
4386
Yaowu Xuf883b422016-08-30 14:01:10 -07004387 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004388
4389 rc->projected_frame_size = (int)(*size) << 3;
4390 restore_coding_context(cpi);
4391
4392 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4393 }
4394
Yaowu Xuf883b422016-08-30 14:01:10 -07004395 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004396 loop = 0;
4397 } else {
4398 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4399 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4400 int last_q = q;
4401 int64_t kf_err;
4402
4403 int64_t high_err_target = cpi->ambient_err;
4404 int64_t low_err_target = cpi->ambient_err >> 1;
4405
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004406#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004407 if (cm->use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004408 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004409 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004410 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004411 }
4412#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07004413 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004414#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004415
4416 // Prevent possible divide by zero error below for perfect KF
4417 kf_err += !kf_err;
4418
4419 // The key frame is not good enough or we can afford
4420 // to make it better without undue risk of popping.
4421 if ((kf_err > high_err_target &&
4422 rc->projected_frame_size <= frame_over_shoot_limit) ||
4423 (kf_err > low_err_target &&
4424 rc->projected_frame_size <= frame_under_shoot_limit)) {
4425 // Lower q_high
4426 q_high = q > q_low ? q - 1 : q_low;
4427
4428 // Adjust Q
4429 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004430 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004431 } else if (kf_err < low_err_target &&
4432 rc->projected_frame_size >= frame_under_shoot_limit) {
4433 // The key frame is much better than the previous frame
4434 // Raise q_low
4435 q_low = q < q_high ? q + 1 : q_high;
4436
4437 // Adjust Q
4438 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004439 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004440 }
4441
4442 // Clamp Q to upper and lower limits:
4443 q = clamp(q, q_low, q_high);
4444
4445 loop = q != last_q;
4446 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4447 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004448 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004449 // Is the projected frame size out of range and are we allowed
4450 // to attempt to recode.
4451 int last_q = q;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004452#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004453 int retries = 0;
4454
Yaowu Xuc27fc142016-08-22 16:08:15 -07004455 // Frame size out of permitted range:
4456 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004457 // Frame is too large
4458 if (rc->projected_frame_size > rc->this_frame_target) {
4459 // Special case if the projected size is > the max allowed.
4460 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4461 q_high = rc->worst_quality;
4462
4463 // Raise Qlow as to at least the current value
4464 q_low = q < q_high ? q + 1 : q_high;
4465
4466 if (undershoot_seen || loop_at_this_size > 1) {
4467 // Update rate_correction_factor unless
Yaowu Xuf883b422016-08-30 14:01:10 -07004468 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004469
4470 q = (q_high + q_low + 1) / 2;
4471 } else {
4472 // Update rate_correction_factor unless
Yaowu Xuf883b422016-08-30 14:01:10 -07004473 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004474
Yaowu Xuf883b422016-08-30 14:01:10 -07004475 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4476 AOMMAX(q_high, top_index));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004477
4478 while (q < q_low && retries < 10) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004479 av1_rc_update_rate_correction_factors(cpi);
4480 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4481 AOMMAX(q_high, top_index));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004482 retries++;
4483 }
4484 }
4485
4486 overshoot_seen = 1;
4487 } else {
4488 // Frame is too small
4489 q_high = q > q_low ? q - 1 : q_low;
4490
4491 if (overshoot_seen || loop_at_this_size > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004492 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004493 q = (q_high + q_low) / 2;
4494 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004495 av1_rc_update_rate_correction_factors(cpi);
4496 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4497 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004498 // Special case reset for qlow for constrained quality.
4499 // This should only trigger where there is very substantial
4500 // undershoot on a frame and the auto cq level is above
4501 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004502 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004503 q_low = q;
4504 }
4505
4506 while (q > q_high && retries < 10) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004507 av1_rc_update_rate_correction_factors(cpi);
4508 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4509 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004510 retries++;
4511 }
4512 }
4513
4514 undershoot_seen = 1;
4515 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004516#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004517
4518 // Clamp Q to upper and lower limits:
4519 q = clamp(q, q_low, q_high);
4520
4521 loop = (q != last_q);
4522 } else {
4523 loop = 0;
4524 }
4525 }
4526
4527 // Special case for overlay frame.
4528 if (rc->is_src_frame_alt_ref &&
4529 rc->projected_frame_size < rc->max_frame_bandwidth)
4530 loop = 0;
4531
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004532#if CONFIG_GLOBAL_MOTION
4533 if (recode_loop_test_global_motion(cpi)) {
4534 loop = 1;
4535 }
4536#endif // CONFIG_GLOBAL_MOTION
4537
Yaowu Xuc27fc142016-08-22 16:08:15 -07004538 if (loop) {
4539 ++loop_count;
4540 ++loop_at_this_size;
4541
4542#if CONFIG_INTERNAL_STATS
4543 ++cpi->tot_recode_hits;
4544#endif
4545 }
4546 } while (loop);
4547}
4548
Yaowu Xuf883b422016-08-30 14:01:10 -07004549static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004550 const int *const map = cpi->common.ref_frame_map;
4551
4552#if CONFIG_EXT_REFS
4553 const int last2_is_last =
4554 map[cpi->lst_fb_idxes[1]] == map[cpi->lst_fb_idxes[0]];
4555 const int last3_is_last =
4556 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[0]];
4557 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[0]];
Zoe Liu5a978832017-08-15 16:33:34 -07004558#if CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02004559 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
4560 const int last3_is_last2 =
4561 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
4562 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
4563 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
Zoe Liu5a978832017-08-15 16:33:34 -07004564#else // !CONFIG_ONE_SIDED_COMPOUND || CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004565 const int bwd_is_last = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[0]];
4566 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
4567
4568 const int last3_is_last2 =
4569 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
4570 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
4571 const int bwd_is_last2 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[1]];
4572
4573 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
4574 const int bwd_is_last3 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[2]];
4575
4576 const int bwd_is_gld = map[cpi->bwd_fb_idx] == map[cpi->gld_fb_idx];
Zoe Liu5a978832017-08-15 16:33:34 -07004577#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07004578
4579#if CONFIG_ALTREF2
4580 const int alt2_is_last = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[0]];
4581 const int alt2_is_last2 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[1]];
4582 const int alt2_is_last3 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[2]];
4583 const int alt2_is_gld = map[cpi->alt2_fb_idx] == map[cpi->gld_fb_idx];
4584 const int alt2_is_bwd = map[cpi->alt2_fb_idx] == map[cpi->bwd_fb_idx];
4585#endif // CONFIG_ALTREF2
4586
Yaowu Xuc27fc142016-08-22 16:08:15 -07004587 const int last2_is_alt = map[cpi->lst_fb_idxes[1]] == map[cpi->alt_fb_idx];
4588 const int last3_is_alt = map[cpi->lst_fb_idxes[2]] == map[cpi->alt_fb_idx];
4589 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4590 const int bwd_is_alt = map[cpi->bwd_fb_idx] == map[cpi->alt_fb_idx];
Zoe Liue9b15e22017-07-19 15:53:01 -07004591#if CONFIG_ALTREF2
4592 const int alt2_is_alt = map[cpi->alt2_fb_idx] == map[cpi->alt_fb_idx];
4593#endif // CONFIG_ALTREF2
Zoe Liufcf5fa22017-06-26 16:00:38 -07004594#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004595 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
4596 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4597 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
4598#endif // CONFIG_EXT_REFS
4599
Yaowu Xuf883b422016-08-30 14:01:10 -07004600 int flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004601
4602#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07004603#if !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004604 // Disable the use of BWDREF_FRAME for non-bipredictive frames.
4605 if (!(cpi->rc.is_bipred_frame || cpi->rc.is_last_bipred_frame ||
4606 (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs)))
Yaowu Xuf883b422016-08-30 14:01:10 -07004607 flags &= ~AOM_BWD_FLAG;
Zoe Liue9b15e22017-07-19 15:53:01 -07004608#endif // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004609#endif // CONFIG_EXT_REFS
4610
Yaowu Xuf883b422016-08-30 14:01:10 -07004611 if (gld_is_last || gld_is_alt) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004612
Yaowu Xuf883b422016-08-30 14:01:10 -07004613 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004614
Yaowu Xuf883b422016-08-30 14:01:10 -07004615 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004616
4617#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07004618 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004619
Yaowu Xuf883b422016-08-30 14:01:10 -07004620 if (last3_is_last || last3_is_last2 || last3_is_alt) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004621
Yaowu Xuf883b422016-08-30 14:01:10 -07004622 if (gld_is_last2 || gld_is_last3) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004623
Zoe Liu5a978832017-08-15 16:33:34 -07004624#if CONFIG_ONE_SIDED_COMPOUND && \
4625 !CONFIG_EXT_COMP_REFS // Changes LL & HL bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02004626 /* Allow biprediction between two identical frames (e.g. bwd_is_last = 1) */
4627 if (bwd_is_alt && (flags & AOM_BWD_FLAG)) flags &= ~AOM_BWD_FLAG;
Zoe Liu5a978832017-08-15 16:33:34 -07004628#else // !CONFIG_ONE_SIDED_COMPOUND || CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004629 if ((bwd_is_last || bwd_is_last2 || bwd_is_last3 || bwd_is_gld ||
4630 bwd_is_alt) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004631 (flags & AOM_BWD_FLAG))
4632 flags &= ~AOM_BWD_FLAG;
Zoe Liu5a978832017-08-15 16:33:34 -07004633#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07004634
4635#if CONFIG_ALTREF2
4636 if ((alt2_is_last || alt2_is_last2 || alt2_is_last3 || alt2_is_gld ||
4637 alt2_is_bwd || alt2_is_alt) &&
4638 (flags & AOM_ALT2_FLAG))
4639 flags &= ~AOM_ALT2_FLAG;
4640#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004641#endif // CONFIG_EXT_REFS
4642
4643 return flags;
4644}
4645
Yaowu Xuf883b422016-08-30 14:01:10 -07004646static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004647 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07004648 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07004649 // Note: The overrides are valid only for the next frame passed
4650 // to encode_frame_to_data_rate() function
4651 if (cpi->ext_refresh_frame_context_pending) {
4652 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4653 cpi->ext_refresh_frame_context_pending = 0;
4654 }
4655 if (cpi->ext_refresh_frame_flags_pending) {
4656 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4657 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4658 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
4659 cpi->ext_refresh_frame_flags_pending = 0;
4660 }
4661}
4662
Yaowu Xuf883b422016-08-30 14:01:10 -07004663static void set_arf_sign_bias(AV1_COMP *cpi) {
4664 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004665 int arf_sign_bias;
4666#if CONFIG_EXT_REFS
4667 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -07004668// The arf_sign_bias will be one for internal ARFs'
4669#if CONFIG_ALTREF2
4670 arf_sign_bias = cpi->rc.source_alt_ref_active &&
4671 (!cpi->refresh_alt_ref_frame ||
4672 gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE);
4673#else // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004674 arf_sign_bias = cpi->rc.source_alt_ref_active &&
4675 (!cpi->refresh_alt_ref_frame ||
4676 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
Zoe Liue9b15e22017-07-19 15:53:01 -07004677#endif // CONFIG_ALTREF2
4678#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004679 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
4680 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4681 arf_sign_bias = cpi->rc.source_alt_ref_active &&
4682 (!cpi->refresh_alt_ref_frame ||
4683 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
4684 } else {
4685 arf_sign_bias =
4686 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
4687 }
Zoe Liu6cfaff92016-10-18 17:12:11 -07004688#endif // CONFIG_EXT_REFS
4689
Yaowu Xuc27fc142016-08-22 16:08:15 -07004690 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
4691#if CONFIG_EXT_REFS
4692 cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
Zoe Liue9b15e22017-07-19 15:53:01 -07004693#if CONFIG_ALTREF2
4694 cm->ref_frame_sign_bias[ALTREF2_FRAME] =
4695 cm->ref_frame_sign_bias[ALTREF_FRAME];
4696#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004697#endif // CONFIG_EXT_REFS
4698}
4699
Yaowu Xuf883b422016-08-30 14:01:10 -07004700static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07004701 InterpFilter ifilter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004702 int ref_total[TOTAL_REFS_PER_FRAME] = { 0 };
4703 MV_REFERENCE_FRAME ref;
4704 int mask = 0;
4705 int arf_idx = ALTREF_FRAME;
4706
Zoe Liue9b15e22017-07-19 15:53:01 -07004707#if CONFIG_EXT_REFS && !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004708 // Get which arf used as ALTREF_FRAME
4709 if (cpi->oxcf.pass == 2)
4710 arf_idx += cpi->twopass.gf_group.arf_ref_idx[cpi->twopass.gf_group.index];
Zoe Liue9b15e22017-07-19 15:53:01 -07004711#endif // CONFIG_EXT_REFS && !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004712
Zoe Liue9b15e22017-07-19 15:53:01 -07004713#if CONFIG_ALTREF2
4714 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
4715 cpi->refresh_alt2_ref_frame)
4716#else // !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004717 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Zoe Liue9b15e22017-07-19 15:53:01 -07004718#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004719 return mask;
4720
4721#if CONFIG_EXT_REFS
4722 for (ref = LAST_FRAME; ref < ALTREF_FRAME; ++ref)
4723 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4724 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
4725
Zoe Liue9b15e22017-07-19 15:53:01 -07004726#if !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004727 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4728 ref_total[ref] += cpi->interp_filter_selected[arf_idx][ifilter];
Zoe Liue9b15e22017-07-19 15:53:01 -07004729#endif // !CONFIG_ALTREF2
4730#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004731 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4732 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4733 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Zoe Liu6cfaff92016-10-18 17:12:11 -07004734#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004735
4736 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
4737 if ((ref_total[LAST_FRAME] &&
4738 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
4739#if CONFIG_EXT_REFS
4740 (ref_total[LAST2_FRAME] == 0 ||
4741 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
4742 ref_total[LAST2_FRAME]) &&
4743 (ref_total[LAST3_FRAME] == 0 ||
4744 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
4745 ref_total[LAST3_FRAME]) &&
4746#endif // CONFIG_EXT_REFS
4747 (ref_total[GOLDEN_FRAME] == 0 ||
4748 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4749 ref_total[GOLDEN_FRAME]) &&
4750#if CONFIG_EXT_REFS
4751 (ref_total[BWDREF_FRAME] == 0 ||
4752 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
4753 ref_total[BWDREF_FRAME]) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004754#if CONFIG_ALTREF2
4755 (ref_total[ALTREF2_FRAME] == 0 ||
4756 cpi->interp_filter_selected[ALTREF2_FRAME][ifilter] * 50 <
4757 ref_total[ALTREF2_FRAME]) &&
4758#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004759#endif // CONFIG_EXT_REFS
4760 (ref_total[ALTREF_FRAME] == 0 ||
4761 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
4762 ref_total[ALTREF_FRAME]))
4763 mask |= 1 << ifilter;
4764 }
4765 return mask;
4766}
4767
4768#define DUMP_RECON_FRAMES 0
4769
4770#if DUMP_RECON_FRAMES == 1
4771// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004772static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4773 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004774 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
4775 int h;
4776 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4777 FILE *f_recon = NULL;
4778
4779 if (recon_buf == NULL || !cm->show_frame) {
4780 printf("Frame %d is not ready or no show to dump.\n",
4781 cm->current_video_frame);
4782 return;
4783 }
4784
4785 if (cm->current_video_frame == 0) {
4786 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4787 printf("Unable to open file %s to write.\n", file_name);
4788 return;
4789 }
4790 } else {
4791 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4792 printf("Unable to open file %s to append.\n", file_name);
4793 return;
4794 }
4795 }
4796 printf(
4797 "\nFrame=%5d, encode_update_type[%5d]=%1d, show_existing_frame=%d, "
Zoe Liufcf5fa22017-06-26 16:00:38 -07004798 "source_alt_ref_active=%d, refresh_alt_ref_frame=%d, rf_level=%d, "
4799 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004800 cm->current_video_frame, cpi->twopass.gf_group.index,
4801 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liufcf5fa22017-06-26 16:00:38 -07004802 cm->show_existing_frame, cpi->rc.source_alt_ref_active,
4803 cpi->refresh_alt_ref_frame,
4804 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4805 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004806#if 0
4807 int ref_frame;
4808 printf("get_ref_frame_map_idx: [");
4809 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
4810 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
4811 printf(" ]\n");
4812 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
4813 printf("cm->ref_frame_map = [");
4814 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4815 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
4816 }
4817 printf(" ]\n");
4818#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819
4820 // --- Y ---
4821 for (h = 0; h < cm->height; ++h) {
4822 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4823 f_recon);
4824 }
4825 // --- U ---
4826 for (h = 0; h < (cm->height >> 1); ++h) {
4827 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4828 f_recon);
4829 }
4830 // --- V ---
4831 for (h = 0; h < (cm->height >> 1); ++h) {
4832 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4833 f_recon);
4834 }
4835
4836 fclose(f_recon);
4837}
4838#endif // DUMP_RECON_FRAMES
4839
Thomas Davies028b57f2017-02-22 16:42:11 +00004840static void make_update_tile_list_enc(AV1_COMP *cpi, const int tile_rows,
4841 const int tile_cols,
4842 FRAME_CONTEXT *ec_ctxs[]) {
4843 int i;
4844 for (i = 0; i < tile_rows * tile_cols; ++i)
4845 ec_ctxs[i] = &cpi->tile_data[i].tctx;
4846}
4847
Yaowu Xuf883b422016-08-30 14:01:10 -07004848static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004849 uint8_t *dest, int skip_adapt,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004850 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004851 AV1_COMMON *const cm = &cpi->common;
4852 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004853 struct segmentation *const seg = &cm->seg;
4854 TX_SIZE t;
Thomas Davies493623e2017-03-31 16:12:25 +01004855 FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
4856 sizeof(&cpi->tile_data[0].tctx));
4857 aom_cdf_prob **cdf_ptrs =
4858 aom_malloc(cm->tile_rows * cm->tile_cols *
4859 sizeof(&cpi->tile_data[0].tctx.partition_cdf[0][0]));
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004860#if CONFIG_XIPHRC
4861 int frame_type;
4862 int drop_this_frame = 0;
Zoe Liue04abf72017-04-19 15:37:11 -07004863#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004864 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004865 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004866
4867 // Set the arf sign bias for this frame.
4868 set_arf_sign_bias(cpi);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004869#if CONFIG_TEMPMV_SIGNALING
4870 // frame type has been decided outside of this function call
4871 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4872 cm->use_prev_frame_mvs =
4873 !cpi->oxcf.disable_tempmv && !cm->cur_frame->intra_only;
4874#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004875
4876#if CONFIG_EXT_REFS
4877 // NOTE:
4878 // (1) Move the setup of the ref_frame_flags upfront as it would be
4879 // determined by the current frame properties;
4880 // (2) The setup of the ref_frame_flags applies to both show_existing_frame's
4881 // and the other cases.
4882 if (cm->current_video_frame > 0)
4883 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4884
4885 if (cm->show_existing_frame) {
4886 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
4887 // BWDREF_FRAME in the reference frame buffer.
4888 cm->frame_type = INTER_FRAME;
4889 cm->show_frame = 1;
4890 cpi->frame_flags = *frame_flags;
4891
4892 // In the case of show_existing frame, we will not send fresh flag
4893 // to decoder. Any change in the reference frame buffer can be done by
4894 // switching the virtual indices.
4895
4896 cpi->refresh_last_frame = 0;
4897 cpi->refresh_golden_frame = 0;
4898 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07004899#if CONFIG_ALTREF2
4900 cpi->refresh_alt2_ref_frame = 0;
4901#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07004902 cpi->refresh_alt_ref_frame = 0;
4903
4904 cpi->rc.is_bwd_ref_frame = 0;
4905 cpi->rc.is_last_bipred_frame = 0;
4906 cpi->rc.is_bipred_frame = 0;
4907
4908 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07004909 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004910
4911 // Set up frame to show to get ready for stats collection.
4912 cm->frame_to_show = get_frame_new_buffer(cm);
4913
4914#if DUMP_RECON_FRAMES == 1
4915 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4916 dump_filtered_recon_frames(cpi);
4917#endif // DUMP_RECON_FRAMES
4918
4919 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07004920 // NOTE:
4921 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
4922 // update has been done previously when handling the LAST_BIPRED_FRAME
4923 // right before BWDREF_FRAME (in the display order);
4924 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
4925 // update will be done when the following is called, which will exchange
4926 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
4927 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2, and
4928 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Yaowu Xuf883b422016-08-30 14:01:10 -07004929 av1_update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004930
4931 // Update frame flags
4932 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4933 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4934 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4935
4936 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4937
4938 // Update the frame type
4939 cm->last_frame_type = cm->frame_type;
4940
Yaowu Xuc27fc142016-08-22 16:08:15 -07004941 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4942 // to do post-encoding update accordingly.
4943 if (cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004944 av1_set_target_rate(cpi);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004945#if CONFIG_XIPHRC
Zoe Liue04abf72017-04-19 15:37:11 -07004946 frame_type = cm->frame_type == INTER_FRAME ? OD_P_FRAME : OD_I_FRAME;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004947 drop_this_frame = od_enc_rc_update_state(
4948 &cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
4949 cpi->refresh_alt_ref_frame, frame_type, cpi->droppable);
4950#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004951 av1_rc_postencode_update(cpi, *size);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004952#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004953 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004954
Yaowu Xuc27fc142016-08-22 16:08:15 -07004955 ++cm->current_video_frame;
4956
Jingning Hanf6214b92017-04-12 11:43:37 -07004957 aom_free(tile_ctxs);
4958 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004959 return;
4960 }
4961#endif // CONFIG_EXT_REFS
4962
4963 // Set default state for segment based loop filter update flags.
4964 cm->lf.mode_ref_delta_update = 0;
4965
4966 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4967 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4968
4969 // Set various flags etc to special state if it is a key frame.
4970 if (frame_is_intra_only(cm)) {
4971 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004972 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004973
4974 // If segmentation is enabled force a map update for key frames.
4975 if (seg->enabled) {
4976 seg->update_map = 1;
4977 seg->update_data = 1;
4978 }
4979
4980 // The alternate reference frame cannot be active for a key frame.
4981 cpi->rc.source_alt_ref_active = 0;
4982
4983 cm->error_resilient_mode = oxcf->error_resilient_mode;
4984
Thomas Daedea6a854b2017-06-22 17:49:11 -07004985#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07004986 // By default, encoder assumes decoder can use prev_mi.
4987 if (cm->error_resilient_mode) {
4988 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
4989 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4990 } else if (cm->intra_only) {
4991 // Only reset the current context.
4992 cm->reset_frame_context = RESET_FRAME_CONTEXT_CURRENT;
4993 }
Thomas Daedea6a854b2017-06-22 17:49:11 -07004994#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004995 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00004996 if (cpi->oxcf.mtu == 0) {
4997 cm->num_tg = cpi->oxcf.num_tile_groups;
4998 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08004999 // Use a default value for the purposes of weighting costs in probability
5000 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005001 cm->num_tg = DEFAULT_MAX_NUM_TG;
5002 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005003
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005004#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07005005 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5006 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005007#endif // CONFIG_EXT_TILE
5008
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005009#if CONFIG_XIPHRC
5010 if (drop_this_frame) {
5011 av1_rc_postencode_update_drop_frame(cpi);
5012 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005013 aom_free(tile_ctxs);
5014 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005015 return;
5016 }
5017#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07005018 // For 1 pass CBR, check if we are dropping this frame.
5019 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005020 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005021 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005022 if (av1_rc_drop_frame(cpi)) {
5023 av1_rc_postencode_update_drop_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005024 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005025 aom_free(tile_ctxs);
5026 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005027 return;
5028 }
5029 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005030#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005031
Yaowu Xuf883b422016-08-30 14:01:10 -07005032 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005033
5034#if CONFIG_INTERNAL_STATS
5035 memset(cpi->mode_chosen_counts, 0,
5036 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5037#endif
5038
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005039#if CONFIG_REFERENCE_BUFFER
5040 {
5041 /* Non-normative definition of current_frame_id ("frame counter" with
5042 * wraparound) */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005043 const int frame_id_length = FRAME_ID_LENGTH_MINUS7 + 7;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005044 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005045 int lsb, msb;
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005046/* quasi-random initialization of current_frame_id for a key frame */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005047#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07005048 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5049 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5050 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005051 } else {
5052#endif
Alex Conversef77fd0b2017-04-20 11:00:24 -07005053 lsb = cpi->source->y_buffer[0] & 0xff;
5054 msb = cpi->source->y_buffer[1] & 0xff;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005055#if CONFIG_HIGHBITDEPTH
David Barker49a76562016-12-07 14:50:21 +00005056 }
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005057#endif
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005058 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005059 } else {
5060 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005061 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
5062 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005063 }
5064 }
5065#endif
5066
Fangwen Fu6160df22017-04-24 09:45:51 -07005067#if CONFIG_EXT_DELTA_Q
5068 cm->delta_q_present_flag = cpi->oxcf.deltaq_mode != NO_DELTA_Q;
Fangwen Fu231fe422017-04-24 17:52:29 -07005069 cm->delta_lf_present_flag = cpi->oxcf.deltaq_mode == DELTA_Q_LF;
Fangwen Fu6160df22017-04-24 09:45:51 -07005070#endif
5071
Yaowu Xuc27fc142016-08-22 16:08:15 -07005072 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
5073 encode_without_recode_loop(cpi);
5074 } else {
5075 encode_with_recode_loop(cpi, size, dest);
5076 }
5077
Yi Luo10e23002017-07-31 11:54:43 -07005078 cm->last_tile_cols = cm->tile_cols;
5079 cm->last_tile_rows = cm->tile_rows;
5080
Yaowu Xuc27fc142016-08-22 16:08:15 -07005081#ifdef OUTPUT_YUV_SKINMAP
5082 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005083 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005084 }
5085#endif // OUTPUT_YUV_SKINMAP
5086
5087 // Special case code to reduce pulsing when key frames are forced at a
5088 // fixed interval. Note the reconstruction error if it is the frame before
5089 // the force key frame
5090 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005091#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005092 if (cm->use_highbitdepth) {
5093 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07005094 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005095 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005096 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005097 }
5098#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07005099 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005100#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005101 }
5102
5103 // If the encoder forced a KEY_FRAME decision
5104 if (cm->frame_type == KEY_FRAME) {
5105 cpi->refresh_last_frame = 1;
5106 }
5107
5108 cm->frame_to_show = get_frame_new_buffer(cm);
5109 cm->frame_to_show->color_space = cm->color_space;
anorkin76fb1262017-03-22 15:12:12 -07005110#if CONFIG_COLORSPACE_HEADERS
5111 cm->frame_to_show->transfer_function = cm->transfer_function;
5112 cm->frame_to_show->chroma_sample_position = cm->chroma_sample_position;
5113#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005114 cm->frame_to_show->color_range = cm->color_range;
5115 cm->frame_to_show->render_width = cm->render_width;
5116 cm->frame_to_show->render_height = cm->render_height;
5117
5118#if CONFIG_EXT_REFS
5119// TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5120// off.
5121#endif // CONFIG_EXT_REFS
5122
5123 // Pick the loop filter level for the frame.
5124 loopfilter_frame(cpi, cm);
5125
5126 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07005127 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005128
Jingning Hanf6214b92017-04-12 11:43:37 -07005129 if (skip_adapt) {
Jingning Hanf6214b92017-04-12 11:43:37 -07005130 aom_free(tile_ctxs);
5131 aom_free(cdf_ptrs);
Jingning Hanf6214b92017-04-12 11:43:37 -07005132 return;
5133 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005134
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005135#if CONFIG_REFERENCE_BUFFER
5136 {
5137 int i;
5138 /* Update reference frame id values based on the value of refresh_mask */
5139 for (i = 0; i < REF_FRAMES; i++) {
5140 if ((cm->refresh_mask >> i) & 1) {
5141 cm->ref_frame_id[i] = cm->current_frame_id;
5142 }
5143 }
5144 }
5145#endif
5146
Yaowu Xuc27fc142016-08-22 16:08:15 -07005147#if DUMP_RECON_FRAMES == 1
5148 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5149 if (cm->show_frame) dump_filtered_recon_frames(cpi);
5150#endif // DUMP_RECON_FRAMES
5151
5152 if (cm->seg.update_map) update_reference_segmentation_map(cpi);
5153
5154 if (frame_is_intra_only(cm) == 0) {
5155 release_scaled_references(cpi);
5156 }
5157
Yaowu Xuf883b422016-08-30 14:01:10 -07005158 av1_update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005159
Jingning Han6de954c2016-12-16 09:52:04 -08005160 for (t = 0; t < TX_SIZES; t++)
Yaowu Xuf883b422016-08-30 14:01:10 -07005161 av1_full_to_model_counts(cpi->td.counts->coef[t],
5162 cpi->td.rd_counts.coef_counts[t]);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005163#if CONFIG_ENTROPY_STATS
5164 av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
Zoe Liua56f9162017-06-21 22:49:57 -07005165 assert(cm->frame_context_idx < FRAME_CONTEXTS);
5166 av1_accumulate_frame_counts(&aggregate_fc_per_type[cm->frame_context_idx],
5167 &cm->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005168#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005169 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005170 av1_adapt_coef_probs(cm);
5171 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00005172 make_update_tile_list_enc(cpi, cm->tile_rows, cm->tile_cols, tile_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01005173 av1_average_tile_coef_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005174 cm->tile_rows * cm->tile_cols);
Thomas Davies493623e2017-03-31 16:12:25 +01005175 av1_average_tile_intra_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005176 cm->tile_rows * cm->tile_cols);
Yushin Chob188ea12017-03-13 13:45:23 -07005177#if CONFIG_PVQ
5178 av1_average_tile_pvq_cdfs(cpi->common.fc, tile_ctxs,
5179 cm->tile_rows * cm->tile_cols);
5180#endif // CONFIG_PVQ
hui suff0da2b2017-03-07 15:51:37 -08005181#if CONFIG_ADAPT_SCAN
5182 av1_adapt_scan_order(cm);
5183#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005184 }
5185
5186 if (!frame_is_intra_only(cm)) {
5187 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005188 av1_adapt_inter_frame_probs(cm);
5189 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00005190 av1_average_tile_inter_cdfs(&cpi->common, cpi->common.fc, tile_ctxs,
Thomas Davies493623e2017-03-31 16:12:25 +01005191 cdf_ptrs, cm->tile_rows * cm->tile_cols);
5192 av1_average_tile_mv_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005193 cm->tile_rows * cm->tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005194 }
5195 }
5196
5197 if (cpi->refresh_golden_frame == 1)
5198 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5199 else
5200 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5201
5202 if (cpi->refresh_alt_ref_frame == 1)
5203 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5204 else
5205 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5206
5207#if CONFIG_EXT_REFS
5208 if (cpi->refresh_bwd_ref_frame == 1)
5209 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5210 else
5211 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
5212#endif // CONFIG_EXT_REFS
5213
5214#if !CONFIG_EXT_REFS
5215 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5216#endif // !CONFIG_EXT_REFS
5217
Yaowu Xuc27fc142016-08-22 16:08:15 -07005218 cm->last_frame_type = cm->frame_type;
5219
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005220#if CONFIG_XIPHRC
5221 frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
5222
5223 drop_this_frame =
5224 od_enc_rc_update_state(&cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5225 cpi->refresh_alt_ref_frame, frame_type, 0);
5226 if (drop_this_frame) {
5227 av1_rc_postencode_update_drop_frame(cpi);
5228 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005229 aom_free(tile_ctxs);
5230 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005231 return;
5232 }
Zoe Liue04abf72017-04-19 15:37:11 -07005233#else // !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005234 av1_rc_postencode_update(cpi, *size);
Zoe Liue04abf72017-04-19 15:37:11 -07005235#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005236
5237#if 0
5238 output_frame_level_debug_stats(cpi);
5239#endif
5240
5241 if (cm->frame_type == KEY_FRAME) {
5242 // Tell the caller that the frame was coded as a key frame
5243 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5244 } else {
5245 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5246 }
5247
5248 // Clear the one shot update flags for segmentation map and mode/ref loop
5249 // filter deltas.
5250 cm->seg.update_map = 0;
5251 cm->seg.update_data = 0;
5252 cm->lf.mode_ref_delta_update = 0;
5253
Yaowu Xuc27fc142016-08-22 16:08:15 -07005254 if (cm->show_frame) {
5255#if CONFIG_EXT_REFS
5256// TODO(zoeliu): We may only swamp mi and prev_mi for those frames that are
5257// being used as reference.
5258#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005259 av1_swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005260 // Don't increment frame counters if this was an altref buffer
5261 // update not a real frame
5262 ++cm->current_video_frame;
5263 }
5264
5265#if CONFIG_EXT_REFS
5266 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005267 if (cm->is_reference_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005268#endif // CONFIG_EXT_REFS
5269 cm->prev_frame = cm->cur_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005270 // keep track of the last coded dimensions
5271 cm->last_width = cm->width;
5272 cm->last_height = cm->height;
5273
5274 // reset to normal state now that we are done.
5275 cm->last_show_frame = cm->show_frame;
5276#if CONFIG_EXT_REFS
5277 }
5278#endif // CONFIG_EXT_REFS
Yi Luo10e23002017-07-31 11:54:43 -07005279
Thomas Davies493623e2017-03-31 16:12:25 +01005280 aom_free(tile_ctxs);
5281 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005282}
5283
Yaowu Xuf883b422016-08-30 14:01:10 -07005284static void Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005285 int skip_adapt, unsigned int *frame_flags) {
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005286#if CONFIG_XIPHRC
5287 int64_t ip_count;
5288 int frame_type, is_golden, is_altref;
5289
5290 /* Not updated during init so update it here */
5291 if (cpi->oxcf.rc_mode == AOM_Q) cpi->od_rc.quality = cpi->oxcf.cq_level;
5292
5293 frame_type = od_frame_type(&cpi->od_rc, cpi->od_rc.cur_frame, &is_golden,
5294 &is_altref, &ip_count);
5295
5296 if (frame_type == OD_I_FRAME) {
5297 frame_type = KEY_FRAME;
5298 cpi->frame_flags &= FRAMEFLAGS_KEY;
5299 } else if (frame_type == OD_P_FRAME) {
5300 frame_type = INTER_FRAME;
5301 }
5302
5303 if (is_altref) {
5304 cpi->refresh_alt_ref_frame = 1;
5305 cpi->rc.source_alt_ref_active = 1;
5306 }
5307
5308 cpi->refresh_golden_frame = is_golden;
5309 cpi->common.frame_type = frame_type;
5310 if (is_golden) cpi->frame_flags &= FRAMEFLAGS_GOLDEN;
5311#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005312 if (cpi->oxcf.rc_mode == AOM_CBR) {
5313 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005314 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005315 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005316 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005317#endif
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005318 encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005319}
5320
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005321#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005322static void Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005323 unsigned int *frame_flags) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005324 encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005325
5326#if CONFIG_EXT_REFS
5327 // Do not do post-encoding update for those frames that do not have a spot in
5328 // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
5329 // even when show_existing_frame is used.
5330 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005331 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005332 }
5333 check_show_existing_frame(cpi);
5334#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005335 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005336#endif // CONFIG_EXT_REFS
5337}
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005338#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005339
Yaowu Xuf883b422016-08-30 14:01:10 -07005340static void init_ref_frame_bufs(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005341 int i;
5342 BufferPool *const pool = cm->buffer_pool;
5343 cm->new_fb_idx = INVALID_IDX;
5344 for (i = 0; i < REF_FRAMES; ++i) {
5345 cm->ref_frame_map[i] = INVALID_IDX;
5346 pool->frame_bufs[i].ref_count = 0;
5347 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07005348#if CONFIG_HASH_ME
5349 for (i = 0; i < FRAME_BUFFERS; ++i) {
5350 av1_hash_table_init(&pool->frame_bufs[i].hash_table);
5351 }
5352#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005353}
5354
Yaowu Xuf883b422016-08-30 14:01:10 -07005355static void check_initial_width(AV1_COMP *cpi,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005356#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005357 int use_highbitdepth,
5358#endif
5359 int subsampling_x, int subsampling_y) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005360 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005361
5362 if (!cpi->initial_width ||
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005363#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005364 cm->use_highbitdepth != use_highbitdepth ||
5365#endif
5366 cm->subsampling_x != subsampling_x ||
5367 cm->subsampling_y != subsampling_y) {
5368 cm->subsampling_x = subsampling_x;
5369 cm->subsampling_y = subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005370#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005371 cm->use_highbitdepth = use_highbitdepth;
5372#endif
5373
5374 alloc_raw_frame_buffers(cpi);
5375 init_ref_frame_bufs(cm);
5376 alloc_util_frame_buffers(cpi);
5377
5378 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
5379
5380 cpi->initial_width = cm->width;
5381 cpi->initial_height = cm->height;
5382 cpi->initial_mbs = cm->MBs;
5383 }
5384}
5385
James Zern3e2613b2017-03-30 23:14:40 -07005386int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005387 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5388 int64_t end_time) {
5389 AV1_COMMON *const cm = &cpi->common;
5390 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005391 int res = 0;
5392 const int subsampling_x = sd->subsampling_x;
5393 const int subsampling_y = sd->subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005394#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005395 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5396#endif
5397
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005398#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005399 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5400#else
5401 check_initial_width(cpi, subsampling_x, subsampling_y);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005402#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005403
Yaowu Xuf883b422016-08-30 14:01:10 -07005404 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005405
Yaowu Xuf883b422016-08-30 14:01:10 -07005406 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005407#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005408 use_highbitdepth,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005409#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005410 frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005411 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005412 aom_usec_timer_mark(&timer);
5413 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005414
5415 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
5416 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005417 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005418 "Non-4:2:0 color format requires profile 1 or 3");
5419 res = -1;
5420 }
5421 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
5422 (subsampling_x == 1 && subsampling_y == 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005423 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005424 "4:2:0 color format requires profile 0 or 2");
5425 res = -1;
5426 }
5427
5428 return res;
5429}
5430
Yaowu Xuf883b422016-08-30 14:01:10 -07005431static int frame_is_reference(const AV1_COMP *cpi) {
5432 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005433
5434 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
5435 cpi->refresh_golden_frame ||
5436#if CONFIG_EXT_REFS
5437 cpi->refresh_bwd_ref_frame ||
Zoe Liue9b15e22017-07-19 15:53:01 -07005438#if CONFIG_ALTREF2
5439 cpi->refresh_alt2_ref_frame ||
5440#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07005441#endif // CONFIG_EXT_REFS
5442 cpi->refresh_alt_ref_frame || !cm->error_resilient_mode ||
5443 cm->lf.mode_ref_delta_update || cm->seg.update_map ||
5444 cm->seg.update_data;
5445}
5446
Yaowu Xuf883b422016-08-30 14:01:10 -07005447static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005448 const struct lookahead_entry *source) {
5449 int64_t this_duration;
5450 int step = 0;
5451
5452 if (source->ts_start == cpi->first_time_stamp_ever) {
5453 this_duration = source->ts_end - source->ts_start;
5454 step = 1;
5455 } else {
5456 int64_t last_duration =
5457 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5458
5459 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5460
5461 // do a step update if the duration changes by 10%
5462 if (last_duration)
5463 step = (int)((this_duration - last_duration) * 10 / last_duration);
5464 }
5465
5466 if (this_duration) {
5467 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005468 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005469 } else {
5470 // Average this frame's rate into the last second's average
5471 // frame rate. If we haven't seen 1 second yet, then average
5472 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005473 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005474 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5475 double avg_duration = 10000000.0 / cpi->framerate;
5476 avg_duration *= (interval - avg_duration + this_duration);
5477 avg_duration /= interval;
5478
Yaowu Xuf883b422016-08-30 14:01:10 -07005479 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005480 }
5481 }
5482 cpi->last_time_stamp_seen = source->ts_start;
5483 cpi->last_end_time_stamp_seen = source->ts_end;
5484}
5485
5486// Returns 0 if this is not an alt ref else the offset of the source frame
5487// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005488static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005489 RATE_CONTROL *const rc = &cpi->rc;
5490 int arf_src_index = 0;
5491 if (is_altref_enabled(cpi)) {
5492 if (cpi->oxcf.pass == 2) {
5493 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5494 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5495 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5496 }
5497 } else if (rc->source_alt_ref_pending) {
5498 arf_src_index = rc->frames_till_gf_update_due;
5499 }
5500 }
5501 return arf_src_index;
5502}
5503
5504#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005505static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005506 int brf_src_index = 0;
5507 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5508
5509 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5510 // flag.
5511 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5512 if (cpi->oxcf.pass == 2) {
5513 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5514 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5515 } else {
5516 // TODO(zoeliu): To re-visit the setup for this scenario
5517 brf_src_index = cpi->rc.bipred_group_interval - 1;
5518 }
5519 }
5520
5521 return brf_src_index;
5522}
Zoe Liue9b15e22017-07-19 15:53:01 -07005523
5524#if CONFIG_ALTREF2
5525// Returns 0 if this is not an alt ref else the offset of the source frame
5526// used as the arf midpoint.
5527static int get_arf2_src_index(AV1_COMP *cpi) {
5528 int arf2_src_index = 0;
5529 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5530 if (cpi->oxcf.pass == 2) {
5531 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5532 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5533 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5534 }
5535 }
5536 }
5537 return arf2_src_index;
5538}
5539#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07005540#endif // CONFIG_EXT_REFS
5541
Yaowu Xuf883b422016-08-30 14:01:10 -07005542static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005543 const struct lookahead_entry *source) {
5544 RATE_CONTROL *const rc = &cpi->rc;
5545
5546 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005547 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005548
5549 if (cpi->oxcf.pass == 2) {
5550 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5551 rc->is_src_frame_alt_ref =
5552#if CONFIG_EXT_REFS
5553 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Zoe Liu6cfaff92016-10-18 17:12:11 -07005554#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005555 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liu438b3ae2017-08-01 17:36:04 -07005556#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07005557 rc->is_src_frame_ext_arf =
5558 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Zoe Liu438b3ae2017-08-01 17:36:04 -07005559#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07005560 } else {
5561 rc->is_src_frame_alt_ref =
5562 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5563 }
5564
5565 if (rc->is_src_frame_alt_ref) {
5566 // Current frame is an ARF overlay frame.
5567 cpi->alt_ref_source = NULL;
5568
Zoe Liu438b3ae2017-08-01 17:36:04 -07005569#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07005570 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
5571 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
5572 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
5573 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
5574 cpi->refresh_last_frame = 1;
5575 } else {
Zoe Liu438b3ae2017-08-01 17:36:04 -07005576#endif // CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07005577 // Don't refresh the last buffer for an ARF overlay frame. It will
5578 // become the GF so preserve last as an alternative prediction option.
5579 cpi->refresh_last_frame = 0;
Zoe Liu438b3ae2017-08-01 17:36:04 -07005580#if CONFIG_ALTREF2
Zoe Liue9b15e22017-07-19 15:53:01 -07005581 }
Zoe Liu438b3ae2017-08-01 17:36:04 -07005582#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07005583 }
5584}
5585
5586#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005587extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5588 const unsigned char *img2, int img2_pitch,
5589 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005590
5591static void adjust_image_stat(double y, double u, double v, double all,
5592 ImageStat *s) {
5593 s->stat[Y] += y;
5594 s->stat[U] += u;
5595 s->stat[V] += v;
5596 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005597 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005598}
5599
Angie Chiang08a22a62017-07-17 17:29:17 -07005600static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005601 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005602 double samples = 0.0;
5603 uint32_t in_bit_depth = 8;
5604 uint32_t bit_depth = 8;
5605
Angie Chiang08a22a62017-07-17 17:29:17 -07005606#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07005607 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005608#endif
5609 cpi->bytes += frame_bytes;
5610
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005611#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005612 if (cm->use_highbitdepth) {
5613 in_bit_depth = cpi->oxcf.input_bit_depth;
5614 bit_depth = cm->bit_depth;
5615 }
5616#endif
5617 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005618 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005619 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5620 double y, u, v, frame_all;
5621
5622 cpi->count++;
5623 if (cpi->b_calculate_psnr) {
5624 PSNR_STATS psnr;
5625 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005626 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005627// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005628#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005629 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005630#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005631 aom_calc_psnr(orig, recon, &psnr);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005632#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005633
5634 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5635 &cpi->psnr);
5636 cpi->total_sq_error += psnr.sse[0];
5637 cpi->total_samples += psnr.samples[0];
5638 samples = psnr.samples[0];
5639// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005640#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005641 if (cm->use_highbitdepth)
5642 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005643 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005644 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005645 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005646#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005647 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005648#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005649
Yaowu Xuf883b422016-08-30 14:01:10 -07005650 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005651 cpi->summed_quality += frame_ssim2 * weight;
5652 cpi->summed_weights += weight;
5653
5654#if 0
5655 {
5656 FILE *f = fopen("q_used.stt", "a");
5657 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5658 cpi->common.current_video_frame, y2, u2, v2,
5659 frame_psnr2, frame_ssim2);
5660 fclose(f);
5661 }
5662#endif
5663 }
5664 if (cpi->b_calculate_blockiness) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005665#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005666 if (!cm->use_highbitdepth)
5667#endif
5668 {
5669 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005670 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5671 recon->y_stride, orig->y_width, orig->y_height);
5672 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005673 cpi->total_blockiness += frame_blockiness;
5674 }
5675
5676 if (cpi->b_calculate_consistency) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005677#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005678 if (!cm->use_highbitdepth)
5679#endif
5680 {
Yaowu Xuf883b422016-08-30 14:01:10 -07005681 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005682 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5683 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5684
5685 const double peak = (double)((1 << in_bit_depth) - 1);
5686 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005687 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005688 if (consistency > 0.0)
5689 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005690 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005691 cpi->total_inconsistency += this_inconsistency;
5692 }
5693 }
5694 }
5695
5696 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005697 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005698 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005699 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005700 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5701 }
5702}
5703#endif // CONFIG_INTERNAL_STATS
5704
Yaowu Xuf883b422016-08-30 14:01:10 -07005705int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5706 size_t *size, uint8_t *dest, int64_t *time_stamp,
5707 int64_t *time_end, int flush) {
5708 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5709 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005710 BufferPool *const pool = cm->buffer_pool;
5711 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07005712 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005713 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5714 struct lookahead_entry *last_source = NULL;
5715 struct lookahead_entry *source = NULL;
5716 int arf_src_index;
5717#if CONFIG_EXT_REFS
5718 int brf_src_index;
5719#endif // CONFIG_EXT_REFS
5720 int i;
5721
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005722#if CONFIG_XIPHRC
5723 cpi->od_rc.end_of_input = flush;
5724#endif
5725
Yaowu Xuc27fc142016-08-22 16:08:15 -07005726#if CONFIG_BITSTREAM_DEBUG
5727 assert(cpi->oxcf.max_threads == 0 &&
5728 "bitstream debug tool does not support multithreading");
5729 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07005730 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005731#endif
5732
Yaowu Xuf883b422016-08-30 14:01:10 -07005733 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005734
Yaowu Xuf883b422016-08-30 14:01:10 -07005735 av1_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005736
5737 // Is multi-arf enabled.
5738 // Note that at the moment multi_arf is only configured for 2 pass VBR
5739 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
5740 cpi->multi_arf_allowed = 1;
5741 else
5742 cpi->multi_arf_allowed = 0;
5743
Thomas Daedea6a854b2017-06-22 17:49:11 -07005744// Normal defaults
5745#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005746 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07005747#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005748 cm->refresh_frame_context =
5749 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
5750 ? REFRESH_FRAME_CONTEXT_FORWARD
5751 : REFRESH_FRAME_CONTEXT_BACKWARD;
5752
5753 cpi->refresh_last_frame = 1;
5754 cpi->refresh_golden_frame = 0;
5755#if CONFIG_EXT_REFS
5756 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005757#if CONFIG_ALTREF2
5758 cpi->refresh_alt2_ref_frame = 0;
5759#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07005760#endif // CONFIG_EXT_REFS
5761 cpi->refresh_alt_ref_frame = 0;
5762
Zoe Liue04abf72017-04-19 15:37:11 -07005763#if CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005764 if (oxcf->pass == 2 && cm->show_existing_frame) {
5765 // Manage the source buffer and flush out the source frame that has been
5766 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07005767 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005768 *size = 0;
5769 return -1;
5770 }
Alex Conversef77fd0b2017-04-20 11:00:24 -07005771 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005772 // TODO(zoeliu): To track down to determine whether it's needed to adjust
5773 // the frame rate.
5774 *time_stamp = source->ts_start;
5775 *time_end = source->ts_end;
5776
5777 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005778 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005779
5780 // Find a free buffer for the new frame, releasing the reference previously
5781 // held.
5782 if (cm->new_fb_idx != INVALID_IDX) {
5783 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5784 }
5785 cm->new_fb_idx = get_free_fb(cm);
5786
5787 if (cm->new_fb_idx == INVALID_IDX) return -1;
5788
5789 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005790 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005791
5792 // Start with a 0 size frame.
5793 *size = 0;
5794
5795 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005796 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005797
5798 Pass2Encode(cpi, size, dest, frame_flags);
5799
5800 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
5801
5802#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07005803 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005804#endif // CONFIG_INTERNAL_STATS
5805
5806 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005807 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005808
5809 cm->show_existing_frame = 0;
5810 return 0;
5811 }
Zoe Liue04abf72017-04-19 15:37:11 -07005812#endif // CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005813
5814 // Should we encode an arf frame.
5815 arf_src_index = get_arf_src_index(cpi);
5816 if (arf_src_index) {
5817 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005818 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005819 // Avoid creating an alt-ref if there's a forced keyframe pending.
5820 if (e == NULL) {
5821 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07005822 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005823 arf_src_index = 0;
5824 flush = 1;
5825 break;
5826 }
5827 }
5828 }
5829
5830 if (arf_src_index) {
5831 assert(arf_src_index <= rc->frames_to_key);
5832
Yaowu Xuf883b422016-08-30 14:01:10 -07005833 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005834 cpi->alt_ref_source = source;
5835
5836 if (oxcf->arnr_max_frames > 0) {
Todd Nguyen302d0972017-06-16 16:16:29 -07005837// Produce the filtered ARF frame.
5838#if CONFIG_BGSPRITE
5839 int bgsprite_ret = av1_background_sprite(cpi, arf_src_index);
Todd Nguyen2fc23092017-07-11 12:00:36 -07005840 // Do temporal filter if bgsprite not generated.
5841 if (bgsprite_ret != 0)
Todd Nguyen302d0972017-06-16 16:16:29 -07005842#endif // CONFIG_BGSPRITE
Todd Nguyen8493f912017-07-20 12:45:12 -07005843 av1_temporal_filter(cpi,
5844#if CONFIG_BGSPRITE
Todd Nguyen1fd99c22017-07-31 17:50:41 -07005845 NULL, &cpi->alt_ref_buffer,
Todd Nguyen8493f912017-07-20 12:45:12 -07005846#endif // CONFIG_BGSPRITE
5847 arf_src_index);
Yaowu Xuf883b422016-08-30 14:01:10 -07005848 aom_extend_frame_borders(&cpi->alt_ref_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005849 force_src_buffer = &cpi->alt_ref_buffer;
5850 }
5851
5852 cm->show_frame = 0;
5853 cm->intra_only = 0;
5854 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005855 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005856 cpi->refresh_golden_frame = 0;
5857#if CONFIG_EXT_REFS
5858 cpi->refresh_bwd_ref_frame = 0;
5859#if CONFIG_ALTREF2
5860 cpi->refresh_alt2_ref_frame = 0;
5861#endif // CONFIG_ALTREF2
5862#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005863 rc->is_src_frame_alt_ref = 0;
5864 }
5865 rc->source_alt_ref_pending = 0;
5866 }
5867
5868#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005869#if CONFIG_ALTREF2
5870 // Should we encode an arf2 frame.
5871 arf_src_index = get_arf2_src_index(cpi);
5872 if (arf_src_index) {
5873 for (i = 0; i <= arf_src_index; ++i) {
5874 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
5875 // Avoid creating an alt-ref if there's a forced keyframe pending.
5876 if (e == NULL) {
5877 break;
5878 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
5879 arf_src_index = 0;
5880 flush = 1;
5881 break;
5882 }
5883 }
5884 }
5885
5886 if (arf_src_index) {
5887 assert(arf_src_index <= rc->frames_to_key);
5888
5889 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
5890 cpi->alt_ref_source = source;
5891
5892 if (oxcf->arnr_max_frames > 0) {
5893 // Produce the filtered ARF frame.
5894 av1_temporal_filter(cpi, arf_src_index);
5895 aom_extend_frame_borders(&cpi->alt_ref_buffer);
5896 force_src_buffer = &cpi->alt_ref_buffer;
5897 }
5898
5899 cm->show_frame = 0;
5900 cm->intra_only = 0;
5901 cpi->refresh_alt2_ref_frame = 1;
5902 cpi->refresh_last_frame = 0;
5903 cpi->refresh_golden_frame = 0;
5904 cpi->refresh_bwd_ref_frame = 0;
5905 cpi->refresh_alt_ref_frame = 0;
5906 rc->is_src_frame_alt_ref = 0;
5907 rc->is_src_frame_ext_arf = 0;
5908 }
5909 rc->source_alt_ref_pending = 0;
5910 }
5911#endif // CONFIG_ALTREF2
5912
Yaowu Xuc27fc142016-08-22 16:08:15 -07005913 rc->is_bwd_ref_frame = 0;
5914 brf_src_index = get_brf_src_index(cpi);
5915 if (brf_src_index) {
5916 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07005917 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005918 cm->show_frame = 0;
5919 cm->intra_only = 0;
5920
5921 cpi->refresh_bwd_ref_frame = 1;
5922 cpi->refresh_last_frame = 0;
5923 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005924#if CONFIG_ALTREF2
5925 cpi->refresh_alt2_ref_frame = 0;
5926#endif // CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07005927 cpi->refresh_alt_ref_frame = 0;
5928
5929 rc->is_bwd_ref_frame = 1;
5930 }
5931 }
5932#endif // CONFIG_EXT_REFS
5933
5934 if (!source) {
5935 // Get last frame source.
5936 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005937 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005938 return -1;
5939 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005940 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005941 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005942 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005943
5944 if (source != NULL) {
5945 cm->show_frame = 1;
5946 cm->intra_only = 0;
5947
5948 // Check to see if the frame should be encoded as an arf overlay.
5949 check_src_altref(cpi, source);
5950 }
5951 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005952 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005953 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005954 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005955 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
5956
5957 *time_stamp = source->ts_start;
5958 *time_end = source->ts_end;
Yaowu Xuf883b422016-08-30 14:01:10 -07005959 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005960
5961 } else {
5962 *size = 0;
5963 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005964#if CONFIG_XIPHRC
5965 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 1);
5966#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005967 av1_end_first_pass(cpi); /* get last stats packet */
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005968#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005969 cpi->twopass.first_pass_done = 1;
5970 }
5971 return -1;
5972 }
5973
5974 if (source->ts_start < cpi->first_time_stamp_ever) {
5975 cpi->first_time_stamp_ever = source->ts_start;
5976 cpi->last_end_time_stamp_seen = source->ts_start;
5977 }
5978
5979 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005980 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005981
5982 // adjust frame rates based on timestamps given
5983 if (cm->show_frame) adjust_frame_rate(cpi, source);
5984
5985 // Find a free buffer for the new frame, releasing the reference previously
5986 // held.
5987 if (cm->new_fb_idx != INVALID_IDX) {
5988 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5989 }
5990 cm->new_fb_idx = get_free_fb(cm);
5991
5992 if (cm->new_fb_idx == INVALID_IDX) return -1;
5993
5994 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005995#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
5996 cm->cur_frame->buf.buf_8bit_valid = 0;
5997#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005998#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005999#if !CONFIG_ALTREF2
Yaowu Xuc27fc142016-08-22 16:08:15 -07006000 if (oxcf->pass == 2) {
6001 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
6002 cpi->alt_fb_idx = cpi->arf_map[gf_group->arf_ref_idx[gf_group->index]];
6003 }
Zoe Liue9b15e22017-07-19 15:53:01 -07006004#endif // !CONFIG_ALTREF2
6005#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006006 if (cpi->multi_arf_allowed) {
6007 if (cm->frame_type == KEY_FRAME) {
6008 init_buffer_indices(cpi);
6009 } else if (oxcf->pass == 2) {
6010 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
6011 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
6012 }
6013 }
Zoe Liu6cfaff92016-10-18 17:12:11 -07006014#endif // CONFIG_EXT_REFS
6015
Yaowu Xuc27fc142016-08-22 16:08:15 -07006016 // Start with a 0 size frame.
6017 *size = 0;
6018
6019 cpi->frame_flags = *frame_flags;
6020
6021 if (oxcf->pass == 2) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006022#if CONFIG_XIPHRC
6023 if (od_enc_rc_2pass_in(&cpi->od_rc) < 0) return -1;
6024 }
6025#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006026 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006027 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07006028 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006029 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006030#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006031
6032 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
6033 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i)
6034 cpi->scaled_ref_idx[i] = INVALID_IDX;
6035 }
6036
6037#if CONFIG_AOM_QM
6038 cm->using_qmatrix = cpi->oxcf.using_qm;
6039 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
6040 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
6041#endif
6042
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006043#if CONFIG_REFERENCE_BUFFER
6044 if (*time_stamp == 0) {
6045 cpi->common.current_frame_id = -1;
6046 }
6047#endif
6048
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006049#if CONFIG_XIPHRC
6050 if (oxcf->pass == 1) {
6051 size_t tmp;
6052 if (cpi->od_rc.cur_frame == 0) Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6053 cpi->od_rc.firstpass_quant = cpi->od_rc.target_quantizer;
6054 Pass0Encode(cpi, &tmp, dest, 0, frame_flags);
6055 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 0);
6056 } else if (oxcf->pass == 2) {
6057 Pass0Encode(cpi, size, dest, 0, frame_flags);
6058 } else {
6059 if (cpi->od_rc.cur_frame == 0) {
6060 size_t tmp;
6061 Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6062 }
6063 Pass0Encode(cpi, size, dest, 0, frame_flags);
6064 }
6065#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006066 if (oxcf->pass == 1) {
6067 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07006068 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006069 } else if (oxcf->pass == 2) {
6070 Pass2Encode(cpi, size, dest, frame_flags);
6071 } else {
6072 // One pass encode
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006073 Pass0Encode(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006074 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006075#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07006076#if CONFIG_HASH_ME
6077 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
6078 // add to hash table
6079 const int pic_width = cpi->source->y_crop_width;
6080 const int pic_height = cpi->source->y_crop_height;
6081 uint32_t *block_hash_values[2][2];
6082 int8_t *is_block_same[2][3];
6083 int k, j;
6084
6085 for (k = 0; k < 2; k++) {
6086 for (j = 0; j < 2; j++) {
6087 CHECK_MEM_ERROR(cm, block_hash_values[k][j],
6088 aom_malloc(sizeof(uint32_t) * pic_width * pic_height));
6089 }
6090
6091 for (j = 0; j < 3; j++) {
6092 CHECK_MEM_ERROR(cm, is_block_same[k][j],
6093 aom_malloc(sizeof(int8_t) * pic_width * pic_height));
6094 }
6095 }
6096
6097 av1_hash_table_create(&cm->cur_frame->hash_table);
6098 av1_generate_block_2x2_hash_value(cpi->source, block_hash_values[0],
6099 is_block_same[0]);
6100 av1_generate_block_hash_value(cpi->source, 4, block_hash_values[0],
6101 block_hash_values[1], is_block_same[0],
6102 is_block_same[1]);
6103 av1_generate_block_hash_value(cpi->source, 8, block_hash_values[1],
6104 block_hash_values[0], is_block_same[1],
6105 is_block_same[0]);
6106 av1_add_to_hash_map_by_row_with_precal_data(
6107 &cm->cur_frame->hash_table, block_hash_values[0], is_block_same[0][2],
6108 pic_width, pic_height, 8);
6109 av1_generate_block_hash_value(cpi->source, 16, block_hash_values[0],
6110 block_hash_values[1], is_block_same[0],
6111 is_block_same[1]);
6112 av1_add_to_hash_map_by_row_with_precal_data(
6113 &cm->cur_frame->hash_table, block_hash_values[1], is_block_same[1][2],
6114 pic_width, pic_height, 16);
6115 av1_generate_block_hash_value(cpi->source, 32, block_hash_values[1],
6116 block_hash_values[0], is_block_same[1],
6117 is_block_same[0]);
6118 av1_add_to_hash_map_by_row_with_precal_data(
6119 &cm->cur_frame->hash_table, block_hash_values[0], is_block_same[0][2],
6120 pic_width, pic_height, 32);
6121 av1_generate_block_hash_value(cpi->source, 64, block_hash_values[0],
6122 block_hash_values[1], is_block_same[0],
6123 is_block_same[1]);
6124 av1_add_to_hash_map_by_row_with_precal_data(
6125 &cm->cur_frame->hash_table, block_hash_values[1], is_block_same[1][2],
6126 pic_width, pic_height, 64);
6127
6128 for (k = 0; k < 2; k++) {
6129 for (j = 0; j < 2; j++) {
6130 aom_free(block_hash_values[k][j]);
6131 }
6132
6133 for (j = 0; j < 3; j++) {
6134 aom_free(is_block_same[k][j]);
6135 }
6136 }
6137 }
6138
6139#endif
6140
Thomas Daededa4d8b92017-06-05 15:44:14 -07006141#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -07006142 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006143#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006144 if (!cm->error_resilient_mode)
6145 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006146#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07006147
6148 // No frame encoded, or frame was dropped, release scaled references.
6149 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6150 release_scaled_references(cpi);
6151 }
6152
6153 if (*size > 0) {
6154 cpi->droppable = !frame_is_reference(cpi);
6155 }
6156
Yaowu Xuf883b422016-08-30 14:01:10 -07006157 aom_usec_timer_mark(&cmptimer);
6158 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006159
6160 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6161 generate_psnr_packet(cpi);
6162
6163#if CONFIG_INTERNAL_STATS
6164 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006165 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006166 }
6167#endif // CONFIG_INTERNAL_STATS
6168
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006169#if CONFIG_XIPHRC
6170 cpi->od_rc.cur_frame++;
6171#endif
6172
Yaowu Xuf883b422016-08-30 14:01:10 -07006173 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006174
6175 return 0;
6176}
6177
Yaowu Xuf883b422016-08-30 14:01:10 -07006178int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6179 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006180 if (!cm->show_frame) {
6181 return -1;
6182 } else {
6183 int ret;
6184 if (cm->frame_to_show) {
6185 *dest = *cm->frame_to_show;
6186 dest->y_width = cm->width;
6187 dest->y_height = cm->height;
6188 dest->uv_width = cm->width >> cm->subsampling_x;
6189 dest->uv_height = cm->height >> cm->subsampling_y;
6190 ret = 0;
6191 } else {
6192 ret = -1;
6193 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006194 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006195 return ret;
6196 }
6197}
6198
Yaowu Xuf883b422016-08-30 14:01:10 -07006199int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006200 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6201
6202 *frame =
6203 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6204 return 0;
6205}
6206
Yaowu Xuf883b422016-08-30 14:01:10 -07006207int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6208 AOM_SCALING vert_mode) {
6209 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006210 int hr = 0, hs = 0, vr = 0, vs = 0;
6211
6212 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6213
6214 Scale2Ratio(horiz_mode, &hr, &hs);
6215 Scale2Ratio(vert_mode, &vr, &vs);
6216
6217 // always go to the next whole number
6218 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6219 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
6220 assert(cm->width <= cpi->initial_width);
6221 assert(cm->height <= cpi->initial_height);
6222
6223 update_frame_size(cpi);
6224
6225 return 0;
6226}
6227
Fergus Simpson0a30c882017-05-16 16:54:51 -07006228int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006229 AV1_COMMON *cm = &cpi->common;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006230#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee887069f2017-06-16 18:54:36 -07006231 check_initial_width(cpi, cm->use_highbitdepth, cm->subsampling_x,
6232 cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006233#else
Debargha Mukherjee887069f2017-06-16 18:54:36 -07006234 check_initial_width(cpi, cm->subsampling_x, cm->subsampling_y);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006235#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006236
Fergus Simpson0a30c882017-05-16 16:54:51 -07006237 if (width <= 0 || height <= 0) return 1;
6238
6239 cm->width = width;
6240 if (cm->width > cpi->initial_width) {
6241 cm->width = cpi->initial_width;
6242 printf("Warning: Desired width too large, changed to %d\n", cm->width);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006243 }
6244
Fergus Simpson0a30c882017-05-16 16:54:51 -07006245 cm->height = height;
6246 if (cm->height > cpi->initial_height) {
6247 cm->height = cpi->initial_height;
6248 printf("Warning: Desired height too large, changed to %d\n", cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006249 }
Fergus Simpson0a30c882017-05-16 16:54:51 -07006250
Yaowu Xuc27fc142016-08-22 16:08:15 -07006251 assert(cm->width <= cpi->initial_width);
6252 assert(cm->height <= cpi->initial_height);
6253
6254 update_frame_size(cpi);
6255
6256 return 0;
6257}
6258
Yaowu Xuf883b422016-08-30 14:01:10 -07006259int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006260
Yaowu Xuf883b422016-08-30 14:01:10 -07006261void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006262 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07006263 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF)) {
6264 int ref = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006265
Yaowu Xuf883b422016-08-30 14:01:10 -07006266 if (flags & AOM_EFLAG_NO_REF_LAST) {
6267 ref ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006268#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006269 ref ^= AOM_LAST2_FLAG;
6270 ref ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006271#endif // CONFIG_EXT_REFS
6272 }
6273
Yaowu Xuf883b422016-08-30 14:01:10 -07006274 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006275
Yaowu Xuf883b422016-08-30 14:01:10 -07006276 if (flags & AOM_EFLAG_NO_REF_ARF) ref ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006277
Yaowu Xuf883b422016-08-30 14:01:10 -07006278 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006279 }
6280
6281 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07006282 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
6283 AOM_EFLAG_FORCE_GF | AOM_EFLAG_FORCE_ARF)) {
6284 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006285
Yaowu Xuf883b422016-08-30 14:01:10 -07006286 if (flags & AOM_EFLAG_NO_UPD_LAST) {
6287 upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006288#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006289 upd ^= AOM_LAST2_FLAG;
6290 upd ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006291#endif // CONFIG_EXT_REFS
6292 }
6293
Yaowu Xuf883b422016-08-30 14:01:10 -07006294 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006295
Yaowu Xuf883b422016-08-30 14:01:10 -07006296 if (flags & AOM_EFLAG_NO_UPD_ARF) upd ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006297
Yaowu Xuf883b422016-08-30 14:01:10 -07006298 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006299 }
6300
Yaowu Xuf883b422016-08-30 14:01:10 -07006301 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6302 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006303 }
6304}