blob: a55d33e9ef87403e9c840c5aa11959d4050a19da [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
13#include <math.h>
14#include <stdio.h>
15
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "./aom_config.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
18#include "av1/common/alloccommon.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010019#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020#include "av1/common/filter.h"
21#include "av1/common/idct.h"
22#include "av1/common/reconinter.h"
23#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070024#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "av1/common/tile_common.h"
26
27#include "av1/encoder/aq_complexity.h"
28#include "av1/encoder/aq_cyclicrefresh.h"
29#include "av1/encoder/aq_variance.h"
30#include "av1/encoder/bitstream.h"
Todd Nguyen302d0972017-06-16 16:16:29 -070031#if CONFIG_BGSPRITE
32#include "av1/encoder/bgsprite.h"
33#endif // CONFIG_BGSPRITE
Yaowu Xuc27fc142016-08-22 16:08:15 -070034#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -070035#include "aom_dsp/buf_ans.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070036#endif
37#include "av1/encoder/context_tree.h"
38#include "av1/encoder/encodeframe.h"
39#include "av1/encoder/encodemv.h"
40#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070041#if CONFIG_LV_MAP
42#include "av1/encoder/encodetxb.h"
43#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070044#include "av1/encoder/ethread.h"
45#include "av1/encoder/firstpass.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070046#if CONFIG_HASH_ME
47#include "av1/encoder/hash_motion.h"
48#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070049#include "av1/encoder/mbgraph.h"
50#include "av1/encoder/picklpf.h"
51#if CONFIG_LOOP_RESTORATION
52#include "av1/encoder/pickrst.h"
53#endif // CONFIG_LOOP_RESTORATION
Debargha Mukherjee7166f222017-09-05 21:32:42 -070054#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/encoder/ratectrl.h"
56#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070057#include "av1/encoder/segmentation.h"
58#include "av1/encoder/speed_features.h"
59#include "av1/encoder/temporal_filter.h"
60
Yaowu Xuf883b422016-08-30 14:01:10 -070061#include "./av1_rtcd.h"
62#include "./aom_dsp_rtcd.h"
63#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070064#include "aom_dsp/psnr.h"
65#if CONFIG_INTERNAL_STATS
66#include "aom_dsp/ssim.h"
67#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070068#include "aom_dsp/aom_dsp_common.h"
69#include "aom_dsp/aom_filter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070070#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070071#include "aom_ports/mem.h"
72#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070073#include "aom_scale/aom_scale.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070074#if CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070075#include "aom_util/debug_util.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070076#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070077
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080078#if CONFIG_ENTROPY_STATS
79FRAME_COUNTS aggregate_fc;
Zoe Liua56f9162017-06-21 22:49:57 -070080// Aggregate frame counts per frame context type
81FRAME_COUNTS aggregate_fc_per_type[FRAME_CONTEXTS];
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080082#endif // CONFIG_ENTROPY_STATS
83
Yaowu Xuc27fc142016-08-22 16:08:15 -070084#define AM_SEGMENT_ID_INACTIVE 7
85#define AM_SEGMENT_ID_ACTIVE 0
86
87#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
88
89#define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
90 // for altref computation.
91#define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
92 // mv. Choose a very high value for
93 // now so that HIGH_PRECISION is always
94 // chosen.
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070095
Yaowu Xuc27fc142016-08-22 16:08:15 -070096// #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);
Cheng Chend8184da2017-09-26 18:15:22 -0700178#if CONFIG_LOOPFILTER_LEVEL
179 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
180 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
181 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
182 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
183
184 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
185 -MAX_LOOP_FILTER);
186 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
187 -MAX_LOOP_FILTER);
188 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
189 -MAX_LOOP_FILTER);
190 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
191 -MAX_LOOP_FILTER);
192#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700193 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700194 // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
Yushin Chod728c212017-11-03 12:30:24 -0700195 // filter level being zero.
Yaowu Xuf883b422016-08-30 14:01:10 -0700196 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
197 -MAX_LOOP_FILTER);
Cheng Chend8184da2017-09-26 18:15:22 -0700198#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700200 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700201#if CONFIG_LOOPFILTER_LEVEL
202 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
203 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
204 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
205 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
206#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700207 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Cheng Chend8184da2017-09-26 18:15:22 -0700208#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700209 if (seg->enabled) {
210 seg->update_data = 1;
211 seg->update_map = 1;
212 }
213 }
214 cpi->active_map.update = 0;
215 }
216}
217
Yaowu Xuf883b422016-08-30 14:01:10 -0700218int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
219 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700220 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
221 unsigned char *const active_map_8x8 = cpi->active_map.map;
222 const int mi_rows = cpi->common.mi_rows;
223 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700224 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
225 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 cpi->active_map.update = 1;
227 if (new_map_16x16) {
228 int r, c;
229 for (r = 0; r < mi_rows; ++r) {
230 for (c = 0; c < mi_cols; ++c) {
231 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700232 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700233 ? AM_SEGMENT_ID_ACTIVE
234 : AM_SEGMENT_ID_INACTIVE;
235 }
236 }
237 cpi->active_map.enabled = 1;
238 } else {
239 cpi->active_map.enabled = 0;
240 }
241 return 0;
242 } else {
243 return -1;
244 }
245}
246
Yaowu Xuf883b422016-08-30 14:01:10 -0700247int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
248 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
250 new_map_16x16) {
251 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
252 const int mi_rows = cpi->common.mi_rows;
253 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700254 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
255 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
256
Yaowu Xuc27fc142016-08-22 16:08:15 -0700257 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
258 if (cpi->active_map.enabled) {
259 int r, c;
260 for (r = 0; r < mi_rows; ++r) {
261 for (c = 0; c < mi_cols; ++c) {
262 // Cyclic refresh segments are considered active despite not having
263 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700264 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
266 }
267 }
268 }
269 return 0;
270 } else {
271 return -1;
272 }
273}
274
RogerZhou3b635242017-09-19 10:06:46 -0700275static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv
276#if CONFIG_AMVR
277 ,
RogerZhou10a03802017-10-26 11:49:48 -0700278 int cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -0700279#endif
280 ) {
James Zern01a9d702017-08-25 19:09:33 +0000281 MACROBLOCK *const mb = &cpi->td.mb;
282 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
283
RogerZhou3b635242017-09-19 10:06:46 -0700284#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -0700285 if (cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0) {
RogerZhou3b635242017-09-19 10:06:46 -0700286#else
James Zern01a9d702017-08-25 19:09:33 +0000287 if (cpi->common.allow_high_precision_mv) {
RogerZhou3b635242017-09-19 10:06:46 -0700288#endif
James Zern01a9d702017-08-25 19:09:33 +0000289 int i;
290 for (i = 0; i < NMV_CONTEXTS; ++i) {
291 mb->mv_cost_stack[i] = mb->nmvcost_hp[i];
292 }
293 } else {
294 int i;
295 for (i = 0; i < NMV_CONTEXTS; ++i) {
296 mb->mv_cost_stack[i] = mb->nmvcost[i];
297 }
298 }
299}
300
Yaowu Xuf883b422016-08-30 14:01:10 -0700301static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700302#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700303 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304 return BLOCK_64X64;
305
Yaowu Xuf883b422016-08-30 14:01:10 -0700306 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307 return BLOCK_128X128;
308
Yaowu Xuf883b422016-08-30 14:01:10 -0700309 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310
311 assert(IMPLIES(cpi->common.tile_cols > 1,
312 cpi->common.tile_width % MAX_MIB_SIZE == 0));
313 assert(IMPLIES(cpi->common.tile_rows > 1,
314 cpi->common.tile_height % MAX_MIB_SIZE == 0));
315
316 // TODO(any): Possibly could improve this with a heuristic.
317 return BLOCK_128X128;
318#else
319 (void)cpi;
320 return BLOCK_64X64;
321#endif // CONFIG_EXT_PARTITION
322}
323
Yaowu Xuf883b422016-08-30 14:01:10 -0700324static void setup_frame(AV1_COMP *cpi) {
325 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326 // Set up entropy context depending on frame type. The decoder mandates
327 // the use of the default context, index 0, for keyframes and inter
328 // frames where the error_resilient_mode or intra_only flag is set. For
329 // other inter-frames the encoder currently uses only two contexts;
330 // context 1 for ALTREF frames and context 0 for the others.
331 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700332 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700334#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
335// Just use frame context from first signaled reference frame.
336// This will always be LAST_FRAME for now.
337#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700338 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -0700339 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700340 cm->frame_context_idx = EXT_ARF_FRAME;
341 else if (cpi->refresh_alt_ref_frame)
342 cm->frame_context_idx = ARF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343 else if (cpi->rc.is_src_frame_alt_ref)
344 cm->frame_context_idx = OVERLAY_FRAME;
345 else if (cpi->refresh_golden_frame)
346 cm->frame_context_idx = GLD_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347 else if (cpi->refresh_bwd_ref_frame)
348 cm->frame_context_idx = BRF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700349 else
350 cm->frame_context_idx = REGULAR_FRAME;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700351#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700352 }
353
354 if (cm->frame_type == KEY_FRAME) {
355 cpi->refresh_golden_frame = 1;
356 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700357 av1_zero(cpi->interp_filter_selected);
Debargha Mukherjeed2630fa2017-09-22 10:32:51 -0700358 set_sb_size(cm, select_sb_size(cpi));
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700359#if CONFIG_REFERENCE_BUFFER
360 set_use_reference_buffer(cm, 0);
361#endif // CONFIG_REFERENCE_BUFFER
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700363#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
364 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
365 cm->frame_refs[0].idx < 0) {
366 *cm->fc = cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
367 } else {
368 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
369 }
370#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700371 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700372#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuf883b422016-08-30 14:01:10 -0700373 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700374 }
Zoe Liuc01dddb2017-11-07 08:44:06 -0800375#if !CONFIG_EXT_COMP_REFS // No change to bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200376 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
377 cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
378 cpi->rc.is_bipred_frame = 1;
379 }
Zoe Liuc01dddb2017-11-07 08:44:06 -0800380#endif // !CONFIG_EXT_COMP_REFS
Thomas Daededa4d8b92017-06-05 15:44:14 -0700381#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -0700382 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
383 cm->frame_refs[0].idx < 0) {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700384 // use default frame context values
385 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
386 } else {
Thomas Daedea6a854b2017-06-22 17:49:11 -0700387 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700388 cm->pre_fc = &cm->frame_contexts[cm->frame_refs[0].idx];
389 }
390#else
Thomas Daede10e1da92017-04-26 13:22:21 -0700391 cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700392#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700393
394 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700395}
396
Cheng Chen46f30c72017-09-07 11:13:33 -0700397static void enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700398 int i;
399 cm->mi = cm->mip + cm->mi_stride + 1;
400 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
401 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
402 // Clear top border row
403 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
404 // Clear left border column
405 for (i = 1; i < cm->mi_rows + 1; ++i)
406 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700407 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
408 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
409
410 memset(cm->mi_grid_base, 0,
411 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
412}
413
Cheng Chen46f30c72017-09-07 11:13:33 -0700414static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700415 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700416 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700417 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700418 if (!cm->prev_mip) return 1;
419 cm->mi_alloc_size = mi_size;
420
Yaowu Xuf883b422016-08-30 14:01:10 -0700421 cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700422 if (!cm->mi_grid_base) return 1;
423 cm->prev_mi_grid_base =
Yaowu Xuf883b422016-08-30 14:01:10 -0700424 (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700425 if (!cm->prev_mi_grid_base) return 1;
426
427 return 0;
428}
429
Cheng Chen46f30c72017-09-07 11:13:33 -0700430static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700431 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700432 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700433 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700434 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700435 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700436 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700437 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700438 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700439 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700440}
441
Cheng Chen46f30c72017-09-07 11:13:33 -0700442static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700443 // Current mip will be the prev_mip for the next frame.
444 MODE_INFO **temp_base = cm->prev_mi_grid_base;
445 MODE_INFO *temp = cm->prev_mip;
446 cm->prev_mip = cm->mip;
447 cm->mip = temp;
448
449 // Update the upper left visible macroblock ptrs.
450 cm->mi = cm->mip + cm->mi_stride + 1;
451 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
452
453 cm->prev_mi_grid_base = cm->mi_grid_base;
454 cm->mi_grid_base = temp_base;
455 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
456 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
457}
458
Yaowu Xuf883b422016-08-30 14:01:10 -0700459void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700460 static volatile int init_done = 0;
461
462 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700463 av1_rtcd();
464 aom_dsp_rtcd();
465 aom_scale_rtcd();
466 av1_init_intra_predictors();
467 av1_init_me_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000468#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -0700469 av1_rc_init_minq_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000470#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700471 av1_entropy_mv_init();
472 av1_encode_token_init();
Yaowu Xuf883b422016-08-30 14:01:10 -0700473 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700474 init_done = 1;
475 }
476}
477
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700478static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
479 if (cpi->mbmi_ext_base) {
480 aom_free(cpi->mbmi_ext_base);
481 cpi->mbmi_ext_base = NULL;
482 }
483}
484
485static void alloc_context_buffers_ext(AV1_COMP *cpi) {
486 AV1_COMMON *cm = &cpi->common;
487 int mi_size = cm->mi_cols * cm->mi_rows;
488
489 dealloc_context_buffers_ext(cpi);
490 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
491 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
492}
493
Yaowu Xuf883b422016-08-30 14:01:10 -0700494static void dealloc_compressor_data(AV1_COMP *cpi) {
495 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700496
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700497 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700498
Yaowu Xuf883b422016-08-30 14:01:10 -0700499 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700500 cpi->tile_data = NULL;
501
502 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700503 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700505
Yaowu Xuf883b422016-08-30 14:01:10 -0700506 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700507 cpi->cyclic_refresh = NULL;
508
Yaowu Xuf883b422016-08-30 14:01:10 -0700509 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510 cpi->active_map.map = NULL;
511
Jingning Hand064cf02017-06-01 10:00:39 -0700512 aom_free(cpi->td.mb.above_pred_buf);
513 cpi->td.mb.above_pred_buf = NULL;
514
515 aom_free(cpi->td.mb.left_pred_buf);
516 cpi->td.mb.left_pred_buf = NULL;
517
518 aom_free(cpi->td.mb.wsrc_buf);
519 cpi->td.mb.wsrc_buf = NULL;
520
521 aom_free(cpi->td.mb.mask_buf);
522 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700523
Jingning Han6cc1fd32017-10-13 09:05:36 -0700524#if CONFIG_MFMV
525 aom_free(cm->tpl_mvs);
526 cm->tpl_mvs = NULL;
527#endif
528
Yaowu Xuf883b422016-08-30 14:01:10 -0700529 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700530#if CONFIG_LV_MAP
531 av1_free_txb_buf(cpi);
532#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700533 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534
Yaowu Xuf883b422016-08-30 14:01:10 -0700535 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700537 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800538 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700539#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700540 aom_free_frame_buffer(&cpi->scaled_source);
541 aom_free_frame_buffer(&cpi->scaled_last_source);
542 aom_free_frame_buffer(&cpi->alt_ref_buffer);
543 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544
Yaowu Xuf883b422016-08-30 14:01:10 -0700545 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700546 cpi->tile_tok[0][0] = 0;
547
Yaowu Xuf883b422016-08-30 14:01:10 -0700548 av1_free_pc_tree(&cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549
hui sud9a812b2017-07-06 14:34:37 -0700550 aom_free(cpi->td.mb.palette_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -0700553 aom_buf_ans_free(&cpi->buf_ans);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554#endif // CONFIG_ANS
555}
556
Yaowu Xuf883b422016-08-30 14:01:10 -0700557static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700558 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700559 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000560 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700561
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200562 // Stores a snapshot of key state variables which can subsequently be
563 // restored with a call to av1_restore_coding_context. These functions are
564 // intended for use in a re-code loop in av1_compress_frame where the
565 // quantizer value is adjusted between loop iterations.
James Zern01a9d702017-08-25 19:09:33 +0000566 for (i = 0; i < NMV_CONTEXTS; ++i) {
567 av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
568 av1_copy(cc->nmv_costs, cpi->nmv_costs);
569 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
570 }
571
Yaowu Xuf883b422016-08-30 14:01:10 -0700572 av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
573 av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700574
575 cc->fc = *cm->fc;
576}
577
Yaowu Xuf883b422016-08-30 14:01:10 -0700578static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700580 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000581 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200583 // Restore key state variables to the snapshot state stored in the
584 // previous call to av1_save_coding_context.
James Zern01a9d702017-08-25 19:09:33 +0000585 for (i = 0; i < NMV_CONTEXTS; ++i) {
586 av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
587 av1_copy(cpi->nmv_costs, cc->nmv_costs);
588 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
589 }
590
Yaowu Xuf883b422016-08-30 14:01:10 -0700591 av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
592 av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593
594 *cm->fc = cc->fc;
595}
596
Yaowu Xuf883b422016-08-30 14:01:10 -0700597static void configure_static_seg_features(AV1_COMP *cpi) {
598 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700599 const RATE_CONTROL *const rc = &cpi->rc;
600 struct segmentation *const seg = &cm->seg;
601
602 int high_q = (int)(rc->avg_q > 48.0);
603 int qi_delta;
604
605 // Disable and clear down for KF
606 if (cm->frame_type == KEY_FRAME) {
607 // Clear down the global segmentation map
608 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
609 seg->update_map = 0;
610 seg->update_data = 0;
611 cpi->static_mb_pct = 0;
612
613 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700614 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700615
616 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700617 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700618 } else if (cpi->refresh_alt_ref_frame) {
619 // If this is an alt ref frame
620 // Clear down the global segmentation map
621 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
622 seg->update_map = 0;
623 seg->update_data = 0;
624 cpi->static_mb_pct = 0;
625
626 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700627 av1_disable_segmentation(seg);
628 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629
630 // Scan frames from current to arf frame.
631 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700632 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700633
634 // If segmentation was enabled set those features needed for the
635 // arf itself.
636 if (seg->enabled) {
637 seg->update_map = 1;
638 seg->update_data = 1;
639
640 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700641 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
642 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700643#if CONFIG_LOOPFILTER_LEVEL
644 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
645 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
646 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
647 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
648
649 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
650 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
651 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
652 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
653#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Cheng Chend8184da2017-09-26 18:15:22 -0700655 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
656#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700657
Yaowu Xuf883b422016-08-30 14:01:10 -0700658 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700659 }
660 } else if (seg->enabled) {
661 // All other frames if segmentation has been enabled
662
663 // First normal frame in a valid gf or alt ref group
664 if (rc->frames_since_golden == 0) {
665 // Set up segment features for normal frames in an arf group
666 if (rc->source_alt_ref_active) {
667 seg->update_map = 0;
668 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700669
Yaowu Xuf883b422016-08-30 14:01:10 -0700670 qi_delta =
671 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
672 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
673 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700674
Cheng Chend8184da2017-09-26 18:15:22 -0700675#if CONFIG_LOOPFILTER_LEVEL
676 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
677 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
678 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
679 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
680
681 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
682 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
683 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
684 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
685#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700686 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
687 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Cheng Chend8184da2017-09-26 18:15:22 -0700688#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689
690 // Segment coding disabled for compred testing
691 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
693 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
694 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700695 }
696 } else {
697 // Disable segmentation and clear down features if alt ref
698 // is not active for this group
699
Yaowu Xuf883b422016-08-30 14:01:10 -0700700 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700701
702 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
703
704 seg->update_map = 0;
705 seg->update_data = 0;
706
Yaowu Xuf883b422016-08-30 14:01:10 -0700707 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700708 }
709 } else if (rc->is_src_frame_alt_ref) {
710 // Special case where we are coding over the top of a previous
711 // alt ref frame.
712 // Segment coding disabled for compred testing
713
714 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700715 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
716 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700717
718 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700719 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
720 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
721 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
722 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723
724 // Skip all MBs if high Q (0,0 mv and skip coeffs)
725 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
727 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728 }
729 // Enable data update
730 seg->update_data = 1;
731 } else {
732 // All other frames.
733
734 // No updates.. leave things as they are.
735 seg->update_map = 0;
736 seg->update_data = 0;
737 }
738 }
739}
740
Yaowu Xuf883b422016-08-30 14:01:10 -0700741static void update_reference_segmentation_map(AV1_COMP *cpi) {
742 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700743 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
Soo-Chul Han934af352017-10-15 15:21:51 -0400744#if CONFIG_SEGMENT_PRED_LAST
745 uint8_t *cache_ptr = cm->current_frame_seg_map;
746#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700747 uint8_t *cache_ptr = cm->last_frame_seg_map;
Soo-Chul Han934af352017-10-15 15:21:51 -0400748#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700749 int row, col;
750
751 for (row = 0; row < cm->mi_rows; row++) {
752 MODE_INFO **mi_8x8 = mi_8x8_ptr;
753 uint8_t *cache = cache_ptr;
754 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
755 cache[0] = mi_8x8[0]->mbmi.segment_id;
756 mi_8x8_ptr += cm->mi_stride;
757 cache_ptr += cm->mi_cols;
758 }
759}
760
Yaowu Xuf883b422016-08-30 14:01:10 -0700761static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
762 AV1_COMMON *cm = &cpi->common;
763 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700764
765 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700766 cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
767 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200768#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700769 cm->use_highbitdepth,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700771 oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700772 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700773 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774 "Failed to allocate lag buffers");
775
776 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700777 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200779#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 cm->use_highbitdepth,
781#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700782 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
783 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700784 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700785 "Failed to allocate altref buffer");
786}
787
Yaowu Xuf883b422016-08-30 14:01:10 -0700788static void alloc_util_frame_buffers(AV1_COMP *cpi) {
789 AV1_COMMON *const cm = &cpi->common;
790 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200792#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793 cm->use_highbitdepth,
794#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700795 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
796 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700797 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700798 "Failed to allocate last frame buffer");
799
800#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700801 if (aom_realloc_frame_buffer(
802 &cpi->trial_frame_rst,
803#if CONFIG_FRAME_SUPERRES
804 cm->superres_upscaled_width, cm->superres_upscaled_height,
805#else
806 cm->width, cm->height,
807#endif // CONFIG_FRAME_SUPERRES
808 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200809#if CONFIG_HIGHBITDEPTH
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700810 cm->use_highbitdepth,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800811#endif
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700812 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800813 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800814 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700815#endif // CONFIG_LOOP_RESTORATION
816
Yaowu Xuf883b422016-08-30 14:01:10 -0700817 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200819#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820 cm->use_highbitdepth,
821#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700822 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
823 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700824 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700825 "Failed to allocate scaled source buffer");
826
Yaowu Xuf883b422016-08-30 14:01:10 -0700827 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200829#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700830 cm->use_highbitdepth,
831#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700832 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
833 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700834 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 "Failed to allocate scaled last source buffer");
836}
837
Cheng Chen46f30c72017-09-07 11:13:33 -0700838static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700839 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700840
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700843#if CONFIG_LV_MAP
844 av1_alloc_txb_buf(cpi);
845#endif
846
Yaowu Xuc27fc142016-08-22 16:08:15 -0700847 alloc_context_buffers_ext(cpi);
848
Yaowu Xuf883b422016-08-30 14:01:10 -0700849 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700850
851 {
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +0000852 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols,
853 MAX_SB_SIZE_LOG2, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700854 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700855 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856 }
857
Yaowu Xuf883b422016-08-30 14:01:10 -0700858 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859}
860
Yaowu Xuf883b422016-08-30 14:01:10 -0700861void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700862 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000863#if CONFIG_XIPHRC
864 if (!cpi->od_rc.cur_frame) return;
865 cpi->od_rc.framerate = cpi->framerate;
866 od_enc_rc_resize(&cpi->od_rc);
867#else
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700868 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000869#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870}
871
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200872#if CONFIG_MAX_TILE
873
874static void set_tile_info_max_tile(AV1_COMP *cpi) {
875 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200876 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200877
878 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200879
880 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200881 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200882 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200883 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
884 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200885 } else {
Yaowu Xu81d458b2017-11-07 15:30:13 -0800886 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
887 int sb_cols = mi_cols >> MAX_MIB_SIZE_LOG2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200888 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200889 cm->uniform_tile_spacing_flag = 0;
890 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
891 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200892 size_sb = cpi->oxcf.tile_widths[j++];
893 if (j >= cpi->oxcf.tile_width_count) j = 0;
894 start_sb += AOMMIN(size_sb, MAX_TILE_WIDTH_SB);
Dominic Symesf58f1112017-09-25 12:47:40 +0200895 }
896 cm->tile_cols = i;
897 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200898 }
899 av1_calculate_tile_cols(cm);
900
901 // configure tile rows
902 if (cm->uniform_tile_spacing_flag) {
903 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
904 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200905 } else {
Yaowu Xu81d458b2017-11-07 15:30:13 -0800906 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
907 int sb_rows = mi_rows >> MAX_MIB_SIZE_LOG2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200908 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200909 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
910 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200911 size_sb = cpi->oxcf.tile_heights[j++];
912 if (j >= cpi->oxcf.tile_height_count) j = 0;
913 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200914 }
915 cm->tile_rows = i;
916 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200917 }
918 av1_calculate_tile_rows(cm);
919}
920
921#endif
922
Yaowu Xuf883b422016-08-30 14:01:10 -0700923static void set_tile_info(AV1_COMP *cpi) {
924 AV1_COMMON *const cm = &cpi->common;
Yue Chen4eba69b2017-11-09 22:37:35 -0800925 (void)cm;
Thomas Daviesb25ba502017-07-18 10:18:24 +0100926#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -0800927 int tile_row, tile_col, num_tiles_in_tg;
928 int tg_row_start, tg_col_start;
929#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700930#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700931 if (cpi->oxcf.large_scale_tile) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700932#if CONFIG_EXT_PARTITION
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700933 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
934 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
935 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
936 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
937 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
938 } else {
939 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
940 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
941 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
942 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
943 }
944#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
946 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700947 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
948 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700949#endif // CONFIG_EXT_PARTITION
950
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700951 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
952 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700953
Yaowu Xu8d0039c2017-10-13 07:51:46 -0700954 assert(cm->tile_width >> MAX_MIB_SIZE_LOG2 <= 32);
955 assert(cm->tile_height >> MAX_MIB_SIZE_LOG2 <= 32);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700956
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700957 // Get the number of tiles
958 cm->tile_cols = 1;
959 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700961 cm->tile_rows = 1;
962 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
963 } else {
964#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700965
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200966#if CONFIG_MAX_TILE
967 set_tile_info_max_tile(cpi);
Sebastien Alaiwan619f4172017-11-13 17:13:42 +0100968 (void)cm;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200969#else
970 int min_log2_tile_cols, max_log2_tile_cols;
971 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700972
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200973 cm->log2_tile_cols =
974 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
975 cm->log2_tile_rows = cpi->oxcf.tile_rows;
Rupert Swarbrick940942b2017-09-01 12:05:55 +0100976
Rupert Swarbrick5a010aa2017-09-26 16:16:48 +0100977 cm->tile_width =
978 get_tile_size(cm->mi_cols, cm->log2_tile_cols, &cm->tile_cols);
979 cm->tile_height =
980 get_tile_size(cm->mi_rows, cm->log2_tile_rows, &cm->tile_rows);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200981#endif // CONFIG_MAX_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700982#if CONFIG_EXT_TILE
983 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700984#endif // CONFIG_EXT_TILE
Ryan Lei7386eda2016-12-08 21:08:31 -0800985
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800986#if CONFIG_DEPENDENT_HORZTILES
987 cm->dependent_horz_tiles = cpi->oxcf.dependent_horz_tiles;
Fangwen Fu70bcb892017-05-06 17:05:19 -0700988#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700989 if (cm->large_scale_tile) {
990 // May not needed since cpi->oxcf.dependent_horz_tiles is already adjusted.
991 cm->dependent_horz_tiles = 0;
Fangwen Fu73126c02017-02-08 22:37:47 -0800992 } else {
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700993#endif // CONFIG_EXT_TILE
994 if (cm->log2_tile_rows == 0) cm->dependent_horz_tiles = 0;
995#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -0800996 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700997#endif // CONFIG_EXT_TILE
998
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700999#if CONFIG_EXT_TILE
1000 if (!cm->large_scale_tile) {
1001#endif // CONFIG_EXT_TILE
1002 if (cpi->oxcf.mtu == 0) {
1003 cm->num_tg = cpi->oxcf.num_tile_groups;
1004 } else {
1005 // Use a default value for the purposes of weighting costs in probability
1006 // updates
1007 cm->num_tg = DEFAULT_MAX_NUM_TG;
Fangwen Fu73126c02017-02-08 22:37:47 -08001008 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001009 num_tiles_in_tg =
1010 (cm->tile_cols * cm->tile_rows + cm->num_tg - 1) / cm->num_tg;
1011 tg_row_start = 0;
1012 tg_col_start = 0;
1013 for (tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
1014 for (tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
1015 if ((tile_row * cm->tile_cols + tile_col) % num_tiles_in_tg == 0) {
1016 tg_row_start = tile_row;
1017 tg_col_start = tile_col;
1018 }
1019 cm->tile_group_start_row[tile_row][tile_col] = tg_row_start;
1020 cm->tile_group_start_col[tile_row][tile_col] = tg_col_start;
1021 }
1022 }
1023#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -08001024 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001025#endif // CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -08001026#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08001027
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001028#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08001029 cm->loop_filter_across_tiles_enabled =
1030 cpi->oxcf.loop_filter_across_tiles_enabled;
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001031#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001032}
1033
Yaowu Xuf883b422016-08-30 14:01:10 -07001034static void update_frame_size(AV1_COMP *cpi) {
1035 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001036 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1037
Yaowu Xuf883b422016-08-30 14:01:10 -07001038 av1_set_mb_mi(cm, cm->width, cm->height);
1039 av1_init_context_buffers(cm);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001040 av1_init_macroblockd(cm, xd,
Luc Trudeauf8164152017-04-11 16:20:51 -04001041#if CONFIG_CFL
1042 &NULL_CFL,
1043#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07001044 NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001045 memset(cpi->mbmi_ext_base, 0,
1046 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001047 set_tile_info(cpi);
1048}
1049
Yaowu Xuf883b422016-08-30 14:01:10 -07001050static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001051 int fb_idx;
1052 for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
1053 cpi->lst_fb_idxes[fb_idx] = fb_idx;
1054 cpi->gld_fb_idx = LAST_REF_FRAMES;
1055 cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07001056 cpi->alt2_fb_idx = LAST_REF_FRAMES + 2;
1057 cpi->alt_fb_idx = LAST_REF_FRAMES + 3;
Zoe Liu8dd1c982017-09-11 10:14:35 -07001058 cpi->ext_fb_idx = LAST_REF_FRAMES + 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001059 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
1060 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -07001061#if CONFIG_AMVR
1062 cpi->rate_index = 0;
1063 cpi->rate_size = 0;
1064 cpi->cur_poc = -1;
1065#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001066}
1067
Yaowu Xuf883b422016-08-30 14:01:10 -07001068static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1069 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001070
1071 cpi->oxcf = *oxcf;
1072 cpi->framerate = oxcf->init_framerate;
1073
1074 cm->profile = oxcf->profile;
1075 cm->bit_depth = oxcf->bit_depth;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001076#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001077 cm->use_highbitdepth = oxcf->use_highbitdepth;
1078#endif
1079 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07001080#if CONFIG_COLORSPACE_HEADERS
1081 cm->transfer_function = oxcf->transfer_function;
1082 cm->chroma_sample_position = oxcf->chroma_sample_position;
1083#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001084 cm->color_range = oxcf->color_range;
1085
1086 cm->width = oxcf->width;
1087 cm->height = oxcf->height;
Cheng Chen46f30c72017-09-07 11:13:33 -07001088 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001089
1090 // Single thread case: use counts in common.
1091 cpi->td.counts = &cm->counts;
1092
1093 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001094 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001095
1096 cpi->static_mb_pct = 0;
1097 cpi->ref_frame_flags = 0;
1098
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001099 // Reset resize pending flags
1100 cpi->resize_pending_width = 0;
1101 cpi->resize_pending_height = 0;
1102
Yaowu Xuc27fc142016-08-22 16:08:15 -07001103 init_buffer_indices(cpi);
1104}
1105
1106static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001107 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001108 const int64_t bandwidth = oxcf->target_bandwidth;
1109 const int64_t starting = oxcf->starting_buffer_level_ms;
1110 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1111 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1112
1113 rc->starting_buffer_level = starting * bandwidth / 1000;
1114 rc->optimal_buffer_level =
1115 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1116 rc->maximum_buffer_size =
1117 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1118}
1119
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001120#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001121#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
1122 cpi->fn_ptr[BT].sdf = SDF; \
1123 cpi->fn_ptr[BT].sdaf = SDAF; \
1124 cpi->fn_ptr[BT].vf = VF; \
1125 cpi->fn_ptr[BT].svf = SVF; \
1126 cpi->fn_ptr[BT].svaf = SVAF; \
1127 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1128 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1129 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1130
1131#define MAKE_BFP_SAD_WRAPPER(fnname) \
1132 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1133 int source_stride, \
1134 const uint8_t *ref_ptr, int ref_stride) { \
1135 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1136 } \
1137 static unsigned int fnname##_bits10( \
1138 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1139 int ref_stride) { \
1140 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1141 } \
1142 static unsigned int fnname##_bits12( \
1143 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1144 int ref_stride) { \
1145 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1146 }
1147
1148#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1149 static unsigned int fnname##_bits8( \
1150 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1151 int ref_stride, const uint8_t *second_pred) { \
1152 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1153 } \
1154 static unsigned int fnname##_bits10( \
1155 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1156 int ref_stride, const uint8_t *second_pred) { \
1157 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1158 2; \
1159 } \
1160 static unsigned int fnname##_bits12( \
1161 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1162 int ref_stride, const uint8_t *second_pred) { \
1163 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1164 4; \
1165 }
1166
1167#define MAKE_BFP_SAD3_WRAPPER(fnname) \
1168 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1169 const uint8_t *ref_ptr, int ref_stride, \
1170 unsigned int *sad_array) { \
1171 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1172 } \
1173 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1174 const uint8_t *ref_ptr, int ref_stride, \
1175 unsigned int *sad_array) { \
1176 int i; \
1177 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1178 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1179 } \
1180 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1181 const uint8_t *ref_ptr, int ref_stride, \
1182 unsigned int *sad_array) { \
1183 int i; \
1184 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1185 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1186 }
1187
1188#define MAKE_BFP_SAD8_WRAPPER(fnname) \
1189 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1190 const uint8_t *ref_ptr, int ref_stride, \
1191 unsigned int *sad_array) { \
1192 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1193 } \
1194 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1195 const uint8_t *ref_ptr, int ref_stride, \
1196 unsigned int *sad_array) { \
1197 int i; \
1198 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1199 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1200 } \
1201 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1202 const uint8_t *ref_ptr, int ref_stride, \
1203 unsigned int *sad_array) { \
1204 int i; \
1205 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1206 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1207 }
1208#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1209 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1210 const uint8_t *const ref_ptr[], int ref_stride, \
1211 unsigned int *sad_array) { \
1212 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1213 } \
1214 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1215 const uint8_t *const ref_ptr[], int ref_stride, \
1216 unsigned int *sad_array) { \
1217 int i; \
1218 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1219 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1220 } \
1221 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1222 const uint8_t *const ref_ptr[], int ref_stride, \
1223 unsigned int *sad_array) { \
1224 int i; \
1225 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1226 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1227 }
1228
1229#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001230MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1231MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1232MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
1233MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
1234MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1235MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1236MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1237MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1238MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1239MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1240MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001241#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001242MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1243MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1244MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1245MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1246MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1247MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1248MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1249MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1250MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1251MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1252MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1253MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1254MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1255MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1256MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
1257MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
1258MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1259MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1260MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1261MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
1262MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
1263MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1264MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1265MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1266MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
1267MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
1268MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1269MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1270MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1271MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
1272MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
1273MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1274MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1275MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1276MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
1277MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
1278MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1279MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1280MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1281MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
1282MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
1283MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1284MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1285MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1286MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
1287MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1288MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1289MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1290MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
1291MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1292MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1293MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1294MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
1295MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
1296MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001297
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001298#if CONFIG_EXT_PARTITION_TYPES
1299MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1300MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1301MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1302MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1303MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1304MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1305MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1306MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1307MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1308MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1309MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1310MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001311MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1312MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1313MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1314MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1315MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1316MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001317#if CONFIG_EXT_PARTITION
1318MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x128)
1319MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x128_avg)
1320MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x128x4d)
1321MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x32)
1322MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x32_avg)
1323MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x32x4d)
1324#endif // CONFIG_EXT_PARTITION
1325#endif // CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001326
David Barker0f3c94e2017-05-16 15:21:50 +01001327#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001328 cpi->fn_ptr[BT].msdf = MCSDF; \
1329 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001330
David Barkerc155e012017-05-11 13:54:54 +01001331#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1332 static unsigned int fnname##_bits8( \
1333 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1334 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1335 int m_stride, int invert_mask) { \
1336 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1337 second_pred_ptr, m, m_stride, invert_mask); \
1338 } \
1339 static unsigned int fnname##_bits10( \
1340 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1341 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1342 int m_stride, int invert_mask) { \
1343 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1344 second_pred_ptr, m, m_stride, invert_mask) >> \
1345 2; \
1346 } \
1347 static unsigned int fnname##_bits12( \
1348 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1349 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1350 int m_stride, int invert_mask) { \
1351 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1352 second_pred_ptr, m, m_stride, invert_mask) >> \
1353 4; \
1354 }
1355
Yaowu Xuc27fc142016-08-22 16:08:15 -07001356#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001357MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1358MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1359MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001360#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001361MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1362MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1363MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1364MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1365MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1366MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1367MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1368MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1369MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1370MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1371MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1372MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1373MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001374
1375#if CONFIG_EXT_PARTITION_TYPES
1376MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1377MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1378MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1379MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001380MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1381MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001382#if CONFIG_EXT_PARTITION
1383MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x128)
1384MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x32)
1385#endif // CONFIG_EXT_PARTITION
1386#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001387
Yaowu Xuc27fc142016-08-22 16:08:15 -07001388#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1389 cpi->fn_ptr[BT].osdf = OSDF; \
1390 cpi->fn_ptr[BT].ovf = OVF; \
1391 cpi->fn_ptr[BT].osvf = OSVF;
1392
1393#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1394 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1395 const int32_t *wsrc, \
1396 const int32_t *msk) { \
1397 return fnname(ref, ref_stride, wsrc, msk); \
1398 } \
1399 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1400 const int32_t *wsrc, \
1401 const int32_t *msk) { \
1402 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1403 } \
1404 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1405 const int32_t *wsrc, \
1406 const int32_t *msk) { \
1407 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1408 }
1409
1410#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001411MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1412MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1413MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001414#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001415MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1416MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1417MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1418MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1419MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1420MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1421MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1422MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1423MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1424MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1425MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1426MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1427MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001428
1429#if CONFIG_EXT_PARTITION_TYPES
1430MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1431MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1432MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1433MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001434MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1435MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001436#if CONFIG_EXT_PARTITION
1437MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x128)
1438MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x32)
1439#endif // CONFIG_EXT_PARTITION
1440#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001441
Yaowu Xuf883b422016-08-30 14:01:10 -07001442static void highbd_set_var_fns(AV1_COMP *const cpi) {
1443 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001444 if (cm->use_highbitdepth) {
1445 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001446 case AOM_BITS_8:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001447#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001448#if CONFIG_EXT_PARTITION
1449 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits8,
1450 aom_highbd_sad128x32_avg_bits8, aom_highbd_8_variance128x32,
1451 aom_highbd_8_sub_pixel_variance128x32,
1452 aom_highbd_8_sub_pixel_avg_variance128x32, NULL, NULL,
1453 aom_highbd_sad128x32x4d_bits8)
1454
1455 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits8,
1456 aom_highbd_sad32x128_avg_bits8, aom_highbd_8_variance32x128,
1457 aom_highbd_8_sub_pixel_variance32x128,
1458 aom_highbd_8_sub_pixel_avg_variance32x128, NULL, NULL,
1459 aom_highbd_sad32x128x4d_bits8)
1460#endif // CONFIG_EXT_PARTITION
1461
Rupert Swarbrick72678572017-08-02 12:05:26 +01001462 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1463 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1464 aom_highbd_8_sub_pixel_variance64x16,
1465 aom_highbd_8_sub_pixel_avg_variance64x16, NULL, NULL,
1466 aom_highbd_sad64x16x4d_bits8)
1467
1468 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1469 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1470 aom_highbd_8_sub_pixel_variance16x64,
1471 aom_highbd_8_sub_pixel_avg_variance16x64, NULL, NULL,
1472 aom_highbd_sad16x64x4d_bits8)
1473
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001474 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits8,
1475 aom_highbd_sad32x8_avg_bits8, aom_highbd_8_variance32x8,
1476 aom_highbd_8_sub_pixel_variance32x8,
1477 aom_highbd_8_sub_pixel_avg_variance32x8, NULL, NULL,
1478 aom_highbd_sad32x8x4d_bits8)
1479
1480 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits8,
1481 aom_highbd_sad8x32_avg_bits8, aom_highbd_8_variance8x32,
1482 aom_highbd_8_sub_pixel_variance8x32,
1483 aom_highbd_8_sub_pixel_avg_variance8x32, NULL, NULL,
1484 aom_highbd_sad8x32x4d_bits8)
1485
1486 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits8,
1487 aom_highbd_sad16x4_avg_bits8, aom_highbd_8_variance16x4,
1488 aom_highbd_8_sub_pixel_variance16x4,
1489 aom_highbd_8_sub_pixel_avg_variance16x4, NULL, NULL,
1490 aom_highbd_sad16x4x4d_bits8)
1491
1492 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits8,
1493 aom_highbd_sad4x16_avg_bits8, aom_highbd_8_variance4x16,
1494 aom_highbd_8_sub_pixel_variance4x16,
1495 aom_highbd_8_sub_pixel_avg_variance4x16, NULL, NULL,
1496 aom_highbd_sad4x16x4d_bits8)
1497#endif
1498
Yaowu Xuf883b422016-08-30 14:01:10 -07001499 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1500 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1501 aom_highbd_8_sub_pixel_variance32x16,
1502 aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1503 aom_highbd_sad32x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001504
Yaowu Xuf883b422016-08-30 14:01:10 -07001505 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1506 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1507 aom_highbd_8_sub_pixel_variance16x32,
1508 aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1509 aom_highbd_sad16x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001510
Yaowu Xuf883b422016-08-30 14:01:10 -07001511 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1512 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1513 aom_highbd_8_sub_pixel_variance64x32,
1514 aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1515 aom_highbd_sad64x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001516
Yaowu Xuf883b422016-08-30 14:01:10 -07001517 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1518 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1519 aom_highbd_8_sub_pixel_variance32x64,
1520 aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1521 aom_highbd_sad32x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001522
Yaowu Xuf883b422016-08-30 14:01:10 -07001523 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1524 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1525 aom_highbd_8_sub_pixel_variance32x32,
1526 aom_highbd_8_sub_pixel_avg_variance32x32,
1527 aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
1528 aom_highbd_sad32x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001529
Yaowu Xuf883b422016-08-30 14:01:10 -07001530 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1531 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1532 aom_highbd_8_sub_pixel_variance64x64,
1533 aom_highbd_8_sub_pixel_avg_variance64x64,
1534 aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
1535 aom_highbd_sad64x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001536
Yaowu Xuf883b422016-08-30 14:01:10 -07001537 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1538 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1539 aom_highbd_8_sub_pixel_variance16x16,
1540 aom_highbd_8_sub_pixel_avg_variance16x16,
1541 aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
1542 aom_highbd_sad16x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001543
1544 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001545 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1546 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1547 aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
1548 aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001549
1550 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001551 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1552 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1553 aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
1554 aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001555
1556 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001557 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1558 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1559 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits8,
1560 aom_highbd_sad8x8x8_bits8, aom_highbd_sad8x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001561
Yaowu Xuf883b422016-08-30 14:01:10 -07001562 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1563 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1564 aom_highbd_8_sub_pixel_variance8x4,
1565 aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
1566 aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001567
Yaowu Xuf883b422016-08-30 14:01:10 -07001568 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1569 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1570 aom_highbd_8_sub_pixel_variance4x8,
1571 aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
1572 aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001573
1574 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001575 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1576 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1577 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits8,
1578 aom_highbd_sad4x4x8_bits8, aom_highbd_sad4x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001579
Jingning Hancc5bdf42016-12-19 11:14:30 -08001580 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_8_variance2x2, NULL, NULL,
1581 NULL, NULL, NULL)
1582 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_8_variance4x2, NULL, NULL,
1583 NULL, NULL, NULL)
1584 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_8_variance2x4, NULL, NULL,
1585 NULL, NULL, NULL)
Jingning Hancc5bdf42016-12-19 11:14:30 -08001586
Yaowu Xuc27fc142016-08-22 16:08:15 -07001587#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001588 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1589 aom_highbd_sad128x128_avg_bits8,
1590 aom_highbd_8_variance128x128,
1591 aom_highbd_8_sub_pixel_variance128x128,
1592 aom_highbd_8_sub_pixel_avg_variance128x128,
1593 aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
1594 aom_highbd_sad128x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001595
Yaowu Xuf883b422016-08-30 14:01:10 -07001596 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1597 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1598 aom_highbd_8_sub_pixel_variance128x64,
1599 aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
1600 aom_highbd_sad128x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001601
Yaowu Xuf883b422016-08-30 14:01:10 -07001602 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1603 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1604 aom_highbd_8_sub_pixel_variance64x128,
1605 aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
1606 aom_highbd_sad64x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001607#endif // CONFIG_EXT_PARTITION
1608
Yaowu Xuc27fc142016-08-22 16:08:15 -07001609#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001610 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1611 aom_highbd_8_masked_sub_pixel_variance128x128)
1612 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1613 aom_highbd_8_masked_sub_pixel_variance128x64)
1614 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1615 aom_highbd_8_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001616#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001617 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1618 aom_highbd_8_masked_sub_pixel_variance64x64)
1619 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1620 aom_highbd_8_masked_sub_pixel_variance64x32)
1621 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1622 aom_highbd_8_masked_sub_pixel_variance32x64)
1623 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1624 aom_highbd_8_masked_sub_pixel_variance32x32)
1625 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1626 aom_highbd_8_masked_sub_pixel_variance32x16)
1627 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1628 aom_highbd_8_masked_sub_pixel_variance16x32)
1629 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1630 aom_highbd_8_masked_sub_pixel_variance16x16)
1631 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1632 aom_highbd_8_masked_sub_pixel_variance8x16)
1633 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1634 aom_highbd_8_masked_sub_pixel_variance16x8)
1635 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1636 aom_highbd_8_masked_sub_pixel_variance8x8)
1637 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1638 aom_highbd_8_masked_sub_pixel_variance4x8)
1639 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1640 aom_highbd_8_masked_sub_pixel_variance8x4)
1641 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1642 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001643#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001644#if CONFIG_EXT_PARTITION
1645 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits8,
1646 aom_highbd_8_masked_sub_pixel_variance128x32)
1647
1648 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits8,
1649 aom_highbd_8_masked_sub_pixel_variance32x128)
1650#endif // CONFIG_EXT_PARTITION
1651
Rupert Swarbrick72678572017-08-02 12:05:26 +01001652 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1653 aom_highbd_8_masked_sub_pixel_variance64x16)
1654
1655 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1656 aom_highbd_8_masked_sub_pixel_variance16x64)
1657
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001658 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1659 aom_highbd_8_masked_sub_pixel_variance32x8)
1660
1661 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1662 aom_highbd_8_masked_sub_pixel_variance8x32)
1663
1664 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1665 aom_highbd_8_masked_sub_pixel_variance16x4)
1666
1667 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1668 aom_highbd_8_masked_sub_pixel_variance4x16)
1669#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001670#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001671 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1672 aom_highbd_obmc_variance128x128,
1673 aom_highbd_obmc_sub_pixel_variance128x128)
1674 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1675 aom_highbd_obmc_variance128x64,
1676 aom_highbd_obmc_sub_pixel_variance128x64)
1677 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1678 aom_highbd_obmc_variance64x128,
1679 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001680#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001681 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1682 aom_highbd_obmc_variance64x64,
1683 aom_highbd_obmc_sub_pixel_variance64x64)
1684 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1685 aom_highbd_obmc_variance64x32,
1686 aom_highbd_obmc_sub_pixel_variance64x32)
1687 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1688 aom_highbd_obmc_variance32x64,
1689 aom_highbd_obmc_sub_pixel_variance32x64)
1690 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1691 aom_highbd_obmc_variance32x32,
1692 aom_highbd_obmc_sub_pixel_variance32x32)
1693 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1694 aom_highbd_obmc_variance32x16,
1695 aom_highbd_obmc_sub_pixel_variance32x16)
1696 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1697 aom_highbd_obmc_variance16x32,
1698 aom_highbd_obmc_sub_pixel_variance16x32)
1699 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1700 aom_highbd_obmc_variance16x16,
1701 aom_highbd_obmc_sub_pixel_variance16x16)
1702 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1703 aom_highbd_obmc_variance8x16,
1704 aom_highbd_obmc_sub_pixel_variance8x16)
1705 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1706 aom_highbd_obmc_variance16x8,
1707 aom_highbd_obmc_sub_pixel_variance16x8)
1708 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1709 aom_highbd_obmc_variance8x8,
1710 aom_highbd_obmc_sub_pixel_variance8x8)
1711 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1712 aom_highbd_obmc_variance4x8,
1713 aom_highbd_obmc_sub_pixel_variance4x8)
1714 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1715 aom_highbd_obmc_variance8x4,
1716 aom_highbd_obmc_sub_pixel_variance8x4)
1717 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1718 aom_highbd_obmc_variance4x4,
1719 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001720#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001721#if CONFIG_EXT_PARTITION
1722 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits8,
1723 aom_highbd_obmc_variance128x32,
1724 aom_highbd_obmc_sub_pixel_variance128x32)
1725
1726 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits8,
1727 aom_highbd_obmc_variance32x128,
1728 aom_highbd_obmc_sub_pixel_variance32x128)
1729#endif // CONFIG_EXT_PARTITION
1730
Rupert Swarbrick72678572017-08-02 12:05:26 +01001731 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1732 aom_highbd_obmc_variance64x16,
1733 aom_highbd_obmc_sub_pixel_variance64x16)
1734
1735 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1736 aom_highbd_obmc_variance16x64,
1737 aom_highbd_obmc_sub_pixel_variance16x64)
1738
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001739 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1740 aom_highbd_obmc_variance32x8,
1741 aom_highbd_obmc_sub_pixel_variance32x8)
1742
1743 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1744 aom_highbd_obmc_variance8x32,
1745 aom_highbd_obmc_sub_pixel_variance8x32)
1746
1747 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1748 aom_highbd_obmc_variance16x4,
1749 aom_highbd_obmc_sub_pixel_variance16x4)
1750
1751 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1752 aom_highbd_obmc_variance4x16,
1753 aom_highbd_obmc_sub_pixel_variance4x16)
1754#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001755 break;
1756
Yaowu Xuf883b422016-08-30 14:01:10 -07001757 case AOM_BITS_10:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001758#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001759#if CONFIG_EXT_PARTITION
1760 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits10,
1761 aom_highbd_sad128x32_avg_bits10,
1762 aom_highbd_10_variance128x32,
1763 aom_highbd_10_sub_pixel_variance128x32,
1764 aom_highbd_10_sub_pixel_avg_variance128x32, NULL, NULL,
1765 aom_highbd_sad128x32x4d_bits10)
1766
1767 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits10,
1768 aom_highbd_sad32x128_avg_bits10,
1769 aom_highbd_10_variance32x128,
1770 aom_highbd_10_sub_pixel_variance32x128,
1771 aom_highbd_10_sub_pixel_avg_variance32x128, NULL, NULL,
1772 aom_highbd_sad32x128x4d_bits10)
1773#endif // CONFIG_EXT_PARTITION
1774
Rupert Swarbrick72678572017-08-02 12:05:26 +01001775 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1776 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1777 aom_highbd_10_sub_pixel_variance64x16,
1778 aom_highbd_10_sub_pixel_avg_variance64x16, NULL, NULL,
1779 aom_highbd_sad64x16x4d_bits10)
1780
1781 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1782 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1783 aom_highbd_10_sub_pixel_variance16x64,
1784 aom_highbd_10_sub_pixel_avg_variance16x64, NULL, NULL,
1785 aom_highbd_sad16x64x4d_bits10)
1786
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001787 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1788 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1789 aom_highbd_10_sub_pixel_variance32x8,
1790 aom_highbd_10_sub_pixel_avg_variance32x8, NULL, NULL,
1791 aom_highbd_sad32x8x4d_bits10)
1792
1793 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1794 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1795 aom_highbd_10_sub_pixel_variance8x32,
1796 aom_highbd_10_sub_pixel_avg_variance8x32, NULL, NULL,
1797 aom_highbd_sad8x32x4d_bits10)
1798
1799 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1800 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1801 aom_highbd_10_sub_pixel_variance16x4,
1802 aom_highbd_10_sub_pixel_avg_variance16x4, NULL, NULL,
1803 aom_highbd_sad16x4x4d_bits10)
1804
1805 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1806 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1807 aom_highbd_10_sub_pixel_variance4x16,
1808 aom_highbd_10_sub_pixel_avg_variance4x16, NULL, NULL,
1809 aom_highbd_sad4x16x4d_bits10)
1810#endif
1811
Yaowu Xuf883b422016-08-30 14:01:10 -07001812 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1813 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1814 aom_highbd_10_sub_pixel_variance32x16,
1815 aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1816 aom_highbd_sad32x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001817
Yaowu Xuf883b422016-08-30 14:01:10 -07001818 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1819 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1820 aom_highbd_10_sub_pixel_variance16x32,
1821 aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1822 aom_highbd_sad16x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001823
Yaowu Xuf883b422016-08-30 14:01:10 -07001824 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1825 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1826 aom_highbd_10_sub_pixel_variance64x32,
1827 aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1828 aom_highbd_sad64x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001829
Yaowu Xuf883b422016-08-30 14:01:10 -07001830 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1831 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1832 aom_highbd_10_sub_pixel_variance32x64,
1833 aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1834 aom_highbd_sad32x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001835
Yaowu Xuf883b422016-08-30 14:01:10 -07001836 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1837 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1838 aom_highbd_10_sub_pixel_variance32x32,
1839 aom_highbd_10_sub_pixel_avg_variance32x32,
1840 aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
1841 aom_highbd_sad32x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001842
Yaowu Xuf883b422016-08-30 14:01:10 -07001843 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1844 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1845 aom_highbd_10_sub_pixel_variance64x64,
1846 aom_highbd_10_sub_pixel_avg_variance64x64,
1847 aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
1848 aom_highbd_sad64x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001849
Yaowu Xuf883b422016-08-30 14:01:10 -07001850 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1851 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1852 aom_highbd_10_sub_pixel_variance16x16,
1853 aom_highbd_10_sub_pixel_avg_variance16x16,
1854 aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
1855 aom_highbd_sad16x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001856
Yaowu Xuf883b422016-08-30 14:01:10 -07001857 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1858 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1859 aom_highbd_10_sub_pixel_variance16x8,
1860 aom_highbd_10_sub_pixel_avg_variance16x8,
1861 aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
1862 aom_highbd_sad16x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001863
Yaowu Xuf883b422016-08-30 14:01:10 -07001864 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1865 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1866 aom_highbd_10_sub_pixel_variance8x16,
1867 aom_highbd_10_sub_pixel_avg_variance8x16,
1868 aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
1869 aom_highbd_sad8x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001870
1871 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001872 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1873 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1874 aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
1875 aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001876
Yaowu Xuf883b422016-08-30 14:01:10 -07001877 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
1878 aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
1879 aom_highbd_10_sub_pixel_variance8x4,
1880 aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
1881 aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001882
Yaowu Xuf883b422016-08-30 14:01:10 -07001883 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
1884 aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
1885 aom_highbd_10_sub_pixel_variance4x8,
1886 aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
1887 aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001888
1889 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001890 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1891 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1892 aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
1893 aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001894
Jingning Hancc5bdf42016-12-19 11:14:30 -08001895 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_10_variance2x2, NULL, NULL,
1896 NULL, NULL, NULL)
1897 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_10_variance4x2, NULL, NULL,
1898 NULL, NULL, NULL)
1899 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_10_variance2x4, NULL, NULL,
1900 NULL, NULL, NULL)
Jingning Hancc5bdf42016-12-19 11:14:30 -08001901
Yaowu Xuc27fc142016-08-22 16:08:15 -07001902#if CONFIG_EXT_PARTITION
1903 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001904 BLOCK_128X128, aom_highbd_sad128x128_bits10,
1905 aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
1906 aom_highbd_10_sub_pixel_variance128x128,
1907 aom_highbd_10_sub_pixel_avg_variance128x128,
1908 aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
1909 aom_highbd_sad128x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001910
Yaowu Xuf883b422016-08-30 14:01:10 -07001911 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1912 aom_highbd_sad128x64_avg_bits10,
1913 aom_highbd_10_variance128x64,
1914 aom_highbd_10_sub_pixel_variance128x64,
1915 aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
1916 aom_highbd_sad128x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001917
Yaowu Xuf883b422016-08-30 14:01:10 -07001918 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1919 aom_highbd_sad64x128_avg_bits10,
1920 aom_highbd_10_variance64x128,
1921 aom_highbd_10_sub_pixel_variance64x128,
1922 aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
1923 aom_highbd_sad64x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001924#endif // CONFIG_EXT_PARTITION
1925
Yaowu Xuc27fc142016-08-22 16:08:15 -07001926#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001927 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1928 aom_highbd_10_masked_sub_pixel_variance128x128)
1929 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1930 aom_highbd_10_masked_sub_pixel_variance128x64)
1931 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1932 aom_highbd_10_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001933#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001934 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1935 aom_highbd_10_masked_sub_pixel_variance64x64)
1936 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1937 aom_highbd_10_masked_sub_pixel_variance64x32)
1938 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1939 aom_highbd_10_masked_sub_pixel_variance32x64)
1940 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1941 aom_highbd_10_masked_sub_pixel_variance32x32)
1942 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1943 aom_highbd_10_masked_sub_pixel_variance32x16)
1944 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1945 aom_highbd_10_masked_sub_pixel_variance16x32)
1946 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1947 aom_highbd_10_masked_sub_pixel_variance16x16)
1948 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1949 aom_highbd_10_masked_sub_pixel_variance8x16)
1950 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1951 aom_highbd_10_masked_sub_pixel_variance16x8)
1952 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1953 aom_highbd_10_masked_sub_pixel_variance8x8)
1954 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1955 aom_highbd_10_masked_sub_pixel_variance4x8)
1956 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1957 aom_highbd_10_masked_sub_pixel_variance8x4)
1958 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1959 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001960#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001961#if CONFIG_EXT_PARTITION
1962 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits10,
1963 aom_highbd_10_masked_sub_pixel_variance128x32)
1964
1965 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits10,
1966 aom_highbd_10_masked_sub_pixel_variance32x128)
1967#endif // CONFIG_EXT_PARTITION
1968
Rupert Swarbrick72678572017-08-02 12:05:26 +01001969 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1970 aom_highbd_10_masked_sub_pixel_variance64x16)
1971
1972 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1973 aom_highbd_10_masked_sub_pixel_variance16x64)
1974
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001975 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1976 aom_highbd_10_masked_sub_pixel_variance32x8)
1977
1978 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1979 aom_highbd_10_masked_sub_pixel_variance8x32)
1980
1981 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1982 aom_highbd_10_masked_sub_pixel_variance16x4)
1983
1984 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1985 aom_highbd_10_masked_sub_pixel_variance4x16)
1986#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001987#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001988 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1989 aom_highbd_10_obmc_variance128x128,
1990 aom_highbd_10_obmc_sub_pixel_variance128x128)
1991 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1992 aom_highbd_10_obmc_variance128x64,
1993 aom_highbd_10_obmc_sub_pixel_variance128x64)
1994 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1995 aom_highbd_10_obmc_variance64x128,
1996 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001997#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001998 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1999 aom_highbd_10_obmc_variance64x64,
2000 aom_highbd_10_obmc_sub_pixel_variance64x64)
2001 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2002 aom_highbd_10_obmc_variance64x32,
2003 aom_highbd_10_obmc_sub_pixel_variance64x32)
2004 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2005 aom_highbd_10_obmc_variance32x64,
2006 aom_highbd_10_obmc_sub_pixel_variance32x64)
2007 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2008 aom_highbd_10_obmc_variance32x32,
2009 aom_highbd_10_obmc_sub_pixel_variance32x32)
2010 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2011 aom_highbd_10_obmc_variance32x16,
2012 aom_highbd_10_obmc_sub_pixel_variance32x16)
2013 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2014 aom_highbd_10_obmc_variance16x32,
2015 aom_highbd_10_obmc_sub_pixel_variance16x32)
2016 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2017 aom_highbd_10_obmc_variance16x16,
2018 aom_highbd_10_obmc_sub_pixel_variance16x16)
2019 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2020 aom_highbd_10_obmc_variance8x16,
2021 aom_highbd_10_obmc_sub_pixel_variance8x16)
2022 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2023 aom_highbd_10_obmc_variance16x8,
2024 aom_highbd_10_obmc_sub_pixel_variance16x8)
2025 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2026 aom_highbd_10_obmc_variance8x8,
2027 aom_highbd_10_obmc_sub_pixel_variance8x8)
2028 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2029 aom_highbd_10_obmc_variance4x8,
2030 aom_highbd_10_obmc_sub_pixel_variance4x8)
2031 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2032 aom_highbd_10_obmc_variance8x4,
2033 aom_highbd_10_obmc_sub_pixel_variance8x4)
2034 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2035 aom_highbd_10_obmc_variance4x4,
2036 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002037#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002038#if CONFIG_EXT_PARTITION
2039 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits10,
2040 aom_highbd_10_obmc_variance128x32,
2041 aom_highbd_10_obmc_sub_pixel_variance128x32)
2042
2043 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits10,
2044 aom_highbd_10_obmc_variance32x128,
2045 aom_highbd_10_obmc_sub_pixel_variance32x128)
2046#endif // CONFIG_EXT_PARTITION
2047
Rupert Swarbrick72678572017-08-02 12:05:26 +01002048 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2049 aom_highbd_10_obmc_variance64x16,
2050 aom_highbd_10_obmc_sub_pixel_variance64x16)
2051
2052 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2053 aom_highbd_10_obmc_variance16x64,
2054 aom_highbd_10_obmc_sub_pixel_variance16x64)
2055
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002056 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2057 aom_highbd_10_obmc_variance32x8,
2058 aom_highbd_10_obmc_sub_pixel_variance32x8)
2059
2060 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2061 aom_highbd_10_obmc_variance8x32,
2062 aom_highbd_10_obmc_sub_pixel_variance8x32)
2063
2064 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2065 aom_highbd_10_obmc_variance16x4,
2066 aom_highbd_10_obmc_sub_pixel_variance16x4)
2067
2068 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2069 aom_highbd_10_obmc_variance4x16,
2070 aom_highbd_10_obmc_sub_pixel_variance4x16)
2071#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002072 break;
2073
Yaowu Xuf883b422016-08-30 14:01:10 -07002074 case AOM_BITS_12:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002075#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002076#if CONFIG_EXT_PARTITION
2077 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits12,
2078 aom_highbd_sad128x32_avg_bits12,
2079 aom_highbd_12_variance128x32,
2080 aom_highbd_12_sub_pixel_variance128x32,
2081 aom_highbd_12_sub_pixel_avg_variance128x32, NULL, NULL,
2082 aom_highbd_sad128x32x4d_bits12)
2083
2084 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits12,
2085 aom_highbd_sad32x128_avg_bits12,
2086 aom_highbd_12_variance32x128,
2087 aom_highbd_12_sub_pixel_variance32x128,
2088 aom_highbd_12_sub_pixel_avg_variance32x128, NULL, NULL,
2089 aom_highbd_sad32x128x4d_bits12)
2090#endif // CONFIG_EXT_PARTITION
2091
Rupert Swarbrick72678572017-08-02 12:05:26 +01002092 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2093 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2094 aom_highbd_12_sub_pixel_variance64x16,
2095 aom_highbd_12_sub_pixel_avg_variance64x16, NULL, NULL,
2096 aom_highbd_sad64x16x4d_bits12)
2097
2098 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2099 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2100 aom_highbd_12_sub_pixel_variance16x64,
2101 aom_highbd_12_sub_pixel_avg_variance16x64, NULL, NULL,
2102 aom_highbd_sad16x64x4d_bits12)
2103
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002104 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2105 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2106 aom_highbd_12_sub_pixel_variance32x8,
2107 aom_highbd_12_sub_pixel_avg_variance32x8, NULL, NULL,
2108 aom_highbd_sad32x8x4d_bits12)
2109
2110 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2111 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2112 aom_highbd_12_sub_pixel_variance8x32,
2113 aom_highbd_12_sub_pixel_avg_variance8x32, NULL, NULL,
2114 aom_highbd_sad8x32x4d_bits12)
2115
2116 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2117 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2118 aom_highbd_12_sub_pixel_variance16x4,
2119 aom_highbd_12_sub_pixel_avg_variance16x4, NULL, NULL,
2120 aom_highbd_sad16x4x4d_bits12)
2121
2122 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2123 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2124 aom_highbd_12_sub_pixel_variance4x16,
2125 aom_highbd_12_sub_pixel_avg_variance4x16, NULL, NULL,
2126 aom_highbd_sad4x16x4d_bits12)
2127#endif
2128
Yaowu Xuf883b422016-08-30 14:01:10 -07002129 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2130 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2131 aom_highbd_12_sub_pixel_variance32x16,
2132 aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
2133 aom_highbd_sad32x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002134
Yaowu Xuf883b422016-08-30 14:01:10 -07002135 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2136 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2137 aom_highbd_12_sub_pixel_variance16x32,
2138 aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
2139 aom_highbd_sad16x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002140
Yaowu Xuf883b422016-08-30 14:01:10 -07002141 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2142 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2143 aom_highbd_12_sub_pixel_variance64x32,
2144 aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
2145 aom_highbd_sad64x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002146
Yaowu Xuf883b422016-08-30 14:01:10 -07002147 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2148 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2149 aom_highbd_12_sub_pixel_variance32x64,
2150 aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
2151 aom_highbd_sad32x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002152
Yaowu Xuf883b422016-08-30 14:01:10 -07002153 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2154 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2155 aom_highbd_12_sub_pixel_variance32x32,
2156 aom_highbd_12_sub_pixel_avg_variance32x32,
2157 aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
2158 aom_highbd_sad32x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002159
Yaowu Xuf883b422016-08-30 14:01:10 -07002160 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2161 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2162 aom_highbd_12_sub_pixel_variance64x64,
2163 aom_highbd_12_sub_pixel_avg_variance64x64,
2164 aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
2165 aom_highbd_sad64x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002166
Yaowu Xuf883b422016-08-30 14:01:10 -07002167 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2168 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2169 aom_highbd_12_sub_pixel_variance16x16,
2170 aom_highbd_12_sub_pixel_avg_variance16x16,
2171 aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
2172 aom_highbd_sad16x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002173
Yaowu Xuf883b422016-08-30 14:01:10 -07002174 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2175 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2176 aom_highbd_12_sub_pixel_variance16x8,
2177 aom_highbd_12_sub_pixel_avg_variance16x8,
2178 aom_highbd_sad16x8x3_bits12, aom_highbd_sad16x8x8_bits12,
2179 aom_highbd_sad16x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002180
Yaowu Xuf883b422016-08-30 14:01:10 -07002181 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2182 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2183 aom_highbd_12_sub_pixel_variance8x16,
2184 aom_highbd_12_sub_pixel_avg_variance8x16,
2185 aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
2186 aom_highbd_sad8x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002187
2188 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002189 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2190 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
2191 aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
2192 aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002193
Yaowu Xuf883b422016-08-30 14:01:10 -07002194 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
2195 aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
2196 aom_highbd_12_sub_pixel_variance8x4,
2197 aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
2198 aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002199
Yaowu Xuf883b422016-08-30 14:01:10 -07002200 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
2201 aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
2202 aom_highbd_12_sub_pixel_variance4x8,
2203 aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
2204 aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002205
2206 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002207 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2208 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
2209 aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
2210 aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002211
Jingning Hancc5bdf42016-12-19 11:14:30 -08002212 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_12_variance2x2, NULL, NULL,
2213 NULL, NULL, NULL)
2214 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_12_variance4x2, NULL, NULL,
2215 NULL, NULL, NULL)
2216 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_12_variance2x4, NULL, NULL,
2217 NULL, NULL, NULL)
Jingning Hancc5bdf42016-12-19 11:14:30 -08002218
Yaowu Xuc27fc142016-08-22 16:08:15 -07002219#if CONFIG_EXT_PARTITION
2220 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002221 BLOCK_128X128, aom_highbd_sad128x128_bits12,
2222 aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
2223 aom_highbd_12_sub_pixel_variance128x128,
2224 aom_highbd_12_sub_pixel_avg_variance128x128,
2225 aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
2226 aom_highbd_sad128x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002227
Yaowu Xuf883b422016-08-30 14:01:10 -07002228 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2229 aom_highbd_sad128x64_avg_bits12,
2230 aom_highbd_12_variance128x64,
2231 aom_highbd_12_sub_pixel_variance128x64,
2232 aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
2233 aom_highbd_sad128x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002234
Yaowu Xuf883b422016-08-30 14:01:10 -07002235 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2236 aom_highbd_sad64x128_avg_bits12,
2237 aom_highbd_12_variance64x128,
2238 aom_highbd_12_sub_pixel_variance64x128,
2239 aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
2240 aom_highbd_sad64x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002241#endif // CONFIG_EXT_PARTITION
2242
Yaowu Xuc27fc142016-08-22 16:08:15 -07002243#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002244 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2245 aom_highbd_12_masked_sub_pixel_variance128x128)
2246 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2247 aom_highbd_12_masked_sub_pixel_variance128x64)
2248 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2249 aom_highbd_12_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002250#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002251 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2252 aom_highbd_12_masked_sub_pixel_variance64x64)
2253 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2254 aom_highbd_12_masked_sub_pixel_variance64x32)
2255 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2256 aom_highbd_12_masked_sub_pixel_variance32x64)
2257 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2258 aom_highbd_12_masked_sub_pixel_variance32x32)
2259 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2260 aom_highbd_12_masked_sub_pixel_variance32x16)
2261 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2262 aom_highbd_12_masked_sub_pixel_variance16x32)
2263 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2264 aom_highbd_12_masked_sub_pixel_variance16x16)
2265 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2266 aom_highbd_12_masked_sub_pixel_variance8x16)
2267 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2268 aom_highbd_12_masked_sub_pixel_variance16x8)
2269 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2270 aom_highbd_12_masked_sub_pixel_variance8x8)
2271 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2272 aom_highbd_12_masked_sub_pixel_variance4x8)
2273 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2274 aom_highbd_12_masked_sub_pixel_variance8x4)
2275 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2276 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002277#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002278#if CONFIG_EXT_PARTITION
2279 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits12,
2280 aom_highbd_12_masked_sub_pixel_variance128x32)
2281
2282 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits12,
2283 aom_highbd_12_masked_sub_pixel_variance32x128)
2284#endif // CONFIG_EXT_PARTITION
2285
Rupert Swarbrick72678572017-08-02 12:05:26 +01002286 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2287 aom_highbd_12_masked_sub_pixel_variance64x16)
2288
2289 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2290 aom_highbd_12_masked_sub_pixel_variance16x64)
2291
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002292 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2293 aom_highbd_12_masked_sub_pixel_variance32x8)
2294
2295 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2296 aom_highbd_12_masked_sub_pixel_variance8x32)
2297
2298 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2299 aom_highbd_12_masked_sub_pixel_variance16x4)
2300
2301 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2302 aom_highbd_12_masked_sub_pixel_variance4x16)
2303#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002304
Yaowu Xuc27fc142016-08-22 16:08:15 -07002305#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002306 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2307 aom_highbd_12_obmc_variance128x128,
2308 aom_highbd_12_obmc_sub_pixel_variance128x128)
2309 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2310 aom_highbd_12_obmc_variance128x64,
2311 aom_highbd_12_obmc_sub_pixel_variance128x64)
2312 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2313 aom_highbd_12_obmc_variance64x128,
2314 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002315#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002316 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2317 aom_highbd_12_obmc_variance64x64,
2318 aom_highbd_12_obmc_sub_pixel_variance64x64)
2319 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2320 aom_highbd_12_obmc_variance64x32,
2321 aom_highbd_12_obmc_sub_pixel_variance64x32)
2322 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2323 aom_highbd_12_obmc_variance32x64,
2324 aom_highbd_12_obmc_sub_pixel_variance32x64)
2325 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2326 aom_highbd_12_obmc_variance32x32,
2327 aom_highbd_12_obmc_sub_pixel_variance32x32)
2328 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2329 aom_highbd_12_obmc_variance32x16,
2330 aom_highbd_12_obmc_sub_pixel_variance32x16)
2331 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2332 aom_highbd_12_obmc_variance16x32,
2333 aom_highbd_12_obmc_sub_pixel_variance16x32)
2334 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2335 aom_highbd_12_obmc_variance16x16,
2336 aom_highbd_12_obmc_sub_pixel_variance16x16)
2337 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2338 aom_highbd_12_obmc_variance8x16,
2339 aom_highbd_12_obmc_sub_pixel_variance8x16)
2340 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2341 aom_highbd_12_obmc_variance16x8,
2342 aom_highbd_12_obmc_sub_pixel_variance16x8)
2343 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2344 aom_highbd_12_obmc_variance8x8,
2345 aom_highbd_12_obmc_sub_pixel_variance8x8)
2346 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2347 aom_highbd_12_obmc_variance4x8,
2348 aom_highbd_12_obmc_sub_pixel_variance4x8)
2349 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2350 aom_highbd_12_obmc_variance8x4,
2351 aom_highbd_12_obmc_sub_pixel_variance8x4)
2352 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2353 aom_highbd_12_obmc_variance4x4,
2354 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002355#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002356#if CONFIG_EXT_PARTITION
2357 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits12,
2358 aom_highbd_12_obmc_variance128x32,
2359 aom_highbd_12_obmc_sub_pixel_variance128x32)
2360
2361 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits12,
2362 aom_highbd_12_obmc_variance32x128,
2363 aom_highbd_12_obmc_sub_pixel_variance32x128)
2364#endif // CONFIG_EXT_PARTITION
2365
Rupert Swarbrick72678572017-08-02 12:05:26 +01002366 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2367 aom_highbd_12_obmc_variance64x16,
2368 aom_highbd_12_obmc_sub_pixel_variance64x16)
2369
2370 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2371 aom_highbd_12_obmc_variance16x64,
2372 aom_highbd_12_obmc_sub_pixel_variance16x64)
2373
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002374 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2375 aom_highbd_12_obmc_variance32x8,
2376 aom_highbd_12_obmc_sub_pixel_variance32x8)
2377
2378 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2379 aom_highbd_12_obmc_variance8x32,
2380 aom_highbd_12_obmc_sub_pixel_variance8x32)
2381
2382 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2383 aom_highbd_12_obmc_variance16x4,
2384 aom_highbd_12_obmc_sub_pixel_variance16x4)
2385
2386 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2387 aom_highbd_12_obmc_variance4x16,
2388 aom_highbd_12_obmc_sub_pixel_variance4x16)
2389#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390 break;
2391
2392 default:
2393 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07002394 "cm->bit_depth should be AOM_BITS_8, "
2395 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002396 }
2397 }
2398}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002399#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400
Yaowu Xuf883b422016-08-30 14:01:10 -07002401static void realloc_segmentation_maps(AV1_COMP *cpi) {
2402 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002403
2404 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002405 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002406 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002407 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002408
2409 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002410 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002411 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002412 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002413
2414 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002415 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002416 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002417 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002418}
2419
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002420void set_compound_tools(AV1_COMMON *cm) {
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002421 cm->allow_interintra_compound = 1;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002422 cm->allow_masked_compound = 1;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002423}
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002424
Yaowu Xuf883b422016-08-30 14:01:10 -07002425void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2426 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002427 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002428 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002429
2430 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
2431 cm->bit_depth = oxcf->bit_depth;
2432 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07002433#if CONFIG_COLORSPACE_HEADERS
2434 cm->transfer_function = oxcf->transfer_function;
2435 cm->chroma_sample_position = oxcf->chroma_sample_position;
2436#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002437 cm->color_range = oxcf->color_range;
2438
2439 if (cm->profile <= PROFILE_1)
Yaowu Xuf883b422016-08-30 14:01:10 -07002440 assert(cm->bit_depth == AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002442 assert(cm->bit_depth > AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443
2444 cpi->oxcf = *oxcf;
hui sud9a812b2017-07-06 14:34:37 -07002445 x->e_mbd.bd = (int)cm->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002446 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002447
Yaowu Xuf883b422016-08-30 14:01:10 -07002448 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2450 } else {
2451 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2452 }
2453
2454 cpi->refresh_last_frame = 1;
2455 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002457 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458
2459 cm->refresh_frame_context =
2460 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
2461 ? REFRESH_FRAME_CONTEXT_FORWARD
2462 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002463#if CONFIG_EXT_TILE
2464 if (oxcf->large_scale_tile)
2465 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
2466#endif // CONFIG_EXT_TILE
2467
Thomas Daedea6a854b2017-06-22 17:49:11 -07002468#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07002469 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07002470#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002471
Alex Converse74ad0912017-07-18 10:22:58 -07002472 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002473 CHECK_MEM_ERROR(cm, x->palette_buffer,
2474 aom_memalign(16, sizeof(*x->palette_buffer)));
2475 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002476 set_compound_tools(cm);
Yaowu Xuf883b422016-08-30 14:01:10 -07002477 av1_reset_segment_features(cm);
RogerZhou3b635242017-09-19 10:06:46 -07002478#if CONFIG_AMVR
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002479 set_high_precision_mv(cpi, 1, 0);
RogerZhou3b635242017-09-19 10:06:46 -07002480#else
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002481 set_high_precision_mv(cpi, 1);
RogerZhou3b635242017-09-19 10:06:46 -07002482#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002483
Yaowu Xuc27fc142016-08-22 16:08:15 -07002484 set_rc_buffer_sizes(rc, &cpi->oxcf);
2485
2486 // Under a configuration change, where maximum_buffer_size may change,
2487 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002488 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2489 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002490
2491 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002492 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002493
2494 // Set absolute upper and lower quality limits
2495 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2496 rc->best_quality = cpi->oxcf.best_allowed_q;
2497
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002498#if CONFIG_EXT_TILE
2499 if (!oxcf->large_scale_tile)
2500#endif // CONFIG_EXT_TILE
2501 cm->interp_filter = cpi->sf.default_interp_filter;
2502#if CONFIG_EXT_TILE
2503 else
2504 cm->interp_filter = EIGHTTAP_REGULAR;
2505#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -07002506
2507 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2508 cm->render_width = cpi->oxcf.render_width;
2509 cm->render_height = cpi->oxcf.render_height;
2510 } else {
2511 cm->render_width = cpi->oxcf.width;
2512 cm->render_height = cpi->oxcf.height;
2513 }
2514 cm->width = cpi->oxcf.width;
2515 cm->height = cpi->oxcf.height;
2516
2517 if (cpi->initial_width) {
2518 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002519 av1_free_context_buffers(cm);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07002520 av1_free_pc_tree(&cpi->td);
Cheng Chen46f30c72017-09-07 11:13:33 -07002521 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002522 realloc_segmentation_maps(cpi);
2523 cpi->initial_width = cpi->initial_height = 0;
2524 }
2525 }
2526 update_frame_size(cpi);
2527
2528 cpi->alt_ref_source = NULL;
2529 rc->is_src_frame_alt_ref = 0;
2530
Yaowu Xuc27fc142016-08-22 16:08:15 -07002531 rc->is_bwd_ref_frame = 0;
2532 rc->is_last_bipred_frame = 0;
2533 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002534
2535#if 0
2536 // Experimental RD Code
2537 cpi->frame_distortion = 0;
2538 cpi->last_frame_distortion = 0;
2539#endif
2540
2541 set_tile_info(cpi);
2542
2543 cpi->ext_refresh_frame_flags_pending = 0;
2544 cpi->ext_refresh_frame_context_pending = 0;
2545
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002546#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002547 highbd_set_var_fns(cpi);
2548#endif
Alex Converseeb780e72016-12-13 12:46:41 -08002549#if CONFIG_ANS && ANS_MAX_SYMBOLS
2550 cpi->common.ans_window_size_log2 = cpi->oxcf.ans_window_size_log2;
Alex Converseeb780e72016-12-13 12:46:41 -08002551#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
RogerZhou3b635242017-09-19 10:06:46 -07002552#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07002553 cm->seq_force_integer_mv = 2;
RogerZhou3b635242017-09-19 10:06:46 -07002554#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002555}
2556
Yaowu Xuf883b422016-08-30 14:01:10 -07002557AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2558 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002559 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002560 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2561 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002562
2563 if (!cm) return NULL;
2564
Yaowu Xuf883b422016-08-30 14:01:10 -07002565 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002566
2567 if (setjmp(cm->error.jmp)) {
2568 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002569 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570 return 0;
2571 }
2572
2573 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002574 cm->alloc_mi = enc_alloc_mi;
2575 cm->free_mi = enc_free_mi;
2576 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002577
Angie Chianga5d96c42016-10-21 16:16:56 -07002578 CHECK_MEM_ERROR(cm, cm->fc,
2579 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2580 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2581 (FRAME_CONTEXT *)aom_memalign(
2582 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2583 memset(cm->fc, 0, sizeof(*cm->fc));
2584 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002585
2586 cpi->resize_state = 0;
2587 cpi->resize_avg_qp = 0;
2588 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002589
Yaowu Xuc27fc142016-08-22 16:08:15 -07002590 cpi->common.buffer_pool = pool;
2591
2592 init_config(cpi, oxcf);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002593#if CONFIG_XIPHRC
2594 cpi->od_rc.framerate = cpi->framerate;
2595 cpi->od_rc.frame_width = cm->render_width;
2596 cpi->od_rc.frame_height = cm->render_height;
2597 cpi->od_rc.keyframe_rate = oxcf->key_freq;
2598 cpi->od_rc.goldenframe_rate = FIXED_GF_INTERVAL;
2599 cpi->od_rc.altref_rate = 25;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002600 cpi->od_rc.firstpass_quant = 1;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002601 cpi->od_rc.bit_depth = cm->bit_depth;
2602 cpi->od_rc.minq = oxcf->best_allowed_q;
2603 cpi->od_rc.maxq = oxcf->worst_allowed_q;
2604 if (cpi->oxcf.rc_mode == AOM_CQ) cpi->od_rc.minq = cpi->od_rc.quality;
2605 cpi->od_rc.quality = cpi->oxcf.rc_mode == AOM_Q ? oxcf->cq_level : -1;
2606 cpi->od_rc.periodic_boosts = oxcf->frame_periodic_boost;
2607 od_enc_rc_init(&cpi->od_rc,
2608 cpi->oxcf.rc_mode == AOM_Q ? -1 : oxcf->target_bandwidth,
2609 oxcf->maximum_buffer_size_ms);
2610#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002611 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002612#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002613
2614 cm->current_video_frame = 0;
2615 cpi->partition_search_skippable_frame = 0;
2616 cpi->tile_data = NULL;
2617 cpi->last_show_frame_buf_idx = INVALID_IDX;
2618
2619 realloc_segmentation_maps(cpi);
2620
James Zern01a9d702017-08-25 19:09:33 +00002621 for (i = 0; i < NMV_CONTEXTS; ++i) {
2622 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2623 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
2624 }
2625
Yaowu Xuc27fc142016-08-22 16:08:15 -07002626 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2627 i++) {
2628 CHECK_MEM_ERROR(
2629 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002630 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631 }
2632
2633#if CONFIG_FP_MB_STATS
2634 cpi->use_fp_mb_stats = 0;
2635 if (cpi->use_fp_mb_stats) {
2636 // a place holder used to store the first pass mb stats in the first pass
2637 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002638 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639 } else {
2640 cpi->twopass.frame_mb_stats_buf = NULL;
2641 }
2642#endif
2643
2644 cpi->refresh_alt_ref_frame = 0;
2645 cpi->multi_arf_last_grp_enabled = 0;
2646
2647 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2648#if CONFIG_INTERNAL_STATS
2649 cpi->b_calculate_blockiness = 1;
2650 cpi->b_calculate_consistency = 1;
2651 cpi->total_inconsistency = 0;
2652 cpi->psnr.worst = 100.0;
2653 cpi->worst_ssim = 100.0;
2654
2655 cpi->count = 0;
2656 cpi->bytes = 0;
2657
2658 if (cpi->b_calculate_psnr) {
2659 cpi->total_sq_error = 0;
2660 cpi->total_samples = 0;
2661 cpi->tot_recode_hits = 0;
2662 cpi->summed_quality = 0;
2663 cpi->summed_weights = 0;
2664 }
2665
2666 cpi->fastssim.worst = 100.0;
2667 cpi->psnrhvs.worst = 100.0;
2668
2669 if (cpi->b_calculate_blockiness) {
2670 cpi->total_blockiness = 0;
2671 cpi->worst_blockiness = 0.0;
2672 }
2673
2674 if (cpi->b_calculate_consistency) {
2675 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002676 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677 cpi->common.mi_rows * cpi->common.mi_cols));
2678 cpi->worst_consistency = 100.0;
2679 }
2680#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002681#if CONFIG_ENTROPY_STATS
2682 av1_zero(aggregate_fc);
Zoe Liua56f9162017-06-21 22:49:57 -07002683 av1_zero_array(aggregate_fc_per_type, FRAME_CONTEXTS);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002684#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002685
2686 cpi->first_time_stamp_ever = INT64_MAX;
2687
James Zern01a9d702017-08-25 19:09:33 +00002688 for (i = 0; i < NMV_CONTEXTS; ++i) {
2689 cpi->td.mb.nmvcost[i][0] = &cpi->nmv_costs[i][0][MV_MAX];
2690 cpi->td.mb.nmvcost[i][1] = &cpi->nmv_costs[i][1][MV_MAX];
2691 cpi->td.mb.nmvcost_hp[i][0] = &cpi->nmv_costs_hp[i][0][MV_MAX];
2692 cpi->td.mb.nmvcost_hp[i][1] = &cpi->nmv_costs_hp[i][1][MV_MAX];
2693 }
2694
Yaowu Xuc27fc142016-08-22 16:08:15 -07002695#ifdef OUTPUT_YUV_SKINMAP
2696 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2697#endif
2698#ifdef OUTPUT_YUV_REC
2699 yuv_rec_file = fopen("rec.yuv", "wb");
2700#endif
2701
2702#if 0
2703 framepsnr = fopen("framepsnr.stt", "a");
2704 kf_list = fopen("kf_list.stt", "w");
2705#endif
2706
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002707#if CONFIG_XIPHRC
2708 if (oxcf->pass == 2) {
2709 cpi->od_rc.twopass_allframes_buf = oxcf->two_pass_stats_in.buf;
2710 cpi->od_rc.twopass_allframes_buf_size = oxcf->two_pass_stats_in.sz;
2711 }
2712#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002713 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002714 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002715 } else if (oxcf->pass == 2) {
2716 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2717 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2718
2719#if CONFIG_FP_MB_STATS
2720 if (cpi->use_fp_mb_stats) {
2721 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2722 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2723
2724 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2725 oxcf->firstpass_mb_stats_in.buf;
2726 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2727 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2728 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2729 }
2730#endif
2731
2732 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2733 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2734 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2735
Yaowu Xuf883b422016-08-30 14:01:10 -07002736 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002737 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002738#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002739
Jingning Hand064cf02017-06-01 10:00:39 -07002740#if CONFIG_HIGHBITDEPTH
2741 int buf_scaler = 2;
2742#else
2743 int buf_scaler = 1;
2744#endif
2745 CHECK_MEM_ERROR(
2746 cm, cpi->td.mb.above_pred_buf,
clang-format4eafefe2017-09-04 12:51:20 -07002747 (uint8_t *)aom_memalign(16,
2748 buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2749 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002750 CHECK_MEM_ERROR(
2751 cm, cpi->td.mb.left_pred_buf,
clang-format4eafefe2017-09-04 12:51:20 -07002752 (uint8_t *)aom_memalign(16,
2753 buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2754 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002755
2756 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2757 (int32_t *)aom_memalign(
2758 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2759
2760 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2761 (int32_t *)aom_memalign(
2762 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2763
Yaowu Xuf883b422016-08-30 14:01:10 -07002764 av1_set_speed_features_framesize_independent(cpi);
2765 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002766
Cheng Chenf78632e2017-10-20 15:30:51 -07002767#if CONFIG_JNT_COMP
2768#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF, JSDAF, JSVAF) \
2769 cpi->fn_ptr[BT].sdf = SDF; \
2770 cpi->fn_ptr[BT].sdaf = SDAF; \
2771 cpi->fn_ptr[BT].vf = VF; \
2772 cpi->fn_ptr[BT].svf = SVF; \
2773 cpi->fn_ptr[BT].svaf = SVAF; \
2774 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2775 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2776 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2777 cpi->fn_ptr[BT].jsdaf = JSDAF; \
2778 cpi->fn_ptr[BT].jsvaf = JSVAF;
2779#else // CONFIG_JNT_COMP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002780#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
2781 cpi->fn_ptr[BT].sdf = SDF; \
2782 cpi->fn_ptr[BT].sdaf = SDAF; \
2783 cpi->fn_ptr[BT].vf = VF; \
2784 cpi->fn_ptr[BT].svf = SVF; \
2785 cpi->fn_ptr[BT].svaf = SVAF; \
2786 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2787 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2788 cpi->fn_ptr[BT].sdx4df = SDX4DF;
Cheng Chenf78632e2017-10-20 15:30:51 -07002789#endif // CONFIG_JNT_COMP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002790
Cheng Chenf78632e2017-10-20 15:30:51 -07002791#if CONFIG_JNT_COMP
2792#if CONFIG_EXT_PARTITION_TYPES
2793 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2794 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002795 aom_sad4x16x4d, aom_jnt_sad4x16_avg, aom_jnt_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002796
2797 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2798 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002799 aom_sad16x4x4d, aom_jnt_sad16x4_avg, aom_jnt_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002800
2801 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2802 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002803 aom_sad8x32x4d, aom_jnt_sad8x32_avg, aom_jnt_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002804
2805 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2806 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002807 aom_sad32x8x4d, aom_jnt_sad32x8_avg, aom_jnt_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002808
2809 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2810 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002811 aom_sad16x64x4d, aom_jnt_sad16x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002812 aom_jnt_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002813
2814 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2815 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002816 aom_sad64x16x4d, aom_jnt_sad64x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002817 aom_jnt_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002818
2819#if CONFIG_EXT_PARTITION
2820 BFP(BLOCK_32X128, aom_sad32x128, aom_sad32x128_avg, aom_variance32x128,
2821 aom_sub_pixel_variance32x128, aom_sub_pixel_avg_variance32x128, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002822 NULL, aom_sad32x128x4d, aom_jnt_sad32x128_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002823 aom_jnt_sub_pixel_avg_variance32x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002824
2825 BFP(BLOCK_128X32, aom_sad128x32, aom_sad128x32_avg, aom_variance128x32,
2826 aom_sub_pixel_variance128x32, aom_sub_pixel_avg_variance128x32, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002827 NULL, aom_sad128x32x4d, aom_jnt_sad128x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002828 aom_jnt_sub_pixel_avg_variance128x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002829#endif // CONFIG_EXT_PARTITION
2830#endif // CONFIG_EXT_PARTITION_TYPES
2831
2832#if CONFIG_EXT_PARTITION
2833 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2834 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2835 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d,
Cheng Chend0179a62017-11-16 17:02:53 -08002836 aom_jnt_sad128x128_avg, aom_jnt_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002837
2838 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2839 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002840 NULL, aom_sad128x64x4d, aom_jnt_sad128x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002841 aom_jnt_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002842
2843 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2844 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002845 NULL, aom_sad64x128x4d, aom_jnt_sad64x128_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002846 aom_jnt_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002847#endif // CONFIG_EXT_PARTITION
2848
2849 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2850 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002851 aom_sad32x16x4d, aom_jnt_sad32x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002852 aom_jnt_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002853
2854 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2855 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002856 aom_sad16x32x4d, aom_jnt_sad16x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002857 aom_jnt_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002858
2859 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2860 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002861 aom_sad64x32x4d, aom_jnt_sad64x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002862 aom_jnt_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002863
2864 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2865 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002866 aom_sad32x64x4d, aom_jnt_sad32x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002867 aom_jnt_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002868
2869 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2870 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002871 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d, aom_jnt_sad32x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002872 aom_jnt_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002873
2874 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2875 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002876 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d, aom_jnt_sad64x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002877 aom_jnt_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002878
2879 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2880 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002881 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d, aom_jnt_sad16x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002882 aom_jnt_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002883
2884 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2885 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
Cheng Chend0179a62017-11-16 17:02:53 -08002886 aom_sad16x8x8, aom_sad16x8x4d, aom_jnt_sad16x8_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002887 aom_jnt_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002888
2889 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2890 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
Cheng Chend0179a62017-11-16 17:02:53 -08002891 aom_sad8x16x8, aom_sad8x16x4d, aom_jnt_sad8x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002892 aom_jnt_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002893
2894 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2895 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
Cheng Chend0179a62017-11-16 17:02:53 -08002896 aom_sad8x8x8, aom_sad8x8x4d, aom_jnt_sad8x8_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002897 aom_jnt_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002898
2899 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2900 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002901 aom_sad8x4x8, aom_sad8x4x4d, aom_jnt_sad8x4_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002902 aom_jnt_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002903
2904 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2905 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002906 aom_sad4x8x8, aom_sad4x8x4d, aom_jnt_sad4x8_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002907 aom_jnt_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002908
2909 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2910 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
Cheng Chend0179a62017-11-16 17:02:53 -08002911 aom_sad4x4x8, aom_sad4x4x4d, aom_jnt_sad4x4_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002912 aom_jnt_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002913
2914 BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL,
2915 NULL, NULL)
2916 BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL,
2917 NULL, NULL)
2918 BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL,
2919 NULL, NULL)
2920#else // CONFIG_JNT_COMP
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002921#if CONFIG_EXT_PARTITION_TYPES
2922 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2923 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, NULL, NULL,
2924 aom_sad4x16x4d)
2925
2926 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2927 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, NULL, NULL,
2928 aom_sad16x4x4d)
2929
2930 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2931 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, NULL, NULL,
2932 aom_sad8x32x4d)
2933
2934 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2935 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, NULL, NULL,
2936 aom_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002937
2938 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2939 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, NULL, NULL,
2940 aom_sad16x64x4d)
2941
2942 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2943 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, NULL, NULL,
2944 aom_sad64x16x4d)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002945
2946#if CONFIG_EXT_PARTITION
2947 BFP(BLOCK_32X128, aom_sad32x128, aom_sad32x128_avg, aom_variance32x128,
2948 aom_sub_pixel_variance32x128, aom_sub_pixel_avg_variance32x128, NULL,
2949 NULL, aom_sad32x128x4d)
2950
2951 BFP(BLOCK_128X32, aom_sad128x32, aom_sad128x32_avg, aom_variance128x32,
2952 aom_sub_pixel_variance128x32, aom_sub_pixel_avg_variance128x32, NULL,
2953 NULL, aom_sad128x32x4d)
2954#endif // CONFIG_EXT_PARTITION
2955#endif // CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002956
Yaowu Xuc27fc142016-08-22 16:08:15 -07002957#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002958 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2959 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2960 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002961
Yaowu Xuf883b422016-08-30 14:01:10 -07002962 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2963 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
2964 NULL, aom_sad128x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002965
Yaowu Xuf883b422016-08-30 14:01:10 -07002966 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2967 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
2968 NULL, aom_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969#endif // CONFIG_EXT_PARTITION
2970
Yaowu Xuf883b422016-08-30 14:01:10 -07002971 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2972 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2973 aom_sad32x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002974
Yaowu Xuf883b422016-08-30 14:01:10 -07002975 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2976 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2977 aom_sad16x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002978
Yaowu Xuf883b422016-08-30 14:01:10 -07002979 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2980 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2981 aom_sad64x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002982
Yaowu Xuf883b422016-08-30 14:01:10 -07002983 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2984 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2985 aom_sad32x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002986
Yaowu Xuf883b422016-08-30 14:01:10 -07002987 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2988 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2989 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002990
Yaowu Xuf883b422016-08-30 14:01:10 -07002991 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2992 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2993 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002994
Yaowu Xuf883b422016-08-30 14:01:10 -07002995 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2996 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2997 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002998
Yaowu Xuf883b422016-08-30 14:01:10 -07002999 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
3000 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
3001 aom_sad16x8x8, aom_sad16x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003002
Yaowu Xuf883b422016-08-30 14:01:10 -07003003 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
3004 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
3005 aom_sad8x16x8, aom_sad8x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003006
Yaowu Xuf883b422016-08-30 14:01:10 -07003007 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
3008 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
3009 aom_sad8x8x8, aom_sad8x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003010
Yaowu Xuf883b422016-08-30 14:01:10 -07003011 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
3012 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
3013 aom_sad8x4x8, aom_sad8x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003014
Yaowu Xuf883b422016-08-30 14:01:10 -07003015 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
3016 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
3017 aom_sad4x8x8, aom_sad4x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003018
Yaowu Xuf883b422016-08-30 14:01:10 -07003019 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
3020 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
3021 aom_sad4x4x8, aom_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003022
Jingning Han9e7c49f2016-12-06 11:20:10 -08003023 BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL)
Jingning Hane2ffaf82016-12-14 15:26:30 -08003024 BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL)
3025 BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL)
Cheng Chenf78632e2017-10-20 15:30:51 -07003026#endif // CONFIG_JNT_COMP
Jingning Han9e7c49f2016-12-06 11:20:10 -08003027
Yaowu Xuc27fc142016-08-22 16:08:15 -07003028#define OBFP(BT, OSDF, OVF, OSVF) \
3029 cpi->fn_ptr[BT].osdf = OSDF; \
3030 cpi->fn_ptr[BT].ovf = OVF; \
3031 cpi->fn_ptr[BT].osvf = OSVF;
3032
3033#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07003034 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3035 aom_obmc_sub_pixel_variance128x128)
3036 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3037 aom_obmc_sub_pixel_variance128x64)
3038 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3039 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003040#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07003041 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3042 aom_obmc_sub_pixel_variance64x64)
3043 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3044 aom_obmc_sub_pixel_variance64x32)
3045 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3046 aom_obmc_sub_pixel_variance32x64)
3047 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3048 aom_obmc_sub_pixel_variance32x32)
3049 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3050 aom_obmc_sub_pixel_variance32x16)
3051 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3052 aom_obmc_sub_pixel_variance16x32)
3053 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3054 aom_obmc_sub_pixel_variance16x16)
3055 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3056 aom_obmc_sub_pixel_variance16x8)
3057 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3058 aom_obmc_sub_pixel_variance8x16)
3059 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3060 aom_obmc_sub_pixel_variance8x8)
3061 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3062 aom_obmc_sub_pixel_variance4x8)
3063 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3064 aom_obmc_sub_pixel_variance8x4)
3065 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3066 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003067
3068#if CONFIG_EXT_PARTITION_TYPES
3069 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3070 aom_obmc_sub_pixel_variance4x16)
3071
3072 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3073 aom_obmc_sub_pixel_variance16x4)
3074
3075 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3076 aom_obmc_sub_pixel_variance8x32)
3077
3078 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3079 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003080
3081 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3082 aom_obmc_sub_pixel_variance16x64)
3083
3084 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3085 aom_obmc_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003086
3087#if CONFIG_EXT_PARTITION
3088 OBFP(BLOCK_32X128, aom_obmc_sad32x128, aom_obmc_variance32x128,
3089 aom_obmc_sub_pixel_variance32x128)
3090
3091 OBFP(BLOCK_128X32, aom_obmc_sad128x32, aom_obmc_variance128x32,
3092 aom_obmc_sub_pixel_variance128x32)
3093#endif // CONFIG_EXT_PARTITION
3094#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003095
David Barkerf19f35f2017-05-22 16:33:22 +01003096#define MBFP(BT, MCSDF, MCSVF) \
3097 cpi->fn_ptr[BT].msdf = MCSDF; \
3098 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003099
3100#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01003101 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3102 aom_masked_sub_pixel_variance128x128)
3103 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3104 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003105#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01003106 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3107 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3108 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3109 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3110 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3111 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3112 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3113 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3114 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3115 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3116 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3117 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3118 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003119
3120#if CONFIG_EXT_PARTITION_TYPES
3121 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3122
3123 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3124
3125 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3126
3127 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003128
3129 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3130
3131 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003132
3133#if CONFIG_EXT_PARTITION
3134 MBFP(BLOCK_32X128, aom_masked_sad32x128, aom_masked_sub_pixel_variance32x128)
3135
3136 MBFP(BLOCK_128X32, aom_masked_sad128x32, aom_masked_sub_pixel_variance128x32)
3137#endif // CONFIG_EXT_PARTITION
3138#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003139
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003140#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003141 highbd_set_var_fns(cpi);
3142#endif
3143
Yaowu Xuf883b422016-08-30 14:01:10 -07003144 /* av1_init_quantizer() is first called here. Add check in
3145 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003146 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003147 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003148 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003149 av1_init_quantizer(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003150#if CONFIG_AOM_QM
3151 aom_qm_init(cm);
3152#endif
3153
Yaowu Xuf883b422016-08-30 14:01:10 -07003154 av1_loop_filter_init(cm);
Fergus Simpsondac6aff2017-05-18 15:11:38 -07003155#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07003156 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003157 cm->superres_upscaled_width = oxcf->width;
3158 cm->superres_upscaled_height = oxcf->height;
Fergus Simpsondac6aff2017-05-18 15:11:38 -07003159#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003160#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07003161 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003162#endif // CONFIG_LOOP_RESTORATION
3163
3164 cm->error.setjmp = 0;
3165
3166 return cpi;
3167}
3168
3169#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3170
3171#define SNPRINT2(H, T, V) \
3172 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
3173
Yaowu Xuf883b422016-08-30 14:01:10 -07003174void av1_remove_compressor(AV1_COMP *cpi) {
3175 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003176 unsigned int i;
3177 int t;
3178
3179 if (!cpi) return;
3180
3181 cm = &cpi->common;
3182 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003183#if CONFIG_ENTROPY_STATS
3184 if (cpi->oxcf.pass != 1) {
3185 fprintf(stderr, "Writing counts.stt\n");
3186 FILE *f = fopen("counts.stt", "wb");
3187 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
Zoe Liua56f9162017-06-21 22:49:57 -07003188 fwrite(aggregate_fc_per_type, sizeof(aggregate_fc_per_type[0]),
3189 FRAME_CONTEXTS, f);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003190 fclose(f);
3191 }
3192#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003193#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003194 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003195
3196 if (cpi->oxcf.pass != 1) {
3197 char headings[512] = { 0 };
3198 char results[512] = { 0 };
3199 FILE *f = fopen("opsnr.stt", "a");
3200 double time_encoded =
3201 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3202 10000000.000;
3203 double total_encode_time =
3204 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3205 const double dr =
3206 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3207 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3208 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3209 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3210
3211 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003212 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003213 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3214 const double total_ssim =
3215 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3216 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003217 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003218 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003219 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08003220 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003221 snprintf(results, sizeof(results),
3222 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3223 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003224 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08003225 "%7.3f\t%7.3f\t%7.3f",
Yaowu Xuc27fc142016-08-22 16:08:15 -07003226 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
Jingning Han87651b22017-11-28 20:02:26 -08003227 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003228 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
3229 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003230 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Jingning Han87651b22017-11-28 20:02:26 -08003231 cpi->psnr.stat[Y] / cpi->count, cpi->psnr.stat[U] / cpi->count,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003232 cpi->psnr.stat[V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003233
3234 if (cpi->b_calculate_blockiness) {
3235 SNPRINT(headings, "\t Block\tWstBlck");
3236 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3237 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3238 }
3239
3240 if (cpi->b_calculate_consistency) {
3241 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003242 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003243 (double)cpi->total_inconsistency);
3244
3245 SNPRINT(headings, "\tConsist\tWstCons");
3246 SNPRINT2(results, "\t%7.3f", consistency);
3247 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3248 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003249 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3250 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003251 rate_err, fabs(rate_err));
3252 }
3253
3254 fclose(f);
3255 }
3256
3257#endif
3258
3259#if 0
3260 {
3261 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
3262 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
3263 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
3264 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
3265 cpi->time_compress_data / 1000,
3266 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
3267 }
3268#endif
3269 }
3270
3271 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003272 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003273 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3274
3275 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003276 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003277
3278 // Deallocate allocated thread data.
3279 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07003280 aom_free(thread_data->td->palette_buffer);
Jingning Hand064cf02017-06-01 10:00:39 -07003281 aom_free(thread_data->td->above_pred_buf);
3282 aom_free(thread_data->td->left_pred_buf);
3283 aom_free(thread_data->td->wsrc_buf);
3284 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003285 aom_free(thread_data->td->counts);
3286 av1_free_pc_tree(thread_data->td);
Yaowu Xuf883b422016-08-30 14:01:10 -07003287 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003288 }
3289 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003290 aom_free(cpi->tile_thr_data);
3291 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003292
Yaowu Xuf883b422016-08-30 14:01:10 -07003293 if (cpi->num_workers > 1) av1_loop_filter_dealloc(&cpi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003294
3295 dealloc_compressor_data(cpi);
3296
3297 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3298 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003299 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003300 }
3301
3302#if CONFIG_FP_MB_STATS
3303 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003304 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003305 cpi->twopass.frame_mb_stats_buf = NULL;
3306 }
3307#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003308#if CONFIG_INTERNAL_STATS
3309 aom_free(cpi->ssim_vars);
3310 cpi->ssim_vars = NULL;
3311#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003312
Yaowu Xuf883b422016-08-30 14:01:10 -07003313 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003314#if CONFIG_HASH_ME
3315 for (i = 0; i < FRAME_BUFFERS; ++i) {
3316 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3317 }
3318#endif // CONFIG_HASH_ME
Yaowu Xuf883b422016-08-30 14:01:10 -07003319 av1_free_ref_frame_buffers(cm->buffer_pool);
3320 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003321
3322#ifdef OUTPUT_YUV_SKINMAP
3323 fclose(yuv_skinmap_file);
3324#endif
3325#ifdef OUTPUT_YUV_REC
3326 fclose(yuv_rec_file);
3327#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003328#if 0
3329
3330 if (keyfile)
3331 fclose(keyfile);
3332
3333 if (framepsnr)
3334 fclose(framepsnr);
3335
3336 if (kf_list)
3337 fclose(kf_list);
3338
3339#endif
3340}
3341
Yaowu Xuf883b422016-08-30 14:01:10 -07003342static void generate_psnr_packet(AV1_COMP *cpi) {
3343 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003344 int i;
3345 PSNR_STATS psnr;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003346#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07003347 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003348 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3349#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07003350 aom_calc_psnr(cpi->source, cpi->common.frame_to_show, &psnr);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003351#endif
3352
3353 for (i = 0; i < 4; ++i) {
3354 pkt.data.psnr.samples[i] = psnr.samples[i];
3355 pkt.data.psnr.sse[i] = psnr.sse[i];
3356 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3357 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003358 pkt.kind = AOM_CODEC_PSNR_PKT;
3359 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003360}
3361
Yaowu Xuf883b422016-08-30 14:01:10 -07003362int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003363 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3364
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003365 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003366 return 0;
3367}
3368
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003369void av1_update_reference(AV1_COMP *cpi, int ref_frame_upd_flags) {
3370 cpi->ext_refresh_last_frame = (ref_frame_upd_flags & AOM_LAST_FLAG) != 0;
3371 cpi->ext_refresh_golden_frame = (ref_frame_upd_flags & AOM_GOLD_FLAG) != 0;
3372 cpi->ext_refresh_alt_ref_frame = (ref_frame_upd_flags & AOM_ALT_FLAG) != 0;
3373 cpi->ext_refresh_bwd_ref_frame = (ref_frame_upd_flags & AOM_BWD_FLAG) != 0;
3374 cpi->ext_refresh_alt2_ref_frame = (ref_frame_upd_flags & AOM_ALT2_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003375 cpi->ext_refresh_frame_flags_pending = 1;
3376}
3377
Thomas Daede497d1952017-08-08 17:33:06 -07003378int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3379 AV1_COMMON *const cm = &cpi->common;
3380 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003381 if (cfg) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003382 aom_yv12_copy_frame(cfg, sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003383 return 0;
3384 } else {
3385 return -1;
3386 }
3387}
3388
Thomas Daede497d1952017-08-08 17:33:06 -07003389int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3390 AV1_COMMON *const cm = &cpi->common;
3391 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003392 if (cfg) {
3393 aom_yv12_copy_frame(sd, cfg);
3394 return 0;
3395 } else {
3396 return -1;
3397 }
3398}
3399
3400int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003401 cpi->ext_refresh_frame_context = update;
3402 cpi->ext_refresh_frame_context_pending = 1;
3403 return 0;
3404}
3405
3406#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3407// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3408// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3409// not denoise the UV channels at this time. If ever we implement UV channel
3410// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003411void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003412 uint8_t *src = s->y_buffer;
3413 int h = s->y_height;
3414
3415 do {
3416 fwrite(src, s->y_width, 1, f);
3417 src += s->y_stride;
3418 } while (--h);
3419
3420 src = s->u_buffer;
3421 h = s->uv_height;
3422
3423 do {
3424 fwrite(src, s->uv_width, 1, f);
3425 src += s->uv_stride;
3426 } while (--h);
3427
3428 src = s->v_buffer;
3429 h = s->uv_height;
3430
3431 do {
3432 fwrite(src, s->uv_width, 1, f);
3433 src += s->uv_stride;
3434 } while (--h);
3435}
3436#endif
3437
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003438#if !CONFIG_XIPHRC
Zoe Liu8dd1c982017-09-11 10:14:35 -07003439#if USE_GF16_MULTI_LAYER
3440static void check_show_existing_frame_gf16(AV1_COMP *cpi) {
3441 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3442 AV1_COMMON *const cm = &cpi->common;
3443 const FRAME_UPDATE_TYPE next_frame_update_type =
3444 gf_group->update_type[gf_group->index];
3445
3446 if (cm->show_existing_frame == 1) {
3447 cm->show_existing_frame = 0;
3448 } else if (cpi->rc.is_last_bipred_frame) {
3449 cpi->rc.is_last_bipred_frame = 0;
3450 cm->show_existing_frame = 1;
3451 cpi->existing_fb_idx_to_show = cpi->bwd_fb_idx;
3452 } else if (next_frame_update_type == OVERLAY_UPDATE ||
3453 next_frame_update_type == INTNL_OVERLAY_UPDATE) {
3454 // Check the temporal filtering status for the next OVERLAY frame
3455 const int num_arfs_in_gf = cpi->num_extra_arfs + 1;
3456 int which_arf = 0, arf_idx;
3457 // Identify the index to the next overlay frame.
3458 for (arf_idx = 0; arf_idx < num_arfs_in_gf; arf_idx++) {
3459 if (gf_group->index == cpi->arf_pos_for_ovrly[arf_idx]) {
3460 which_arf = arf_idx;
3461 break;
3462 }
3463 }
3464 assert(arf_idx < num_arfs_in_gf);
3465 if (cpi->is_arf_filter_off[which_arf]) {
3466 cm->show_existing_frame = 1;
3467 cpi->rc.is_src_frame_alt_ref = 1;
3468 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3469 ? cpi->alt_fb_idx
3470 : cpi->bwd_fb_idx;
3471 cpi->is_arf_filter_off[which_arf] = 0;
3472 }
3473 }
3474 cpi->rc.is_src_frame_ext_arf = 0;
3475}
3476#endif // USE_GF16_MULTI_LAYER
3477
Yaowu Xuf883b422016-08-30 14:01:10 -07003478static void check_show_existing_frame(AV1_COMP *cpi) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003479#if USE_GF16_MULTI_LAYER
3480 if (cpi->rc.baseline_gf_interval == 16) {
3481 check_show_existing_frame_gf16(cpi);
3482 return;
3483 }
3484#endif // USE_GF16_MULTI_LAYER
3485
Yaowu Xuc27fc142016-08-22 16:08:15 -07003486 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003487 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488 const FRAME_UPDATE_TYPE next_frame_update_type =
3489 gf_group->update_type[gf_group->index];
3490 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07003491
3492 if (cm->show_existing_frame == 1) {
3493 cm->show_existing_frame = 0;
3494 } else if (cpi->rc.is_last_bipred_frame) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003495 // NOTE: If the current frame is a last bi-predictive frame, it is
3496 // needed next to show the BWDREF_FRAME, which is pointed by
3497 // the last_fb_idxes[0] after reference frame buffer update
Yaowu Xuc27fc142016-08-22 16:08:15 -07003498 cpi->rc.is_last_bipred_frame = 0;
3499 cm->show_existing_frame = 1;
3500 cpi->existing_fb_idx_to_show = cpi->lst_fb_idxes[0];
3501 } else if (cpi->is_arf_filter_off[which_arf] &&
3502 (next_frame_update_type == OVERLAY_UPDATE ||
3503 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
3504 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003505 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003506 cm->show_existing_frame = 1;
3507 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003508 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3509 ? cpi->alt_fb_idx
3510 : cpi->alt2_fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003511 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003512 }
3513 cpi->rc.is_src_frame_ext_arf = 0;
3514}
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003515#endif // !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516
3517#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003518void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003519 uint8_t *src = s->y_buffer;
3520 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003521 if (yuv_rec_file == NULL) return;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003522#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003523 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3524 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3525
3526 do {
3527 fwrite(src16, s->y_width, 2, yuv_rec_file);
3528 src16 += s->y_stride;
3529 } while (--h);
3530
3531 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3532 h = s->uv_height;
3533
3534 do {
3535 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3536 src16 += s->uv_stride;
3537 } while (--h);
3538
3539 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3540 h = s->uv_height;
3541
3542 do {
3543 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3544 src16 += s->uv_stride;
3545 } while (--h);
3546
3547 fflush(yuv_rec_file);
3548 return;
3549 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003550#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003551
3552 do {
3553 fwrite(src, s->y_width, 1, yuv_rec_file);
3554 src += s->y_stride;
3555 } while (--h);
3556
3557 src = s->u_buffer;
3558 h = s->uv_height;
3559
3560 do {
3561 fwrite(src, s->uv_width, 1, yuv_rec_file);
3562 src += s->uv_stride;
3563 } while (--h);
3564
3565 src = s->v_buffer;
3566 h = s->uv_height;
3567
3568 do {
3569 fwrite(src, s->uv_width, 1, yuv_rec_file);
3570 src += s->uv_stride;
3571 } while (--h);
3572
3573 fflush(yuv_rec_file);
3574}
3575#endif // OUTPUT_YUV_REC
3576
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003577#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003578static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3579 int i;
3580 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003581 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003582 AV1_COMMON *const cm = &cpi->common;
3583 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3584 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003585 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003586 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003587 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003588 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003589 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003590 recode = 1;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003591 recode |= (rdc->global_motion_used[i] > 0);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003592 }
3593 }
3594 return recode;
3595}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003596
Yaowu Xuc27fc142016-08-22 16:08:15 -07003597// Function to test for conditions that indicate we should loop
3598// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003599static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3600 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003601 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003602 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003603 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3604 int force_recode = 0;
3605
3606 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3607 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3608 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003609 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3610 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3611 (rc->projected_frame_size < low_limit && q > minq)) {
3612 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003613 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003614 // Deal with frame undershoot and whether or not we are
3615 // below the automatically set cq level.
3616 if (q > oxcf->cq_level &&
3617 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3618 force_recode = 1;
3619 }
3620 }
3621 }
3622 return force_recode;
3623}
3624
Yaowu Xuc27fc142016-08-22 16:08:15 -07003625#define DUMP_REF_FRAME_IMAGES 0
3626
3627#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003628static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003629 const YV12_BUFFER_CONFIG *const ref_buf,
3630 char *file_name) {
3631 int h;
3632 FILE *f_ref = NULL;
3633
3634 if (ref_buf == NULL) {
3635 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003636 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003637 }
3638
3639 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3640 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003641 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003642 }
3643
3644 // --- Y ---
3645 for (h = 0; h < cm->height; ++h) {
3646 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3647 }
3648 // --- U ---
3649 for (h = 0; h < (cm->height >> 1); ++h) {
3650 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3651 f_ref);
3652 }
3653 // --- V ---
3654 for (h = 0; h < (cm->height >> 1); ++h) {
3655 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3656 f_ref);
3657 }
3658
3659 fclose(f_ref);
3660
Yaowu Xuf883b422016-08-30 14:01:10 -07003661 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003662}
3663
Yaowu Xuf883b422016-08-30 14:01:10 -07003664static void dump_ref_frame_images(AV1_COMP *cpi) {
3665 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003666 MV_REFERENCE_FRAME ref_frame;
3667
3668 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3669 char file_name[256] = "";
3670 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3671 cm->current_video_frame, ref_frame);
3672 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3673 }
3674}
3675#endif // DUMP_REF_FRAME_IMAGES == 1
3676
Yaowu Xuc27fc142016-08-22 16:08:15 -07003677// This function is used to shift the virtual indices of last reference frames
3678// as follows:
3679// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3680// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003681static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003682 int ref_frame;
3683 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
3684 cpi->lst_fb_idxes[ref_frame] = cpi->lst_fb_idxes[ref_frame - 1];
3685
3686 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003687 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003688 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003689 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3690 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3691 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003692 }
3693 }
3694}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003695
Zoe Liu8dd1c982017-09-11 10:14:35 -07003696#if USE_GF16_MULTI_LAYER
3697static void update_reference_frames_gf16(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003698 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003699 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003700
Zoe Liu8dd1c982017-09-11 10:14:35 -07003701 if (cm->frame_type == KEY_FRAME) {
3702 for (int ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3703 ref_cnt_fb(pool->frame_bufs,
3704 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3705 cm->new_fb_idx);
3706 }
3707 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3708 cm->new_fb_idx);
3709 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3710 cm->new_fb_idx);
3711 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3712 cm->new_fb_idx);
3713 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3714 cm->new_fb_idx);
3715 } else {
3716 if (cpi->refresh_last_frame || cpi->refresh_golden_frame ||
3717 cpi->refresh_bwd_ref_frame || cpi->refresh_alt2_ref_frame ||
3718 cpi->refresh_alt_ref_frame) {
3719 assert(cpi->refresh_fb_idx >= 0 && cpi->refresh_fb_idx < REF_FRAMES);
3720 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->refresh_fb_idx],
3721 cm->new_fb_idx);
3722 }
3723
3724 // TODO(zoeliu): To handle cpi->interp_filter_selected[].
3725
3726 // For GF of 16, an additional ref frame index mapping needs to be handled
3727 // if this is the last frame to encode in the current GF group.
3728 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3729 if (gf_group->update_type[gf_group->index + 1] == OVERLAY_UPDATE)
3730 av1_ref_frame_map_idx_updates(cpi, gf_group->index + 1);
3731 }
3732
3733#if DUMP_REF_FRAME_IMAGES == 1
3734 // Dump out all reference frame images.
3735 dump_ref_frame_images(cpi);
3736#endif // DUMP_REF_FRAME_IMAGES
3737}
3738#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003739
3740static void update_reference_frames(AV1_COMP *cpi) {
3741 AV1_COMMON *const cm = &cpi->common;
3742
Yaowu Xuc27fc142016-08-22 16:08:15 -07003743 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3744 // for the purpose to verify no mismatch between encoder and decoder.
3745 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3746
Zoe Liu8dd1c982017-09-11 10:14:35 -07003747#if USE_GF16_MULTI_LAYER
3748 if (cpi->rc.baseline_gf_interval == 16) {
3749 update_reference_frames_gf16(cpi);
3750 return;
3751 }
3752#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003753
3754 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003755 // At this point the new frame has been encoded.
3756 // If any buffer copy / swapping is signaled it should be done here.
3757 if (cm->frame_type == KEY_FRAME) {
3758 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3759 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003760 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3761 cm->new_fb_idx);
Zoe Liue9b15e22017-07-19 15:53:01 -07003762 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3763 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003764 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3765 cm->new_fb_idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003766 } else if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003767 // We have decided to preserve the previously existing golden frame as our
3768 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003769 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003770 // we're updating the GF with the current decoded frame, we save it to the
3771 // ARF slot instead.
3772 // We now have to update the ARF with the current frame and swap gld_fb_idx
3773 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3774 // slot and, if we're updating the GF, the current frame becomes the new GF.
3775 int tmp;
3776
3777 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3778 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003779 tmp = cpi->alt_fb_idx;
3780 cpi->alt_fb_idx = cpi->gld_fb_idx;
3781 cpi->gld_fb_idx = tmp;
3782
Yaowu Xuc27fc142016-08-22 16:08:15 -07003783 // We need to modify the mapping accordingly
3784 cpi->arf_map[0] = cpi->alt_fb_idx;
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003785 // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3786 // cpi->interp_filter_selected[GOLDEN_FRAME]?
Yaowu Xuc27fc142016-08-22 16:08:15 -07003787 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3788 // Deal with the special case for showing existing internal ALTREF_FRAME
3789 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3790 // by updating the virtual indices.
3791 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -07003792 const int which_arf = gf_group->arf_ref_idx[gf_group->index];
3793 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003794
Zoe Liue9b15e22017-07-19 15:53:01 -07003795 const int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003796 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003797
Zoe Liue9b15e22017-07-19 15:53:01 -07003798 cpi->lst_fb_idxes[0] = cpi->alt2_fb_idx;
3799 cpi->alt2_fb_idx = tmp;
3800 // We need to modify the mapping accordingly
3801 cpi->arf_map[which_arf] = cpi->alt2_fb_idx;
3802
3803 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3804 cpi->interp_filter_selected[ALTREF2_FRAME],
3805 sizeof(cpi->interp_filter_selected[ALTREF2_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003806 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003807 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003808 if (cpi->refresh_alt_ref_frame) {
3809 int arf_idx = cpi->alt_fb_idx;
3810 int which_arf = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003811 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003812
3813 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3814 cpi->interp_filter_selected[0],
3815 sizeof(cpi->interp_filter_selected[0]));
3816 }
3817
Zoe Liue9b15e22017-07-19 15:53:01 -07003818 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003819 if (cpi->refresh_golden_frame) {
3820 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3821 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003822
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003823 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3824 cpi->interp_filter_selected[0],
3825 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003826 }
3827
Zoe Liue9b15e22017-07-19 15:53:01 -07003828 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003829 if (cpi->refresh_bwd_ref_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003830 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3831 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003832
3833 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3834 cpi->interp_filter_selected[0],
3835 sizeof(cpi->interp_filter_selected[0]));
3836 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003837
Zoe Liue9b15e22017-07-19 15:53:01 -07003838 // === ALTREF2_FRAME ===
3839 if (cpi->refresh_alt2_ref_frame) {
3840 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3841 cm->new_fb_idx);
3842
3843 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3844 cpi->interp_filter_selected[0],
3845 sizeof(cpi->interp_filter_selected[0]));
3846 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003847 }
3848
3849 if (cpi->refresh_last_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003850 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3851 // reference to the reference frame buffer virtual index; and then (2) from
3852 // the virtual index to the reference frame buffer physical index:
3853 //
3854 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3855 // | | |
3856 // v v v
3857 // lst_fb_idxes[0], ..., lst_fb_idxes[2], ..., alt_fb_idx
3858 // | | |
3859 // v v v
3860 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3861 //
3862 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3863 // have the other 2 LAST reference frames shifted as follows:
3864 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3865 // , and then have LAST_FRAME refreshed by the newly coded frame.
3866 //
3867 // To fulfill it, the decoder will be notified to execute following 2 steps:
3868 //
3869 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3870 // to point to the newly coded frame, i.e.
3871 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3872 //
3873 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3874 // original virtual index of LAST3_FRAME and have the other mappings
3875 // shifted as follows:
3876 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3877 // | | |
3878 // v v v
3879 // lst_fb_idxes[2], lst_fb_idxes[0], lst_fb_idxes[1]
3880 int ref_frame;
Zoe Liu5fca7242016-10-10 17:18:57 -07003881
Yaowu Xuc27fc142016-08-22 16:08:15 -07003882 if (cm->frame_type == KEY_FRAME) {
3883 for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3884 ref_cnt_fb(pool->frame_bufs,
3885 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3886 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003887 }
3888 } else {
3889 int tmp;
3890
3891 ref_cnt_fb(pool->frame_bufs,
3892 &cm->ref_frame_map[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3893 cm->new_fb_idx);
3894
Yaowu Xuc27fc142016-08-22 16:08:15 -07003895 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3896
3897 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003898 cpi->lst_fb_idxes[0] = tmp;
3899
Zoe Liuf0e46692016-10-12 12:31:43 -07003900 assert(cm->show_existing_frame == 0);
Zoe Liuf0e46692016-10-12 12:31:43 -07003901 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3902 cpi->interp_filter_selected[0],
3903 sizeof(cpi->interp_filter_selected[0]));
Zoe Liuaff92d52017-07-11 21:35:08 -07003904
3905 if (cpi->rc.is_last_bipred_frame) {
3906 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3907 // LAST3_FRAME by updating the virtual indices.
3908 //
3909 // NOTE: The source frame for BWDREF does not have a holding position as
3910 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3911 // virtual index reshuffling for BWDREF, the encoder always
3912 // specifies a LAST_BIPRED right before BWDREF and completes the
3913 // reshuffling job accordingly.
3914 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3915
3916 shift_last_ref_frames(cpi);
3917 cpi->lst_fb_idxes[0] = cpi->bwd_fb_idx;
3918 cpi->bwd_fb_idx = tmp;
3919
3920 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3921 cpi->interp_filter_selected[BWDREF_FRAME],
3922 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
3923 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003924 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003925 }
3926
3927#if DUMP_REF_FRAME_IMAGES == 1
3928 // Dump out all reference frame images.
3929 dump_ref_frame_images(cpi);
3930#endif // DUMP_REF_FRAME_IMAGES
3931}
3932
Yaowu Xuf883b422016-08-30 14:01:10 -07003933static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003934 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003935 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003936 ensure_mv_buffer(new_fb_ptr, cm);
3937 new_fb_ptr->width = cm->width;
3938 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003939}
3940
Soo-Chul Han934af352017-10-15 15:21:51 -04003941#if CONFIG_SEGMENT_PRED_LAST
3942static INLINE void alloc_frame_segmap(AV1_COMMON *const cm, int buffer_idx) {
3943 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
3944 if (new_fb_ptr->seg_map == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
3945 new_fb_ptr->mi_cols < cm->mi_cols) {
3946 aom_free(new_fb_ptr->seg_map);
3947 CHECK_MEM_ERROR(cm, new_fb_ptr->seg_map,
3948 (uint8_t *)aom_calloc(cm->mi_rows * cm->mi_cols,
3949 sizeof(*new_fb_ptr->seg_map)));
3950 new_fb_ptr->mi_rows = cm->mi_rows;
3951 new_fb_ptr->mi_cols = cm->mi_cols;
3952 }
3953}
3954#endif
3955
Cheng Chen46f30c72017-09-07 11:13:33 -07003956static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003957 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003958 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003959 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003960 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3961 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003962 };
3963
3964 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003965 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003966 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3967 BufferPool *const pool = cm->buffer_pool;
3968 const YV12_BUFFER_CONFIG *const ref =
3969 get_ref_frame_buffer(cpi, ref_frame);
3970
3971 if (ref == NULL) {
3972 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3973 continue;
3974 }
3975
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003976#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003977 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3978 RefCntBuffer *new_fb_ptr = NULL;
3979 int force_scaling = 0;
3980 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3981 if (new_fb == INVALID_IDX) {
3982 new_fb = get_free_fb(cm);
3983 force_scaling = 1;
3984 }
3985 if (new_fb == INVALID_IDX) return;
3986 new_fb_ptr = &pool->frame_bufs[new_fb];
3987 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3988 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003989 if (aom_realloc_frame_buffer(
3990 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3991 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3992 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003993 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003994 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003995 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf,
3996 (int)cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003997 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3998 alloc_frame_mvs(cm, new_fb);
Soo-Chul Han934af352017-10-15 15:21:51 -04003999#if CONFIG_SEGMENT_PRED_LAST
4000 alloc_frame_segmap(cm, new_fb);
4001#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004002 }
4003#else
4004 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
4005 RefCntBuffer *new_fb_ptr = NULL;
4006 int force_scaling = 0;
4007 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
4008 if (new_fb == INVALID_IDX) {
4009 new_fb = get_free_fb(cm);
4010 force_scaling = 1;
4011 }
4012 if (new_fb == INVALID_IDX) return;
4013 new_fb_ptr = &pool->frame_bufs[new_fb];
4014 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
4015 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004016 if (aom_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004017 cm->subsampling_x, cm->subsampling_y,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07004018 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
4019 NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004020 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004021 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07004022 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004023 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
4024 alloc_frame_mvs(cm, new_fb);
Soo-Chul Han934af352017-10-15 15:21:51 -04004025#if CONFIG_SEGMENT_PRED_LAST
4026 alloc_frame_segmap(cm, new_fb);
4027#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004028 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004029#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004030 } else {
4031 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4032 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
4033 buf->buf.y_crop_width = ref->y_crop_width;
4034 buf->buf.y_crop_height = ref->y_crop_height;
4035 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
4036 ++buf->ref_count;
4037 }
4038 } else {
4039 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
4040 }
4041 }
4042}
4043
Yaowu Xuf883b422016-08-30 14:01:10 -07004044static void release_scaled_references(AV1_COMP *cpi) {
4045 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004046 int i;
4047 if (cpi->oxcf.pass == 0) {
4048 // Only release scaled references under certain conditions:
4049 // if reference will be updated, or if scaled reference has same resolution.
4050 int refresh[INTER_REFS_PER_FRAME];
4051 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004052 refresh[1] = refresh[2] = 0;
4053 refresh[3] = (cpi->refresh_golden_frame) ? 1 : 0;
4054 refresh[4] = (cpi->refresh_bwd_ref_frame) ? 1 : 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07004055 refresh[5] = (cpi->refresh_alt2_ref_frame) ? 1 : 0;
4056 refresh[6] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004057 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
4058 const int idx = cpi->scaled_ref_idx[i - 1];
4059 RefCntBuffer *const buf =
4060 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
4061 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
4062 if (buf != NULL &&
4063 (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
4064 buf->buf.y_crop_height == ref->y_crop_height))) {
4065 --buf->ref_count;
4066 cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
4067 }
4068 }
4069 } else {
4070 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) {
4071 const int idx = cpi->scaled_ref_idx[i];
4072 RefCntBuffer *const buf =
4073 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
4074 if (buf != NULL) {
4075 --buf->ref_count;
4076 cpi->scaled_ref_idx[i] = INVALID_IDX;
4077 }
4078 }
4079 }
4080}
4081
Yaowu Xuc27fc142016-08-22 16:08:15 -07004082#if 0 && CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07004083static void output_frame_level_debug_stats(AV1_COMP *cpi) {
4084 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004085 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
4086 int64_t recon_err;
4087
Yaowu Xuf883b422016-08-30 14:01:10 -07004088 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004089
Alex Conversef77fd0b2017-04-20 11:00:24 -07004090 recon_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004091
4092 if (cpi->twopass.total_left_stats.coded_error != 0.0)
Yunqing Wang8c1e57c2016-10-25 15:15:23 -07004093 fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
Yaowu Xuc27fc142016-08-22 16:08:15 -07004094 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
4095 "%10"PRId64" %10"PRId64" %10d "
4096 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
4097 "%6d %6d %5d %5d %5d "
4098 "%10"PRId64" %10.3lf"
4099 "%10lf %8u %10"PRId64" %10d %10d %10d\n",
4100 cpi->common.current_video_frame,
4101 cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004102 cpi->rc.source_alt_ref_pending,
4103 cpi->rc.source_alt_ref_active,
4104 cpi->rc.this_frame_target,
4105 cpi->rc.projected_frame_size,
4106 cpi->rc.projected_frame_size / cpi->common.MBs,
4107 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
4108 cpi->rc.vbr_bits_off_target,
4109 cpi->rc.vbr_bits_off_target_fast,
4110 cpi->twopass.extend_minq,
4111 cpi->twopass.extend_minq_fast,
4112 cpi->rc.total_target_vs_actual,
4113 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
4114 cpi->rc.total_actual_bits, cm->base_qindex,
Yaowu Xuf883b422016-08-30 14:01:10 -07004115 av1_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
4116 (double)av1_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
4117 av1_convert_qindex_to_q(cpi->twopass.active_worst_quality,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004118 cm->bit_depth),
4119 cpi->rc.avg_q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004120 av1_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
Yaowu Xuc27fc142016-08-22 16:08:15 -07004121 cpi->refresh_last_frame, cpi->refresh_golden_frame,
4122 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
4123 cpi->twopass.bits_left,
4124 cpi->twopass.total_left_stats.coded_error,
4125 cpi->twopass.bits_left /
4126 (1 + cpi->twopass.total_left_stats.coded_error),
4127 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
4128 cpi->twopass.kf_zeromotion_pct,
4129 cpi->twopass.fr_content_type);
4130
4131 fclose(f);
4132
4133 if (0) {
4134 FILE *const fmodes = fopen("Modes.stt", "a");
4135 int i;
4136
4137 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
4138 cm->frame_type, cpi->refresh_golden_frame,
4139 cpi->refresh_alt_ref_frame);
4140
4141 for (i = 0; i < MAX_MODES; ++i)
4142 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4143
4144 fprintf(fmodes, "\n");
4145
4146 fclose(fmodes);
4147 }
4148}
4149#endif
4150
Yaowu Xuf883b422016-08-30 14:01:10 -07004151static void set_mv_search_params(AV1_COMP *cpi) {
4152 const AV1_COMMON *const cm = &cpi->common;
4153 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004154
4155 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07004156 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004157
4158 if (cpi->sf.mv.auto_mv_step_size) {
4159 if (frame_is_intra_only(cm)) {
4160 // Initialize max_mv_magnitude for use in the first INTER frame
4161 // after a key/intra-only frame.
4162 cpi->max_mv_magnitude = max_mv_def;
4163 } else {
4164 if (cm->show_frame) {
4165 // Allow mv_steps to correspond to twice the max mv magnitude found
4166 // in the previous frame, capped by the default max_mv_magnitude based
4167 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07004168 cpi->mv_step_param = av1_init_search_range(
4169 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004170 }
4171 cpi->max_mv_magnitude = 0;
4172 }
4173 }
4174}
4175
Yaowu Xuf883b422016-08-30 14:01:10 -07004176static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004177 int i;
4178 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01004179 cpi->common.global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004180 }
4181 cpi->global_motion_search_done = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004182 av1_set_speed_features_framesize_independent(cpi);
4183 av1_set_rd_speed_thresholds(cpi);
4184 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004185 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07004186 if (!frame_is_intra_only(&cpi->common)) set_compound_tools(&cpi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004187}
4188
Yaowu Xuf883b422016-08-30 14:01:10 -07004189static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004190 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004191 AV1_COMMON *const cm = &cpi->common;
4192 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004193
4194 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004195 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004196
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004197// Decide q and q bounds.
4198#if CONFIG_XIPHRC
4199 int frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
4200 *q = od_enc_rc_select_quantizers_and_lambdas(
4201 &cpi->od_rc, cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
4202 frame_type, bottom_index, top_index);
4203#else
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004204 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
4205 top_index);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004206#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004207
James Zern01a9d702017-08-25 19:09:33 +00004208 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07004209#if CONFIG_AMVR
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07004210#if CONFIG_EIGHTH_PEL_MV_ONLY
4211 set_high_precision_mv(cpi, 1, cpi->common.cur_frame_force_integer_mv);
4212#else
RogerZhou3b635242017-09-19 10:06:46 -07004213 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07004214 cpi->common.cur_frame_force_integer_mv);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07004215#endif // CONFIG_EIGHTH_PEL_MV_ONLY
4216#else
4217#if CONFIG_EIGHTH_PEL_MV_ONLY
4218 set_high_precision_mv(cpi, 1);
RogerZhou3b635242017-09-19 10:06:46 -07004219#else
Cheng Chen46f30c72017-09-07 11:13:33 -07004220 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07004221#endif // CONFIG_EIGHTH_PEL_MV_ONLY
RogerZhou3b635242017-09-19 10:06:46 -07004222#endif
James Zern01a9d702017-08-25 19:09:33 +00004223 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004224
4225 // Configure experimental use of segmentation for enhanced coding of
4226 // static regions if indicated.
4227 // Only allowed in the second pass of a two pass encode, as it requires
4228 // lagged coding, and if the relevant speed feature flag is set.
4229 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
4230 configure_static_seg_features(cpi);
4231}
4232
Yaowu Xuf883b422016-08-30 14:01:10 -07004233static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004234 int y_stride = cpi->scaled_source.y_stride;
4235
4236 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004237 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004238 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004239 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004240 }
4241}
4242
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004243#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004244#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004245static void set_restoration_unit_size(int width, int height, int sx, int sy,
4246 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004247 (void)width;
4248 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004249 (void)sx;
4250 (void)sy;
4251#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4252 int s = AOMMIN(sx, sy);
4253#else
4254 int s = 0;
4255#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4256
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004257 rst[0].restoration_unit_size = (RESTORATION_TILESIZE_MAX >> 1);
4258 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4259 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004260}
4261#endif // CONFIG_LOOP_RESTORATION
4262
Cheng Chen46f30c72017-09-07 11:13:33 -07004263static void init_ref_frame_bufs(AV1_COMMON *cm) {
4264 int i;
4265 BufferPool *const pool = cm->buffer_pool;
4266 cm->new_fb_idx = INVALID_IDX;
4267 for (i = 0; i < REF_FRAMES; ++i) {
4268 cm->ref_frame_map[i] = INVALID_IDX;
4269 pool->frame_bufs[i].ref_count = 0;
4270 }
4271#if CONFIG_HASH_ME
4272 for (i = 0; i < FRAME_BUFFERS; ++i) {
4273 av1_hash_table_init(&pool->frame_bufs[i].hash_table);
4274 }
4275#endif
4276}
4277
4278static void check_initial_width(AV1_COMP *cpi,
4279#if CONFIG_HIGHBITDEPTH
4280 int use_highbitdepth,
4281#endif
4282 int subsampling_x, int subsampling_y) {
4283 AV1_COMMON *const cm = &cpi->common;
4284
4285 if (!cpi->initial_width ||
4286#if CONFIG_HIGHBITDEPTH
4287 cm->use_highbitdepth != use_highbitdepth ||
4288#endif
4289 cm->subsampling_x != subsampling_x ||
4290 cm->subsampling_y != subsampling_y) {
4291 cm->subsampling_x = subsampling_x;
4292 cm->subsampling_y = subsampling_y;
4293#if CONFIG_HIGHBITDEPTH
4294 cm->use_highbitdepth = use_highbitdepth;
4295#endif
4296
4297 alloc_raw_frame_buffers(cpi);
4298 init_ref_frame_bufs(cm);
4299 alloc_util_frame_buffers(cpi);
4300
4301 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4302
4303 cpi->initial_width = cm->width;
4304 cpi->initial_height = cm->height;
4305 cpi->initial_mbs = cm->MBs;
4306 }
4307}
4308
4309// Returns 1 if the assigned width or height was <= 0.
4310static int set_size_literal(AV1_COMP *cpi, int width, int height) {
4311 AV1_COMMON *cm = &cpi->common;
4312#if CONFIG_HIGHBITDEPTH
4313 check_initial_width(cpi, cm->use_highbitdepth, cm->subsampling_x,
4314 cm->subsampling_y);
4315#else
4316 check_initial_width(cpi, cm->subsampling_x, cm->subsampling_y);
4317#endif // CONFIG_HIGHBITDEPTH
4318
4319 if (width <= 0 || height <= 0) return 1;
4320
4321 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004322 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004323
4324 if (cpi->initial_width && cpi->initial_height &&
4325 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4326 av1_free_context_buffers(cm);
4327 av1_free_pc_tree(&cpi->td);
4328 alloc_compressor_data(cpi);
4329 realloc_segmentation_maps(cpi);
4330 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004331 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004332 update_frame_size(cpi);
4333
4334 return 0;
4335}
4336
Fergus Simpsonbc189932017-05-16 17:02:39 -07004337static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004338 AV1_COMMON *const cm = &cpi->common;
Fergus Simpsonbc189932017-05-16 17:02:39 -07004339 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004340 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004341
Fergus Simpsonbc189932017-05-16 17:02:39 -07004342 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004343 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07004344 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004345 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004346 }
4347
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004348#if !CONFIG_XIPHRC
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004349 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004350 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004351 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004352#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004353
4354 alloc_frame_mvs(cm, cm->new_fb_idx);
Soo-Chul Han934af352017-10-15 15:21:51 -04004355#if CONFIG_SEGMENT_PRED_LAST
4356 alloc_frame_segmap(cm, cm->new_fb_idx);
4357#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004358
4359 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07004360 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004361 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004362#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363 cm->use_highbitdepth,
4364#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07004365 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
4366 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004367 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004368 "Failed to allocate frame buffer");
4369
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004370#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004371#if CONFIG_FRAME_SUPERRES
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004372 const int frame_width = cm->superres_upscaled_width;
4373 const int frame_height = cm->superres_upscaled_height;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004374#else
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004375 const int frame_width = cm->width;
4376 const int frame_height = cm->height;
4377#endif
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004378 set_restoration_unit_size(frame_width, frame_height, cm->subsampling_x,
4379 cm->subsampling_y, cm->rst_info);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004380 for (int i = 0; i < MAX_MB_PLANE; ++i)
4381 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004382
4383 av1_alloc_restoration_buffers(cm);
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004384#endif // CONFIG_LOOP_RESTORATION
4385 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004386 init_motion_estimation(cpi);
4387
4388 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4389 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
4390 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4391
4392 ref_buf->idx = buf_idx;
4393
4394 if (buf_idx != INVALID_IDX) {
4395 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
4396 ref_buf->buf = buf;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004397#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004398 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004399 &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
4400 cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
4401#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004402 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
4403 buf->y_crop_height, cm->width,
4404 cm->height);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004405#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004406 if (av1_is_scaled(&ref_buf->sf)) aom_extend_frame_borders(buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004407 } else {
4408 ref_buf->buf = NULL;
4409 }
4410 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004411
Alex Conversee816b312017-05-01 09:51:24 -07004412#if CONFIG_INTRABC
4413#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan1dcb7072017-05-12 11:13:05 +02004414 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4415 cm->width, cm->height,
4416 cm->use_highbitdepth);
Alex Conversee816b312017-05-01 09:51:24 -07004417#else
4418 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4419 cm->width, cm->height);
4420#endif // CONFIG_HIGHBITDEPTH
4421#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004422
4423 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4424}
4425
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004426static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4427 // Choose an arbitrary random number
4428 static unsigned int seed = 56789;
4429 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004430 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4431 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004432
4433 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004434 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004435 case RESIZE_FIXED:
4436 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004437 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004438 else
Urvang Joshide71d142017-10-05 12:12:15 -07004439 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004440 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004441 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004442 default: assert(0);
4443 }
Urvang Joshide71d142017-10-05 12:12:15 -07004444 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004445}
4446
4447#if CONFIG_FRAME_SUPERRES
Urvang Joshie58b5642017-10-05 17:59:43 -07004448
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004449static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4450 // Choose an arbitrary random number
4451 static unsigned int seed = 34567;
4452 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004453 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4454 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004455 int bottom_index, top_index, q, qthresh;
4456
4457 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004458 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004459 case SUPERRES_FIXED:
4460 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004461 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004462 else
Urvang Joshide71d142017-10-05 12:12:15 -07004463 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004464 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004465 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004466 case SUPERRES_QTHRESH:
4467 qthresh = (cpi->common.frame_type == KEY_FRAME ? oxcf->superres_kf_qthresh
4468 : oxcf->superres_qthresh);
4469 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
4470 q = av1_rc_pick_q_and_bounds(cpi, cpi->oxcf.width, cpi->oxcf.height,
4471 &bottom_index, &top_index);
4472 if (q < qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004473 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004474 } else {
Urvang Joshi28983f72017-10-25 14:41:06 -07004475 const uint8_t min_denom = SCALE_NUMERATOR + 1;
4476 const uint8_t denom_step = (MAXQ - qthresh + 1) >> 3;
4477 const uint8_t additional_denom = (q - qthresh) / denom_step;
4478 new_denom = AOMMIN(min_denom + additional_denom, SCALE_NUMERATOR << 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004479 }
4480 break;
4481 default: assert(0);
4482 }
Urvang Joshide71d142017-10-05 12:12:15 -07004483 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004484}
4485
Urvang Joshide71d142017-10-05 12:12:15 -07004486static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4487 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4488}
4489
4490static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
4491 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom) &&
Urvang Joshi3d8bcb22017-10-09 12:18:59 -07004492 (CONFIG_HORZONLY_FRAME_SUPERRES ||
Urvang Joshie58b5642017-10-05 17:59:43 -07004493 dimension_is_ok(oheight, rsz->resize_height, rsz->superres_denom));
Urvang Joshide71d142017-10-05 12:12:15 -07004494}
4495
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004496static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004497 SUPERRES_MODE superres_mode, int owidth,
4498 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004499 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004500 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004501 }
4502
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004503 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004504 int resize_denom =
4505 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4506 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4507
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004508 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004509 // Alter superres scale as needed to enforce conformity.
4510 rsz->superres_denom =
4511 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4512 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4513 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004514 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004515 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004516 // Alter resize scale as needed to enforce conformity.
4517 resize_denom =
4518 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004519 rsz->resize_width = owidth;
4520 rsz->resize_height = oheight;
4521 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004522 resize_denom);
4523 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4524 if (resize_denom > SCALE_NUMERATOR) {
4525 --resize_denom;
4526 rsz->resize_width = owidth;
4527 rsz->resize_height = oheight;
4528 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4529 resize_denom);
4530 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004531 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004532 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004533 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004534 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004535 if (resize_denom > rsz->superres_denom)
4536 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004537 else
Urvang Joshide71d142017-10-05 12:12:15 -07004538 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004539 rsz->resize_width = owidth;
4540 rsz->resize_height = oheight;
4541 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004542 resize_denom);
4543 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4544 (resize_denom > SCALE_NUMERATOR ||
4545 rsz->superres_denom > SCALE_NUMERATOR));
4546 } else { // We are allowed to alter neither resize scale nor superres scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004547 return 0;
4548 }
Urvang Joshide71d142017-10-05 12:12:15 -07004549 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004550}
4551#endif // CONFIG_FRAME_SUPERRES
4552
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004553// Calculates resize and superres params for next frame
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004554size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
4555 const AV1EncoderConfig *oxcf = &cpi->oxcf;
4556 size_params_type rsz = {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004557 oxcf->width,
4558 oxcf->height,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004559#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07004560 SCALE_NUMERATOR
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004561#endif // CONFIG_FRAME_SUPERRES
4562 };
Urvang Joshide71d142017-10-05 12:12:15 -07004563 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004564 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004565 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4566 rsz.resize_width = cpi->resize_pending_width;
4567 rsz.resize_height = cpi->resize_pending_height;
4568 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4569 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004570 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004571 rsz.resize_width = cpi->oxcf.width;
4572 rsz.resize_height = cpi->oxcf.height;
4573 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004574 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004575 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004576#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07004577 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004578 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4579 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004580 assert(0 && "Invalid scale parameters");
4581#endif // CONFIG_FRAME_SUPERRES
4582 return rsz;
4583}
4584
4585static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004586 int encode_width = rsz->resize_width;
4587 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004588
4589#if CONFIG_FRAME_SUPERRES
4590 AV1_COMMON *cm = &cpi->common;
4591 cm->superres_upscaled_width = encode_width;
4592 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004593 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004594 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4595 rsz->superres_denom);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004596#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004597 set_frame_size(cpi, encode_width, encode_height);
4598}
4599
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004600static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004601 size_params_type rsz = av1_calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004602 setup_frame_size_from_params(cpi, &rsz);
4603}
4604
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004605#if CONFIG_FRAME_SUPERRES
4606static void superres_post_encode(AV1_COMP *cpi) {
4607 AV1_COMMON *cm = &cpi->common;
4608
4609 if (av1_superres_unscaled(cm)) return;
4610
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004611 av1_superres_upscale(cm, NULL);
4612
4613 // If regular resizing is occurring the source will need to be downscaled to
4614 // match the upscaled superres resolution. Otherwise the original source is
4615 // used.
4616 if (av1_resize_unscaled(cm)) {
4617 cpi->source = cpi->unscaled_source;
4618 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4619 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004620 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4621 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004622 // Do downscale. cm->(width|height) has been updated by av1_superres_upscale
4623 if (aom_realloc_frame_buffer(
4624 &cpi->scaled_source, cm->superres_upscaled_width,
4625 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
4626#if CONFIG_HIGHBITDEPTH
4627 cm->use_highbitdepth,
4628#endif // CONFIG_HIGHBITDEPTH
4629 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
4630 aom_internal_error(
4631 &cm->error, AOM_CODEC_MEM_ERROR,
4632 "Failed to reallocate scaled source buffer for superres");
4633 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4634 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
4635#if CONFIG_HIGHBITDEPTH
4636 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
4637 (int)cm->bit_depth);
4638#else
4639 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source);
4640#endif // CONFIG_HIGHBITDEPTH
4641 cpi->source = &cpi->scaled_source;
4642 }
4643}
4644#endif // CONFIG_FRAME_SUPERRES
4645
4646static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4647 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4648 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004649 int no_loopfilter = 0;
Yaowu Xu35ee2342017-11-08 11:50:46 -08004650#if CONFIG_LOOP_RESTORATION
4651 int no_restoration = 0;
Hui Su27a4fb62017-11-10 16:03:50 -08004652#endif // CONFIG_LOOP_RESTORATION
4653
Yaowu Xu35ee2342017-11-08 11:50:46 -08004654 if (is_lossless_requested(&cpi->oxcf)
Hui Su27df8342017-11-07 15:16:05 -08004655#if CONFIG_INTRABC
4656 || (cm->allow_intrabc && NO_FILTER_FOR_IBC)
4657#endif // CONFIG_INTRABC
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004658#if CONFIG_EXT_TILE
Yaowu Xu35ee2342017-11-08 11:50:46 -08004659 || cm->single_tile_decoding
Hui Su27df8342017-11-07 15:16:05 -08004660#endif // CONFIG_EXT_TILE
Yaowu Xu35ee2342017-11-08 11:50:46 -08004661 ) {
4662 no_loopfilter = 1;
Hui Su27a4fb62017-11-10 16:03:50 -08004663#if CONFIG_LOOP_RESTORATION
Yaowu Xu35ee2342017-11-08 11:50:46 -08004664 no_restoration = 1;
Hui Su27a4fb62017-11-10 16:03:50 -08004665#endif // CONFIG_LOOP_RESTORATION
Yaowu Xu35ee2342017-11-08 11:50:46 -08004666 }
Steinar Midtskogene44c6222017-11-09 13:22:09 +01004667
Hui Su27a4fb62017-11-10 16:03:50 -08004668 int no_cdef = 0;
Steinar Midtskogene44c6222017-11-09 13:22:09 +01004669 if (is_lossless_requested(&cpi->oxcf) || !cpi->oxcf.using_cdef
Hui Su27a4fb62017-11-10 16:03:50 -08004670#if CONFIG_INTRABC
4671 || (cm->allow_intrabc && NO_FILTER_FOR_IBC)
4672#endif // CONFIG_INTRABC
Steinar Midtskogene44c6222017-11-09 13:22:09 +01004673#if CONFIG_EXT_TILE
4674 || cm->single_tile_decoding
4675#endif
4676 ) {
4677 no_cdef = 1;
4678 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004679
4680 if (no_loopfilter) {
Cheng Chen13fc8192017-08-19 11:49:28 -07004681#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004682 lf->filter_level[0] = 0;
4683 lf->filter_level[1] = 0;
4684#else
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004685 lf->filter_level = 0;
Cheng Chen179479f2017-08-04 10:56:39 -07004686#endif
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004687 } else {
4688 struct aom_usec_timer timer;
4689
4690 aom_clear_system_state();
4691
4692 aom_usec_timer_start(&timer);
4693
4694 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4695
4696 aom_usec_timer_mark(&timer);
4697 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4698 }
4699
Cheng Chenf572cd32017-08-25 18:34:51 -07004700#if !CONFIG_LPF_SB
Cheng Chen13fc8192017-08-19 11:49:28 -07004701#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004702 if (lf->filter_level[0] || lf->filter_level[1])
4703#else
4704 if (lf->filter_level > 0)
4705#endif
Cheng Chenf572cd32017-08-25 18:34:51 -07004706#endif // CONFIG_LPF_SB
Cheng Chen179479f2017-08-04 10:56:39 -07004707 {
Cheng Chenf572cd32017-08-25 18:34:51 -07004708#if CONFIG_LPF_SB
4709 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0, 0,
4710 0);
4711#else
Cheng Chen13fc8192017-08-19 11:49:28 -07004712#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004713 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level[0],
4714 lf->filter_level[1], 0, 0);
4715 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_u,
4716 lf->filter_level_u, 1, 0);
4717 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_v,
4718 lf->filter_level_v, 2, 0);
4719
Cheng Chene94df5c2017-07-19 17:25:33 -07004720#else
4721 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
Cheng Chen13fc8192017-08-19 11:49:28 -07004722#endif // CONFIG_LOOPFILTER_LEVEL
Cheng Chenf572cd32017-08-25 18:34:51 -07004723#endif // CONFIG_LPF_SB
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004724 }
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004725
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004726#if CONFIG_STRIPED_LOOP_RESTORATION
Rupert Swarbrick76c78002017-11-02 17:26:35 +00004727#if CONFIG_FRAME_SUPERRES && CONFIG_HORZONLY_FRAME_SUPERRES
Rupert Swarbrick8ce049e2017-11-06 16:47:47 +00004728 if (!av1_superres_unscaled(cm)) aom_extend_frame_borders(cm->frame_to_show);
Rupert Swarbrick76c78002017-11-02 17:26:35 +00004729#endif
Yaowu Xu35ee2342017-11-08 11:50:46 -08004730 if (!no_restoration)
4731 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004732#endif
4733
Yaowu Xu35ee2342017-11-08 11:50:46 -08004734 if (no_cdef) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004735 cm->cdef_bits = 0;
4736 cm->cdef_strengths[0] = 0;
4737 cm->nb_cdef_strengths = 1;
4738 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004739 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004740 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004741 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004742
4743 // Apply the filter
4744 av1_cdef_frame(cm->frame_to_show, cm, xd);
4745 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004746
4747#if CONFIG_FRAME_SUPERRES
4748 superres_post_encode(cpi);
4749#endif // CONFIG_FRAME_SUPERRES
4750
4751#if CONFIG_LOOP_RESTORATION
Yaowu Xu35ee2342017-11-08 11:50:46 -08004752 if (no_restoration) {
4753 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4754 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4755 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4756 } else {
Yaowu Xu07ec5e82017-11-09 15:58:33 -08004757#if CONFIG_STRIPED_LOOP_RESTORATION
Yaowu Xu35ee2342017-11-08 11:50:46 -08004758 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 1);
Yaowu Xu07ec5e82017-11-09 15:58:33 -08004759#endif
Yaowu Xu35ee2342017-11-08 11:50:46 -08004760 av1_pick_filter_restoration(cpi->source, cpi);
4761 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4762 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4763 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4764 av1_loop_restoration_filter_frame(cm->frame_to_show, cm, cm->rst_info, 7,
4765 NULL);
4766 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004767 }
4768#endif // CONFIG_LOOP_RESTORATION
4769 // TODO(debargha): Fix mv search range on encoder side
4770 // aom_extend_frame_inner_borders(cm->frame_to_show);
4771 aom_extend_frame_borders(cm->frame_to_show);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004772}
4773
Yaowu Xuf883b422016-08-30 14:01:10 -07004774static void encode_without_recode_loop(AV1_COMP *cpi) {
4775 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004776 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004777
Yaowu Xuf883b422016-08-30 14:01:10 -07004778 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004779
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004780 set_size_independent_vars(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004781
Fergus Simpsonbc189932017-05-16 17:02:39 -07004782 setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004783
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004784 assert(cm->width == cpi->scaled_source.y_crop_width);
4785 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004786
Yaowu Xuc27fc142016-08-22 16:08:15 -07004787 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4788
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004789 cpi->source =
4790 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4791 if (cpi->unscaled_last_source != NULL)
4792 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4793 &cpi->scaled_last_source);
Sebastien Alaiwan48795802017-10-30 12:07:13 +01004794#if CONFIG_HIGHBITDEPTH
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004795 cpi->source->buf_8bit_valid = 0;
4796#endif
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004797
4798 if (frame_is_intra_only(cm) == 0) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004799 scale_references(cpi);
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004800 }
4801
Yaowu Xuf883b422016-08-30 14:01:10 -07004802 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004803 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004804 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004805
Yaowu Xuc27fc142016-08-22 16:08:15 -07004806 // Variance adaptive and in frame q adjustment experiments are mutually
4807 // exclusive.
4808 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004809 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004810 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004811 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004812 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004813 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004814 }
4815 apply_active_map(cpi);
4816
4817 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004818 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819
4820 // Update some stats from cyclic refresh, and check if we should not update
4821 // golden reference, for 1 pass CBR.
4822 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004823 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4824 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004825
4826 // Update the skip mb flag probabilities based on the distribution
4827 // seen in the last encoder iteration.
4828 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004829 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004830}
4831
Yaowu Xuf883b422016-08-30 14:01:10 -07004832static void encode_with_recode_loop(AV1_COMP *cpi, size_t *size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004833 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004834 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004835 RATE_CONTROL *const rc = &cpi->rc;
4836 int bottom_index, top_index;
4837 int loop_count = 0;
4838 int loop_at_this_size = 0;
4839 int loop = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004840#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004841 int overshoot_seen = 0;
4842 int undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004843#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004844 int frame_over_shoot_limit;
4845 int frame_under_shoot_limit;
4846 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004847
4848 set_size_independent_vars(cpi);
4849
Sebastien Alaiwan48795802017-10-30 12:07:13 +01004850#if CONFIG_HIGHBITDEPTH
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004851 cpi->source->buf_8bit_valid = 0;
4852#endif
4853
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004854 aom_clear_system_state();
4855 setup_frame_size(cpi);
4856 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4857
Yaowu Xuc27fc142016-08-22 16:08:15 -07004858 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004859 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004860
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004861 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004862 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4863 set_mv_search_params(cpi);
4864
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004865#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004866 // Reset the loop state for new frame size.
4867 overshoot_seen = 0;
4868 undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004869#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004870
Yaowu Xuc27fc142016-08-22 16:08:15 -07004871 q_low = bottom_index;
4872 q_high = top_index;
4873
4874 loop_at_this_size = 0;
4875 }
4876
4877 // Decide frame size bounds first time through.
4878 if (loop_count == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004879 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4880 &frame_under_shoot_limit,
4881 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004882 }
4883
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004884 // if frame was scaled calculate global_motion_search again if already done
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004885 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4886 if (cpi->source->y_crop_width != cm->width ||
4887 cpi->source->y_crop_height != cm->height)
4888 cpi->global_motion_search_done = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004889 cpi->source =
4890 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004891 if (cpi->unscaled_last_source != NULL)
4892 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4893 &cpi->scaled_last_source);
4894
Yaowu Xuc27fc142016-08-22 16:08:15 -07004895 if (frame_is_intra_only(cm) == 0) {
4896 if (loop_count > 0) {
4897 release_scaled_references(cpi);
4898 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004899 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004900 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004901 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004902
4903 if (loop_count == 0) setup_frame(cpi);
4904
hui su0d103572017-03-01 17:58:01 -08004905#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004906 // Base q-index may have changed, so we need to assign proper default coef
4907 // probs before every iteration.
4908 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004909 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004910 av1_setup_frame_contexts(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004911 }
hui su0d103572017-03-01 17:58:01 -08004912#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004913
Yaowu Xuc27fc142016-08-22 16:08:15 -07004914 // Variance adaptive and in frame q adjustment experiments are mutually
4915 // exclusive.
4916 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004917 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004918 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004919 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004920 }
4921
4922 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004923 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004924 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004925
4926 // Update the skip mb flag probabilities based on the distribution
4927 // seen in the last encoder iteration.
4928 // update_base_skip_probs(cpi);
4929
Yaowu Xuf883b422016-08-30 14:01:10 -07004930 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004931
4932 // Dummy pack of the bitstream using up to date stats to get an
4933 // accurate estimate of output frame size to determine if we need
4934 // to recode.
4935 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004936 restore_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004937 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004938
4939 rc->projected_frame_size = (int)(*size) << 3;
4940 restore_coding_context(cpi);
4941
4942 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4943 }
4944
Yaowu Xuf883b422016-08-30 14:01:10 -07004945 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004946 loop = 0;
4947 } else {
4948 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4949 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4950 int last_q = q;
4951 int64_t kf_err;
4952
4953 int64_t high_err_target = cpi->ambient_err;
4954 int64_t low_err_target = cpi->ambient_err >> 1;
4955
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004956#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004957 if (cm->use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004958 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004959 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004960 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004961 }
4962#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07004963 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004964#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004965
4966 // Prevent possible divide by zero error below for perfect KF
4967 kf_err += !kf_err;
4968
4969 // The key frame is not good enough or we can afford
4970 // to make it better without undue risk of popping.
4971 if ((kf_err > high_err_target &&
4972 rc->projected_frame_size <= frame_over_shoot_limit) ||
4973 (kf_err > low_err_target &&
4974 rc->projected_frame_size <= frame_under_shoot_limit)) {
4975 // Lower q_high
4976 q_high = q > q_low ? q - 1 : q_low;
4977
4978 // Adjust Q
4979 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004980 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004981 } else if (kf_err < low_err_target &&
4982 rc->projected_frame_size >= frame_under_shoot_limit) {
4983 // The key frame is much better than the previous frame
4984 // Raise q_low
4985 q_low = q < q_high ? q + 1 : q_high;
4986
4987 // Adjust Q
4988 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004989 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004990 }
4991
4992 // Clamp Q to upper and lower limits:
4993 q = clamp(q, q_low, q_high);
4994
4995 loop = q != last_q;
4996 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4997 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004998 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004999 // Is the projected frame size out of range and are we allowed
5000 // to attempt to recode.
5001 int last_q = q;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005002#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005003 int retries = 0;
5004
Yaowu Xuc27fc142016-08-22 16:08:15 -07005005 // Frame size out of permitted range:
5006 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07005007 // Frame is too large
5008 if (rc->projected_frame_size > rc->this_frame_target) {
5009 // Special case if the projected size is > the max allowed.
5010 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
5011 q_high = rc->worst_quality;
5012
5013 // Raise Qlow as to at least the current value
5014 q_low = q < q_high ? q + 1 : q_high;
5015
5016 if (undershoot_seen || loop_at_this_size > 1) {
5017 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005018 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005019
5020 q = (q_high + q_low + 1) / 2;
5021 } else {
5022 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005023 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005024
Yaowu Xuf883b422016-08-30 14:01:10 -07005025 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005026 AOMMAX(q_high, top_index), cm->width,
5027 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005028
5029 while (q < q_low && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005030 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005031 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005032 AOMMAX(q_high, top_index), cm->width,
5033 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005034 retries++;
5035 }
5036 }
5037
5038 overshoot_seen = 1;
5039 } else {
5040 // Frame is too small
5041 q_high = q > q_low ? q - 1 : q_low;
5042
5043 if (overshoot_seen || loop_at_this_size > 1) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005044 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005045 q = (q_high + q_low) / 2;
5046 } else {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005047 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005048 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005049 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005050 // Special case reset for qlow for constrained quality.
5051 // This should only trigger where there is very substantial
5052 // undershoot on a frame and the auto cq level is above
5053 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07005054 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005055 q_low = q;
5056 }
5057
5058 while (q > q_high && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005059 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005060 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005061 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005062 retries++;
5063 }
5064 }
5065
5066 undershoot_seen = 1;
5067 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005068#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005069
5070 // Clamp Q to upper and lower limits:
5071 q = clamp(q, q_low, q_high);
5072
5073 loop = (q != last_q);
5074 } else {
5075 loop = 0;
5076 }
5077 }
5078
5079 // Special case for overlay frame.
5080 if (rc->is_src_frame_alt_ref &&
5081 rc->projected_frame_size < rc->max_frame_bandwidth)
5082 loop = 0;
5083
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005084 if (recode_loop_test_global_motion(cpi)) {
5085 loop = 1;
5086 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005087
Yaowu Xuc27fc142016-08-22 16:08:15 -07005088 if (loop) {
5089 ++loop_count;
5090 ++loop_at_this_size;
5091
5092#if CONFIG_INTERNAL_STATS
5093 ++cpi->tot_recode_hits;
5094#endif
5095 }
5096 } while (loop);
5097}
5098
Yaowu Xuf883b422016-08-30 14:01:10 -07005099static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005100 const int *const map = cpi->common.ref_frame_map;
5101
Zoe Liu368bf162017-11-03 20:10:19 -07005102 // No.1 Priority: LAST_FRAME
Yaowu Xuc27fc142016-08-22 16:08:15 -07005103 const int last2_is_last =
5104 map[cpi->lst_fb_idxes[1]] == map[cpi->lst_fb_idxes[0]];
5105 const int last3_is_last =
5106 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[0]];
5107 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[0]];
5108 const int bwd_is_last = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[0]];
Zoe Liu368bf162017-11-03 20:10:19 -07005109 const int alt2_is_last = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005110 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
5111
Zoe Liu368bf162017-11-03 20:10:19 -07005112 // No.2 Priority: ALTREF_FRAME
Yaowu Xuc27fc142016-08-22 16:08:15 -07005113 const int last2_is_alt = map[cpi->lst_fb_idxes[1]] == map[cpi->alt_fb_idx];
5114 const int last3_is_alt = map[cpi->lst_fb_idxes[2]] == map[cpi->alt_fb_idx];
5115 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
5116 const int bwd_is_alt = map[cpi->bwd_fb_idx] == map[cpi->alt_fb_idx];
Zoe Liue9b15e22017-07-19 15:53:01 -07005117 const int alt2_is_alt = map[cpi->alt2_fb_idx] == map[cpi->alt_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005118
Zoe Liu368bf162017-11-03 20:10:19 -07005119 // No.3 Priority: LAST2_FRAME
5120 const int last3_is_last2 =
5121 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
5122 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
5123 const int bwd_is_last2 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[1]];
5124 const int alt2_is_last2 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005125
Zoe Liu368bf162017-11-03 20:10:19 -07005126 // No.4 Priority: LAST3_FRAME
5127 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
5128 const int bwd_is_last3 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[2]];
5129 const int alt2_is_last3 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[2]];
5130
5131 // No.5 Priority: GOLDEN_FRAME
5132 const int bwd_is_gld = map[cpi->bwd_fb_idx] == map[cpi->gld_fb_idx];
5133 const int alt2_is_gld = map[cpi->alt2_fb_idx] == map[cpi->gld_fb_idx];
5134
5135 // No.6 Priority: BWDREF_FRAME
5136 const int alt2_is_bwd = map[cpi->alt2_fb_idx] == map[cpi->bwd_fb_idx];
5137
5138 // No.7 Priority: ALTREF2_FRAME
5139
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005140 // After av1_apply_encoding_flags() is called, cpi->ref_frame_flags might be
5141 // adjusted according to external encoder flags.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005142 int flags = cpi->ext_ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005143
Yaowu Xuf883b422016-08-30 14:01:10 -07005144 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005145
Yaowu Xuf883b422016-08-30 14:01:10 -07005146 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005147
Yaowu Xuf883b422016-08-30 14:01:10 -07005148 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005149
Zoe Liu368bf162017-11-03 20:10:19 -07005150 if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005151
Zoe Liu368bf162017-11-03 20:10:19 -07005152 if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
5153 flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005154
Zoe Liu368bf162017-11-03 20:10:19 -07005155 if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 ||
5156 bwd_is_gld) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07005157 (flags & AOM_BWD_FLAG))
5158 flags &= ~AOM_BWD_FLAG;
Zoe Liue9b15e22017-07-19 15:53:01 -07005159
Zoe Liu368bf162017-11-03 20:10:19 -07005160 if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
5161 alt2_is_gld || alt2_is_bwd) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07005162 (flags & AOM_ALT2_FLAG))
5163 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005164
5165 return flags;
5166}
5167
Yaowu Xuf883b422016-08-30 14:01:10 -07005168static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005169 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07005170 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07005171 // Note: The overrides are valid only for the next frame passed
5172 // to encode_frame_to_data_rate() function
5173 if (cpi->ext_refresh_frame_context_pending) {
5174 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
5175 cpi->ext_refresh_frame_context_pending = 0;
5176 }
5177 if (cpi->ext_refresh_frame_flags_pending) {
5178 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
5179 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
5180 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005181 cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
5182 cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005183 cpi->ext_refresh_frame_flags_pending = 0;
5184 }
5185}
5186
Zoe Liu17af2742017-10-06 10:36:42 -07005187#if !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuf883b422016-08-30 14:01:10 -07005188static void set_arf_sign_bias(AV1_COMP *cpi) {
5189 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005190 int arf_sign_bias;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005191 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liu3ac20932017-08-30 16:35:55 -07005192 // The arf_sign_bias will be one for internal ARFs'
Zoe Liue9b15e22017-07-19 15:53:01 -07005193 arf_sign_bias = cpi->rc.source_alt_ref_active &&
5194 (!cpi->refresh_alt_ref_frame ||
5195 gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE);
Zoe Liu6cfaff92016-10-18 17:12:11 -07005196
Yaowu Xuc27fc142016-08-22 16:08:15 -07005197 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005198 cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
Zoe Liue9b15e22017-07-19 15:53:01 -07005199 cm->ref_frame_sign_bias[ALTREF2_FRAME] =
5200 cm->ref_frame_sign_bias[ALTREF_FRAME];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005201}
Zoe Liu17af2742017-10-06 10:36:42 -07005202#endif // !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005203
Yaowu Xuf883b422016-08-30 14:01:10 -07005204static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07005205 InterpFilter ifilter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005206 int ref_total[TOTAL_REFS_PER_FRAME] = { 0 };
5207 MV_REFERENCE_FRAME ref;
5208 int mask = 0;
5209 int arf_idx = ALTREF_FRAME;
5210
Zoe Liue9b15e22017-07-19 15:53:01 -07005211 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
5212 cpi->refresh_alt2_ref_frame)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005213 return mask;
5214
Yaowu Xuc27fc142016-08-22 16:08:15 -07005215 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
5216 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
5217 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005218
5219 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
5220 if ((ref_total[LAST_FRAME] &&
5221 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005222 (ref_total[LAST2_FRAME] == 0 ||
5223 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
5224 ref_total[LAST2_FRAME]) &&
5225 (ref_total[LAST3_FRAME] == 0 ||
5226 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
5227 ref_total[LAST3_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005228 (ref_total[GOLDEN_FRAME] == 0 ||
5229 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
5230 ref_total[GOLDEN_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005231 (ref_total[BWDREF_FRAME] == 0 ||
5232 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
5233 ref_total[BWDREF_FRAME]) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07005234 (ref_total[ALTREF2_FRAME] == 0 ||
5235 cpi->interp_filter_selected[ALTREF2_FRAME][ifilter] * 50 <
5236 ref_total[ALTREF2_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005237 (ref_total[ALTREF_FRAME] == 0 ||
5238 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
5239 ref_total[ALTREF_FRAME]))
5240 mask |= 1 << ifilter;
5241 }
5242 return mask;
5243}
5244
5245#define DUMP_RECON_FRAMES 0
5246
5247#if DUMP_RECON_FRAMES == 1
5248// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005249static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5250 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005251 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005252
Zoe Liub4f31032017-11-03 23:48:35 -07005253 if (recon_buf == NULL) {
5254 printf("Frame %d is not ready.\n", cm->current_video_frame);
5255 return;
5256 }
5257
5258#if CONFIG_FRAME_MARKER
5259 static const int flag_list[TOTAL_REFS_PER_FRAME] = { 0,
5260 AOM_LAST_FLAG,
5261 AOM_LAST2_FLAG,
5262 AOM_LAST3_FLAG,
5263 AOM_GOLD_FLAG,
5264 AOM_BWD_FLAG,
5265 AOM_ALT2_FLAG,
5266 AOM_ALT_FLAG };
5267 printf(
5268 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5269 "show_existing_frame=%d) "
5270 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
5271 cm->current_video_frame, cm->frame_offset, cm->show_frame,
5272 cm->show_existing_frame);
5273 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
5274 const int buf_idx = cm->frame_refs[ref_frame - LAST_FRAME].idx;
5275 const int ref_offset =
5276 (buf_idx >= 0)
5277 ? (int)cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset
5278 : -1;
Zoe Liuf452fdf2017-11-02 23:08:12 -07005279 printf(
5280 " %d(%c-%d-%4.2f)", ref_offset,
5281 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N',
5282 (buf_idx >= 0) ? (int)cpi->frame_rf_level[buf_idx] : -1,
5283 (buf_idx >= 0) ? rate_factor_deltas[cpi->frame_rf_level[buf_idx]] : -1);
Zoe Liub4f31032017-11-03 23:48:35 -07005284 }
5285 printf(" ]\n");
5286#endif // CONFIG_FRAME_MARKER
5287
5288 if (!cm->show_frame) {
5289 printf("Frame %d is a no show frame, so no image dump.\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07005290 cm->current_video_frame);
5291 return;
5292 }
5293
Zoe Liub4f31032017-11-03 23:48:35 -07005294 int h;
5295 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5296 FILE *f_recon = NULL;
5297
Yaowu Xuc27fc142016-08-22 16:08:15 -07005298 if (cm->current_video_frame == 0) {
5299 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5300 printf("Unable to open file %s to write.\n", file_name);
5301 return;
5302 }
5303 } else {
5304 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5305 printf("Unable to open file %s to append.\n", file_name);
5306 return;
5307 }
5308 }
Zoe Liuf40a9572017-10-13 12:37:19 -07005309#if CONFIG_FRAME_MARKER
Yaowu Xuc27fc142016-08-22 16:08:15 -07005310 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07005311 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
5312 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
5313 "refresh_alt_ref_frame=%d, rf_level=%d, "
5314 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07005315 cm->current_video_frame, cpi->twopass.gf_group.index,
5316 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liuf40a9572017-10-13 12:37:19 -07005317 cm->frame_offset, cm->show_frame, cm->show_existing_frame,
5318 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
5319 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
5320 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
5321#else
5322 printf(
5323 "\nFrame=%5d, encode_update_type[%5d]=%1d, "
5324 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
5325 "refresh_alt_ref_frame=%d, rf_level=%d, "
5326 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
5327 cm->current_video_frame, cpi->twopass.gf_group.index,
5328 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
5329 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
Zoe Liufcf5fa22017-06-26 16:00:38 -07005330 cpi->refresh_alt_ref_frame,
5331 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
5332 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liuf40a9572017-10-13 12:37:19 -07005333#endif // CONFIG_FRAME_MARKER
Zoe Liue9b15e22017-07-19 15:53:01 -07005334#if 0
5335 int ref_frame;
5336 printf("get_ref_frame_map_idx: [");
5337 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
5338 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
5339 printf(" ]\n");
5340 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
5341 printf("cm->ref_frame_map = [");
5342 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
5343 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
5344 }
5345 printf(" ]\n");
5346#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005347
5348 // --- Y ---
5349 for (h = 0; h < cm->height; ++h) {
5350 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5351 f_recon);
5352 }
5353 // --- U ---
5354 for (h = 0; h < (cm->height >> 1); ++h) {
5355 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5356 f_recon);
5357 }
5358 // --- V ---
5359 for (h = 0; h < (cm->height >> 1); ++h) {
5360 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5361 f_recon);
5362 }
5363
5364 fclose(f_recon);
5365}
5366#endif // DUMP_RECON_FRAMES
5367
Thomas Davies4822e142017-10-10 11:30:36 +01005368static void make_update_tile_list_enc(AV1_COMP *cpi, const int start_tile,
5369 const int num_tiles,
Thomas Davies028b57f2017-02-22 16:42:11 +00005370 FRAME_CONTEXT *ec_ctxs[]) {
5371 int i;
Thomas Davies4822e142017-10-10 11:30:36 +01005372 for (i = start_tile; i < start_tile + num_tiles; ++i)
5373 ec_ctxs[i - start_tile] = &cpi->tile_data[i].tctx;
Thomas Davies028b57f2017-02-22 16:42:11 +00005374}
5375
Yaowu Xuf883b422016-08-30 14:01:10 -07005376static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005377 uint8_t *dest, int skip_adapt,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005378 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005379 AV1_COMMON *const cm = &cpi->common;
5380 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005381 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005382#if CONFIG_SIMPLE_BWD_ADAPT
5383 const int num_bwd_ctxs = 1;
5384#else
5385 const int num_bwd_ctxs = cm->tile_rows * cm->tile_cols;
5386#endif
5387
5388 FRAME_CONTEXT **tile_ctxs =
5389 aom_malloc(num_bwd_ctxs * sizeof(&cpi->tile_data[0].tctx));
5390 aom_cdf_prob **cdf_ptrs = aom_malloc(
5391 num_bwd_ctxs * sizeof(&cpi->tile_data[0].tctx.partition_cdf[0][0]));
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005392#if CONFIG_XIPHRC
5393 int frame_type;
5394 int drop_this_frame = 0;
Zoe Liue04abf72017-04-19 15:37:11 -07005395#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005396 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07005397 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005398
Zoe Liu17af2742017-10-06 10:36:42 -07005399#if !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005400 // Set the arf sign bias for this frame.
5401 set_arf_sign_bias(cpi);
Zoe Liu17af2742017-10-06 10:36:42 -07005402#endif // !CONFIG_FRAME_SIGN_BIAS
5403
Fangwen Fu8d164de2016-12-14 13:40:54 -08005404#if CONFIG_TEMPMV_SIGNALING
5405 // frame type has been decided outside of this function call
5406 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
Jingning Hane17ebe92017-11-03 15:25:42 -07005407 cm->use_ref_frame_mvs =
Fangwen Fu8d164de2016-12-14 13:40:54 -08005408 !cpi->oxcf.disable_tempmv && !cm->cur_frame->intra_only;
Jingning Hane17ebe92017-11-03 15:25:42 -07005409 cm->use_prev_frame_mvs = cm->use_ref_frame_mvs;
Fangwen Fu8d164de2016-12-14 13:40:54 -08005410#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005411
Jingning Hand8a15a62017-10-30 10:53:42 -07005412 // Reset the frame packet stamp index.
5413 if (cm->frame_type == KEY_FRAME) cm->current_video_frame = 0;
5414
Yaowu Xuc27fc142016-08-22 16:08:15 -07005415 // NOTE:
5416 // (1) Move the setup of the ref_frame_flags upfront as it would be
5417 // determined by the current frame properties;
5418 // (2) The setup of the ref_frame_flags applies to both show_existing_frame's
5419 // and the other cases.
5420 if (cm->current_video_frame > 0)
5421 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5422
5423 if (cm->show_existing_frame) {
5424 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
5425 // BWDREF_FRAME in the reference frame buffer.
5426 cm->frame_type = INTER_FRAME;
5427 cm->show_frame = 1;
5428 cpi->frame_flags = *frame_flags;
5429
5430 // In the case of show_existing frame, we will not send fresh flag
5431 // to decoder. Any change in the reference frame buffer can be done by
5432 // switching the virtual indices.
5433
5434 cpi->refresh_last_frame = 0;
5435 cpi->refresh_golden_frame = 0;
5436 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005437 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005438 cpi->refresh_alt_ref_frame = 0;
5439
5440 cpi->rc.is_bwd_ref_frame = 0;
5441 cpi->rc.is_last_bipred_frame = 0;
5442 cpi->rc.is_bipred_frame = 0;
5443
Jingning Han8f661602017-08-19 08:16:50 -07005444 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07005445
Yaowu Xuc27fc142016-08-22 16:08:15 -07005446 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07005447 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005448
5449 // Set up frame to show to get ready for stats collection.
5450 cm->frame_to_show = get_frame_new_buffer(cm);
5451
Zoe Liub4f31032017-11-03 23:48:35 -07005452#if CONFIG_FRAME_MARKER
5453 // Update current frame offset.
5454 cm->frame_offset =
5455 cm->buffer_pool->frame_bufs[cm->new_fb_idx].cur_frame_offset;
5456#endif // CONFIG_FRAME_MARKER
5457
Yaowu Xuc27fc142016-08-22 16:08:15 -07005458#if DUMP_RECON_FRAMES == 1
5459 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5460 dump_filtered_recon_frames(cpi);
5461#endif // DUMP_RECON_FRAMES
5462
5463 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07005464 // NOTE:
5465 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
5466 // update has been done previously when handling the LAST_BIPRED_FRAME
5467 // right before BWDREF_FRAME (in the display order);
5468 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
5469 // update will be done when the following is called, which will exchange
5470 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
5471 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2, and
5472 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Cheng Chen46f30c72017-09-07 11:13:33 -07005473 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005474
5475 // Update frame flags
5476 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5477 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
5478 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5479
5480 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5481
5482 // Update the frame type
5483 cm->last_frame_type = cm->frame_type;
5484
Yaowu Xuc27fc142016-08-22 16:08:15 -07005485 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5486 // to do post-encoding update accordingly.
5487 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005488 av1_set_target_rate(cpi, cm->width, cm->height);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005489#if CONFIG_XIPHRC
Zoe Liue04abf72017-04-19 15:37:11 -07005490 frame_type = cm->frame_type == INTER_FRAME ? OD_P_FRAME : OD_I_FRAME;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005491 drop_this_frame = od_enc_rc_update_state(
5492 &cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5493 cpi->refresh_alt_ref_frame, frame_type, cpi->droppable);
5494#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005495 av1_rc_postencode_update(cpi, *size);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005496#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005497 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005498
Yaowu Xuc27fc142016-08-22 16:08:15 -07005499 ++cm->current_video_frame;
5500
Jingning Hanf6214b92017-04-12 11:43:37 -07005501 aom_free(tile_ctxs);
5502 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005503 return;
5504 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005505
5506 // Set default state for segment based loop filter update flags.
5507 cm->lf.mode_ref_delta_update = 0;
5508
5509 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5510 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
5511
5512 // Set various flags etc to special state if it is a key frame.
5513 if (frame_is_intra_only(cm)) {
5514 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005515 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005516
5517 // If segmentation is enabled force a map update for key frames.
5518 if (seg->enabled) {
5519 seg->update_map = 1;
5520 seg->update_data = 1;
5521 }
5522
5523 // The alternate reference frame cannot be active for a key frame.
5524 cpi->rc.source_alt_ref_active = 0;
5525
5526 cm->error_resilient_mode = oxcf->error_resilient_mode;
5527
Thomas Daedea6a854b2017-06-22 17:49:11 -07005528#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005529 // By default, encoder assumes decoder can use prev_mi.
5530 if (cm->error_resilient_mode) {
5531 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
5532 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
5533 } else if (cm->intra_only) {
5534 // Only reset the current context.
5535 cm->reset_frame_context = RESET_FRAME_CONTEXT_CURRENT;
5536 }
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005537#if CONFIG_EXT_TILE
5538 if (cpi->oxcf.large_scale_tile)
5539 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
5540#endif // CONFIG_EXT_TILE
5541#endif // !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005542 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005543 if (cpi->oxcf.mtu == 0) {
5544 cm->num_tg = cpi->oxcf.num_tile_groups;
5545 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005546 // Use a default value for the purposes of weighting costs in probability
5547 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005548 cm->num_tg = DEFAULT_MAX_NUM_TG;
5549 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005550
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005551#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07005552 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5553 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08005554#if CONFIG_REFERENCE_BUFFER
5555 if (cm->large_scale_tile) cm->seq_params.frame_id_numbers_present_flag = 0;
5556#endif // CONFIG_REFERENCE_BUFFER
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005557#endif // CONFIG_EXT_TILE
5558
Rupert Swarbricke0b15992017-11-09 15:04:32 +00005559#if CONFIG_MONO_VIDEO
Rupert Swarbrickdcb3cff2017-11-09 15:58:33 +00005560 cm->seq_params.monochrome = oxcf->monochrome;
Rupert Swarbricke0b15992017-11-09 15:04:32 +00005561#endif // CONFIG_MONO_VIDEO
5562
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005563#if CONFIG_XIPHRC
5564 if (drop_this_frame) {
5565 av1_rc_postencode_update_drop_frame(cpi);
5566 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005567 aom_free(tile_ctxs);
5568 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005569 return;
5570 }
5571#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07005572 // For 1 pass CBR, check if we are dropping this frame.
5573 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005574 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005575 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005576 if (av1_rc_drop_frame(cpi)) {
5577 av1_rc_postencode_update_drop_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005578 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005579 aom_free(tile_ctxs);
5580 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005581 return;
5582 }
5583 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005584#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005585
Yaowu Xuf883b422016-08-30 14:01:10 -07005586 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005587
5588#if CONFIG_INTERNAL_STATS
5589 memset(cpi->mode_chosen_counts, 0,
5590 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5591#endif
5592
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005593#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01005594 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005595 /* Non-normative definition of current_frame_id ("frame counter" with
5596 * wraparound) */
Sebastien Alaiwand418f682017-10-19 15:06:52 +02005597 const int frame_id_length = FRAME_ID_LENGTH;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005598 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005599 int lsb, msb;
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005600/* quasi-random initialization of current_frame_id for a key frame */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005601#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07005602 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5603 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5604 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005605 } else {
5606#endif
Alex Conversef77fd0b2017-04-20 11:00:24 -07005607 lsb = cpi->source->y_buffer[0] & 0xff;
5608 msb = cpi->source->y_buffer[1] & 0xff;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005609#if CONFIG_HIGHBITDEPTH
David Barker49a76562016-12-07 14:50:21 +00005610 }
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005611#endif
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005612 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005613 } else {
5614 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005615 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
5616 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005617 }
5618 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005619#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005620
Fangwen Fu6160df22017-04-24 09:45:51 -07005621#if CONFIG_EXT_DELTA_Q
5622 cm->delta_q_present_flag = cpi->oxcf.deltaq_mode != NO_DELTA_Q;
Fangwen Fu231fe422017-04-24 17:52:29 -07005623 cm->delta_lf_present_flag = cpi->oxcf.deltaq_mode == DELTA_Q_LF;
Cheng Chen880166a2017-10-02 17:48:48 -07005624#if CONFIG_LOOPFILTER_LEVEL
5625 cm->delta_lf_multi = DEFAULT_DELTA_LF_MULTI;
5626#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu6160df22017-04-24 09:45:51 -07005627#endif
5628
Yaowu Xuc27fc142016-08-22 16:08:15 -07005629 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
5630 encode_without_recode_loop(cpi);
5631 } else {
5632 encode_with_recode_loop(cpi, size, dest);
5633 }
5634
Yi Luo10e23002017-07-31 11:54:43 -07005635 cm->last_tile_cols = cm->tile_cols;
5636 cm->last_tile_rows = cm->tile_rows;
5637
Yaowu Xuc27fc142016-08-22 16:08:15 -07005638#ifdef OUTPUT_YUV_SKINMAP
5639 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005640 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005641 }
5642#endif // OUTPUT_YUV_SKINMAP
5643
5644 // Special case code to reduce pulsing when key frames are forced at a
5645 // fixed interval. Note the reconstruction error if it is the frame before
5646 // the force key frame
5647 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005648#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005649 if (cm->use_highbitdepth) {
5650 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07005651 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005652 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005653 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005654 }
5655#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07005656 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005657#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005658 }
5659
5660 // If the encoder forced a KEY_FRAME decision
5661 if (cm->frame_type == KEY_FRAME) {
5662 cpi->refresh_last_frame = 1;
5663 }
5664
5665 cm->frame_to_show = get_frame_new_buffer(cm);
5666 cm->frame_to_show->color_space = cm->color_space;
anorkin76fb1262017-03-22 15:12:12 -07005667#if CONFIG_COLORSPACE_HEADERS
5668 cm->frame_to_show->transfer_function = cm->transfer_function;
5669 cm->frame_to_show->chroma_sample_position = cm->chroma_sample_position;
5670#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005671 cm->frame_to_show->color_range = cm->color_range;
5672 cm->frame_to_show->render_width = cm->render_width;
5673 cm->frame_to_show->render_height = cm->render_height;
5674
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005675 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5676 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005677
5678 // Pick the loop filter level for the frame.
5679 loopfilter_frame(cpi, cm);
5680
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005681#ifdef OUTPUT_YUV_REC
5682 aom_write_one_yuv_frame(cm, cm->frame_to_show);
5683#endif
5684
Yaowu Xuc27fc142016-08-22 16:08:15 -07005685 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07005686 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005687
Jingning Hanf6214b92017-04-12 11:43:37 -07005688 if (skip_adapt) {
Jingning Hanf6214b92017-04-12 11:43:37 -07005689 aom_free(tile_ctxs);
5690 aom_free(cdf_ptrs);
Jingning Hanf6214b92017-04-12 11:43:37 -07005691 return;
5692 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005693
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005694#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01005695 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005696 int i;
5697 /* Update reference frame id values based on the value of refresh_mask */
5698 for (i = 0; i < REF_FRAMES; i++) {
5699 if ((cm->refresh_mask >> i) & 1) {
5700 cm->ref_frame_id[i] = cm->current_frame_id;
5701 }
5702 }
5703 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005704#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005705
Yaowu Xuc27fc142016-08-22 16:08:15 -07005706#if DUMP_RECON_FRAMES == 1
5707 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005708 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005709#endif // DUMP_RECON_FRAMES
5710
Soo-Chul Han934af352017-10-15 15:21:51 -04005711#if CONFIG_SEGMENT_PRED_LAST
5712 if (cm->seg.enabled) {
5713 if (cm->seg.update_map) {
5714 update_reference_segmentation_map(cpi);
5715 } else {
5716 memcpy(cm->current_frame_seg_map, cm->last_frame_seg_map,
5717 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5718 }
5719 }
5720#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07005721 if (cm->seg.update_map) update_reference_segmentation_map(cpi);
Soo-Chul Han934af352017-10-15 15:21:51 -04005722#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005723
5724 if (frame_is_intra_only(cm) == 0) {
5725 release_scaled_references(cpi);
5726 }
5727
Cheng Chen46f30c72017-09-07 11:13:33 -07005728 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005729
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005730#if CONFIG_ENTROPY_STATS
5731 av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
Zoe Liua56f9162017-06-21 22:49:57 -07005732 assert(cm->frame_context_idx < FRAME_CONTEXTS);
5733 av1_accumulate_frame_counts(&aggregate_fc_per_type[cm->frame_context_idx],
5734 &cm->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005735#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005736 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005737 av1_adapt_intra_frame_probs(cm);
Thomas Davies4822e142017-10-10 11:30:36 +01005738#if CONFIG_SIMPLE_BWD_ADAPT
5739 make_update_tile_list_enc(cpi, cm->largest_tile_id, 1, tile_ctxs);
5740#else
5741 make_update_tile_list_enc(cpi, 0, num_bwd_ctxs, tile_ctxs);
5742#endif
Thomas Davies493623e2017-03-31 16:12:25 +01005743 av1_average_tile_coef_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01005744 num_bwd_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01005745 av1_average_tile_intra_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01005746 num_bwd_ctxs);
Debargha Mukherjee43061b32017-10-13 16:50:17 -07005747 av1_average_tile_loopfilter_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
5748 num_bwd_ctxs);
hui suff0da2b2017-03-07 15:51:37 -08005749#if CONFIG_ADAPT_SCAN
5750 av1_adapt_scan_order(cm);
5751#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005752 }
5753
5754 if (!frame_is_intra_only(cm)) {
5755 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005756 av1_adapt_inter_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00005757 av1_average_tile_inter_cdfs(&cpi->common, cpi->common.fc, tile_ctxs,
Thomas Davies4822e142017-10-10 11:30:36 +01005758 cdf_ptrs, num_bwd_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01005759 av1_average_tile_mv_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01005760 num_bwd_ctxs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005761 }
5762 }
5763
5764 if (cpi->refresh_golden_frame == 1)
5765 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5766 else
5767 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5768
5769 if (cpi->refresh_alt_ref_frame == 1)
5770 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5771 else
5772 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5773
Yaowu Xuc27fc142016-08-22 16:08:15 -07005774 if (cpi->refresh_bwd_ref_frame == 1)
5775 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5776 else
5777 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005778
Yaowu Xuc27fc142016-08-22 16:08:15 -07005779 cm->last_frame_type = cm->frame_type;
5780
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005781#if CONFIG_XIPHRC
5782 frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
5783
5784 drop_this_frame =
5785 od_enc_rc_update_state(&cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5786 cpi->refresh_alt_ref_frame, frame_type, 0);
5787 if (drop_this_frame) {
5788 av1_rc_postencode_update_drop_frame(cpi);
5789 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005790 aom_free(tile_ctxs);
5791 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005792 return;
5793 }
Zoe Liue04abf72017-04-19 15:37:11 -07005794#else // !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005795 av1_rc_postencode_update(cpi, *size);
Zoe Liue04abf72017-04-19 15:37:11 -07005796#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005797
5798#if 0
5799 output_frame_level_debug_stats(cpi);
5800#endif
5801
5802 if (cm->frame_type == KEY_FRAME) {
5803 // Tell the caller that the frame was coded as a key frame
5804 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5805 } else {
5806 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5807 }
5808
5809 // Clear the one shot update flags for segmentation map and mode/ref loop
5810 // filter deltas.
5811 cm->seg.update_map = 0;
5812 cm->seg.update_data = 0;
5813 cm->lf.mode_ref_delta_update = 0;
5814
Yaowu Xuc27fc142016-08-22 16:08:15 -07005815 if (cm->show_frame) {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005816 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that are
5817 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005818 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005819 // Don't increment frame counters if this was an altref buffer
5820 // update not a real frame
5821 ++cm->current_video_frame;
5822 }
5823
Yaowu Xuc27fc142016-08-22 16:08:15 -07005824 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005825 if (cm->is_reference_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005826 cm->prev_frame = cm->cur_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005827 // keep track of the last coded dimensions
5828 cm->last_width = cm->width;
5829 cm->last_height = cm->height;
5830
5831 // reset to normal state now that we are done.
5832 cm->last_show_frame = cm->show_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005833 }
Yi Luo10e23002017-07-31 11:54:43 -07005834
Thomas Davies493623e2017-03-31 16:12:25 +01005835 aom_free(tile_ctxs);
5836 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005837}
5838
Yaowu Xuf883b422016-08-30 14:01:10 -07005839static void Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005840 int skip_adapt, unsigned int *frame_flags) {
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005841#if CONFIG_XIPHRC
5842 int64_t ip_count;
5843 int frame_type, is_golden, is_altref;
5844
5845 /* Not updated during init so update it here */
5846 if (cpi->oxcf.rc_mode == AOM_Q) cpi->od_rc.quality = cpi->oxcf.cq_level;
5847
5848 frame_type = od_frame_type(&cpi->od_rc, cpi->od_rc.cur_frame, &is_golden,
5849 &is_altref, &ip_count);
5850
5851 if (frame_type == OD_I_FRAME) {
5852 frame_type = KEY_FRAME;
5853 cpi->frame_flags &= FRAMEFLAGS_KEY;
5854 } else if (frame_type == OD_P_FRAME) {
5855 frame_type = INTER_FRAME;
5856 }
5857
5858 if (is_altref) {
5859 cpi->refresh_alt_ref_frame = 1;
5860 cpi->rc.source_alt_ref_active = 1;
5861 }
5862
5863 cpi->refresh_golden_frame = is_golden;
5864 cpi->common.frame_type = frame_type;
5865 if (is_golden) cpi->frame_flags &= FRAMEFLAGS_GOLDEN;
5866#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005867 if (cpi->oxcf.rc_mode == AOM_CBR) {
5868 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005869 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005870 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005871 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005872#endif
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005873 encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005874}
5875
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005876#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005877static void Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005878 unsigned int *frame_flags) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005879 encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005880
Yaowu Xuc27fc142016-08-22 16:08:15 -07005881 // Do not do post-encoding update for those frames that do not have a spot in
5882 // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
5883 // even when show_existing_frame is used.
5884 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005885 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005886 }
5887 check_show_existing_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005888}
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005889#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005890
James Zern3e2613b2017-03-30 23:14:40 -07005891int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005892 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5893 int64_t end_time) {
5894 AV1_COMMON *const cm = &cpi->common;
5895 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005896 int res = 0;
5897 const int subsampling_x = sd->subsampling_x;
5898 const int subsampling_y = sd->subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005899#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005900 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5901#endif
5902
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005903#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005904 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5905#else
5906 check_initial_width(cpi, subsampling_x, subsampling_y);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005907#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005908
Yaowu Xuf883b422016-08-30 14:01:10 -07005909 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005910
Yaowu Xuf883b422016-08-30 14:01:10 -07005911 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005912#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005913 use_highbitdepth,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005914#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005915 frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005916 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005917 aom_usec_timer_mark(&timer);
5918 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005919
5920 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
5921 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005922 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005923 "Non-4:2:0 color format requires profile 1 or 3");
5924 res = -1;
5925 }
5926 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
5927 (subsampling_x == 1 && subsampling_y == 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005928 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005929 "4:2:0 color format requires profile 0 or 2");
5930 res = -1;
5931 }
5932
5933 return res;
5934}
5935
Yaowu Xuf883b422016-08-30 14:01:10 -07005936static int frame_is_reference(const AV1_COMP *cpi) {
5937 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005938
5939 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005940 cpi->refresh_golden_frame || cpi->refresh_bwd_ref_frame ||
5941 cpi->refresh_alt2_ref_frame || cpi->refresh_alt_ref_frame ||
5942 !cm->error_resilient_mode || cm->lf.mode_ref_delta_update ||
5943 cm->seg.update_map || cm->seg.update_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005944}
5945
Yaowu Xuf883b422016-08-30 14:01:10 -07005946static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005947 const struct lookahead_entry *source) {
5948 int64_t this_duration;
5949 int step = 0;
5950
5951 if (source->ts_start == cpi->first_time_stamp_ever) {
5952 this_duration = source->ts_end - source->ts_start;
5953 step = 1;
5954 } else {
5955 int64_t last_duration =
5956 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5957
5958 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5959
5960 // do a step update if the duration changes by 10%
5961 if (last_duration)
5962 step = (int)((this_duration - last_duration) * 10 / last_duration);
5963 }
5964
5965 if (this_duration) {
5966 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005967 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005968 } else {
5969 // Average this frame's rate into the last second's average
5970 // frame rate. If we haven't seen 1 second yet, then average
5971 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005972 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005973 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5974 double avg_duration = 10000000.0 / cpi->framerate;
5975 avg_duration *= (interval - avg_duration + this_duration);
5976 avg_duration /= interval;
5977
Yaowu Xuf883b422016-08-30 14:01:10 -07005978 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005979 }
5980 }
5981 cpi->last_time_stamp_seen = source->ts_start;
5982 cpi->last_end_time_stamp_seen = source->ts_end;
5983}
5984
5985// Returns 0 if this is not an alt ref else the offset of the source frame
5986// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005987static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005988 RATE_CONTROL *const rc = &cpi->rc;
5989 int arf_src_index = 0;
5990 if (is_altref_enabled(cpi)) {
5991 if (cpi->oxcf.pass == 2) {
5992 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5993 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5994 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5995 }
5996 } else if (rc->source_alt_ref_pending) {
5997 arf_src_index = rc->frames_till_gf_update_due;
5998 }
5999 }
6000 return arf_src_index;
6001}
6002
Yaowu Xuf883b422016-08-30 14:01:10 -07006003static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006004 int brf_src_index = 0;
6005 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
6006
6007 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
6008 // flag.
6009 if (gf_group->bidir_pred_enabled[gf_group->index]) {
6010 if (cpi->oxcf.pass == 2) {
6011 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
6012 brf_src_index = gf_group->brf_src_offset[gf_group->index];
6013 } else {
6014 // TODO(zoeliu): To re-visit the setup for this scenario
6015 brf_src_index = cpi->rc.bipred_group_interval - 1;
6016 }
6017 }
6018
6019 return brf_src_index;
6020}
Zoe Liue9b15e22017-07-19 15:53:01 -07006021
Zoe Liue9b15e22017-07-19 15:53:01 -07006022// Returns 0 if this is not an alt ref else the offset of the source frame
6023// used as the arf midpoint.
6024static int get_arf2_src_index(AV1_COMP *cpi) {
6025 int arf2_src_index = 0;
6026 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
6027 if (cpi->oxcf.pass == 2) {
6028 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
6029 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
6030 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
6031 }
6032 }
6033 }
6034 return arf2_src_index;
6035}
Yaowu Xuc27fc142016-08-22 16:08:15 -07006036
Yaowu Xuf883b422016-08-30 14:01:10 -07006037static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006038 const struct lookahead_entry *source) {
6039 RATE_CONTROL *const rc = &cpi->rc;
6040
6041 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07006042 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07006043
6044 if (cpi->oxcf.pass == 2) {
6045 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
6046 rc->is_src_frame_alt_ref =
Yaowu Xuc27fc142016-08-22 16:08:15 -07006047 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07006048 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07006049 rc->is_src_frame_ext_arf =
6050 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006051 } else {
6052 rc->is_src_frame_alt_ref =
6053 cpi->alt_ref_source && (source == cpi->alt_ref_source);
6054 }
6055
6056 if (rc->is_src_frame_alt_ref) {
6057 // Current frame is an ARF overlay frame.
6058 cpi->alt_ref_source = NULL;
6059
Zoe Liue9b15e22017-07-19 15:53:01 -07006060 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
6061 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
6062 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
6063 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
6064 cpi->refresh_last_frame = 1;
6065 } else {
Zoe Liue9b15e22017-07-19 15:53:01 -07006066 // Don't refresh the last buffer for an ARF overlay frame. It will
6067 // become the GF so preserve last as an alternative prediction option.
6068 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006069 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006070 }
6071}
6072
6073#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006074extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6075 const unsigned char *img2, int img2_pitch,
6076 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006077
6078static void adjust_image_stat(double y, double u, double v, double all,
6079 ImageStat *s) {
6080 s->stat[Y] += y;
6081 s->stat[U] += u;
6082 s->stat[V] += v;
6083 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006084 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006085}
6086
Angie Chiang08a22a62017-07-17 17:29:17 -07006087static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006088 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006089 double samples = 0.0;
6090 uint32_t in_bit_depth = 8;
6091 uint32_t bit_depth = 8;
6092
Angie Chiang08a22a62017-07-17 17:29:17 -07006093#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07006094 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006095#endif
6096 cpi->bytes += frame_bytes;
6097
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006098#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006099 if (cm->use_highbitdepth) {
6100 in_bit_depth = cpi->oxcf.input_bit_depth;
6101 bit_depth = cm->bit_depth;
6102 }
6103#endif
6104 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006105 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006106 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
6107 double y, u, v, frame_all;
6108
6109 cpi->count++;
6110 if (cpi->b_calculate_psnr) {
6111 PSNR_STATS psnr;
6112 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006113 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006114// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006115#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07006116 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006117#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006118 aom_calc_psnr(orig, recon, &psnr);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006119#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006120
6121 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6122 &cpi->psnr);
6123 cpi->total_sq_error += psnr.sse[0];
6124 cpi->total_samples += psnr.samples[0];
6125 samples = psnr.samples[0];
6126// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006127#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006128 if (cm->use_highbitdepth)
6129 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006130 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006131 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006132 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006133#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006134 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006135#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006136
Yaowu Xuf883b422016-08-30 14:01:10 -07006137 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006138 cpi->summed_quality += frame_ssim2 * weight;
6139 cpi->summed_weights += weight;
6140
6141#if 0
6142 {
6143 FILE *f = fopen("q_used.stt", "a");
6144 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
6145 cpi->common.current_video_frame, y2, u2, v2,
6146 frame_psnr2, frame_ssim2);
6147 fclose(f);
6148 }
6149#endif
6150 }
6151 if (cpi->b_calculate_blockiness) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006152#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006153 if (!cm->use_highbitdepth)
6154#endif
6155 {
6156 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006157 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6158 recon->y_stride, orig->y_width, orig->y_height);
6159 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006160 cpi->total_blockiness += frame_blockiness;
6161 }
6162
6163 if (cpi->b_calculate_consistency) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006164#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006165 if (!cm->use_highbitdepth)
6166#endif
6167 {
Yaowu Xuf883b422016-08-30 14:01:10 -07006168 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006169 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6170 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6171
6172 const double peak = (double)((1 << in_bit_depth) - 1);
6173 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006174 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006175 if (consistency > 0.0)
6176 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006177 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006178 cpi->total_inconsistency += this_inconsistency;
6179 }
6180 }
6181 }
6182
6183 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006184 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006185 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006186 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006187 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6188 }
6189}
6190#endif // CONFIG_INTERNAL_STATS
6191
RogerZhou3b635242017-09-19 10:06:46 -07006192#if CONFIG_AMVR
6193static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
6194 const YV12_BUFFER_CONFIG *last_picture,
6195 hash_table *last_hash_table) {
6196 aom_clear_system_state();
6197 // check use hash ME
6198 int k;
6199 uint32_t hash_value_1;
6200 uint32_t hash_value_2;
6201
6202 const int block_size = 8;
6203 const double threshold_current = 0.8;
6204 const double threshold_average = 0.95;
6205 const int max_history_size = 32;
6206 int T = 0; // total block
6207 int C = 0; // match with collocated block
6208 int S = 0; // smooth region but not match with collocated block
6209 int M = 0; // match with other block
6210
6211 const int pic_width = cur_picture->y_width;
6212 const int pic_height = cur_picture->y_height;
6213 for (int i = 0; i + block_size <= pic_height; i += block_size) {
6214 for (int j = 0; j + block_size <= pic_width; j += block_size) {
6215 const int x_pos = j;
6216 const int y_pos = i;
6217 int match = 1;
6218 T++;
6219
6220 // check whether collocated block match with current
6221 uint8_t *p_cur = cur_picture->y_buffer;
6222 uint8_t *p_ref = last_picture->y_buffer;
6223 int stride_cur = cur_picture->y_stride;
6224 int stride_ref = last_picture->y_stride;
6225 p_cur += (y_pos * stride_cur + x_pos);
6226 p_ref += (y_pos * stride_ref + x_pos);
6227
6228 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6229 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6230 if (p_cur[tmpX] != p_ref[tmpX]) {
6231 match = 0;
6232 }
6233 }
6234 p_cur += stride_cur;
6235 p_ref += stride_ref;
6236 }
6237
6238 if (match) {
6239 C++;
6240 continue;
6241 }
6242
6243 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
6244 y_pos) ||
6245 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
6246 S++;
6247 continue;
6248 }
6249
6250 av1_get_block_hash_value(
6251 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
6252 block_size, &hash_value_1, &hash_value_2);
6253
6254 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
6255 M++;
6256 }
6257 }
6258 }
6259
6260 assert(T > 0);
6261 double csm_rate = ((double)(C + S + M)) / ((double)(T));
6262 double m_rate = ((double)(M)) / ((double)(T));
6263
6264 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
6265 cpi->m_rate_array[cpi->rate_index] = m_rate;
6266
6267 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
6268 cpi->rate_size++;
6269 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
6270
6271 if (csm_rate < threshold_current) {
6272 return 0;
6273 }
6274
6275 if (C == T) {
6276 return 1;
6277 }
6278
6279 double csm_average = 0.0;
6280 double m_average = 0.0;
6281
6282 for (k = 0; k < cpi->rate_size; k++) {
6283 csm_average += cpi->csm_rate_array[k];
6284 m_average += cpi->m_rate_array[k];
6285 }
6286 csm_average /= cpi->rate_size;
6287 m_average /= cpi->rate_size;
6288
6289 if (csm_average < threshold_average) {
6290 return 0;
6291 }
6292
6293 if (M > (T - C - S) / 3) {
6294 return 1;
6295 }
6296
6297 if (csm_rate > 0.99 && m_rate > 0.01) {
6298 return 1;
6299 }
6300
6301 if (csm_average + m_average > 1.01) {
6302 return 1;
6303 }
6304
6305 return 0;
6306}
6307#endif
6308
Yaowu Xuf883b422016-08-30 14:01:10 -07006309int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6310 size_t *size, uint8_t *dest, int64_t *time_stamp,
6311 int64_t *time_end, int flush) {
6312 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6313 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006314 BufferPool *const pool = cm->buffer_pool;
6315 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07006316 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006317 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
6318 struct lookahead_entry *last_source = NULL;
6319 struct lookahead_entry *source = NULL;
6320 int arf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006321 int brf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006322 int i;
6323
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006324#if CONFIG_XIPHRC
6325 cpi->od_rc.end_of_input = flush;
6326#endif
6327
Yaowu Xuc27fc142016-08-22 16:08:15 -07006328#if CONFIG_BITSTREAM_DEBUG
6329 assert(cpi->oxcf.max_threads == 0 &&
6330 "bitstream debug tool does not support multithreading");
6331 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07006332 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006333#endif
6334
Yaowu Xuf883b422016-08-30 14:01:10 -07006335 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006336
RogerZhou3b635242017-09-19 10:06:46 -07006337#if CONFIG_AMVR
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07006338#if CONFIG_EIGHTH_PEL_MV_ONLY
6339 set_high_precision_mv(cpi, 1, 0);
6340#else
RogerZhou3b635242017-09-19 10:06:46 -07006341 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07006342#endif // CONFIG_EIGHTH_PEL_MV_ONLY
6343#else
6344#if CONFIG_EIGHTH_PEL_MV_ONLY
6345 set_high_precision_mv(cpi, 1);
RogerZhou3b635242017-09-19 10:06:46 -07006346#else
Cheng Chen46f30c72017-09-07 11:13:33 -07006347 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07006348#endif // CONFIG_EIGHTH_PEL_MV_ONLY
RogerZhou3b635242017-09-19 10:06:46 -07006349#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006350
6351 // Is multi-arf enabled.
6352 // Note that at the moment multi_arf is only configured for 2 pass VBR
6353 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
6354 cpi->multi_arf_allowed = 1;
6355 else
6356 cpi->multi_arf_allowed = 0;
6357
Thomas Daedea6a854b2017-06-22 17:49:11 -07006358// Normal defaults
6359#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07006360 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07006361#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006362 cm->refresh_frame_context =
6363 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
6364 ? REFRESH_FRAME_CONTEXT_FORWARD
6365 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006366#if CONFIG_EXT_TILE
6367 if (oxcf->large_scale_tile)
6368 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
6369#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006370
6371 cpi->refresh_last_frame = 1;
6372 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006373 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006374 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006375 cpi->refresh_alt_ref_frame = 0;
6376
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02006377#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07006378 if (oxcf->pass == 2 && cm->show_existing_frame) {
6379 // Manage the source buffer and flush out the source frame that has been
6380 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07006381 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006382 *size = 0;
6383 return -1;
6384 }
Alex Conversef77fd0b2017-04-20 11:00:24 -07006385 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006386 // TODO(zoeliu): To track down to determine whether it's needed to adjust
6387 // the frame rate.
6388 *time_stamp = source->ts_start;
6389 *time_end = source->ts_end;
6390
6391 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006392 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006393
6394 // Find a free buffer for the new frame, releasing the reference previously
6395 // held.
6396 if (cm->new_fb_idx != INVALID_IDX) {
6397 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6398 }
6399 cm->new_fb_idx = get_free_fb(cm);
6400
6401 if (cm->new_fb_idx == INVALID_IDX) return -1;
6402
6403 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006404 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006405
6406 // Start with a 0 size frame.
6407 *size = 0;
6408
6409 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006410 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006411
6412 Pass2Encode(cpi, size, dest, frame_flags);
6413
6414 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
6415
6416#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07006417 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006418#endif // CONFIG_INTERNAL_STATS
6419
6420 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006421 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006422
6423 cm->show_existing_frame = 0;
6424 return 0;
6425 }
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02006426#endif // !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07006427
6428 // Should we encode an arf frame.
6429 arf_src_index = get_arf_src_index(cpi);
6430 if (arf_src_index) {
6431 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006432 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006433 // Avoid creating an alt-ref if there's a forced keyframe pending.
6434 if (e == NULL) {
6435 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07006436 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006437 arf_src_index = 0;
6438 flush = 1;
6439 break;
6440 }
6441 }
6442 }
6443
6444 if (arf_src_index) {
6445 assert(arf_src_index <= rc->frames_to_key);
6446
Yaowu Xuf883b422016-08-30 14:01:10 -07006447 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006448 cpi->alt_ref_source = source;
6449
6450 if (oxcf->arnr_max_frames > 0) {
Todd Nguyen302d0972017-06-16 16:16:29 -07006451// Produce the filtered ARF frame.
6452#if CONFIG_BGSPRITE
6453 int bgsprite_ret = av1_background_sprite(cpi, arf_src_index);
Todd Nguyen2fc23092017-07-11 12:00:36 -07006454 // Do temporal filter if bgsprite not generated.
6455 if (bgsprite_ret != 0)
Todd Nguyen302d0972017-06-16 16:16:29 -07006456#endif // CONFIG_BGSPRITE
Todd Nguyen8493f912017-07-20 12:45:12 -07006457 av1_temporal_filter(cpi,
6458#if CONFIG_BGSPRITE
Todd Nguyen1fd99c22017-07-31 17:50:41 -07006459 NULL, &cpi->alt_ref_buffer,
Todd Nguyen8493f912017-07-20 12:45:12 -07006460#endif // CONFIG_BGSPRITE
6461 arf_src_index);
Yaowu Xuf883b422016-08-30 14:01:10 -07006462 aom_extend_frame_borders(&cpi->alt_ref_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006463 force_src_buffer = &cpi->alt_ref_buffer;
6464 }
6465
6466 cm->show_frame = 0;
6467 cm->intra_only = 0;
6468 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006469 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006470 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006471 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006472 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006473 rc->is_src_frame_alt_ref = 0;
6474 }
6475 rc->source_alt_ref_pending = 0;
6476 }
6477
Zoe Liue9b15e22017-07-19 15:53:01 -07006478 // Should we encode an arf2 frame.
6479 arf_src_index = get_arf2_src_index(cpi);
6480 if (arf_src_index) {
6481 for (i = 0; i <= arf_src_index; ++i) {
6482 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
6483 // Avoid creating an alt-ref if there's a forced keyframe pending.
6484 if (e == NULL) {
6485 break;
6486 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
6487 arf_src_index = 0;
6488 flush = 1;
6489 break;
6490 }
6491 }
6492 }
6493
6494 if (arf_src_index) {
6495 assert(arf_src_index <= rc->frames_to_key);
6496
6497 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
6498 cpi->alt_ref_source = source;
6499
6500 if (oxcf->arnr_max_frames > 0) {
6501 // Produce the filtered ARF frame.
Zoe Liu3ac20932017-08-30 16:35:55 -07006502 av1_temporal_filter(cpi,
6503#if CONFIG_BGSPRITE
6504 NULL, NULL,
6505#endif // CONFIG_BGSPRITE
6506 arf_src_index);
Zoe Liue9b15e22017-07-19 15:53:01 -07006507 aom_extend_frame_borders(&cpi->alt_ref_buffer);
6508 force_src_buffer = &cpi->alt_ref_buffer;
6509 }
6510
6511 cm->show_frame = 0;
6512 cm->intra_only = 0;
6513 cpi->refresh_alt2_ref_frame = 1;
6514 cpi->refresh_last_frame = 0;
6515 cpi->refresh_golden_frame = 0;
6516 cpi->refresh_bwd_ref_frame = 0;
6517 cpi->refresh_alt_ref_frame = 0;
6518 rc->is_src_frame_alt_ref = 0;
6519 rc->is_src_frame_ext_arf = 0;
6520 }
6521 rc->source_alt_ref_pending = 0;
6522 }
Zoe Liue9b15e22017-07-19 15:53:01 -07006523
Yaowu Xuc27fc142016-08-22 16:08:15 -07006524 rc->is_bwd_ref_frame = 0;
6525 brf_src_index = get_brf_src_index(cpi);
6526 if (brf_src_index) {
6527 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07006528 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006529 cm->show_frame = 0;
6530 cm->intra_only = 0;
6531
6532 cpi->refresh_bwd_ref_frame = 1;
6533 cpi->refresh_last_frame = 0;
6534 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006535 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006536 cpi->refresh_alt_ref_frame = 0;
6537
6538 rc->is_bwd_ref_frame = 1;
6539 }
6540 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006541
6542 if (!source) {
6543 // Get last frame source.
6544 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006545 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006546 return -1;
6547 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006548 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006549 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07006550 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006551
6552 if (source != NULL) {
6553 cm->show_frame = 1;
6554 cm->intra_only = 0;
6555
6556 // Check to see if the frame should be encoded as an arf overlay.
6557 check_src_altref(cpi, source);
6558 }
6559 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006560 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006561 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07006562 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006563 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
6564
6565 *time_stamp = source->ts_start;
6566 *time_end = source->ts_end;
Yaowu Xuf883b422016-08-30 14:01:10 -07006567 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006568
6569 } else {
6570 *size = 0;
6571 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006572#if CONFIG_XIPHRC
6573 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 1);
6574#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006575 av1_end_first_pass(cpi); /* get last stats packet */
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006576#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006577 cpi->twopass.first_pass_done = 1;
6578 }
6579 return -1;
6580 }
6581
6582 if (source->ts_start < cpi->first_time_stamp_ever) {
6583 cpi->first_time_stamp_ever = source->ts_start;
6584 cpi->last_end_time_stamp_seen = source->ts_start;
6585 }
6586
6587 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006588 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006589
6590 // adjust frame rates based on timestamps given
6591 if (cm->show_frame) adjust_frame_rate(cpi, source);
6592
6593 // Find a free buffer for the new frame, releasing the reference previously
6594 // held.
6595 if (cm->new_fb_idx != INVALID_IDX) {
6596 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6597 }
6598 cm->new_fb_idx = get_free_fb(cm);
6599
6600 if (cm->new_fb_idx == INVALID_IDX) return -1;
6601
Zoe Liuf452fdf2017-11-02 23:08:12 -07006602#if CONFIG_FRAME_MARKER
6603 // Retain the RF_LEVEL for the current newly coded frame.
6604 cpi->frame_rf_level[cm->new_fb_idx] =
6605 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
6606#endif // CONFIG_FRAME_MARKER
6607
Yaowu Xuc27fc142016-08-22 16:08:15 -07006608 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Sebastien Alaiwan48795802017-10-30 12:07:13 +01006609#if CONFIG_HIGHBITDEPTH
Yaowu Xu9b0f7032017-07-31 11:01:19 -07006610 cm->cur_frame->buf.buf_8bit_valid = 0;
6611#endif
Zoe Liu6cfaff92016-10-18 17:12:11 -07006612
Yaowu Xuc27fc142016-08-22 16:08:15 -07006613 // Start with a 0 size frame.
6614 *size = 0;
6615
6616 cpi->frame_flags = *frame_flags;
6617
6618 if (oxcf->pass == 2) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006619#if CONFIG_XIPHRC
6620 if (od_enc_rc_2pass_in(&cpi->od_rc) < 0) return -1;
6621 }
6622#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006623 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006624 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07006625 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006626 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006627#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006628
6629 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
6630 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i)
6631 cpi->scaled_ref_idx[i] = INVALID_IDX;
6632 }
6633
6634#if CONFIG_AOM_QM
6635 cm->using_qmatrix = cpi->oxcf.using_qm;
6636 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
6637 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
6638#endif
6639
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006640#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01006641 if (cm->seq_params.frame_id_numbers_present_flag) {
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006642 if (*time_stamp == 0) {
6643 cpi->common.current_frame_id = -1;
6644 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006645 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006646#endif // CONFIG_REFERENCE_BUFFER
RogerZhou3b635242017-09-19 10:06:46 -07006647#if CONFIG_AMVR
6648 cpi->cur_poc++;
6649 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
RogerZhou10a03802017-10-26 11:49:48 -07006650 if (cpi->common.seq_force_integer_mv == 2) {
RogerZhou3b635242017-09-19 10:06:46 -07006651 struct lookahead_entry *previous_entry =
6652 cpi->lookahead->buf + cpi->previsous_index;
RogerZhou10a03802017-10-26 11:49:48 -07006653 cpi->common.cur_frame_force_integer_mv = is_integer_mv(
RogerZhou3b635242017-09-19 10:06:46 -07006654 cpi, cpi->source, &previous_entry->img, cpi->previsou_hash_table);
6655 } else {
RogerZhou10a03802017-10-26 11:49:48 -07006656 cpi->common.cur_frame_force_integer_mv = cpi->common.seq_force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07006657 }
6658 } else {
RogerZhou10a03802017-10-26 11:49:48 -07006659 cpi->common.cur_frame_force_integer_mv = 0;
RogerZhou3b635242017-09-19 10:06:46 -07006660 }
6661#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006662
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006663#if CONFIG_XIPHRC
6664 if (oxcf->pass == 1) {
6665 size_t tmp;
6666 if (cpi->od_rc.cur_frame == 0) Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6667 cpi->od_rc.firstpass_quant = cpi->od_rc.target_quantizer;
6668 Pass0Encode(cpi, &tmp, dest, 0, frame_flags);
6669 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 0);
6670 } else if (oxcf->pass == 2) {
6671 Pass0Encode(cpi, size, dest, 0, frame_flags);
6672 } else {
6673 if (cpi->od_rc.cur_frame == 0) {
6674 size_t tmp;
6675 Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6676 }
6677 Pass0Encode(cpi, size, dest, 0, frame_flags);
6678 }
6679#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006680 if (oxcf->pass == 1) {
6681 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07006682 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006683 } else if (oxcf->pass == 2) {
6684 Pass2Encode(cpi, size, dest, frame_flags);
6685 } else {
6686 // One pass encode
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006687 Pass0Encode(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006688 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006689#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07006690#if CONFIG_HASH_ME
6691 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
RogerZhou3b635242017-09-19 10:06:46 -07006692#if CONFIG_AMVR
6693 cpi->previsou_hash_table = &cm->cur_frame->hash_table;
6694 {
6695 int l;
6696 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
6697 if ((cpi->lookahead->buf + l) == source) {
6698 cpi->previsous_index = l;
6699 break;
6700 }
6701 }
6702
6703 if (l == cpi->lookahead->max_sz) {
6704 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6705 "Failed to find last frame original buffer");
6706 }
6707 }
6708#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07006709 }
6710
6711#endif
6712
Yunqing Wang267e3272017-11-09 14:23:22 -08006713#if CONFIG_EXT_TILE
6714 if (!cm->large_scale_tile) {
6715#endif // CONFIG_EXT_TILE
Thomas Daededa4d8b92017-06-05 15:44:14 -07006716#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yunqing Wang267e3272017-11-09 14:23:22 -08006717 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006718#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006719 if (!cm->error_resilient_mode)
6720 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006721#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yunqing Wang267e3272017-11-09 14:23:22 -08006722#if CONFIG_EXT_TILE
6723 }
6724#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006725
Yunqing Wangb041d8a2017-11-15 12:31:18 -08006726#if CONFIG_EXT_TILE
6727#define EXT_TILE_DEBUG 0
6728#if EXT_TILE_DEBUG
6729 if (cm->large_scale_tile && oxcf->pass == 2) {
6730 char fn[20] = "./fc";
6731 fn[4] = cm->current_video_frame / 100 + '0';
6732 fn[5] = (cm->current_video_frame % 100) / 10 + '0';
6733 fn[6] = (cm->current_video_frame % 10) + '0';
6734 fn[7] = '\0';
6735 av1_print_frame_contexts(cm->fc, fn);
6736 }
6737#endif // EXT_TILE_DEBUG
6738#undef EXT_TILE_DEBUG
6739#endif // CONFIG_EXT_TILE
6740
Yaowu Xuc27fc142016-08-22 16:08:15 -07006741 // No frame encoded, or frame was dropped, release scaled references.
6742 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6743 release_scaled_references(cpi);
6744 }
6745
6746 if (*size > 0) {
6747 cpi->droppable = !frame_is_reference(cpi);
6748 }
6749
Yaowu Xuf883b422016-08-30 14:01:10 -07006750 aom_usec_timer_mark(&cmptimer);
6751 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006752
6753 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6754 generate_psnr_packet(cpi);
6755
6756#if CONFIG_INTERNAL_STATS
6757 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006758 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006759 }
6760#endif // CONFIG_INTERNAL_STATS
6761
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006762#if CONFIG_XIPHRC
6763 cpi->od_rc.cur_frame++;
6764#endif
6765
Yaowu Xuf883b422016-08-30 14:01:10 -07006766 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006767
6768 return 0;
6769}
6770
Yaowu Xuf883b422016-08-30 14:01:10 -07006771int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6772 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006773 if (!cm->show_frame) {
6774 return -1;
6775 } else {
6776 int ret;
6777 if (cm->frame_to_show) {
6778 *dest = *cm->frame_to_show;
6779 dest->y_width = cm->width;
6780 dest->y_height = cm->height;
6781 dest->uv_width = cm->width >> cm->subsampling_x;
6782 dest->uv_height = cm->height >> cm->subsampling_y;
6783 ret = 0;
6784 } else {
6785 ret = -1;
6786 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006787 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006788 return ret;
6789 }
6790}
6791
Yaowu Xuf883b422016-08-30 14:01:10 -07006792int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006793 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6794
6795 *frame =
6796 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6797 return 0;
6798}
6799
Yaowu Xuf883b422016-08-30 14:01:10 -07006800int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6801 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006802 int hr = 0, hs = 0, vr = 0, vs = 0;
6803
6804 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6805
6806 Scale2Ratio(horiz_mode, &hr, &hs);
6807 Scale2Ratio(vert_mode, &vr, &vs);
6808
6809 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006810 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6811 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006812
6813 return 0;
6814}
6815
Yaowu Xuf883b422016-08-30 14:01:10 -07006816int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006817
Yaowu Xuf883b422016-08-30 14:01:10 -07006818void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006819 // TODO(yunqingwang): For what references to use, external encoding flags
6820 // should be consistent with internal reference frame selection. Need to
6821 // ensure that there is not conflict between the two. In AV1 encoder, the
6822 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
6823 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
6824 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006825 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006826 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006827 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6828 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6829 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006830 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006831 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006832 } else {
6833 int ref = AOM_REFFRAME_ALL;
6834
6835 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6836 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6837
6838 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6839
6840 if (flags & AOM_EFLAG_NO_REF_ARF) {
6841 ref ^= AOM_ALT_FLAG;
6842 ref ^= AOM_BWD_FLAG;
6843 ref ^= AOM_ALT2_FLAG;
6844 } else {
6845 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6846 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
6847 }
6848
6849 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006850 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006851 }
6852
6853 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006854 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006855 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006856
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006857 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6858 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006859
Yaowu Xuf883b422016-08-30 14:01:10 -07006860 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006861
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006862 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6863 upd ^= AOM_ALT_FLAG;
6864 upd ^= AOM_BWD_FLAG;
6865 upd ^= AOM_ALT2_FLAG;
6866 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006867
Yaowu Xuf883b422016-08-30 14:01:10 -07006868 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006869 }
6870
Yaowu Xuf883b422016-08-30 14:01:10 -07006871 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6872 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006873 }
6874}