blob: a37d7850e98f5cd15cfd39ecd45fba6a1cc65cef [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
Yushin Chod728c212017-11-03 12:30:24 -0700197 // filter level being zero.
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 Liuc01dddb2017-11-07 08:44:06 -0800377#if !CONFIG_EXT_COMP_REFS // No change to bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200378 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
379 cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
380 cpi->rc.is_bipred_frame = 1;
381 }
Zoe Liuc01dddb2017-11-07 08:44:06 -0800382#endif // !CONFIG_EXT_COMP_REFS
Thomas Daededa4d8b92017-06-05 15:44:14 -0700383#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -0700384 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
385 cm->frame_refs[0].idx < 0) {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700386 // use default frame context values
387 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
388 } else {
Thomas Daedea6a854b2017-06-22 17:49:11 -0700389 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700390 cm->pre_fc = &cm->frame_contexts[cm->frame_refs[0].idx];
391 }
392#else
Thomas Daede10e1da92017-04-26 13:22:21 -0700393 cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700394#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700395
396 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397}
398
Cheng Chen46f30c72017-09-07 11:13:33 -0700399static void enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400 int i;
401 cm->mi = cm->mip + cm->mi_stride + 1;
402 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
403 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
404 // Clear top border row
405 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
406 // Clear left border column
407 for (i = 1; i < cm->mi_rows + 1; ++i)
408 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700409 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
410 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
411
412 memset(cm->mi_grid_base, 0,
413 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
414}
415
Cheng Chen46f30c72017-09-07 11:13:33 -0700416static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700417 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700418 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700419 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700420 if (!cm->prev_mip) return 1;
421 cm->mi_alloc_size = mi_size;
422
Yaowu Xuf883b422016-08-30 14:01:10 -0700423 cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700424 if (!cm->mi_grid_base) return 1;
425 cm->prev_mi_grid_base =
Yaowu Xuf883b422016-08-30 14:01:10 -0700426 (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700427 if (!cm->prev_mi_grid_base) return 1;
428
429 return 0;
430}
431
Cheng Chen46f30c72017-09-07 11:13:33 -0700432static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700433 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700434 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700435 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700436 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700437 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700438 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700439 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700440 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700441 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700442}
443
Cheng Chen46f30c72017-09-07 11:13:33 -0700444static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700445 // Current mip will be the prev_mip for the next frame.
446 MODE_INFO **temp_base = cm->prev_mi_grid_base;
447 MODE_INFO *temp = cm->prev_mip;
448 cm->prev_mip = cm->mip;
449 cm->mip = temp;
450
451 // Update the upper left visible macroblock ptrs.
452 cm->mi = cm->mip + cm->mi_stride + 1;
453 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
454
455 cm->prev_mi_grid_base = cm->mi_grid_base;
456 cm->mi_grid_base = temp_base;
457 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
458 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
459}
460
Yaowu Xuf883b422016-08-30 14:01:10 -0700461void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700462 static volatile int init_done = 0;
463
464 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700465 av1_rtcd();
466 aom_dsp_rtcd();
467 aom_scale_rtcd();
468 av1_init_intra_predictors();
469 av1_init_me_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000470#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -0700471 av1_rc_init_minq_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000472#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700473 av1_entropy_mv_init();
474 av1_encode_token_init();
Yaowu Xuf883b422016-08-30 14:01:10 -0700475 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700476 init_done = 1;
477 }
478}
479
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700480static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
481 if (cpi->mbmi_ext_base) {
482 aom_free(cpi->mbmi_ext_base);
483 cpi->mbmi_ext_base = NULL;
484 }
485}
486
487static void alloc_context_buffers_ext(AV1_COMP *cpi) {
488 AV1_COMMON *cm = &cpi->common;
489 int mi_size = cm->mi_cols * cm->mi_rows;
490
491 dealloc_context_buffers_ext(cpi);
492 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
493 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
494}
495
Yaowu Xuf883b422016-08-30 14:01:10 -0700496static void dealloc_compressor_data(AV1_COMP *cpi) {
497 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700498
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700499 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700500
Yaowu Xuf883b422016-08-30 14:01:10 -0700501 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700502 cpi->tile_data = NULL;
503
504 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700505 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700507
Yaowu Xuf883b422016-08-30 14:01:10 -0700508 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700509 cpi->cyclic_refresh = NULL;
510
Yaowu Xuf883b422016-08-30 14:01:10 -0700511 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700512 cpi->active_map.map = NULL;
513
Jingning Hand064cf02017-06-01 10:00:39 -0700514 aom_free(cpi->td.mb.above_pred_buf);
515 cpi->td.mb.above_pred_buf = NULL;
516
517 aom_free(cpi->td.mb.left_pred_buf);
518 cpi->td.mb.left_pred_buf = NULL;
519
520 aom_free(cpi->td.mb.wsrc_buf);
521 cpi->td.mb.wsrc_buf = NULL;
522
523 aom_free(cpi->td.mb.mask_buf);
524 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700525
Jingning Han6cc1fd32017-10-13 09:05:36 -0700526#if CONFIG_MFMV
527 aom_free(cm->tpl_mvs);
528 cm->tpl_mvs = NULL;
529#endif
530
Yaowu Xuf883b422016-08-30 14:01:10 -0700531 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700532#if CONFIG_LV_MAP
533 av1_free_txb_buf(cpi);
534#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700535 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536
Yaowu Xuf883b422016-08-30 14:01:10 -0700537 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700539 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800540 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700541#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700542 aom_free_frame_buffer(&cpi->scaled_source);
543 aom_free_frame_buffer(&cpi->scaled_last_source);
544 aom_free_frame_buffer(&cpi->alt_ref_buffer);
545 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700546
Yaowu Xuf883b422016-08-30 14:01:10 -0700547 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700548 cpi->tile_tok[0][0] = 0;
549
Yaowu Xuf883b422016-08-30 14:01:10 -0700550 av1_free_pc_tree(&cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551
hui sud9a812b2017-07-06 14:34:37 -0700552 aom_free(cpi->td.mb.palette_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700553
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -0700555 aom_buf_ans_free(&cpi->buf_ans);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700556#endif // CONFIG_ANS
557}
558
Yaowu Xuf883b422016-08-30 14:01:10 -0700559static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700560 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700561 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000562 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700563
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200564 // Stores a snapshot of key state variables which can subsequently be
565 // restored with a call to av1_restore_coding_context. These functions are
566 // intended for use in a re-code loop in av1_compress_frame where the
567 // quantizer value is adjusted between loop iterations.
James Zern01a9d702017-08-25 19:09:33 +0000568 for (i = 0; i < NMV_CONTEXTS; ++i) {
569 av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
570 av1_copy(cc->nmv_costs, cpi->nmv_costs);
571 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
572 }
573
Yaowu Xuf883b422016-08-30 14:01:10 -0700574 av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
575 av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700576
577 cc->fc = *cm->fc;
578}
579
Yaowu Xuf883b422016-08-30 14:01:10 -0700580static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700582 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000583 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700584
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200585 // Restore key state variables to the snapshot state stored in the
586 // previous call to av1_save_coding_context.
James Zern01a9d702017-08-25 19:09:33 +0000587 for (i = 0; i < NMV_CONTEXTS; ++i) {
588 av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
589 av1_copy(cpi->nmv_costs, cc->nmv_costs);
590 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
591 }
592
Yaowu Xuf883b422016-08-30 14:01:10 -0700593 av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
594 av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700595
596 *cm->fc = cc->fc;
597}
598
Yaowu Xuf883b422016-08-30 14:01:10 -0700599static void configure_static_seg_features(AV1_COMP *cpi) {
600 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700601 const RATE_CONTROL *const rc = &cpi->rc;
602 struct segmentation *const seg = &cm->seg;
603
604 int high_q = (int)(rc->avg_q > 48.0);
605 int qi_delta;
606
607 // Disable and clear down for KF
608 if (cm->frame_type == KEY_FRAME) {
609 // Clear down the global segmentation map
610 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
611 seg->update_map = 0;
612 seg->update_data = 0;
613 cpi->static_mb_pct = 0;
614
615 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700616 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700617
618 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700619 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700620 } else if (cpi->refresh_alt_ref_frame) {
621 // If this is an alt ref frame
622 // Clear down the global segmentation map
623 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
624 seg->update_map = 0;
625 seg->update_data = 0;
626 cpi->static_mb_pct = 0;
627
628 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700629 av1_disable_segmentation(seg);
630 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700631
632 // Scan frames from current to arf frame.
633 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700634 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700635
636 // If segmentation was enabled set those features needed for the
637 // arf itself.
638 if (seg->enabled) {
639 seg->update_map = 1;
640 seg->update_data = 1;
641
642 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700643 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
644 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700645#if CONFIG_LOOPFILTER_LEVEL
646 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
647 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
648 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
649 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
650
651 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
652 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
653 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
654 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
655#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700656 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Cheng Chend8184da2017-09-26 18:15:22 -0700657 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
658#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700659
Yaowu Xuf883b422016-08-30 14:01:10 -0700660 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700661 }
662 } else if (seg->enabled) {
663 // All other frames if segmentation has been enabled
664
665 // First normal frame in a valid gf or alt ref group
666 if (rc->frames_since_golden == 0) {
667 // Set up segment features for normal frames in an arf group
668 if (rc->source_alt_ref_active) {
669 seg->update_map = 0;
670 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700671
Yaowu Xuf883b422016-08-30 14:01:10 -0700672 qi_delta =
673 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
674 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
675 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700676
Cheng Chend8184da2017-09-26 18:15:22 -0700677#if CONFIG_LOOPFILTER_LEVEL
678 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
679 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
680 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
681 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
682
683 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
684 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
685 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
686 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
687#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700688 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
689 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Cheng Chend8184da2017-09-26 18:15:22 -0700690#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700691
692 // Segment coding disabled for compred testing
693 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700694 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
695 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
696 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697 }
698 } else {
699 // Disable segmentation and clear down features if alt ref
700 // is not active for this group
701
Yaowu Xuf883b422016-08-30 14:01:10 -0700702 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703
704 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
705
706 seg->update_map = 0;
707 seg->update_data = 0;
708
Yaowu Xuf883b422016-08-30 14:01:10 -0700709 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710 }
711 } else if (rc->is_src_frame_alt_ref) {
712 // Special case where we are coding over the top of a previous
713 // alt ref frame.
714 // Segment coding disabled for compred testing
715
716 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700717 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
718 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719
720 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700721 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
722 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
723 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
724 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700725
726 // Skip all MBs if high Q (0,0 mv and skip coeffs)
727 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700728 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
729 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730 }
731 // Enable data update
732 seg->update_data = 1;
733 } else {
734 // All other frames.
735
736 // No updates.. leave things as they are.
737 seg->update_map = 0;
738 seg->update_data = 0;
739 }
740 }
741}
742
Yaowu Xuf883b422016-08-30 14:01:10 -0700743static void update_reference_segmentation_map(AV1_COMP *cpi) {
744 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700745 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
746 uint8_t *cache_ptr = cm->last_frame_seg_map;
747 int row, col;
748
749 for (row = 0; row < cm->mi_rows; row++) {
750 MODE_INFO **mi_8x8 = mi_8x8_ptr;
751 uint8_t *cache = cache_ptr;
752 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
753 cache[0] = mi_8x8[0]->mbmi.segment_id;
754 mi_8x8_ptr += cm->mi_stride;
755 cache_ptr += cm->mi_cols;
756 }
757}
758
Yaowu Xuf883b422016-08-30 14:01:10 -0700759static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
760 AV1_COMMON *cm = &cpi->common;
761 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700762
763 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700764 cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
765 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200766#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700767 cm->use_highbitdepth,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700768#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700769 oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700771 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700772 "Failed to allocate lag buffers");
773
774 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700775 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700776 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200777#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 cm->use_highbitdepth,
779#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700780 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
781 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700782 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783 "Failed to allocate altref buffer");
784}
785
Yaowu Xuf883b422016-08-30 14:01:10 -0700786static void alloc_util_frame_buffers(AV1_COMP *cpi) {
787 AV1_COMMON *const cm = &cpi->common;
788 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700789 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200790#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 cm->use_highbitdepth,
792#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700793 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
794 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700795 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796 "Failed to allocate last frame buffer");
797
798#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700799 if (aom_realloc_frame_buffer(
800 &cpi->trial_frame_rst,
801#if CONFIG_FRAME_SUPERRES
802 cm->superres_upscaled_width, cm->superres_upscaled_height,
803#else
804 cm->width, cm->height,
805#endif // CONFIG_FRAME_SUPERRES
806 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200807#if CONFIG_HIGHBITDEPTH
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700808 cm->use_highbitdepth,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800809#endif
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700810 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800811 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800812 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700813#endif // CONFIG_LOOP_RESTORATION
814
Yaowu Xuf883b422016-08-30 14:01:10 -0700815 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200817#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818 cm->use_highbitdepth,
819#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700820 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
821 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700822 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823 "Failed to allocate scaled source buffer");
824
Yaowu Xuf883b422016-08-30 14:01:10 -0700825 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200827#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828 cm->use_highbitdepth,
829#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700830 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
831 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700832 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833 "Failed to allocate scaled last source buffer");
834}
835
Cheng Chen46f30c72017-09-07 11:13:33 -0700836static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700837 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700838
Yaowu Xuf883b422016-08-30 14:01:10 -0700839 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700840
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700841#if CONFIG_LV_MAP
842 av1_alloc_txb_buf(cpi);
843#endif
844
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845 alloc_context_buffers_ext(cpi);
846
Yaowu Xuf883b422016-08-30 14:01:10 -0700847 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700848
849 {
Yaowu Xue39b3b82017-10-31 16:11:59 -0700850 unsigned int tokens =
851 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700853 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700854 }
855
Yaowu Xuf883b422016-08-30 14:01:10 -0700856 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857}
858
Yaowu Xuf883b422016-08-30 14:01:10 -0700859void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700860 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000861#if CONFIG_XIPHRC
862 if (!cpi->od_rc.cur_frame) return;
863 cpi->od_rc.framerate = cpi->framerate;
864 od_enc_rc_resize(&cpi->od_rc);
865#else
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700866 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000867#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868}
869
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200870#if CONFIG_MAX_TILE
871
872static void set_tile_info_max_tile(AV1_COMP *cpi) {
873 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200874 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200875
876 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200877
878 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200879 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200880 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200881 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
882 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200883 } else {
Yaowu Xu81d458b2017-11-07 15:30:13 -0800884 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
885 int sb_cols = mi_cols >> MAX_MIB_SIZE_LOG2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200886 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200887 cm->uniform_tile_spacing_flag = 0;
888 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
889 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200890 size_sb = cpi->oxcf.tile_widths[j++];
891 if (j >= cpi->oxcf.tile_width_count) j = 0;
892 start_sb += AOMMIN(size_sb, MAX_TILE_WIDTH_SB);
Dominic Symesf58f1112017-09-25 12:47:40 +0200893 }
894 cm->tile_cols = i;
895 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200896 }
897 av1_calculate_tile_cols(cm);
898
899 // configure tile rows
900 if (cm->uniform_tile_spacing_flag) {
901 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
902 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200903 } else {
Yaowu Xu81d458b2017-11-07 15:30:13 -0800904 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
905 int sb_rows = mi_rows >> MAX_MIB_SIZE_LOG2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200906 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200907 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
908 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200909 size_sb = cpi->oxcf.tile_heights[j++];
910 if (j >= cpi->oxcf.tile_height_count) j = 0;
911 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200912 }
913 cm->tile_rows = i;
914 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200915 }
916 av1_calculate_tile_rows(cm);
917}
918
919#endif
920
Yaowu Xuf883b422016-08-30 14:01:10 -0700921static void set_tile_info(AV1_COMP *cpi) {
922 AV1_COMMON *const cm = &cpi->common;
Thomas Daviesb25ba502017-07-18 10:18:24 +0100923#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -0800924 int tile_row, tile_col, num_tiles_in_tg;
925 int tg_row_start, tg_col_start;
926#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700927#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700928 if (cpi->oxcf.large_scale_tile) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929#if CONFIG_EXT_PARTITION
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700930 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
931 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
932 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
933 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
934 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
935 } else {
936 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
937 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
938 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
939 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
940 }
941#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700942 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
943 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700944 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
945 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700946#endif // CONFIG_EXT_PARTITION
947
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700948 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
949 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700950
Yaowu Xu8d0039c2017-10-13 07:51:46 -0700951 assert(cm->tile_width >> MAX_MIB_SIZE_LOG2 <= 32);
952 assert(cm->tile_height >> MAX_MIB_SIZE_LOG2 <= 32);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700953
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700954 // Get the number of tiles
955 cm->tile_cols = 1;
956 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700958 cm->tile_rows = 1;
959 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
960 } else {
961#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200963#if CONFIG_MAX_TILE
964 set_tile_info_max_tile(cpi);
965#else
966 int min_log2_tile_cols, max_log2_tile_cols;
967 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700968
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200969 cm->log2_tile_cols =
970 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
971 cm->log2_tile_rows = cpi->oxcf.tile_rows;
Rupert Swarbrick940942b2017-09-01 12:05:55 +0100972
Rupert Swarbrick5a010aa2017-09-26 16:16:48 +0100973 cm->tile_width =
974 get_tile_size(cm->mi_cols, cm->log2_tile_cols, &cm->tile_cols);
975 cm->tile_height =
976 get_tile_size(cm->mi_rows, cm->log2_tile_rows, &cm->tile_rows);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200977#endif // CONFIG_MAX_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700978#if CONFIG_EXT_TILE
979 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700980#endif // CONFIG_EXT_TILE
Ryan Lei7386eda2016-12-08 21:08:31 -0800981
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800982#if CONFIG_DEPENDENT_HORZTILES
983 cm->dependent_horz_tiles = cpi->oxcf.dependent_horz_tiles;
Fangwen Fu70bcb892017-05-06 17:05:19 -0700984#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700985 if (cm->large_scale_tile) {
986 // May not needed since cpi->oxcf.dependent_horz_tiles is already adjusted.
987 cm->dependent_horz_tiles = 0;
Fangwen Fu73126c02017-02-08 22:37:47 -0800988 } else {
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700989#endif // CONFIG_EXT_TILE
990 if (cm->log2_tile_rows == 0) cm->dependent_horz_tiles = 0;
991#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -0800992 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700993#endif // CONFIG_EXT_TILE
994
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700995#if CONFIG_EXT_TILE
996 if (!cm->large_scale_tile) {
997#endif // CONFIG_EXT_TILE
998 if (cpi->oxcf.mtu == 0) {
999 cm->num_tg = cpi->oxcf.num_tile_groups;
1000 } else {
1001 // Use a default value for the purposes of weighting costs in probability
1002 // updates
1003 cm->num_tg = DEFAULT_MAX_NUM_TG;
Fangwen Fu73126c02017-02-08 22:37:47 -08001004 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001005 num_tiles_in_tg =
1006 (cm->tile_cols * cm->tile_rows + cm->num_tg - 1) / cm->num_tg;
1007 tg_row_start = 0;
1008 tg_col_start = 0;
1009 for (tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
1010 for (tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
1011 if ((tile_row * cm->tile_cols + tile_col) % num_tiles_in_tg == 0) {
1012 tg_row_start = tile_row;
1013 tg_col_start = tile_col;
1014 }
1015 cm->tile_group_start_row[tile_row][tile_col] = tg_row_start;
1016 cm->tile_group_start_col[tile_row][tile_col] = tg_col_start;
1017 }
1018 }
1019#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -08001020 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001021#endif // CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -08001022#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08001023
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001024#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08001025 cm->loop_filter_across_tiles_enabled =
1026 cpi->oxcf.loop_filter_across_tiles_enabled;
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001027#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001028}
1029
Yaowu Xuf883b422016-08-30 14:01:10 -07001030static void update_frame_size(AV1_COMP *cpi) {
1031 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001032 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1033
Yaowu Xuf883b422016-08-30 14:01:10 -07001034 av1_set_mb_mi(cm, cm->width, cm->height);
1035 av1_init_context_buffers(cm);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001036 av1_init_macroblockd(cm, xd,
Luc Trudeauf8164152017-04-11 16:20:51 -04001037#if CONFIG_CFL
1038 &NULL_CFL,
1039#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07001040 NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001041 memset(cpi->mbmi_ext_base, 0,
1042 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001043 set_tile_info(cpi);
1044}
1045
Yaowu Xuf883b422016-08-30 14:01:10 -07001046static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001047 int fb_idx;
1048 for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
1049 cpi->lst_fb_idxes[fb_idx] = fb_idx;
1050 cpi->gld_fb_idx = LAST_REF_FRAMES;
1051 cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07001052 cpi->alt2_fb_idx = LAST_REF_FRAMES + 2;
1053 cpi->alt_fb_idx = LAST_REF_FRAMES + 3;
Zoe Liu8dd1c982017-09-11 10:14:35 -07001054 cpi->ext_fb_idx = LAST_REF_FRAMES + 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001055 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
1056 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -07001057#if CONFIG_AMVR
1058 cpi->rate_index = 0;
1059 cpi->rate_size = 0;
1060 cpi->cur_poc = -1;
1061#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001062}
1063
Yaowu Xuf883b422016-08-30 14:01:10 -07001064static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1065 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001066
1067 cpi->oxcf = *oxcf;
1068 cpi->framerate = oxcf->init_framerate;
1069
1070 cm->profile = oxcf->profile;
1071 cm->bit_depth = oxcf->bit_depth;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001072#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001073 cm->use_highbitdepth = oxcf->use_highbitdepth;
1074#endif
1075 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07001076#if CONFIG_COLORSPACE_HEADERS
1077 cm->transfer_function = oxcf->transfer_function;
1078 cm->chroma_sample_position = oxcf->chroma_sample_position;
1079#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001080 cm->color_range = oxcf->color_range;
1081
1082 cm->width = oxcf->width;
1083 cm->height = oxcf->height;
Cheng Chen46f30c72017-09-07 11:13:33 -07001084 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001085
1086 // Single thread case: use counts in common.
1087 cpi->td.counts = &cm->counts;
1088
1089 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001090 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001091
1092 cpi->static_mb_pct = 0;
1093 cpi->ref_frame_flags = 0;
1094
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001095 // Reset resize pending flags
1096 cpi->resize_pending_width = 0;
1097 cpi->resize_pending_height = 0;
1098
Yaowu Xuc27fc142016-08-22 16:08:15 -07001099 init_buffer_indices(cpi);
1100}
1101
1102static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001103 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001104 const int64_t bandwidth = oxcf->target_bandwidth;
1105 const int64_t starting = oxcf->starting_buffer_level_ms;
1106 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1107 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1108
1109 rc->starting_buffer_level = starting * bandwidth / 1000;
1110 rc->optimal_buffer_level =
1111 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1112 rc->maximum_buffer_size =
1113 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1114}
1115
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001116#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001117#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
1118 cpi->fn_ptr[BT].sdf = SDF; \
1119 cpi->fn_ptr[BT].sdaf = SDAF; \
1120 cpi->fn_ptr[BT].vf = VF; \
1121 cpi->fn_ptr[BT].svf = SVF; \
1122 cpi->fn_ptr[BT].svaf = SVAF; \
1123 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1124 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1125 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1126
1127#define MAKE_BFP_SAD_WRAPPER(fnname) \
1128 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1129 int source_stride, \
1130 const uint8_t *ref_ptr, int ref_stride) { \
1131 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1132 } \
1133 static unsigned int fnname##_bits10( \
1134 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1135 int ref_stride) { \
1136 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1137 } \
1138 static unsigned int fnname##_bits12( \
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) >> 4; \
1142 }
1143
1144#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1145 static unsigned int fnname##_bits8( \
1146 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1147 int ref_stride, const uint8_t *second_pred) { \
1148 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1149 } \
1150 static unsigned int fnname##_bits10( \
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 2; \
1155 } \
1156 static unsigned int fnname##_bits12( \
1157 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1158 int ref_stride, const uint8_t *second_pred) { \
1159 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1160 4; \
1161 }
1162
1163#define MAKE_BFP_SAD3_WRAPPER(fnname) \
1164 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1165 const uint8_t *ref_ptr, int ref_stride, \
1166 unsigned int *sad_array) { \
1167 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1168 } \
1169 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1170 const uint8_t *ref_ptr, int ref_stride, \
1171 unsigned int *sad_array) { \
1172 int i; \
1173 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1174 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1175 } \
1176 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1177 const uint8_t *ref_ptr, int ref_stride, \
1178 unsigned int *sad_array) { \
1179 int i; \
1180 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1181 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1182 }
1183
1184#define MAKE_BFP_SAD8_WRAPPER(fnname) \
1185 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1186 const uint8_t *ref_ptr, int ref_stride, \
1187 unsigned int *sad_array) { \
1188 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1189 } \
1190 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1191 const uint8_t *ref_ptr, int ref_stride, \
1192 unsigned int *sad_array) { \
1193 int i; \
1194 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1195 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1196 } \
1197 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1198 const uint8_t *ref_ptr, int ref_stride, \
1199 unsigned int *sad_array) { \
1200 int i; \
1201 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1202 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1203 }
1204#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1205 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1206 const uint8_t *const ref_ptr[], int ref_stride, \
1207 unsigned int *sad_array) { \
1208 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1209 } \
1210 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1211 const uint8_t *const ref_ptr[], int ref_stride, \
1212 unsigned int *sad_array) { \
1213 int i; \
1214 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1215 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1216 } \
1217 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1218 const uint8_t *const ref_ptr[], int ref_stride, \
1219 unsigned int *sad_array) { \
1220 int i; \
1221 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1222 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1223 }
1224
1225#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001226MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1227MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1228MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
1229MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
1230MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1231MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1232MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1233MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1234MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1235MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1236MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001237#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001238MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1239MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1240MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1241MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1242MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1243MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1244MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1245MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1246MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1247MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1248MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1249MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1250MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1251MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1252MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
1253MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
1254MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1255MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1256MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1257MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
1258MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
1259MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1260MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1261MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1262MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
1263MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
1264MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1265MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1266MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1267MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
1268MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
1269MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1270MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1271MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1272MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
1273MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
1274MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1275MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1276MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1277MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
1278MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
1279MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1280MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1281MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1282MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
1283MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1284MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1285MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1286MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
1287MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1288MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1289MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1290MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
1291MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
1292MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001293
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001294#if CONFIG_EXT_PARTITION_TYPES
1295MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1296MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1297MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1298MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1299MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1300MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1301MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1302MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1303MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1304MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1305MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1306MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001307MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1308MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1309MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1310MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1311MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1312MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001313#if CONFIG_EXT_PARTITION
1314MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x128)
1315MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x128_avg)
1316MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x128x4d)
1317MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x32)
1318MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x32_avg)
1319MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x32x4d)
1320#endif // CONFIG_EXT_PARTITION
1321#endif // CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001322
David Barker0f3c94e2017-05-16 15:21:50 +01001323#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001324 cpi->fn_ptr[BT].msdf = MCSDF; \
1325 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001326
David Barkerc155e012017-05-11 13:54:54 +01001327#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1328 static unsigned int fnname##_bits8( \
1329 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1330 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1331 int m_stride, int invert_mask) { \
1332 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1333 second_pred_ptr, m, m_stride, invert_mask); \
1334 } \
1335 static unsigned int fnname##_bits10( \
1336 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1337 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1338 int m_stride, int invert_mask) { \
1339 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1340 second_pred_ptr, m, m_stride, invert_mask) >> \
1341 2; \
1342 } \
1343 static unsigned int fnname##_bits12( \
1344 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1345 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1346 int m_stride, int invert_mask) { \
1347 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1348 second_pred_ptr, m, m_stride, invert_mask) >> \
1349 4; \
1350 }
1351
Yaowu Xuc27fc142016-08-22 16:08:15 -07001352#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001353MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1354MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1355MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001356#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001357MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1358MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1359MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1360MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1361MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1362MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1363MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1364MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1365MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1366MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1367MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1368MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1369MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001370
1371#if CONFIG_EXT_PARTITION_TYPES
1372MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1373MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1374MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1375MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001376MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1377MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001378#if CONFIG_EXT_PARTITION
1379MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x128)
1380MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x32)
1381#endif // CONFIG_EXT_PARTITION
1382#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001383
Yaowu Xuc27fc142016-08-22 16:08:15 -07001384#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1385 cpi->fn_ptr[BT].osdf = OSDF; \
1386 cpi->fn_ptr[BT].ovf = OVF; \
1387 cpi->fn_ptr[BT].osvf = OSVF;
1388
1389#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1390 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1391 const int32_t *wsrc, \
1392 const int32_t *msk) { \
1393 return fnname(ref, ref_stride, wsrc, msk); \
1394 } \
1395 static unsigned int fnname##_bits10(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) >> 2; \
1399 } \
1400 static unsigned int fnname##_bits12(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) >> 4; \
1404 }
1405
1406#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001407MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1408MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1409MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001410#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001411MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1412MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1413MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1414MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1415MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1416MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1417MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1418MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1419MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1420MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1421MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1422MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1423MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001424
1425#if CONFIG_EXT_PARTITION_TYPES
1426MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1427MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1428MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1429MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001430MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1431MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001432#if CONFIG_EXT_PARTITION
1433MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x128)
1434MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x32)
1435#endif // CONFIG_EXT_PARTITION
1436#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001437
Yaowu Xuf883b422016-08-30 14:01:10 -07001438static void highbd_set_var_fns(AV1_COMP *const cpi) {
1439 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001440 if (cm->use_highbitdepth) {
1441 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001442 case AOM_BITS_8:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001443#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001444#if CONFIG_EXT_PARTITION
1445 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits8,
1446 aom_highbd_sad128x32_avg_bits8, aom_highbd_8_variance128x32,
1447 aom_highbd_8_sub_pixel_variance128x32,
1448 aom_highbd_8_sub_pixel_avg_variance128x32, NULL, NULL,
1449 aom_highbd_sad128x32x4d_bits8)
1450
1451 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits8,
1452 aom_highbd_sad32x128_avg_bits8, aom_highbd_8_variance32x128,
1453 aom_highbd_8_sub_pixel_variance32x128,
1454 aom_highbd_8_sub_pixel_avg_variance32x128, NULL, NULL,
1455 aom_highbd_sad32x128x4d_bits8)
1456#endif // CONFIG_EXT_PARTITION
1457
Rupert Swarbrick72678572017-08-02 12:05:26 +01001458 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1459 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1460 aom_highbd_8_sub_pixel_variance64x16,
1461 aom_highbd_8_sub_pixel_avg_variance64x16, NULL, NULL,
1462 aom_highbd_sad64x16x4d_bits8)
1463
1464 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1465 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1466 aom_highbd_8_sub_pixel_variance16x64,
1467 aom_highbd_8_sub_pixel_avg_variance16x64, NULL, NULL,
1468 aom_highbd_sad16x64x4d_bits8)
1469
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001470 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits8,
1471 aom_highbd_sad32x8_avg_bits8, aom_highbd_8_variance32x8,
1472 aom_highbd_8_sub_pixel_variance32x8,
1473 aom_highbd_8_sub_pixel_avg_variance32x8, NULL, NULL,
1474 aom_highbd_sad32x8x4d_bits8)
1475
1476 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits8,
1477 aom_highbd_sad8x32_avg_bits8, aom_highbd_8_variance8x32,
1478 aom_highbd_8_sub_pixel_variance8x32,
1479 aom_highbd_8_sub_pixel_avg_variance8x32, NULL, NULL,
1480 aom_highbd_sad8x32x4d_bits8)
1481
1482 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits8,
1483 aom_highbd_sad16x4_avg_bits8, aom_highbd_8_variance16x4,
1484 aom_highbd_8_sub_pixel_variance16x4,
1485 aom_highbd_8_sub_pixel_avg_variance16x4, NULL, NULL,
1486 aom_highbd_sad16x4x4d_bits8)
1487
1488 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits8,
1489 aom_highbd_sad4x16_avg_bits8, aom_highbd_8_variance4x16,
1490 aom_highbd_8_sub_pixel_variance4x16,
1491 aom_highbd_8_sub_pixel_avg_variance4x16, NULL, NULL,
1492 aom_highbd_sad4x16x4d_bits8)
1493#endif
1494
Yaowu Xuf883b422016-08-30 14:01:10 -07001495 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1496 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1497 aom_highbd_8_sub_pixel_variance32x16,
1498 aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1499 aom_highbd_sad32x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001500
Yaowu Xuf883b422016-08-30 14:01:10 -07001501 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1502 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1503 aom_highbd_8_sub_pixel_variance16x32,
1504 aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1505 aom_highbd_sad16x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001506
Yaowu Xuf883b422016-08-30 14:01:10 -07001507 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1508 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1509 aom_highbd_8_sub_pixel_variance64x32,
1510 aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1511 aom_highbd_sad64x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001512
Yaowu Xuf883b422016-08-30 14:01:10 -07001513 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1514 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1515 aom_highbd_8_sub_pixel_variance32x64,
1516 aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1517 aom_highbd_sad32x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001518
Yaowu Xuf883b422016-08-30 14:01:10 -07001519 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1520 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1521 aom_highbd_8_sub_pixel_variance32x32,
1522 aom_highbd_8_sub_pixel_avg_variance32x32,
1523 aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
1524 aom_highbd_sad32x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001525
Yaowu Xuf883b422016-08-30 14:01:10 -07001526 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1527 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1528 aom_highbd_8_sub_pixel_variance64x64,
1529 aom_highbd_8_sub_pixel_avg_variance64x64,
1530 aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
1531 aom_highbd_sad64x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001532
Yaowu Xuf883b422016-08-30 14:01:10 -07001533 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1534 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1535 aom_highbd_8_sub_pixel_variance16x16,
1536 aom_highbd_8_sub_pixel_avg_variance16x16,
1537 aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
1538 aom_highbd_sad16x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001539
1540 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001541 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1542 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1543 aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
1544 aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001545
1546 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001547 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1548 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1549 aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
1550 aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001551
1552 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001553 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1554 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1555 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits8,
1556 aom_highbd_sad8x8x8_bits8, aom_highbd_sad8x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001557
Yaowu Xuf883b422016-08-30 14:01:10 -07001558 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1559 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1560 aom_highbd_8_sub_pixel_variance8x4,
1561 aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
1562 aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001563
Yaowu Xuf883b422016-08-30 14:01:10 -07001564 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1565 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1566 aom_highbd_8_sub_pixel_variance4x8,
1567 aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
1568 aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001569
1570 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001571 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1572 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1573 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits8,
1574 aom_highbd_sad4x4x8_bits8, aom_highbd_sad4x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001575
Jingning Hancc5bdf42016-12-19 11:14:30 -08001576 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_8_variance2x2, NULL, NULL,
1577 NULL, NULL, NULL)
1578 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_8_variance4x2, NULL, NULL,
1579 NULL, NULL, NULL)
1580 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_8_variance2x4, NULL, NULL,
1581 NULL, NULL, NULL)
Jingning Hancc5bdf42016-12-19 11:14:30 -08001582
Yaowu Xuc27fc142016-08-22 16:08:15 -07001583#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001584 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1585 aom_highbd_sad128x128_avg_bits8,
1586 aom_highbd_8_variance128x128,
1587 aom_highbd_8_sub_pixel_variance128x128,
1588 aom_highbd_8_sub_pixel_avg_variance128x128,
1589 aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
1590 aom_highbd_sad128x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001591
Yaowu Xuf883b422016-08-30 14:01:10 -07001592 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1593 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1594 aom_highbd_8_sub_pixel_variance128x64,
1595 aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
1596 aom_highbd_sad128x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001597
Yaowu Xuf883b422016-08-30 14:01:10 -07001598 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1599 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1600 aom_highbd_8_sub_pixel_variance64x128,
1601 aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
1602 aom_highbd_sad64x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001603#endif // CONFIG_EXT_PARTITION
1604
Yaowu Xuc27fc142016-08-22 16:08:15 -07001605#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001606 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1607 aom_highbd_8_masked_sub_pixel_variance128x128)
1608 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1609 aom_highbd_8_masked_sub_pixel_variance128x64)
1610 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1611 aom_highbd_8_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001612#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001613 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1614 aom_highbd_8_masked_sub_pixel_variance64x64)
1615 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1616 aom_highbd_8_masked_sub_pixel_variance64x32)
1617 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1618 aom_highbd_8_masked_sub_pixel_variance32x64)
1619 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1620 aom_highbd_8_masked_sub_pixel_variance32x32)
1621 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1622 aom_highbd_8_masked_sub_pixel_variance32x16)
1623 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1624 aom_highbd_8_masked_sub_pixel_variance16x32)
1625 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1626 aom_highbd_8_masked_sub_pixel_variance16x16)
1627 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1628 aom_highbd_8_masked_sub_pixel_variance8x16)
1629 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1630 aom_highbd_8_masked_sub_pixel_variance16x8)
1631 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1632 aom_highbd_8_masked_sub_pixel_variance8x8)
1633 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1634 aom_highbd_8_masked_sub_pixel_variance4x8)
1635 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1636 aom_highbd_8_masked_sub_pixel_variance8x4)
1637 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1638 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001639#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001640#if CONFIG_EXT_PARTITION
1641 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits8,
1642 aom_highbd_8_masked_sub_pixel_variance128x32)
1643
1644 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits8,
1645 aom_highbd_8_masked_sub_pixel_variance32x128)
1646#endif // CONFIG_EXT_PARTITION
1647
Rupert Swarbrick72678572017-08-02 12:05:26 +01001648 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1649 aom_highbd_8_masked_sub_pixel_variance64x16)
1650
1651 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1652 aom_highbd_8_masked_sub_pixel_variance16x64)
1653
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001654 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1655 aom_highbd_8_masked_sub_pixel_variance32x8)
1656
1657 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1658 aom_highbd_8_masked_sub_pixel_variance8x32)
1659
1660 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1661 aom_highbd_8_masked_sub_pixel_variance16x4)
1662
1663 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1664 aom_highbd_8_masked_sub_pixel_variance4x16)
1665#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001666#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001667 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1668 aom_highbd_obmc_variance128x128,
1669 aom_highbd_obmc_sub_pixel_variance128x128)
1670 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1671 aom_highbd_obmc_variance128x64,
1672 aom_highbd_obmc_sub_pixel_variance128x64)
1673 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1674 aom_highbd_obmc_variance64x128,
1675 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001676#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001677 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1678 aom_highbd_obmc_variance64x64,
1679 aom_highbd_obmc_sub_pixel_variance64x64)
1680 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1681 aom_highbd_obmc_variance64x32,
1682 aom_highbd_obmc_sub_pixel_variance64x32)
1683 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1684 aom_highbd_obmc_variance32x64,
1685 aom_highbd_obmc_sub_pixel_variance32x64)
1686 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1687 aom_highbd_obmc_variance32x32,
1688 aom_highbd_obmc_sub_pixel_variance32x32)
1689 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1690 aom_highbd_obmc_variance32x16,
1691 aom_highbd_obmc_sub_pixel_variance32x16)
1692 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1693 aom_highbd_obmc_variance16x32,
1694 aom_highbd_obmc_sub_pixel_variance16x32)
1695 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1696 aom_highbd_obmc_variance16x16,
1697 aom_highbd_obmc_sub_pixel_variance16x16)
1698 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1699 aom_highbd_obmc_variance8x16,
1700 aom_highbd_obmc_sub_pixel_variance8x16)
1701 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1702 aom_highbd_obmc_variance16x8,
1703 aom_highbd_obmc_sub_pixel_variance16x8)
1704 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1705 aom_highbd_obmc_variance8x8,
1706 aom_highbd_obmc_sub_pixel_variance8x8)
1707 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1708 aom_highbd_obmc_variance4x8,
1709 aom_highbd_obmc_sub_pixel_variance4x8)
1710 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1711 aom_highbd_obmc_variance8x4,
1712 aom_highbd_obmc_sub_pixel_variance8x4)
1713 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1714 aom_highbd_obmc_variance4x4,
1715 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001716#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001717#if CONFIG_EXT_PARTITION
1718 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits8,
1719 aom_highbd_obmc_variance128x32,
1720 aom_highbd_obmc_sub_pixel_variance128x32)
1721
1722 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits8,
1723 aom_highbd_obmc_variance32x128,
1724 aom_highbd_obmc_sub_pixel_variance32x128)
1725#endif // CONFIG_EXT_PARTITION
1726
Rupert Swarbrick72678572017-08-02 12:05:26 +01001727 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1728 aom_highbd_obmc_variance64x16,
1729 aom_highbd_obmc_sub_pixel_variance64x16)
1730
1731 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1732 aom_highbd_obmc_variance16x64,
1733 aom_highbd_obmc_sub_pixel_variance16x64)
1734
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001735 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1736 aom_highbd_obmc_variance32x8,
1737 aom_highbd_obmc_sub_pixel_variance32x8)
1738
1739 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1740 aom_highbd_obmc_variance8x32,
1741 aom_highbd_obmc_sub_pixel_variance8x32)
1742
1743 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1744 aom_highbd_obmc_variance16x4,
1745 aom_highbd_obmc_sub_pixel_variance16x4)
1746
1747 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1748 aom_highbd_obmc_variance4x16,
1749 aom_highbd_obmc_sub_pixel_variance4x16)
1750#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001751 break;
1752
Yaowu Xuf883b422016-08-30 14:01:10 -07001753 case AOM_BITS_10:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001754#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001755#if CONFIG_EXT_PARTITION
1756 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits10,
1757 aom_highbd_sad128x32_avg_bits10,
1758 aom_highbd_10_variance128x32,
1759 aom_highbd_10_sub_pixel_variance128x32,
1760 aom_highbd_10_sub_pixel_avg_variance128x32, NULL, NULL,
1761 aom_highbd_sad128x32x4d_bits10)
1762
1763 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits10,
1764 aom_highbd_sad32x128_avg_bits10,
1765 aom_highbd_10_variance32x128,
1766 aom_highbd_10_sub_pixel_variance32x128,
1767 aom_highbd_10_sub_pixel_avg_variance32x128, NULL, NULL,
1768 aom_highbd_sad32x128x4d_bits10)
1769#endif // CONFIG_EXT_PARTITION
1770
Rupert Swarbrick72678572017-08-02 12:05:26 +01001771 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1772 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1773 aom_highbd_10_sub_pixel_variance64x16,
1774 aom_highbd_10_sub_pixel_avg_variance64x16, NULL, NULL,
1775 aom_highbd_sad64x16x4d_bits10)
1776
1777 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1778 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1779 aom_highbd_10_sub_pixel_variance16x64,
1780 aom_highbd_10_sub_pixel_avg_variance16x64, NULL, NULL,
1781 aom_highbd_sad16x64x4d_bits10)
1782
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001783 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1784 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1785 aom_highbd_10_sub_pixel_variance32x8,
1786 aom_highbd_10_sub_pixel_avg_variance32x8, NULL, NULL,
1787 aom_highbd_sad32x8x4d_bits10)
1788
1789 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1790 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1791 aom_highbd_10_sub_pixel_variance8x32,
1792 aom_highbd_10_sub_pixel_avg_variance8x32, NULL, NULL,
1793 aom_highbd_sad8x32x4d_bits10)
1794
1795 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1796 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1797 aom_highbd_10_sub_pixel_variance16x4,
1798 aom_highbd_10_sub_pixel_avg_variance16x4, NULL, NULL,
1799 aom_highbd_sad16x4x4d_bits10)
1800
1801 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1802 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1803 aom_highbd_10_sub_pixel_variance4x16,
1804 aom_highbd_10_sub_pixel_avg_variance4x16, NULL, NULL,
1805 aom_highbd_sad4x16x4d_bits10)
1806#endif
1807
Yaowu Xuf883b422016-08-30 14:01:10 -07001808 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1809 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1810 aom_highbd_10_sub_pixel_variance32x16,
1811 aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1812 aom_highbd_sad32x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001813
Yaowu Xuf883b422016-08-30 14:01:10 -07001814 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1815 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1816 aom_highbd_10_sub_pixel_variance16x32,
1817 aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1818 aom_highbd_sad16x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001819
Yaowu Xuf883b422016-08-30 14:01:10 -07001820 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1821 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1822 aom_highbd_10_sub_pixel_variance64x32,
1823 aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1824 aom_highbd_sad64x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001825
Yaowu Xuf883b422016-08-30 14:01:10 -07001826 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1827 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1828 aom_highbd_10_sub_pixel_variance32x64,
1829 aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1830 aom_highbd_sad32x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001831
Yaowu Xuf883b422016-08-30 14:01:10 -07001832 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1833 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1834 aom_highbd_10_sub_pixel_variance32x32,
1835 aom_highbd_10_sub_pixel_avg_variance32x32,
1836 aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
1837 aom_highbd_sad32x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001838
Yaowu Xuf883b422016-08-30 14:01:10 -07001839 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1840 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1841 aom_highbd_10_sub_pixel_variance64x64,
1842 aom_highbd_10_sub_pixel_avg_variance64x64,
1843 aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
1844 aom_highbd_sad64x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001845
Yaowu Xuf883b422016-08-30 14:01:10 -07001846 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1847 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1848 aom_highbd_10_sub_pixel_variance16x16,
1849 aom_highbd_10_sub_pixel_avg_variance16x16,
1850 aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
1851 aom_highbd_sad16x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001852
Yaowu Xuf883b422016-08-30 14:01:10 -07001853 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1854 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1855 aom_highbd_10_sub_pixel_variance16x8,
1856 aom_highbd_10_sub_pixel_avg_variance16x8,
1857 aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
1858 aom_highbd_sad16x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001859
Yaowu Xuf883b422016-08-30 14:01:10 -07001860 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1861 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1862 aom_highbd_10_sub_pixel_variance8x16,
1863 aom_highbd_10_sub_pixel_avg_variance8x16,
1864 aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
1865 aom_highbd_sad8x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001866
1867 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001868 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1869 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1870 aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
1871 aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001872
Yaowu Xuf883b422016-08-30 14:01:10 -07001873 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
1874 aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
1875 aom_highbd_10_sub_pixel_variance8x4,
1876 aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
1877 aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001878
Yaowu Xuf883b422016-08-30 14:01:10 -07001879 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
1880 aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
1881 aom_highbd_10_sub_pixel_variance4x8,
1882 aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
1883 aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001884
1885 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001886 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1887 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1888 aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
1889 aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001890
Jingning Hancc5bdf42016-12-19 11:14:30 -08001891 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_10_variance2x2, NULL, NULL,
1892 NULL, NULL, NULL)
1893 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_10_variance4x2, NULL, NULL,
1894 NULL, NULL, NULL)
1895 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_10_variance2x4, NULL, NULL,
1896 NULL, NULL, NULL)
Jingning Hancc5bdf42016-12-19 11:14:30 -08001897
Yaowu Xuc27fc142016-08-22 16:08:15 -07001898#if CONFIG_EXT_PARTITION
1899 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001900 BLOCK_128X128, aom_highbd_sad128x128_bits10,
1901 aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
1902 aom_highbd_10_sub_pixel_variance128x128,
1903 aom_highbd_10_sub_pixel_avg_variance128x128,
1904 aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
1905 aom_highbd_sad128x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001906
Yaowu Xuf883b422016-08-30 14:01:10 -07001907 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1908 aom_highbd_sad128x64_avg_bits10,
1909 aom_highbd_10_variance128x64,
1910 aom_highbd_10_sub_pixel_variance128x64,
1911 aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
1912 aom_highbd_sad128x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001913
Yaowu Xuf883b422016-08-30 14:01:10 -07001914 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1915 aom_highbd_sad64x128_avg_bits10,
1916 aom_highbd_10_variance64x128,
1917 aom_highbd_10_sub_pixel_variance64x128,
1918 aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
1919 aom_highbd_sad64x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001920#endif // CONFIG_EXT_PARTITION
1921
Yaowu Xuc27fc142016-08-22 16:08:15 -07001922#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001923 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1924 aom_highbd_10_masked_sub_pixel_variance128x128)
1925 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1926 aom_highbd_10_masked_sub_pixel_variance128x64)
1927 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1928 aom_highbd_10_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001929#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001930 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1931 aom_highbd_10_masked_sub_pixel_variance64x64)
1932 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1933 aom_highbd_10_masked_sub_pixel_variance64x32)
1934 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1935 aom_highbd_10_masked_sub_pixel_variance32x64)
1936 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1937 aom_highbd_10_masked_sub_pixel_variance32x32)
1938 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1939 aom_highbd_10_masked_sub_pixel_variance32x16)
1940 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1941 aom_highbd_10_masked_sub_pixel_variance16x32)
1942 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1943 aom_highbd_10_masked_sub_pixel_variance16x16)
1944 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1945 aom_highbd_10_masked_sub_pixel_variance8x16)
1946 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1947 aom_highbd_10_masked_sub_pixel_variance16x8)
1948 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1949 aom_highbd_10_masked_sub_pixel_variance8x8)
1950 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1951 aom_highbd_10_masked_sub_pixel_variance4x8)
1952 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1953 aom_highbd_10_masked_sub_pixel_variance8x4)
1954 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1955 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001956#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001957#if CONFIG_EXT_PARTITION
1958 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits10,
1959 aom_highbd_10_masked_sub_pixel_variance128x32)
1960
1961 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits10,
1962 aom_highbd_10_masked_sub_pixel_variance32x128)
1963#endif // CONFIG_EXT_PARTITION
1964
Rupert Swarbrick72678572017-08-02 12:05:26 +01001965 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1966 aom_highbd_10_masked_sub_pixel_variance64x16)
1967
1968 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1969 aom_highbd_10_masked_sub_pixel_variance16x64)
1970
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001971 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1972 aom_highbd_10_masked_sub_pixel_variance32x8)
1973
1974 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1975 aom_highbd_10_masked_sub_pixel_variance8x32)
1976
1977 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1978 aom_highbd_10_masked_sub_pixel_variance16x4)
1979
1980 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1981 aom_highbd_10_masked_sub_pixel_variance4x16)
1982#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001983#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001984 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1985 aom_highbd_10_obmc_variance128x128,
1986 aom_highbd_10_obmc_sub_pixel_variance128x128)
1987 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1988 aom_highbd_10_obmc_variance128x64,
1989 aom_highbd_10_obmc_sub_pixel_variance128x64)
1990 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1991 aom_highbd_10_obmc_variance64x128,
1992 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001993#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001994 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1995 aom_highbd_10_obmc_variance64x64,
1996 aom_highbd_10_obmc_sub_pixel_variance64x64)
1997 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1998 aom_highbd_10_obmc_variance64x32,
1999 aom_highbd_10_obmc_sub_pixel_variance64x32)
2000 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2001 aom_highbd_10_obmc_variance32x64,
2002 aom_highbd_10_obmc_sub_pixel_variance32x64)
2003 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2004 aom_highbd_10_obmc_variance32x32,
2005 aom_highbd_10_obmc_sub_pixel_variance32x32)
2006 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2007 aom_highbd_10_obmc_variance32x16,
2008 aom_highbd_10_obmc_sub_pixel_variance32x16)
2009 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2010 aom_highbd_10_obmc_variance16x32,
2011 aom_highbd_10_obmc_sub_pixel_variance16x32)
2012 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2013 aom_highbd_10_obmc_variance16x16,
2014 aom_highbd_10_obmc_sub_pixel_variance16x16)
2015 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2016 aom_highbd_10_obmc_variance8x16,
2017 aom_highbd_10_obmc_sub_pixel_variance8x16)
2018 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2019 aom_highbd_10_obmc_variance16x8,
2020 aom_highbd_10_obmc_sub_pixel_variance16x8)
2021 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2022 aom_highbd_10_obmc_variance8x8,
2023 aom_highbd_10_obmc_sub_pixel_variance8x8)
2024 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2025 aom_highbd_10_obmc_variance4x8,
2026 aom_highbd_10_obmc_sub_pixel_variance4x8)
2027 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2028 aom_highbd_10_obmc_variance8x4,
2029 aom_highbd_10_obmc_sub_pixel_variance8x4)
2030 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2031 aom_highbd_10_obmc_variance4x4,
2032 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002033#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002034#if CONFIG_EXT_PARTITION
2035 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits10,
2036 aom_highbd_10_obmc_variance128x32,
2037 aom_highbd_10_obmc_sub_pixel_variance128x32)
2038
2039 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits10,
2040 aom_highbd_10_obmc_variance32x128,
2041 aom_highbd_10_obmc_sub_pixel_variance32x128)
2042#endif // CONFIG_EXT_PARTITION
2043
Rupert Swarbrick72678572017-08-02 12:05:26 +01002044 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2045 aom_highbd_10_obmc_variance64x16,
2046 aom_highbd_10_obmc_sub_pixel_variance64x16)
2047
2048 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2049 aom_highbd_10_obmc_variance16x64,
2050 aom_highbd_10_obmc_sub_pixel_variance16x64)
2051
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002052 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2053 aom_highbd_10_obmc_variance32x8,
2054 aom_highbd_10_obmc_sub_pixel_variance32x8)
2055
2056 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2057 aom_highbd_10_obmc_variance8x32,
2058 aom_highbd_10_obmc_sub_pixel_variance8x32)
2059
2060 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2061 aom_highbd_10_obmc_variance16x4,
2062 aom_highbd_10_obmc_sub_pixel_variance16x4)
2063
2064 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2065 aom_highbd_10_obmc_variance4x16,
2066 aom_highbd_10_obmc_sub_pixel_variance4x16)
2067#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002068 break;
2069
Yaowu Xuf883b422016-08-30 14:01:10 -07002070 case AOM_BITS_12:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002071#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002072#if CONFIG_EXT_PARTITION
2073 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits12,
2074 aom_highbd_sad128x32_avg_bits12,
2075 aom_highbd_12_variance128x32,
2076 aom_highbd_12_sub_pixel_variance128x32,
2077 aom_highbd_12_sub_pixel_avg_variance128x32, NULL, NULL,
2078 aom_highbd_sad128x32x4d_bits12)
2079
2080 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits12,
2081 aom_highbd_sad32x128_avg_bits12,
2082 aom_highbd_12_variance32x128,
2083 aom_highbd_12_sub_pixel_variance32x128,
2084 aom_highbd_12_sub_pixel_avg_variance32x128, NULL, NULL,
2085 aom_highbd_sad32x128x4d_bits12)
2086#endif // CONFIG_EXT_PARTITION
2087
Rupert Swarbrick72678572017-08-02 12:05:26 +01002088 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2089 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2090 aom_highbd_12_sub_pixel_variance64x16,
2091 aom_highbd_12_sub_pixel_avg_variance64x16, NULL, NULL,
2092 aom_highbd_sad64x16x4d_bits12)
2093
2094 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2095 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2096 aom_highbd_12_sub_pixel_variance16x64,
2097 aom_highbd_12_sub_pixel_avg_variance16x64, NULL, NULL,
2098 aom_highbd_sad16x64x4d_bits12)
2099
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002100 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2101 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2102 aom_highbd_12_sub_pixel_variance32x8,
2103 aom_highbd_12_sub_pixel_avg_variance32x8, NULL, NULL,
2104 aom_highbd_sad32x8x4d_bits12)
2105
2106 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2107 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2108 aom_highbd_12_sub_pixel_variance8x32,
2109 aom_highbd_12_sub_pixel_avg_variance8x32, NULL, NULL,
2110 aom_highbd_sad8x32x4d_bits12)
2111
2112 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2113 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2114 aom_highbd_12_sub_pixel_variance16x4,
2115 aom_highbd_12_sub_pixel_avg_variance16x4, NULL, NULL,
2116 aom_highbd_sad16x4x4d_bits12)
2117
2118 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2119 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2120 aom_highbd_12_sub_pixel_variance4x16,
2121 aom_highbd_12_sub_pixel_avg_variance4x16, NULL, NULL,
2122 aom_highbd_sad4x16x4d_bits12)
2123#endif
2124
Yaowu Xuf883b422016-08-30 14:01:10 -07002125 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2126 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2127 aom_highbd_12_sub_pixel_variance32x16,
2128 aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
2129 aom_highbd_sad32x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002130
Yaowu Xuf883b422016-08-30 14:01:10 -07002131 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2132 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2133 aom_highbd_12_sub_pixel_variance16x32,
2134 aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
2135 aom_highbd_sad16x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002136
Yaowu Xuf883b422016-08-30 14:01:10 -07002137 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2138 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2139 aom_highbd_12_sub_pixel_variance64x32,
2140 aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
2141 aom_highbd_sad64x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002142
Yaowu Xuf883b422016-08-30 14:01:10 -07002143 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2144 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2145 aom_highbd_12_sub_pixel_variance32x64,
2146 aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
2147 aom_highbd_sad32x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002148
Yaowu Xuf883b422016-08-30 14:01:10 -07002149 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2150 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2151 aom_highbd_12_sub_pixel_variance32x32,
2152 aom_highbd_12_sub_pixel_avg_variance32x32,
2153 aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
2154 aom_highbd_sad32x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002155
Yaowu Xuf883b422016-08-30 14:01:10 -07002156 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2157 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2158 aom_highbd_12_sub_pixel_variance64x64,
2159 aom_highbd_12_sub_pixel_avg_variance64x64,
2160 aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
2161 aom_highbd_sad64x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002162
Yaowu Xuf883b422016-08-30 14:01:10 -07002163 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2164 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2165 aom_highbd_12_sub_pixel_variance16x16,
2166 aom_highbd_12_sub_pixel_avg_variance16x16,
2167 aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
2168 aom_highbd_sad16x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002169
Yaowu Xuf883b422016-08-30 14:01:10 -07002170 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2171 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2172 aom_highbd_12_sub_pixel_variance16x8,
2173 aom_highbd_12_sub_pixel_avg_variance16x8,
2174 aom_highbd_sad16x8x3_bits12, aom_highbd_sad16x8x8_bits12,
2175 aom_highbd_sad16x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002176
Yaowu Xuf883b422016-08-30 14:01:10 -07002177 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2178 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2179 aom_highbd_12_sub_pixel_variance8x16,
2180 aom_highbd_12_sub_pixel_avg_variance8x16,
2181 aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
2182 aom_highbd_sad8x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002183
2184 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002185 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2186 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
2187 aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
2188 aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002189
Yaowu Xuf883b422016-08-30 14:01:10 -07002190 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
2191 aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
2192 aom_highbd_12_sub_pixel_variance8x4,
2193 aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
2194 aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002195
Yaowu Xuf883b422016-08-30 14:01:10 -07002196 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
2197 aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
2198 aom_highbd_12_sub_pixel_variance4x8,
2199 aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
2200 aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002201
2202 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002203 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2204 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
2205 aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
2206 aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002207
Jingning Hancc5bdf42016-12-19 11:14:30 -08002208 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_12_variance2x2, NULL, NULL,
2209 NULL, NULL, NULL)
2210 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_12_variance4x2, NULL, NULL,
2211 NULL, NULL, NULL)
2212 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_12_variance2x4, NULL, NULL,
2213 NULL, NULL, NULL)
Jingning Hancc5bdf42016-12-19 11:14:30 -08002214
Yaowu Xuc27fc142016-08-22 16:08:15 -07002215#if CONFIG_EXT_PARTITION
2216 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002217 BLOCK_128X128, aom_highbd_sad128x128_bits12,
2218 aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
2219 aom_highbd_12_sub_pixel_variance128x128,
2220 aom_highbd_12_sub_pixel_avg_variance128x128,
2221 aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
2222 aom_highbd_sad128x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002223
Yaowu Xuf883b422016-08-30 14:01:10 -07002224 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2225 aom_highbd_sad128x64_avg_bits12,
2226 aom_highbd_12_variance128x64,
2227 aom_highbd_12_sub_pixel_variance128x64,
2228 aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
2229 aom_highbd_sad128x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002230
Yaowu Xuf883b422016-08-30 14:01:10 -07002231 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2232 aom_highbd_sad64x128_avg_bits12,
2233 aom_highbd_12_variance64x128,
2234 aom_highbd_12_sub_pixel_variance64x128,
2235 aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
2236 aom_highbd_sad64x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002237#endif // CONFIG_EXT_PARTITION
2238
Yaowu Xuc27fc142016-08-22 16:08:15 -07002239#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002240 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2241 aom_highbd_12_masked_sub_pixel_variance128x128)
2242 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2243 aom_highbd_12_masked_sub_pixel_variance128x64)
2244 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2245 aom_highbd_12_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002246#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002247 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2248 aom_highbd_12_masked_sub_pixel_variance64x64)
2249 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2250 aom_highbd_12_masked_sub_pixel_variance64x32)
2251 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2252 aom_highbd_12_masked_sub_pixel_variance32x64)
2253 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2254 aom_highbd_12_masked_sub_pixel_variance32x32)
2255 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2256 aom_highbd_12_masked_sub_pixel_variance32x16)
2257 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2258 aom_highbd_12_masked_sub_pixel_variance16x32)
2259 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2260 aom_highbd_12_masked_sub_pixel_variance16x16)
2261 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2262 aom_highbd_12_masked_sub_pixel_variance8x16)
2263 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2264 aom_highbd_12_masked_sub_pixel_variance16x8)
2265 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2266 aom_highbd_12_masked_sub_pixel_variance8x8)
2267 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2268 aom_highbd_12_masked_sub_pixel_variance4x8)
2269 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2270 aom_highbd_12_masked_sub_pixel_variance8x4)
2271 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2272 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002273#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002274#if CONFIG_EXT_PARTITION
2275 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits12,
2276 aom_highbd_12_masked_sub_pixel_variance128x32)
2277
2278 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits12,
2279 aom_highbd_12_masked_sub_pixel_variance32x128)
2280#endif // CONFIG_EXT_PARTITION
2281
Rupert Swarbrick72678572017-08-02 12:05:26 +01002282 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2283 aom_highbd_12_masked_sub_pixel_variance64x16)
2284
2285 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2286 aom_highbd_12_masked_sub_pixel_variance16x64)
2287
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002288 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2289 aom_highbd_12_masked_sub_pixel_variance32x8)
2290
2291 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2292 aom_highbd_12_masked_sub_pixel_variance8x32)
2293
2294 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2295 aom_highbd_12_masked_sub_pixel_variance16x4)
2296
2297 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2298 aom_highbd_12_masked_sub_pixel_variance4x16)
2299#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002300
Yaowu Xuc27fc142016-08-22 16:08:15 -07002301#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002302 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2303 aom_highbd_12_obmc_variance128x128,
2304 aom_highbd_12_obmc_sub_pixel_variance128x128)
2305 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2306 aom_highbd_12_obmc_variance128x64,
2307 aom_highbd_12_obmc_sub_pixel_variance128x64)
2308 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2309 aom_highbd_12_obmc_variance64x128,
2310 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002311#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002312 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2313 aom_highbd_12_obmc_variance64x64,
2314 aom_highbd_12_obmc_sub_pixel_variance64x64)
2315 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2316 aom_highbd_12_obmc_variance64x32,
2317 aom_highbd_12_obmc_sub_pixel_variance64x32)
2318 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2319 aom_highbd_12_obmc_variance32x64,
2320 aom_highbd_12_obmc_sub_pixel_variance32x64)
2321 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2322 aom_highbd_12_obmc_variance32x32,
2323 aom_highbd_12_obmc_sub_pixel_variance32x32)
2324 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2325 aom_highbd_12_obmc_variance32x16,
2326 aom_highbd_12_obmc_sub_pixel_variance32x16)
2327 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2328 aom_highbd_12_obmc_variance16x32,
2329 aom_highbd_12_obmc_sub_pixel_variance16x32)
2330 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2331 aom_highbd_12_obmc_variance16x16,
2332 aom_highbd_12_obmc_sub_pixel_variance16x16)
2333 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2334 aom_highbd_12_obmc_variance8x16,
2335 aom_highbd_12_obmc_sub_pixel_variance8x16)
2336 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2337 aom_highbd_12_obmc_variance16x8,
2338 aom_highbd_12_obmc_sub_pixel_variance16x8)
2339 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2340 aom_highbd_12_obmc_variance8x8,
2341 aom_highbd_12_obmc_sub_pixel_variance8x8)
2342 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2343 aom_highbd_12_obmc_variance4x8,
2344 aom_highbd_12_obmc_sub_pixel_variance4x8)
2345 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2346 aom_highbd_12_obmc_variance8x4,
2347 aom_highbd_12_obmc_sub_pixel_variance8x4)
2348 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2349 aom_highbd_12_obmc_variance4x4,
2350 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002351#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002352#if CONFIG_EXT_PARTITION
2353 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits12,
2354 aom_highbd_12_obmc_variance128x32,
2355 aom_highbd_12_obmc_sub_pixel_variance128x32)
2356
2357 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits12,
2358 aom_highbd_12_obmc_variance32x128,
2359 aom_highbd_12_obmc_sub_pixel_variance32x128)
2360#endif // CONFIG_EXT_PARTITION
2361
Rupert Swarbrick72678572017-08-02 12:05:26 +01002362 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2363 aom_highbd_12_obmc_variance64x16,
2364 aom_highbd_12_obmc_sub_pixel_variance64x16)
2365
2366 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2367 aom_highbd_12_obmc_variance16x64,
2368 aom_highbd_12_obmc_sub_pixel_variance16x64)
2369
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002370 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2371 aom_highbd_12_obmc_variance32x8,
2372 aom_highbd_12_obmc_sub_pixel_variance32x8)
2373
2374 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2375 aom_highbd_12_obmc_variance8x32,
2376 aom_highbd_12_obmc_sub_pixel_variance8x32)
2377
2378 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2379 aom_highbd_12_obmc_variance16x4,
2380 aom_highbd_12_obmc_sub_pixel_variance16x4)
2381
2382 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2383 aom_highbd_12_obmc_variance4x16,
2384 aom_highbd_12_obmc_sub_pixel_variance4x16)
2385#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386 break;
2387
2388 default:
2389 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07002390 "cm->bit_depth should be AOM_BITS_8, "
2391 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392 }
2393 }
2394}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002395#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002396
Yaowu Xuf883b422016-08-30 14:01:10 -07002397static void realloc_segmentation_maps(AV1_COMP *cpi) {
2398 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002399
2400 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002401 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002402 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002403 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002404
2405 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002406 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002407 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002408 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002409
2410 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002411 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002413 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002414}
2415
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002416void set_compound_tools(AV1_COMMON *cm) {
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002417 cm->allow_interintra_compound = 1;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002418 cm->allow_masked_compound = 1;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002419}
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002420
Yaowu Xuf883b422016-08-30 14:01:10 -07002421void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2422 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002423 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002424 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002425
2426 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
2427 cm->bit_depth = oxcf->bit_depth;
2428 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07002429#if CONFIG_COLORSPACE_HEADERS
2430 cm->transfer_function = oxcf->transfer_function;
2431 cm->chroma_sample_position = oxcf->chroma_sample_position;
2432#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002433 cm->color_range = oxcf->color_range;
2434
2435 if (cm->profile <= PROFILE_1)
Yaowu Xuf883b422016-08-30 14:01:10 -07002436 assert(cm->bit_depth == AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002437 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002438 assert(cm->bit_depth > AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002439
2440 cpi->oxcf = *oxcf;
hui sud9a812b2017-07-06 14:34:37 -07002441 x->e_mbd.bd = (int)cm->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002442 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443
Yaowu Xuf883b422016-08-30 14:01:10 -07002444 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002445 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2446 } else {
2447 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2448 }
2449
2450 cpi->refresh_last_frame = 1;
2451 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002453 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002454
2455 cm->refresh_frame_context =
2456 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
2457 ? REFRESH_FRAME_CONTEXT_FORWARD
2458 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002459#if CONFIG_EXT_TILE
2460 if (oxcf->large_scale_tile)
2461 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
2462#endif // CONFIG_EXT_TILE
2463
Thomas Daedea6a854b2017-06-22 17:49:11 -07002464#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07002466#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002467
Alex Converse74ad0912017-07-18 10:22:58 -07002468 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002469 CHECK_MEM_ERROR(cm, x->palette_buffer,
2470 aom_memalign(16, sizeof(*x->palette_buffer)));
2471 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002472 set_compound_tools(cm);
Yaowu Xuf883b422016-08-30 14:01:10 -07002473 av1_reset_segment_features(cm);
RogerZhou3b635242017-09-19 10:06:46 -07002474#if CONFIG_AMVR
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002475 set_high_precision_mv(cpi, 1, 0);
RogerZhou3b635242017-09-19 10:06:46 -07002476#else
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002477 set_high_precision_mv(cpi, 1);
RogerZhou3b635242017-09-19 10:06:46 -07002478#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002479
Yaowu Xuc27fc142016-08-22 16:08:15 -07002480 set_rc_buffer_sizes(rc, &cpi->oxcf);
2481
2482 // Under a configuration change, where maximum_buffer_size may change,
2483 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002484 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2485 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002486
2487 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002488 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002489
2490 // Set absolute upper and lower quality limits
2491 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2492 rc->best_quality = cpi->oxcf.best_allowed_q;
2493
2494 cm->interp_filter = cpi->sf.default_interp_filter;
2495
2496 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2497 cm->render_width = cpi->oxcf.render_width;
2498 cm->render_height = cpi->oxcf.render_height;
2499 } else {
2500 cm->render_width = cpi->oxcf.width;
2501 cm->render_height = cpi->oxcf.height;
2502 }
2503 cm->width = cpi->oxcf.width;
2504 cm->height = cpi->oxcf.height;
2505
2506 if (cpi->initial_width) {
2507 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002508 av1_free_context_buffers(cm);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07002509 av1_free_pc_tree(&cpi->td);
Cheng Chen46f30c72017-09-07 11:13:33 -07002510 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002511 realloc_segmentation_maps(cpi);
2512 cpi->initial_width = cpi->initial_height = 0;
2513 }
2514 }
2515 update_frame_size(cpi);
2516
2517 cpi->alt_ref_source = NULL;
2518 rc->is_src_frame_alt_ref = 0;
2519
Yaowu Xuc27fc142016-08-22 16:08:15 -07002520 rc->is_bwd_ref_frame = 0;
2521 rc->is_last_bipred_frame = 0;
2522 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002523
2524#if 0
2525 // Experimental RD Code
2526 cpi->frame_distortion = 0;
2527 cpi->last_frame_distortion = 0;
2528#endif
2529
2530 set_tile_info(cpi);
2531
2532 cpi->ext_refresh_frame_flags_pending = 0;
2533 cpi->ext_refresh_frame_context_pending = 0;
2534
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002535#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002536 highbd_set_var_fns(cpi);
2537#endif
Alex Converseeb780e72016-12-13 12:46:41 -08002538#if CONFIG_ANS && ANS_MAX_SYMBOLS
2539 cpi->common.ans_window_size_log2 = cpi->oxcf.ans_window_size_log2;
Alex Converseeb780e72016-12-13 12:46:41 -08002540#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
RogerZhou3b635242017-09-19 10:06:46 -07002541#if CONFIG_AMVR
RogerZhou10a03802017-10-26 11:49:48 -07002542 cm->seq_force_integer_mv = 2;
RogerZhou3b635242017-09-19 10:06:46 -07002543#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002544}
2545
Yaowu Xuf883b422016-08-30 14:01:10 -07002546AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2547 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002548 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002549 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2550 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002551
2552 if (!cm) return NULL;
2553
Yaowu Xuf883b422016-08-30 14:01:10 -07002554 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002555
2556 if (setjmp(cm->error.jmp)) {
2557 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002558 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002559 return 0;
2560 }
2561
2562 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002563 cm->alloc_mi = enc_alloc_mi;
2564 cm->free_mi = enc_free_mi;
2565 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002566
Angie Chianga5d96c42016-10-21 16:16:56 -07002567 CHECK_MEM_ERROR(cm, cm->fc,
2568 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2569 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2570 (FRAME_CONTEXT *)aom_memalign(
2571 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2572 memset(cm->fc, 0, sizeof(*cm->fc));
2573 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002574
2575 cpi->resize_state = 0;
2576 cpi->resize_avg_qp = 0;
2577 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002578
Yaowu Xuc27fc142016-08-22 16:08:15 -07002579 cpi->common.buffer_pool = pool;
2580
2581 init_config(cpi, oxcf);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002582#if CONFIG_XIPHRC
2583 cpi->od_rc.framerate = cpi->framerate;
2584 cpi->od_rc.frame_width = cm->render_width;
2585 cpi->od_rc.frame_height = cm->render_height;
2586 cpi->od_rc.keyframe_rate = oxcf->key_freq;
2587 cpi->od_rc.goldenframe_rate = FIXED_GF_INTERVAL;
2588 cpi->od_rc.altref_rate = 25;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002589 cpi->od_rc.firstpass_quant = 1;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002590 cpi->od_rc.bit_depth = cm->bit_depth;
2591 cpi->od_rc.minq = oxcf->best_allowed_q;
2592 cpi->od_rc.maxq = oxcf->worst_allowed_q;
2593 if (cpi->oxcf.rc_mode == AOM_CQ) cpi->od_rc.minq = cpi->od_rc.quality;
2594 cpi->od_rc.quality = cpi->oxcf.rc_mode == AOM_Q ? oxcf->cq_level : -1;
2595 cpi->od_rc.periodic_boosts = oxcf->frame_periodic_boost;
2596 od_enc_rc_init(&cpi->od_rc,
2597 cpi->oxcf.rc_mode == AOM_Q ? -1 : oxcf->target_bandwidth,
2598 oxcf->maximum_buffer_size_ms);
2599#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002600 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002601#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002602
2603 cm->current_video_frame = 0;
2604 cpi->partition_search_skippable_frame = 0;
2605 cpi->tile_data = NULL;
2606 cpi->last_show_frame_buf_idx = INVALID_IDX;
2607
2608 realloc_segmentation_maps(cpi);
2609
James Zern01a9d702017-08-25 19:09:33 +00002610 for (i = 0; i < NMV_CONTEXTS; ++i) {
2611 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2612 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
2613 }
2614
Yaowu Xuc27fc142016-08-22 16:08:15 -07002615 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2616 i++) {
2617 CHECK_MEM_ERROR(
2618 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002619 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002620 }
2621
2622#if CONFIG_FP_MB_STATS
2623 cpi->use_fp_mb_stats = 0;
2624 if (cpi->use_fp_mb_stats) {
2625 // a place holder used to store the first pass mb stats in the first pass
2626 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002627 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002628 } else {
2629 cpi->twopass.frame_mb_stats_buf = NULL;
2630 }
2631#endif
2632
2633 cpi->refresh_alt_ref_frame = 0;
2634 cpi->multi_arf_last_grp_enabled = 0;
2635
2636 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2637#if CONFIG_INTERNAL_STATS
2638 cpi->b_calculate_blockiness = 1;
2639 cpi->b_calculate_consistency = 1;
2640 cpi->total_inconsistency = 0;
2641 cpi->psnr.worst = 100.0;
2642 cpi->worst_ssim = 100.0;
2643
2644 cpi->count = 0;
2645 cpi->bytes = 0;
2646
2647 if (cpi->b_calculate_psnr) {
2648 cpi->total_sq_error = 0;
2649 cpi->total_samples = 0;
2650 cpi->tot_recode_hits = 0;
2651 cpi->summed_quality = 0;
2652 cpi->summed_weights = 0;
2653 }
2654
2655 cpi->fastssim.worst = 100.0;
2656 cpi->psnrhvs.worst = 100.0;
2657
2658 if (cpi->b_calculate_blockiness) {
2659 cpi->total_blockiness = 0;
2660 cpi->worst_blockiness = 0.0;
2661 }
2662
2663 if (cpi->b_calculate_consistency) {
2664 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002665 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666 cpi->common.mi_rows * cpi->common.mi_cols));
2667 cpi->worst_consistency = 100.0;
2668 }
2669#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002670#if CONFIG_ENTROPY_STATS
2671 av1_zero(aggregate_fc);
Zoe Liua56f9162017-06-21 22:49:57 -07002672 av1_zero_array(aggregate_fc_per_type, FRAME_CONTEXTS);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002673#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002674
2675 cpi->first_time_stamp_ever = INT64_MAX;
2676
James Zern01a9d702017-08-25 19:09:33 +00002677 for (i = 0; i < NMV_CONTEXTS; ++i) {
2678 cpi->td.mb.nmvcost[i][0] = &cpi->nmv_costs[i][0][MV_MAX];
2679 cpi->td.mb.nmvcost[i][1] = &cpi->nmv_costs[i][1][MV_MAX];
2680 cpi->td.mb.nmvcost_hp[i][0] = &cpi->nmv_costs_hp[i][0][MV_MAX];
2681 cpi->td.mb.nmvcost_hp[i][1] = &cpi->nmv_costs_hp[i][1][MV_MAX];
2682 }
2683
Yaowu Xuc27fc142016-08-22 16:08:15 -07002684#ifdef OUTPUT_YUV_SKINMAP
2685 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2686#endif
2687#ifdef OUTPUT_YUV_REC
2688 yuv_rec_file = fopen("rec.yuv", "wb");
2689#endif
2690
2691#if 0
2692 framepsnr = fopen("framepsnr.stt", "a");
2693 kf_list = fopen("kf_list.stt", "w");
2694#endif
2695
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002696#if CONFIG_XIPHRC
2697 if (oxcf->pass == 2) {
2698 cpi->od_rc.twopass_allframes_buf = oxcf->two_pass_stats_in.buf;
2699 cpi->od_rc.twopass_allframes_buf_size = oxcf->two_pass_stats_in.sz;
2700 }
2701#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002702 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002703 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002704 } else if (oxcf->pass == 2) {
2705 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2706 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2707
2708#if CONFIG_FP_MB_STATS
2709 if (cpi->use_fp_mb_stats) {
2710 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2711 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2712
2713 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2714 oxcf->firstpass_mb_stats_in.buf;
2715 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2716 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2717 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2718 }
2719#endif
2720
2721 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2722 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2723 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2724
Yaowu Xuf883b422016-08-30 14:01:10 -07002725 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002726 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002727#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002728
Jingning Hand064cf02017-06-01 10:00:39 -07002729#if CONFIG_HIGHBITDEPTH
2730 int buf_scaler = 2;
2731#else
2732 int buf_scaler = 1;
2733#endif
2734 CHECK_MEM_ERROR(
2735 cm, cpi->td.mb.above_pred_buf,
clang-format4eafefe2017-09-04 12:51:20 -07002736 (uint8_t *)aom_memalign(16,
2737 buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2738 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002739 CHECK_MEM_ERROR(
2740 cm, cpi->td.mb.left_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.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002744
2745 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2746 (int32_t *)aom_memalign(
2747 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2748
2749 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2750 (int32_t *)aom_memalign(
2751 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2752
Yaowu Xuf883b422016-08-30 14:01:10 -07002753 av1_set_speed_features_framesize_independent(cpi);
2754 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002755
Cheng Chenf78632e2017-10-20 15:30:51 -07002756#if CONFIG_JNT_COMP
2757#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF, JSDAF, JSVAF) \
2758 cpi->fn_ptr[BT].sdf = SDF; \
2759 cpi->fn_ptr[BT].sdaf = SDAF; \
2760 cpi->fn_ptr[BT].vf = VF; \
2761 cpi->fn_ptr[BT].svf = SVF; \
2762 cpi->fn_ptr[BT].svaf = SVAF; \
2763 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2764 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2765 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2766 cpi->fn_ptr[BT].jsdaf = JSDAF; \
2767 cpi->fn_ptr[BT].jsvaf = JSVAF;
2768#else // CONFIG_JNT_COMP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002769#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
2770 cpi->fn_ptr[BT].sdf = SDF; \
2771 cpi->fn_ptr[BT].sdaf = SDAF; \
2772 cpi->fn_ptr[BT].vf = VF; \
2773 cpi->fn_ptr[BT].svf = SVF; \
2774 cpi->fn_ptr[BT].svaf = SVAF; \
2775 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2776 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2777 cpi->fn_ptr[BT].sdx4df = SDX4DF;
Cheng Chenf78632e2017-10-20 15:30:51 -07002778#endif // CONFIG_JNT_COMP
Yaowu Xuc27fc142016-08-22 16:08:15 -07002779
Cheng Chenf78632e2017-10-20 15:30:51 -07002780#if CONFIG_JNT_COMP
2781#if CONFIG_EXT_PARTITION_TYPES
2782 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2783 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, NULL, NULL,
2784 aom_sad4x16x4d, aom_jnt_sad4x16_avg_c,
2785 aom_jnt_sub_pixel_avg_variance4x16_c)
2786
2787 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2788 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, NULL, NULL,
2789 aom_sad16x4x4d, aom_jnt_sad16x4_avg_c,
2790 aom_jnt_sub_pixel_avg_variance16x4_c)
2791
2792 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2793 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, NULL, NULL,
2794 aom_sad8x32x4d, aom_jnt_sad8x32_avg_c,
2795 aom_jnt_sub_pixel_avg_variance8x32_c)
2796
2797 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2798 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, NULL, NULL,
2799 aom_sad32x8x4d, aom_jnt_sad32x8_avg_c,
2800 aom_jnt_sub_pixel_avg_variance32x8_c)
2801
2802 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2803 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, NULL, NULL,
2804 aom_sad16x64x4d, aom_jnt_sad16x64_avg_c,
2805 aom_jnt_sub_pixel_avg_variance16x64_c)
2806
2807 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2808 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, NULL, NULL,
2809 aom_sad64x16x4d, aom_jnt_sad64x16_avg_c,
2810 aom_jnt_sub_pixel_avg_variance64x16_c)
2811
2812#if CONFIG_EXT_PARTITION
2813 BFP(BLOCK_32X128, aom_sad32x128, aom_sad32x128_avg, aom_variance32x128,
2814 aom_sub_pixel_variance32x128, aom_sub_pixel_avg_variance32x128, NULL,
2815 NULL, aom_sad32x128x4d, aom_jnt_sad32x128_avg_c,
2816 aom_jnt_sub_pixel_avg_variance32x128_c)
2817
2818 BFP(BLOCK_128X32, aom_sad128x32, aom_sad128x32_avg, aom_variance128x32,
2819 aom_sub_pixel_variance128x32, aom_sub_pixel_avg_variance128x32, NULL,
2820 NULL, aom_sad128x32x4d, aom_jnt_sad128x32_avg_c,
2821 aom_jnt_sub_pixel_avg_variance128x32_c)
2822#endif // CONFIG_EXT_PARTITION
2823#endif // CONFIG_EXT_PARTITION_TYPES
2824
2825#if CONFIG_EXT_PARTITION
2826 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2827 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2828 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d,
2829 aom_jnt_sad128x128_avg_c, aom_jnt_sub_pixel_avg_variance128x128_c)
2830
2831 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2832 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
2833 NULL, aom_sad128x64x4d, aom_jnt_sad128x64_avg_c,
2834 aom_jnt_sub_pixel_avg_variance128x64_c)
2835
2836 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2837 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
2838 NULL, aom_sad64x128x4d, aom_jnt_sad64x128_avg_c,
2839 aom_jnt_sub_pixel_avg_variance64x128_c)
2840#endif // CONFIG_EXT_PARTITION
2841
2842 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2843 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2844 aom_sad32x16x4d, aom_jnt_sad32x16_avg_c,
2845 aom_jnt_sub_pixel_avg_variance32x16_c)
2846
2847 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2848 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2849 aom_sad16x32x4d, aom_jnt_sad16x32_avg_c,
2850 aom_jnt_sub_pixel_avg_variance16x32_c)
2851
2852 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2853 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2854 aom_sad64x32x4d, aom_jnt_sad64x32_avg_c,
2855 aom_jnt_sub_pixel_avg_variance64x32_c)
2856
2857 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2858 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2859 aom_sad32x64x4d, aom_jnt_sad32x64_avg_c,
2860 aom_jnt_sub_pixel_avg_variance32x64_c)
2861
2862 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2863 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2864 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d, aom_jnt_sad32x32_avg_c,
2865 aom_jnt_sub_pixel_avg_variance32x32_c)
2866
2867 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2868 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2869 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d, aom_jnt_sad64x64_avg_c,
2870 aom_jnt_sub_pixel_avg_variance64x64_c)
2871
2872 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2873 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2874 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d, aom_jnt_sad16x16_avg_c,
2875 aom_jnt_sub_pixel_avg_variance16x16_c)
2876
2877 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2878 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
2879 aom_sad16x8x8, aom_sad16x8x4d, aom_jnt_sad16x8_avg_c,
2880 aom_jnt_sub_pixel_avg_variance16x8_c)
2881
2882 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2883 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
2884 aom_sad8x16x8, aom_sad8x16x4d, aom_jnt_sad8x16_avg_c,
2885 aom_jnt_sub_pixel_avg_variance8x16_c)
2886
2887 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2888 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
2889 aom_sad8x8x8, aom_sad8x8x4d, aom_jnt_sad8x8_avg_c,
2890 aom_jnt_sub_pixel_avg_variance8x8_c)
2891
2892 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2893 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
2894 aom_sad8x4x8, aom_sad8x4x4d, aom_jnt_sad8x4_avg_c,
2895 aom_jnt_sub_pixel_avg_variance8x4_c)
2896
2897 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2898 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
2899 aom_sad4x8x8, aom_sad4x8x4d, aom_jnt_sad4x8_avg_c,
2900 aom_jnt_sub_pixel_avg_variance4x8_c)
2901
2902 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2903 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
2904 aom_sad4x4x8, aom_sad4x4x4d, aom_jnt_sad4x4_avg_c,
2905 aom_jnt_sub_pixel_avg_variance4x4_c)
2906
2907 BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL,
2908 NULL, NULL)
2909 BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL,
2910 NULL, NULL)
2911 BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL,
2912 NULL, NULL)
2913#else // CONFIG_JNT_COMP
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002914#if CONFIG_EXT_PARTITION_TYPES
2915 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2916 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, NULL, NULL,
2917 aom_sad4x16x4d)
2918
2919 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2920 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, NULL, NULL,
2921 aom_sad16x4x4d)
2922
2923 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2924 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, NULL, NULL,
2925 aom_sad8x32x4d)
2926
2927 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2928 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, NULL, NULL,
2929 aom_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002930
2931 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2932 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, NULL, NULL,
2933 aom_sad16x64x4d)
2934
2935 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2936 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, NULL, NULL,
2937 aom_sad64x16x4d)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002938
2939#if CONFIG_EXT_PARTITION
2940 BFP(BLOCK_32X128, aom_sad32x128, aom_sad32x128_avg, aom_variance32x128,
2941 aom_sub_pixel_variance32x128, aom_sub_pixel_avg_variance32x128, NULL,
2942 NULL, aom_sad32x128x4d)
2943
2944 BFP(BLOCK_128X32, aom_sad128x32, aom_sad128x32_avg, aom_variance128x32,
2945 aom_sub_pixel_variance128x32, aom_sub_pixel_avg_variance128x32, NULL,
2946 NULL, aom_sad128x32x4d)
2947#endif // CONFIG_EXT_PARTITION
2948#endif // CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002949
Yaowu Xuc27fc142016-08-22 16:08:15 -07002950#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002951 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2952 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2953 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002954
Yaowu Xuf883b422016-08-30 14:01:10 -07002955 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2956 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
2957 NULL, aom_sad128x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002958
Yaowu Xuf883b422016-08-30 14:01:10 -07002959 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2960 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
2961 NULL, aom_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002962#endif // CONFIG_EXT_PARTITION
2963
Yaowu Xuf883b422016-08-30 14:01:10 -07002964 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2965 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2966 aom_sad32x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002967
Yaowu Xuf883b422016-08-30 14:01:10 -07002968 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2969 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2970 aom_sad16x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002971
Yaowu Xuf883b422016-08-30 14:01:10 -07002972 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2973 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2974 aom_sad64x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002975
Yaowu Xuf883b422016-08-30 14:01:10 -07002976 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2977 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2978 aom_sad32x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002979
Yaowu Xuf883b422016-08-30 14:01:10 -07002980 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2981 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2982 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002983
Yaowu Xuf883b422016-08-30 14:01:10 -07002984 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2985 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2986 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002987
Yaowu Xuf883b422016-08-30 14:01:10 -07002988 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2989 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2990 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002991
Yaowu Xuf883b422016-08-30 14:01:10 -07002992 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2993 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
2994 aom_sad16x8x8, aom_sad16x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002995
Yaowu Xuf883b422016-08-30 14:01:10 -07002996 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2997 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
2998 aom_sad8x16x8, aom_sad8x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002999
Yaowu Xuf883b422016-08-30 14:01:10 -07003000 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
3001 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
3002 aom_sad8x8x8, aom_sad8x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003003
Yaowu Xuf883b422016-08-30 14:01:10 -07003004 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
3005 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
3006 aom_sad8x4x8, aom_sad8x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003007
Yaowu Xuf883b422016-08-30 14:01:10 -07003008 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
3009 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
3010 aom_sad4x8x8, aom_sad4x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003011
Yaowu Xuf883b422016-08-30 14:01:10 -07003012 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
3013 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
3014 aom_sad4x4x8, aom_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003015
Jingning Han9e7c49f2016-12-06 11:20:10 -08003016 BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL)
Jingning Hane2ffaf82016-12-14 15:26:30 -08003017 BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL)
3018 BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL)
Cheng Chenf78632e2017-10-20 15:30:51 -07003019#endif // CONFIG_JNT_COMP
Jingning Han9e7c49f2016-12-06 11:20:10 -08003020
Yaowu Xuc27fc142016-08-22 16:08:15 -07003021#define OBFP(BT, OSDF, OVF, OSVF) \
3022 cpi->fn_ptr[BT].osdf = OSDF; \
3023 cpi->fn_ptr[BT].ovf = OVF; \
3024 cpi->fn_ptr[BT].osvf = OSVF;
3025
3026#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07003027 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3028 aom_obmc_sub_pixel_variance128x128)
3029 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3030 aom_obmc_sub_pixel_variance128x64)
3031 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3032 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003033#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07003034 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3035 aom_obmc_sub_pixel_variance64x64)
3036 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3037 aom_obmc_sub_pixel_variance64x32)
3038 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3039 aom_obmc_sub_pixel_variance32x64)
3040 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3041 aom_obmc_sub_pixel_variance32x32)
3042 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3043 aom_obmc_sub_pixel_variance32x16)
3044 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3045 aom_obmc_sub_pixel_variance16x32)
3046 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3047 aom_obmc_sub_pixel_variance16x16)
3048 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3049 aom_obmc_sub_pixel_variance16x8)
3050 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3051 aom_obmc_sub_pixel_variance8x16)
3052 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3053 aom_obmc_sub_pixel_variance8x8)
3054 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3055 aom_obmc_sub_pixel_variance4x8)
3056 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3057 aom_obmc_sub_pixel_variance8x4)
3058 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3059 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003060
3061#if CONFIG_EXT_PARTITION_TYPES
3062 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3063 aom_obmc_sub_pixel_variance4x16)
3064
3065 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3066 aom_obmc_sub_pixel_variance16x4)
3067
3068 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3069 aom_obmc_sub_pixel_variance8x32)
3070
3071 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3072 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003073
3074 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3075 aom_obmc_sub_pixel_variance16x64)
3076
3077 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3078 aom_obmc_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003079
3080#if CONFIG_EXT_PARTITION
3081 OBFP(BLOCK_32X128, aom_obmc_sad32x128, aom_obmc_variance32x128,
3082 aom_obmc_sub_pixel_variance32x128)
3083
3084 OBFP(BLOCK_128X32, aom_obmc_sad128x32, aom_obmc_variance128x32,
3085 aom_obmc_sub_pixel_variance128x32)
3086#endif // CONFIG_EXT_PARTITION
3087#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003088
David Barkerf19f35f2017-05-22 16:33:22 +01003089#define MBFP(BT, MCSDF, MCSVF) \
3090 cpi->fn_ptr[BT].msdf = MCSDF; \
3091 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003092
3093#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01003094 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3095 aom_masked_sub_pixel_variance128x128)
3096 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3097 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003098#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01003099 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3100 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3101 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3102 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3103 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3104 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3105 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3106 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3107 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3108 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3109 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3110 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3111 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003112
3113#if CONFIG_EXT_PARTITION_TYPES
3114 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3115
3116 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3117
3118 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3119
3120 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003121
3122 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3123
3124 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003125
3126#if CONFIG_EXT_PARTITION
3127 MBFP(BLOCK_32X128, aom_masked_sad32x128, aom_masked_sub_pixel_variance32x128)
3128
3129 MBFP(BLOCK_128X32, aom_masked_sad128x32, aom_masked_sub_pixel_variance128x32)
3130#endif // CONFIG_EXT_PARTITION
3131#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003132
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003133#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003134 highbd_set_var_fns(cpi);
3135#endif
3136
Yaowu Xuf883b422016-08-30 14:01:10 -07003137 /* av1_init_quantizer() is first called here. Add check in
3138 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003139 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003140 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003141 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003142 av1_init_quantizer(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003143#if CONFIG_AOM_QM
3144 aom_qm_init(cm);
3145#endif
3146
Yaowu Xuf883b422016-08-30 14:01:10 -07003147 av1_loop_filter_init(cm);
Fergus Simpsondac6aff2017-05-18 15:11:38 -07003148#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07003149 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003150 cm->superres_upscaled_width = oxcf->width;
3151 cm->superres_upscaled_height = oxcf->height;
Fergus Simpsondac6aff2017-05-18 15:11:38 -07003152#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003153#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07003154 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003155#endif // CONFIG_LOOP_RESTORATION
3156
3157 cm->error.setjmp = 0;
3158
3159 return cpi;
3160}
3161
3162#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3163
3164#define SNPRINT2(H, T, V) \
3165 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
3166
Yaowu Xuf883b422016-08-30 14:01:10 -07003167void av1_remove_compressor(AV1_COMP *cpi) {
3168 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003169 unsigned int i;
3170 int t;
3171
3172 if (!cpi) return;
3173
3174 cm = &cpi->common;
3175 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003176#if CONFIG_ENTROPY_STATS
3177 if (cpi->oxcf.pass != 1) {
3178 fprintf(stderr, "Writing counts.stt\n");
3179 FILE *f = fopen("counts.stt", "wb");
3180 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
Zoe Liua56f9162017-06-21 22:49:57 -07003181 fwrite(aggregate_fc_per_type, sizeof(aggregate_fc_per_type[0]),
3182 FRAME_CONTEXTS, f);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003183 fclose(f);
3184 }
3185#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003186#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003187 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003188
3189 if (cpi->oxcf.pass != 1) {
3190 char headings[512] = { 0 };
3191 char results[512] = { 0 };
3192 FILE *f = fopen("opsnr.stt", "a");
3193 double time_encoded =
3194 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3195 10000000.000;
3196 double total_encode_time =
3197 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3198 const double dr =
3199 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3200 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3201 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3202 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3203
3204 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003205 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003206 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3207 const double total_ssim =
3208 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3209 snprintf(headings, sizeof(headings),
3210 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003211 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Yaowu Xuc27fc142016-08-22 16:08:15 -07003212 "WstPsnr\tWstSsim\tWstFast\tWstHVS");
3213 snprintf(results, sizeof(results),
3214 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3215 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3216 "%7.3f\t%7.3f\t%7.3f\t%7.3f",
3217 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
3218 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
3219 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
3220 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
3221 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
3222
3223 if (cpi->b_calculate_blockiness) {
3224 SNPRINT(headings, "\t Block\tWstBlck");
3225 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3226 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3227 }
3228
3229 if (cpi->b_calculate_consistency) {
3230 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003231 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003232 (double)cpi->total_inconsistency);
3233
3234 SNPRINT(headings, "\tConsist\tWstCons");
3235 SNPRINT2(results, "\t%7.3f", consistency);
3236 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3237 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003238 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3239 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003240 rate_err, fabs(rate_err));
3241 }
3242
3243 fclose(f);
3244 }
3245
3246#endif
3247
3248#if 0
3249 {
3250 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
3251 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
3252 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
3253 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
3254 cpi->time_compress_data / 1000,
3255 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
3256 }
3257#endif
3258 }
3259
3260 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003261 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003262 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3263
3264 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003265 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003266
3267 // Deallocate allocated thread data.
3268 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07003269 aom_free(thread_data->td->palette_buffer);
Jingning Hand064cf02017-06-01 10:00:39 -07003270 aom_free(thread_data->td->above_pred_buf);
3271 aom_free(thread_data->td->left_pred_buf);
3272 aom_free(thread_data->td->wsrc_buf);
3273 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003274 aom_free(thread_data->td->counts);
3275 av1_free_pc_tree(thread_data->td);
Yaowu Xuf883b422016-08-30 14:01:10 -07003276 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003277 }
3278 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003279 aom_free(cpi->tile_thr_data);
3280 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003281
Yaowu Xuf883b422016-08-30 14:01:10 -07003282 if (cpi->num_workers > 1) av1_loop_filter_dealloc(&cpi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003283
3284 dealloc_compressor_data(cpi);
3285
3286 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3287 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003288 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003289 }
3290
3291#if CONFIG_FP_MB_STATS
3292 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003293 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003294 cpi->twopass.frame_mb_stats_buf = NULL;
3295 }
3296#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003297#if CONFIG_INTERNAL_STATS
3298 aom_free(cpi->ssim_vars);
3299 cpi->ssim_vars = NULL;
3300#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003301
Yaowu Xuf883b422016-08-30 14:01:10 -07003302 av1_remove_common(cm);
3303 av1_free_ref_frame_buffers(cm->buffer_pool);
3304 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003305
3306#ifdef OUTPUT_YUV_SKINMAP
3307 fclose(yuv_skinmap_file);
3308#endif
3309#ifdef OUTPUT_YUV_REC
3310 fclose(yuv_rec_file);
3311#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003312#if 0
3313
3314 if (keyfile)
3315 fclose(keyfile);
3316
3317 if (framepsnr)
3318 fclose(framepsnr);
3319
3320 if (kf_list)
3321 fclose(kf_list);
3322
3323#endif
3324}
3325
Yaowu Xuf883b422016-08-30 14:01:10 -07003326static void generate_psnr_packet(AV1_COMP *cpi) {
3327 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003328 int i;
3329 PSNR_STATS psnr;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003330#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07003331 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003332 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3333#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07003334 aom_calc_psnr(cpi->source, cpi->common.frame_to_show, &psnr);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003335#endif
3336
3337 for (i = 0; i < 4; ++i) {
3338 pkt.data.psnr.samples[i] = psnr.samples[i];
3339 pkt.data.psnr.sse[i] = psnr.sse[i];
3340 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3341 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003342 pkt.kind = AOM_CODEC_PSNR_PKT;
3343 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003344}
3345
Yaowu Xuf883b422016-08-30 14:01:10 -07003346int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003347 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3348
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003349 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003350 return 0;
3351}
3352
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003353void av1_update_reference(AV1_COMP *cpi, int ref_frame_upd_flags) {
3354 cpi->ext_refresh_last_frame = (ref_frame_upd_flags & AOM_LAST_FLAG) != 0;
3355 cpi->ext_refresh_golden_frame = (ref_frame_upd_flags & AOM_GOLD_FLAG) != 0;
3356 cpi->ext_refresh_alt_ref_frame = (ref_frame_upd_flags & AOM_ALT_FLAG) != 0;
3357 cpi->ext_refresh_bwd_ref_frame = (ref_frame_upd_flags & AOM_BWD_FLAG) != 0;
3358 cpi->ext_refresh_alt2_ref_frame = (ref_frame_upd_flags & AOM_ALT2_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003359 cpi->ext_refresh_frame_flags_pending = 1;
3360}
3361
Thomas Daede497d1952017-08-08 17:33:06 -07003362int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3363 AV1_COMMON *const cm = &cpi->common;
3364 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003365 if (cfg) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003366 aom_yv12_copy_frame(cfg, sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003367 return 0;
3368 } else {
3369 return -1;
3370 }
3371}
3372
Thomas Daede497d1952017-08-08 17:33:06 -07003373int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3374 AV1_COMMON *const cm = &cpi->common;
3375 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003376 if (cfg) {
3377 aom_yv12_copy_frame(sd, cfg);
3378 return 0;
3379 } else {
3380 return -1;
3381 }
3382}
3383
3384int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003385 cpi->ext_refresh_frame_context = update;
3386 cpi->ext_refresh_frame_context_pending = 1;
3387 return 0;
3388}
3389
3390#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3391// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3392// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3393// not denoise the UV channels at this time. If ever we implement UV channel
3394// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003395void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003396 uint8_t *src = s->y_buffer;
3397 int h = s->y_height;
3398
3399 do {
3400 fwrite(src, s->y_width, 1, f);
3401 src += s->y_stride;
3402 } while (--h);
3403
3404 src = s->u_buffer;
3405 h = s->uv_height;
3406
3407 do {
3408 fwrite(src, s->uv_width, 1, f);
3409 src += s->uv_stride;
3410 } while (--h);
3411
3412 src = s->v_buffer;
3413 h = s->uv_height;
3414
3415 do {
3416 fwrite(src, s->uv_width, 1, f);
3417 src += s->uv_stride;
3418 } while (--h);
3419}
3420#endif
3421
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003422#if !CONFIG_XIPHRC
Zoe Liu8dd1c982017-09-11 10:14:35 -07003423#if USE_GF16_MULTI_LAYER
3424static void check_show_existing_frame_gf16(AV1_COMP *cpi) {
3425 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3426 AV1_COMMON *const cm = &cpi->common;
3427 const FRAME_UPDATE_TYPE next_frame_update_type =
3428 gf_group->update_type[gf_group->index];
3429
3430 if (cm->show_existing_frame == 1) {
3431 cm->show_existing_frame = 0;
3432 } else if (cpi->rc.is_last_bipred_frame) {
3433 cpi->rc.is_last_bipred_frame = 0;
3434 cm->show_existing_frame = 1;
3435 cpi->existing_fb_idx_to_show = cpi->bwd_fb_idx;
3436 } else if (next_frame_update_type == OVERLAY_UPDATE ||
3437 next_frame_update_type == INTNL_OVERLAY_UPDATE) {
3438 // Check the temporal filtering status for the next OVERLAY frame
3439 const int num_arfs_in_gf = cpi->num_extra_arfs + 1;
3440 int which_arf = 0, arf_idx;
3441 // Identify the index to the next overlay frame.
3442 for (arf_idx = 0; arf_idx < num_arfs_in_gf; arf_idx++) {
3443 if (gf_group->index == cpi->arf_pos_for_ovrly[arf_idx]) {
3444 which_arf = arf_idx;
3445 break;
3446 }
3447 }
3448 assert(arf_idx < num_arfs_in_gf);
3449 if (cpi->is_arf_filter_off[which_arf]) {
3450 cm->show_existing_frame = 1;
3451 cpi->rc.is_src_frame_alt_ref = 1;
3452 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3453 ? cpi->alt_fb_idx
3454 : cpi->bwd_fb_idx;
3455 cpi->is_arf_filter_off[which_arf] = 0;
3456 }
3457 }
3458 cpi->rc.is_src_frame_ext_arf = 0;
3459}
3460#endif // USE_GF16_MULTI_LAYER
3461
Yaowu Xuf883b422016-08-30 14:01:10 -07003462static void check_show_existing_frame(AV1_COMP *cpi) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003463#if USE_GF16_MULTI_LAYER
3464 if (cpi->rc.baseline_gf_interval == 16) {
3465 check_show_existing_frame_gf16(cpi);
3466 return;
3467 }
3468#endif // USE_GF16_MULTI_LAYER
3469
Yaowu Xuc27fc142016-08-22 16:08:15 -07003470 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003471 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003472 const FRAME_UPDATE_TYPE next_frame_update_type =
3473 gf_group->update_type[gf_group->index];
3474 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07003475
3476 if (cm->show_existing_frame == 1) {
3477 cm->show_existing_frame = 0;
3478 } else if (cpi->rc.is_last_bipred_frame) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003479 // NOTE: If the current frame is a last bi-predictive frame, it is
3480 // needed next to show the BWDREF_FRAME, which is pointed by
3481 // the last_fb_idxes[0] after reference frame buffer update
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482 cpi->rc.is_last_bipred_frame = 0;
3483 cm->show_existing_frame = 1;
3484 cpi->existing_fb_idx_to_show = cpi->lst_fb_idxes[0];
3485 } else if (cpi->is_arf_filter_off[which_arf] &&
3486 (next_frame_update_type == OVERLAY_UPDATE ||
3487 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
3488 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003489 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003490 cm->show_existing_frame = 1;
3491 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003492 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3493 ? cpi->alt_fb_idx
3494 : cpi->alt2_fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003495 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003496 }
3497 cpi->rc.is_src_frame_ext_arf = 0;
3498}
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003499#endif // !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07003500
3501#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003502void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003503 uint8_t *src = s->y_buffer;
3504 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003505 if (yuv_rec_file == NULL) return;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003506#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003507 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3508 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3509
3510 do {
3511 fwrite(src16, s->y_width, 2, yuv_rec_file);
3512 src16 += s->y_stride;
3513 } while (--h);
3514
3515 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3516 h = s->uv_height;
3517
3518 do {
3519 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3520 src16 += s->uv_stride;
3521 } while (--h);
3522
3523 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3524 h = s->uv_height;
3525
3526 do {
3527 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3528 src16 += s->uv_stride;
3529 } while (--h);
3530
3531 fflush(yuv_rec_file);
3532 return;
3533 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003534#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003535
3536 do {
3537 fwrite(src, s->y_width, 1, yuv_rec_file);
3538 src += s->y_stride;
3539 } while (--h);
3540
3541 src = s->u_buffer;
3542 h = s->uv_height;
3543
3544 do {
3545 fwrite(src, s->uv_width, 1, yuv_rec_file);
3546 src += s->uv_stride;
3547 } while (--h);
3548
3549 src = s->v_buffer;
3550 h = s->uv_height;
3551
3552 do {
3553 fwrite(src, s->uv_width, 1, yuv_rec_file);
3554 src += s->uv_stride;
3555 } while (--h);
3556
3557 fflush(yuv_rec_file);
3558}
3559#endif // OUTPUT_YUV_REC
3560
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003561#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003562static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3563 int i;
3564 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003565 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003566 AV1_COMMON *const cm = &cpi->common;
3567 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3568 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003569 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003570 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003571 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003572 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003573 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003574 recode = 1;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003575 recode |= (rdc->global_motion_used[i] > 0);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003576 }
3577 }
3578 return recode;
3579}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003580
Yaowu Xuc27fc142016-08-22 16:08:15 -07003581// Function to test for conditions that indicate we should loop
3582// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003583static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3584 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003585 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003586 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003587 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3588 int force_recode = 0;
3589
3590 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3591 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3592 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003593 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3594 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3595 (rc->projected_frame_size < low_limit && q > minq)) {
3596 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003597 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003598 // Deal with frame undershoot and whether or not we are
3599 // below the automatically set cq level.
3600 if (q > oxcf->cq_level &&
3601 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3602 force_recode = 1;
3603 }
3604 }
3605 }
3606 return force_recode;
3607}
3608
Yaowu Xuc27fc142016-08-22 16:08:15 -07003609#define DUMP_REF_FRAME_IMAGES 0
3610
3611#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003612static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003613 const YV12_BUFFER_CONFIG *const ref_buf,
3614 char *file_name) {
3615 int h;
3616 FILE *f_ref = NULL;
3617
3618 if (ref_buf == NULL) {
3619 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003620 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003621 }
3622
3623 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3624 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003625 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003626 }
3627
3628 // --- Y ---
3629 for (h = 0; h < cm->height; ++h) {
3630 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3631 }
3632 // --- U ---
3633 for (h = 0; h < (cm->height >> 1); ++h) {
3634 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3635 f_ref);
3636 }
3637 // --- V ---
3638 for (h = 0; h < (cm->height >> 1); ++h) {
3639 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3640 f_ref);
3641 }
3642
3643 fclose(f_ref);
3644
Yaowu Xuf883b422016-08-30 14:01:10 -07003645 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003646}
3647
Yaowu Xuf883b422016-08-30 14:01:10 -07003648static void dump_ref_frame_images(AV1_COMP *cpi) {
3649 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003650 MV_REFERENCE_FRAME ref_frame;
3651
3652 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3653 char file_name[256] = "";
3654 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3655 cm->current_video_frame, ref_frame);
3656 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3657 }
3658}
3659#endif // DUMP_REF_FRAME_IMAGES == 1
3660
Yaowu Xuc27fc142016-08-22 16:08:15 -07003661// This function is used to shift the virtual indices of last reference frames
3662// as follows:
3663// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3664// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003665static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003666 int ref_frame;
3667 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
3668 cpi->lst_fb_idxes[ref_frame] = cpi->lst_fb_idxes[ref_frame - 1];
3669
3670 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003671 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003672 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003673 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3674 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3675 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003676 }
3677 }
3678}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003679
Zoe Liu8dd1c982017-09-11 10:14:35 -07003680#if USE_GF16_MULTI_LAYER
3681static void update_reference_frames_gf16(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003682 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003683 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003684
Zoe Liu8dd1c982017-09-11 10:14:35 -07003685 if (cm->frame_type == KEY_FRAME) {
3686 for (int ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3687 ref_cnt_fb(pool->frame_bufs,
3688 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3689 cm->new_fb_idx);
3690 }
3691 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3692 cm->new_fb_idx);
3693 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3694 cm->new_fb_idx);
3695 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3696 cm->new_fb_idx);
3697 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3698 cm->new_fb_idx);
3699 } else {
3700 if (cpi->refresh_last_frame || cpi->refresh_golden_frame ||
3701 cpi->refresh_bwd_ref_frame || cpi->refresh_alt2_ref_frame ||
3702 cpi->refresh_alt_ref_frame) {
3703 assert(cpi->refresh_fb_idx >= 0 && cpi->refresh_fb_idx < REF_FRAMES);
3704 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->refresh_fb_idx],
3705 cm->new_fb_idx);
3706 }
3707
3708 // TODO(zoeliu): To handle cpi->interp_filter_selected[].
3709
3710 // For GF of 16, an additional ref frame index mapping needs to be handled
3711 // if this is the last frame to encode in the current GF group.
3712 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3713 if (gf_group->update_type[gf_group->index + 1] == OVERLAY_UPDATE)
3714 av1_ref_frame_map_idx_updates(cpi, gf_group->index + 1);
3715 }
3716
3717#if DUMP_REF_FRAME_IMAGES == 1
3718 // Dump out all reference frame images.
3719 dump_ref_frame_images(cpi);
3720#endif // DUMP_REF_FRAME_IMAGES
3721}
3722#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003723
3724static void update_reference_frames(AV1_COMP *cpi) {
3725 AV1_COMMON *const cm = &cpi->common;
3726
Yaowu Xuc27fc142016-08-22 16:08:15 -07003727 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3728 // for the purpose to verify no mismatch between encoder and decoder.
3729 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3730
Zoe Liu8dd1c982017-09-11 10:14:35 -07003731#if USE_GF16_MULTI_LAYER
3732 if (cpi->rc.baseline_gf_interval == 16) {
3733 update_reference_frames_gf16(cpi);
3734 return;
3735 }
3736#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003737
3738 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003739 // At this point the new frame has been encoded.
3740 // If any buffer copy / swapping is signaled it should be done here.
3741 if (cm->frame_type == KEY_FRAME) {
3742 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3743 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003744 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3745 cm->new_fb_idx);
Zoe Liue9b15e22017-07-19 15:53:01 -07003746 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3747 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003748 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3749 cm->new_fb_idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003750 } else if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003751 // We have decided to preserve the previously existing golden frame as our
3752 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003753 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003754 // we're updating the GF with the current decoded frame, we save it to the
3755 // ARF slot instead.
3756 // We now have to update the ARF with the current frame and swap gld_fb_idx
3757 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3758 // slot and, if we're updating the GF, the current frame becomes the new GF.
3759 int tmp;
3760
3761 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3762 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003763 tmp = cpi->alt_fb_idx;
3764 cpi->alt_fb_idx = cpi->gld_fb_idx;
3765 cpi->gld_fb_idx = tmp;
3766
Yaowu Xuc27fc142016-08-22 16:08:15 -07003767 // We need to modify the mapping accordingly
3768 cpi->arf_map[0] = cpi->alt_fb_idx;
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003769 // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3770 // cpi->interp_filter_selected[GOLDEN_FRAME]?
Yaowu Xuc27fc142016-08-22 16:08:15 -07003771 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3772 // Deal with the special case for showing existing internal ALTREF_FRAME
3773 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3774 // by updating the virtual indices.
3775 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -07003776 const int which_arf = gf_group->arf_ref_idx[gf_group->index];
3777 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003778
Zoe Liue9b15e22017-07-19 15:53:01 -07003779 const int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003780 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003781
Zoe Liue9b15e22017-07-19 15:53:01 -07003782 cpi->lst_fb_idxes[0] = cpi->alt2_fb_idx;
3783 cpi->alt2_fb_idx = tmp;
3784 // We need to modify the mapping accordingly
3785 cpi->arf_map[which_arf] = cpi->alt2_fb_idx;
3786
3787 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3788 cpi->interp_filter_selected[ALTREF2_FRAME],
3789 sizeof(cpi->interp_filter_selected[ALTREF2_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003790 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003791 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003792 if (cpi->refresh_alt_ref_frame) {
3793 int arf_idx = cpi->alt_fb_idx;
3794 int which_arf = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003795 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003796
3797 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3798 cpi->interp_filter_selected[0],
3799 sizeof(cpi->interp_filter_selected[0]));
3800 }
3801
Zoe Liue9b15e22017-07-19 15:53:01 -07003802 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003803 if (cpi->refresh_golden_frame) {
3804 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3805 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003806
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003807 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3808 cpi->interp_filter_selected[0],
3809 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003810 }
3811
Zoe Liue9b15e22017-07-19 15:53:01 -07003812 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003813 if (cpi->refresh_bwd_ref_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003814 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3815 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003816
3817 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3818 cpi->interp_filter_selected[0],
3819 sizeof(cpi->interp_filter_selected[0]));
3820 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003821
Zoe Liue9b15e22017-07-19 15:53:01 -07003822 // === ALTREF2_FRAME ===
3823 if (cpi->refresh_alt2_ref_frame) {
3824 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3825 cm->new_fb_idx);
3826
3827 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3828 cpi->interp_filter_selected[0],
3829 sizeof(cpi->interp_filter_selected[0]));
3830 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003831 }
3832
3833 if (cpi->refresh_last_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003834 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3835 // reference to the reference frame buffer virtual index; and then (2) from
3836 // the virtual index to the reference frame buffer physical index:
3837 //
3838 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3839 // | | |
3840 // v v v
3841 // lst_fb_idxes[0], ..., lst_fb_idxes[2], ..., alt_fb_idx
3842 // | | |
3843 // v v v
3844 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3845 //
3846 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3847 // have the other 2 LAST reference frames shifted as follows:
3848 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3849 // , and then have LAST_FRAME refreshed by the newly coded frame.
3850 //
3851 // To fulfill it, the decoder will be notified to execute following 2 steps:
3852 //
3853 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3854 // to point to the newly coded frame, i.e.
3855 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3856 //
3857 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3858 // original virtual index of LAST3_FRAME and have the other mappings
3859 // shifted as follows:
3860 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3861 // | | |
3862 // v v v
3863 // lst_fb_idxes[2], lst_fb_idxes[0], lst_fb_idxes[1]
3864 int ref_frame;
Zoe Liu5fca7242016-10-10 17:18:57 -07003865
Yaowu Xuc27fc142016-08-22 16:08:15 -07003866 if (cm->frame_type == KEY_FRAME) {
3867 for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3868 ref_cnt_fb(pool->frame_bufs,
3869 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3870 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003871 }
3872 } else {
3873 int tmp;
3874
3875 ref_cnt_fb(pool->frame_bufs,
3876 &cm->ref_frame_map[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3877 cm->new_fb_idx);
3878
Yaowu Xuc27fc142016-08-22 16:08:15 -07003879 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3880
3881 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003882 cpi->lst_fb_idxes[0] = tmp;
3883
Zoe Liuf0e46692016-10-12 12:31:43 -07003884 assert(cm->show_existing_frame == 0);
Zoe Liuf0e46692016-10-12 12:31:43 -07003885 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3886 cpi->interp_filter_selected[0],
3887 sizeof(cpi->interp_filter_selected[0]));
Zoe Liuaff92d52017-07-11 21:35:08 -07003888
3889 if (cpi->rc.is_last_bipred_frame) {
3890 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3891 // LAST3_FRAME by updating the virtual indices.
3892 //
3893 // NOTE: The source frame for BWDREF does not have a holding position as
3894 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3895 // virtual index reshuffling for BWDREF, the encoder always
3896 // specifies a LAST_BIPRED right before BWDREF and completes the
3897 // reshuffling job accordingly.
3898 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3899
3900 shift_last_ref_frames(cpi);
3901 cpi->lst_fb_idxes[0] = cpi->bwd_fb_idx;
3902 cpi->bwd_fb_idx = tmp;
3903
3904 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3905 cpi->interp_filter_selected[BWDREF_FRAME],
3906 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
3907 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003908 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003909 }
3910
3911#if DUMP_REF_FRAME_IMAGES == 1
3912 // Dump out all reference frame images.
3913 dump_ref_frame_images(cpi);
3914#endif // DUMP_REF_FRAME_IMAGES
3915}
3916
Yaowu Xuf883b422016-08-30 14:01:10 -07003917static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003918 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003919 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003920 ensure_mv_buffer(new_fb_ptr, cm);
3921 new_fb_ptr->width = cm->width;
3922 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003923}
3924
Cheng Chen46f30c72017-09-07 11:13:33 -07003925static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003926 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003927 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003928 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003929 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3930 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003931 };
3932
3933 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003934 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003935 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3936 BufferPool *const pool = cm->buffer_pool;
3937 const YV12_BUFFER_CONFIG *const ref =
3938 get_ref_frame_buffer(cpi, ref_frame);
3939
3940 if (ref == NULL) {
3941 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3942 continue;
3943 }
3944
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003945#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003946 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3947 RefCntBuffer *new_fb_ptr = NULL;
3948 int force_scaling = 0;
3949 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3950 if (new_fb == INVALID_IDX) {
3951 new_fb = get_free_fb(cm);
3952 force_scaling = 1;
3953 }
3954 if (new_fb == INVALID_IDX) return;
3955 new_fb_ptr = &pool->frame_bufs[new_fb];
3956 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3957 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003958 if (aom_realloc_frame_buffer(
3959 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3960 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3961 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003962 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003963 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003964 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf,
3965 (int)cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003966 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3967 alloc_frame_mvs(cm, new_fb);
3968 }
3969#else
3970 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3971 RefCntBuffer *new_fb_ptr = NULL;
3972 int force_scaling = 0;
3973 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3974 if (new_fb == INVALID_IDX) {
3975 new_fb = get_free_fb(cm);
3976 force_scaling = 1;
3977 }
3978 if (new_fb == INVALID_IDX) return;
3979 new_fb_ptr = &pool->frame_bufs[new_fb];
3980 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3981 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003982 if (aom_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003983 cm->subsampling_x, cm->subsampling_y,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003984 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3985 NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003986 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003987 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003988 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003989 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3990 alloc_frame_mvs(cm, new_fb);
3991 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003992#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003993 } else {
3994 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3995 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3996 buf->buf.y_crop_width = ref->y_crop_width;
3997 buf->buf.y_crop_height = ref->y_crop_height;
3998 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3999 ++buf->ref_count;
4000 }
4001 } else {
4002 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
4003 }
4004 }
4005}
4006
Yaowu Xuf883b422016-08-30 14:01:10 -07004007static void release_scaled_references(AV1_COMP *cpi) {
4008 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004009 int i;
4010 if (cpi->oxcf.pass == 0) {
4011 // Only release scaled references under certain conditions:
4012 // if reference will be updated, or if scaled reference has same resolution.
4013 int refresh[INTER_REFS_PER_FRAME];
4014 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004015 refresh[1] = refresh[2] = 0;
4016 refresh[3] = (cpi->refresh_golden_frame) ? 1 : 0;
4017 refresh[4] = (cpi->refresh_bwd_ref_frame) ? 1 : 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07004018 refresh[5] = (cpi->refresh_alt2_ref_frame) ? 1 : 0;
4019 refresh[6] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004020 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
4021 const int idx = cpi->scaled_ref_idx[i - 1];
4022 RefCntBuffer *const buf =
4023 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
4024 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
4025 if (buf != NULL &&
4026 (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
4027 buf->buf.y_crop_height == ref->y_crop_height))) {
4028 --buf->ref_count;
4029 cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
4030 }
4031 }
4032 } else {
4033 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) {
4034 const int idx = cpi->scaled_ref_idx[i];
4035 RefCntBuffer *const buf =
4036 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
4037 if (buf != NULL) {
4038 --buf->ref_count;
4039 cpi->scaled_ref_idx[i] = INVALID_IDX;
4040 }
4041 }
4042 }
4043}
4044
Yaowu Xuc27fc142016-08-22 16:08:15 -07004045#if 0 && CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07004046static void output_frame_level_debug_stats(AV1_COMP *cpi) {
4047 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004048 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
4049 int64_t recon_err;
4050
Yaowu Xuf883b422016-08-30 14:01:10 -07004051 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004052
Alex Conversef77fd0b2017-04-20 11:00:24 -07004053 recon_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004054
4055 if (cpi->twopass.total_left_stats.coded_error != 0.0)
Yunqing Wang8c1e57c2016-10-25 15:15:23 -07004056 fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
Yaowu Xuc27fc142016-08-22 16:08:15 -07004057 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
4058 "%10"PRId64" %10"PRId64" %10d "
4059 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
4060 "%6d %6d %5d %5d %5d "
4061 "%10"PRId64" %10.3lf"
4062 "%10lf %8u %10"PRId64" %10d %10d %10d\n",
4063 cpi->common.current_video_frame,
4064 cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004065 cpi->rc.source_alt_ref_pending,
4066 cpi->rc.source_alt_ref_active,
4067 cpi->rc.this_frame_target,
4068 cpi->rc.projected_frame_size,
4069 cpi->rc.projected_frame_size / cpi->common.MBs,
4070 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
4071 cpi->rc.vbr_bits_off_target,
4072 cpi->rc.vbr_bits_off_target_fast,
4073 cpi->twopass.extend_minq,
4074 cpi->twopass.extend_minq_fast,
4075 cpi->rc.total_target_vs_actual,
4076 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
4077 cpi->rc.total_actual_bits, cm->base_qindex,
Yaowu Xuf883b422016-08-30 14:01:10 -07004078 av1_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
4079 (double)av1_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
4080 av1_convert_qindex_to_q(cpi->twopass.active_worst_quality,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004081 cm->bit_depth),
4082 cpi->rc.avg_q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004083 av1_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
Yaowu Xuc27fc142016-08-22 16:08:15 -07004084 cpi->refresh_last_frame, cpi->refresh_golden_frame,
4085 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
4086 cpi->twopass.bits_left,
4087 cpi->twopass.total_left_stats.coded_error,
4088 cpi->twopass.bits_left /
4089 (1 + cpi->twopass.total_left_stats.coded_error),
4090 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
4091 cpi->twopass.kf_zeromotion_pct,
4092 cpi->twopass.fr_content_type);
4093
4094 fclose(f);
4095
4096 if (0) {
4097 FILE *const fmodes = fopen("Modes.stt", "a");
4098 int i;
4099
4100 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
4101 cm->frame_type, cpi->refresh_golden_frame,
4102 cpi->refresh_alt_ref_frame);
4103
4104 for (i = 0; i < MAX_MODES; ++i)
4105 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4106
4107 fprintf(fmodes, "\n");
4108
4109 fclose(fmodes);
4110 }
4111}
4112#endif
4113
Yaowu Xuf883b422016-08-30 14:01:10 -07004114static void set_mv_search_params(AV1_COMP *cpi) {
4115 const AV1_COMMON *const cm = &cpi->common;
4116 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004117
4118 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07004119 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004120
4121 if (cpi->sf.mv.auto_mv_step_size) {
4122 if (frame_is_intra_only(cm)) {
4123 // Initialize max_mv_magnitude for use in the first INTER frame
4124 // after a key/intra-only frame.
4125 cpi->max_mv_magnitude = max_mv_def;
4126 } else {
4127 if (cm->show_frame) {
4128 // Allow mv_steps to correspond to twice the max mv magnitude found
4129 // in the previous frame, capped by the default max_mv_magnitude based
4130 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07004131 cpi->mv_step_param = av1_init_search_range(
4132 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004133 }
4134 cpi->max_mv_magnitude = 0;
4135 }
4136 }
4137}
4138
Yaowu Xuf883b422016-08-30 14:01:10 -07004139static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004140 int i;
4141 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01004142 cpi->common.global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004143 }
4144 cpi->global_motion_search_done = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004145 av1_set_speed_features_framesize_independent(cpi);
4146 av1_set_rd_speed_thresholds(cpi);
4147 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004148 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07004149 if (!frame_is_intra_only(&cpi->common)) set_compound_tools(&cpi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004150}
4151
Yaowu Xuf883b422016-08-30 14:01:10 -07004152static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004153 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004154 AV1_COMMON *const cm = &cpi->common;
4155 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004156
4157 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004158 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004159
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004160// Decide q and q bounds.
4161#if CONFIG_XIPHRC
4162 int frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
4163 *q = od_enc_rc_select_quantizers_and_lambdas(
4164 &cpi->od_rc, cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
4165 frame_type, bottom_index, top_index);
4166#else
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004167 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
4168 top_index);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004169#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004170
James Zern01a9d702017-08-25 19:09:33 +00004171 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07004172#if CONFIG_AMVR
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07004173#if CONFIG_EIGHTH_PEL_MV_ONLY
4174 set_high_precision_mv(cpi, 1, cpi->common.cur_frame_force_integer_mv);
4175#else
RogerZhou3b635242017-09-19 10:06:46 -07004176 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07004177 cpi->common.cur_frame_force_integer_mv);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07004178#endif // CONFIG_EIGHTH_PEL_MV_ONLY
4179#else
4180#if CONFIG_EIGHTH_PEL_MV_ONLY
4181 set_high_precision_mv(cpi, 1);
RogerZhou3b635242017-09-19 10:06:46 -07004182#else
Cheng Chen46f30c72017-09-07 11:13:33 -07004183 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07004184#endif // CONFIG_EIGHTH_PEL_MV_ONLY
RogerZhou3b635242017-09-19 10:06:46 -07004185#endif
James Zern01a9d702017-08-25 19:09:33 +00004186 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004187
4188 // Configure experimental use of segmentation for enhanced coding of
4189 // static regions if indicated.
4190 // Only allowed in the second pass of a two pass encode, as it requires
4191 // lagged coding, and if the relevant speed feature flag is set.
4192 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
4193 configure_static_seg_features(cpi);
4194}
4195
Yaowu Xuf883b422016-08-30 14:01:10 -07004196static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004197 int y_stride = cpi->scaled_source.y_stride;
4198
4199 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004200 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004201 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004202 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004203 }
4204}
4205
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004206#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004207#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004208static void set_restoration_unit_size(int width, int height, int sx, int sy,
4209 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004210 (void)width;
4211 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004212 (void)sx;
4213 (void)sy;
4214#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4215 int s = AOMMIN(sx, sy);
4216#else
4217 int s = 0;
4218#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4219
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004220 rst[0].restoration_unit_size = (RESTORATION_TILESIZE_MAX >> 1);
4221 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
4222 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004223}
4224#endif // CONFIG_LOOP_RESTORATION
4225
Cheng Chen46f30c72017-09-07 11:13:33 -07004226static void init_ref_frame_bufs(AV1_COMMON *cm) {
4227 int i;
4228 BufferPool *const pool = cm->buffer_pool;
4229 cm->new_fb_idx = INVALID_IDX;
4230 for (i = 0; i < REF_FRAMES; ++i) {
4231 cm->ref_frame_map[i] = INVALID_IDX;
4232 pool->frame_bufs[i].ref_count = 0;
4233 }
4234#if CONFIG_HASH_ME
4235 for (i = 0; i < FRAME_BUFFERS; ++i) {
4236 av1_hash_table_init(&pool->frame_bufs[i].hash_table);
4237 }
4238#endif
4239}
4240
4241static void check_initial_width(AV1_COMP *cpi,
4242#if CONFIG_HIGHBITDEPTH
4243 int use_highbitdepth,
4244#endif
4245 int subsampling_x, int subsampling_y) {
4246 AV1_COMMON *const cm = &cpi->common;
4247
4248 if (!cpi->initial_width ||
4249#if CONFIG_HIGHBITDEPTH
4250 cm->use_highbitdepth != use_highbitdepth ||
4251#endif
4252 cm->subsampling_x != subsampling_x ||
4253 cm->subsampling_y != subsampling_y) {
4254 cm->subsampling_x = subsampling_x;
4255 cm->subsampling_y = subsampling_y;
4256#if CONFIG_HIGHBITDEPTH
4257 cm->use_highbitdepth = use_highbitdepth;
4258#endif
4259
4260 alloc_raw_frame_buffers(cpi);
4261 init_ref_frame_bufs(cm);
4262 alloc_util_frame_buffers(cpi);
4263
4264 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4265
4266 cpi->initial_width = cm->width;
4267 cpi->initial_height = cm->height;
4268 cpi->initial_mbs = cm->MBs;
4269 }
4270}
4271
4272// Returns 1 if the assigned width or height was <= 0.
4273static int set_size_literal(AV1_COMP *cpi, int width, int height) {
4274 AV1_COMMON *cm = &cpi->common;
4275#if CONFIG_HIGHBITDEPTH
4276 check_initial_width(cpi, cm->use_highbitdepth, cm->subsampling_x,
4277 cm->subsampling_y);
4278#else
4279 check_initial_width(cpi, cm->subsampling_x, cm->subsampling_y);
4280#endif // CONFIG_HIGHBITDEPTH
4281
4282 if (width <= 0 || height <= 0) return 1;
4283
4284 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004285 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004286
4287 if (cpi->initial_width && cpi->initial_height &&
4288 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4289 av1_free_context_buffers(cm);
4290 av1_free_pc_tree(&cpi->td);
4291 alloc_compressor_data(cpi);
4292 realloc_segmentation_maps(cpi);
4293 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004294 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004295 update_frame_size(cpi);
4296
4297 return 0;
4298}
4299
Fergus Simpsonbc189932017-05-16 17:02:39 -07004300static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004301 AV1_COMMON *const cm = &cpi->common;
Fergus Simpsonbc189932017-05-16 17:02:39 -07004302 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004303 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004304
Fergus Simpsonbc189932017-05-16 17:02:39 -07004305 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004306 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07004307 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004308 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004309 }
4310
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004311#if !CONFIG_XIPHRC
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004312 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004313 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004314 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004315#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004316
4317 alloc_frame_mvs(cm, cm->new_fb_idx);
4318
4319 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07004320 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004321 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004322#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004323 cm->use_highbitdepth,
4324#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07004325 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
4326 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004327 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004328 "Failed to allocate frame buffer");
4329
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004330#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004331#if CONFIG_FRAME_SUPERRES
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004332 const int frame_width = cm->superres_upscaled_width;
4333 const int frame_height = cm->superres_upscaled_height;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004334#else
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004335 const int frame_width = cm->width;
4336 const int frame_height = cm->height;
4337#endif
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01004338 set_restoration_unit_size(frame_width, frame_height, cm->subsampling_x,
4339 cm->subsampling_y, cm->rst_info);
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004340 for (int i = 0; i < MAX_MB_PLANE; ++i)
4341 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004342
4343 av1_alloc_restoration_buffers(cm);
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004344#endif // CONFIG_LOOP_RESTORATION
4345 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004346 init_motion_estimation(cpi);
4347
4348 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4349 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
4350 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4351
4352 ref_buf->idx = buf_idx;
4353
4354 if (buf_idx != INVALID_IDX) {
4355 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
4356 ref_buf->buf = buf;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004357#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004358 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004359 &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
4360 cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
4361#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004362 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
4363 buf->y_crop_height, cm->width,
4364 cm->height);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004365#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004366 if (av1_is_scaled(&ref_buf->sf)) aom_extend_frame_borders(buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004367 } else {
4368 ref_buf->buf = NULL;
4369 }
4370 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004371
Alex Conversee816b312017-05-01 09:51:24 -07004372#if CONFIG_INTRABC
4373#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan1dcb7072017-05-12 11:13:05 +02004374 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4375 cm->width, cm->height,
4376 cm->use_highbitdepth);
Alex Conversee816b312017-05-01 09:51:24 -07004377#else
4378 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4379 cm->width, cm->height);
4380#endif // CONFIG_HIGHBITDEPTH
4381#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004382
4383 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4384}
4385
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004386static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4387 // Choose an arbitrary random number
4388 static unsigned int seed = 56789;
4389 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004390 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4391 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004392
4393 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004394 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004395 case RESIZE_FIXED:
4396 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004397 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004398 else
Urvang Joshide71d142017-10-05 12:12:15 -07004399 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004400 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004401 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004402 default: assert(0);
4403 }
Urvang Joshide71d142017-10-05 12:12:15 -07004404 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004405}
4406
4407#if CONFIG_FRAME_SUPERRES
Urvang Joshie58b5642017-10-05 17:59:43 -07004408
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004409static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4410 // Choose an arbitrary random number
4411 static unsigned int seed = 34567;
4412 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004413 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4414 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004415 int bottom_index, top_index, q, qthresh;
4416
4417 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004418 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004419 case SUPERRES_FIXED:
4420 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004421 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004422 else
Urvang Joshide71d142017-10-05 12:12:15 -07004423 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004424 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004425 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004426 case SUPERRES_QTHRESH:
4427 qthresh = (cpi->common.frame_type == KEY_FRAME ? oxcf->superres_kf_qthresh
4428 : oxcf->superres_qthresh);
4429 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
4430 q = av1_rc_pick_q_and_bounds(cpi, cpi->oxcf.width, cpi->oxcf.height,
4431 &bottom_index, &top_index);
4432 if (q < qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004433 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004434 } else {
Urvang Joshi28983f72017-10-25 14:41:06 -07004435 const uint8_t min_denom = SCALE_NUMERATOR + 1;
4436 const uint8_t denom_step = (MAXQ - qthresh + 1) >> 3;
4437 const uint8_t additional_denom = (q - qthresh) / denom_step;
4438 new_denom = AOMMIN(min_denom + additional_denom, SCALE_NUMERATOR << 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004439 }
4440 break;
4441 default: assert(0);
4442 }
Urvang Joshide71d142017-10-05 12:12:15 -07004443 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004444}
4445
Urvang Joshide71d142017-10-05 12:12:15 -07004446static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4447 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4448}
4449
4450static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
4451 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom) &&
Urvang Joshi3d8bcb22017-10-09 12:18:59 -07004452 (CONFIG_HORZONLY_FRAME_SUPERRES ||
Urvang Joshie58b5642017-10-05 17:59:43 -07004453 dimension_is_ok(oheight, rsz->resize_height, rsz->superres_denom));
Urvang Joshide71d142017-10-05 12:12:15 -07004454}
4455
4456#define DIVIDE_AND_ROUND(x, y) (((x) + ((y) >> 1)) / (y))
4457
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004458static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004459 SUPERRES_MODE superres_mode, int owidth,
4460 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004461 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004462 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004463 }
4464
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004465 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004466 int resize_denom =
4467 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4468 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4469
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004470 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004471 // Alter superres scale as needed to enforce conformity.
4472 rsz->superres_denom =
4473 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4474 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4475 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004476 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004477 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004478 // Alter resize scale as needed to enforce conformity.
4479 resize_denom =
4480 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004481 rsz->resize_width = owidth;
4482 rsz->resize_height = oheight;
4483 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004484 resize_denom);
4485 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4486 if (resize_denom > SCALE_NUMERATOR) {
4487 --resize_denom;
4488 rsz->resize_width = owidth;
4489 rsz->resize_height = oheight;
4490 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4491 resize_denom);
4492 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004493 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004494 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004495 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004496 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004497 if (resize_denom > rsz->superres_denom)
4498 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004499 else
Urvang Joshide71d142017-10-05 12:12:15 -07004500 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004501 rsz->resize_width = owidth;
4502 rsz->resize_height = oheight;
4503 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004504 resize_denom);
4505 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4506 (resize_denom > SCALE_NUMERATOR ||
4507 rsz->superres_denom > SCALE_NUMERATOR));
4508 } else { // We are allowed to alter neither resize scale nor superres scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004509 return 0;
4510 }
Urvang Joshide71d142017-10-05 12:12:15 -07004511 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004512}
Urvang Joshide71d142017-10-05 12:12:15 -07004513#undef DIVIDE_AND_ROUND
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004514#endif // CONFIG_FRAME_SUPERRES
4515
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004516// Calculates resize and superres params for next frame
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004517size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
4518 const AV1EncoderConfig *oxcf = &cpi->oxcf;
4519 size_params_type rsz = {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004520 oxcf->width,
4521 oxcf->height,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004522#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07004523 SCALE_NUMERATOR
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004524#endif // CONFIG_FRAME_SUPERRES
4525 };
Urvang Joshide71d142017-10-05 12:12:15 -07004526 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004527 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004528 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4529 rsz.resize_width = cpi->resize_pending_width;
4530 rsz.resize_height = cpi->resize_pending_height;
4531 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4532 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004533 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004534 rsz.resize_width = cpi->oxcf.width;
4535 rsz.resize_height = cpi->oxcf.height;
4536 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004537 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004538 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004539#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07004540 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004541 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4542 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004543 assert(0 && "Invalid scale parameters");
4544#endif // CONFIG_FRAME_SUPERRES
4545 return rsz;
4546}
4547
4548static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004549 int encode_width = rsz->resize_width;
4550 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004551
4552#if CONFIG_FRAME_SUPERRES
4553 AV1_COMMON *cm = &cpi->common;
4554 cm->superres_upscaled_width = encode_width;
4555 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004556 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004557 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4558 rsz->superres_denom);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004559#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004560 set_frame_size(cpi, encode_width, encode_height);
4561}
4562
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004563static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004564 size_params_type rsz = av1_calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004565 setup_frame_size_from_params(cpi, &rsz);
4566}
4567
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004568#if CONFIG_FRAME_SUPERRES
4569static void superres_post_encode(AV1_COMP *cpi) {
4570 AV1_COMMON *cm = &cpi->common;
4571
4572 if (av1_superres_unscaled(cm)) return;
4573
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004574 av1_superres_upscale(cm, NULL);
4575
4576 // If regular resizing is occurring the source will need to be downscaled to
4577 // match the upscaled superres resolution. Otherwise the original source is
4578 // used.
4579 if (av1_resize_unscaled(cm)) {
4580 cpi->source = cpi->unscaled_source;
4581 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4582 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004583 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4584 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004585 // Do downscale. cm->(width|height) has been updated by av1_superres_upscale
4586 if (aom_realloc_frame_buffer(
4587 &cpi->scaled_source, cm->superres_upscaled_width,
4588 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
4589#if CONFIG_HIGHBITDEPTH
4590 cm->use_highbitdepth,
4591#endif // CONFIG_HIGHBITDEPTH
4592 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
4593 aom_internal_error(
4594 &cm->error, AOM_CODEC_MEM_ERROR,
4595 "Failed to reallocate scaled source buffer for superres");
4596 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4597 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
4598#if CONFIG_HIGHBITDEPTH
4599 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
4600 (int)cm->bit_depth);
4601#else
4602 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source);
4603#endif // CONFIG_HIGHBITDEPTH
4604 cpi->source = &cpi->scaled_source;
4605 }
4606}
4607#endif // CONFIG_FRAME_SUPERRES
4608
4609static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4610 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4611 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004612 int no_loopfilter = 0;
Yaowu Xu35ee2342017-11-08 11:50:46 -08004613#if CONFIG_CDEF
4614 int no_cdef = 0;
4615#endif
4616#if CONFIG_LOOP_RESTORATION
4617 int no_restoration = 0;
4618#endif
4619 if (is_lossless_requested(&cpi->oxcf)
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004620#if CONFIG_EXT_TILE
Yaowu Xu35ee2342017-11-08 11:50:46 -08004621 || cm->single_tile_decoding
4622#endif
4623 ) {
4624 no_loopfilter = 1;
4625#if CONFIG_CDEF
4626 no_cdef = 1;
4627#endif
4628#if CONFIG_LOOP_RESTORATION
4629 no_restoration = 1;
4630#endif
4631 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004632
4633 if (no_loopfilter) {
Cheng Chen13fc8192017-08-19 11:49:28 -07004634#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004635 lf->filter_level[0] = 0;
4636 lf->filter_level[1] = 0;
4637#else
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004638 lf->filter_level = 0;
Cheng Chen179479f2017-08-04 10:56:39 -07004639#endif
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004640 } else {
4641 struct aom_usec_timer timer;
4642
4643 aom_clear_system_state();
4644
4645 aom_usec_timer_start(&timer);
4646
4647 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4648
4649 aom_usec_timer_mark(&timer);
4650 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4651 }
4652
Cheng Chen5ad5b282017-10-05 16:36:06 -07004653#if CONFIG_INTRABC
4654// When intraBC is on, do loop filtering per superblock,
4655// instead of do it after the whole frame has been encoded,
4656// as is in the else branch
4657#else
Cheng Chenf572cd32017-08-25 18:34:51 -07004658#if !CONFIG_LPF_SB
Cheng Chen13fc8192017-08-19 11:49:28 -07004659#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004660 if (lf->filter_level[0] || lf->filter_level[1])
4661#else
4662 if (lf->filter_level > 0)
4663#endif
Cheng Chenf572cd32017-08-25 18:34:51 -07004664#endif // CONFIG_LPF_SB
Cheng Chen179479f2017-08-04 10:56:39 -07004665 {
Cheng Chenf572cd32017-08-25 18:34:51 -07004666#if CONFIG_LPF_SB
4667 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0, 0,
4668 0);
4669#else
Cheng Chen13fc8192017-08-19 11:49:28 -07004670#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004671 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level[0],
4672 lf->filter_level[1], 0, 0);
4673 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_u,
4674 lf->filter_level_u, 1, 0);
4675 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_v,
4676 lf->filter_level_v, 2, 0);
4677
Cheng Chene94df5c2017-07-19 17:25:33 -07004678#else
4679 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
Cheng Chen13fc8192017-08-19 11:49:28 -07004680#endif // CONFIG_LOOPFILTER_LEVEL
Cheng Chenf572cd32017-08-25 18:34:51 -07004681#endif // CONFIG_LPF_SB
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004682 }
Cheng Chen5ad5b282017-10-05 16:36:06 -07004683#endif // CONFIG_INTRABC
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004684
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004685#if CONFIG_STRIPED_LOOP_RESTORATION
Rupert Swarbrick76c78002017-11-02 17:26:35 +00004686#if CONFIG_FRAME_SUPERRES && CONFIG_HORZONLY_FRAME_SUPERRES
Rupert Swarbrick8ce049e2017-11-06 16:47:47 +00004687 if (!av1_superres_unscaled(cm)) aom_extend_frame_borders(cm->frame_to_show);
Rupert Swarbrick76c78002017-11-02 17:26:35 +00004688#endif
Yaowu Xu35ee2342017-11-08 11:50:46 -08004689 if (!no_restoration)
4690 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004691#endif
4692
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004693#if CONFIG_CDEF
Yaowu Xu35ee2342017-11-08 11:50:46 -08004694 if (no_cdef) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004695 cm->cdef_bits = 0;
4696 cm->cdef_strengths[0] = 0;
4697 cm->nb_cdef_strengths = 1;
4698 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004699 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004700 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004701 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004702
4703 // Apply the filter
4704 av1_cdef_frame(cm->frame_to_show, cm, xd);
4705 }
4706#endif
4707
4708#if CONFIG_FRAME_SUPERRES
4709 superres_post_encode(cpi);
4710#endif // CONFIG_FRAME_SUPERRES
4711
4712#if CONFIG_LOOP_RESTORATION
Yaowu Xu35ee2342017-11-08 11:50:46 -08004713 if (no_restoration) {
4714 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4715 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4716 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4717 } else {
4718 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 1);
4719 av1_pick_filter_restoration(cpi->source, cpi);
4720 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4721 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4722 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4723 av1_loop_restoration_filter_frame(cm->frame_to_show, cm, cm->rst_info, 7,
4724 NULL);
4725 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004726 }
4727#endif // CONFIG_LOOP_RESTORATION
4728 // TODO(debargha): Fix mv search range on encoder side
4729 // aom_extend_frame_inner_borders(cm->frame_to_show);
4730 aom_extend_frame_borders(cm->frame_to_show);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004731}
4732
Yaowu Xuf883b422016-08-30 14:01:10 -07004733static void encode_without_recode_loop(AV1_COMP *cpi) {
4734 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004735 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004736
Yaowu Xuf883b422016-08-30 14:01:10 -07004737 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004738
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004739 set_size_independent_vars(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004740
Fergus Simpsonbc189932017-05-16 17:02:39 -07004741 setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004742
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004743 assert(cm->width == cpi->scaled_source.y_crop_width);
4744 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004745
Yaowu Xuc27fc142016-08-22 16:08:15 -07004746 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4747
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004748 cpi->source =
4749 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4750 if (cpi->unscaled_last_source != NULL)
4751 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4752 &cpi->scaled_last_source);
Sebastien Alaiwan48795802017-10-30 12:07:13 +01004753#if CONFIG_HIGHBITDEPTH
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004754 cpi->source->buf_8bit_valid = 0;
4755#endif
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004756
4757 if (frame_is_intra_only(cm) == 0) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004758 scale_references(cpi);
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004759 }
4760
Yaowu Xuf883b422016-08-30 14:01:10 -07004761 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004762 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004763 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004764
Yaowu Xuc27fc142016-08-22 16:08:15 -07004765 // Variance adaptive and in frame q adjustment experiments are mutually
4766 // exclusive.
4767 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004768 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004769 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004770 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004771 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004772 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004773 }
4774 apply_active_map(cpi);
4775
4776 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004777 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004778
4779 // Update some stats from cyclic refresh, and check if we should not update
4780 // golden reference, for 1 pass CBR.
4781 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004782 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4783 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004784
4785 // Update the skip mb flag probabilities based on the distribution
4786 // seen in the last encoder iteration.
4787 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004788 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004789}
4790
Yaowu Xuf883b422016-08-30 14:01:10 -07004791static void encode_with_recode_loop(AV1_COMP *cpi, size_t *size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004792 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004793 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004794 RATE_CONTROL *const rc = &cpi->rc;
4795 int bottom_index, top_index;
4796 int loop_count = 0;
4797 int loop_at_this_size = 0;
4798 int loop = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004799#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004800 int overshoot_seen = 0;
4801 int undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004802#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004803 int frame_over_shoot_limit;
4804 int frame_under_shoot_limit;
4805 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004806
4807 set_size_independent_vars(cpi);
4808
Sebastien Alaiwan48795802017-10-30 12:07:13 +01004809#if CONFIG_HIGHBITDEPTH
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004810 cpi->source->buf_8bit_valid = 0;
4811#endif
4812
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004813 aom_clear_system_state();
4814 setup_frame_size(cpi);
4815 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4816
Yaowu Xuc27fc142016-08-22 16:08:15 -07004817 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004818 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004820 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004821 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4822 set_mv_search_params(cpi);
4823
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004824#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004825 // Reset the loop state for new frame size.
4826 overshoot_seen = 0;
4827 undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004828#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004829
Yaowu Xuc27fc142016-08-22 16:08:15 -07004830 q_low = bottom_index;
4831 q_high = top_index;
4832
4833 loop_at_this_size = 0;
4834 }
4835
4836 // Decide frame size bounds first time through.
4837 if (loop_count == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004838 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4839 &frame_under_shoot_limit,
4840 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004841 }
4842
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004843 // if frame was scaled calculate global_motion_search again if already done
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004844 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4845 if (cpi->source->y_crop_width != cm->width ||
4846 cpi->source->y_crop_height != cm->height)
4847 cpi->global_motion_search_done = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004848 cpi->source =
4849 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004850 if (cpi->unscaled_last_source != NULL)
4851 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4852 &cpi->scaled_last_source);
4853
Yaowu Xuc27fc142016-08-22 16:08:15 -07004854 if (frame_is_intra_only(cm) == 0) {
4855 if (loop_count > 0) {
4856 release_scaled_references(cpi);
4857 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004858 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004859 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004860 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004861
4862 if (loop_count == 0) setup_frame(cpi);
4863
hui su0d103572017-03-01 17:58:01 -08004864#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004865 // Base q-index may have changed, so we need to assign proper default coef
4866 // probs before every iteration.
4867 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
4868 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07004869 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004870 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4871 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4872 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4873 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
Thomas Daededa4d8b92017-06-05 15:44:14 -07004874#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
4875 if (cm->frame_refs[0].idx >= 0) {
4876 cm->frame_contexts[cm->frame_refs[0].idx] = *cm->fc;
4877 }
4878#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004879 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07004880#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004881 }
4882 }
hui su0d103572017-03-01 17:58:01 -08004883#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004884
Yaowu Xuc27fc142016-08-22 16:08:15 -07004885 // Variance adaptive and in frame q adjustment experiments are mutually
4886 // exclusive.
4887 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004888 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004889 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004890 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004891 }
4892
4893 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004894 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004895 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004896
4897 // Update the skip mb flag probabilities based on the distribution
4898 // seen in the last encoder iteration.
4899 // update_base_skip_probs(cpi);
4900
Yaowu Xuf883b422016-08-30 14:01:10 -07004901 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004902
4903 // Dummy pack of the bitstream using up to date stats to get an
4904 // accurate estimate of output frame size to determine if we need
4905 // to recode.
4906 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004907 restore_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004908 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004909
4910 rc->projected_frame_size = (int)(*size) << 3;
4911 restore_coding_context(cpi);
4912
4913 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4914 }
4915
Yaowu Xuf883b422016-08-30 14:01:10 -07004916 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004917 loop = 0;
4918 } else {
4919 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4920 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4921 int last_q = q;
4922 int64_t kf_err;
4923
4924 int64_t high_err_target = cpi->ambient_err;
4925 int64_t low_err_target = cpi->ambient_err >> 1;
4926
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004927#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004928 if (cm->use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004929 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004930 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004931 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004932 }
4933#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07004934 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004935#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004936
4937 // Prevent possible divide by zero error below for perfect KF
4938 kf_err += !kf_err;
4939
4940 // The key frame is not good enough or we can afford
4941 // to make it better without undue risk of popping.
4942 if ((kf_err > high_err_target &&
4943 rc->projected_frame_size <= frame_over_shoot_limit) ||
4944 (kf_err > low_err_target &&
4945 rc->projected_frame_size <= frame_under_shoot_limit)) {
4946 // Lower q_high
4947 q_high = q > q_low ? q - 1 : q_low;
4948
4949 // Adjust Q
4950 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004951 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004952 } else if (kf_err < low_err_target &&
4953 rc->projected_frame_size >= frame_under_shoot_limit) {
4954 // The key frame is much better than the previous frame
4955 // Raise q_low
4956 q_low = q < q_high ? q + 1 : q_high;
4957
4958 // Adjust Q
4959 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004960 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004961 }
4962
4963 // Clamp Q to upper and lower limits:
4964 q = clamp(q, q_low, q_high);
4965
4966 loop = q != last_q;
4967 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4968 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004969 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004970 // Is the projected frame size out of range and are we allowed
4971 // to attempt to recode.
4972 int last_q = q;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004973#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004974 int retries = 0;
4975
Yaowu Xuc27fc142016-08-22 16:08:15 -07004976 // Frame size out of permitted range:
4977 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004978 // Frame is too large
4979 if (rc->projected_frame_size > rc->this_frame_target) {
4980 // Special case if the projected size is > the max allowed.
4981 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4982 q_high = rc->worst_quality;
4983
4984 // Raise Qlow as to at least the current value
4985 q_low = q < q_high ? q + 1 : q_high;
4986
4987 if (undershoot_seen || loop_at_this_size > 1) {
4988 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004989 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004990
4991 q = (q_high + q_low + 1) / 2;
4992 } else {
4993 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004994 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004995
Yaowu Xuf883b422016-08-30 14:01:10 -07004996 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004997 AOMMAX(q_high, top_index), cm->width,
4998 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004999
5000 while (q < q_low && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005001 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005002 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005003 AOMMAX(q_high, top_index), cm->width,
5004 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005005 retries++;
5006 }
5007 }
5008
5009 overshoot_seen = 1;
5010 } else {
5011 // Frame is too small
5012 q_high = q > q_low ? q - 1 : q_low;
5013
5014 if (overshoot_seen || loop_at_this_size > 1) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005015 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005016 q = (q_high + q_low) / 2;
5017 } else {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005018 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005019 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005020 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005021 // Special case reset for qlow for constrained quality.
5022 // This should only trigger where there is very substantial
5023 // undershoot on a frame and the auto cq level is above
5024 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07005025 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005026 q_low = q;
5027 }
5028
5029 while (q > q_high && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005030 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005031 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005032 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005033 retries++;
5034 }
5035 }
5036
5037 undershoot_seen = 1;
5038 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005039#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005040
5041 // Clamp Q to upper and lower limits:
5042 q = clamp(q, q_low, q_high);
5043
5044 loop = (q != last_q);
5045 } else {
5046 loop = 0;
5047 }
5048 }
5049
5050 // Special case for overlay frame.
5051 if (rc->is_src_frame_alt_ref &&
5052 rc->projected_frame_size < rc->max_frame_bandwidth)
5053 loop = 0;
5054
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005055 if (recode_loop_test_global_motion(cpi)) {
5056 loop = 1;
5057 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005058
Yaowu Xuc27fc142016-08-22 16:08:15 -07005059 if (loop) {
5060 ++loop_count;
5061 ++loop_at_this_size;
5062
5063#if CONFIG_INTERNAL_STATS
5064 ++cpi->tot_recode_hits;
5065#endif
5066 }
5067 } while (loop);
5068}
5069
Yaowu Xuf883b422016-08-30 14:01:10 -07005070static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005071 const int *const map = cpi->common.ref_frame_map;
5072
Zoe Liu368bf162017-11-03 20:10:19 -07005073 // No.1 Priority: LAST_FRAME
Yaowu Xuc27fc142016-08-22 16:08:15 -07005074 const int last2_is_last =
5075 map[cpi->lst_fb_idxes[1]] == map[cpi->lst_fb_idxes[0]];
5076 const int last3_is_last =
5077 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[0]];
5078 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[0]];
5079 const int bwd_is_last = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[0]];
Zoe Liu368bf162017-11-03 20:10:19 -07005080 const int alt2_is_last = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005081 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
5082
Zoe Liu368bf162017-11-03 20:10:19 -07005083 // No.2 Priority: ALTREF_FRAME
Yaowu Xuc27fc142016-08-22 16:08:15 -07005084 const int last2_is_alt = map[cpi->lst_fb_idxes[1]] == map[cpi->alt_fb_idx];
5085 const int last3_is_alt = map[cpi->lst_fb_idxes[2]] == map[cpi->alt_fb_idx];
5086 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
5087 const int bwd_is_alt = map[cpi->bwd_fb_idx] == map[cpi->alt_fb_idx];
Zoe Liue9b15e22017-07-19 15:53:01 -07005088 const int alt2_is_alt = map[cpi->alt2_fb_idx] == map[cpi->alt_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005089
Zoe Liu368bf162017-11-03 20:10:19 -07005090 // No.3 Priority: LAST2_FRAME
5091 const int last3_is_last2 =
5092 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
5093 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
5094 const int bwd_is_last2 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[1]];
5095 const int alt2_is_last2 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005096
Zoe Liu368bf162017-11-03 20:10:19 -07005097 // No.4 Priority: LAST3_FRAME
5098 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
5099 const int bwd_is_last3 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[2]];
5100 const int alt2_is_last3 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[2]];
5101
5102 // No.5 Priority: GOLDEN_FRAME
5103 const int bwd_is_gld = map[cpi->bwd_fb_idx] == map[cpi->gld_fb_idx];
5104 const int alt2_is_gld = map[cpi->alt2_fb_idx] == map[cpi->gld_fb_idx];
5105
5106 // No.6 Priority: BWDREF_FRAME
5107 const int alt2_is_bwd = map[cpi->alt2_fb_idx] == map[cpi->bwd_fb_idx];
5108
5109 // No.7 Priority: ALTREF2_FRAME
5110
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005111 // After av1_apply_encoding_flags() is called, cpi->ref_frame_flags might be
5112 // adjusted according to external encoder flags.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005113 int flags = cpi->ext_ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005114
Yaowu Xuf883b422016-08-30 14:01:10 -07005115 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005116
Yaowu Xuf883b422016-08-30 14:01:10 -07005117 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005118
Yaowu Xuf883b422016-08-30 14:01:10 -07005119 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005120
Zoe Liu368bf162017-11-03 20:10:19 -07005121 if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005122
Zoe Liu368bf162017-11-03 20:10:19 -07005123 if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
5124 flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005125
Zoe Liu368bf162017-11-03 20:10:19 -07005126 if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 ||
5127 bwd_is_gld) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07005128 (flags & AOM_BWD_FLAG))
5129 flags &= ~AOM_BWD_FLAG;
Zoe Liue9b15e22017-07-19 15:53:01 -07005130
Zoe Liu368bf162017-11-03 20:10:19 -07005131 if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
5132 alt2_is_gld || alt2_is_bwd) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07005133 (flags & AOM_ALT2_FLAG))
5134 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005135
5136 return flags;
5137}
5138
Yaowu Xuf883b422016-08-30 14:01:10 -07005139static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005140 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07005141 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07005142 // Note: The overrides are valid only for the next frame passed
5143 // to encode_frame_to_data_rate() function
5144 if (cpi->ext_refresh_frame_context_pending) {
5145 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
5146 cpi->ext_refresh_frame_context_pending = 0;
5147 }
5148 if (cpi->ext_refresh_frame_flags_pending) {
5149 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
5150 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
5151 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005152 cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
5153 cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005154 cpi->ext_refresh_frame_flags_pending = 0;
5155 }
5156}
5157
Zoe Liu17af2742017-10-06 10:36:42 -07005158#if !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuf883b422016-08-30 14:01:10 -07005159static void set_arf_sign_bias(AV1_COMP *cpi) {
5160 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005161 int arf_sign_bias;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005162 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liu3ac20932017-08-30 16:35:55 -07005163 // The arf_sign_bias will be one for internal ARFs'
Zoe Liue9b15e22017-07-19 15:53:01 -07005164 arf_sign_bias = cpi->rc.source_alt_ref_active &&
5165 (!cpi->refresh_alt_ref_frame ||
5166 gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE);
Zoe Liu6cfaff92016-10-18 17:12:11 -07005167
Yaowu Xuc27fc142016-08-22 16:08:15 -07005168 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005169 cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
Zoe Liue9b15e22017-07-19 15:53:01 -07005170 cm->ref_frame_sign_bias[ALTREF2_FRAME] =
5171 cm->ref_frame_sign_bias[ALTREF_FRAME];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005172}
Zoe Liu17af2742017-10-06 10:36:42 -07005173#endif // !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005174
Yaowu Xuf883b422016-08-30 14:01:10 -07005175static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07005176 InterpFilter ifilter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005177 int ref_total[TOTAL_REFS_PER_FRAME] = { 0 };
5178 MV_REFERENCE_FRAME ref;
5179 int mask = 0;
5180 int arf_idx = ALTREF_FRAME;
5181
Zoe Liue9b15e22017-07-19 15:53:01 -07005182 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
5183 cpi->refresh_alt2_ref_frame)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005184 return mask;
5185
Yaowu Xuc27fc142016-08-22 16:08:15 -07005186 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
5187 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
5188 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005189
5190 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
5191 if ((ref_total[LAST_FRAME] &&
5192 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005193 (ref_total[LAST2_FRAME] == 0 ||
5194 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
5195 ref_total[LAST2_FRAME]) &&
5196 (ref_total[LAST3_FRAME] == 0 ||
5197 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
5198 ref_total[LAST3_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005199 (ref_total[GOLDEN_FRAME] == 0 ||
5200 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
5201 ref_total[GOLDEN_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005202 (ref_total[BWDREF_FRAME] == 0 ||
5203 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
5204 ref_total[BWDREF_FRAME]) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07005205 (ref_total[ALTREF2_FRAME] == 0 ||
5206 cpi->interp_filter_selected[ALTREF2_FRAME][ifilter] * 50 <
5207 ref_total[ALTREF2_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005208 (ref_total[ALTREF_FRAME] == 0 ||
5209 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
5210 ref_total[ALTREF_FRAME]))
5211 mask |= 1 << ifilter;
5212 }
5213 return mask;
5214}
5215
5216#define DUMP_RECON_FRAMES 0
5217
5218#if DUMP_RECON_FRAMES == 1
5219// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005220static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5221 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005222 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005223
Zoe Liub4f31032017-11-03 23:48:35 -07005224 if (recon_buf == NULL) {
5225 printf("Frame %d is not ready.\n", cm->current_video_frame);
5226 return;
5227 }
5228
5229#if CONFIG_FRAME_MARKER
5230 static const int flag_list[TOTAL_REFS_PER_FRAME] = { 0,
5231 AOM_LAST_FLAG,
5232 AOM_LAST2_FLAG,
5233 AOM_LAST3_FLAG,
5234 AOM_GOLD_FLAG,
5235 AOM_BWD_FLAG,
5236 AOM_ALT2_FLAG,
5237 AOM_ALT_FLAG };
5238 printf(
5239 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5240 "show_existing_frame=%d) "
5241 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
5242 cm->current_video_frame, cm->frame_offset, cm->show_frame,
5243 cm->show_existing_frame);
5244 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
5245 const int buf_idx = cm->frame_refs[ref_frame - LAST_FRAME].idx;
5246 const int ref_offset =
5247 (buf_idx >= 0)
5248 ? (int)cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset
5249 : -1;
Zoe Liuf452fdf2017-11-02 23:08:12 -07005250 printf(
5251 " %d(%c-%d-%4.2f)", ref_offset,
5252 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N',
5253 (buf_idx >= 0) ? (int)cpi->frame_rf_level[buf_idx] : -1,
5254 (buf_idx >= 0) ? rate_factor_deltas[cpi->frame_rf_level[buf_idx]] : -1);
Zoe Liub4f31032017-11-03 23:48:35 -07005255 }
5256 printf(" ]\n");
5257#endif // CONFIG_FRAME_MARKER
5258
5259 if (!cm->show_frame) {
5260 printf("Frame %d is a no show frame, so no image dump.\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07005261 cm->current_video_frame);
5262 return;
5263 }
5264
Zoe Liub4f31032017-11-03 23:48:35 -07005265 int h;
5266 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5267 FILE *f_recon = NULL;
5268
Yaowu Xuc27fc142016-08-22 16:08:15 -07005269 if (cm->current_video_frame == 0) {
5270 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5271 printf("Unable to open file %s to write.\n", file_name);
5272 return;
5273 }
5274 } else {
5275 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5276 printf("Unable to open file %s to append.\n", file_name);
5277 return;
5278 }
5279 }
5280 printf(
5281 "\nFrame=%5d, encode_update_type[%5d]=%1d, show_existing_frame=%d, "
Zoe Liufcf5fa22017-06-26 16:00:38 -07005282 "source_alt_ref_active=%d, refresh_alt_ref_frame=%d, rf_level=%d, "
5283 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07005284 cm->current_video_frame, cpi->twopass.gf_group.index,
5285 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liufcf5fa22017-06-26 16:00:38 -07005286 cm->show_existing_frame, cpi->rc.source_alt_ref_active,
5287 cpi->refresh_alt_ref_frame,
5288 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
5289 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07005290#if 0
5291 int ref_frame;
5292 printf("get_ref_frame_map_idx: [");
5293 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
5294 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
5295 printf(" ]\n");
5296 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
5297 printf("cm->ref_frame_map = [");
5298 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
5299 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
5300 }
5301 printf(" ]\n");
5302#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005303
5304 // --- Y ---
5305 for (h = 0; h < cm->height; ++h) {
5306 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5307 f_recon);
5308 }
5309 // --- U ---
5310 for (h = 0; h < (cm->height >> 1); ++h) {
5311 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5312 f_recon);
5313 }
5314 // --- V ---
5315 for (h = 0; h < (cm->height >> 1); ++h) {
5316 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5317 f_recon);
5318 }
5319
5320 fclose(f_recon);
5321}
5322#endif // DUMP_RECON_FRAMES
5323
Thomas Davies4822e142017-10-10 11:30:36 +01005324static void make_update_tile_list_enc(AV1_COMP *cpi, const int start_tile,
5325 const int num_tiles,
Thomas Davies028b57f2017-02-22 16:42:11 +00005326 FRAME_CONTEXT *ec_ctxs[]) {
5327 int i;
Thomas Davies4822e142017-10-10 11:30:36 +01005328 for (i = start_tile; i < start_tile + num_tiles; ++i)
5329 ec_ctxs[i - start_tile] = &cpi->tile_data[i].tctx;
Thomas Davies028b57f2017-02-22 16:42:11 +00005330}
5331
Yaowu Xuf883b422016-08-30 14:01:10 -07005332static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005333 uint8_t *dest, int skip_adapt,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005334 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005335 AV1_COMMON *const cm = &cpi->common;
5336 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005337 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005338#if CONFIG_SIMPLE_BWD_ADAPT
5339 const int num_bwd_ctxs = 1;
5340#else
5341 const int num_bwd_ctxs = cm->tile_rows * cm->tile_cols;
5342#endif
5343
5344 FRAME_CONTEXT **tile_ctxs =
5345 aom_malloc(num_bwd_ctxs * sizeof(&cpi->tile_data[0].tctx));
5346 aom_cdf_prob **cdf_ptrs = aom_malloc(
5347 num_bwd_ctxs * sizeof(&cpi->tile_data[0].tctx.partition_cdf[0][0]));
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005348#if CONFIG_XIPHRC
5349 int frame_type;
5350 int drop_this_frame = 0;
Zoe Liue04abf72017-04-19 15:37:11 -07005351#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005352 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07005353 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005354
Zoe Liu17af2742017-10-06 10:36:42 -07005355#if !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005356 // Set the arf sign bias for this frame.
5357 set_arf_sign_bias(cpi);
Zoe Liu17af2742017-10-06 10:36:42 -07005358#endif // !CONFIG_FRAME_SIGN_BIAS
5359
Fangwen Fu8d164de2016-12-14 13:40:54 -08005360#if CONFIG_TEMPMV_SIGNALING
5361 // frame type has been decided outside of this function call
5362 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
Jingning Hane17ebe92017-11-03 15:25:42 -07005363 cm->use_ref_frame_mvs =
Fangwen Fu8d164de2016-12-14 13:40:54 -08005364 !cpi->oxcf.disable_tempmv && !cm->cur_frame->intra_only;
Jingning Hane17ebe92017-11-03 15:25:42 -07005365 cm->use_prev_frame_mvs = cm->use_ref_frame_mvs;
Fangwen Fu8d164de2016-12-14 13:40:54 -08005366#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005367
Jingning Hand8a15a62017-10-30 10:53:42 -07005368 // Reset the frame packet stamp index.
5369 if (cm->frame_type == KEY_FRAME) cm->current_video_frame = 0;
5370
Yaowu Xuc27fc142016-08-22 16:08:15 -07005371 // NOTE:
5372 // (1) Move the setup of the ref_frame_flags upfront as it would be
5373 // determined by the current frame properties;
5374 // (2) The setup of the ref_frame_flags applies to both show_existing_frame's
5375 // and the other cases.
5376 if (cm->current_video_frame > 0)
5377 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5378
5379 if (cm->show_existing_frame) {
5380 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
5381 // BWDREF_FRAME in the reference frame buffer.
5382 cm->frame_type = INTER_FRAME;
5383 cm->show_frame = 1;
5384 cpi->frame_flags = *frame_flags;
5385
5386 // In the case of show_existing frame, we will not send fresh flag
5387 // to decoder. Any change in the reference frame buffer can be done by
5388 // switching the virtual indices.
5389
5390 cpi->refresh_last_frame = 0;
5391 cpi->refresh_golden_frame = 0;
5392 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005393 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005394 cpi->refresh_alt_ref_frame = 0;
5395
5396 cpi->rc.is_bwd_ref_frame = 0;
5397 cpi->rc.is_last_bipred_frame = 0;
5398 cpi->rc.is_bipred_frame = 0;
5399
Jingning Han8f661602017-08-19 08:16:50 -07005400 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07005401
Yaowu Xuc27fc142016-08-22 16:08:15 -07005402 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07005403 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005404
5405 // Set up frame to show to get ready for stats collection.
5406 cm->frame_to_show = get_frame_new_buffer(cm);
5407
Zoe Liub4f31032017-11-03 23:48:35 -07005408#if CONFIG_FRAME_MARKER
5409 // Update current frame offset.
5410 cm->frame_offset =
5411 cm->buffer_pool->frame_bufs[cm->new_fb_idx].cur_frame_offset;
5412#endif // CONFIG_FRAME_MARKER
5413
Yaowu Xuc27fc142016-08-22 16:08:15 -07005414#if DUMP_RECON_FRAMES == 1
5415 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5416 dump_filtered_recon_frames(cpi);
5417#endif // DUMP_RECON_FRAMES
5418
5419 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07005420 // NOTE:
5421 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
5422 // update has been done previously when handling the LAST_BIPRED_FRAME
5423 // right before BWDREF_FRAME (in the display order);
5424 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
5425 // update will be done when the following is called, which will exchange
5426 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
5427 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2, and
5428 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Cheng Chen46f30c72017-09-07 11:13:33 -07005429 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005430
5431 // Update frame flags
5432 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5433 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
5434 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5435
5436 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5437
5438 // Update the frame type
5439 cm->last_frame_type = cm->frame_type;
5440
Yaowu Xuc27fc142016-08-22 16:08:15 -07005441 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5442 // to do post-encoding update accordingly.
5443 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005444 av1_set_target_rate(cpi, cm->width, cm->height);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005445#if CONFIG_XIPHRC
Zoe Liue04abf72017-04-19 15:37:11 -07005446 frame_type = cm->frame_type == INTER_FRAME ? OD_P_FRAME : OD_I_FRAME;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005447 drop_this_frame = od_enc_rc_update_state(
5448 &cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5449 cpi->refresh_alt_ref_frame, frame_type, cpi->droppable);
5450#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005451 av1_rc_postencode_update(cpi, *size);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005452#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005453 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005454
Yaowu Xuc27fc142016-08-22 16:08:15 -07005455 ++cm->current_video_frame;
5456
Jingning Hanf6214b92017-04-12 11:43:37 -07005457 aom_free(tile_ctxs);
5458 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005459 return;
5460 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005461
5462 // Set default state for segment based loop filter update flags.
5463 cm->lf.mode_ref_delta_update = 0;
5464
5465 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5466 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
5467
5468 // Set various flags etc to special state if it is a key frame.
5469 if (frame_is_intra_only(cm)) {
5470 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005471 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005472
5473 // If segmentation is enabled force a map update for key frames.
5474 if (seg->enabled) {
5475 seg->update_map = 1;
5476 seg->update_data = 1;
5477 }
5478
5479 // The alternate reference frame cannot be active for a key frame.
5480 cpi->rc.source_alt_ref_active = 0;
5481
5482 cm->error_resilient_mode = oxcf->error_resilient_mode;
5483
Thomas Daedea6a854b2017-06-22 17:49:11 -07005484#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005485 // By default, encoder assumes decoder can use prev_mi.
5486 if (cm->error_resilient_mode) {
5487 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
5488 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
5489 } else if (cm->intra_only) {
5490 // Only reset the current context.
5491 cm->reset_frame_context = RESET_FRAME_CONTEXT_CURRENT;
5492 }
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005493#if CONFIG_EXT_TILE
5494 if (cpi->oxcf.large_scale_tile)
5495 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
5496#endif // CONFIG_EXT_TILE
5497#endif // !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005498 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005499 if (cpi->oxcf.mtu == 0) {
5500 cm->num_tg = cpi->oxcf.num_tile_groups;
5501 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005502 // Use a default value for the purposes of weighting costs in probability
5503 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005504 cm->num_tg = DEFAULT_MAX_NUM_TG;
5505 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005506
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005507#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07005508 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5509 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005510#endif // CONFIG_EXT_TILE
5511
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005512#if CONFIG_XIPHRC
5513 if (drop_this_frame) {
5514 av1_rc_postencode_update_drop_frame(cpi);
5515 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005516 aom_free(tile_ctxs);
5517 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005518 return;
5519 }
5520#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07005521 // For 1 pass CBR, check if we are dropping this frame.
5522 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005523 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005524 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005525 if (av1_rc_drop_frame(cpi)) {
5526 av1_rc_postencode_update_drop_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005527 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005528 aom_free(tile_ctxs);
5529 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005530 return;
5531 }
5532 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005533#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005534
Yaowu Xuf883b422016-08-30 14:01:10 -07005535 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005536
5537#if CONFIG_INTERNAL_STATS
5538 memset(cpi->mode_chosen_counts, 0,
5539 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5540#endif
5541
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005542#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01005543 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005544 /* Non-normative definition of current_frame_id ("frame counter" with
5545 * wraparound) */
Sebastien Alaiwand418f682017-10-19 15:06:52 +02005546 const int frame_id_length = FRAME_ID_LENGTH;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005547 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005548 int lsb, msb;
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005549/* quasi-random initialization of current_frame_id for a key frame */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005550#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07005551 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5552 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5553 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005554 } else {
5555#endif
Alex Conversef77fd0b2017-04-20 11:00:24 -07005556 lsb = cpi->source->y_buffer[0] & 0xff;
5557 msb = cpi->source->y_buffer[1] & 0xff;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005558#if CONFIG_HIGHBITDEPTH
David Barker49a76562016-12-07 14:50:21 +00005559 }
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005560#endif
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005561 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005562 } else {
5563 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005564 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
5565 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005566 }
5567 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005568#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005569
Fangwen Fu6160df22017-04-24 09:45:51 -07005570#if CONFIG_EXT_DELTA_Q
5571 cm->delta_q_present_flag = cpi->oxcf.deltaq_mode != NO_DELTA_Q;
Fangwen Fu231fe422017-04-24 17:52:29 -07005572 cm->delta_lf_present_flag = cpi->oxcf.deltaq_mode == DELTA_Q_LF;
Cheng Chen880166a2017-10-02 17:48:48 -07005573#if CONFIG_LOOPFILTER_LEVEL
5574 cm->delta_lf_multi = DEFAULT_DELTA_LF_MULTI;
5575#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu6160df22017-04-24 09:45:51 -07005576#endif
5577
Yaowu Xuc27fc142016-08-22 16:08:15 -07005578 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
5579 encode_without_recode_loop(cpi);
5580 } else {
5581 encode_with_recode_loop(cpi, size, dest);
5582 }
5583
Yi Luo10e23002017-07-31 11:54:43 -07005584 cm->last_tile_cols = cm->tile_cols;
5585 cm->last_tile_rows = cm->tile_rows;
5586
Yaowu Xuc27fc142016-08-22 16:08:15 -07005587#ifdef OUTPUT_YUV_SKINMAP
5588 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005589 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005590 }
5591#endif // OUTPUT_YUV_SKINMAP
5592
5593 // Special case code to reduce pulsing when key frames are forced at a
5594 // fixed interval. Note the reconstruction error if it is the frame before
5595 // the force key frame
5596 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005597#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005598 if (cm->use_highbitdepth) {
5599 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07005600 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005601 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005602 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005603 }
5604#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07005605 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005606#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005607 }
5608
5609 // If the encoder forced a KEY_FRAME decision
5610 if (cm->frame_type == KEY_FRAME) {
5611 cpi->refresh_last_frame = 1;
5612 }
5613
5614 cm->frame_to_show = get_frame_new_buffer(cm);
5615 cm->frame_to_show->color_space = cm->color_space;
anorkin76fb1262017-03-22 15:12:12 -07005616#if CONFIG_COLORSPACE_HEADERS
5617 cm->frame_to_show->transfer_function = cm->transfer_function;
5618 cm->frame_to_show->chroma_sample_position = cm->chroma_sample_position;
5619#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005620 cm->frame_to_show->color_range = cm->color_range;
5621 cm->frame_to_show->render_width = cm->render_width;
5622 cm->frame_to_show->render_height = cm->render_height;
5623
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005624 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5625 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005626
5627 // Pick the loop filter level for the frame.
5628 loopfilter_frame(cpi, cm);
5629
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005630#ifdef OUTPUT_YUV_REC
5631 aom_write_one_yuv_frame(cm, cm->frame_to_show);
5632#endif
5633
Yaowu Xuc27fc142016-08-22 16:08:15 -07005634 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07005635 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005636
Jingning Hanf6214b92017-04-12 11:43:37 -07005637 if (skip_adapt) {
Jingning Hanf6214b92017-04-12 11:43:37 -07005638 aom_free(tile_ctxs);
5639 aom_free(cdf_ptrs);
Jingning Hanf6214b92017-04-12 11:43:37 -07005640 return;
5641 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005642
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005643#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01005644 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005645 int i;
5646 /* Update reference frame id values based on the value of refresh_mask */
5647 for (i = 0; i < REF_FRAMES; i++) {
5648 if ((cm->refresh_mask >> i) & 1) {
5649 cm->ref_frame_id[i] = cm->current_frame_id;
5650 }
5651 }
5652 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005653#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005654
Yaowu Xuc27fc142016-08-22 16:08:15 -07005655#if DUMP_RECON_FRAMES == 1
5656 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005657 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005658#endif // DUMP_RECON_FRAMES
5659
5660 if (cm->seg.update_map) update_reference_segmentation_map(cpi);
5661
5662 if (frame_is_intra_only(cm) == 0) {
5663 release_scaled_references(cpi);
5664 }
5665
Cheng Chen46f30c72017-09-07 11:13:33 -07005666 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005667
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005668#if CONFIG_ENTROPY_STATS
5669 av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
Zoe Liua56f9162017-06-21 22:49:57 -07005670 assert(cm->frame_context_idx < FRAME_CONTEXTS);
5671 av1_accumulate_frame_counts(&aggregate_fc_per_type[cm->frame_context_idx],
5672 &cm->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005673#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005674 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005675 av1_adapt_intra_frame_probs(cm);
Thomas Davies4822e142017-10-10 11:30:36 +01005676#if CONFIG_SIMPLE_BWD_ADAPT
5677 make_update_tile_list_enc(cpi, cm->largest_tile_id, 1, tile_ctxs);
5678#else
5679 make_update_tile_list_enc(cpi, 0, num_bwd_ctxs, tile_ctxs);
5680#endif
Thomas Davies493623e2017-03-31 16:12:25 +01005681 av1_average_tile_coef_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01005682 num_bwd_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01005683 av1_average_tile_intra_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01005684 num_bwd_ctxs);
Debargha Mukherjee43061b32017-10-13 16:50:17 -07005685 av1_average_tile_loopfilter_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
5686 num_bwd_ctxs);
hui suff0da2b2017-03-07 15:51:37 -08005687#if CONFIG_ADAPT_SCAN
5688 av1_adapt_scan_order(cm);
5689#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005690 }
5691
5692 if (!frame_is_intra_only(cm)) {
5693 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005694 av1_adapt_inter_frame_probs(cm);
5695 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00005696 av1_average_tile_inter_cdfs(&cpi->common, cpi->common.fc, tile_ctxs,
Thomas Davies4822e142017-10-10 11:30:36 +01005697 cdf_ptrs, num_bwd_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01005698 av1_average_tile_mv_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies4822e142017-10-10 11:30:36 +01005699 num_bwd_ctxs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005700 }
5701 }
5702
5703 if (cpi->refresh_golden_frame == 1)
5704 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5705 else
5706 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5707
5708 if (cpi->refresh_alt_ref_frame == 1)
5709 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5710 else
5711 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5712
Yaowu Xuc27fc142016-08-22 16:08:15 -07005713 if (cpi->refresh_bwd_ref_frame == 1)
5714 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5715 else
5716 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005717
Yaowu Xuc27fc142016-08-22 16:08:15 -07005718 cm->last_frame_type = cm->frame_type;
5719
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005720#if CONFIG_XIPHRC
5721 frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
5722
5723 drop_this_frame =
5724 od_enc_rc_update_state(&cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5725 cpi->refresh_alt_ref_frame, frame_type, 0);
5726 if (drop_this_frame) {
5727 av1_rc_postencode_update_drop_frame(cpi);
5728 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005729 aom_free(tile_ctxs);
5730 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005731 return;
5732 }
Zoe Liue04abf72017-04-19 15:37:11 -07005733#else // !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005734 av1_rc_postencode_update(cpi, *size);
Zoe Liue04abf72017-04-19 15:37:11 -07005735#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005736
5737#if 0
5738 output_frame_level_debug_stats(cpi);
5739#endif
5740
5741 if (cm->frame_type == KEY_FRAME) {
5742 // Tell the caller that the frame was coded as a key frame
5743 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5744 } else {
5745 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5746 }
5747
5748 // Clear the one shot update flags for segmentation map and mode/ref loop
5749 // filter deltas.
5750 cm->seg.update_map = 0;
5751 cm->seg.update_data = 0;
5752 cm->lf.mode_ref_delta_update = 0;
5753
Yaowu Xuc27fc142016-08-22 16:08:15 -07005754 if (cm->show_frame) {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005755 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that are
5756 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005757 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005758 // Don't increment frame counters if this was an altref buffer
5759 // update not a real frame
5760 ++cm->current_video_frame;
5761 }
5762
Yaowu Xuc27fc142016-08-22 16:08:15 -07005763 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005764 if (cm->is_reference_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005765 cm->prev_frame = cm->cur_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005766 // keep track of the last coded dimensions
5767 cm->last_width = cm->width;
5768 cm->last_height = cm->height;
5769
5770 // reset to normal state now that we are done.
5771 cm->last_show_frame = cm->show_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005772 }
Yi Luo10e23002017-07-31 11:54:43 -07005773
Thomas Davies493623e2017-03-31 16:12:25 +01005774 aom_free(tile_ctxs);
5775 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005776}
5777
Yaowu Xuf883b422016-08-30 14:01:10 -07005778static void Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005779 int skip_adapt, unsigned int *frame_flags) {
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005780#if CONFIG_XIPHRC
5781 int64_t ip_count;
5782 int frame_type, is_golden, is_altref;
5783
5784 /* Not updated during init so update it here */
5785 if (cpi->oxcf.rc_mode == AOM_Q) cpi->od_rc.quality = cpi->oxcf.cq_level;
5786
5787 frame_type = od_frame_type(&cpi->od_rc, cpi->od_rc.cur_frame, &is_golden,
5788 &is_altref, &ip_count);
5789
5790 if (frame_type == OD_I_FRAME) {
5791 frame_type = KEY_FRAME;
5792 cpi->frame_flags &= FRAMEFLAGS_KEY;
5793 } else if (frame_type == OD_P_FRAME) {
5794 frame_type = INTER_FRAME;
5795 }
5796
5797 if (is_altref) {
5798 cpi->refresh_alt_ref_frame = 1;
5799 cpi->rc.source_alt_ref_active = 1;
5800 }
5801
5802 cpi->refresh_golden_frame = is_golden;
5803 cpi->common.frame_type = frame_type;
5804 if (is_golden) cpi->frame_flags &= FRAMEFLAGS_GOLDEN;
5805#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005806 if (cpi->oxcf.rc_mode == AOM_CBR) {
5807 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005808 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005809 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005810 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005811#endif
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005812 encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005813}
5814
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005815#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005816static void Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005817 unsigned int *frame_flags) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005818 encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005819
Yaowu Xuc27fc142016-08-22 16:08:15 -07005820 // Do not do post-encoding update for those frames that do not have a spot in
5821 // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
5822 // even when show_existing_frame is used.
5823 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005824 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005825 }
5826 check_show_existing_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005827}
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005828#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005829
James Zern3e2613b2017-03-30 23:14:40 -07005830int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005831 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5832 int64_t end_time) {
5833 AV1_COMMON *const cm = &cpi->common;
5834 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005835 int res = 0;
5836 const int subsampling_x = sd->subsampling_x;
5837 const int subsampling_y = sd->subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005838#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005839 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5840#endif
5841
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005842#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005843 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5844#else
5845 check_initial_width(cpi, subsampling_x, subsampling_y);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005846#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005847
Yaowu Xuf883b422016-08-30 14:01:10 -07005848 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005849
Yaowu Xuf883b422016-08-30 14:01:10 -07005850 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005851#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005852 use_highbitdepth,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005853#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005854 frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005855 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005856 aom_usec_timer_mark(&timer);
5857 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005858
5859 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
5860 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005861 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005862 "Non-4:2:0 color format requires profile 1 or 3");
5863 res = -1;
5864 }
5865 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
5866 (subsampling_x == 1 && subsampling_y == 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005867 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005868 "4:2:0 color format requires profile 0 or 2");
5869 res = -1;
5870 }
5871
5872 return res;
5873}
5874
Yaowu Xuf883b422016-08-30 14:01:10 -07005875static int frame_is_reference(const AV1_COMP *cpi) {
5876 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005877
5878 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005879 cpi->refresh_golden_frame || cpi->refresh_bwd_ref_frame ||
5880 cpi->refresh_alt2_ref_frame || cpi->refresh_alt_ref_frame ||
5881 !cm->error_resilient_mode || cm->lf.mode_ref_delta_update ||
5882 cm->seg.update_map || cm->seg.update_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005883}
5884
Yaowu Xuf883b422016-08-30 14:01:10 -07005885static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005886 const struct lookahead_entry *source) {
5887 int64_t this_duration;
5888 int step = 0;
5889
5890 if (source->ts_start == cpi->first_time_stamp_ever) {
5891 this_duration = source->ts_end - source->ts_start;
5892 step = 1;
5893 } else {
5894 int64_t last_duration =
5895 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5896
5897 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5898
5899 // do a step update if the duration changes by 10%
5900 if (last_duration)
5901 step = (int)((this_duration - last_duration) * 10 / last_duration);
5902 }
5903
5904 if (this_duration) {
5905 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005906 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005907 } else {
5908 // Average this frame's rate into the last second's average
5909 // frame rate. If we haven't seen 1 second yet, then average
5910 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005911 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005912 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5913 double avg_duration = 10000000.0 / cpi->framerate;
5914 avg_duration *= (interval - avg_duration + this_duration);
5915 avg_duration /= interval;
5916
Yaowu Xuf883b422016-08-30 14:01:10 -07005917 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005918 }
5919 }
5920 cpi->last_time_stamp_seen = source->ts_start;
5921 cpi->last_end_time_stamp_seen = source->ts_end;
5922}
5923
5924// Returns 0 if this is not an alt ref else the offset of the source frame
5925// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005926static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005927 RATE_CONTROL *const rc = &cpi->rc;
5928 int arf_src_index = 0;
5929 if (is_altref_enabled(cpi)) {
5930 if (cpi->oxcf.pass == 2) {
5931 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5932 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5933 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5934 }
5935 } else if (rc->source_alt_ref_pending) {
5936 arf_src_index = rc->frames_till_gf_update_due;
5937 }
5938 }
5939 return arf_src_index;
5940}
5941
Yaowu Xuf883b422016-08-30 14:01:10 -07005942static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005943 int brf_src_index = 0;
5944 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5945
5946 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5947 // flag.
5948 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5949 if (cpi->oxcf.pass == 2) {
5950 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5951 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5952 } else {
5953 // TODO(zoeliu): To re-visit the setup for this scenario
5954 brf_src_index = cpi->rc.bipred_group_interval - 1;
5955 }
5956 }
5957
5958 return brf_src_index;
5959}
Zoe Liue9b15e22017-07-19 15:53:01 -07005960
Zoe Liue9b15e22017-07-19 15:53:01 -07005961// Returns 0 if this is not an alt ref else the offset of the source frame
5962// used as the arf midpoint.
5963static int get_arf2_src_index(AV1_COMP *cpi) {
5964 int arf2_src_index = 0;
5965 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5966 if (cpi->oxcf.pass == 2) {
5967 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5968 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5969 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5970 }
5971 }
5972 }
5973 return arf2_src_index;
5974}
Yaowu Xuc27fc142016-08-22 16:08:15 -07005975
Yaowu Xuf883b422016-08-30 14:01:10 -07005976static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005977 const struct lookahead_entry *source) {
5978 RATE_CONTROL *const rc = &cpi->rc;
5979
5980 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005981 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005982
5983 if (cpi->oxcf.pass == 2) {
5984 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5985 rc->is_src_frame_alt_ref =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005986 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07005987 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07005988 rc->is_src_frame_ext_arf =
5989 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005990 } else {
5991 rc->is_src_frame_alt_ref =
5992 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5993 }
5994
5995 if (rc->is_src_frame_alt_ref) {
5996 // Current frame is an ARF overlay frame.
5997 cpi->alt_ref_source = NULL;
5998
Zoe Liue9b15e22017-07-19 15:53:01 -07005999 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
6000 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
6001 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
6002 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
6003 cpi->refresh_last_frame = 1;
6004 } else {
Zoe Liue9b15e22017-07-19 15:53:01 -07006005 // Don't refresh the last buffer for an ARF overlay frame. It will
6006 // become the GF so preserve last as an alternative prediction option.
6007 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006008 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006009 }
6010}
6011
6012#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006013extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6014 const unsigned char *img2, int img2_pitch,
6015 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006016
6017static void adjust_image_stat(double y, double u, double v, double all,
6018 ImageStat *s) {
6019 s->stat[Y] += y;
6020 s->stat[U] += u;
6021 s->stat[V] += v;
6022 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006023 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006024}
6025
Angie Chiang08a22a62017-07-17 17:29:17 -07006026static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006027 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006028 double samples = 0.0;
6029 uint32_t in_bit_depth = 8;
6030 uint32_t bit_depth = 8;
6031
Angie Chiang08a22a62017-07-17 17:29:17 -07006032#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07006033 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006034#endif
6035 cpi->bytes += frame_bytes;
6036
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006037#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006038 if (cm->use_highbitdepth) {
6039 in_bit_depth = cpi->oxcf.input_bit_depth;
6040 bit_depth = cm->bit_depth;
6041 }
6042#endif
6043 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006044 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006045 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
6046 double y, u, v, frame_all;
6047
6048 cpi->count++;
6049 if (cpi->b_calculate_psnr) {
6050 PSNR_STATS psnr;
6051 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006052 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006053// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006054#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07006055 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006056#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006057 aom_calc_psnr(orig, recon, &psnr);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006058#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006059
6060 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6061 &cpi->psnr);
6062 cpi->total_sq_error += psnr.sse[0];
6063 cpi->total_samples += psnr.samples[0];
6064 samples = psnr.samples[0];
6065// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006066#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006067 if (cm->use_highbitdepth)
6068 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006069 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006070 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006071 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006072#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006073 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006074#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006075
Yaowu Xuf883b422016-08-30 14:01:10 -07006076 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006077 cpi->summed_quality += frame_ssim2 * weight;
6078 cpi->summed_weights += weight;
6079
6080#if 0
6081 {
6082 FILE *f = fopen("q_used.stt", "a");
6083 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
6084 cpi->common.current_video_frame, y2, u2, v2,
6085 frame_psnr2, frame_ssim2);
6086 fclose(f);
6087 }
6088#endif
6089 }
6090 if (cpi->b_calculate_blockiness) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006091#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006092 if (!cm->use_highbitdepth)
6093#endif
6094 {
6095 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006096 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6097 recon->y_stride, orig->y_width, orig->y_height);
6098 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006099 cpi->total_blockiness += frame_blockiness;
6100 }
6101
6102 if (cpi->b_calculate_consistency) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006103#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006104 if (!cm->use_highbitdepth)
6105#endif
6106 {
Yaowu Xuf883b422016-08-30 14:01:10 -07006107 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006108 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6109 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6110
6111 const double peak = (double)((1 << in_bit_depth) - 1);
6112 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006113 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006114 if (consistency > 0.0)
6115 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006116 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006117 cpi->total_inconsistency += this_inconsistency;
6118 }
6119 }
6120 }
6121
6122 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006123 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006124 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006125 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006126 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6127 }
6128}
6129#endif // CONFIG_INTERNAL_STATS
6130
RogerZhou3b635242017-09-19 10:06:46 -07006131#if CONFIG_AMVR
6132static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
6133 const YV12_BUFFER_CONFIG *last_picture,
6134 hash_table *last_hash_table) {
6135 aom_clear_system_state();
6136 // check use hash ME
6137 int k;
6138 uint32_t hash_value_1;
6139 uint32_t hash_value_2;
6140
6141 const int block_size = 8;
6142 const double threshold_current = 0.8;
6143 const double threshold_average = 0.95;
6144 const int max_history_size = 32;
6145 int T = 0; // total block
6146 int C = 0; // match with collocated block
6147 int S = 0; // smooth region but not match with collocated block
6148 int M = 0; // match with other block
6149
6150 const int pic_width = cur_picture->y_width;
6151 const int pic_height = cur_picture->y_height;
6152 for (int i = 0; i + block_size <= pic_height; i += block_size) {
6153 for (int j = 0; j + block_size <= pic_width; j += block_size) {
6154 const int x_pos = j;
6155 const int y_pos = i;
6156 int match = 1;
6157 T++;
6158
6159 // check whether collocated block match with current
6160 uint8_t *p_cur = cur_picture->y_buffer;
6161 uint8_t *p_ref = last_picture->y_buffer;
6162 int stride_cur = cur_picture->y_stride;
6163 int stride_ref = last_picture->y_stride;
6164 p_cur += (y_pos * stride_cur + x_pos);
6165 p_ref += (y_pos * stride_ref + x_pos);
6166
6167 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6168 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6169 if (p_cur[tmpX] != p_ref[tmpX]) {
6170 match = 0;
6171 }
6172 }
6173 p_cur += stride_cur;
6174 p_ref += stride_ref;
6175 }
6176
6177 if (match) {
6178 C++;
6179 continue;
6180 }
6181
6182 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
6183 y_pos) ||
6184 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
6185 S++;
6186 continue;
6187 }
6188
6189 av1_get_block_hash_value(
6190 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
6191 block_size, &hash_value_1, &hash_value_2);
6192
6193 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
6194 M++;
6195 }
6196 }
6197 }
6198
6199 assert(T > 0);
6200 double csm_rate = ((double)(C + S + M)) / ((double)(T));
6201 double m_rate = ((double)(M)) / ((double)(T));
6202
6203 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
6204 cpi->m_rate_array[cpi->rate_index] = m_rate;
6205
6206 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
6207 cpi->rate_size++;
6208 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
6209
6210 if (csm_rate < threshold_current) {
6211 return 0;
6212 }
6213
6214 if (C == T) {
6215 return 1;
6216 }
6217
6218 double csm_average = 0.0;
6219 double m_average = 0.0;
6220
6221 for (k = 0; k < cpi->rate_size; k++) {
6222 csm_average += cpi->csm_rate_array[k];
6223 m_average += cpi->m_rate_array[k];
6224 }
6225 csm_average /= cpi->rate_size;
6226 m_average /= cpi->rate_size;
6227
6228 if (csm_average < threshold_average) {
6229 return 0;
6230 }
6231
6232 if (M > (T - C - S) / 3) {
6233 return 1;
6234 }
6235
6236 if (csm_rate > 0.99 && m_rate > 0.01) {
6237 return 1;
6238 }
6239
6240 if (csm_average + m_average > 1.01) {
6241 return 1;
6242 }
6243
6244 return 0;
6245}
6246#endif
6247
Yaowu Xuf883b422016-08-30 14:01:10 -07006248int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6249 size_t *size, uint8_t *dest, int64_t *time_stamp,
6250 int64_t *time_end, int flush) {
6251 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6252 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006253 BufferPool *const pool = cm->buffer_pool;
6254 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07006255 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006256 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
6257 struct lookahead_entry *last_source = NULL;
6258 struct lookahead_entry *source = NULL;
6259 int arf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006260 int brf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006261 int i;
6262
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006263#if CONFIG_XIPHRC
6264 cpi->od_rc.end_of_input = flush;
6265#endif
6266
Yaowu Xuc27fc142016-08-22 16:08:15 -07006267#if CONFIG_BITSTREAM_DEBUG
6268 assert(cpi->oxcf.max_threads == 0 &&
6269 "bitstream debug tool does not support multithreading");
6270 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07006271 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006272#endif
6273
Yaowu Xuf883b422016-08-30 14:01:10 -07006274 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006275
RogerZhou3b635242017-09-19 10:06:46 -07006276#if CONFIG_AMVR
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07006277#if CONFIG_EIGHTH_PEL_MV_ONLY
6278 set_high_precision_mv(cpi, 1, 0);
6279#else
RogerZhou3b635242017-09-19 10:06:46 -07006280 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07006281#endif // CONFIG_EIGHTH_PEL_MV_ONLY
6282#else
6283#if CONFIG_EIGHTH_PEL_MV_ONLY
6284 set_high_precision_mv(cpi, 1);
RogerZhou3b635242017-09-19 10:06:46 -07006285#else
Cheng Chen46f30c72017-09-07 11:13:33 -07006286 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07006287#endif // CONFIG_EIGHTH_PEL_MV_ONLY
RogerZhou3b635242017-09-19 10:06:46 -07006288#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006289
6290 // Is multi-arf enabled.
6291 // Note that at the moment multi_arf is only configured for 2 pass VBR
6292 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
6293 cpi->multi_arf_allowed = 1;
6294 else
6295 cpi->multi_arf_allowed = 0;
6296
Thomas Daedea6a854b2017-06-22 17:49:11 -07006297// Normal defaults
6298#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07006299 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07006300#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006301 cm->refresh_frame_context =
6302 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
6303 ? REFRESH_FRAME_CONTEXT_FORWARD
6304 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006305#if CONFIG_EXT_TILE
6306 if (oxcf->large_scale_tile)
6307 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
6308#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006309
6310 cpi->refresh_last_frame = 1;
6311 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006312 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006313 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006314 cpi->refresh_alt_ref_frame = 0;
6315
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02006316#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07006317 if (oxcf->pass == 2 && cm->show_existing_frame) {
6318 // Manage the source buffer and flush out the source frame that has been
6319 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07006320 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006321 *size = 0;
6322 return -1;
6323 }
Alex Conversef77fd0b2017-04-20 11:00:24 -07006324 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006325 // TODO(zoeliu): To track down to determine whether it's needed to adjust
6326 // the frame rate.
6327 *time_stamp = source->ts_start;
6328 *time_end = source->ts_end;
6329
6330 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006331 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006332
6333 // Find a free buffer for the new frame, releasing the reference previously
6334 // held.
6335 if (cm->new_fb_idx != INVALID_IDX) {
6336 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6337 }
6338 cm->new_fb_idx = get_free_fb(cm);
6339
6340 if (cm->new_fb_idx == INVALID_IDX) return -1;
6341
6342 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006343 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006344
6345 // Start with a 0 size frame.
6346 *size = 0;
6347
6348 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006349 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006350
6351 Pass2Encode(cpi, size, dest, frame_flags);
6352
6353 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
6354
6355#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07006356 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006357#endif // CONFIG_INTERNAL_STATS
6358
6359 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006360 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006361
6362 cm->show_existing_frame = 0;
6363 return 0;
6364 }
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02006365#endif // !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07006366
6367 // Should we encode an arf frame.
6368 arf_src_index = get_arf_src_index(cpi);
6369 if (arf_src_index) {
6370 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006371 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006372 // Avoid creating an alt-ref if there's a forced keyframe pending.
6373 if (e == NULL) {
6374 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07006375 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006376 arf_src_index = 0;
6377 flush = 1;
6378 break;
6379 }
6380 }
6381 }
6382
6383 if (arf_src_index) {
6384 assert(arf_src_index <= rc->frames_to_key);
6385
Yaowu Xuf883b422016-08-30 14:01:10 -07006386 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006387 cpi->alt_ref_source = source;
6388
6389 if (oxcf->arnr_max_frames > 0) {
Todd Nguyen302d0972017-06-16 16:16:29 -07006390// Produce the filtered ARF frame.
6391#if CONFIG_BGSPRITE
6392 int bgsprite_ret = av1_background_sprite(cpi, arf_src_index);
Todd Nguyen2fc23092017-07-11 12:00:36 -07006393 // Do temporal filter if bgsprite not generated.
6394 if (bgsprite_ret != 0)
Todd Nguyen302d0972017-06-16 16:16:29 -07006395#endif // CONFIG_BGSPRITE
Todd Nguyen8493f912017-07-20 12:45:12 -07006396 av1_temporal_filter(cpi,
6397#if CONFIG_BGSPRITE
Todd Nguyen1fd99c22017-07-31 17:50:41 -07006398 NULL, &cpi->alt_ref_buffer,
Todd Nguyen8493f912017-07-20 12:45:12 -07006399#endif // CONFIG_BGSPRITE
6400 arf_src_index);
Yaowu Xuf883b422016-08-30 14:01:10 -07006401 aom_extend_frame_borders(&cpi->alt_ref_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006402 force_src_buffer = &cpi->alt_ref_buffer;
6403 }
6404
6405 cm->show_frame = 0;
6406 cm->intra_only = 0;
6407 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006408 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006409 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006410 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006411 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006412 rc->is_src_frame_alt_ref = 0;
6413 }
6414 rc->source_alt_ref_pending = 0;
6415 }
6416
Zoe Liue9b15e22017-07-19 15:53:01 -07006417 // Should we encode an arf2 frame.
6418 arf_src_index = get_arf2_src_index(cpi);
6419 if (arf_src_index) {
6420 for (i = 0; i <= arf_src_index; ++i) {
6421 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
6422 // Avoid creating an alt-ref if there's a forced keyframe pending.
6423 if (e == NULL) {
6424 break;
6425 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
6426 arf_src_index = 0;
6427 flush = 1;
6428 break;
6429 }
6430 }
6431 }
6432
6433 if (arf_src_index) {
6434 assert(arf_src_index <= rc->frames_to_key);
6435
6436 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
6437 cpi->alt_ref_source = source;
6438
6439 if (oxcf->arnr_max_frames > 0) {
6440 // Produce the filtered ARF frame.
Zoe Liu3ac20932017-08-30 16:35:55 -07006441 av1_temporal_filter(cpi,
6442#if CONFIG_BGSPRITE
6443 NULL, NULL,
6444#endif // CONFIG_BGSPRITE
6445 arf_src_index);
Zoe Liue9b15e22017-07-19 15:53:01 -07006446 aom_extend_frame_borders(&cpi->alt_ref_buffer);
6447 force_src_buffer = &cpi->alt_ref_buffer;
6448 }
6449
6450 cm->show_frame = 0;
6451 cm->intra_only = 0;
6452 cpi->refresh_alt2_ref_frame = 1;
6453 cpi->refresh_last_frame = 0;
6454 cpi->refresh_golden_frame = 0;
6455 cpi->refresh_bwd_ref_frame = 0;
6456 cpi->refresh_alt_ref_frame = 0;
6457 rc->is_src_frame_alt_ref = 0;
6458 rc->is_src_frame_ext_arf = 0;
6459 }
6460 rc->source_alt_ref_pending = 0;
6461 }
Zoe Liue9b15e22017-07-19 15:53:01 -07006462
Yaowu Xuc27fc142016-08-22 16:08:15 -07006463 rc->is_bwd_ref_frame = 0;
6464 brf_src_index = get_brf_src_index(cpi);
6465 if (brf_src_index) {
6466 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07006467 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006468 cm->show_frame = 0;
6469 cm->intra_only = 0;
6470
6471 cpi->refresh_bwd_ref_frame = 1;
6472 cpi->refresh_last_frame = 0;
6473 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006474 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006475 cpi->refresh_alt_ref_frame = 0;
6476
6477 rc->is_bwd_ref_frame = 1;
6478 }
6479 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006480
6481 if (!source) {
6482 // Get last frame source.
6483 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006484 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006485 return -1;
6486 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006487 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006488 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07006489 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006490
6491 if (source != NULL) {
6492 cm->show_frame = 1;
6493 cm->intra_only = 0;
6494
6495 // Check to see if the frame should be encoded as an arf overlay.
6496 check_src_altref(cpi, source);
6497 }
6498 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006499 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006500 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07006501 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006502 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
6503
6504 *time_stamp = source->ts_start;
6505 *time_end = source->ts_end;
Yaowu Xuf883b422016-08-30 14:01:10 -07006506 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006507
6508 } else {
6509 *size = 0;
6510 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006511#if CONFIG_XIPHRC
6512 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 1);
6513#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006514 av1_end_first_pass(cpi); /* get last stats packet */
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006515#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006516 cpi->twopass.first_pass_done = 1;
6517 }
6518 return -1;
6519 }
6520
6521 if (source->ts_start < cpi->first_time_stamp_ever) {
6522 cpi->first_time_stamp_ever = source->ts_start;
6523 cpi->last_end_time_stamp_seen = source->ts_start;
6524 }
6525
6526 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006527 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006528
6529 // adjust frame rates based on timestamps given
6530 if (cm->show_frame) adjust_frame_rate(cpi, source);
6531
6532 // Find a free buffer for the new frame, releasing the reference previously
6533 // held.
6534 if (cm->new_fb_idx != INVALID_IDX) {
6535 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6536 }
6537 cm->new_fb_idx = get_free_fb(cm);
6538
6539 if (cm->new_fb_idx == INVALID_IDX) return -1;
6540
Zoe Liuf452fdf2017-11-02 23:08:12 -07006541#if CONFIG_FRAME_MARKER
6542 // Retain the RF_LEVEL for the current newly coded frame.
6543 cpi->frame_rf_level[cm->new_fb_idx] =
6544 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
6545#endif // CONFIG_FRAME_MARKER
6546
Yaowu Xuc27fc142016-08-22 16:08:15 -07006547 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Sebastien Alaiwan48795802017-10-30 12:07:13 +01006548#if CONFIG_HIGHBITDEPTH
Yaowu Xu9b0f7032017-07-31 11:01:19 -07006549 cm->cur_frame->buf.buf_8bit_valid = 0;
6550#endif
Zoe Liu6cfaff92016-10-18 17:12:11 -07006551
Yaowu Xuc27fc142016-08-22 16:08:15 -07006552 // Start with a 0 size frame.
6553 *size = 0;
6554
6555 cpi->frame_flags = *frame_flags;
6556
6557 if (oxcf->pass == 2) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006558#if CONFIG_XIPHRC
6559 if (od_enc_rc_2pass_in(&cpi->od_rc) < 0) return -1;
6560 }
6561#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006562 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006563 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07006564 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006565 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006566#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006567
6568 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
6569 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i)
6570 cpi->scaled_ref_idx[i] = INVALID_IDX;
6571 }
6572
6573#if CONFIG_AOM_QM
6574 cm->using_qmatrix = cpi->oxcf.using_qm;
6575 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
6576 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
6577#endif
6578
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006579#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01006580 if (cm->seq_params.frame_id_numbers_present_flag) {
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006581 if (*time_stamp == 0) {
6582 cpi->common.current_frame_id = -1;
6583 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006584 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006585#endif // CONFIG_REFERENCE_BUFFER
RogerZhou3b635242017-09-19 10:06:46 -07006586#if CONFIG_AMVR
6587 cpi->cur_poc++;
6588 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
RogerZhou10a03802017-10-26 11:49:48 -07006589 if (cpi->common.seq_force_integer_mv == 2) {
RogerZhou3b635242017-09-19 10:06:46 -07006590 struct lookahead_entry *previous_entry =
6591 cpi->lookahead->buf + cpi->previsous_index;
RogerZhou10a03802017-10-26 11:49:48 -07006592 cpi->common.cur_frame_force_integer_mv = is_integer_mv(
RogerZhou3b635242017-09-19 10:06:46 -07006593 cpi, cpi->source, &previous_entry->img, cpi->previsou_hash_table);
6594 } else {
RogerZhou10a03802017-10-26 11:49:48 -07006595 cpi->common.cur_frame_force_integer_mv = cpi->common.seq_force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07006596 }
6597 } else {
RogerZhou10a03802017-10-26 11:49:48 -07006598 cpi->common.cur_frame_force_integer_mv = 0;
RogerZhou3b635242017-09-19 10:06:46 -07006599 }
6600#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006601
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006602#if CONFIG_XIPHRC
6603 if (oxcf->pass == 1) {
6604 size_t tmp;
6605 if (cpi->od_rc.cur_frame == 0) Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6606 cpi->od_rc.firstpass_quant = cpi->od_rc.target_quantizer;
6607 Pass0Encode(cpi, &tmp, dest, 0, frame_flags);
6608 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 0);
6609 } else if (oxcf->pass == 2) {
6610 Pass0Encode(cpi, size, dest, 0, frame_flags);
6611 } else {
6612 if (cpi->od_rc.cur_frame == 0) {
6613 size_t tmp;
6614 Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6615 }
6616 Pass0Encode(cpi, size, dest, 0, frame_flags);
6617 }
6618#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006619 if (oxcf->pass == 1) {
6620 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07006621 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006622 } else if (oxcf->pass == 2) {
6623 Pass2Encode(cpi, size, dest, frame_flags);
6624 } else {
6625 // One pass encode
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006626 Pass0Encode(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006627 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006628#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07006629#if CONFIG_HASH_ME
6630 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
RogerZhou3b635242017-09-19 10:06:46 -07006631#if CONFIG_AMVR
6632 cpi->previsou_hash_table = &cm->cur_frame->hash_table;
6633 {
6634 int l;
6635 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
6636 if ((cpi->lookahead->buf + l) == source) {
6637 cpi->previsous_index = l;
6638 break;
6639 }
6640 }
6641
6642 if (l == cpi->lookahead->max_sz) {
6643 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6644 "Failed to find last frame original buffer");
6645 }
6646 }
6647#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07006648 }
6649
6650#endif
6651
Yunqing Wang267e3272017-11-09 14:23:22 -08006652#if CONFIG_EXT_TILE
6653 if (!cm->large_scale_tile) {
6654#endif // CONFIG_EXT_TILE
Thomas Daededa4d8b92017-06-05 15:44:14 -07006655#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yunqing Wang267e3272017-11-09 14:23:22 -08006656 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006657#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006658 if (!cm->error_resilient_mode)
6659 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006660#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yunqing Wang267e3272017-11-09 14:23:22 -08006661#if CONFIG_EXT_TILE
6662 }
6663#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -07006664
6665 // No frame encoded, or frame was dropped, release scaled references.
6666 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6667 release_scaled_references(cpi);
6668 }
6669
6670 if (*size > 0) {
6671 cpi->droppable = !frame_is_reference(cpi);
6672 }
6673
Yaowu Xuf883b422016-08-30 14:01:10 -07006674 aom_usec_timer_mark(&cmptimer);
6675 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006676
6677 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6678 generate_psnr_packet(cpi);
6679
6680#if CONFIG_INTERNAL_STATS
6681 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006682 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006683 }
6684#endif // CONFIG_INTERNAL_STATS
6685
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006686#if CONFIG_XIPHRC
6687 cpi->od_rc.cur_frame++;
6688#endif
6689
Yaowu Xuf883b422016-08-30 14:01:10 -07006690 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006691
6692 return 0;
6693}
6694
Yaowu Xuf883b422016-08-30 14:01:10 -07006695int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6696 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006697 if (!cm->show_frame) {
6698 return -1;
6699 } else {
6700 int ret;
6701 if (cm->frame_to_show) {
6702 *dest = *cm->frame_to_show;
6703 dest->y_width = cm->width;
6704 dest->y_height = cm->height;
6705 dest->uv_width = cm->width >> cm->subsampling_x;
6706 dest->uv_height = cm->height >> cm->subsampling_y;
6707 ret = 0;
6708 } else {
6709 ret = -1;
6710 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006711 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006712 return ret;
6713 }
6714}
6715
Yaowu Xuf883b422016-08-30 14:01:10 -07006716int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006717 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6718
6719 *frame =
6720 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6721 return 0;
6722}
6723
Yaowu Xuf883b422016-08-30 14:01:10 -07006724int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6725 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006726 int hr = 0, hs = 0, vr = 0, vs = 0;
6727
6728 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6729
6730 Scale2Ratio(horiz_mode, &hr, &hs);
6731 Scale2Ratio(vert_mode, &vr, &vs);
6732
6733 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006734 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6735 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006736
6737 return 0;
6738}
6739
Yaowu Xuf883b422016-08-30 14:01:10 -07006740int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006741
Yaowu Xuf883b422016-08-30 14:01:10 -07006742void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006743 // TODO(yunqingwang): For what references to use, external encoding flags
6744 // should be consistent with internal reference frame selection. Need to
6745 // ensure that there is not conflict between the two. In AV1 encoder, the
6746 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
6747 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
6748 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006749 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006750 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006751 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6752 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6753 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006754 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006755 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006756 } else {
6757 int ref = AOM_REFFRAME_ALL;
6758
6759 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6760 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6761
6762 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6763
6764 if (flags & AOM_EFLAG_NO_REF_ARF) {
6765 ref ^= AOM_ALT_FLAG;
6766 ref ^= AOM_BWD_FLAG;
6767 ref ^= AOM_ALT2_FLAG;
6768 } else {
6769 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6770 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
6771 }
6772
6773 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006774 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006775 }
6776
6777 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006778 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006779 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006780
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006781 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6782 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006783
Yaowu Xuf883b422016-08-30 14:01:10 -07006784 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006785
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006786 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6787 upd ^= AOM_ALT_FLAG;
6788 upd ^= AOM_BWD_FLAG;
6789 upd ^= AOM_ALT2_FLAG;
6790 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006791
Yaowu Xuf883b422016-08-30 14:01:10 -07006792 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006793 }
6794
Yaowu Xuf883b422016-08-30 14:01:10 -07006795 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6796 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006797 }
6798}