blob: 1990e7073ba3fd879be88894d1e77a0fe81a2ea6 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
13#include <math.h>
14#include <stdio.h>
15
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "./aom_config.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
18#include "av1/common/alloccommon.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050019#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010020#include "av1/common/cdef.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050021#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -070022#include "av1/common/filter.h"
23#include "av1/common/idct.h"
24#include "av1/common/reconinter.h"
25#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070026#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/common/tile_common.h"
28
29#include "av1/encoder/aq_complexity.h"
30#include "av1/encoder/aq_cyclicrefresh.h"
31#include "av1/encoder/aq_variance.h"
32#include "av1/encoder/bitstream.h"
Todd Nguyen302d0972017-06-16 16:16:29 -070033#if CONFIG_BGSPRITE
34#include "av1/encoder/bgsprite.h"
35#endif // CONFIG_BGSPRITE
Yaowu Xuc27fc142016-08-22 16:08:15 -070036#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -070037#include "aom_dsp/buf_ans.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070038#endif
39#include "av1/encoder/context_tree.h"
40#include "av1/encoder/encodeframe.h"
41#include "av1/encoder/encodemv.h"
42#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070043#if CONFIG_LV_MAP
44#include "av1/encoder/encodetxb.h"
45#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070046#include "av1/encoder/ethread.h"
47#include "av1/encoder/firstpass.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070048#if CONFIG_HASH_ME
49#include "av1/encoder/hash_motion.h"
50#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070051#include "av1/encoder/mbgraph.h"
52#include "av1/encoder/picklpf.h"
53#if CONFIG_LOOP_RESTORATION
54#include "av1/encoder/pickrst.h"
55#endif // CONFIG_LOOP_RESTORATION
Debargha Mukherjee7166f222017-09-05 21:32:42 -070056#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070057#include "av1/encoder/ratectrl.h"
58#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070059#include "av1/encoder/segmentation.h"
60#include "av1/encoder/speed_features.h"
61#include "av1/encoder/temporal_filter.h"
62
Yaowu Xuf883b422016-08-30 14:01:10 -070063#include "./av1_rtcd.h"
64#include "./aom_dsp_rtcd.h"
65#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070066#include "aom_dsp/psnr.h"
67#if CONFIG_INTERNAL_STATS
68#include "aom_dsp/ssim.h"
69#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070070#include "aom_dsp/aom_dsp_common.h"
71#include "aom_dsp/aom_filter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070072#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070073#include "aom_ports/mem.h"
74#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070075#include "aom_scale/aom_scale.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070076#if CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070077#include "aom_util/debug_util.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070078#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070079
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080080#if CONFIG_ENTROPY_STATS
81FRAME_COUNTS aggregate_fc;
Zoe Liua56f9162017-06-21 22:49:57 -070082// Aggregate frame counts per frame context type
83FRAME_COUNTS aggregate_fc_per_type[FRAME_CONTEXTS];
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080084#endif // CONFIG_ENTROPY_STATS
85
Yaowu Xuc27fc142016-08-22 16:08:15 -070086#define AM_SEGMENT_ID_INACTIVE 7
87#define AM_SEGMENT_ID_ACTIVE 0
88
89#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
90
91#define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
92 // for altref computation.
93#define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
94 // mv. Choose a very high value for
95 // now so that HIGH_PRECISION is always
96 // chosen.
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070097
Yaowu Xuc27fc142016-08-22 16:08:15 -070098// #define OUTPUT_YUV_REC
99#ifdef OUTPUT_YUV_DENOISED
100FILE *yuv_denoised_file = NULL;
101#endif
102#ifdef OUTPUT_YUV_SKINMAP
103FILE *yuv_skinmap_file = NULL;
104#endif
105#ifdef OUTPUT_YUV_REC
106FILE *yuv_rec_file;
107#define FILE_NAME_LEN 100
108#endif
109
110#if 0
111FILE *framepsnr;
112FILE *kf_list;
113FILE *keyfile;
114#endif
115
Luc Trudeauf8164152017-04-11 16:20:51 -0400116#if CONFIG_CFL
117CFL_CTX NULL_CFL;
118#endif
119
Urvang Joshib5ed3502016-10-17 16:38:05 -0700120#if CONFIG_INTERNAL_STATS
121typedef enum { Y, U, V, ALL } STAT_TYPE;
122#endif // CONFIG_INTERNAL_STATS
123
Yaowu Xuf883b422016-08-30 14:01:10 -0700124static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 switch (mode) {
126 case NORMAL:
127 *hr = 1;
128 *hs = 1;
129 break;
130 case FOURFIVE:
131 *hr = 4;
132 *hs = 5;
133 break;
134 case THREEFIVE:
135 *hr = 3;
136 *hs = 5;
137 break;
138 case ONETWO:
139 *hr = 1;
140 *hs = 2;
141 break;
142 default:
143 *hr = 1;
144 *hs = 1;
145 assert(0);
146 break;
147 }
148}
149
150// Mark all inactive blocks as active. Other segmentation features may be set
151// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700152static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700153 unsigned char *const seg_map = cpi->segmentation_map;
154 int i;
155 if (cpi->active_map.enabled || cpi->active_map.update)
156 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
157 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
158 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
159}
160
Yaowu Xuf883b422016-08-30 14:01:10 -0700161static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700162 struct segmentation *const seg = &cpi->common.seg;
163 unsigned char *const seg_map = cpi->segmentation_map;
164 const unsigned char *const active_map = cpi->active_map.map;
165 int i;
166
167 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
168
169 if (frame_is_intra_only(&cpi->common)) {
170 cpi->active_map.enabled = 0;
171 cpi->active_map.update = 1;
172 }
173
174 if (cpi->active_map.update) {
175 if (cpi->active_map.enabled) {
176 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
177 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700178 av1_enable_segmentation(seg);
179 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700180#if CONFIG_LOOPFILTER_LEVEL
181 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
182 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
183 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
184 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
185
186 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
187 -MAX_LOOP_FILTER);
188 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
189 -MAX_LOOP_FILTER);
190 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
191 -MAX_LOOP_FILTER);
192 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
193 -MAX_LOOP_FILTER);
194#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700195 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700196 // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
197 // filter level being zero regardless of the value of seg->abs_delta.
Yaowu Xuf883b422016-08-30 14:01:10 -0700198 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
199 -MAX_LOOP_FILTER);
Cheng Chend8184da2017-09-26 18:15:22 -0700200#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700201 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700202 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700203#if CONFIG_LOOPFILTER_LEVEL
204 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
205 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
206 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
207 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
208#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700209 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Cheng Chend8184da2017-09-26 18:15:22 -0700210#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211 if (seg->enabled) {
212 seg->update_data = 1;
213 seg->update_map = 1;
214 }
215 }
216 cpi->active_map.update = 0;
217 }
218}
219
Yaowu Xuf883b422016-08-30 14:01:10 -0700220int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
221 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700222 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
223 unsigned char *const active_map_8x8 = cpi->active_map.map;
224 const int mi_rows = cpi->common.mi_rows;
225 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700226 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
227 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700228 cpi->active_map.update = 1;
229 if (new_map_16x16) {
230 int r, c;
231 for (r = 0; r < mi_rows; ++r) {
232 for (c = 0; c < mi_cols; ++c) {
233 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700234 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235 ? AM_SEGMENT_ID_ACTIVE
236 : AM_SEGMENT_ID_INACTIVE;
237 }
238 }
239 cpi->active_map.enabled = 1;
240 } else {
241 cpi->active_map.enabled = 0;
242 }
243 return 0;
244 } else {
245 return -1;
246 }
247}
248
Yaowu Xuf883b422016-08-30 14:01:10 -0700249int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
250 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700251 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
252 new_map_16x16) {
253 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
254 const int mi_rows = cpi->common.mi_rows;
255 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700256 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
257 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
258
Yaowu Xuc27fc142016-08-22 16:08:15 -0700259 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
260 if (cpi->active_map.enabled) {
261 int r, c;
262 for (r = 0; r < mi_rows; ++r) {
263 for (c = 0; c < mi_cols; ++c) {
264 // Cyclic refresh segments are considered active despite not having
265 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700266 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700267 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
268 }
269 }
270 }
271 return 0;
272 } else {
273 return -1;
274 }
275}
276
RogerZhou3b635242017-09-19 10:06:46 -0700277static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv
278#if CONFIG_AMVR
279 ,
RogerZhou10a03802017-10-26 11:49:48 -0700280 int cur_frame_force_integer_mv
RogerZhou3b635242017-09-19 10:06:46 -0700281#endif
282 ) {
James Zern01a9d702017-08-25 19:09:33 +0000283 MACROBLOCK *const mb = &cpi->td.mb;
284 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
285
RogerZhou3b635242017-09-19 10:06:46 -0700286#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -0700287 if (cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0) {
RogerZhou3b635242017-09-19 10:06:46 -0700288#else
James Zern01a9d702017-08-25 19:09:33 +0000289 if (cpi->common.allow_high_precision_mv) {
RogerZhou3b635242017-09-19 10:06:46 -0700290#endif
James Zern01a9d702017-08-25 19:09:33 +0000291 int i;
292 for (i = 0; i < NMV_CONTEXTS; ++i) {
293 mb->mv_cost_stack[i] = mb->nmvcost_hp[i];
294 }
295 } else {
296 int i;
297 for (i = 0; i < NMV_CONTEXTS; ++i) {
298 mb->mv_cost_stack[i] = mb->nmvcost[i];
299 }
300 }
301}
302
Yaowu Xuf883b422016-08-30 14:01:10 -0700303static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700305 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306 return BLOCK_64X64;
307
Yaowu Xuf883b422016-08-30 14:01:10 -0700308 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309 return BLOCK_128X128;
310
Yaowu Xuf883b422016-08-30 14:01:10 -0700311 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700312
313 assert(IMPLIES(cpi->common.tile_cols > 1,
314 cpi->common.tile_width % MAX_MIB_SIZE == 0));
315 assert(IMPLIES(cpi->common.tile_rows > 1,
316 cpi->common.tile_height % MAX_MIB_SIZE == 0));
317
318 // TODO(any): Possibly could improve this with a heuristic.
319 return BLOCK_128X128;
320#else
321 (void)cpi;
322 return BLOCK_64X64;
323#endif // CONFIG_EXT_PARTITION
324}
325
Yaowu Xuf883b422016-08-30 14:01:10 -0700326static void setup_frame(AV1_COMP *cpi) {
327 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700328 // Set up entropy context depending on frame type. The decoder mandates
329 // the use of the default context, index 0, for keyframes and inter
330 // frames where the error_resilient_mode or intra_only flag is set. For
331 // other inter-frames the encoder currently uses only two contexts;
332 // context 1 for ALTREF frames and context 0 for the others.
333 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700334 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700336#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
337// Just use frame context from first signaled reference frame.
338// This will always be LAST_FRAME for now.
339#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700340 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -0700341 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700342 cm->frame_context_idx = EXT_ARF_FRAME;
343 else if (cpi->refresh_alt_ref_frame)
344 cm->frame_context_idx = ARF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345 else if (cpi->rc.is_src_frame_alt_ref)
346 cm->frame_context_idx = OVERLAY_FRAME;
347 else if (cpi->refresh_golden_frame)
348 cm->frame_context_idx = GLD_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700349 else if (cpi->refresh_bwd_ref_frame)
350 cm->frame_context_idx = BRF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700351 else
352 cm->frame_context_idx = REGULAR_FRAME;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700353#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700354 }
355
356 if (cm->frame_type == KEY_FRAME) {
357 cpi->refresh_golden_frame = 1;
358 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700359 av1_zero(cpi->interp_filter_selected);
Debargha Mukherjeed2630fa2017-09-22 10:32:51 -0700360 set_sb_size(cm, select_sb_size(cpi));
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700361#if CONFIG_REFERENCE_BUFFER
362 set_use_reference_buffer(cm, 0);
363#endif // CONFIG_REFERENCE_BUFFER
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700365#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
366 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
367 cm->frame_refs[0].idx < 0) {
368 *cm->fc = cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
369 } else {
370 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
371 }
372#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700373 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700374#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuf883b422016-08-30 14:01:10 -0700375 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700376 }
Zoe Liu5a978832017-08-15 16:33:34 -0700377#if CONFIG_ONE_SIDED_COMPOUND && \
378 !CONFIG_EXT_COMP_REFS // No change to bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200379 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
380 cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
381 cpi->rc.is_bipred_frame = 1;
382 }
Zoe Liu5a978832017-08-15 16:33:34 -0700383#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Thomas Daededa4d8b92017-06-05 15:44:14 -0700384#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -0700385 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
386 cm->frame_refs[0].idx < 0) {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700387 // use default frame context values
388 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
389 } else {
Thomas Daedea6a854b2017-06-22 17:49:11 -0700390 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700391 cm->pre_fc = &cm->frame_contexts[cm->frame_refs[0].idx];
392 }
393#else
Thomas Daede10e1da92017-04-26 13:22:21 -0700394 cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700395#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700396
397 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700398}
399
Cheng Chen46f30c72017-09-07 11:13:33 -0700400static void enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700401 int i;
402 cm->mi = cm->mip + cm->mi_stride + 1;
403 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
404 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
405 // Clear top border row
406 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
407 // Clear left border column
408 for (i = 1; i < cm->mi_rows + 1; ++i)
409 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700410 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
411 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
412
413 memset(cm->mi_grid_base, 0,
414 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
415}
416
Cheng Chen46f30c72017-09-07 11:13:33 -0700417static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700418 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700419 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700420 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421 if (!cm->prev_mip) return 1;
422 cm->mi_alloc_size = mi_size;
423
Yaowu Xuf883b422016-08-30 14:01:10 -0700424 cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700425 if (!cm->mi_grid_base) return 1;
426 cm->prev_mi_grid_base =
Yaowu Xuf883b422016-08-30 14:01:10 -0700427 (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700428 if (!cm->prev_mi_grid_base) return 1;
429
430 return 0;
431}
432
Cheng Chen46f30c72017-09-07 11:13:33 -0700433static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700434 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700435 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700436 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700438 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700439 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700440 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700441 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700442 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700443}
444
Cheng Chen46f30c72017-09-07 11:13:33 -0700445static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700446 // Current mip will be the prev_mip for the next frame.
447 MODE_INFO **temp_base = cm->prev_mi_grid_base;
448 MODE_INFO *temp = cm->prev_mip;
449 cm->prev_mip = cm->mip;
450 cm->mip = temp;
451
452 // Update the upper left visible macroblock ptrs.
453 cm->mi = cm->mip + cm->mi_stride + 1;
454 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
455
456 cm->prev_mi_grid_base = cm->mi_grid_base;
457 cm->mi_grid_base = temp_base;
458 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
459 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
460}
461
Yaowu Xuf883b422016-08-30 14:01:10 -0700462void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700463 static volatile int init_done = 0;
464
465 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700466 av1_rtcd();
467 aom_dsp_rtcd();
468 aom_scale_rtcd();
469 av1_init_intra_predictors();
470 av1_init_me_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000471#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -0700472 av1_rc_init_minq_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000473#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700474 av1_entropy_mv_init();
475 av1_encode_token_init();
Yaowu Xuf883b422016-08-30 14:01:10 -0700476 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477 init_done = 1;
478 }
479}
480
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700481static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
482 if (cpi->mbmi_ext_base) {
483 aom_free(cpi->mbmi_ext_base);
484 cpi->mbmi_ext_base = NULL;
485 }
486}
487
488static void alloc_context_buffers_ext(AV1_COMP *cpi) {
489 AV1_COMMON *cm = &cpi->common;
490 int mi_size = cm->mi_cols * cm->mi_rows;
491
492 dealloc_context_buffers_ext(cpi);
493 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
494 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
495}
496
Yaowu Xuf883b422016-08-30 14:01:10 -0700497static void dealloc_compressor_data(AV1_COMP *cpi) {
498 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700499
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700500 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700501
Yaowu Xuf883b422016-08-30 14:01:10 -0700502 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700503 cpi->tile_data = NULL;
504
505 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700506 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700507 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508
Yaowu Xuf883b422016-08-30 14:01:10 -0700509 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510 cpi->cyclic_refresh = NULL;
511
Yaowu Xuf883b422016-08-30 14:01:10 -0700512 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700513 cpi->active_map.map = NULL;
514
Jingning Hand064cf02017-06-01 10:00:39 -0700515 aom_free(cpi->td.mb.above_pred_buf);
516 cpi->td.mb.above_pred_buf = NULL;
517
518 aom_free(cpi->td.mb.left_pred_buf);
519 cpi->td.mb.left_pred_buf = NULL;
520
521 aom_free(cpi->td.mb.wsrc_buf);
522 cpi->td.mb.wsrc_buf = NULL;
523
524 aom_free(cpi->td.mb.mask_buf);
525 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700526
Jingning Han6cc1fd32017-10-13 09:05:36 -0700527#if CONFIG_MFMV
528 aom_free(cm->tpl_mvs);
529 cm->tpl_mvs = NULL;
530#endif
531
Yaowu Xuf883b422016-08-30 14:01:10 -0700532 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700533#if CONFIG_LV_MAP
534 av1_free_txb_buf(cpi);
535#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700536 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537
Yaowu Xuf883b422016-08-30 14:01:10 -0700538 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700539#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700540 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800541 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700543 aom_free_frame_buffer(&cpi->scaled_source);
544 aom_free_frame_buffer(&cpi->scaled_last_source);
545 aom_free_frame_buffer(&cpi->alt_ref_buffer);
546 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547
Yaowu Xuf883b422016-08-30 14:01:10 -0700548 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549 cpi->tile_tok[0][0] = 0;
550
Yaowu Xuf883b422016-08-30 14:01:10 -0700551 av1_free_pc_tree(&cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552
hui sud9a812b2017-07-06 14:34:37 -0700553 aom_free(cpi->td.mb.palette_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554
Yaowu Xuc27fc142016-08-22 16:08:15 -0700555#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -0700556 aom_buf_ans_free(&cpi->buf_ans);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700557#endif // CONFIG_ANS
558}
559
Yaowu Xuf883b422016-08-30 14:01:10 -0700560static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700561 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700562 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000563 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200565 // Stores a snapshot of key state variables which can subsequently be
566 // restored with a call to av1_restore_coding_context. These functions are
567 // intended for use in a re-code loop in av1_compress_frame where the
568 // quantizer value is adjusted between loop iterations.
James Zern01a9d702017-08-25 19:09:33 +0000569 for (i = 0; i < NMV_CONTEXTS; ++i) {
570 av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
571 av1_copy(cc->nmv_costs, cpi->nmv_costs);
572 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
573 }
574
Yaowu Xuf883b422016-08-30 14:01:10 -0700575 av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
576 av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700577
578 cc->fc = *cm->fc;
579}
580
Yaowu Xuf883b422016-08-30 14:01:10 -0700581static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700583 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000584 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200586 // Restore key state variables to the snapshot state stored in the
587 // previous call to av1_save_coding_context.
James Zern01a9d702017-08-25 19:09:33 +0000588 for (i = 0; i < NMV_CONTEXTS; ++i) {
589 av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
590 av1_copy(cpi->nmv_costs, cc->nmv_costs);
591 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
592 }
593
Yaowu Xuf883b422016-08-30 14:01:10 -0700594 av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
595 av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596
597 *cm->fc = cc->fc;
598}
599
Yaowu Xuf883b422016-08-30 14:01:10 -0700600static void configure_static_seg_features(AV1_COMP *cpi) {
601 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700602 const RATE_CONTROL *const rc = &cpi->rc;
603 struct segmentation *const seg = &cm->seg;
604
605 int high_q = (int)(rc->avg_q > 48.0);
606 int qi_delta;
607
608 // Disable and clear down for KF
609 if (cm->frame_type == KEY_FRAME) {
610 // Clear down the global segmentation map
611 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
612 seg->update_map = 0;
613 seg->update_data = 0;
614 cpi->static_mb_pct = 0;
615
616 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700617 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700618
619 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700620 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 } else if (cpi->refresh_alt_ref_frame) {
622 // If this is an alt ref frame
623 // Clear down the global segmentation map
624 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
625 seg->update_map = 0;
626 seg->update_data = 0;
627 cpi->static_mb_pct = 0;
628
629 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700630 av1_disable_segmentation(seg);
631 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632
633 // Scan frames from current to arf frame.
634 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700635 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636
637 // If segmentation was enabled set those features needed for the
638 // arf itself.
639 if (seg->enabled) {
640 seg->update_map = 1;
641 seg->update_data = 1;
642
643 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700644 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
645 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700646#if CONFIG_LOOPFILTER_LEVEL
647 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
648 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
649 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
650 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
651
652 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
653 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
654 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
655 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
656#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700657 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Cheng Chend8184da2017-09-26 18:15:22 -0700658 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
659#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660
Yaowu Xuf883b422016-08-30 14:01:10 -0700661 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700662
663 // Where relevant assume segment data is delta data
664 seg->abs_delta = SEGMENT_DELTADATA;
665 }
666 } else if (seg->enabled) {
667 // All other frames if segmentation has been enabled
668
669 // First normal frame in a valid gf or alt ref group
670 if (rc->frames_since_golden == 0) {
671 // Set up segment features for normal frames in an arf group
672 if (rc->source_alt_ref_active) {
673 seg->update_map = 0;
674 seg->update_data = 1;
675 seg->abs_delta = SEGMENT_DELTADATA;
676
Yaowu Xuf883b422016-08-30 14:01:10 -0700677 qi_delta =
678 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
679 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
680 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700681
Cheng Chend8184da2017-09-26 18:15:22 -0700682#if CONFIG_LOOPFILTER_LEVEL
683 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
684 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
685 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
686 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
687
688 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
689 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
690 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
691 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
692#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700693 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
694 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Cheng Chend8184da2017-09-26 18:15:22 -0700695#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696
697 // Segment coding disabled for compred testing
698 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700699 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
700 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
701 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 }
703 } else {
704 // Disable segmentation and clear down features if alt ref
705 // is not active for this group
706
Yaowu Xuf883b422016-08-30 14:01:10 -0700707 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700708
709 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
710
711 seg->update_map = 0;
712 seg->update_data = 0;
713
Yaowu Xuf883b422016-08-30 14:01:10 -0700714 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700715 }
716 } else if (rc->is_src_frame_alt_ref) {
717 // Special case where we are coding over the top of a previous
718 // alt ref frame.
719 // Segment coding disabled for compred testing
720
721 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700722 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
723 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700724
725 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
727 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
728 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
729 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730
731 // Skip all MBs if high Q (0,0 mv and skip coeffs)
732 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700733 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
734 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 }
736 // Enable data update
737 seg->update_data = 1;
738 } else {
739 // All other frames.
740
741 // No updates.. leave things as they are.
742 seg->update_map = 0;
743 seg->update_data = 0;
744 }
745 }
746}
747
Yaowu Xuf883b422016-08-30 14:01:10 -0700748static void update_reference_segmentation_map(AV1_COMP *cpi) {
749 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
751 uint8_t *cache_ptr = cm->last_frame_seg_map;
752 int row, col;
753
754 for (row = 0; row < cm->mi_rows; row++) {
755 MODE_INFO **mi_8x8 = mi_8x8_ptr;
756 uint8_t *cache = cache_ptr;
757 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
758 cache[0] = mi_8x8[0]->mbmi.segment_id;
759 mi_8x8_ptr += cm->mi_stride;
760 cache_ptr += cm->mi_cols;
761 }
762}
763
Yaowu Xuf883b422016-08-30 14:01:10 -0700764static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
765 AV1_COMMON *cm = &cpi->common;
766 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700767
768 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700769 cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
770 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200771#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700772 cm->use_highbitdepth,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700773#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700774 oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700775 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700776 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700777 "Failed to allocate lag buffers");
778
779 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700780 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700781 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200782#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783 cm->use_highbitdepth,
784#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700785 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
786 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700787 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788 "Failed to allocate altref buffer");
789}
790
Yaowu Xuf883b422016-08-30 14:01:10 -0700791static void alloc_util_frame_buffers(AV1_COMP *cpi) {
792 AV1_COMMON *const cm = &cpi->common;
793 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700794 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200795#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796 cm->use_highbitdepth,
797#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700798 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
799 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700800 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700801 "Failed to allocate last frame buffer");
802
803#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700804 if (aom_realloc_frame_buffer(
805 &cpi->trial_frame_rst,
806#if CONFIG_FRAME_SUPERRES
807 cm->superres_upscaled_width, cm->superres_upscaled_height,
808#else
809 cm->width, cm->height,
810#endif // CONFIG_FRAME_SUPERRES
811 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200812#if CONFIG_HIGHBITDEPTH
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700813 cm->use_highbitdepth,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800814#endif
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700815 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800816 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800817 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818#endif // CONFIG_LOOP_RESTORATION
819
Yaowu Xuf883b422016-08-30 14:01:10 -0700820 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700821 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200822#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823 cm->use_highbitdepth,
824#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700825 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
826 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700827 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828 "Failed to allocate scaled source buffer");
829
Yaowu Xuf883b422016-08-30 14:01:10 -0700830 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700831 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200832#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833 cm->use_highbitdepth,
834#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700835 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
836 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700837 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700838 "Failed to allocate scaled last source buffer");
839}
840
Cheng Chen46f30c72017-09-07 11:13:33 -0700841static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700842 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843
Yaowu Xuf883b422016-08-30 14:01:10 -0700844 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700846#if CONFIG_LV_MAP
847 av1_alloc_txb_buf(cpi);
848#endif
849
Yaowu Xuc27fc142016-08-22 16:08:15 -0700850 alloc_context_buffers_ext(cpi);
851
Yaowu Xuf883b422016-08-30 14:01:10 -0700852 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853
854 {
Yaowu Xue39b3b82017-10-31 16:11:59 -0700855 unsigned int tokens =
856 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700858 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859 }
860
Yaowu Xuf883b422016-08-30 14:01:10 -0700861 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700862}
863
Yaowu Xuf883b422016-08-30 14:01:10 -0700864void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000866#if CONFIG_XIPHRC
867 if (!cpi->od_rc.cur_frame) return;
868 cpi->od_rc.framerate = cpi->framerate;
869 od_enc_rc_resize(&cpi->od_rc);
870#else
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700871 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000872#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873}
874
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200875#if CONFIG_MAX_TILE
876
877static void set_tile_info_max_tile(AV1_COMP *cpi) {
878 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200879 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200880
881 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200882
883 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200884 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200885 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200886 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
887 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200888 } else {
889 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
890 int sb_cols = mi_cols >> MAX_MIB_SIZE_LOG2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200891 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200892 cm->uniform_tile_spacing_flag = 0;
893 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
894 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200895 size_sb = cpi->oxcf.tile_widths[j++];
896 if (j >= cpi->oxcf.tile_width_count) j = 0;
897 start_sb += AOMMIN(size_sb, MAX_TILE_WIDTH_SB);
Dominic Symesf58f1112017-09-25 12:47:40 +0200898 }
899 cm->tile_cols = i;
900 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200901 }
902 av1_calculate_tile_cols(cm);
903
904 // configure tile rows
905 if (cm->uniform_tile_spacing_flag) {
906 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
907 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200908 } else {
909 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
910 int sb_rows = mi_rows >> MAX_MIB_SIZE_LOG2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200911 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200912 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
913 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200914 size_sb = cpi->oxcf.tile_heights[j++];
915 if (j >= cpi->oxcf.tile_height_count) j = 0;
916 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200917 }
918 cm->tile_rows = i;
919 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200920 }
921 av1_calculate_tile_rows(cm);
922}
923
924#endif
925
Yaowu Xuf883b422016-08-30 14:01:10 -0700926static void set_tile_info(AV1_COMP *cpi) {
927 AV1_COMMON *const cm = &cpi->common;
Thomas Daviesb25ba502017-07-18 10:18:24 +0100928#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -0800929 int tile_row, tile_col, num_tiles_in_tg;
930 int tg_row_start, tg_col_start;
931#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700932#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700933 if (cpi->oxcf.large_scale_tile) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934#if CONFIG_EXT_PARTITION
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700935 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
936 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
937 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
938 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
939 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
940 } else {
941 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
942 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
943 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
944 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
945 }
946#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700947 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
948 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700949 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
950 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700951#endif // CONFIG_EXT_PARTITION
952
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700953 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
954 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700955
Yaowu Xu8d0039c2017-10-13 07:51:46 -0700956 assert(cm->tile_width >> MAX_MIB_SIZE_LOG2 <= 32);
957 assert(cm->tile_height >> MAX_MIB_SIZE_LOG2 <= 32);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700958
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700959 // Get the number of tiles
960 cm->tile_cols = 1;
961 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700963 cm->tile_rows = 1;
964 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
965 } else {
966#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700967
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200968#if CONFIG_MAX_TILE
969 set_tile_info_max_tile(cpi);
970#else
971 int min_log2_tile_cols, max_log2_tile_cols;
972 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700973
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200974 cm->log2_tile_cols =
975 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
976 cm->log2_tile_rows = cpi->oxcf.tile_rows;
Rupert Swarbrick940942b2017-09-01 12:05:55 +0100977
Rupert Swarbrick5a010aa2017-09-26 16:16:48 +0100978 cm->tile_width =
979 get_tile_size(cm->mi_cols, cm->log2_tile_cols, &cm->tile_cols);
980 cm->tile_height =
981 get_tile_size(cm->mi_rows, cm->log2_tile_rows, &cm->tile_rows);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200982#endif // CONFIG_MAX_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700983#if CONFIG_EXT_TILE
984 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700985#endif // CONFIG_EXT_TILE
Ryan Lei7386eda2016-12-08 21:08:31 -0800986
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800987#if CONFIG_DEPENDENT_HORZTILES
988 cm->dependent_horz_tiles = cpi->oxcf.dependent_horz_tiles;
Fangwen Fu70bcb892017-05-06 17:05:19 -0700989#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700990 if (cm->large_scale_tile) {
991 // May not needed since cpi->oxcf.dependent_horz_tiles is already adjusted.
992 cm->dependent_horz_tiles = 0;
Fangwen Fu73126c02017-02-08 22:37:47 -0800993 } else {
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700994#endif // CONFIG_EXT_TILE
995 if (cm->log2_tile_rows == 0) cm->dependent_horz_tiles = 0;
996#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -0800997 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700998#endif // CONFIG_EXT_TILE
999
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001000#if CONFIG_EXT_TILE
1001 if (!cm->large_scale_tile) {
1002#endif // CONFIG_EXT_TILE
1003 if (cpi->oxcf.mtu == 0) {
1004 cm->num_tg = cpi->oxcf.num_tile_groups;
1005 } else {
1006 // Use a default value for the purposes of weighting costs in probability
1007 // updates
1008 cm->num_tg = DEFAULT_MAX_NUM_TG;
Fangwen Fu73126c02017-02-08 22:37:47 -08001009 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001010 num_tiles_in_tg =
1011 (cm->tile_cols * cm->tile_rows + cm->num_tg - 1) / cm->num_tg;
1012 tg_row_start = 0;
1013 tg_col_start = 0;
1014 for (tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
1015 for (tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
1016 if ((tile_row * cm->tile_cols + tile_col) % num_tiles_in_tg == 0) {
1017 tg_row_start = tile_row;
1018 tg_col_start = tile_col;
1019 }
1020 cm->tile_group_start_row[tile_row][tile_col] = tg_row_start;
1021 cm->tile_group_start_col[tile_row][tile_col] = tg_col_start;
1022 }
1023 }
1024#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -08001025 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001026#endif // CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -08001027#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08001028
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001029#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08001030 cm->loop_filter_across_tiles_enabled =
1031 cpi->oxcf.loop_filter_across_tiles_enabled;
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001032#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001033}
1034
Yaowu Xuf883b422016-08-30 14:01:10 -07001035static void update_frame_size(AV1_COMP *cpi) {
1036 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001037 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1038
Yaowu Xuf883b422016-08-30 14:01:10 -07001039 av1_set_mb_mi(cm, cm->width, cm->height);
1040 av1_init_context_buffers(cm);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001041 av1_init_macroblockd(cm, xd,
Luc Trudeauf8164152017-04-11 16:20:51 -04001042#if CONFIG_CFL
1043 &NULL_CFL,
1044#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07001045 NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001046 memset(cpi->mbmi_ext_base, 0,
1047 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001048 set_tile_info(cpi);
1049}
1050
Yaowu Xuf883b422016-08-30 14:01:10 -07001051static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001052 int fb_idx;
1053 for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
1054 cpi->lst_fb_idxes[fb_idx] = fb_idx;
1055 cpi->gld_fb_idx = LAST_REF_FRAMES;
1056 cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07001057 cpi->alt2_fb_idx = LAST_REF_FRAMES + 2;
1058 cpi->alt_fb_idx = LAST_REF_FRAMES + 3;
Zoe Liu8dd1c982017-09-11 10:14:35 -07001059 cpi->ext_fb_idx = LAST_REF_FRAMES + 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001060 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
1061 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -07001062#if CONFIG_AMVR
1063 cpi->rate_index = 0;
1064 cpi->rate_size = 0;
1065 cpi->cur_poc = -1;
1066#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001067}
1068
Yaowu Xuf883b422016-08-30 14:01:10 -07001069static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1070 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001071
1072 cpi->oxcf = *oxcf;
1073 cpi->framerate = oxcf->init_framerate;
1074
1075 cm->profile = oxcf->profile;
1076 cm->bit_depth = oxcf->bit_depth;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001077#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001078 cm->use_highbitdepth = oxcf->use_highbitdepth;
1079#endif
1080 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07001081#if CONFIG_COLORSPACE_HEADERS
1082 cm->transfer_function = oxcf->transfer_function;
1083 cm->chroma_sample_position = oxcf->chroma_sample_position;
1084#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001085 cm->color_range = oxcf->color_range;
1086
1087 cm->width = oxcf->width;
1088 cm->height = oxcf->height;
Cheng Chen46f30c72017-09-07 11:13:33 -07001089 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001090
1091 // Single thread case: use counts in common.
1092 cpi->td.counts = &cm->counts;
1093
1094 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001095 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001096
1097 cpi->static_mb_pct = 0;
1098 cpi->ref_frame_flags = 0;
1099
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001100 // Reset resize pending flags
1101 cpi->resize_pending_width = 0;
1102 cpi->resize_pending_height = 0;
1103
Yaowu Xuc27fc142016-08-22 16:08:15 -07001104 init_buffer_indices(cpi);
1105}
1106
1107static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001108 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001109 const int64_t bandwidth = oxcf->target_bandwidth;
1110 const int64_t starting = oxcf->starting_buffer_level_ms;
1111 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1112 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1113
1114 rc->starting_buffer_level = starting * bandwidth / 1000;
1115 rc->optimal_buffer_level =
1116 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1117 rc->maximum_buffer_size =
1118 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1119}
1120
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001121#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001122#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
1123 cpi->fn_ptr[BT].sdf = SDF; \
1124 cpi->fn_ptr[BT].sdaf = SDAF; \
1125 cpi->fn_ptr[BT].vf = VF; \
1126 cpi->fn_ptr[BT].svf = SVF; \
1127 cpi->fn_ptr[BT].svaf = SVAF; \
1128 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1129 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1130 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1131
1132#define MAKE_BFP_SAD_WRAPPER(fnname) \
1133 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1134 int source_stride, \
1135 const uint8_t *ref_ptr, int ref_stride) { \
1136 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1137 } \
1138 static unsigned int fnname##_bits10( \
1139 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1140 int ref_stride) { \
1141 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1142 } \
1143 static unsigned int fnname##_bits12( \
1144 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1145 int ref_stride) { \
1146 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1147 }
1148
1149#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1150 static unsigned int fnname##_bits8( \
1151 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1152 int ref_stride, const uint8_t *second_pred) { \
1153 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1154 } \
1155 static unsigned int fnname##_bits10( \
1156 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1157 int ref_stride, const uint8_t *second_pred) { \
1158 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1159 2; \
1160 } \
1161 static unsigned int fnname##_bits12( \
1162 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1163 int ref_stride, const uint8_t *second_pred) { \
1164 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1165 4; \
1166 }
1167
1168#define MAKE_BFP_SAD3_WRAPPER(fnname) \
1169 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1170 const uint8_t *ref_ptr, int ref_stride, \
1171 unsigned int *sad_array) { \
1172 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1173 } \
1174 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1175 const uint8_t *ref_ptr, int ref_stride, \
1176 unsigned int *sad_array) { \
1177 int i; \
1178 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1179 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1180 } \
1181 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1182 const uint8_t *ref_ptr, int ref_stride, \
1183 unsigned int *sad_array) { \
1184 int i; \
1185 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1186 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1187 }
1188
1189#define MAKE_BFP_SAD8_WRAPPER(fnname) \
1190 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1191 const uint8_t *ref_ptr, int ref_stride, \
1192 unsigned int *sad_array) { \
1193 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1194 } \
1195 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1196 const uint8_t *ref_ptr, int ref_stride, \
1197 unsigned int *sad_array) { \
1198 int i; \
1199 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1200 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1201 } \
1202 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1203 const uint8_t *ref_ptr, int ref_stride, \
1204 unsigned int *sad_array) { \
1205 int i; \
1206 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1207 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1208 }
1209#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1210 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1211 const uint8_t *const ref_ptr[], int ref_stride, \
1212 unsigned int *sad_array) { \
1213 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1214 } \
1215 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1216 const uint8_t *const ref_ptr[], int ref_stride, \
1217 unsigned int *sad_array) { \
1218 int i; \
1219 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1220 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1221 } \
1222 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1223 const uint8_t *const ref_ptr[], int ref_stride, \
1224 unsigned int *sad_array) { \
1225 int i; \
1226 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1227 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1228 }
1229
1230#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001231MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1232MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1233MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
1234MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
1235MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1236MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1237MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1238MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1239MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1240MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1241MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001242#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001243MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1244MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1245MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1246MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1247MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1248MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1249MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1250MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1251MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1252MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1253MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1254MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1255MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1256MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1257MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
1258MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
1259MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1260MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1261MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1262MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
1263MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
1264MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1265MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1266MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1267MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
1268MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
1269MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1270MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1271MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1272MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
1273MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
1274MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1275MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1276MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1277MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
1278MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
1279MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1280MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1281MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1282MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
1283MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
1284MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1285MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1286MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1287MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
1288MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1289MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1290MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1291MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
1292MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1293MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1294MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1295MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
1296MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
1297MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001298
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001299#if CONFIG_EXT_PARTITION_TYPES
1300MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1301MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1302MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1303MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1304MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1305MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1306MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1307MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1308MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1309MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1310MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1311MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001312MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1313MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1314MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1315MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1316MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1317MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001318#if CONFIG_EXT_PARTITION
1319MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x128)
1320MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x128_avg)
1321MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x128x4d)
1322MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x32)
1323MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x32_avg)
1324MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x32x4d)
1325#endif // CONFIG_EXT_PARTITION
1326#endif // CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001327
David Barker0f3c94e2017-05-16 15:21:50 +01001328#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001329 cpi->fn_ptr[BT].msdf = MCSDF; \
1330 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001331
David Barkerc155e012017-05-11 13:54:54 +01001332#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1333 static unsigned int fnname##_bits8( \
1334 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1335 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1336 int m_stride, int invert_mask) { \
1337 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1338 second_pred_ptr, m, m_stride, invert_mask); \
1339 } \
1340 static unsigned int fnname##_bits10( \
1341 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1342 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1343 int m_stride, int invert_mask) { \
1344 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1345 second_pred_ptr, m, m_stride, invert_mask) >> \
1346 2; \
1347 } \
1348 static unsigned int fnname##_bits12( \
1349 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1350 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1351 int m_stride, int invert_mask) { \
1352 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1353 second_pred_ptr, m, m_stride, invert_mask) >> \
1354 4; \
1355 }
1356
Yaowu Xuc27fc142016-08-22 16:08:15 -07001357#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001358MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1359MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1360MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001361#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001362MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1363MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1364MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1365MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1366MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1367MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1368MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1369MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1370MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1371MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1372MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1373MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1374MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001375
1376#if CONFIG_EXT_PARTITION_TYPES
1377MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1378MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1379MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1380MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001381MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1382MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001383#if CONFIG_EXT_PARTITION
1384MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x128)
1385MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x32)
1386#endif // CONFIG_EXT_PARTITION
1387#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001388
Yaowu Xuc27fc142016-08-22 16:08:15 -07001389#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1390 cpi->fn_ptr[BT].osdf = OSDF; \
1391 cpi->fn_ptr[BT].ovf = OVF; \
1392 cpi->fn_ptr[BT].osvf = OSVF;
1393
1394#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1395 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1396 const int32_t *wsrc, \
1397 const int32_t *msk) { \
1398 return fnname(ref, ref_stride, wsrc, msk); \
1399 } \
1400 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1401 const int32_t *wsrc, \
1402 const int32_t *msk) { \
1403 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1404 } \
1405 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1406 const int32_t *wsrc, \
1407 const int32_t *msk) { \
1408 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1409 }
1410
1411#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001412MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1413MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1414MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001415#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001416MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1417MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1418MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1419MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1420MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1421MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1422MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1423MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1424MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1425MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1426MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1427MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1428MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001429
1430#if CONFIG_EXT_PARTITION_TYPES
1431MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1432MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1433MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1434MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001435MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1436MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001437#if CONFIG_EXT_PARTITION
1438MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x128)
1439MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x32)
1440#endif // CONFIG_EXT_PARTITION
1441#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001442
Yaowu Xuf883b422016-08-30 14:01:10 -07001443static void highbd_set_var_fns(AV1_COMP *const cpi) {
1444 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001445 if (cm->use_highbitdepth) {
1446 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001447 case AOM_BITS_8:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001448#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001449#if CONFIG_EXT_PARTITION
1450 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits8,
1451 aom_highbd_sad128x32_avg_bits8, aom_highbd_8_variance128x32,
1452 aom_highbd_8_sub_pixel_variance128x32,
1453 aom_highbd_8_sub_pixel_avg_variance128x32, NULL, NULL,
1454 aom_highbd_sad128x32x4d_bits8)
1455
1456 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits8,
1457 aom_highbd_sad32x128_avg_bits8, aom_highbd_8_variance32x128,
1458 aom_highbd_8_sub_pixel_variance32x128,
1459 aom_highbd_8_sub_pixel_avg_variance32x128, NULL, NULL,
1460 aom_highbd_sad32x128x4d_bits8)
1461#endif // CONFIG_EXT_PARTITION
1462
Rupert Swarbrick72678572017-08-02 12:05:26 +01001463 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1464 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1465 aom_highbd_8_sub_pixel_variance64x16,
1466 aom_highbd_8_sub_pixel_avg_variance64x16, NULL, NULL,
1467 aom_highbd_sad64x16x4d_bits8)
1468
1469 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1470 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1471 aom_highbd_8_sub_pixel_variance16x64,
1472 aom_highbd_8_sub_pixel_avg_variance16x64, NULL, NULL,
1473 aom_highbd_sad16x64x4d_bits8)
1474
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001475 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits8,
1476 aom_highbd_sad32x8_avg_bits8, aom_highbd_8_variance32x8,
1477 aom_highbd_8_sub_pixel_variance32x8,
1478 aom_highbd_8_sub_pixel_avg_variance32x8, NULL, NULL,
1479 aom_highbd_sad32x8x4d_bits8)
1480
1481 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits8,
1482 aom_highbd_sad8x32_avg_bits8, aom_highbd_8_variance8x32,
1483 aom_highbd_8_sub_pixel_variance8x32,
1484 aom_highbd_8_sub_pixel_avg_variance8x32, NULL, NULL,
1485 aom_highbd_sad8x32x4d_bits8)
1486
1487 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits8,
1488 aom_highbd_sad16x4_avg_bits8, aom_highbd_8_variance16x4,
1489 aom_highbd_8_sub_pixel_variance16x4,
1490 aom_highbd_8_sub_pixel_avg_variance16x4, NULL, NULL,
1491 aom_highbd_sad16x4x4d_bits8)
1492
1493 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits8,
1494 aom_highbd_sad4x16_avg_bits8, aom_highbd_8_variance4x16,
1495 aom_highbd_8_sub_pixel_variance4x16,
1496 aom_highbd_8_sub_pixel_avg_variance4x16, NULL, NULL,
1497 aom_highbd_sad4x16x4d_bits8)
1498#endif
1499
Yaowu Xuf883b422016-08-30 14:01:10 -07001500 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1501 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1502 aom_highbd_8_sub_pixel_variance32x16,
1503 aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1504 aom_highbd_sad32x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001505
Yaowu Xuf883b422016-08-30 14:01:10 -07001506 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1507 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1508 aom_highbd_8_sub_pixel_variance16x32,
1509 aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1510 aom_highbd_sad16x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001511
Yaowu Xuf883b422016-08-30 14:01:10 -07001512 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1513 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1514 aom_highbd_8_sub_pixel_variance64x32,
1515 aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1516 aom_highbd_sad64x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001517
Yaowu Xuf883b422016-08-30 14:01:10 -07001518 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1519 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1520 aom_highbd_8_sub_pixel_variance32x64,
1521 aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1522 aom_highbd_sad32x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001523
Yaowu Xuf883b422016-08-30 14:01:10 -07001524 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1525 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1526 aom_highbd_8_sub_pixel_variance32x32,
1527 aom_highbd_8_sub_pixel_avg_variance32x32,
1528 aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
1529 aom_highbd_sad32x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001530
Yaowu Xuf883b422016-08-30 14:01:10 -07001531 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1532 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1533 aom_highbd_8_sub_pixel_variance64x64,
1534 aom_highbd_8_sub_pixel_avg_variance64x64,
1535 aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
1536 aom_highbd_sad64x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001537
Yaowu Xuf883b422016-08-30 14:01:10 -07001538 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1539 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1540 aom_highbd_8_sub_pixel_variance16x16,
1541 aom_highbd_8_sub_pixel_avg_variance16x16,
1542 aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
1543 aom_highbd_sad16x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001544
1545 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001546 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1547 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1548 aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
1549 aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001550
1551 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001552 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1553 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1554 aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
1555 aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001556
1557 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001558 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1559 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1560 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits8,
1561 aom_highbd_sad8x8x8_bits8, aom_highbd_sad8x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001562
Yaowu Xuf883b422016-08-30 14:01:10 -07001563 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1564 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1565 aom_highbd_8_sub_pixel_variance8x4,
1566 aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
1567 aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001568
Yaowu Xuf883b422016-08-30 14:01:10 -07001569 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1570 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1571 aom_highbd_8_sub_pixel_variance4x8,
1572 aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
1573 aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001574
1575 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001576 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1577 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1578 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits8,
1579 aom_highbd_sad4x4x8_bits8, aom_highbd_sad4x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001580
Jingning Hancc5bdf42016-12-19 11:14:30 -08001581 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_8_variance2x2, NULL, NULL,
1582 NULL, NULL, NULL)
1583 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_8_variance4x2, NULL, NULL,
1584 NULL, NULL, NULL)
1585 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_8_variance2x4, NULL, NULL,
1586 NULL, NULL, NULL)
Jingning Hancc5bdf42016-12-19 11:14:30 -08001587
Yaowu Xuc27fc142016-08-22 16:08:15 -07001588#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001589 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1590 aom_highbd_sad128x128_avg_bits8,
1591 aom_highbd_8_variance128x128,
1592 aom_highbd_8_sub_pixel_variance128x128,
1593 aom_highbd_8_sub_pixel_avg_variance128x128,
1594 aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
1595 aom_highbd_sad128x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001596
Yaowu Xuf883b422016-08-30 14:01:10 -07001597 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1598 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1599 aom_highbd_8_sub_pixel_variance128x64,
1600 aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
1601 aom_highbd_sad128x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001602
Yaowu Xuf883b422016-08-30 14:01:10 -07001603 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1604 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1605 aom_highbd_8_sub_pixel_variance64x128,
1606 aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
1607 aom_highbd_sad64x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001608#endif // CONFIG_EXT_PARTITION
1609
Yaowu Xuc27fc142016-08-22 16:08:15 -07001610#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001611 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1612 aom_highbd_8_masked_sub_pixel_variance128x128)
1613 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1614 aom_highbd_8_masked_sub_pixel_variance128x64)
1615 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1616 aom_highbd_8_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001617#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001618 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1619 aom_highbd_8_masked_sub_pixel_variance64x64)
1620 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1621 aom_highbd_8_masked_sub_pixel_variance64x32)
1622 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1623 aom_highbd_8_masked_sub_pixel_variance32x64)
1624 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1625 aom_highbd_8_masked_sub_pixel_variance32x32)
1626 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1627 aom_highbd_8_masked_sub_pixel_variance32x16)
1628 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1629 aom_highbd_8_masked_sub_pixel_variance16x32)
1630 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1631 aom_highbd_8_masked_sub_pixel_variance16x16)
1632 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1633 aom_highbd_8_masked_sub_pixel_variance8x16)
1634 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1635 aom_highbd_8_masked_sub_pixel_variance16x8)
1636 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1637 aom_highbd_8_masked_sub_pixel_variance8x8)
1638 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1639 aom_highbd_8_masked_sub_pixel_variance4x8)
1640 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1641 aom_highbd_8_masked_sub_pixel_variance8x4)
1642 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1643 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001644#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001645#if CONFIG_EXT_PARTITION
1646 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits8,
1647 aom_highbd_8_masked_sub_pixel_variance128x32)
1648
1649 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits8,
1650 aom_highbd_8_masked_sub_pixel_variance32x128)
1651#endif // CONFIG_EXT_PARTITION
1652
Rupert Swarbrick72678572017-08-02 12:05:26 +01001653 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1654 aom_highbd_8_masked_sub_pixel_variance64x16)
1655
1656 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1657 aom_highbd_8_masked_sub_pixel_variance16x64)
1658
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001659 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1660 aom_highbd_8_masked_sub_pixel_variance32x8)
1661
1662 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1663 aom_highbd_8_masked_sub_pixel_variance8x32)
1664
1665 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1666 aom_highbd_8_masked_sub_pixel_variance16x4)
1667
1668 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1669 aom_highbd_8_masked_sub_pixel_variance4x16)
1670#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001671#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001672 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1673 aom_highbd_obmc_variance128x128,
1674 aom_highbd_obmc_sub_pixel_variance128x128)
1675 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1676 aom_highbd_obmc_variance128x64,
1677 aom_highbd_obmc_sub_pixel_variance128x64)
1678 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1679 aom_highbd_obmc_variance64x128,
1680 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001681#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001682 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1683 aom_highbd_obmc_variance64x64,
1684 aom_highbd_obmc_sub_pixel_variance64x64)
1685 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1686 aom_highbd_obmc_variance64x32,
1687 aom_highbd_obmc_sub_pixel_variance64x32)
1688 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1689 aom_highbd_obmc_variance32x64,
1690 aom_highbd_obmc_sub_pixel_variance32x64)
1691 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1692 aom_highbd_obmc_variance32x32,
1693 aom_highbd_obmc_sub_pixel_variance32x32)
1694 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1695 aom_highbd_obmc_variance32x16,
1696 aom_highbd_obmc_sub_pixel_variance32x16)
1697 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1698 aom_highbd_obmc_variance16x32,
1699 aom_highbd_obmc_sub_pixel_variance16x32)
1700 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1701 aom_highbd_obmc_variance16x16,
1702 aom_highbd_obmc_sub_pixel_variance16x16)
1703 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1704 aom_highbd_obmc_variance8x16,
1705 aom_highbd_obmc_sub_pixel_variance8x16)
1706 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1707 aom_highbd_obmc_variance16x8,
1708 aom_highbd_obmc_sub_pixel_variance16x8)
1709 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1710 aom_highbd_obmc_variance8x8,
1711 aom_highbd_obmc_sub_pixel_variance8x8)
1712 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1713 aom_highbd_obmc_variance4x8,
1714 aom_highbd_obmc_sub_pixel_variance4x8)
1715 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1716 aom_highbd_obmc_variance8x4,
1717 aom_highbd_obmc_sub_pixel_variance8x4)
1718 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1719 aom_highbd_obmc_variance4x4,
1720 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001721#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001722#if CONFIG_EXT_PARTITION
1723 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits8,
1724 aom_highbd_obmc_variance128x32,
1725 aom_highbd_obmc_sub_pixel_variance128x32)
1726
1727 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits8,
1728 aom_highbd_obmc_variance32x128,
1729 aom_highbd_obmc_sub_pixel_variance32x128)
1730#endif // CONFIG_EXT_PARTITION
1731
Rupert Swarbrick72678572017-08-02 12:05:26 +01001732 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1733 aom_highbd_obmc_variance64x16,
1734 aom_highbd_obmc_sub_pixel_variance64x16)
1735
1736 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1737 aom_highbd_obmc_variance16x64,
1738 aom_highbd_obmc_sub_pixel_variance16x64)
1739
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001740 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1741 aom_highbd_obmc_variance32x8,
1742 aom_highbd_obmc_sub_pixel_variance32x8)
1743
1744 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1745 aom_highbd_obmc_variance8x32,
1746 aom_highbd_obmc_sub_pixel_variance8x32)
1747
1748 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1749 aom_highbd_obmc_variance16x4,
1750 aom_highbd_obmc_sub_pixel_variance16x4)
1751
1752 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1753 aom_highbd_obmc_variance4x16,
1754 aom_highbd_obmc_sub_pixel_variance4x16)
1755#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001756 break;
1757
Yaowu Xuf883b422016-08-30 14:01:10 -07001758 case AOM_BITS_10:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001759#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001760#if CONFIG_EXT_PARTITION
1761 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits10,
1762 aom_highbd_sad128x32_avg_bits10,
1763 aom_highbd_10_variance128x32,
1764 aom_highbd_10_sub_pixel_variance128x32,
1765 aom_highbd_10_sub_pixel_avg_variance128x32, NULL, NULL,
1766 aom_highbd_sad128x32x4d_bits10)
1767
1768 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits10,
1769 aom_highbd_sad32x128_avg_bits10,
1770 aom_highbd_10_variance32x128,
1771 aom_highbd_10_sub_pixel_variance32x128,
1772 aom_highbd_10_sub_pixel_avg_variance32x128, NULL, NULL,
1773 aom_highbd_sad32x128x4d_bits10)
1774#endif // CONFIG_EXT_PARTITION
1775
Rupert Swarbrick72678572017-08-02 12:05:26 +01001776 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1777 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1778 aom_highbd_10_sub_pixel_variance64x16,
1779 aom_highbd_10_sub_pixel_avg_variance64x16, NULL, NULL,
1780 aom_highbd_sad64x16x4d_bits10)
1781
1782 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1783 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1784 aom_highbd_10_sub_pixel_variance16x64,
1785 aom_highbd_10_sub_pixel_avg_variance16x64, NULL, NULL,
1786 aom_highbd_sad16x64x4d_bits10)
1787
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001788 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1789 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1790 aom_highbd_10_sub_pixel_variance32x8,
1791 aom_highbd_10_sub_pixel_avg_variance32x8, NULL, NULL,
1792 aom_highbd_sad32x8x4d_bits10)
1793
1794 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1795 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1796 aom_highbd_10_sub_pixel_variance8x32,
1797 aom_highbd_10_sub_pixel_avg_variance8x32, NULL, NULL,
1798 aom_highbd_sad8x32x4d_bits10)
1799
1800 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1801 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1802 aom_highbd_10_sub_pixel_variance16x4,
1803 aom_highbd_10_sub_pixel_avg_variance16x4, NULL, NULL,
1804 aom_highbd_sad16x4x4d_bits10)
1805
1806 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1807 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1808 aom_highbd_10_sub_pixel_variance4x16,
1809 aom_highbd_10_sub_pixel_avg_variance4x16, NULL, NULL,
1810 aom_highbd_sad4x16x4d_bits10)
1811#endif
1812
Yaowu Xuf883b422016-08-30 14:01:10 -07001813 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1814 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1815 aom_highbd_10_sub_pixel_variance32x16,
1816 aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1817 aom_highbd_sad32x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001818
Yaowu Xuf883b422016-08-30 14:01:10 -07001819 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1820 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1821 aom_highbd_10_sub_pixel_variance16x32,
1822 aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1823 aom_highbd_sad16x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001824
Yaowu Xuf883b422016-08-30 14:01:10 -07001825 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1826 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1827 aom_highbd_10_sub_pixel_variance64x32,
1828 aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1829 aom_highbd_sad64x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001830
Yaowu Xuf883b422016-08-30 14:01:10 -07001831 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1832 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1833 aom_highbd_10_sub_pixel_variance32x64,
1834 aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1835 aom_highbd_sad32x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001836
Yaowu Xuf883b422016-08-30 14:01:10 -07001837 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1838 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1839 aom_highbd_10_sub_pixel_variance32x32,
1840 aom_highbd_10_sub_pixel_avg_variance32x32,
1841 aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
1842 aom_highbd_sad32x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001843
Yaowu Xuf883b422016-08-30 14:01:10 -07001844 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1845 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1846 aom_highbd_10_sub_pixel_variance64x64,
1847 aom_highbd_10_sub_pixel_avg_variance64x64,
1848 aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
1849 aom_highbd_sad64x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001850
Yaowu Xuf883b422016-08-30 14:01:10 -07001851 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1852 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1853 aom_highbd_10_sub_pixel_variance16x16,
1854 aom_highbd_10_sub_pixel_avg_variance16x16,
1855 aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
1856 aom_highbd_sad16x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001857
Yaowu Xuf883b422016-08-30 14:01:10 -07001858 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1859 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1860 aom_highbd_10_sub_pixel_variance16x8,
1861 aom_highbd_10_sub_pixel_avg_variance16x8,
1862 aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
1863 aom_highbd_sad16x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001864
Yaowu Xuf883b422016-08-30 14:01:10 -07001865 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1866 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1867 aom_highbd_10_sub_pixel_variance8x16,
1868 aom_highbd_10_sub_pixel_avg_variance8x16,
1869 aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
1870 aom_highbd_sad8x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001871
1872 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001873 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1874 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1875 aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
1876 aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001877
Yaowu Xuf883b422016-08-30 14:01:10 -07001878 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
1879 aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
1880 aom_highbd_10_sub_pixel_variance8x4,
1881 aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
1882 aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001883
Yaowu Xuf883b422016-08-30 14:01:10 -07001884 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
1885 aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
1886 aom_highbd_10_sub_pixel_variance4x8,
1887 aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
1888 aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001889
1890 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001891 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1892 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1893 aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
1894 aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001895
Jingning Hancc5bdf42016-12-19 11:14:30 -08001896 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_10_variance2x2, NULL, NULL,
1897 NULL, NULL, NULL)
1898 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_10_variance4x2, NULL, NULL,
1899 NULL, NULL, NULL)
1900 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_10_variance2x4, NULL, NULL,
1901 NULL, NULL, NULL)
Jingning Hancc5bdf42016-12-19 11:14:30 -08001902
Yaowu Xuc27fc142016-08-22 16:08:15 -07001903#if CONFIG_EXT_PARTITION
1904 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001905 BLOCK_128X128, aom_highbd_sad128x128_bits10,
1906 aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
1907 aom_highbd_10_sub_pixel_variance128x128,
1908 aom_highbd_10_sub_pixel_avg_variance128x128,
1909 aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
1910 aom_highbd_sad128x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001911
Yaowu Xuf883b422016-08-30 14:01:10 -07001912 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1913 aom_highbd_sad128x64_avg_bits10,
1914 aom_highbd_10_variance128x64,
1915 aom_highbd_10_sub_pixel_variance128x64,
1916 aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
1917 aom_highbd_sad128x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001918
Yaowu Xuf883b422016-08-30 14:01:10 -07001919 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1920 aom_highbd_sad64x128_avg_bits10,
1921 aom_highbd_10_variance64x128,
1922 aom_highbd_10_sub_pixel_variance64x128,
1923 aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
1924 aom_highbd_sad64x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001925#endif // CONFIG_EXT_PARTITION
1926
Yaowu Xuc27fc142016-08-22 16:08:15 -07001927#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001928 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1929 aom_highbd_10_masked_sub_pixel_variance128x128)
1930 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1931 aom_highbd_10_masked_sub_pixel_variance128x64)
1932 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1933 aom_highbd_10_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001934#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001935 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1936 aom_highbd_10_masked_sub_pixel_variance64x64)
1937 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1938 aom_highbd_10_masked_sub_pixel_variance64x32)
1939 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1940 aom_highbd_10_masked_sub_pixel_variance32x64)
1941 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1942 aom_highbd_10_masked_sub_pixel_variance32x32)
1943 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1944 aom_highbd_10_masked_sub_pixel_variance32x16)
1945 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1946 aom_highbd_10_masked_sub_pixel_variance16x32)
1947 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1948 aom_highbd_10_masked_sub_pixel_variance16x16)
1949 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1950 aom_highbd_10_masked_sub_pixel_variance8x16)
1951 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1952 aom_highbd_10_masked_sub_pixel_variance16x8)
1953 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1954 aom_highbd_10_masked_sub_pixel_variance8x8)
1955 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1956 aom_highbd_10_masked_sub_pixel_variance4x8)
1957 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1958 aom_highbd_10_masked_sub_pixel_variance8x4)
1959 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1960 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001961#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001962#if CONFIG_EXT_PARTITION
1963 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits10,
1964 aom_highbd_10_masked_sub_pixel_variance128x32)
1965
1966 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits10,
1967 aom_highbd_10_masked_sub_pixel_variance32x128)
1968#endif // CONFIG_EXT_PARTITION
1969
Rupert Swarbrick72678572017-08-02 12:05:26 +01001970 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1971 aom_highbd_10_masked_sub_pixel_variance64x16)
1972
1973 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1974 aom_highbd_10_masked_sub_pixel_variance16x64)
1975
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001976 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1977 aom_highbd_10_masked_sub_pixel_variance32x8)
1978
1979 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1980 aom_highbd_10_masked_sub_pixel_variance8x32)
1981
1982 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1983 aom_highbd_10_masked_sub_pixel_variance16x4)
1984
1985 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1986 aom_highbd_10_masked_sub_pixel_variance4x16)
1987#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001989 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1990 aom_highbd_10_obmc_variance128x128,
1991 aom_highbd_10_obmc_sub_pixel_variance128x128)
1992 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1993 aom_highbd_10_obmc_variance128x64,
1994 aom_highbd_10_obmc_sub_pixel_variance128x64)
1995 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1996 aom_highbd_10_obmc_variance64x128,
1997 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001998#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001999 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2000 aom_highbd_10_obmc_variance64x64,
2001 aom_highbd_10_obmc_sub_pixel_variance64x64)
2002 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2003 aom_highbd_10_obmc_variance64x32,
2004 aom_highbd_10_obmc_sub_pixel_variance64x32)
2005 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2006 aom_highbd_10_obmc_variance32x64,
2007 aom_highbd_10_obmc_sub_pixel_variance32x64)
2008 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2009 aom_highbd_10_obmc_variance32x32,
2010 aom_highbd_10_obmc_sub_pixel_variance32x32)
2011 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2012 aom_highbd_10_obmc_variance32x16,
2013 aom_highbd_10_obmc_sub_pixel_variance32x16)
2014 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2015 aom_highbd_10_obmc_variance16x32,
2016 aom_highbd_10_obmc_sub_pixel_variance16x32)
2017 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2018 aom_highbd_10_obmc_variance16x16,
2019 aom_highbd_10_obmc_sub_pixel_variance16x16)
2020 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2021 aom_highbd_10_obmc_variance8x16,
2022 aom_highbd_10_obmc_sub_pixel_variance8x16)
2023 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2024 aom_highbd_10_obmc_variance16x8,
2025 aom_highbd_10_obmc_sub_pixel_variance16x8)
2026 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2027 aom_highbd_10_obmc_variance8x8,
2028 aom_highbd_10_obmc_sub_pixel_variance8x8)
2029 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2030 aom_highbd_10_obmc_variance4x8,
2031 aom_highbd_10_obmc_sub_pixel_variance4x8)
2032 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2033 aom_highbd_10_obmc_variance8x4,
2034 aom_highbd_10_obmc_sub_pixel_variance8x4)
2035 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2036 aom_highbd_10_obmc_variance4x4,
2037 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002038#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002039#if CONFIG_EXT_PARTITION
2040 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits10,
2041 aom_highbd_10_obmc_variance128x32,
2042 aom_highbd_10_obmc_sub_pixel_variance128x32)
2043
2044 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits10,
2045 aom_highbd_10_obmc_variance32x128,
2046 aom_highbd_10_obmc_sub_pixel_variance32x128)
2047#endif // CONFIG_EXT_PARTITION
2048
Rupert Swarbrick72678572017-08-02 12:05:26 +01002049 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2050 aom_highbd_10_obmc_variance64x16,
2051 aom_highbd_10_obmc_sub_pixel_variance64x16)
2052
2053 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2054 aom_highbd_10_obmc_variance16x64,
2055 aom_highbd_10_obmc_sub_pixel_variance16x64)
2056
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002057 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2058 aom_highbd_10_obmc_variance32x8,
2059 aom_highbd_10_obmc_sub_pixel_variance32x8)
2060
2061 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2062 aom_highbd_10_obmc_variance8x32,
2063 aom_highbd_10_obmc_sub_pixel_variance8x32)
2064
2065 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2066 aom_highbd_10_obmc_variance16x4,
2067 aom_highbd_10_obmc_sub_pixel_variance16x4)
2068
2069 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2070 aom_highbd_10_obmc_variance4x16,
2071 aom_highbd_10_obmc_sub_pixel_variance4x16)
2072#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002073 break;
2074
Yaowu Xuf883b422016-08-30 14:01:10 -07002075 case AOM_BITS_12:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002076#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002077#if CONFIG_EXT_PARTITION
2078 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits12,
2079 aom_highbd_sad128x32_avg_bits12,
2080 aom_highbd_12_variance128x32,
2081 aom_highbd_12_sub_pixel_variance128x32,
2082 aom_highbd_12_sub_pixel_avg_variance128x32, NULL, NULL,
2083 aom_highbd_sad128x32x4d_bits12)
2084
2085 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits12,
2086 aom_highbd_sad32x128_avg_bits12,
2087 aom_highbd_12_variance32x128,
2088 aom_highbd_12_sub_pixel_variance32x128,
2089 aom_highbd_12_sub_pixel_avg_variance32x128, NULL, NULL,
2090 aom_highbd_sad32x128x4d_bits12)
2091#endif // CONFIG_EXT_PARTITION
2092
Rupert Swarbrick72678572017-08-02 12:05:26 +01002093 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2094 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2095 aom_highbd_12_sub_pixel_variance64x16,
2096 aom_highbd_12_sub_pixel_avg_variance64x16, NULL, NULL,
2097 aom_highbd_sad64x16x4d_bits12)
2098
2099 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2100 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2101 aom_highbd_12_sub_pixel_variance16x64,
2102 aom_highbd_12_sub_pixel_avg_variance16x64, NULL, NULL,
2103 aom_highbd_sad16x64x4d_bits12)
2104
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002105 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2106 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2107 aom_highbd_12_sub_pixel_variance32x8,
2108 aom_highbd_12_sub_pixel_avg_variance32x8, NULL, NULL,
2109 aom_highbd_sad32x8x4d_bits12)
2110
2111 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2112 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2113 aom_highbd_12_sub_pixel_variance8x32,
2114 aom_highbd_12_sub_pixel_avg_variance8x32, NULL, NULL,
2115 aom_highbd_sad8x32x4d_bits12)
2116
2117 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2118 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2119 aom_highbd_12_sub_pixel_variance16x4,
2120 aom_highbd_12_sub_pixel_avg_variance16x4, NULL, NULL,
2121 aom_highbd_sad16x4x4d_bits12)
2122
2123 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2124 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2125 aom_highbd_12_sub_pixel_variance4x16,
2126 aom_highbd_12_sub_pixel_avg_variance4x16, NULL, NULL,
2127 aom_highbd_sad4x16x4d_bits12)
2128#endif
2129
Yaowu Xuf883b422016-08-30 14:01:10 -07002130 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2131 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2132 aom_highbd_12_sub_pixel_variance32x16,
2133 aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
2134 aom_highbd_sad32x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002135
Yaowu Xuf883b422016-08-30 14:01:10 -07002136 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2137 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2138 aom_highbd_12_sub_pixel_variance16x32,
2139 aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
2140 aom_highbd_sad16x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002141
Yaowu Xuf883b422016-08-30 14:01:10 -07002142 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2143 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2144 aom_highbd_12_sub_pixel_variance64x32,
2145 aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
2146 aom_highbd_sad64x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002147
Yaowu Xuf883b422016-08-30 14:01:10 -07002148 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2149 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2150 aom_highbd_12_sub_pixel_variance32x64,
2151 aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
2152 aom_highbd_sad32x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002153
Yaowu Xuf883b422016-08-30 14:01:10 -07002154 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2155 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2156 aom_highbd_12_sub_pixel_variance32x32,
2157 aom_highbd_12_sub_pixel_avg_variance32x32,
2158 aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
2159 aom_highbd_sad32x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002160
Yaowu Xuf883b422016-08-30 14:01:10 -07002161 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2162 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2163 aom_highbd_12_sub_pixel_variance64x64,
2164 aom_highbd_12_sub_pixel_avg_variance64x64,
2165 aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
2166 aom_highbd_sad64x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002167
Yaowu Xuf883b422016-08-30 14:01:10 -07002168 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2169 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2170 aom_highbd_12_sub_pixel_variance16x16,
2171 aom_highbd_12_sub_pixel_avg_variance16x16,
2172 aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
2173 aom_highbd_sad16x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002174
Yaowu Xuf883b422016-08-30 14:01:10 -07002175 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2176 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2177 aom_highbd_12_sub_pixel_variance16x8,
2178 aom_highbd_12_sub_pixel_avg_variance16x8,
2179 aom_highbd_sad16x8x3_bits12, aom_highbd_sad16x8x8_bits12,
2180 aom_highbd_sad16x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002181
Yaowu Xuf883b422016-08-30 14:01:10 -07002182 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2183 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2184 aom_highbd_12_sub_pixel_variance8x16,
2185 aom_highbd_12_sub_pixel_avg_variance8x16,
2186 aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
2187 aom_highbd_sad8x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002188
2189 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002190 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2191 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
2192 aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
2193 aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002194
Yaowu Xuf883b422016-08-30 14:01:10 -07002195 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
2196 aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
2197 aom_highbd_12_sub_pixel_variance8x4,
2198 aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
2199 aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002200
Yaowu Xuf883b422016-08-30 14:01:10 -07002201 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
2202 aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
2203 aom_highbd_12_sub_pixel_variance4x8,
2204 aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
2205 aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002206
2207 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002208 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2209 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
2210 aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
2211 aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002212
Jingning Hancc5bdf42016-12-19 11:14:30 -08002213 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_12_variance2x2, NULL, NULL,
2214 NULL, NULL, NULL)
2215 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_12_variance4x2, NULL, NULL,
2216 NULL, NULL, NULL)
2217 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_12_variance2x4, NULL, NULL,
2218 NULL, NULL, NULL)
Jingning Hancc5bdf42016-12-19 11:14:30 -08002219
Yaowu Xuc27fc142016-08-22 16:08:15 -07002220#if CONFIG_EXT_PARTITION
2221 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002222 BLOCK_128X128, aom_highbd_sad128x128_bits12,
2223 aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
2224 aom_highbd_12_sub_pixel_variance128x128,
2225 aom_highbd_12_sub_pixel_avg_variance128x128,
2226 aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
2227 aom_highbd_sad128x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002228
Yaowu Xuf883b422016-08-30 14:01:10 -07002229 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2230 aom_highbd_sad128x64_avg_bits12,
2231 aom_highbd_12_variance128x64,
2232 aom_highbd_12_sub_pixel_variance128x64,
2233 aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
2234 aom_highbd_sad128x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002235
Yaowu Xuf883b422016-08-30 14:01:10 -07002236 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2237 aom_highbd_sad64x128_avg_bits12,
2238 aom_highbd_12_variance64x128,
2239 aom_highbd_12_sub_pixel_variance64x128,
2240 aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
2241 aom_highbd_sad64x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002242#endif // CONFIG_EXT_PARTITION
2243
Yaowu Xuc27fc142016-08-22 16:08:15 -07002244#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002245 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2246 aom_highbd_12_masked_sub_pixel_variance128x128)
2247 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2248 aom_highbd_12_masked_sub_pixel_variance128x64)
2249 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2250 aom_highbd_12_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002251#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002252 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2253 aom_highbd_12_masked_sub_pixel_variance64x64)
2254 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2255 aom_highbd_12_masked_sub_pixel_variance64x32)
2256 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2257 aom_highbd_12_masked_sub_pixel_variance32x64)
2258 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2259 aom_highbd_12_masked_sub_pixel_variance32x32)
2260 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2261 aom_highbd_12_masked_sub_pixel_variance32x16)
2262 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2263 aom_highbd_12_masked_sub_pixel_variance16x32)
2264 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2265 aom_highbd_12_masked_sub_pixel_variance16x16)
2266 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2267 aom_highbd_12_masked_sub_pixel_variance8x16)
2268 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2269 aom_highbd_12_masked_sub_pixel_variance16x8)
2270 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2271 aom_highbd_12_masked_sub_pixel_variance8x8)
2272 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2273 aom_highbd_12_masked_sub_pixel_variance4x8)
2274 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2275 aom_highbd_12_masked_sub_pixel_variance8x4)
2276 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2277 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002278#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002279#if CONFIG_EXT_PARTITION
2280 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits12,
2281 aom_highbd_12_masked_sub_pixel_variance128x32)
2282
2283 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits12,
2284 aom_highbd_12_masked_sub_pixel_variance32x128)
2285#endif // CONFIG_EXT_PARTITION
2286
Rupert Swarbrick72678572017-08-02 12:05:26 +01002287 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2288 aom_highbd_12_masked_sub_pixel_variance64x16)
2289
2290 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2291 aom_highbd_12_masked_sub_pixel_variance16x64)
2292
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002293 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2294 aom_highbd_12_masked_sub_pixel_variance32x8)
2295
2296 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2297 aom_highbd_12_masked_sub_pixel_variance8x32)
2298
2299 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2300 aom_highbd_12_masked_sub_pixel_variance16x4)
2301
2302 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2303 aom_highbd_12_masked_sub_pixel_variance4x16)
2304#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002305
Yaowu Xuc27fc142016-08-22 16:08:15 -07002306#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002307 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2308 aom_highbd_12_obmc_variance128x128,
2309 aom_highbd_12_obmc_sub_pixel_variance128x128)
2310 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2311 aom_highbd_12_obmc_variance128x64,
2312 aom_highbd_12_obmc_sub_pixel_variance128x64)
2313 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2314 aom_highbd_12_obmc_variance64x128,
2315 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002316#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002317 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2318 aom_highbd_12_obmc_variance64x64,
2319 aom_highbd_12_obmc_sub_pixel_variance64x64)
2320 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2321 aom_highbd_12_obmc_variance64x32,
2322 aom_highbd_12_obmc_sub_pixel_variance64x32)
2323 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2324 aom_highbd_12_obmc_variance32x64,
2325 aom_highbd_12_obmc_sub_pixel_variance32x64)
2326 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2327 aom_highbd_12_obmc_variance32x32,
2328 aom_highbd_12_obmc_sub_pixel_variance32x32)
2329 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2330 aom_highbd_12_obmc_variance32x16,
2331 aom_highbd_12_obmc_sub_pixel_variance32x16)
2332 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2333 aom_highbd_12_obmc_variance16x32,
2334 aom_highbd_12_obmc_sub_pixel_variance16x32)
2335 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2336 aom_highbd_12_obmc_variance16x16,
2337 aom_highbd_12_obmc_sub_pixel_variance16x16)
2338 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2339 aom_highbd_12_obmc_variance8x16,
2340 aom_highbd_12_obmc_sub_pixel_variance8x16)
2341 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2342 aom_highbd_12_obmc_variance16x8,
2343 aom_highbd_12_obmc_sub_pixel_variance16x8)
2344 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2345 aom_highbd_12_obmc_variance8x8,
2346 aom_highbd_12_obmc_sub_pixel_variance8x8)
2347 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2348 aom_highbd_12_obmc_variance4x8,
2349 aom_highbd_12_obmc_sub_pixel_variance4x8)
2350 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2351 aom_highbd_12_obmc_variance8x4,
2352 aom_highbd_12_obmc_sub_pixel_variance8x4)
2353 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2354 aom_highbd_12_obmc_variance4x4,
2355 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002356#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002357#if CONFIG_EXT_PARTITION
2358 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits12,
2359 aom_highbd_12_obmc_variance128x32,
2360 aom_highbd_12_obmc_sub_pixel_variance128x32)
2361
2362 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits12,
2363 aom_highbd_12_obmc_variance32x128,
2364 aom_highbd_12_obmc_sub_pixel_variance32x128)
2365#endif // CONFIG_EXT_PARTITION
2366
Rupert Swarbrick72678572017-08-02 12:05:26 +01002367 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2368 aom_highbd_12_obmc_variance64x16,
2369 aom_highbd_12_obmc_sub_pixel_variance64x16)
2370
2371 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2372 aom_highbd_12_obmc_variance16x64,
2373 aom_highbd_12_obmc_sub_pixel_variance16x64)
2374
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002375 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2376 aom_highbd_12_obmc_variance32x8,
2377 aom_highbd_12_obmc_sub_pixel_variance32x8)
2378
2379 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2380 aom_highbd_12_obmc_variance8x32,
2381 aom_highbd_12_obmc_sub_pixel_variance8x32)
2382
2383 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2384 aom_highbd_12_obmc_variance16x4,
2385 aom_highbd_12_obmc_sub_pixel_variance16x4)
2386
2387 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2388 aom_highbd_12_obmc_variance4x16,
2389 aom_highbd_12_obmc_sub_pixel_variance4x16)
2390#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002391 break;
2392
2393 default:
2394 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07002395 "cm->bit_depth should be AOM_BITS_8, "
2396 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397 }
2398 }
2399}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002400#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002401
Yaowu Xuf883b422016-08-30 14:01:10 -07002402static void realloc_segmentation_maps(AV1_COMP *cpi) {
2403 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002404
2405 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002406 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002407 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002408 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002409
2410 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002411 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002413 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002414
2415 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002416 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002417 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002418 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002419}
2420
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002421void set_compound_tools(AV1_COMMON *cm) {
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002422 cm->allow_interintra_compound = 1;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002423 cm->allow_masked_compound = 1;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002424}
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002425
Yaowu Xuf883b422016-08-30 14:01:10 -07002426void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2427 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002428 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002429 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002430
2431 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
2432 cm->bit_depth = oxcf->bit_depth;
2433 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07002434#if CONFIG_COLORSPACE_HEADERS
2435 cm->transfer_function = oxcf->transfer_function;
2436 cm->chroma_sample_position = oxcf->chroma_sample_position;
2437#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002438 cm->color_range = oxcf->color_range;
2439
2440 if (cm->profile <= PROFILE_1)
Yaowu Xuf883b422016-08-30 14:01:10 -07002441 assert(cm->bit_depth == AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002442 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002443 assert(cm->bit_depth > AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002444
2445 cpi->oxcf = *oxcf;
hui sud9a812b2017-07-06 14:34:37 -07002446 x->e_mbd.bd = (int)cm->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002447 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002448
Yaowu Xuf883b422016-08-30 14:01:10 -07002449 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002450 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2451 } else {
2452 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2453 }
2454
2455 cpi->refresh_last_frame = 1;
2456 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002457 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002458 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002459
2460 cm->refresh_frame_context =
2461 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
2462 ? REFRESH_FRAME_CONTEXT_FORWARD
2463 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002464#if CONFIG_EXT_TILE
2465 if (oxcf->large_scale_tile)
2466 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
2467#endif // CONFIG_EXT_TILE
2468
Thomas Daedea6a854b2017-06-22 17:49:11 -07002469#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07002470 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07002471#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002472
Alex Converse74ad0912017-07-18 10:22:58 -07002473 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002474 CHECK_MEM_ERROR(cm, x->palette_buffer,
2475 aom_memalign(16, sizeof(*x->palette_buffer)));
2476 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002477 set_compound_tools(cm);
Yaowu Xuf883b422016-08-30 14:01:10 -07002478 av1_reset_segment_features(cm);
RogerZhou3b635242017-09-19 10:06:46 -07002479#if CONFIG_AMVR
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002480 set_high_precision_mv(cpi, 1, 0);
RogerZhou3b635242017-09-19 10:06:46 -07002481#else
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002482 set_high_precision_mv(cpi, 1);
RogerZhou3b635242017-09-19 10:06:46 -07002483#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002484
Yaowu Xuc27fc142016-08-22 16:08:15 -07002485 set_rc_buffer_sizes(rc, &cpi->oxcf);
2486
2487 // Under a configuration change, where maximum_buffer_size may change,
2488 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002489 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2490 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491
2492 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002493 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002494
2495 // Set absolute upper and lower quality limits
2496 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2497 rc->best_quality = cpi->oxcf.best_allowed_q;
2498
2499 cm->interp_filter = cpi->sf.default_interp_filter;
2500
2501 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2502 cm->render_width = cpi->oxcf.render_width;
2503 cm->render_height = cpi->oxcf.render_height;
2504 } else {
2505 cm->render_width = cpi->oxcf.width;
2506 cm->render_height = cpi->oxcf.height;
2507 }
2508 cm->width = cpi->oxcf.width;
2509 cm->height = cpi->oxcf.height;
2510
2511 if (cpi->initial_width) {
2512 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002513 av1_free_context_buffers(cm);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07002514 av1_free_pc_tree(&cpi->td);
Cheng Chen46f30c72017-09-07 11:13:33 -07002515 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002516 realloc_segmentation_maps(cpi);
2517 cpi->initial_width = cpi->initial_height = 0;
2518 }
2519 }
2520 update_frame_size(cpi);
2521
2522 cpi->alt_ref_source = NULL;
2523 rc->is_src_frame_alt_ref = 0;
2524
Yaowu Xuc27fc142016-08-22 16:08:15 -07002525 rc->is_bwd_ref_frame = 0;
2526 rc->is_last_bipred_frame = 0;
2527 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002528
2529#if 0
2530 // Experimental RD Code
2531 cpi->frame_distortion = 0;
2532 cpi->last_frame_distortion = 0;
2533#endif
2534
2535 set_tile_info(cpi);
2536
2537 cpi->ext_refresh_frame_flags_pending = 0;
2538 cpi->ext_refresh_frame_context_pending = 0;
2539
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002540#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002541 highbd_set_var_fns(cpi);
2542#endif
Alex Converseeb780e72016-12-13 12:46:41 -08002543#if CONFIG_ANS && ANS_MAX_SYMBOLS
2544 cpi->common.ans_window_size_log2 = cpi->oxcf.ans_window_size_log2;
Alex Converseeb780e72016-12-13 12:46:41 -08002545#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
RogerZhou3b635242017-09-19 10:06:46 -07002546#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07002547 cm->seq_force_integer_mv = 2;
RogerZhou3b635242017-09-19 10:06:46 -07002548#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002549}
2550
Yaowu Xuf883b422016-08-30 14:01:10 -07002551AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2552 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002553 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002554 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2555 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002556
2557 if (!cm) return NULL;
2558
Yaowu Xuf883b422016-08-30 14:01:10 -07002559 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002560
2561 if (setjmp(cm->error.jmp)) {
2562 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002563 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002564 return 0;
2565 }
2566
2567 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002568 cm->alloc_mi = enc_alloc_mi;
2569 cm->free_mi = enc_free_mi;
2570 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002571
Angie Chianga5d96c42016-10-21 16:16:56 -07002572 CHECK_MEM_ERROR(cm, cm->fc,
2573 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2574 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2575 (FRAME_CONTEXT *)aom_memalign(
2576 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2577 memset(cm->fc, 0, sizeof(*cm->fc));
2578 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002579
2580 cpi->resize_state = 0;
2581 cpi->resize_avg_qp = 0;
2582 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002583
Yaowu Xuc27fc142016-08-22 16:08:15 -07002584 cpi->common.buffer_pool = pool;
2585
2586 init_config(cpi, oxcf);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002587#if CONFIG_XIPHRC
2588 cpi->od_rc.framerate = cpi->framerate;
2589 cpi->od_rc.frame_width = cm->render_width;
2590 cpi->od_rc.frame_height = cm->render_height;
2591 cpi->od_rc.keyframe_rate = oxcf->key_freq;
2592 cpi->od_rc.goldenframe_rate = FIXED_GF_INTERVAL;
2593 cpi->od_rc.altref_rate = 25;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002594 cpi->od_rc.firstpass_quant = 1;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002595 cpi->od_rc.bit_depth = cm->bit_depth;
2596 cpi->od_rc.minq = oxcf->best_allowed_q;
2597 cpi->od_rc.maxq = oxcf->worst_allowed_q;
2598 if (cpi->oxcf.rc_mode == AOM_CQ) cpi->od_rc.minq = cpi->od_rc.quality;
2599 cpi->od_rc.quality = cpi->oxcf.rc_mode == AOM_Q ? oxcf->cq_level : -1;
2600 cpi->od_rc.periodic_boosts = oxcf->frame_periodic_boost;
2601 od_enc_rc_init(&cpi->od_rc,
2602 cpi->oxcf.rc_mode == AOM_Q ? -1 : oxcf->target_bandwidth,
2603 oxcf->maximum_buffer_size_ms);
2604#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002605 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002606#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002607
2608 cm->current_video_frame = 0;
2609 cpi->partition_search_skippable_frame = 0;
2610 cpi->tile_data = NULL;
2611 cpi->last_show_frame_buf_idx = INVALID_IDX;
2612
2613 realloc_segmentation_maps(cpi);
2614
James Zern01a9d702017-08-25 19:09:33 +00002615 for (i = 0; i < NMV_CONTEXTS; ++i) {
2616 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2617 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
2618 }
2619
Yaowu Xuc27fc142016-08-22 16:08:15 -07002620 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2621 i++) {
2622 CHECK_MEM_ERROR(
2623 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002624 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002625 }
2626
2627#if CONFIG_FP_MB_STATS
2628 cpi->use_fp_mb_stats = 0;
2629 if (cpi->use_fp_mb_stats) {
2630 // a place holder used to store the first pass mb stats in the first pass
2631 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002632 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002633 } else {
2634 cpi->twopass.frame_mb_stats_buf = NULL;
2635 }
2636#endif
2637
2638 cpi->refresh_alt_ref_frame = 0;
2639 cpi->multi_arf_last_grp_enabled = 0;
2640
2641 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2642#if CONFIG_INTERNAL_STATS
2643 cpi->b_calculate_blockiness = 1;
2644 cpi->b_calculate_consistency = 1;
2645 cpi->total_inconsistency = 0;
2646 cpi->psnr.worst = 100.0;
2647 cpi->worst_ssim = 100.0;
2648
2649 cpi->count = 0;
2650 cpi->bytes = 0;
2651
2652 if (cpi->b_calculate_psnr) {
2653 cpi->total_sq_error = 0;
2654 cpi->total_samples = 0;
2655 cpi->tot_recode_hits = 0;
2656 cpi->summed_quality = 0;
2657 cpi->summed_weights = 0;
2658 }
2659
2660 cpi->fastssim.worst = 100.0;
2661 cpi->psnrhvs.worst = 100.0;
2662
2663 if (cpi->b_calculate_blockiness) {
2664 cpi->total_blockiness = 0;
2665 cpi->worst_blockiness = 0.0;
2666 }
2667
2668 if (cpi->b_calculate_consistency) {
2669 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002670 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002671 cpi->common.mi_rows * cpi->common.mi_cols));
2672 cpi->worst_consistency = 100.0;
2673 }
2674#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002675#if CONFIG_ENTROPY_STATS
2676 av1_zero(aggregate_fc);
Zoe Liua56f9162017-06-21 22:49:57 -07002677 av1_zero_array(aggregate_fc_per_type, FRAME_CONTEXTS);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002678#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002679
2680 cpi->first_time_stamp_ever = INT64_MAX;
2681
James Zern01a9d702017-08-25 19:09:33 +00002682 for (i = 0; i < NMV_CONTEXTS; ++i) {
2683 cpi->td.mb.nmvcost[i][0] = &cpi->nmv_costs[i][0][MV_MAX];
2684 cpi->td.mb.nmvcost[i][1] = &cpi->nmv_costs[i][1][MV_MAX];
2685 cpi->td.mb.nmvcost_hp[i][0] = &cpi->nmv_costs_hp[i][0][MV_MAX];
2686 cpi->td.mb.nmvcost_hp[i][1] = &cpi->nmv_costs_hp[i][1][MV_MAX];
2687 }
2688
Yaowu Xuc27fc142016-08-22 16:08:15 -07002689#ifdef OUTPUT_YUV_SKINMAP
2690 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2691#endif
2692#ifdef OUTPUT_YUV_REC
2693 yuv_rec_file = fopen("rec.yuv", "wb");
2694#endif
2695
2696#if 0
2697 framepsnr = fopen("framepsnr.stt", "a");
2698 kf_list = fopen("kf_list.stt", "w");
2699#endif
2700
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002701#if CONFIG_XIPHRC
2702 if (oxcf->pass == 2) {
2703 cpi->od_rc.twopass_allframes_buf = oxcf->two_pass_stats_in.buf;
2704 cpi->od_rc.twopass_allframes_buf_size = oxcf->two_pass_stats_in.sz;
2705 }
2706#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002707 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002708 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002709 } else if (oxcf->pass == 2) {
2710 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2711 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2712
2713#if CONFIG_FP_MB_STATS
2714 if (cpi->use_fp_mb_stats) {
2715 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2716 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2717
2718 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2719 oxcf->firstpass_mb_stats_in.buf;
2720 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2721 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2722 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2723 }
2724#endif
2725
2726 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2727 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2728 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2729
Yaowu Xuf883b422016-08-30 14:01:10 -07002730 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002731 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002732#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002733
Jingning Hand064cf02017-06-01 10:00:39 -07002734#if CONFIG_HIGHBITDEPTH
2735 int buf_scaler = 2;
2736#else
2737 int buf_scaler = 1;
2738#endif
2739 CHECK_MEM_ERROR(
2740 cm, cpi->td.mb.above_pred_buf,
clang-format4eafefe2017-09-04 12:51:20 -07002741 (uint8_t *)aom_memalign(16,
2742 buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2743 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002744 CHECK_MEM_ERROR(
2745 cm, cpi->td.mb.left_pred_buf,
clang-format4eafefe2017-09-04 12:51:20 -07002746 (uint8_t *)aom_memalign(16,
2747 buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2748 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002749
2750 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2751 (int32_t *)aom_memalign(
2752 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2753
2754 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2755 (int32_t *)aom_memalign(
2756 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2757
Yaowu Xuf883b422016-08-30 14:01:10 -07002758 av1_set_speed_features_framesize_independent(cpi);
2759 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002760
Yaowu Xuc27fc142016-08-22 16:08:15 -07002761#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
2762 cpi->fn_ptr[BT].sdf = SDF; \
2763 cpi->fn_ptr[BT].sdaf = SDAF; \
2764 cpi->fn_ptr[BT].vf = VF; \
2765 cpi->fn_ptr[BT].svf = SVF; \
2766 cpi->fn_ptr[BT].svaf = SVAF; \
2767 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2768 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2769 cpi->fn_ptr[BT].sdx4df = SDX4DF;
2770
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002771#if CONFIG_EXT_PARTITION_TYPES
2772 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2773 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, NULL, NULL,
2774 aom_sad4x16x4d)
2775
2776 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2777 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, NULL, NULL,
2778 aom_sad16x4x4d)
2779
2780 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2781 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, NULL, NULL,
2782 aom_sad8x32x4d)
2783
2784 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2785 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, NULL, NULL,
2786 aom_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002787
2788 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2789 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, NULL, NULL,
2790 aom_sad16x64x4d)
2791
2792 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2793 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, NULL, NULL,
2794 aom_sad64x16x4d)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002795
2796#if CONFIG_EXT_PARTITION
2797 BFP(BLOCK_32X128, aom_sad32x128, aom_sad32x128_avg, aom_variance32x128,
2798 aom_sub_pixel_variance32x128, aom_sub_pixel_avg_variance32x128, NULL,
2799 NULL, aom_sad32x128x4d)
2800
2801 BFP(BLOCK_128X32, aom_sad128x32, aom_sad128x32_avg, aom_variance128x32,
2802 aom_sub_pixel_variance128x32, aom_sub_pixel_avg_variance128x32, NULL,
2803 NULL, aom_sad128x32x4d)
2804#endif // CONFIG_EXT_PARTITION
2805#endif // CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002806
Yaowu Xuc27fc142016-08-22 16:08:15 -07002807#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002808 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2809 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2810 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002811
Yaowu Xuf883b422016-08-30 14:01:10 -07002812 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2813 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
2814 NULL, aom_sad128x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002815
Yaowu Xuf883b422016-08-30 14:01:10 -07002816 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2817 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
2818 NULL, aom_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002819#endif // CONFIG_EXT_PARTITION
2820
Cheng Chenefc55fd2017-10-10 12:08:28 -07002821#if CONFIG_JNT_COMP
2822 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg_c, aom_variance32x16,
2823 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2824 aom_sad32x16x4d)
2825
2826 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg_c, aom_variance16x32,
2827 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2828 aom_sad16x32x4d)
2829
2830 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg_c, aom_variance64x32,
2831 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2832 aom_sad64x32x4d)
2833
2834 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg_c, aom_variance32x64,
2835 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2836 aom_sad32x64x4d)
2837
2838 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg_c, aom_variance32x32,
2839 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2840 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
2841
2842 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg_c, aom_variance64x64,
2843 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2844 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
2845
2846 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg_c, aom_variance16x16,
2847 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2848 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
2849
2850 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg_c, aom_variance16x8,
2851 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
2852 aom_sad16x8x8, aom_sad16x8x4d)
2853
2854 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg_c, aom_variance8x16,
2855 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
2856 aom_sad8x16x8, aom_sad8x16x4d)
2857
2858 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg_c, aom_variance8x8,
2859 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
2860 aom_sad8x8x8, aom_sad8x8x4d)
2861
2862 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg_c, aom_variance8x4,
2863 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
2864 aom_sad8x4x8, aom_sad8x4x4d)
2865
2866 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg_c, aom_variance4x8,
2867 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
2868 aom_sad4x8x8, aom_sad4x8x4d)
2869
2870 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg_c, aom_variance4x4,
2871 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
2872 aom_sad4x4x8, aom_sad4x4x4d)
2873#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002874 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2875 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2876 aom_sad32x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002877
Yaowu Xuf883b422016-08-30 14:01:10 -07002878 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2879 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2880 aom_sad16x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002881
Yaowu Xuf883b422016-08-30 14:01:10 -07002882 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2883 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2884 aom_sad64x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002885
Yaowu Xuf883b422016-08-30 14:01:10 -07002886 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2887 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2888 aom_sad32x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889
Yaowu Xuf883b422016-08-30 14:01:10 -07002890 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2891 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2892 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002893
Yaowu Xuf883b422016-08-30 14:01:10 -07002894 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2895 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2896 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002897
Yaowu Xuf883b422016-08-30 14:01:10 -07002898 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2899 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2900 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002901
Yaowu Xuf883b422016-08-30 14:01:10 -07002902 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2903 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
2904 aom_sad16x8x8, aom_sad16x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002905
Yaowu Xuf883b422016-08-30 14:01:10 -07002906 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2907 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
2908 aom_sad8x16x8, aom_sad8x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909
Yaowu Xuf883b422016-08-30 14:01:10 -07002910 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2911 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
2912 aom_sad8x8x8, aom_sad8x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002913
Yaowu Xuf883b422016-08-30 14:01:10 -07002914 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2915 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
2916 aom_sad8x4x8, aom_sad8x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002917
Yaowu Xuf883b422016-08-30 14:01:10 -07002918 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2919 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
2920 aom_sad4x8x8, aom_sad4x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002921
Yaowu Xuf883b422016-08-30 14:01:10 -07002922 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2923 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
2924 aom_sad4x4x8, aom_sad4x4x4d)
Cheng Chenefc55fd2017-10-10 12:08:28 -07002925#endif // CONFIG_JNT_COMP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002926
Jingning Han9e7c49f2016-12-06 11:20:10 -08002927 BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL)
Jingning Hane2ffaf82016-12-14 15:26:30 -08002928 BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL)
2929 BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL)
Jingning Han9e7c49f2016-12-06 11:20:10 -08002930
Yaowu Xuc27fc142016-08-22 16:08:15 -07002931#define OBFP(BT, OSDF, OVF, OSVF) \
2932 cpi->fn_ptr[BT].osdf = OSDF; \
2933 cpi->fn_ptr[BT].ovf = OVF; \
2934 cpi->fn_ptr[BT].osvf = OSVF;
2935
2936#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002937 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2938 aom_obmc_sub_pixel_variance128x128)
2939 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2940 aom_obmc_sub_pixel_variance128x64)
2941 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2942 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002943#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002944 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2945 aom_obmc_sub_pixel_variance64x64)
2946 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2947 aom_obmc_sub_pixel_variance64x32)
2948 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2949 aom_obmc_sub_pixel_variance32x64)
2950 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2951 aom_obmc_sub_pixel_variance32x32)
2952 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2953 aom_obmc_sub_pixel_variance32x16)
2954 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2955 aom_obmc_sub_pixel_variance16x32)
2956 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2957 aom_obmc_sub_pixel_variance16x16)
2958 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2959 aom_obmc_sub_pixel_variance16x8)
2960 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2961 aom_obmc_sub_pixel_variance8x16)
2962 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2963 aom_obmc_sub_pixel_variance8x8)
2964 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2965 aom_obmc_sub_pixel_variance4x8)
2966 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2967 aom_obmc_sub_pixel_variance8x4)
2968 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2969 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002970
2971#if CONFIG_EXT_PARTITION_TYPES
2972 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2973 aom_obmc_sub_pixel_variance4x16)
2974
2975 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2976 aom_obmc_sub_pixel_variance16x4)
2977
2978 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2979 aom_obmc_sub_pixel_variance8x32)
2980
2981 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2982 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002983
2984 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2985 aom_obmc_sub_pixel_variance16x64)
2986
2987 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2988 aom_obmc_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002989
2990#if CONFIG_EXT_PARTITION
2991 OBFP(BLOCK_32X128, aom_obmc_sad32x128, aom_obmc_variance32x128,
2992 aom_obmc_sub_pixel_variance32x128)
2993
2994 OBFP(BLOCK_128X32, aom_obmc_sad128x32, aom_obmc_variance128x32,
2995 aom_obmc_sub_pixel_variance128x32)
2996#endif // CONFIG_EXT_PARTITION
2997#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07002998
David Barkerf19f35f2017-05-22 16:33:22 +01002999#define MBFP(BT, MCSDF, MCSVF) \
3000 cpi->fn_ptr[BT].msdf = MCSDF; \
3001 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003002
3003#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01003004 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3005 aom_masked_sub_pixel_variance128x128)
3006 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3007 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003008#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01003009 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3010 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3011 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3012 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3013 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3014 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3015 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3016 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3017 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3018 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3019 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3020 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3021 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003022
3023#if CONFIG_EXT_PARTITION_TYPES
3024 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3025
3026 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3027
3028 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3029
3030 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003031
3032 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3033
3034 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003035
3036#if CONFIG_EXT_PARTITION
3037 MBFP(BLOCK_32X128, aom_masked_sad32x128, aom_masked_sub_pixel_variance32x128)
3038
3039 MBFP(BLOCK_128X32, aom_masked_sad128x32, aom_masked_sub_pixel_variance128x32)
3040#endif // CONFIG_EXT_PARTITION
3041#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003042
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003043#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044 highbd_set_var_fns(cpi);
3045#endif
3046
Yaowu Xuf883b422016-08-30 14:01:10 -07003047 /* av1_init_quantizer() is first called here. Add check in
3048 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003049 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003050 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003051 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003052 av1_init_quantizer(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003053#if CONFIG_AOM_QM
3054 aom_qm_init(cm);
3055#endif
3056
Yaowu Xuf883b422016-08-30 14:01:10 -07003057 av1_loop_filter_init(cm);
Fergus Simpsondac6aff2017-05-18 15:11:38 -07003058#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07003059 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003060 cm->superres_upscaled_width = oxcf->width;
3061 cm->superres_upscaled_height = oxcf->height;
Fergus Simpsondac6aff2017-05-18 15:11:38 -07003062#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003063#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07003064 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003065#endif // CONFIG_LOOP_RESTORATION
3066
3067 cm->error.setjmp = 0;
3068
3069 return cpi;
3070}
3071
3072#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3073
3074#define SNPRINT2(H, T, V) \
3075 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
3076
Yaowu Xuf883b422016-08-30 14:01:10 -07003077void av1_remove_compressor(AV1_COMP *cpi) {
3078 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003079 unsigned int i;
3080 int t;
3081
3082 if (!cpi) return;
3083
3084 cm = &cpi->common;
3085 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003086#if CONFIG_ENTROPY_STATS
3087 if (cpi->oxcf.pass != 1) {
3088 fprintf(stderr, "Writing counts.stt\n");
3089 FILE *f = fopen("counts.stt", "wb");
3090 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
Zoe Liua56f9162017-06-21 22:49:57 -07003091 fwrite(aggregate_fc_per_type, sizeof(aggregate_fc_per_type[0]),
3092 FRAME_CONTEXTS, f);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003093 fclose(f);
3094 }
3095#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003096#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003097 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003098
3099 if (cpi->oxcf.pass != 1) {
3100 char headings[512] = { 0 };
3101 char results[512] = { 0 };
3102 FILE *f = fopen("opsnr.stt", "a");
3103 double time_encoded =
3104 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3105 10000000.000;
3106 double total_encode_time =
3107 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3108 const double dr =
3109 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3110 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3111 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3112 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3113
3114 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003115 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003116 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3117 const double total_ssim =
3118 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3119 snprintf(headings, sizeof(headings),
3120 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003121 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Yaowu Xuc27fc142016-08-22 16:08:15 -07003122 "WstPsnr\tWstSsim\tWstFast\tWstHVS");
3123 snprintf(results, sizeof(results),
3124 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3125 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3126 "%7.3f\t%7.3f\t%7.3f\t%7.3f",
3127 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
3128 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
3129 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
3130 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
3131 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
3132
3133 if (cpi->b_calculate_blockiness) {
3134 SNPRINT(headings, "\t Block\tWstBlck");
3135 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3136 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3137 }
3138
3139 if (cpi->b_calculate_consistency) {
3140 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003141 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003142 (double)cpi->total_inconsistency);
3143
3144 SNPRINT(headings, "\tConsist\tWstCons");
3145 SNPRINT2(results, "\t%7.3f", consistency);
3146 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3147 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003148 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3149 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003150 rate_err, fabs(rate_err));
3151 }
3152
3153 fclose(f);
3154 }
3155
3156#endif
3157
3158#if 0
3159 {
3160 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
3161 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
3162 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
3163 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
3164 cpi->time_compress_data / 1000,
3165 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
3166 }
3167#endif
3168 }
3169
3170 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003171 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003172 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3173
3174 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003175 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003176
3177 // Deallocate allocated thread data.
3178 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07003179 aom_free(thread_data->td->palette_buffer);
Jingning Hand064cf02017-06-01 10:00:39 -07003180 aom_free(thread_data->td->above_pred_buf);
3181 aom_free(thread_data->td->left_pred_buf);
3182 aom_free(thread_data->td->wsrc_buf);
3183 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003184 aom_free(thread_data->td->counts);
3185 av1_free_pc_tree(thread_data->td);
Yaowu Xuf883b422016-08-30 14:01:10 -07003186 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003187 }
3188 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003189 aom_free(cpi->tile_thr_data);
3190 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003191
Yaowu Xuf883b422016-08-30 14:01:10 -07003192 if (cpi->num_workers > 1) av1_loop_filter_dealloc(&cpi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003193
3194 dealloc_compressor_data(cpi);
3195
3196 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3197 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003198 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003199 }
3200
3201#if CONFIG_FP_MB_STATS
3202 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003203 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003204 cpi->twopass.frame_mb_stats_buf = NULL;
3205 }
3206#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003207#if CONFIG_INTERNAL_STATS
3208 aom_free(cpi->ssim_vars);
3209 cpi->ssim_vars = NULL;
3210#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003211
Yaowu Xuf883b422016-08-30 14:01:10 -07003212 av1_remove_common(cm);
3213 av1_free_ref_frame_buffers(cm->buffer_pool);
3214 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003215
3216#ifdef OUTPUT_YUV_SKINMAP
3217 fclose(yuv_skinmap_file);
3218#endif
3219#ifdef OUTPUT_YUV_REC
3220 fclose(yuv_rec_file);
3221#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003222#if 0
3223
3224 if (keyfile)
3225 fclose(keyfile);
3226
3227 if (framepsnr)
3228 fclose(framepsnr);
3229
3230 if (kf_list)
3231 fclose(kf_list);
3232
3233#endif
3234}
3235
Yaowu Xuf883b422016-08-30 14:01:10 -07003236static void generate_psnr_packet(AV1_COMP *cpi) {
3237 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003238 int i;
3239 PSNR_STATS psnr;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003240#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07003241 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003242 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3243#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07003244 aom_calc_psnr(cpi->source, cpi->common.frame_to_show, &psnr);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003245#endif
3246
3247 for (i = 0; i < 4; ++i) {
3248 pkt.data.psnr.samples[i] = psnr.samples[i];
3249 pkt.data.psnr.sse[i] = psnr.sse[i];
3250 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3251 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003252 pkt.kind = AOM_CODEC_PSNR_PKT;
3253 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003254}
3255
Yaowu Xuf883b422016-08-30 14:01:10 -07003256int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003257 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3258
3259 cpi->ref_frame_flags = ref_frame_flags;
3260 return 0;
3261}
3262
Yaowu Xuf883b422016-08-30 14:01:10 -07003263void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags) {
3264 cpi->ext_refresh_golden_frame = (ref_frame_flags & AOM_GOLD_FLAG) != 0;
3265 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & AOM_ALT_FLAG) != 0;
3266 cpi->ext_refresh_last_frame = (ref_frame_flags & AOM_LAST_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003267 cpi->ext_refresh_frame_flags_pending = 1;
3268}
3269
Thomas Daede497d1952017-08-08 17:33:06 -07003270int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3271 AV1_COMMON *const cm = &cpi->common;
3272 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003273 if (cfg) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003274 aom_yv12_copy_frame(cfg, sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003275 return 0;
3276 } else {
3277 return -1;
3278 }
3279}
3280
Thomas Daede497d1952017-08-08 17:33:06 -07003281int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3282 AV1_COMMON *const cm = &cpi->common;
3283 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003284 if (cfg) {
3285 aom_yv12_copy_frame(sd, cfg);
3286 return 0;
3287 } else {
3288 return -1;
3289 }
3290}
3291
3292int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003293 cpi->ext_refresh_frame_context = update;
3294 cpi->ext_refresh_frame_context_pending = 1;
3295 return 0;
3296}
3297
3298#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3299// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3300// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3301// not denoise the UV channels at this time. If ever we implement UV channel
3302// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003303void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003304 uint8_t *src = s->y_buffer;
3305 int h = s->y_height;
3306
3307 do {
3308 fwrite(src, s->y_width, 1, f);
3309 src += s->y_stride;
3310 } while (--h);
3311
3312 src = s->u_buffer;
3313 h = s->uv_height;
3314
3315 do {
3316 fwrite(src, s->uv_width, 1, f);
3317 src += s->uv_stride;
3318 } while (--h);
3319
3320 src = s->v_buffer;
3321 h = s->uv_height;
3322
3323 do {
3324 fwrite(src, s->uv_width, 1, f);
3325 src += s->uv_stride;
3326 } while (--h);
3327}
3328#endif
3329
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003330#if !CONFIG_XIPHRC
Zoe Liu8dd1c982017-09-11 10:14:35 -07003331#if USE_GF16_MULTI_LAYER
3332static void check_show_existing_frame_gf16(AV1_COMP *cpi) {
3333 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3334 AV1_COMMON *const cm = &cpi->common;
3335 const FRAME_UPDATE_TYPE next_frame_update_type =
3336 gf_group->update_type[gf_group->index];
3337
3338 if (cm->show_existing_frame == 1) {
3339 cm->show_existing_frame = 0;
3340 } else if (cpi->rc.is_last_bipred_frame) {
3341 cpi->rc.is_last_bipred_frame = 0;
3342 cm->show_existing_frame = 1;
3343 cpi->existing_fb_idx_to_show = cpi->bwd_fb_idx;
3344 } else if (next_frame_update_type == OVERLAY_UPDATE ||
3345 next_frame_update_type == INTNL_OVERLAY_UPDATE) {
3346 // Check the temporal filtering status for the next OVERLAY frame
3347 const int num_arfs_in_gf = cpi->num_extra_arfs + 1;
3348 int which_arf = 0, arf_idx;
3349 // Identify the index to the next overlay frame.
3350 for (arf_idx = 0; arf_idx < num_arfs_in_gf; arf_idx++) {
3351 if (gf_group->index == cpi->arf_pos_for_ovrly[arf_idx]) {
3352 which_arf = arf_idx;
3353 break;
3354 }
3355 }
3356 assert(arf_idx < num_arfs_in_gf);
3357 if (cpi->is_arf_filter_off[which_arf]) {
3358 cm->show_existing_frame = 1;
3359 cpi->rc.is_src_frame_alt_ref = 1;
3360 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3361 ? cpi->alt_fb_idx
3362 : cpi->bwd_fb_idx;
3363 cpi->is_arf_filter_off[which_arf] = 0;
3364 }
3365 }
3366 cpi->rc.is_src_frame_ext_arf = 0;
3367}
3368#endif // USE_GF16_MULTI_LAYER
3369
Yaowu Xuf883b422016-08-30 14:01:10 -07003370static void check_show_existing_frame(AV1_COMP *cpi) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003371#if USE_GF16_MULTI_LAYER
3372 if (cpi->rc.baseline_gf_interval == 16) {
3373 check_show_existing_frame_gf16(cpi);
3374 return;
3375 }
3376#endif // USE_GF16_MULTI_LAYER
3377
Yaowu Xuc27fc142016-08-22 16:08:15 -07003378 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003379 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003380 const FRAME_UPDATE_TYPE next_frame_update_type =
3381 gf_group->update_type[gf_group->index];
3382 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07003383
3384 if (cm->show_existing_frame == 1) {
3385 cm->show_existing_frame = 0;
3386 } else if (cpi->rc.is_last_bipred_frame) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003387 // NOTE: If the current frame is a last bi-predictive frame, it is
3388 // needed next to show the BWDREF_FRAME, which is pointed by
3389 // the last_fb_idxes[0] after reference frame buffer update
Yaowu Xuc27fc142016-08-22 16:08:15 -07003390 cpi->rc.is_last_bipred_frame = 0;
3391 cm->show_existing_frame = 1;
3392 cpi->existing_fb_idx_to_show = cpi->lst_fb_idxes[0];
3393 } else if (cpi->is_arf_filter_off[which_arf] &&
3394 (next_frame_update_type == OVERLAY_UPDATE ||
3395 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
3396 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003397 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003398 cm->show_existing_frame = 1;
3399 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003400 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3401 ? cpi->alt_fb_idx
3402 : cpi->alt2_fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003403 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003404 }
3405 cpi->rc.is_src_frame_ext_arf = 0;
3406}
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003407#endif // !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07003408
3409#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003410void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411 uint8_t *src = s->y_buffer;
3412 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003413 if (yuv_rec_file == NULL) return;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003414#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003415 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3416 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3417
3418 do {
3419 fwrite(src16, s->y_width, 2, yuv_rec_file);
3420 src16 += s->y_stride;
3421 } while (--h);
3422
3423 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3424 h = s->uv_height;
3425
3426 do {
3427 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3428 src16 += s->uv_stride;
3429 } while (--h);
3430
3431 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3432 h = s->uv_height;
3433
3434 do {
3435 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3436 src16 += s->uv_stride;
3437 } while (--h);
3438
3439 fflush(yuv_rec_file);
3440 return;
3441 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003442#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003443
3444 do {
3445 fwrite(src, s->y_width, 1, yuv_rec_file);
3446 src += s->y_stride;
3447 } while (--h);
3448
3449 src = s->u_buffer;
3450 h = s->uv_height;
3451
3452 do {
3453 fwrite(src, s->uv_width, 1, yuv_rec_file);
3454 src += s->uv_stride;
3455 } while (--h);
3456
3457 src = s->v_buffer;
3458 h = s->uv_height;
3459
3460 do {
3461 fwrite(src, s->uv_width, 1, yuv_rec_file);
3462 src += s->uv_stride;
3463 } while (--h);
3464
3465 fflush(yuv_rec_file);
3466}
3467#endif // OUTPUT_YUV_REC
3468
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003469#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003470static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3471 int i;
3472 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003473 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003474 AV1_COMMON *const cm = &cpi->common;
3475 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3476 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003477 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003478 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003479 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003480 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003481 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003482 recode = 1;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003483 recode |= (rdc->global_motion_used[i] > 0);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003484 }
3485 }
3486 return recode;
3487}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003488
Yaowu Xuc27fc142016-08-22 16:08:15 -07003489// Function to test for conditions that indicate we should loop
3490// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003491static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3492 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003493 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003494 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003495 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3496 int force_recode = 0;
3497
3498 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3499 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3500 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003501 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3502 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3503 (rc->projected_frame_size < low_limit && q > minq)) {
3504 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003505 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003506 // Deal with frame undershoot and whether or not we are
3507 // below the automatically set cq level.
3508 if (q > oxcf->cq_level &&
3509 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3510 force_recode = 1;
3511 }
3512 }
3513 }
3514 return force_recode;
3515}
3516
Yaowu Xuc27fc142016-08-22 16:08:15 -07003517#define DUMP_REF_FRAME_IMAGES 0
3518
3519#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003520static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003521 const YV12_BUFFER_CONFIG *const ref_buf,
3522 char *file_name) {
3523 int h;
3524 FILE *f_ref = NULL;
3525
3526 if (ref_buf == NULL) {
3527 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003528 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003529 }
3530
3531 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3532 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003533 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003534 }
3535
3536 // --- Y ---
3537 for (h = 0; h < cm->height; ++h) {
3538 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3539 }
3540 // --- U ---
3541 for (h = 0; h < (cm->height >> 1); ++h) {
3542 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3543 f_ref);
3544 }
3545 // --- V ---
3546 for (h = 0; h < (cm->height >> 1); ++h) {
3547 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3548 f_ref);
3549 }
3550
3551 fclose(f_ref);
3552
Yaowu Xuf883b422016-08-30 14:01:10 -07003553 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003554}
3555
Yaowu Xuf883b422016-08-30 14:01:10 -07003556static void dump_ref_frame_images(AV1_COMP *cpi) {
3557 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003558 MV_REFERENCE_FRAME ref_frame;
3559
3560 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3561 char file_name[256] = "";
3562 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3563 cm->current_video_frame, ref_frame);
3564 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3565 }
3566}
3567#endif // DUMP_REF_FRAME_IMAGES == 1
3568
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569// This function is used to shift the virtual indices of last reference frames
3570// as follows:
3571// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3572// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003573static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003574 int ref_frame;
3575 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
3576 cpi->lst_fb_idxes[ref_frame] = cpi->lst_fb_idxes[ref_frame - 1];
3577
3578 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003579 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003580 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003581 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3582 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3583 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003584 }
3585 }
3586}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003587
Zoe Liu8dd1c982017-09-11 10:14:35 -07003588#if USE_GF16_MULTI_LAYER
3589static void update_reference_frames_gf16(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003590 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003591 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003592
Zoe Liu8dd1c982017-09-11 10:14:35 -07003593 if (cm->frame_type == KEY_FRAME) {
3594 for (int ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3595 ref_cnt_fb(pool->frame_bufs,
3596 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3597 cm->new_fb_idx);
3598 }
3599 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3600 cm->new_fb_idx);
3601 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3602 cm->new_fb_idx);
3603 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3604 cm->new_fb_idx);
3605 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3606 cm->new_fb_idx);
3607 } else {
3608 if (cpi->refresh_last_frame || cpi->refresh_golden_frame ||
3609 cpi->refresh_bwd_ref_frame || cpi->refresh_alt2_ref_frame ||
3610 cpi->refresh_alt_ref_frame) {
3611 assert(cpi->refresh_fb_idx >= 0 && cpi->refresh_fb_idx < REF_FRAMES);
3612 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->refresh_fb_idx],
3613 cm->new_fb_idx);
3614 }
3615
3616 // TODO(zoeliu): To handle cpi->interp_filter_selected[].
3617
3618 // For GF of 16, an additional ref frame index mapping needs to be handled
3619 // if this is the last frame to encode in the current GF group.
3620 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3621 if (gf_group->update_type[gf_group->index + 1] == OVERLAY_UPDATE)
3622 av1_ref_frame_map_idx_updates(cpi, gf_group->index + 1);
3623 }
3624
3625#if DUMP_REF_FRAME_IMAGES == 1
3626 // Dump out all reference frame images.
3627 dump_ref_frame_images(cpi);
3628#endif // DUMP_REF_FRAME_IMAGES
3629}
3630#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003631
3632static void update_reference_frames(AV1_COMP *cpi) {
3633 AV1_COMMON *const cm = &cpi->common;
3634
Yaowu Xuc27fc142016-08-22 16:08:15 -07003635 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3636 // for the purpose to verify no mismatch between encoder and decoder.
3637 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3638
Zoe Liu8dd1c982017-09-11 10:14:35 -07003639#if USE_GF16_MULTI_LAYER
3640 if (cpi->rc.baseline_gf_interval == 16) {
3641 update_reference_frames_gf16(cpi);
3642 return;
3643 }
3644#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003645
3646 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003647 // At this point the new frame has been encoded.
3648 // If any buffer copy / swapping is signaled it should be done here.
3649 if (cm->frame_type == KEY_FRAME) {
3650 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3651 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003652 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3653 cm->new_fb_idx);
Zoe Liue9b15e22017-07-19 15:53:01 -07003654 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3655 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003656 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3657 cm->new_fb_idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003658 } else if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003659 // We have decided to preserve the previously existing golden frame as our
3660 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003661 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003662 // we're updating the GF with the current decoded frame, we save it to the
3663 // ARF slot instead.
3664 // We now have to update the ARF with the current frame and swap gld_fb_idx
3665 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3666 // slot and, if we're updating the GF, the current frame becomes the new GF.
3667 int tmp;
3668
3669 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3670 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003671 tmp = cpi->alt_fb_idx;
3672 cpi->alt_fb_idx = cpi->gld_fb_idx;
3673 cpi->gld_fb_idx = tmp;
3674
Yaowu Xuc27fc142016-08-22 16:08:15 -07003675 // We need to modify the mapping accordingly
3676 cpi->arf_map[0] = cpi->alt_fb_idx;
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003677 // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3678 // cpi->interp_filter_selected[GOLDEN_FRAME]?
Yaowu Xuc27fc142016-08-22 16:08:15 -07003679 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3680 // Deal with the special case for showing existing internal ALTREF_FRAME
3681 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3682 // by updating the virtual indices.
3683 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -07003684 const int which_arf = gf_group->arf_ref_idx[gf_group->index];
3685 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003686
Zoe Liue9b15e22017-07-19 15:53:01 -07003687 const int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003688 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003689
Zoe Liue9b15e22017-07-19 15:53:01 -07003690 cpi->lst_fb_idxes[0] = cpi->alt2_fb_idx;
3691 cpi->alt2_fb_idx = tmp;
3692 // We need to modify the mapping accordingly
3693 cpi->arf_map[which_arf] = cpi->alt2_fb_idx;
3694
3695 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3696 cpi->interp_filter_selected[ALTREF2_FRAME],
3697 sizeof(cpi->interp_filter_selected[ALTREF2_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003698 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003699 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003700 if (cpi->refresh_alt_ref_frame) {
3701 int arf_idx = cpi->alt_fb_idx;
3702 int which_arf = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003703 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003704
3705 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3706 cpi->interp_filter_selected[0],
3707 sizeof(cpi->interp_filter_selected[0]));
3708 }
3709
Zoe Liue9b15e22017-07-19 15:53:01 -07003710 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003711 if (cpi->refresh_golden_frame) {
3712 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3713 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003714
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003715 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3716 cpi->interp_filter_selected[0],
3717 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003718 }
3719
Zoe Liue9b15e22017-07-19 15:53:01 -07003720 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003721 if (cpi->refresh_bwd_ref_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003722 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3723 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003724
3725 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3726 cpi->interp_filter_selected[0],
3727 sizeof(cpi->interp_filter_selected[0]));
3728 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003729
Zoe Liue9b15e22017-07-19 15:53:01 -07003730 // === ALTREF2_FRAME ===
3731 if (cpi->refresh_alt2_ref_frame) {
3732 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3733 cm->new_fb_idx);
3734
3735 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3736 cpi->interp_filter_selected[0],
3737 sizeof(cpi->interp_filter_selected[0]));
3738 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003739 }
3740
3741 if (cpi->refresh_last_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3743 // reference to the reference frame buffer virtual index; and then (2) from
3744 // the virtual index to the reference frame buffer physical index:
3745 //
3746 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3747 // | | |
3748 // v v v
3749 // lst_fb_idxes[0], ..., lst_fb_idxes[2], ..., alt_fb_idx
3750 // | | |
3751 // v v v
3752 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3753 //
3754 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3755 // have the other 2 LAST reference frames shifted as follows:
3756 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3757 // , and then have LAST_FRAME refreshed by the newly coded frame.
3758 //
3759 // To fulfill it, the decoder will be notified to execute following 2 steps:
3760 //
3761 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3762 // to point to the newly coded frame, i.e.
3763 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3764 //
3765 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3766 // original virtual index of LAST3_FRAME and have the other mappings
3767 // shifted as follows:
3768 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3769 // | | |
3770 // v v v
3771 // lst_fb_idxes[2], lst_fb_idxes[0], lst_fb_idxes[1]
3772 int ref_frame;
Zoe Liu5fca7242016-10-10 17:18:57 -07003773
Yaowu Xuc27fc142016-08-22 16:08:15 -07003774 if (cm->frame_type == KEY_FRAME) {
3775 for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3776 ref_cnt_fb(pool->frame_bufs,
3777 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3778 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003779 }
3780 } else {
3781 int tmp;
3782
3783 ref_cnt_fb(pool->frame_bufs,
3784 &cm->ref_frame_map[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3785 cm->new_fb_idx);
3786
Yaowu Xuc27fc142016-08-22 16:08:15 -07003787 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3788
3789 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003790 cpi->lst_fb_idxes[0] = tmp;
3791
Zoe Liuf0e46692016-10-12 12:31:43 -07003792 assert(cm->show_existing_frame == 0);
Zoe Liuf0e46692016-10-12 12:31:43 -07003793 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3794 cpi->interp_filter_selected[0],
3795 sizeof(cpi->interp_filter_selected[0]));
Zoe Liuaff92d52017-07-11 21:35:08 -07003796
3797 if (cpi->rc.is_last_bipred_frame) {
3798 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3799 // LAST3_FRAME by updating the virtual indices.
3800 //
3801 // NOTE: The source frame for BWDREF does not have a holding position as
3802 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3803 // virtual index reshuffling for BWDREF, the encoder always
3804 // specifies a LAST_BIPRED right before BWDREF and completes the
3805 // reshuffling job accordingly.
3806 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3807
3808 shift_last_ref_frames(cpi);
3809 cpi->lst_fb_idxes[0] = cpi->bwd_fb_idx;
3810 cpi->bwd_fb_idx = tmp;
3811
3812 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3813 cpi->interp_filter_selected[BWDREF_FRAME],
3814 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
3815 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003816 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003817 }
3818
3819#if DUMP_REF_FRAME_IMAGES == 1
3820 // Dump out all reference frame images.
3821 dump_ref_frame_images(cpi);
3822#endif // DUMP_REF_FRAME_IMAGES
3823}
3824
Yaowu Xuf883b422016-08-30 14:01:10 -07003825static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003826 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003827 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003828 ensure_mv_buffer(new_fb_ptr, cm);
3829 new_fb_ptr->width = cm->width;
3830 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003831}
3832
Cheng Chen46f30c72017-09-07 11:13:33 -07003833static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003834 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003835 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003836 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003837 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3838 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003839 };
3840
3841 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003842 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003843 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3844 BufferPool *const pool = cm->buffer_pool;
3845 const YV12_BUFFER_CONFIG *const ref =
3846 get_ref_frame_buffer(cpi, ref_frame);
3847
3848 if (ref == NULL) {
3849 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3850 continue;
3851 }
3852
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003853#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003854 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3855 RefCntBuffer *new_fb_ptr = NULL;
3856 int force_scaling = 0;
3857 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3858 if (new_fb == INVALID_IDX) {
3859 new_fb = get_free_fb(cm);
3860 force_scaling = 1;
3861 }
3862 if (new_fb == INVALID_IDX) return;
3863 new_fb_ptr = &pool->frame_bufs[new_fb];
3864 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3865 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003866 if (aom_realloc_frame_buffer(
3867 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3868 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3869 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003870 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003871 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003872 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf,
3873 (int)cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003874 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3875 alloc_frame_mvs(cm, new_fb);
3876 }
3877#else
3878 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3879 RefCntBuffer *new_fb_ptr = NULL;
3880 int force_scaling = 0;
3881 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3882 if (new_fb == INVALID_IDX) {
3883 new_fb = get_free_fb(cm);
3884 force_scaling = 1;
3885 }
3886 if (new_fb == INVALID_IDX) return;
3887 new_fb_ptr = &pool->frame_bufs[new_fb];
3888 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3889 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003890 if (aom_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003891 cm->subsampling_x, cm->subsampling_y,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003892 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3893 NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003894 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003895 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003896 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003897 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3898 alloc_frame_mvs(cm, new_fb);
3899 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003900#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003901 } else {
3902 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3903 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3904 buf->buf.y_crop_width = ref->y_crop_width;
3905 buf->buf.y_crop_height = ref->y_crop_height;
3906 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3907 ++buf->ref_count;
3908 }
3909 } else {
3910 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3911 }
3912 }
3913}
3914
Yaowu Xuf883b422016-08-30 14:01:10 -07003915static void release_scaled_references(AV1_COMP *cpi) {
3916 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003917 int i;
3918 if (cpi->oxcf.pass == 0) {
3919 // Only release scaled references under certain conditions:
3920 // if reference will be updated, or if scaled reference has same resolution.
3921 int refresh[INTER_REFS_PER_FRAME];
3922 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003923 refresh[1] = refresh[2] = 0;
3924 refresh[3] = (cpi->refresh_golden_frame) ? 1 : 0;
3925 refresh[4] = (cpi->refresh_bwd_ref_frame) ? 1 : 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07003926 refresh[5] = (cpi->refresh_alt2_ref_frame) ? 1 : 0;
3927 refresh[6] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003928 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3929 const int idx = cpi->scaled_ref_idx[i - 1];
3930 RefCntBuffer *const buf =
3931 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3932 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
3933 if (buf != NULL &&
3934 (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
3935 buf->buf.y_crop_height == ref->y_crop_height))) {
3936 --buf->ref_count;
3937 cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
3938 }
3939 }
3940 } else {
3941 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) {
3942 const int idx = cpi->scaled_ref_idx[i];
3943 RefCntBuffer *const buf =
3944 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3945 if (buf != NULL) {
3946 --buf->ref_count;
3947 cpi->scaled_ref_idx[i] = INVALID_IDX;
3948 }
3949 }
3950 }
3951}
3952
Yaowu Xuc27fc142016-08-22 16:08:15 -07003953#if 0 && CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003954static void output_frame_level_debug_stats(AV1_COMP *cpi) {
3955 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003956 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
3957 int64_t recon_err;
3958
Yaowu Xuf883b422016-08-30 14:01:10 -07003959 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003960
Alex Conversef77fd0b2017-04-20 11:00:24 -07003961 recon_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003962
3963 if (cpi->twopass.total_left_stats.coded_error != 0.0)
Yunqing Wang8c1e57c2016-10-25 15:15:23 -07003964 fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
Yaowu Xuc27fc142016-08-22 16:08:15 -07003965 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
3966 "%10"PRId64" %10"PRId64" %10d "
3967 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
3968 "%6d %6d %5d %5d %5d "
3969 "%10"PRId64" %10.3lf"
3970 "%10lf %8u %10"PRId64" %10d %10d %10d\n",
3971 cpi->common.current_video_frame,
3972 cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003973 cpi->rc.source_alt_ref_pending,
3974 cpi->rc.source_alt_ref_active,
3975 cpi->rc.this_frame_target,
3976 cpi->rc.projected_frame_size,
3977 cpi->rc.projected_frame_size / cpi->common.MBs,
3978 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
3979 cpi->rc.vbr_bits_off_target,
3980 cpi->rc.vbr_bits_off_target_fast,
3981 cpi->twopass.extend_minq,
3982 cpi->twopass.extend_minq_fast,
3983 cpi->rc.total_target_vs_actual,
3984 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
3985 cpi->rc.total_actual_bits, cm->base_qindex,
Yaowu Xuf883b422016-08-30 14:01:10 -07003986 av1_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
3987 (double)av1_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
3988 av1_convert_qindex_to_q(cpi->twopass.active_worst_quality,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003989 cm->bit_depth),
3990 cpi->rc.avg_q,
Yaowu Xuf883b422016-08-30 14:01:10 -07003991 av1_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003992 cpi->refresh_last_frame, cpi->refresh_golden_frame,
3993 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
3994 cpi->twopass.bits_left,
3995 cpi->twopass.total_left_stats.coded_error,
3996 cpi->twopass.bits_left /
3997 (1 + cpi->twopass.total_left_stats.coded_error),
3998 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
3999 cpi->twopass.kf_zeromotion_pct,
4000 cpi->twopass.fr_content_type);
4001
4002 fclose(f);
4003
4004 if (0) {
4005 FILE *const fmodes = fopen("Modes.stt", "a");
4006 int i;
4007
4008 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
4009 cm->frame_type, cpi->refresh_golden_frame,
4010 cpi->refresh_alt_ref_frame);
4011
4012 for (i = 0; i < MAX_MODES; ++i)
4013 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4014
4015 fprintf(fmodes, "\n");
4016
4017 fclose(fmodes);
4018 }
4019}
4020#endif
4021
Yaowu Xuf883b422016-08-30 14:01:10 -07004022static void set_mv_search_params(AV1_COMP *cpi) {
4023 const AV1_COMMON *const cm = &cpi->common;
4024 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004025
4026 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07004027 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004028
4029 if (cpi->sf.mv.auto_mv_step_size) {
4030 if (frame_is_intra_only(cm)) {
4031 // Initialize max_mv_magnitude for use in the first INTER frame
4032 // after a key/intra-only frame.
4033 cpi->max_mv_magnitude = max_mv_def;
4034 } else {
4035 if (cm->show_frame) {
4036 // Allow mv_steps to correspond to twice the max mv magnitude found
4037 // in the previous frame, capped by the default max_mv_magnitude based
4038 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07004039 cpi->mv_step_param = av1_init_search_range(
4040 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004041 }
4042 cpi->max_mv_magnitude = 0;
4043 }
4044 }
4045}
4046
Yaowu Xuf883b422016-08-30 14:01:10 -07004047static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004048 int i;
4049 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01004050 cpi->common.global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004051 }
4052 cpi->global_motion_search_done = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004053 av1_set_speed_features_framesize_independent(cpi);
4054 av1_set_rd_speed_thresholds(cpi);
4055 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004056 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07004057 if (!frame_is_intra_only(&cpi->common)) set_compound_tools(&cpi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004058}
4059
Yaowu Xuf883b422016-08-30 14:01:10 -07004060static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004061 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004062 AV1_COMMON *const cm = &cpi->common;
4063 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004064
4065 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004066 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004067
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004068// Decide q and q bounds.
4069#if CONFIG_XIPHRC
4070 int frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
4071 *q = od_enc_rc_select_quantizers_and_lambdas(
4072 &cpi->od_rc, cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
4073 frame_type, bottom_index, top_index);
4074#else
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004075 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
4076 top_index);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004077#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004078
James Zern01a9d702017-08-25 19:09:33 +00004079 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07004080#if CONFIG_AMVR
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07004081#if CONFIG_EIGHTH_PEL_MV_ONLY
4082 set_high_precision_mv(cpi, 1, cpi->common.cur_frame_force_integer_mv);
4083#else
RogerZhou3b635242017-09-19 10:06:46 -07004084 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07004085 cpi->common.cur_frame_force_integer_mv);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07004086#endif // CONFIG_EIGHTH_PEL_MV_ONLY
4087#else
4088#if CONFIG_EIGHTH_PEL_MV_ONLY
4089 set_high_precision_mv(cpi, 1);
RogerZhou3b635242017-09-19 10:06:46 -07004090#else
Cheng Chen46f30c72017-09-07 11:13:33 -07004091 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07004092#endif // CONFIG_EIGHTH_PEL_MV_ONLY
RogerZhou3b635242017-09-19 10:06:46 -07004093#endif
James Zern01a9d702017-08-25 19:09:33 +00004094 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004095
4096 // Configure experimental use of segmentation for enhanced coding of
4097 // static regions if indicated.
4098 // Only allowed in the second pass of a two pass encode, as it requires
4099 // lagged coding, and if the relevant speed feature flag is set.
4100 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
4101 configure_static_seg_features(cpi);
4102}
4103
Yaowu Xuf883b422016-08-30 14:01:10 -07004104static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004105 int y_stride = cpi->scaled_source.y_stride;
4106
4107 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004108 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004109 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004110 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004111 }
4112}
4113
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004114#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004115#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004116static void set_restoration_unit_size(int width, int height, int sx, int sy,
4117 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004118 (void)width;
4119 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004120 (void)sx;
4121 (void)sy;
4122#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4123 int s = AOMMIN(sx, sy);
4124#else
4125 int s = 0;
4126#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4127
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004128 rst[0].restoration_unit_size = (RESTORATION_TILESIZE_MAX >> 1);
4129 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4130 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004131}
4132#endif // CONFIG_LOOP_RESTORATION
4133
Cheng Chen46f30c72017-09-07 11:13:33 -07004134static void init_ref_frame_bufs(AV1_COMMON *cm) {
4135 int i;
4136 BufferPool *const pool = cm->buffer_pool;
4137 cm->new_fb_idx = INVALID_IDX;
4138 for (i = 0; i < REF_FRAMES; ++i) {
4139 cm->ref_frame_map[i] = INVALID_IDX;
4140 pool->frame_bufs[i].ref_count = 0;
4141 }
4142#if CONFIG_HASH_ME
4143 for (i = 0; i < FRAME_BUFFERS; ++i) {
4144 av1_hash_table_init(&pool->frame_bufs[i].hash_table);
4145 }
4146#endif
4147}
4148
4149static void check_initial_width(AV1_COMP *cpi,
4150#if CONFIG_HIGHBITDEPTH
4151 int use_highbitdepth,
4152#endif
4153 int subsampling_x, int subsampling_y) {
4154 AV1_COMMON *const cm = &cpi->common;
4155
4156 if (!cpi->initial_width ||
4157#if CONFIG_HIGHBITDEPTH
4158 cm->use_highbitdepth != use_highbitdepth ||
4159#endif
4160 cm->subsampling_x != subsampling_x ||
4161 cm->subsampling_y != subsampling_y) {
4162 cm->subsampling_x = subsampling_x;
4163 cm->subsampling_y = subsampling_y;
4164#if CONFIG_HIGHBITDEPTH
4165 cm->use_highbitdepth = use_highbitdepth;
4166#endif
4167
4168 alloc_raw_frame_buffers(cpi);
4169 init_ref_frame_bufs(cm);
4170 alloc_util_frame_buffers(cpi);
4171
4172 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4173
4174 cpi->initial_width = cm->width;
4175 cpi->initial_height = cm->height;
4176 cpi->initial_mbs = cm->MBs;
4177 }
4178}
4179
4180// Returns 1 if the assigned width or height was <= 0.
4181static int set_size_literal(AV1_COMP *cpi, int width, int height) {
4182 AV1_COMMON *cm = &cpi->common;
4183#if CONFIG_HIGHBITDEPTH
4184 check_initial_width(cpi, cm->use_highbitdepth, cm->subsampling_x,
4185 cm->subsampling_y);
4186#else
4187 check_initial_width(cpi, cm->subsampling_x, cm->subsampling_y);
4188#endif // CONFIG_HIGHBITDEPTH
4189
4190 if (width <= 0 || height <= 0) return 1;
4191
4192 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004193 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004194
4195 if (cpi->initial_width && cpi->initial_height &&
4196 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4197 av1_free_context_buffers(cm);
4198 av1_free_pc_tree(&cpi->td);
4199 alloc_compressor_data(cpi);
4200 realloc_segmentation_maps(cpi);
4201 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004202 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004203 update_frame_size(cpi);
4204
4205 return 0;
4206}
4207
Fergus Simpsonbc189932017-05-16 17:02:39 -07004208static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004209 AV1_COMMON *const cm = &cpi->common;
Fergus Simpsonbc189932017-05-16 17:02:39 -07004210 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004211 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004212
Fergus Simpsonbc189932017-05-16 17:02:39 -07004213 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004214 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07004215 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004216 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004217 }
4218
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004219#if !CONFIG_XIPHRC
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004220 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004221 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004222 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004223#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004224
4225 alloc_frame_mvs(cm, cm->new_fb_idx);
4226
4227 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07004228 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004229 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004230#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004231 cm->use_highbitdepth,
4232#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07004233 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
4234 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004235 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004236 "Failed to allocate frame buffer");
4237
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004238#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004239#if CONFIG_FRAME_SUPERRES
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004240 const int frame_width = cm->superres_upscaled_width;
4241 const int frame_height = cm->superres_upscaled_height;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004242#else
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004243 const int frame_width = cm->width;
4244 const int frame_height = cm->height;
4245#endif
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004246 set_restoration_unit_size(frame_width, frame_height, cm->subsampling_x,
4247 cm->subsampling_y, cm->rst_info);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004248 for (int i = 0; i < MAX_MB_PLANE; ++i)
4249 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004250
4251 av1_alloc_restoration_buffers(cm);
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004252#endif // CONFIG_LOOP_RESTORATION
4253 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004254 init_motion_estimation(cpi);
4255
4256 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4257 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
4258 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4259
4260 ref_buf->idx = buf_idx;
4261
4262 if (buf_idx != INVALID_IDX) {
4263 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
4264 ref_buf->buf = buf;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004265#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004266 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004267 &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
4268 cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
4269#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004270 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
4271 buf->y_crop_height, cm->width,
4272 cm->height);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004273#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004274 if (av1_is_scaled(&ref_buf->sf)) aom_extend_frame_borders(buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004275 } else {
4276 ref_buf->buf = NULL;
4277 }
4278 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004279
Alex Conversee816b312017-05-01 09:51:24 -07004280#if CONFIG_INTRABC
4281#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan1dcb7072017-05-12 11:13:05 +02004282 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4283 cm->width, cm->height,
4284 cm->use_highbitdepth);
Alex Conversee816b312017-05-01 09:51:24 -07004285#else
4286 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4287 cm->width, cm->height);
4288#endif // CONFIG_HIGHBITDEPTH
4289#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004290
4291 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4292}
4293
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004294static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4295 // Choose an arbitrary random number
4296 static unsigned int seed = 56789;
4297 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004298 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4299 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004300
4301 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004302 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004303 case RESIZE_FIXED:
4304 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004305 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004306 else
Urvang Joshide71d142017-10-05 12:12:15 -07004307 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004308 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004309 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004310 default: assert(0);
4311 }
Urvang Joshide71d142017-10-05 12:12:15 -07004312 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004313}
4314
4315#if CONFIG_FRAME_SUPERRES
Urvang Joshie58b5642017-10-05 17:59:43 -07004316
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004317static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4318 // Choose an arbitrary random number
4319 static unsigned int seed = 34567;
4320 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004321 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4322 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004323 int bottom_index, top_index, q, qthresh;
4324
4325 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004326 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004327 case SUPERRES_FIXED:
4328 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004329 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004330 else
Urvang Joshide71d142017-10-05 12:12:15 -07004331 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004332 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004333 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004334 case SUPERRES_QTHRESH:
4335 qthresh = (cpi->common.frame_type == KEY_FRAME ? oxcf->superres_kf_qthresh
4336 : oxcf->superres_qthresh);
4337 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
4338 q = av1_rc_pick_q_and_bounds(cpi, cpi->oxcf.width, cpi->oxcf.height,
4339 &bottom_index, &top_index);
4340 if (q < qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004341 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004342 } else {
Urvang Joshi28983f72017-10-25 14:41:06 -07004343 const uint8_t min_denom = SCALE_NUMERATOR + 1;
4344 const uint8_t denom_step = (MAXQ - qthresh + 1) >> 3;
4345 const uint8_t additional_denom = (q - qthresh) / denom_step;
4346 new_denom = AOMMIN(min_denom + additional_denom, SCALE_NUMERATOR << 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004347 }
4348 break;
4349 default: assert(0);
4350 }
Urvang Joshide71d142017-10-05 12:12:15 -07004351 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004352}
4353
Urvang Joshide71d142017-10-05 12:12:15 -07004354static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4355 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4356}
4357
4358static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
4359 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom) &&
Urvang Joshi3d8bcb22017-10-09 12:18:59 -07004360 (CONFIG_HORZONLY_FRAME_SUPERRES ||
Urvang Joshie58b5642017-10-05 17:59:43 -07004361 dimension_is_ok(oheight, rsz->resize_height, rsz->superres_denom));
Urvang Joshide71d142017-10-05 12:12:15 -07004362}
4363
4364#define DIVIDE_AND_ROUND(x, y) (((x) + ((y) >> 1)) / (y))
4365
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004366static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004367 SUPERRES_MODE superres_mode, int owidth,
4368 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004369 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004370 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004371 }
4372
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004373 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004374 int resize_denom =
4375 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4376 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4377
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004378 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004379 // Alter superres scale as needed to enforce conformity.
4380 rsz->superres_denom =
4381 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4382 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4383 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004384 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004385 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004386 // Alter resize scale as needed to enforce conformity.
4387 resize_denom =
4388 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004389 rsz->resize_width = owidth;
4390 rsz->resize_height = oheight;
4391 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004392 resize_denom);
4393 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4394 if (resize_denom > SCALE_NUMERATOR) {
4395 --resize_denom;
4396 rsz->resize_width = owidth;
4397 rsz->resize_height = oheight;
4398 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4399 resize_denom);
4400 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004401 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004402 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004403 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004404 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004405 if (resize_denom > rsz->superres_denom)
4406 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004407 else
Urvang Joshide71d142017-10-05 12:12:15 -07004408 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004409 rsz->resize_width = owidth;
4410 rsz->resize_height = oheight;
4411 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004412 resize_denom);
4413 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4414 (resize_denom > SCALE_NUMERATOR ||
4415 rsz->superres_denom > SCALE_NUMERATOR));
4416 } else { // We are allowed to alter neither resize scale nor superres scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004417 return 0;
4418 }
Urvang Joshide71d142017-10-05 12:12:15 -07004419 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004420}
Urvang Joshide71d142017-10-05 12:12:15 -07004421#undef DIVIDE_AND_ROUND
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004422#endif // CONFIG_FRAME_SUPERRES
4423
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004424// Calculates resize and superres params for next frame
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004425size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
4426 const AV1EncoderConfig *oxcf = &cpi->oxcf;
4427 size_params_type rsz = {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004428 oxcf->width,
4429 oxcf->height,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004430#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07004431 SCALE_NUMERATOR
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004432#endif // CONFIG_FRAME_SUPERRES
4433 };
Urvang Joshide71d142017-10-05 12:12:15 -07004434 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004435 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004436 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4437 rsz.resize_width = cpi->resize_pending_width;
4438 rsz.resize_height = cpi->resize_pending_height;
4439 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4440 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004441 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004442 rsz.resize_width = cpi->oxcf.width;
4443 rsz.resize_height = cpi->oxcf.height;
4444 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004445 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004446 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004447#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07004448 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004449 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4450 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004451 assert(0 && "Invalid scale parameters");
4452#endif // CONFIG_FRAME_SUPERRES
4453 return rsz;
4454}
4455
4456static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004457 int encode_width = rsz->resize_width;
4458 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004459
4460#if CONFIG_FRAME_SUPERRES
4461 AV1_COMMON *cm = &cpi->common;
4462 cm->superres_upscaled_width = encode_width;
4463 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004464 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004465 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4466 rsz->superres_denom);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004467#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004468 set_frame_size(cpi, encode_width, encode_height);
4469}
4470
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004471static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004472 size_params_type rsz = av1_calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004473 setup_frame_size_from_params(cpi, &rsz);
4474}
4475
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004476#if CONFIG_FRAME_SUPERRES
4477static void superres_post_encode(AV1_COMP *cpi) {
4478 AV1_COMMON *cm = &cpi->common;
4479
4480 if (av1_superres_unscaled(cm)) return;
4481
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004482 av1_superres_upscale(cm, NULL);
4483
4484 // If regular resizing is occurring the source will need to be downscaled to
4485 // match the upscaled superres resolution. Otherwise the original source is
4486 // used.
4487 if (av1_resize_unscaled(cm)) {
4488 cpi->source = cpi->unscaled_source;
4489 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4490 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004491 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4492 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004493 // Do downscale. cm->(width|height) has been updated by av1_superres_upscale
4494 if (aom_realloc_frame_buffer(
4495 &cpi->scaled_source, cm->superres_upscaled_width,
4496 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
4497#if CONFIG_HIGHBITDEPTH
4498 cm->use_highbitdepth,
4499#endif // CONFIG_HIGHBITDEPTH
4500 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
4501 aom_internal_error(
4502 &cm->error, AOM_CODEC_MEM_ERROR,
4503 "Failed to reallocate scaled source buffer for superres");
4504 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4505 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
4506#if CONFIG_HIGHBITDEPTH
4507 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
4508 (int)cm->bit_depth);
4509#else
4510 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source);
4511#endif // CONFIG_HIGHBITDEPTH
4512 cpi->source = &cpi->scaled_source;
4513 }
4514}
4515#endif // CONFIG_FRAME_SUPERRES
4516
4517static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4518 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4519 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004520 int no_loopfilter = 0;
4521
4522 if (is_lossless_requested(&cpi->oxcf)) no_loopfilter = 1;
4523
4524#if CONFIG_EXT_TILE
4525 // 0 loopfilter level is only necessary if individual tile
4526 // decoding is required.
4527 if (cm->single_tile_decoding) no_loopfilter = 1;
4528#endif // CONFIG_EXT_TILE
4529
4530 if (no_loopfilter) {
Cheng Chen13fc8192017-08-19 11:49:28 -07004531#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004532 lf->filter_level[0] = 0;
4533 lf->filter_level[1] = 0;
4534#else
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004535 lf->filter_level = 0;
Cheng Chen179479f2017-08-04 10:56:39 -07004536#endif
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004537 } else {
4538 struct aom_usec_timer timer;
4539
4540 aom_clear_system_state();
4541
4542 aom_usec_timer_start(&timer);
4543
4544 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4545
4546 aom_usec_timer_mark(&timer);
4547 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4548 }
4549
Cheng Chen5ad5b282017-10-05 16:36:06 -07004550#if CONFIG_INTRABC
4551// When intraBC is on, do loop filtering per superblock,
4552// instead of do it after the whole frame has been encoded,
4553// as is in the else branch
4554#else
Cheng Chenf572cd32017-08-25 18:34:51 -07004555#if !CONFIG_LPF_SB
Cheng Chen13fc8192017-08-19 11:49:28 -07004556#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004557 if (lf->filter_level[0] || lf->filter_level[1])
4558#else
4559 if (lf->filter_level > 0)
4560#endif
Cheng Chenf572cd32017-08-25 18:34:51 -07004561#endif // CONFIG_LPF_SB
Cheng Chen179479f2017-08-04 10:56:39 -07004562 {
Cheng Chenf572cd32017-08-25 18:34:51 -07004563#if CONFIG_LPF_SB
4564 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0, 0,
4565 0);
4566#else
Cheng Chen13fc8192017-08-19 11:49:28 -07004567#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004568 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level[0],
4569 lf->filter_level[1], 0, 0);
4570 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_u,
4571 lf->filter_level_u, 1, 0);
4572 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_v,
4573 lf->filter_level_v, 2, 0);
4574
Cheng Chene94df5c2017-07-19 17:25:33 -07004575#else
4576 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
Cheng Chen13fc8192017-08-19 11:49:28 -07004577#endif // CONFIG_LOOPFILTER_LEVEL
Cheng Chenf572cd32017-08-25 18:34:51 -07004578#endif // CONFIG_LPF_SB
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004579 }
Cheng Chen5ad5b282017-10-05 16:36:06 -07004580#endif // CONFIG_INTRABC
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004581
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004582#if CONFIG_STRIPED_LOOP_RESTORATION
Rupert Swarbrick76c78002017-11-02 17:26:35 +00004583#if CONFIG_FRAME_SUPERRES && CONFIG_HORZONLY_FRAME_SUPERRES
4584 aom_extend_frame_borders(cm->frame_to_show);
4585#endif
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004586 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm);
4587#endif
4588
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004589#if CONFIG_CDEF
4590 if (is_lossless_requested(&cpi->oxcf)) {
4591 cm->cdef_bits = 0;
4592 cm->cdef_strengths[0] = 0;
4593 cm->nb_cdef_strengths = 1;
4594 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004595 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004596 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004597 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004598
4599 // Apply the filter
4600 av1_cdef_frame(cm->frame_to_show, cm, xd);
4601 }
4602#endif
4603
4604#if CONFIG_FRAME_SUPERRES
4605 superres_post_encode(cpi);
4606#endif // CONFIG_FRAME_SUPERRES
4607
4608#if CONFIG_LOOP_RESTORATION
Rupert Swarbrick146a0602017-10-17 16:52:20 +01004609 av1_pick_filter_restoration(cpi->source, cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004610 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4611 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4612 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Rupert Swarbrickdd6f09a2017-10-19 16:10:23 +01004613 av1_loop_restoration_filter_frame(cm->frame_to_show, cm, cm->rst_info, 7,
4614 NULL);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004615 }
4616#endif // CONFIG_LOOP_RESTORATION
4617 // TODO(debargha): Fix mv search range on encoder side
4618 // aom_extend_frame_inner_borders(cm->frame_to_show);
4619 aom_extend_frame_borders(cm->frame_to_show);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004620}
4621
Yaowu Xuf883b422016-08-30 14:01:10 -07004622static void encode_without_recode_loop(AV1_COMP *cpi) {
4623 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004624 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004625
Yaowu Xuf883b422016-08-30 14:01:10 -07004626 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004627
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004628 set_size_independent_vars(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004629
Fergus Simpsonbc189932017-05-16 17:02:39 -07004630 setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004631
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004632 assert(cm->width == cpi->scaled_source.y_crop_width);
4633 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004634
Yaowu Xuc27fc142016-08-22 16:08:15 -07004635 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4636
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004637 cpi->source =
4638 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4639 if (cpi->unscaled_last_source != NULL)
4640 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4641 &cpi->scaled_last_source);
Sebastien Alaiwan48795802017-10-30 12:07:13 +01004642#if CONFIG_HIGHBITDEPTH
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004643 cpi->source->buf_8bit_valid = 0;
4644#endif
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004645
4646 if (frame_is_intra_only(cm) == 0) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004647 scale_references(cpi);
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004648 }
4649
Yaowu Xuf883b422016-08-30 14:01:10 -07004650 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004651 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004652 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004653
Yaowu Xuc27fc142016-08-22 16:08:15 -07004654 // Variance adaptive and in frame q adjustment experiments are mutually
4655 // exclusive.
4656 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004657 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004658 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004659 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004660 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004661 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004662 }
4663 apply_active_map(cpi);
4664
4665 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004666 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004667
4668 // Update some stats from cyclic refresh, and check if we should not update
4669 // golden reference, for 1 pass CBR.
4670 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004671 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4672 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004673
4674 // Update the skip mb flag probabilities based on the distribution
4675 // seen in the last encoder iteration.
4676 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004677 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004678}
4679
Yaowu Xuf883b422016-08-30 14:01:10 -07004680static void encode_with_recode_loop(AV1_COMP *cpi, size_t *size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004681 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004682 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004683 RATE_CONTROL *const rc = &cpi->rc;
4684 int bottom_index, top_index;
4685 int loop_count = 0;
4686 int loop_at_this_size = 0;
4687 int loop = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004688#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004689 int overshoot_seen = 0;
4690 int undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004691#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004692 int frame_over_shoot_limit;
4693 int frame_under_shoot_limit;
4694 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004695
4696 set_size_independent_vars(cpi);
4697
Sebastien Alaiwan48795802017-10-30 12:07:13 +01004698#if CONFIG_HIGHBITDEPTH
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004699 cpi->source->buf_8bit_valid = 0;
4700#endif
4701
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004702 aom_clear_system_state();
4703 setup_frame_size(cpi);
4704 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4705
Yaowu Xuc27fc142016-08-22 16:08:15 -07004706 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004707 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004708
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004709 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004710 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4711 set_mv_search_params(cpi);
4712
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004713#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004714 // Reset the loop state for new frame size.
4715 overshoot_seen = 0;
4716 undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004717#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004718
Yaowu Xuc27fc142016-08-22 16:08:15 -07004719 q_low = bottom_index;
4720 q_high = top_index;
4721
4722 loop_at_this_size = 0;
4723 }
4724
4725 // Decide frame size bounds first time through.
4726 if (loop_count == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004727 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4728 &frame_under_shoot_limit,
4729 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004730 }
4731
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004732 // if frame was scaled calculate global_motion_search again if already done
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004733 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4734 if (cpi->source->y_crop_width != cm->width ||
4735 cpi->source->y_crop_height != cm->height)
4736 cpi->global_motion_search_done = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004737 cpi->source =
4738 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004739 if (cpi->unscaled_last_source != NULL)
4740 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4741 &cpi->scaled_last_source);
4742
Yaowu Xuc27fc142016-08-22 16:08:15 -07004743 if (frame_is_intra_only(cm) == 0) {
4744 if (loop_count > 0) {
4745 release_scaled_references(cpi);
4746 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004747 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004748 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004749 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004750
4751 if (loop_count == 0) setup_frame(cpi);
4752
hui su0d103572017-03-01 17:58:01 -08004753#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004754 // Base q-index may have changed, so we need to assign proper default coef
4755 // probs before every iteration.
4756 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
4757 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07004758 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004759 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4760 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4761 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4762 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
Thomas Daededa4d8b92017-06-05 15:44:14 -07004763#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
4764 if (cm->frame_refs[0].idx >= 0) {
4765 cm->frame_contexts[cm->frame_refs[0].idx] = *cm->fc;
4766 }
4767#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004768 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07004769#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004770 }
4771 }
hui su0d103572017-03-01 17:58:01 -08004772#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004773
Yaowu Xuc27fc142016-08-22 16:08:15 -07004774 // Variance adaptive and in frame q adjustment experiments are mutually
4775 // exclusive.
4776 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004777 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004778 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004779 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004780 }
4781
4782 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004783 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004784 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004785
4786 // Update the skip mb flag probabilities based on the distribution
4787 // seen in the last encoder iteration.
4788 // update_base_skip_probs(cpi);
4789
Yaowu Xuf883b422016-08-30 14:01:10 -07004790 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004791
4792 // Dummy pack of the bitstream using up to date stats to get an
4793 // accurate estimate of output frame size to determine if we need
4794 // to recode.
4795 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004796 restore_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004797 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004798
4799 rc->projected_frame_size = (int)(*size) << 3;
4800 restore_coding_context(cpi);
4801
4802 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4803 }
4804
Yaowu Xuf883b422016-08-30 14:01:10 -07004805 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004806 loop = 0;
4807 } else {
4808 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4809 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4810 int last_q = q;
4811 int64_t kf_err;
4812
4813 int64_t high_err_target = cpi->ambient_err;
4814 int64_t low_err_target = cpi->ambient_err >> 1;
4815
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004816#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004817 if (cm->use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004818 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004820 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004821 }
4822#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07004823 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004824#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004825
4826 // Prevent possible divide by zero error below for perfect KF
4827 kf_err += !kf_err;
4828
4829 // The key frame is not good enough or we can afford
4830 // to make it better without undue risk of popping.
4831 if ((kf_err > high_err_target &&
4832 rc->projected_frame_size <= frame_over_shoot_limit) ||
4833 (kf_err > low_err_target &&
4834 rc->projected_frame_size <= frame_under_shoot_limit)) {
4835 // Lower q_high
4836 q_high = q > q_low ? q - 1 : q_low;
4837
4838 // Adjust Q
4839 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004840 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004841 } else if (kf_err < low_err_target &&
4842 rc->projected_frame_size >= frame_under_shoot_limit) {
4843 // The key frame is much better than the previous frame
4844 // Raise q_low
4845 q_low = q < q_high ? q + 1 : q_high;
4846
4847 // Adjust Q
4848 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004849 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004850 }
4851
4852 // Clamp Q to upper and lower limits:
4853 q = clamp(q, q_low, q_high);
4854
4855 loop = q != last_q;
4856 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4857 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004858 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004859 // Is the projected frame size out of range and are we allowed
4860 // to attempt to recode.
4861 int last_q = q;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004862#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004863 int retries = 0;
4864
Yaowu Xuc27fc142016-08-22 16:08:15 -07004865 // Frame size out of permitted range:
4866 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004867 // Frame is too large
4868 if (rc->projected_frame_size > rc->this_frame_target) {
4869 // Special case if the projected size is > the max allowed.
4870 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4871 q_high = rc->worst_quality;
4872
4873 // Raise Qlow as to at least the current value
4874 q_low = q < q_high ? q + 1 : q_high;
4875
4876 if (undershoot_seen || loop_at_this_size > 1) {
4877 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004878 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004879
4880 q = (q_high + q_low + 1) / 2;
4881 } else {
4882 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004883 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004884
Yaowu Xuf883b422016-08-30 14:01:10 -07004885 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004886 AOMMAX(q_high, top_index), cm->width,
4887 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004888
4889 while (q < q_low && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004890 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004891 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004892 AOMMAX(q_high, top_index), cm->width,
4893 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004894 retries++;
4895 }
4896 }
4897
4898 overshoot_seen = 1;
4899 } else {
4900 // Frame is too small
4901 q_high = q > q_low ? q - 1 : q_low;
4902
4903 if (overshoot_seen || loop_at_this_size > 1) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004904 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004905 q = (q_high + q_low) / 2;
4906 } else {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004907 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004908 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004909 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004910 // Special case reset for qlow for constrained quality.
4911 // This should only trigger where there is very substantial
4912 // undershoot on a frame and the auto cq level is above
4913 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004914 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004915 q_low = q;
4916 }
4917
4918 while (q > q_high && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004919 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004920 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004921 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004922 retries++;
4923 }
4924 }
4925
4926 undershoot_seen = 1;
4927 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004928#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004929
4930 // Clamp Q to upper and lower limits:
4931 q = clamp(q, q_low, q_high);
4932
4933 loop = (q != last_q);
4934 } else {
4935 loop = 0;
4936 }
4937 }
4938
4939 // Special case for overlay frame.
4940 if (rc->is_src_frame_alt_ref &&
4941 rc->projected_frame_size < rc->max_frame_bandwidth)
4942 loop = 0;
4943
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004944 if (recode_loop_test_global_motion(cpi)) {
4945 loop = 1;
4946 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004947
Yaowu Xuc27fc142016-08-22 16:08:15 -07004948 if (loop) {
4949 ++loop_count;
4950 ++loop_at_this_size;
4951
4952#if CONFIG_INTERNAL_STATS
4953 ++cpi->tot_recode_hits;
4954#endif
4955 }
4956 } while (loop);
4957}
4958
Yaowu Xuf883b422016-08-30 14:01:10 -07004959static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004960 const int *const map = cpi->common.ref_frame_map;
4961
Zoe Liu368bf162017-11-03 20:10:19 -07004962 // No.1 Priority: LAST_FRAME
Yaowu Xuc27fc142016-08-22 16:08:15 -07004963 const int last2_is_last =
4964 map[cpi->lst_fb_idxes[1]] == map[cpi->lst_fb_idxes[0]];
4965 const int last3_is_last =
4966 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[0]];
4967 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[0]];
4968 const int bwd_is_last = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[0]];
Zoe Liu368bf162017-11-03 20:10:19 -07004969 const int alt2_is_last = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004970 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
4971
Zoe Liu368bf162017-11-03 20:10:19 -07004972 // No.2 Priority: ALTREF_FRAME
Yaowu Xuc27fc142016-08-22 16:08:15 -07004973 const int last2_is_alt = map[cpi->lst_fb_idxes[1]] == map[cpi->alt_fb_idx];
4974 const int last3_is_alt = map[cpi->lst_fb_idxes[2]] == map[cpi->alt_fb_idx];
4975 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4976 const int bwd_is_alt = map[cpi->bwd_fb_idx] == map[cpi->alt_fb_idx];
Zoe Liue9b15e22017-07-19 15:53:01 -07004977 const int alt2_is_alt = map[cpi->alt2_fb_idx] == map[cpi->alt_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004978
Zoe Liu368bf162017-11-03 20:10:19 -07004979 // No.3 Priority: LAST2_FRAME
4980 const int last3_is_last2 =
4981 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
4982 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
4983 const int bwd_is_last2 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[1]];
4984 const int alt2_is_last2 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004985
Zoe Liu368bf162017-11-03 20:10:19 -07004986 // No.4 Priority: LAST3_FRAME
4987 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
4988 const int bwd_is_last3 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[2]];
4989 const int alt2_is_last3 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[2]];
4990
4991 // No.5 Priority: GOLDEN_FRAME
4992 const int bwd_is_gld = map[cpi->bwd_fb_idx] == map[cpi->gld_fb_idx];
4993 const int alt2_is_gld = map[cpi->alt2_fb_idx] == map[cpi->gld_fb_idx];
4994
4995 // No.6 Priority: BWDREF_FRAME
4996 const int alt2_is_bwd = map[cpi->alt2_fb_idx] == map[cpi->bwd_fb_idx];
4997
4998 // No.7 Priority: ALTREF2_FRAME
4999
5000 int flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005001
Yaowu Xuf883b422016-08-30 14:01:10 -07005002 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005003
Yaowu Xuf883b422016-08-30 14:01:10 -07005004 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005005
Yaowu Xuf883b422016-08-30 14:01:10 -07005006 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005007
Zoe Liu368bf162017-11-03 20:10:19 -07005008 if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005009
Zoe Liu368bf162017-11-03 20:10:19 -07005010 if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
5011 flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005012
Zoe Liu368bf162017-11-03 20:10:19 -07005013 if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 ||
5014 bwd_is_gld) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07005015 (flags & AOM_BWD_FLAG))
5016 flags &= ~AOM_BWD_FLAG;
Zoe Liue9b15e22017-07-19 15:53:01 -07005017
Zoe Liu368bf162017-11-03 20:10:19 -07005018 if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
5019 alt2_is_gld || alt2_is_bwd) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07005020 (flags & AOM_ALT2_FLAG))
5021 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005022
5023 return flags;
5024}
5025
Yaowu Xuf883b422016-08-30 14:01:10 -07005026static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005027 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07005028 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07005029 // Note: The overrides are valid only for the next frame passed
5030 // to encode_frame_to_data_rate() function
5031 if (cpi->ext_refresh_frame_context_pending) {
5032 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
5033 cpi->ext_refresh_frame_context_pending = 0;
5034 }
5035 if (cpi->ext_refresh_frame_flags_pending) {
5036 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
5037 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
5038 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
5039 cpi->ext_refresh_frame_flags_pending = 0;
5040 }
5041}
5042
Zoe Liu17af2742017-10-06 10:36:42 -07005043#if !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuf883b422016-08-30 14:01:10 -07005044static void set_arf_sign_bias(AV1_COMP *cpi) {
5045 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005046 int arf_sign_bias;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005047 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liu3ac20932017-08-30 16:35:55 -07005048 // The arf_sign_bias will be one for internal ARFs'
Zoe Liue9b15e22017-07-19 15:53:01 -07005049 arf_sign_bias = cpi->rc.source_alt_ref_active &&
5050 (!cpi->refresh_alt_ref_frame ||
5051 gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE);
Zoe Liu6cfaff92016-10-18 17:12:11 -07005052
Yaowu Xuc27fc142016-08-22 16:08:15 -07005053 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005054 cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
Zoe Liue9b15e22017-07-19 15:53:01 -07005055 cm->ref_frame_sign_bias[ALTREF2_FRAME] =
5056 cm->ref_frame_sign_bias[ALTREF_FRAME];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005057}
Zoe Liu17af2742017-10-06 10:36:42 -07005058#endif // !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005059
Yaowu Xuf883b422016-08-30 14:01:10 -07005060static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07005061 InterpFilter ifilter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005062 int ref_total[TOTAL_REFS_PER_FRAME] = { 0 };
5063 MV_REFERENCE_FRAME ref;
5064 int mask = 0;
5065 int arf_idx = ALTREF_FRAME;
5066
Zoe Liue9b15e22017-07-19 15:53:01 -07005067 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
5068 cpi->refresh_alt2_ref_frame)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005069 return mask;
5070
Yaowu Xuc27fc142016-08-22 16:08:15 -07005071 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
5072 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
5073 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005074
5075 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
5076 if ((ref_total[LAST_FRAME] &&
5077 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005078 (ref_total[LAST2_FRAME] == 0 ||
5079 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
5080 ref_total[LAST2_FRAME]) &&
5081 (ref_total[LAST3_FRAME] == 0 ||
5082 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
5083 ref_total[LAST3_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005084 (ref_total[GOLDEN_FRAME] == 0 ||
5085 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
5086 ref_total[GOLDEN_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005087 (ref_total[BWDREF_FRAME] == 0 ||
5088 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
5089 ref_total[BWDREF_FRAME]) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07005090 (ref_total[ALTREF2_FRAME] == 0 ||
5091 cpi->interp_filter_selected[ALTREF2_FRAME][ifilter] * 50 <
5092 ref_total[ALTREF2_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005093 (ref_total[ALTREF_FRAME] == 0 ||
5094 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
5095 ref_total[ALTREF_FRAME]))
5096 mask |= 1 << ifilter;
5097 }
5098 return mask;
5099}
5100
5101#define DUMP_RECON_FRAMES 0
5102
5103#if DUMP_RECON_FRAMES == 1
5104// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005105static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5106 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005107 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005108
Zoe Liub4f31032017-11-03 23:48:35 -07005109 if (recon_buf == NULL) {
5110 printf("Frame %d is not ready.\n", cm->current_video_frame);
5111 return;
5112 }
5113
5114#if CONFIG_FRAME_MARKER
5115 static const int flag_list[TOTAL_REFS_PER_FRAME] = { 0,
5116 AOM_LAST_FLAG,
5117 AOM_LAST2_FLAG,
5118 AOM_LAST3_FLAG,
5119 AOM_GOLD_FLAG,
5120 AOM_BWD_FLAG,
5121 AOM_ALT2_FLAG,
5122 AOM_ALT_FLAG };
5123 printf(
5124 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5125 "show_existing_frame=%d) "
5126 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
5127 cm->current_video_frame, cm->frame_offset, cm->show_frame,
5128 cm->show_existing_frame);
5129 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
5130 const int buf_idx = cm->frame_refs[ref_frame - LAST_FRAME].idx;
5131 const int ref_offset =
5132 (buf_idx >= 0)
5133 ? (int)cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset
5134 : -1;
5135 printf(" %d(%c)", ref_offset,
5136 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
5137 }
5138 printf(" ]\n");
5139#endif // CONFIG_FRAME_MARKER
5140
5141 if (!cm->show_frame) {
5142 printf("Frame %d is a no show frame, so no image dump.\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07005143 cm->current_video_frame);
5144 return;
5145 }
5146
Zoe Liub4f31032017-11-03 23:48:35 -07005147 int h;
5148 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5149 FILE *f_recon = NULL;
5150
Yaowu Xuc27fc142016-08-22 16:08:15 -07005151 if (cm->current_video_frame == 0) {
5152 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5153 printf("Unable to open file %s to write.\n", file_name);
5154 return;
5155 }
5156 } else {
5157 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5158 printf("Unable to open file %s to append.\n", file_name);
5159 return;
5160 }
5161 }
5162 printf(
5163 "\nFrame=%5d, encode_update_type[%5d]=%1d, show_existing_frame=%d, "
Zoe Liufcf5fa22017-06-26 16:00:38 -07005164 "source_alt_ref_active=%d, refresh_alt_ref_frame=%d, rf_level=%d, "
5165 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07005166 cm->current_video_frame, cpi->twopass.gf_group.index,
5167 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liufcf5fa22017-06-26 16:00:38 -07005168 cm->show_existing_frame, cpi->rc.source_alt_ref_active,
5169 cpi->refresh_alt_ref_frame,
5170 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
5171 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07005172#if 0
5173 int ref_frame;
5174 printf("get_ref_frame_map_idx: [");
5175 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
5176 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
5177 printf(" ]\n");
5178 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
5179 printf("cm->ref_frame_map = [");
5180 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
5181 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
5182 }
5183 printf(" ]\n");
5184#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005185
5186 // --- Y ---
5187 for (h = 0; h < cm->height; ++h) {
5188 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5189 f_recon);
5190 }
5191 // --- U ---
5192 for (h = 0; h < (cm->height >> 1); ++h) {
5193 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5194 f_recon);
5195 }
5196 // --- V ---
5197 for (h = 0; h < (cm->height >> 1); ++h) {
5198 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5199 f_recon);
5200 }
5201
5202 fclose(f_recon);
5203}
5204#endif // DUMP_RECON_FRAMES
5205
Thomas Davies4822e142017-10-10 11:30:36 +01005206static void make_update_tile_list_enc(AV1_COMP *cpi, const int start_tile,
5207 const int num_tiles,
Thomas Davies028b57f2017-02-22 16:42:11 +00005208 FRAME_CONTEXT *ec_ctxs[]) {
5209 int i;
Thomas Davies4822e142017-10-10 11:30:36 +01005210 for (i = start_tile; i < start_tile + num_tiles; ++i)
5211 ec_ctxs[i - start_tile] = &cpi->tile_data[i].tctx;
Thomas Davies028b57f2017-02-22 16:42:11 +00005212}
5213
Yaowu Xuf883b422016-08-30 14:01:10 -07005214static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005215 uint8_t *dest, int skip_adapt,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005216 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005217 AV1_COMMON *const cm = &cpi->common;
5218 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005219 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005220#if CONFIG_SIMPLE_BWD_ADAPT
5221 const int num_bwd_ctxs = 1;
5222#else
5223 const int num_bwd_ctxs = cm->tile_rows * cm->tile_cols;
5224#endif
5225
5226 FRAME_CONTEXT **tile_ctxs =
5227 aom_malloc(num_bwd_ctxs * sizeof(&cpi->tile_data[0].tctx));
5228 aom_cdf_prob **cdf_ptrs = aom_malloc(
5229 num_bwd_ctxs * sizeof(&cpi->tile_data[0].tctx.partition_cdf[0][0]));
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005230#if CONFIG_XIPHRC
5231 int frame_type;
5232 int drop_this_frame = 0;
Zoe Liue04abf72017-04-19 15:37:11 -07005233#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005234 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07005235 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005236
Zoe Liu17af2742017-10-06 10:36:42 -07005237#if !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005238 // Set the arf sign bias for this frame.
5239 set_arf_sign_bias(cpi);
Zoe Liu17af2742017-10-06 10:36:42 -07005240#endif // !CONFIG_FRAME_SIGN_BIAS
5241
Fangwen Fu8d164de2016-12-14 13:40:54 -08005242#if CONFIG_TEMPMV_SIGNALING
5243 // frame type has been decided outside of this function call
5244 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
Jingning Hane17ebe92017-11-03 15:25:42 -07005245 cm->use_ref_frame_mvs =
Fangwen Fu8d164de2016-12-14 13:40:54 -08005246 !cpi->oxcf.disable_tempmv && !cm->cur_frame->intra_only;
Jingning Hane17ebe92017-11-03 15:25:42 -07005247 cm->use_prev_frame_mvs = cm->use_ref_frame_mvs;
Fangwen Fu8d164de2016-12-14 13:40:54 -08005248#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005249
Jingning Hand8a15a62017-10-30 10:53:42 -07005250 // Reset the frame packet stamp index.
5251 if (cm->frame_type == KEY_FRAME) cm->current_video_frame = 0;
5252
Yaowu Xuc27fc142016-08-22 16:08:15 -07005253 // NOTE:
5254 // (1) Move the setup of the ref_frame_flags upfront as it would be
5255 // determined by the current frame properties;
5256 // (2) The setup of the ref_frame_flags applies to both show_existing_frame's
5257 // and the other cases.
5258 if (cm->current_video_frame > 0)
5259 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5260
5261 if (cm->show_existing_frame) {
5262 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
5263 // BWDREF_FRAME in the reference frame buffer.
5264 cm->frame_type = INTER_FRAME;
5265 cm->show_frame = 1;
5266 cpi->frame_flags = *frame_flags;
5267
5268 // In the case of show_existing frame, we will not send fresh flag
5269 // to decoder. Any change in the reference frame buffer can be done by
5270 // switching the virtual indices.
5271
5272 cpi->refresh_last_frame = 0;
5273 cpi->refresh_golden_frame = 0;
5274 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005275 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005276 cpi->refresh_alt_ref_frame = 0;
5277
5278 cpi->rc.is_bwd_ref_frame = 0;
5279 cpi->rc.is_last_bipred_frame = 0;
5280 cpi->rc.is_bipred_frame = 0;
5281
Jingning Han8f661602017-08-19 08:16:50 -07005282 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07005283
Yaowu Xuc27fc142016-08-22 16:08:15 -07005284 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07005285 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005286
5287 // Set up frame to show to get ready for stats collection.
5288 cm->frame_to_show = get_frame_new_buffer(cm);
5289
Zoe Liub4f31032017-11-03 23:48:35 -07005290#if CONFIG_FRAME_MARKER
5291 // Update current frame offset.
5292 cm->frame_offset =
5293 cm->buffer_pool->frame_bufs[cm->new_fb_idx].cur_frame_offset;
5294#endif // CONFIG_FRAME_MARKER
5295
Yaowu Xuc27fc142016-08-22 16:08:15 -07005296#if DUMP_RECON_FRAMES == 1
5297 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5298 dump_filtered_recon_frames(cpi);
5299#endif // DUMP_RECON_FRAMES
5300
5301 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07005302 // NOTE:
5303 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
5304 // update has been done previously when handling the LAST_BIPRED_FRAME
5305 // right before BWDREF_FRAME (in the display order);
5306 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
5307 // update will be done when the following is called, which will exchange
5308 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
5309 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2, and
5310 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Cheng Chen46f30c72017-09-07 11:13:33 -07005311 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005312
5313 // Update frame flags
5314 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5315 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
5316 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5317
5318 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5319
5320 // Update the frame type
5321 cm->last_frame_type = cm->frame_type;
5322
Yaowu Xuc27fc142016-08-22 16:08:15 -07005323 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5324 // to do post-encoding update accordingly.
5325 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005326 av1_set_target_rate(cpi, cm->width, cm->height);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005327#if CONFIG_XIPHRC
Zoe Liue04abf72017-04-19 15:37:11 -07005328 frame_type = cm->frame_type == INTER_FRAME ? OD_P_FRAME : OD_I_FRAME;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005329 drop_this_frame = od_enc_rc_update_state(
5330 &cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5331 cpi->refresh_alt_ref_frame, frame_type, cpi->droppable);
5332#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005333 av1_rc_postencode_update(cpi, *size);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005334#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005335 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005336
Yaowu Xuc27fc142016-08-22 16:08:15 -07005337 ++cm->current_video_frame;
5338
Jingning Hanf6214b92017-04-12 11:43:37 -07005339 aom_free(tile_ctxs);
5340 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005341 return;
5342 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005343
5344 // Set default state for segment based loop filter update flags.
5345 cm->lf.mode_ref_delta_update = 0;
5346
5347 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5348 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
5349
5350 // Set various flags etc to special state if it is a key frame.
5351 if (frame_is_intra_only(cm)) {
5352 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005353 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005354
5355 // If segmentation is enabled force a map update for key frames.
5356 if (seg->enabled) {
5357 seg->update_map = 1;
5358 seg->update_data = 1;
5359 }
5360
5361 // The alternate reference frame cannot be active for a key frame.
5362 cpi->rc.source_alt_ref_active = 0;
5363
5364 cm->error_resilient_mode = oxcf->error_resilient_mode;
5365
Thomas Daedea6a854b2017-06-22 17:49:11 -07005366#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005367 // By default, encoder assumes decoder can use prev_mi.
5368 if (cm->error_resilient_mode) {
5369 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
5370 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
5371 } else if (cm->intra_only) {
5372 // Only reset the current context.
5373 cm->reset_frame_context = RESET_FRAME_CONTEXT_CURRENT;
5374 }
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005375#if CONFIG_EXT_TILE
5376 if (cpi->oxcf.large_scale_tile)
5377 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
5378#endif // CONFIG_EXT_TILE
5379#endif // !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005380 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005381 if (cpi->oxcf.mtu == 0) {
5382 cm->num_tg = cpi->oxcf.num_tile_groups;
5383 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005384 // Use a default value for the purposes of weighting costs in probability
5385 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005386 cm->num_tg = DEFAULT_MAX_NUM_TG;
5387 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005388
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005389#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07005390 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5391 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005392#endif // CONFIG_EXT_TILE
5393
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005394#if CONFIG_XIPHRC
5395 if (drop_this_frame) {
5396 av1_rc_postencode_update_drop_frame(cpi);
5397 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005398 aom_free(tile_ctxs);
5399 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005400 return;
5401 }
5402#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07005403 // For 1 pass CBR, check if we are dropping this frame.
5404 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005405 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005406 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005407 if (av1_rc_drop_frame(cpi)) {
5408 av1_rc_postencode_update_drop_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005409 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005410 aom_free(tile_ctxs);
5411 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005412 return;
5413 }
5414 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005415#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005416
Yaowu Xuf883b422016-08-30 14:01:10 -07005417 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005418
5419#if CONFIG_INTERNAL_STATS
5420 memset(cpi->mode_chosen_counts, 0,
5421 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5422#endif
5423
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005424#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01005425 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005426 /* Non-normative definition of current_frame_id ("frame counter" with
5427 * wraparound) */
Sebastien Alaiwand418f682017-10-19 15:06:52 +02005428 const int frame_id_length = FRAME_ID_LENGTH;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005429 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005430 int lsb, msb;
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005431/* quasi-random initialization of current_frame_id for a key frame */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005432#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07005433 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5434 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5435 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005436 } else {
5437#endif
Alex Conversef77fd0b2017-04-20 11:00:24 -07005438 lsb = cpi->source->y_buffer[0] & 0xff;
5439 msb = cpi->source->y_buffer[1] & 0xff;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005440#if CONFIG_HIGHBITDEPTH
David Barker49a76562016-12-07 14:50:21 +00005441 }
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005442#endif
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005443 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005444 } else {
5445 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005446 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
5447 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005448 }
5449 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005450#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005451
Fangwen Fu6160df22017-04-24 09:45:51 -07005452#if CONFIG_EXT_DELTA_Q
5453 cm->delta_q_present_flag = cpi->oxcf.deltaq_mode != NO_DELTA_Q;
Fangwen Fu231fe422017-04-24 17:52:29 -07005454 cm->delta_lf_present_flag = cpi->oxcf.deltaq_mode == DELTA_Q_LF;
Cheng Chen880166a2017-10-02 17:48:48 -07005455#if CONFIG_LOOPFILTER_LEVEL
5456 cm->delta_lf_multi = DEFAULT_DELTA_LF_MULTI;
5457#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu6160df22017-04-24 09:45:51 -07005458#endif
5459
Yaowu Xuc27fc142016-08-22 16:08:15 -07005460 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
5461 encode_without_recode_loop(cpi);
5462 } else {
5463 encode_with_recode_loop(cpi, size, dest);
5464 }
5465
Yi Luo10e23002017-07-31 11:54:43 -07005466 cm->last_tile_cols = cm->tile_cols;
5467 cm->last_tile_rows = cm->tile_rows;
5468
Yaowu Xuc27fc142016-08-22 16:08:15 -07005469#ifdef OUTPUT_YUV_SKINMAP
5470 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005471 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005472 }
5473#endif // OUTPUT_YUV_SKINMAP
5474
5475 // Special case code to reduce pulsing when key frames are forced at a
5476 // fixed interval. Note the reconstruction error if it is the frame before
5477 // the force key frame
5478 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005479#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005480 if (cm->use_highbitdepth) {
5481 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07005482 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005483 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005484 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005485 }
5486#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07005487 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005488#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005489 }
5490
5491 // If the encoder forced a KEY_FRAME decision
5492 if (cm->frame_type == KEY_FRAME) {
5493 cpi->refresh_last_frame = 1;
5494 }
5495
5496 cm->frame_to_show = get_frame_new_buffer(cm);
5497 cm->frame_to_show->color_space = cm->color_space;
anorkin76fb1262017-03-22 15:12:12 -07005498#if CONFIG_COLORSPACE_HEADERS
5499 cm->frame_to_show->transfer_function = cm->transfer_function;
5500 cm->frame_to_show->chroma_sample_position = cm->chroma_sample_position;
5501#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005502 cm->frame_to_show->color_range = cm->color_range;
5503 cm->frame_to_show->render_width = cm->render_width;
5504 cm->frame_to_show->render_height = cm->render_height;
5505
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005506 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5507 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005508
5509 // Pick the loop filter level for the frame.
5510 loopfilter_frame(cpi, cm);
5511
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005512#ifdef OUTPUT_YUV_REC
5513 aom_write_one_yuv_frame(cm, cm->frame_to_show);
5514#endif
5515
Yaowu Xuc27fc142016-08-22 16:08:15 -07005516 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07005517 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005518
Jingning Hanf6214b92017-04-12 11:43:37 -07005519 if (skip_adapt) {
Jingning Hanf6214b92017-04-12 11:43:37 -07005520 aom_free(tile_ctxs);
5521 aom_free(cdf_ptrs);
Jingning Hanf6214b92017-04-12 11:43:37 -07005522 return;
5523 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005524
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005525#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01005526 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005527 int i;
5528 /* Update reference frame id values based on the value of refresh_mask */
5529 for (i = 0; i < REF_FRAMES; i++) {
5530 if ((cm->refresh_mask >> i) & 1) {
5531 cm->ref_frame_id[i] = cm->current_frame_id;
5532 }
5533 }
5534 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005535#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005536
Yaowu Xuc27fc142016-08-22 16:08:15 -07005537#if DUMP_RECON_FRAMES == 1
5538 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005539 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005540#endif // DUMP_RECON_FRAMES
5541
5542 if (cm->seg.update_map) update_reference_segmentation_map(cpi);
5543
5544 if (frame_is_intra_only(cm) == 0) {
5545 release_scaled_references(cpi);
5546 }
5547
Cheng Chen46f30c72017-09-07 11:13:33 -07005548 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005549
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005550#if CONFIG_ENTROPY_STATS
5551 av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
Zoe Liua56f9162017-06-21 22:49:57 -07005552 assert(cm->frame_context_idx < FRAME_CONTEXTS);
5553 av1_accumulate_frame_counts(&aggregate_fc_per_type[cm->frame_context_idx],
5554 &cm->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005555#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005556 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005557 av1_adapt_intra_frame_probs(cm);
Thomas Davies4822e142017-10-10 11:30:36 +01005558#if CONFIG_SIMPLE_BWD_ADAPT
5559 make_update_tile_list_enc(cpi, cm->largest_tile_id, 1, tile_ctxs);
5560#else
5561 make_update_tile_list_enc(cpi, 0, num_bwd_ctxs, tile_ctxs);
5562#endif
Thomas Davies493623e2017-03-31 16:12:25 +01005563 av1_average_tile_coef_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01005564 num_bwd_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01005565 av1_average_tile_intra_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01005566 num_bwd_ctxs);
Debargha Mukherjee43061b32017-10-13 16:50:17 -07005567 av1_average_tile_loopfilter_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
5568 num_bwd_ctxs);
hui suff0da2b2017-03-07 15:51:37 -08005569#if CONFIG_ADAPT_SCAN
5570 av1_adapt_scan_order(cm);
5571#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005572 }
5573
5574 if (!frame_is_intra_only(cm)) {
5575 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005576 av1_adapt_inter_frame_probs(cm);
5577 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00005578 av1_average_tile_inter_cdfs(&cpi->common, cpi->common.fc, tile_ctxs,
Thomas Davies4822e142017-10-10 11:30:36 +01005579 cdf_ptrs, num_bwd_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01005580 av1_average_tile_mv_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01005581 num_bwd_ctxs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005582 }
5583 }
5584
5585 if (cpi->refresh_golden_frame == 1)
5586 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5587 else
5588 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5589
5590 if (cpi->refresh_alt_ref_frame == 1)
5591 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5592 else
5593 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5594
Yaowu Xuc27fc142016-08-22 16:08:15 -07005595 if (cpi->refresh_bwd_ref_frame == 1)
5596 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5597 else
5598 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005599
Yaowu Xuc27fc142016-08-22 16:08:15 -07005600 cm->last_frame_type = cm->frame_type;
5601
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005602#if CONFIG_XIPHRC
5603 frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
5604
5605 drop_this_frame =
5606 od_enc_rc_update_state(&cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5607 cpi->refresh_alt_ref_frame, frame_type, 0);
5608 if (drop_this_frame) {
5609 av1_rc_postencode_update_drop_frame(cpi);
5610 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005611 aom_free(tile_ctxs);
5612 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005613 return;
5614 }
Zoe Liue04abf72017-04-19 15:37:11 -07005615#else // !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005616 av1_rc_postencode_update(cpi, *size);
Zoe Liue04abf72017-04-19 15:37:11 -07005617#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005618
5619#if 0
5620 output_frame_level_debug_stats(cpi);
5621#endif
5622
5623 if (cm->frame_type == KEY_FRAME) {
5624 // Tell the caller that the frame was coded as a key frame
5625 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5626 } else {
5627 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5628 }
5629
5630 // Clear the one shot update flags for segmentation map and mode/ref loop
5631 // filter deltas.
5632 cm->seg.update_map = 0;
5633 cm->seg.update_data = 0;
5634 cm->lf.mode_ref_delta_update = 0;
5635
Yaowu Xuc27fc142016-08-22 16:08:15 -07005636 if (cm->show_frame) {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005637 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that are
5638 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005639 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005640 // Don't increment frame counters if this was an altref buffer
5641 // update not a real frame
5642 ++cm->current_video_frame;
5643 }
5644
Yaowu Xuc27fc142016-08-22 16:08:15 -07005645 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005646 if (cm->is_reference_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005647 cm->prev_frame = cm->cur_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005648 // keep track of the last coded dimensions
5649 cm->last_width = cm->width;
5650 cm->last_height = cm->height;
5651
5652 // reset to normal state now that we are done.
5653 cm->last_show_frame = cm->show_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005654 }
Yi Luo10e23002017-07-31 11:54:43 -07005655
Thomas Davies493623e2017-03-31 16:12:25 +01005656 aom_free(tile_ctxs);
5657 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005658}
5659
Yaowu Xuf883b422016-08-30 14:01:10 -07005660static void Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005661 int skip_adapt, unsigned int *frame_flags) {
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005662#if CONFIG_XIPHRC
5663 int64_t ip_count;
5664 int frame_type, is_golden, is_altref;
5665
5666 /* Not updated during init so update it here */
5667 if (cpi->oxcf.rc_mode == AOM_Q) cpi->od_rc.quality = cpi->oxcf.cq_level;
5668
5669 frame_type = od_frame_type(&cpi->od_rc, cpi->od_rc.cur_frame, &is_golden,
5670 &is_altref, &ip_count);
5671
5672 if (frame_type == OD_I_FRAME) {
5673 frame_type = KEY_FRAME;
5674 cpi->frame_flags &= FRAMEFLAGS_KEY;
5675 } else if (frame_type == OD_P_FRAME) {
5676 frame_type = INTER_FRAME;
5677 }
5678
5679 if (is_altref) {
5680 cpi->refresh_alt_ref_frame = 1;
5681 cpi->rc.source_alt_ref_active = 1;
5682 }
5683
5684 cpi->refresh_golden_frame = is_golden;
5685 cpi->common.frame_type = frame_type;
5686 if (is_golden) cpi->frame_flags &= FRAMEFLAGS_GOLDEN;
5687#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005688 if (cpi->oxcf.rc_mode == AOM_CBR) {
5689 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005690 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005691 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005692 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005693#endif
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005694 encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005695}
5696
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005697#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005698static void Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005699 unsigned int *frame_flags) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005700 encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005701
Yaowu Xuc27fc142016-08-22 16:08:15 -07005702 // Do not do post-encoding update for those frames that do not have a spot in
5703 // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
5704 // even when show_existing_frame is used.
5705 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005706 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005707 }
5708 check_show_existing_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005709}
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005710#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005711
James Zern3e2613b2017-03-30 23:14:40 -07005712int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005713 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5714 int64_t end_time) {
5715 AV1_COMMON *const cm = &cpi->common;
5716 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005717 int res = 0;
5718 const int subsampling_x = sd->subsampling_x;
5719 const int subsampling_y = sd->subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005720#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005721 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5722#endif
5723
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005724#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005725 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5726#else
5727 check_initial_width(cpi, subsampling_x, subsampling_y);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005728#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005729
Yaowu Xuf883b422016-08-30 14:01:10 -07005730 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005731
Yaowu Xuf883b422016-08-30 14:01:10 -07005732 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005733#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005734 use_highbitdepth,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005735#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005736 frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005737 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005738 aom_usec_timer_mark(&timer);
5739 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005740
5741 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
5742 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005743 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005744 "Non-4:2:0 color format requires profile 1 or 3");
5745 res = -1;
5746 }
5747 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
5748 (subsampling_x == 1 && subsampling_y == 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005749 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005750 "4:2:0 color format requires profile 0 or 2");
5751 res = -1;
5752 }
5753
5754 return res;
5755}
5756
Yaowu Xuf883b422016-08-30 14:01:10 -07005757static int frame_is_reference(const AV1_COMP *cpi) {
5758 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005759
5760 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005761 cpi->refresh_golden_frame || cpi->refresh_bwd_ref_frame ||
5762 cpi->refresh_alt2_ref_frame || cpi->refresh_alt_ref_frame ||
5763 !cm->error_resilient_mode || cm->lf.mode_ref_delta_update ||
5764 cm->seg.update_map || cm->seg.update_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005765}
5766
Yaowu Xuf883b422016-08-30 14:01:10 -07005767static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005768 const struct lookahead_entry *source) {
5769 int64_t this_duration;
5770 int step = 0;
5771
5772 if (source->ts_start == cpi->first_time_stamp_ever) {
5773 this_duration = source->ts_end - source->ts_start;
5774 step = 1;
5775 } else {
5776 int64_t last_duration =
5777 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5778
5779 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5780
5781 // do a step update if the duration changes by 10%
5782 if (last_duration)
5783 step = (int)((this_duration - last_duration) * 10 / last_duration);
5784 }
5785
5786 if (this_duration) {
5787 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005788 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005789 } else {
5790 // Average this frame's rate into the last second's average
5791 // frame rate. If we haven't seen 1 second yet, then average
5792 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005793 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005794 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5795 double avg_duration = 10000000.0 / cpi->framerate;
5796 avg_duration *= (interval - avg_duration + this_duration);
5797 avg_duration /= interval;
5798
Yaowu Xuf883b422016-08-30 14:01:10 -07005799 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005800 }
5801 }
5802 cpi->last_time_stamp_seen = source->ts_start;
5803 cpi->last_end_time_stamp_seen = source->ts_end;
5804}
5805
5806// Returns 0 if this is not an alt ref else the offset of the source frame
5807// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005808static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005809 RATE_CONTROL *const rc = &cpi->rc;
5810 int arf_src_index = 0;
5811 if (is_altref_enabled(cpi)) {
5812 if (cpi->oxcf.pass == 2) {
5813 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5814 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5815 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5816 }
5817 } else if (rc->source_alt_ref_pending) {
5818 arf_src_index = rc->frames_till_gf_update_due;
5819 }
5820 }
5821 return arf_src_index;
5822}
5823
Yaowu Xuf883b422016-08-30 14:01:10 -07005824static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005825 int brf_src_index = 0;
5826 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5827
5828 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5829 // flag.
5830 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5831 if (cpi->oxcf.pass == 2) {
5832 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5833 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5834 } else {
5835 // TODO(zoeliu): To re-visit the setup for this scenario
5836 brf_src_index = cpi->rc.bipred_group_interval - 1;
5837 }
5838 }
5839
5840 return brf_src_index;
5841}
Zoe Liue9b15e22017-07-19 15:53:01 -07005842
Zoe Liue9b15e22017-07-19 15:53:01 -07005843// Returns 0 if this is not an alt ref else the offset of the source frame
5844// used as the arf midpoint.
5845static int get_arf2_src_index(AV1_COMP *cpi) {
5846 int arf2_src_index = 0;
5847 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5848 if (cpi->oxcf.pass == 2) {
5849 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5850 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5851 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5852 }
5853 }
5854 }
5855 return arf2_src_index;
5856}
Yaowu Xuc27fc142016-08-22 16:08:15 -07005857
Yaowu Xuf883b422016-08-30 14:01:10 -07005858static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005859 const struct lookahead_entry *source) {
5860 RATE_CONTROL *const rc = &cpi->rc;
5861
5862 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005863 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005864
5865 if (cpi->oxcf.pass == 2) {
5866 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5867 rc->is_src_frame_alt_ref =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005868 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07005869 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07005870 rc->is_src_frame_ext_arf =
5871 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005872 } else {
5873 rc->is_src_frame_alt_ref =
5874 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5875 }
5876
5877 if (rc->is_src_frame_alt_ref) {
5878 // Current frame is an ARF overlay frame.
5879 cpi->alt_ref_source = NULL;
5880
Zoe Liue9b15e22017-07-19 15:53:01 -07005881 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
5882 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
5883 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
5884 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
5885 cpi->refresh_last_frame = 1;
5886 } else {
Zoe Liue9b15e22017-07-19 15:53:01 -07005887 // Don't refresh the last buffer for an ARF overlay frame. It will
5888 // become the GF so preserve last as an alternative prediction option.
5889 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005890 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005891 }
5892}
5893
5894#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005895extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5896 const unsigned char *img2, int img2_pitch,
5897 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005898
5899static void adjust_image_stat(double y, double u, double v, double all,
5900 ImageStat *s) {
5901 s->stat[Y] += y;
5902 s->stat[U] += u;
5903 s->stat[V] += v;
5904 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005905 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005906}
5907
Angie Chiang08a22a62017-07-17 17:29:17 -07005908static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005909 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005910 double samples = 0.0;
5911 uint32_t in_bit_depth = 8;
5912 uint32_t bit_depth = 8;
5913
Angie Chiang08a22a62017-07-17 17:29:17 -07005914#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07005915 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005916#endif
5917 cpi->bytes += frame_bytes;
5918
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005919#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005920 if (cm->use_highbitdepth) {
5921 in_bit_depth = cpi->oxcf.input_bit_depth;
5922 bit_depth = cm->bit_depth;
5923 }
5924#endif
5925 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005926 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005927 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5928 double y, u, v, frame_all;
5929
5930 cpi->count++;
5931 if (cpi->b_calculate_psnr) {
5932 PSNR_STATS psnr;
5933 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005934 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005935// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005936#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005937 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005938#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005939 aom_calc_psnr(orig, recon, &psnr);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005940#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005941
5942 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5943 &cpi->psnr);
5944 cpi->total_sq_error += psnr.sse[0];
5945 cpi->total_samples += psnr.samples[0];
5946 samples = psnr.samples[0];
5947// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005948#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005949 if (cm->use_highbitdepth)
5950 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005951 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005952 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005953 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005954#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005955 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005956#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005957
Yaowu Xuf883b422016-08-30 14:01:10 -07005958 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005959 cpi->summed_quality += frame_ssim2 * weight;
5960 cpi->summed_weights += weight;
5961
5962#if 0
5963 {
5964 FILE *f = fopen("q_used.stt", "a");
5965 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5966 cpi->common.current_video_frame, y2, u2, v2,
5967 frame_psnr2, frame_ssim2);
5968 fclose(f);
5969 }
5970#endif
5971 }
5972 if (cpi->b_calculate_blockiness) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005973#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005974 if (!cm->use_highbitdepth)
5975#endif
5976 {
5977 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005978 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5979 recon->y_stride, orig->y_width, orig->y_height);
5980 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005981 cpi->total_blockiness += frame_blockiness;
5982 }
5983
5984 if (cpi->b_calculate_consistency) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005985#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005986 if (!cm->use_highbitdepth)
5987#endif
5988 {
Yaowu Xuf883b422016-08-30 14:01:10 -07005989 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005990 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5991 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5992
5993 const double peak = (double)((1 << in_bit_depth) - 1);
5994 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005995 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005996 if (consistency > 0.0)
5997 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005998 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005999 cpi->total_inconsistency += this_inconsistency;
6000 }
6001 }
6002 }
6003
6004 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006005 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006006 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006007 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006008 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6009 }
6010}
6011#endif // CONFIG_INTERNAL_STATS
6012
RogerZhou3b635242017-09-19 10:06:46 -07006013#if CONFIG_AMVR
6014static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
6015 const YV12_BUFFER_CONFIG *last_picture,
6016 hash_table *last_hash_table) {
6017 aom_clear_system_state();
6018 // check use hash ME
6019 int k;
6020 uint32_t hash_value_1;
6021 uint32_t hash_value_2;
6022
6023 const int block_size = 8;
6024 const double threshold_current = 0.8;
6025 const double threshold_average = 0.95;
6026 const int max_history_size = 32;
6027 int T = 0; // total block
6028 int C = 0; // match with collocated block
6029 int S = 0; // smooth region but not match with collocated block
6030 int M = 0; // match with other block
6031
6032 const int pic_width = cur_picture->y_width;
6033 const int pic_height = cur_picture->y_height;
6034 for (int i = 0; i + block_size <= pic_height; i += block_size) {
6035 for (int j = 0; j + block_size <= pic_width; j += block_size) {
6036 const int x_pos = j;
6037 const int y_pos = i;
6038 int match = 1;
6039 T++;
6040
6041 // check whether collocated block match with current
6042 uint8_t *p_cur = cur_picture->y_buffer;
6043 uint8_t *p_ref = last_picture->y_buffer;
6044 int stride_cur = cur_picture->y_stride;
6045 int stride_ref = last_picture->y_stride;
6046 p_cur += (y_pos * stride_cur + x_pos);
6047 p_ref += (y_pos * stride_ref + x_pos);
6048
6049 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6050 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6051 if (p_cur[tmpX] != p_ref[tmpX]) {
6052 match = 0;
6053 }
6054 }
6055 p_cur += stride_cur;
6056 p_ref += stride_ref;
6057 }
6058
6059 if (match) {
6060 C++;
6061 continue;
6062 }
6063
6064 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
6065 y_pos) ||
6066 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
6067 S++;
6068 continue;
6069 }
6070
6071 av1_get_block_hash_value(
6072 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
6073 block_size, &hash_value_1, &hash_value_2);
6074
6075 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
6076 M++;
6077 }
6078 }
6079 }
6080
6081 assert(T > 0);
6082 double csm_rate = ((double)(C + S + M)) / ((double)(T));
6083 double m_rate = ((double)(M)) / ((double)(T));
6084
6085 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
6086 cpi->m_rate_array[cpi->rate_index] = m_rate;
6087
6088 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
6089 cpi->rate_size++;
6090 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
6091
6092 if (csm_rate < threshold_current) {
6093 return 0;
6094 }
6095
6096 if (C == T) {
6097 return 1;
6098 }
6099
6100 double csm_average = 0.0;
6101 double m_average = 0.0;
6102
6103 for (k = 0; k < cpi->rate_size; k++) {
6104 csm_average += cpi->csm_rate_array[k];
6105 m_average += cpi->m_rate_array[k];
6106 }
6107 csm_average /= cpi->rate_size;
6108 m_average /= cpi->rate_size;
6109
6110 if (csm_average < threshold_average) {
6111 return 0;
6112 }
6113
6114 if (M > (T - C - S) / 3) {
6115 return 1;
6116 }
6117
6118 if (csm_rate > 0.99 && m_rate > 0.01) {
6119 return 1;
6120 }
6121
6122 if (csm_average + m_average > 1.01) {
6123 return 1;
6124 }
6125
6126 return 0;
6127}
6128#endif
6129
Yaowu Xuf883b422016-08-30 14:01:10 -07006130int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6131 size_t *size, uint8_t *dest, int64_t *time_stamp,
6132 int64_t *time_end, int flush) {
6133 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6134 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006135 BufferPool *const pool = cm->buffer_pool;
6136 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07006137 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006138 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
6139 struct lookahead_entry *last_source = NULL;
6140 struct lookahead_entry *source = NULL;
6141 int arf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006142 int brf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006143 int i;
6144
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006145#if CONFIG_XIPHRC
6146 cpi->od_rc.end_of_input = flush;
6147#endif
6148
Yaowu Xuc27fc142016-08-22 16:08:15 -07006149#if CONFIG_BITSTREAM_DEBUG
6150 assert(cpi->oxcf.max_threads == 0 &&
6151 "bitstream debug tool does not support multithreading");
6152 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07006153 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006154#endif
6155
Yaowu Xuf883b422016-08-30 14:01:10 -07006156 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006157
RogerZhou3b635242017-09-19 10:06:46 -07006158#if CONFIG_AMVR
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07006159#if CONFIG_EIGHTH_PEL_MV_ONLY
6160 set_high_precision_mv(cpi, 1, 0);
6161#else
RogerZhou3b635242017-09-19 10:06:46 -07006162 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07006163#endif // CONFIG_EIGHTH_PEL_MV_ONLY
6164#else
6165#if CONFIG_EIGHTH_PEL_MV_ONLY
6166 set_high_precision_mv(cpi, 1);
RogerZhou3b635242017-09-19 10:06:46 -07006167#else
Cheng Chen46f30c72017-09-07 11:13:33 -07006168 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07006169#endif // CONFIG_EIGHTH_PEL_MV_ONLY
RogerZhou3b635242017-09-19 10:06:46 -07006170#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006171
6172 // Is multi-arf enabled.
6173 // Note that at the moment multi_arf is only configured for 2 pass VBR
6174 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
6175 cpi->multi_arf_allowed = 1;
6176 else
6177 cpi->multi_arf_allowed = 0;
6178
Thomas Daedea6a854b2017-06-22 17:49:11 -07006179// Normal defaults
6180#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07006181 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07006182#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006183 cm->refresh_frame_context =
6184 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
6185 ? REFRESH_FRAME_CONTEXT_FORWARD
6186 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006187#if CONFIG_EXT_TILE
6188 if (oxcf->large_scale_tile)
6189 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
6190#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006191
6192 cpi->refresh_last_frame = 1;
6193 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006194 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006195 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006196 cpi->refresh_alt_ref_frame = 0;
6197
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02006198#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07006199 if (oxcf->pass == 2 && cm->show_existing_frame) {
6200 // Manage the source buffer and flush out the source frame that has been
6201 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07006202 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006203 *size = 0;
6204 return -1;
6205 }
Alex Conversef77fd0b2017-04-20 11:00:24 -07006206 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006207 // TODO(zoeliu): To track down to determine whether it's needed to adjust
6208 // the frame rate.
6209 *time_stamp = source->ts_start;
6210 *time_end = source->ts_end;
6211
6212 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006213 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006214
6215 // Find a free buffer for the new frame, releasing the reference previously
6216 // held.
6217 if (cm->new_fb_idx != INVALID_IDX) {
6218 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6219 }
6220 cm->new_fb_idx = get_free_fb(cm);
6221
6222 if (cm->new_fb_idx == INVALID_IDX) return -1;
6223
6224 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006225 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006226
6227 // Start with a 0 size frame.
6228 *size = 0;
6229
6230 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006231 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006232
6233 Pass2Encode(cpi, size, dest, frame_flags);
6234
6235 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
6236
6237#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07006238 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006239#endif // CONFIG_INTERNAL_STATS
6240
6241 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006242 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006243
6244 cm->show_existing_frame = 0;
6245 return 0;
6246 }
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02006247#endif // !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07006248
6249 // Should we encode an arf frame.
6250 arf_src_index = get_arf_src_index(cpi);
6251 if (arf_src_index) {
6252 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006253 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006254 // Avoid creating an alt-ref if there's a forced keyframe pending.
6255 if (e == NULL) {
6256 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07006257 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006258 arf_src_index = 0;
6259 flush = 1;
6260 break;
6261 }
6262 }
6263 }
6264
6265 if (arf_src_index) {
6266 assert(arf_src_index <= rc->frames_to_key);
6267
Yaowu Xuf883b422016-08-30 14:01:10 -07006268 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006269 cpi->alt_ref_source = source;
6270
6271 if (oxcf->arnr_max_frames > 0) {
Todd Nguyen302d0972017-06-16 16:16:29 -07006272// Produce the filtered ARF frame.
6273#if CONFIG_BGSPRITE
6274 int bgsprite_ret = av1_background_sprite(cpi, arf_src_index);
Todd Nguyen2fc23092017-07-11 12:00:36 -07006275 // Do temporal filter if bgsprite not generated.
6276 if (bgsprite_ret != 0)
Todd Nguyen302d0972017-06-16 16:16:29 -07006277#endif // CONFIG_BGSPRITE
Todd Nguyen8493f912017-07-20 12:45:12 -07006278 av1_temporal_filter(cpi,
6279#if CONFIG_BGSPRITE
Todd Nguyen1fd99c22017-07-31 17:50:41 -07006280 NULL, &cpi->alt_ref_buffer,
Todd Nguyen8493f912017-07-20 12:45:12 -07006281#endif // CONFIG_BGSPRITE
6282 arf_src_index);
Yaowu Xuf883b422016-08-30 14:01:10 -07006283 aom_extend_frame_borders(&cpi->alt_ref_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006284 force_src_buffer = &cpi->alt_ref_buffer;
6285 }
6286
6287 cm->show_frame = 0;
6288 cm->intra_only = 0;
6289 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006290 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006291 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006292 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006293 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006294 rc->is_src_frame_alt_ref = 0;
6295 }
6296 rc->source_alt_ref_pending = 0;
6297 }
6298
Zoe Liue9b15e22017-07-19 15:53:01 -07006299 // Should we encode an arf2 frame.
6300 arf_src_index = get_arf2_src_index(cpi);
6301 if (arf_src_index) {
6302 for (i = 0; i <= arf_src_index; ++i) {
6303 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
6304 // Avoid creating an alt-ref if there's a forced keyframe pending.
6305 if (e == NULL) {
6306 break;
6307 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
6308 arf_src_index = 0;
6309 flush = 1;
6310 break;
6311 }
6312 }
6313 }
6314
6315 if (arf_src_index) {
6316 assert(arf_src_index <= rc->frames_to_key);
6317
6318 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
6319 cpi->alt_ref_source = source;
6320
6321 if (oxcf->arnr_max_frames > 0) {
6322 // Produce the filtered ARF frame.
Zoe Liu3ac20932017-08-30 16:35:55 -07006323 av1_temporal_filter(cpi,
6324#if CONFIG_BGSPRITE
6325 NULL, NULL,
6326#endif // CONFIG_BGSPRITE
6327 arf_src_index);
Zoe Liue9b15e22017-07-19 15:53:01 -07006328 aom_extend_frame_borders(&cpi->alt_ref_buffer);
6329 force_src_buffer = &cpi->alt_ref_buffer;
6330 }
6331
6332 cm->show_frame = 0;
6333 cm->intra_only = 0;
6334 cpi->refresh_alt2_ref_frame = 1;
6335 cpi->refresh_last_frame = 0;
6336 cpi->refresh_golden_frame = 0;
6337 cpi->refresh_bwd_ref_frame = 0;
6338 cpi->refresh_alt_ref_frame = 0;
6339 rc->is_src_frame_alt_ref = 0;
6340 rc->is_src_frame_ext_arf = 0;
6341 }
6342 rc->source_alt_ref_pending = 0;
6343 }
Zoe Liue9b15e22017-07-19 15:53:01 -07006344
Yaowu Xuc27fc142016-08-22 16:08:15 -07006345 rc->is_bwd_ref_frame = 0;
6346 brf_src_index = get_brf_src_index(cpi);
6347 if (brf_src_index) {
6348 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07006349 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006350 cm->show_frame = 0;
6351 cm->intra_only = 0;
6352
6353 cpi->refresh_bwd_ref_frame = 1;
6354 cpi->refresh_last_frame = 0;
6355 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006356 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006357 cpi->refresh_alt_ref_frame = 0;
6358
6359 rc->is_bwd_ref_frame = 1;
6360 }
6361 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006362
6363 if (!source) {
6364 // Get last frame source.
6365 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006366 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006367 return -1;
6368 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006369 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006370 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07006371 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006372
6373 if (source != NULL) {
6374 cm->show_frame = 1;
6375 cm->intra_only = 0;
6376
6377 // Check to see if the frame should be encoded as an arf overlay.
6378 check_src_altref(cpi, source);
6379 }
6380 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006381 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006382 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07006383 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006384 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
6385
6386 *time_stamp = source->ts_start;
6387 *time_end = source->ts_end;
Yaowu Xuf883b422016-08-30 14:01:10 -07006388 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006389
6390 } else {
6391 *size = 0;
6392 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006393#if CONFIG_XIPHRC
6394 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 1);
6395#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006396 av1_end_first_pass(cpi); /* get last stats packet */
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006397#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006398 cpi->twopass.first_pass_done = 1;
6399 }
6400 return -1;
6401 }
6402
6403 if (source->ts_start < cpi->first_time_stamp_ever) {
6404 cpi->first_time_stamp_ever = source->ts_start;
6405 cpi->last_end_time_stamp_seen = source->ts_start;
6406 }
6407
6408 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006409 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006410
6411 // adjust frame rates based on timestamps given
6412 if (cm->show_frame) adjust_frame_rate(cpi, source);
6413
6414 // Find a free buffer for the new frame, releasing the reference previously
6415 // held.
6416 if (cm->new_fb_idx != INVALID_IDX) {
6417 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6418 }
6419 cm->new_fb_idx = get_free_fb(cm);
6420
6421 if (cm->new_fb_idx == INVALID_IDX) return -1;
6422
6423 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Sebastien Alaiwan48795802017-10-30 12:07:13 +01006424#if CONFIG_HIGHBITDEPTH
Yaowu Xu9b0f7032017-07-31 11:01:19 -07006425 cm->cur_frame->buf.buf_8bit_valid = 0;
6426#endif
Zoe Liu6cfaff92016-10-18 17:12:11 -07006427
Yaowu Xuc27fc142016-08-22 16:08:15 -07006428 // Start with a 0 size frame.
6429 *size = 0;
6430
6431 cpi->frame_flags = *frame_flags;
6432
6433 if (oxcf->pass == 2) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006434#if CONFIG_XIPHRC
6435 if (od_enc_rc_2pass_in(&cpi->od_rc) < 0) return -1;
6436 }
6437#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006438 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006439 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07006440 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006441 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006442#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006443
6444 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
6445 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i)
6446 cpi->scaled_ref_idx[i] = INVALID_IDX;
6447 }
6448
6449#if CONFIG_AOM_QM
6450 cm->using_qmatrix = cpi->oxcf.using_qm;
6451 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
6452 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
6453#endif
6454
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006455#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01006456 if (cm->seq_params.frame_id_numbers_present_flag) {
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006457 if (*time_stamp == 0) {
6458 cpi->common.current_frame_id = -1;
6459 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006460 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006461#endif // CONFIG_REFERENCE_BUFFER
RogerZhou3b635242017-09-19 10:06:46 -07006462#if CONFIG_AMVR
6463 cpi->cur_poc++;
6464 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
RogerZhou10a03802017-10-26 11:49:48 -07006465 if (cpi->common.seq_force_integer_mv == 2) {
RogerZhou3b635242017-09-19 10:06:46 -07006466 struct lookahead_entry *previous_entry =
6467 cpi->lookahead->buf + cpi->previsous_index;
RogerZhou10a03802017-10-26 11:49:48 -07006468 cpi->common.cur_frame_force_integer_mv = is_integer_mv(
RogerZhou3b635242017-09-19 10:06:46 -07006469 cpi, cpi->source, &previous_entry->img, cpi->previsou_hash_table);
6470 } else {
RogerZhou10a03802017-10-26 11:49:48 -07006471 cpi->common.cur_frame_force_integer_mv = cpi->common.seq_force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07006472 }
6473 } else {
RogerZhou10a03802017-10-26 11:49:48 -07006474 cpi->common.cur_frame_force_integer_mv = 0;
RogerZhou3b635242017-09-19 10:06:46 -07006475 }
6476#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006477
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006478#if CONFIG_XIPHRC
6479 if (oxcf->pass == 1) {
6480 size_t tmp;
6481 if (cpi->od_rc.cur_frame == 0) Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6482 cpi->od_rc.firstpass_quant = cpi->od_rc.target_quantizer;
6483 Pass0Encode(cpi, &tmp, dest, 0, frame_flags);
6484 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 0);
6485 } else if (oxcf->pass == 2) {
6486 Pass0Encode(cpi, size, dest, 0, frame_flags);
6487 } else {
6488 if (cpi->od_rc.cur_frame == 0) {
6489 size_t tmp;
6490 Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6491 }
6492 Pass0Encode(cpi, size, dest, 0, frame_flags);
6493 }
6494#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006495 if (oxcf->pass == 1) {
6496 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07006497 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006498 } else if (oxcf->pass == 2) {
6499 Pass2Encode(cpi, size, dest, frame_flags);
6500 } else {
6501 // One pass encode
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006502 Pass0Encode(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006503 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006504#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07006505#if CONFIG_HASH_ME
6506 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
RogerZhou3b635242017-09-19 10:06:46 -07006507#if CONFIG_AMVR
6508 cpi->previsou_hash_table = &cm->cur_frame->hash_table;
6509 {
6510 int l;
6511 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
6512 if ((cpi->lookahead->buf + l) == source) {
6513 cpi->previsous_index = l;
6514 break;
6515 }
6516 }
6517
6518 if (l == cpi->lookahead->max_sz) {
6519 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6520 "Failed to find last frame original buffer");
6521 }
6522 }
6523#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07006524 }
6525
6526#endif
6527
Thomas Daededa4d8b92017-06-05 15:44:14 -07006528#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -07006529 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006530#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006531 if (!cm->error_resilient_mode)
6532 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006533#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07006534
6535 // No frame encoded, or frame was dropped, release scaled references.
6536 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6537 release_scaled_references(cpi);
6538 }
6539
6540 if (*size > 0) {
6541 cpi->droppable = !frame_is_reference(cpi);
6542 }
6543
Yaowu Xuf883b422016-08-30 14:01:10 -07006544 aom_usec_timer_mark(&cmptimer);
6545 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006546
6547 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6548 generate_psnr_packet(cpi);
6549
6550#if CONFIG_INTERNAL_STATS
6551 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006552 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006553 }
6554#endif // CONFIG_INTERNAL_STATS
6555
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006556#if CONFIG_XIPHRC
6557 cpi->od_rc.cur_frame++;
6558#endif
6559
Yaowu Xuf883b422016-08-30 14:01:10 -07006560 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006561
6562 return 0;
6563}
6564
Yaowu Xuf883b422016-08-30 14:01:10 -07006565int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6566 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006567 if (!cm->show_frame) {
6568 return -1;
6569 } else {
6570 int ret;
6571 if (cm->frame_to_show) {
6572 *dest = *cm->frame_to_show;
6573 dest->y_width = cm->width;
6574 dest->y_height = cm->height;
6575 dest->uv_width = cm->width >> cm->subsampling_x;
6576 dest->uv_height = cm->height >> cm->subsampling_y;
6577 ret = 0;
6578 } else {
6579 ret = -1;
6580 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006581 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006582 return ret;
6583 }
6584}
6585
Yaowu Xuf883b422016-08-30 14:01:10 -07006586int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006587 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6588
6589 *frame =
6590 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6591 return 0;
6592}
6593
Yaowu Xuf883b422016-08-30 14:01:10 -07006594int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6595 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006596 int hr = 0, hs = 0, vr = 0, vs = 0;
6597
6598 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6599
6600 Scale2Ratio(horiz_mode, &hr, &hs);
6601 Scale2Ratio(vert_mode, &vr, &vs);
6602
6603 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006604 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6605 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006606
6607 return 0;
6608}
6609
Yaowu Xuf883b422016-08-30 14:01:10 -07006610int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006611
Yaowu Xuf883b422016-08-30 14:01:10 -07006612void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006613 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07006614 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF)) {
6615 int ref = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006616
Yaowu Xuf883b422016-08-30 14:01:10 -07006617 if (flags & AOM_EFLAG_NO_REF_LAST) {
6618 ref ^= AOM_LAST_FLAG;
Yaowu Xuf883b422016-08-30 14:01:10 -07006619 ref ^= AOM_LAST2_FLAG;
6620 ref ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006621 }
6622
Yaowu Xuf883b422016-08-30 14:01:10 -07006623 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006624
Yaowu Xuf883b422016-08-30 14:01:10 -07006625 if (flags & AOM_EFLAG_NO_REF_ARF) ref ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006626
Yaowu Xuf883b422016-08-30 14:01:10 -07006627 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006628 }
6629
6630 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07006631 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
6632 AOM_EFLAG_FORCE_GF | AOM_EFLAG_FORCE_ARF)) {
6633 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006634
Yaowu Xuf883b422016-08-30 14:01:10 -07006635 if (flags & AOM_EFLAG_NO_UPD_LAST) {
6636 upd ^= AOM_LAST_FLAG;
Yaowu Xuf883b422016-08-30 14:01:10 -07006637 upd ^= AOM_LAST2_FLAG;
6638 upd ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006639 }
6640
Yaowu Xuf883b422016-08-30 14:01:10 -07006641 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006642
Yaowu Xuf883b422016-08-30 14:01:10 -07006643 if (flags & AOM_EFLAG_NO_UPD_ARF) upd ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006644
Yaowu Xuf883b422016-08-30 14:01:10 -07006645 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006646 }
6647
Yaowu Xuf883b422016-08-30 14:01:10 -07006648 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6649 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006650 }
6651}