blob: 4c844d83696c47cdb353e02d0821a056e11716a0 [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"
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070052#if CONFIG_NCOBMC_ADAPT_WEIGHT
53#include "av1/common/ncobmc_kernels.h"
54#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
Yaowu Xuc27fc142016-08-22 16:08:15 -070055#include "av1/encoder/picklpf.h"
56#if CONFIG_LOOP_RESTORATION
57#include "av1/encoder/pickrst.h"
58#endif // CONFIG_LOOP_RESTORATION
Debargha Mukherjee7166f222017-09-05 21:32:42 -070059#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070060#include "av1/encoder/ratectrl.h"
61#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070062#include "av1/encoder/segmentation.h"
63#include "av1/encoder/speed_features.h"
64#include "av1/encoder/temporal_filter.h"
65
Yaowu Xuf883b422016-08-30 14:01:10 -070066#include "./av1_rtcd.h"
67#include "./aom_dsp_rtcd.h"
68#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070069#include "aom_dsp/psnr.h"
70#if CONFIG_INTERNAL_STATS
71#include "aom_dsp/ssim.h"
72#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070073#include "aom_dsp/aom_dsp_common.h"
74#include "aom_dsp/aom_filter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070075#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070076#include "aom_ports/mem.h"
77#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070078#include "aom_scale/aom_scale.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070079#if CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070080#include "aom_util/debug_util.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070081#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070082
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080083#if CONFIG_ENTROPY_STATS
84FRAME_COUNTS aggregate_fc;
Zoe Liua56f9162017-06-21 22:49:57 -070085// Aggregate frame counts per frame context type
86FRAME_COUNTS aggregate_fc_per_type[FRAME_CONTEXTS];
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080087#endif // CONFIG_ENTROPY_STATS
88
Yaowu Xuc27fc142016-08-22 16:08:15 -070089#define AM_SEGMENT_ID_INACTIVE 7
90#define AM_SEGMENT_ID_ACTIVE 0
91
92#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
93
94#define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
95 // for altref computation.
96#define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
97 // mv. Choose a very high value for
98 // now so that HIGH_PRECISION is always
99 // chosen.
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -0700100
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101// #define OUTPUT_YUV_REC
102#ifdef OUTPUT_YUV_DENOISED
103FILE *yuv_denoised_file = NULL;
104#endif
105#ifdef OUTPUT_YUV_SKINMAP
106FILE *yuv_skinmap_file = NULL;
107#endif
108#ifdef OUTPUT_YUV_REC
109FILE *yuv_rec_file;
110#define FILE_NAME_LEN 100
111#endif
112
113#if 0
114FILE *framepsnr;
115FILE *kf_list;
116FILE *keyfile;
117#endif
118
Luc Trudeauf8164152017-04-11 16:20:51 -0400119#if CONFIG_CFL
120CFL_CTX NULL_CFL;
121#endif
122
Urvang Joshib5ed3502016-10-17 16:38:05 -0700123#if CONFIG_INTERNAL_STATS
124typedef enum { Y, U, V, ALL } STAT_TYPE;
125#endif // CONFIG_INTERNAL_STATS
126
Yaowu Xuf883b422016-08-30 14:01:10 -0700127static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700128 switch (mode) {
129 case NORMAL:
130 *hr = 1;
131 *hs = 1;
132 break;
133 case FOURFIVE:
134 *hr = 4;
135 *hs = 5;
136 break;
137 case THREEFIVE:
138 *hr = 3;
139 *hs = 5;
140 break;
141 case ONETWO:
142 *hr = 1;
143 *hs = 2;
144 break;
145 default:
146 *hr = 1;
147 *hs = 1;
148 assert(0);
149 break;
150 }
151}
152
153// Mark all inactive blocks as active. Other segmentation features may be set
154// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700155static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700156 unsigned char *const seg_map = cpi->segmentation_map;
157 int i;
158 if (cpi->active_map.enabled || cpi->active_map.update)
159 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
160 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
161 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
162}
163
Yaowu Xuf883b422016-08-30 14:01:10 -0700164static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 struct segmentation *const seg = &cpi->common.seg;
166 unsigned char *const seg_map = cpi->segmentation_map;
167 const unsigned char *const active_map = cpi->active_map.map;
168 int i;
169
170 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
171
172 if (frame_is_intra_only(&cpi->common)) {
173 cpi->active_map.enabled = 0;
174 cpi->active_map.update = 1;
175 }
176
177 if (cpi->active_map.update) {
178 if (cpi->active_map.enabled) {
179 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
180 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700181 av1_enable_segmentation(seg);
182 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700183#if CONFIG_LOOPFILTER_LEVEL
184 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
185 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
186 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
187 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
188
189 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
190 -MAX_LOOP_FILTER);
191 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
192 -MAX_LOOP_FILTER);
193 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
194 -MAX_LOOP_FILTER);
195 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
196 -MAX_LOOP_FILTER);
197#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700198 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199 // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
200 // filter level being zero regardless of the value of seg->abs_delta.
Yaowu Xuf883b422016-08-30 14:01:10 -0700201 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
202 -MAX_LOOP_FILTER);
Cheng Chend8184da2017-09-26 18:15:22 -0700203#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700204 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700205 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700206#if CONFIG_LOOPFILTER_LEVEL
207 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
208 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
209 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
210 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
211#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700212 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Cheng Chend8184da2017-09-26 18:15:22 -0700213#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700214 if (seg->enabled) {
215 seg->update_data = 1;
216 seg->update_map = 1;
217 }
218 }
219 cpi->active_map.update = 0;
220 }
221}
222
Yaowu Xuf883b422016-08-30 14:01:10 -0700223int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
224 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
226 unsigned char *const active_map_8x8 = cpi->active_map.map;
227 const int mi_rows = cpi->common.mi_rows;
228 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700229 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
230 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700231 cpi->active_map.update = 1;
232 if (new_map_16x16) {
233 int r, c;
234 for (r = 0; r < mi_rows; ++r) {
235 for (c = 0; c < mi_cols; ++c) {
236 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700237 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700238 ? AM_SEGMENT_ID_ACTIVE
239 : AM_SEGMENT_ID_INACTIVE;
240 }
241 }
242 cpi->active_map.enabled = 1;
243 } else {
244 cpi->active_map.enabled = 0;
245 }
246 return 0;
247 } else {
248 return -1;
249 }
250}
251
Yaowu Xuf883b422016-08-30 14:01:10 -0700252int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
253 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700254 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
255 new_map_16x16) {
256 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
257 const int mi_rows = cpi->common.mi_rows;
258 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700259 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
260 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
261
Yaowu Xuc27fc142016-08-22 16:08:15 -0700262 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
263 if (cpi->active_map.enabled) {
264 int r, c;
265 for (r = 0; r < mi_rows; ++r) {
266 for (c = 0; c < mi_cols; ++c) {
267 // Cyclic refresh segments are considered active despite not having
268 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700269 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
271 }
272 }
273 }
274 return 0;
275 } else {
276 return -1;
277 }
278}
279
RogerZhou3b635242017-09-19 10:06:46 -0700280static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv
281#if CONFIG_AMVR
282 ,
283 int cur_frame_mv_precision_level
284#endif
285 ) {
James Zern01a9d702017-08-25 19:09:33 +0000286 MACROBLOCK *const mb = &cpi->td.mb;
287 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
288
RogerZhou3b635242017-09-19 10:06:46 -0700289#if CONFIG_AMVR
290 if (cpi->common.allow_high_precision_mv &&
291 cur_frame_mv_precision_level == 0) {
292#else
James Zern01a9d702017-08-25 19:09:33 +0000293 if (cpi->common.allow_high_precision_mv) {
RogerZhou3b635242017-09-19 10:06:46 -0700294#endif
James Zern01a9d702017-08-25 19:09:33 +0000295 int i;
296 for (i = 0; i < NMV_CONTEXTS; ++i) {
297 mb->mv_cost_stack[i] = mb->nmvcost_hp[i];
298 }
299 } else {
300 int i;
301 for (i = 0; i < NMV_CONTEXTS; ++i) {
302 mb->mv_cost_stack[i] = mb->nmvcost[i];
303 }
304 }
305}
306
Yaowu Xuf883b422016-08-30 14:01:10 -0700307static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700308#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700309 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700310 return BLOCK_64X64;
311
Yaowu Xuf883b422016-08-30 14:01:10 -0700312 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700313 return BLOCK_128X128;
314
Yaowu Xuf883b422016-08-30 14:01:10 -0700315 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700316
317 assert(IMPLIES(cpi->common.tile_cols > 1,
318 cpi->common.tile_width % MAX_MIB_SIZE == 0));
319 assert(IMPLIES(cpi->common.tile_rows > 1,
320 cpi->common.tile_height % MAX_MIB_SIZE == 0));
321
322 // TODO(any): Possibly could improve this with a heuristic.
323 return BLOCK_128X128;
324#else
325 (void)cpi;
326 return BLOCK_64X64;
327#endif // CONFIG_EXT_PARTITION
328}
329
Yaowu Xuf883b422016-08-30 14:01:10 -0700330static void setup_frame(AV1_COMP *cpi) {
331 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700332 // Set up entropy context depending on frame type. The decoder mandates
333 // the use of the default context, index 0, for keyframes and inter
334 // frames where the error_resilient_mode or intra_only flag is set. For
335 // other inter-frames the encoder currently uses only two contexts;
336 // context 1 for ALTREF frames and context 0 for the others.
337 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700338 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700339 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700340#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
341// Just use frame context from first signaled reference frame.
342// This will always be LAST_FRAME for now.
343#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344#if CONFIG_EXT_REFS
345 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -0700346 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347 cm->frame_context_idx = EXT_ARF_FRAME;
348 else if (cpi->refresh_alt_ref_frame)
349 cm->frame_context_idx = ARF_FRAME;
Zoe Liue9b15e22017-07-19 15:53:01 -0700350#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700351 if (cpi->refresh_alt_ref_frame) cm->frame_context_idx = ARF_FRAME;
Zoe Liu6cfaff92016-10-18 17:12:11 -0700352#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700353 else if (cpi->rc.is_src_frame_alt_ref)
354 cm->frame_context_idx = OVERLAY_FRAME;
355 else if (cpi->refresh_golden_frame)
356 cm->frame_context_idx = GLD_FRAME;
357#if CONFIG_EXT_REFS
358 else if (cpi->refresh_bwd_ref_frame)
359 cm->frame_context_idx = BRF_FRAME;
Zoe Liu6cfaff92016-10-18 17:12:11 -0700360#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700361 else
362 cm->frame_context_idx = REGULAR_FRAME;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700363#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364 }
365
366 if (cm->frame_type == KEY_FRAME) {
367 cpi->refresh_golden_frame = 1;
368 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700369 av1_zero(cpi->interp_filter_selected);
Debargha Mukherjeed2630fa2017-09-22 10:32:51 -0700370 set_sb_size(cm, select_sb_size(cpi));
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700371#if CONFIG_REFERENCE_BUFFER
372 set_use_reference_buffer(cm, 0);
373#endif // CONFIG_REFERENCE_BUFFER
Yaowu Xuc27fc142016-08-22 16:08:15 -0700374 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700375#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
376 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
377 cm->frame_refs[0].idx < 0) {
378 *cm->fc = cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
379 } else {
380 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
381 }
382#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700384#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuf883b422016-08-30 14:01:10 -0700385 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700386 }
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200387#if CONFIG_EXT_REFS
Zoe Liu5a978832017-08-15 16:33:34 -0700388#if CONFIG_ONE_SIDED_COMPOUND && \
389 !CONFIG_EXT_COMP_REFS // No change to bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200390 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
391 cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
392 cpi->rc.is_bipred_frame = 1;
393 }
Zoe Liu5a978832017-08-15 16:33:34 -0700394#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
395#endif // CONFIG_EXT_REFS
Thomas Daededa4d8b92017-06-05 15:44:14 -0700396#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -0700397 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
398 cm->frame_refs[0].idx < 0) {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700399 // use default frame context values
400 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
401 } else {
Thomas Daedea6a854b2017-06-22 17:49:11 -0700402 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700403 cm->pre_fc = &cm->frame_contexts[cm->frame_refs[0].idx];
404 }
405#else
Thomas Daede10e1da92017-04-26 13:22:21 -0700406 cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700407#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700408
409 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700410}
411
Cheng Chen46f30c72017-09-07 11:13:33 -0700412static void enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700413 int i;
414 cm->mi = cm->mip + cm->mi_stride + 1;
415 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
416 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
417 // Clear top border row
418 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
419 // Clear left border column
420 for (i = 1; i < cm->mi_rows + 1; ++i)
421 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700422 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
423 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
424
425 memset(cm->mi_grid_base, 0,
426 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
427}
428
Cheng Chen46f30c72017-09-07 11:13:33 -0700429static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700430 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700431 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700432 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700433 if (!cm->prev_mip) return 1;
434 cm->mi_alloc_size = mi_size;
435
Yaowu Xuf883b422016-08-30 14:01:10 -0700436 cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437 if (!cm->mi_grid_base) return 1;
438 cm->prev_mi_grid_base =
Yaowu Xuf883b422016-08-30 14:01:10 -0700439 (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700440 if (!cm->prev_mi_grid_base) return 1;
441
442 return 0;
443}
444
Cheng Chen46f30c72017-09-07 11:13:33 -0700445static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700446 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700447 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700448 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700449 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700450 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700451 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700452 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700453 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700454 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700455}
456
Cheng Chen46f30c72017-09-07 11:13:33 -0700457static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700458 // Current mip will be the prev_mip for the next frame.
459 MODE_INFO **temp_base = cm->prev_mi_grid_base;
460 MODE_INFO *temp = cm->prev_mip;
461 cm->prev_mip = cm->mip;
462 cm->mip = temp;
463
464 // Update the upper left visible macroblock ptrs.
465 cm->mi = cm->mip + cm->mi_stride + 1;
466 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
467
468 cm->prev_mi_grid_base = cm->mi_grid_base;
469 cm->mi_grid_base = temp_base;
470 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
471 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
472}
473
Yaowu Xuf883b422016-08-30 14:01:10 -0700474void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475 static volatile int init_done = 0;
476
477 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700478 av1_rtcd();
479 aom_dsp_rtcd();
480 aom_scale_rtcd();
481 av1_init_intra_predictors();
482 av1_init_me_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000483#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -0700484 av1_rc_init_minq_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000485#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 av1_entropy_mv_init();
487 av1_encode_token_init();
Yaowu Xuf883b422016-08-30 14:01:10 -0700488 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700489 init_done = 1;
490 }
491}
492
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700493static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
494 if (cpi->mbmi_ext_base) {
495 aom_free(cpi->mbmi_ext_base);
496 cpi->mbmi_ext_base = NULL;
497 }
498}
499
500static void alloc_context_buffers_ext(AV1_COMP *cpi) {
501 AV1_COMMON *cm = &cpi->common;
502 int mi_size = cm->mi_cols * cm->mi_rows;
503
504 dealloc_context_buffers_ext(cpi);
505 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
506 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
507}
508
Yaowu Xuf883b422016-08-30 14:01:10 -0700509static void dealloc_compressor_data(AV1_COMP *cpi) {
510 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700511
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700512 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700513
Yushin Cho77bba8d2016-11-04 16:36:56 -0700514#if CONFIG_PVQ
515 if (cpi->oxcf.pass != 1) {
Yushin Cho749c0572017-04-07 10:36:47 -0700516 const int tile_cols = cm->tile_cols;
517 const int tile_rows = cm->tile_rows;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700518 int tile_col, tile_row;
519
520 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
521 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
522 TileDataEnc *tile_data =
523 &cpi->tile_data[tile_row * tile_cols + tile_col];
524 aom_free(tile_data->pvq_q.buf);
525 }
526 }
527#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700528 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529 cpi->tile_data = NULL;
530
531 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700532 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534
Yaowu Xuf883b422016-08-30 14:01:10 -0700535 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 cpi->cyclic_refresh = NULL;
537
Yaowu Xuf883b422016-08-30 14:01:10 -0700538 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700539 cpi->active_map.map = NULL;
540
Jingning Hand064cf02017-06-01 10:00:39 -0700541#if CONFIG_MOTION_VAR
542 aom_free(cpi->td.mb.above_pred_buf);
543 cpi->td.mb.above_pred_buf = NULL;
544
545 aom_free(cpi->td.mb.left_pred_buf);
546 cpi->td.mb.left_pred_buf = NULL;
547
548 aom_free(cpi->td.mb.wsrc_buf);
549 cpi->td.mb.wsrc_buf = NULL;
550
551 aom_free(cpi->td.mb.mask_buf);
552 cpi->td.mb.mask_buf = NULL;
553#endif
554
Yaowu Xuf883b422016-08-30 14:01:10 -0700555 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700556#if CONFIG_LV_MAP
557 av1_free_txb_buf(cpi);
558#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700559 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700560
Yaowu Xuf883b422016-08-30 14:01:10 -0700561 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700563 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800564 aom_free_frame_buffer(&cpi->last_frame_db);
565 aom_free_frame_buffer(&cpi->trial_frame_rst);
566 aom_free(cpi->extra_rstbuf);
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -0700567 {
568 int i;
569 for (i = 0; i < MAX_MB_PLANE; ++i)
570 av1_free_restoration_struct(&cpi->rst_search[i]);
571 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700572#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700573 aom_free_frame_buffer(&cpi->scaled_source);
574 aom_free_frame_buffer(&cpi->scaled_last_source);
575 aom_free_frame_buffer(&cpi->alt_ref_buffer);
576 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700577
Yaowu Xuf883b422016-08-30 14:01:10 -0700578 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579 cpi->tile_tok[0][0] = 0;
580
Yaowu Xuf883b422016-08-30 14:01:10 -0700581 av1_free_pc_tree(&cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582
hui sud9a812b2017-07-06 14:34:37 -0700583 aom_free(cpi->td.mb.palette_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700584
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -0700586 aom_buf_ans_free(&cpi->buf_ans);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700587#endif // CONFIG_ANS
588}
589
Yaowu Xuf883b422016-08-30 14:01:10 -0700590static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700591 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700592 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000593 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200595 // Stores a snapshot of key state variables which can subsequently be
596 // restored with a call to av1_restore_coding_context. These functions are
597 // intended for use in a re-code loop in av1_compress_frame where the
598 // quantizer value is adjusted between loop iterations.
James Zern01a9d702017-08-25 19:09:33 +0000599 for (i = 0; i < NMV_CONTEXTS; ++i) {
600 av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
601 av1_copy(cc->nmv_costs, cpi->nmv_costs);
602 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
603 }
604
Yaowu Xuf883b422016-08-30 14:01:10 -0700605 av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
606 av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607
608 cc->fc = *cm->fc;
609}
610
Yaowu Xuf883b422016-08-30 14:01:10 -0700611static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700612 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700613 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000614 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700615
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200616 // Restore key state variables to the snapshot state stored in the
617 // previous call to av1_save_coding_context.
James Zern01a9d702017-08-25 19:09:33 +0000618 for (i = 0; i < NMV_CONTEXTS; ++i) {
619 av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
620 av1_copy(cpi->nmv_costs, cc->nmv_costs);
621 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
622 }
623
Yaowu Xuf883b422016-08-30 14:01:10 -0700624 av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
625 av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626
627 *cm->fc = cc->fc;
628}
629
Yaowu Xuf883b422016-08-30 14:01:10 -0700630static void configure_static_seg_features(AV1_COMP *cpi) {
631 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632 const RATE_CONTROL *const rc = &cpi->rc;
633 struct segmentation *const seg = &cm->seg;
634
635 int high_q = (int)(rc->avg_q > 48.0);
636 int qi_delta;
637
638 // Disable and clear down for KF
639 if (cm->frame_type == KEY_FRAME) {
640 // Clear down the global segmentation map
641 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
642 seg->update_map = 0;
643 seg->update_data = 0;
644 cpi->static_mb_pct = 0;
645
646 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700647 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700648
649 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700650 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700651 } else if (cpi->refresh_alt_ref_frame) {
652 // If this is an alt ref frame
653 // Clear down the global segmentation map
654 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
655 seg->update_map = 0;
656 seg->update_data = 0;
657 cpi->static_mb_pct = 0;
658
659 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700660 av1_disable_segmentation(seg);
661 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700662
663 // Scan frames from current to arf frame.
664 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700665 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700666
667 // If segmentation was enabled set those features needed for the
668 // arf itself.
669 if (seg->enabled) {
670 seg->update_map = 1;
671 seg->update_data = 1;
672
673 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700674 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
675 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700676#if CONFIG_LOOPFILTER_LEVEL
677 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
678 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
679 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
680 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
681
682 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
683 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
684 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
685 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
686#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700687 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Cheng Chend8184da2017-09-26 18:15:22 -0700688 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
689#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690
Yaowu Xuf883b422016-08-30 14:01:10 -0700691 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700692
693 // Where relevant assume segment data is delta data
694 seg->abs_delta = SEGMENT_DELTADATA;
695 }
696 } else if (seg->enabled) {
697 // All other frames if segmentation has been enabled
698
699 // First normal frame in a valid gf or alt ref group
700 if (rc->frames_since_golden == 0) {
701 // Set up segment features for normal frames in an arf group
702 if (rc->source_alt_ref_active) {
703 seg->update_map = 0;
704 seg->update_data = 1;
705 seg->abs_delta = SEGMENT_DELTADATA;
706
Yaowu Xuf883b422016-08-30 14:01:10 -0700707 qi_delta =
708 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
709 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
710 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700711
Cheng Chend8184da2017-09-26 18:15:22 -0700712#if CONFIG_LOOPFILTER_LEVEL
713 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
714 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
715 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
716 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
717
718 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
719 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
720 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
721 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
722#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700723 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
724 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Cheng Chend8184da2017-09-26 18:15:22 -0700725#endif // CONFIG_LOOPFILTER_LEVEL
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726
727 // Segment coding disabled for compred testing
728 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
730 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
731 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 }
733 } else {
734 // Disable segmentation and clear down features if alt ref
735 // is not active for this group
736
Yaowu Xuf883b422016-08-30 14:01:10 -0700737 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700738
739 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
740
741 seg->update_map = 0;
742 seg->update_data = 0;
743
Yaowu Xuf883b422016-08-30 14:01:10 -0700744 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700745 }
746 } else if (rc->is_src_frame_alt_ref) {
747 // Special case where we are coding over the top of a previous
748 // alt ref frame.
749 // Segment coding disabled for compred testing
750
751 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700752 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
753 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700754
755 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700756 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
757 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
758 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
759 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760
761 // Skip all MBs if high Q (0,0 mv and skip coeffs)
762 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700763 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
764 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 }
766 // Enable data update
767 seg->update_data = 1;
768 } else {
769 // All other frames.
770
771 // No updates.. leave things as they are.
772 seg->update_map = 0;
773 seg->update_data = 0;
774 }
775 }
776}
777
Yaowu Xuf883b422016-08-30 14:01:10 -0700778static void update_reference_segmentation_map(AV1_COMP *cpi) {
779 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
781 uint8_t *cache_ptr = cm->last_frame_seg_map;
782 int row, col;
783
784 for (row = 0; row < cm->mi_rows; row++) {
785 MODE_INFO **mi_8x8 = mi_8x8_ptr;
786 uint8_t *cache = cache_ptr;
787 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
788 cache[0] = mi_8x8[0]->mbmi.segment_id;
789 mi_8x8_ptr += cm->mi_stride;
790 cache_ptr += cm->mi_cols;
791 }
792}
793
Yaowu Xuf883b422016-08-30 14:01:10 -0700794static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
795 AV1_COMMON *cm = &cpi->common;
796 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797
798 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
800 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200801#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700802 cm->use_highbitdepth,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700804 oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700806 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700807 "Failed to allocate lag buffers");
808
809 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700810 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700811 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200812#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700813 cm->use_highbitdepth,
814#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700815 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
816 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700817 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818 "Failed to allocate altref buffer");
819}
820
Yaowu Xuf883b422016-08-30 14:01:10 -0700821static void alloc_util_frame_buffers(AV1_COMP *cpi) {
822 AV1_COMMON *const cm = &cpi->common;
823 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700824 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200825#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826 cm->use_highbitdepth,
827#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700828 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
829 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700830 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700831 "Failed to allocate last frame buffer");
832
833#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700834 if (aom_realloc_frame_buffer(&cpi->last_frame_db, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200836#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700837 cm->use_highbitdepth,
838#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700839 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
840 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842 "Failed to allocate last frame deblocked buffer");
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700843 if (aom_realloc_frame_buffer(
844 &cpi->trial_frame_rst,
845#if CONFIG_FRAME_SUPERRES
846 cm->superres_upscaled_width, cm->superres_upscaled_height,
847#else
848 cm->width, cm->height,
849#endif // CONFIG_FRAME_SUPERRES
850 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200851#if CONFIG_HIGHBITDEPTH
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700852 cm->use_highbitdepth,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800853#endif
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700854 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800855 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800856 "Failed to allocate trial restored frame buffer");
Alex Converse232e3842017-02-24 12:24:36 -0800857 int extra_rstbuf_sz = RESTORATION_EXTBUF_SIZE;
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -0800858 if (extra_rstbuf_sz > 0) {
Alex Converse7f094f12017-02-23 17:29:40 -0800859 aom_free(cpi->extra_rstbuf);
Alex Converse232e3842017-02-24 12:24:36 -0800860 CHECK_MEM_ERROR(cm, cpi->extra_rstbuf,
Alex Converse7f094f12017-02-23 17:29:40 -0800861 (uint8_t *)aom_malloc(extra_rstbuf_sz));
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -0800862 } else {
863 cpi->extra_rstbuf = NULL;
864 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865#endif // CONFIG_LOOP_RESTORATION
866
Yaowu Xuf883b422016-08-30 14:01:10 -0700867 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200869#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870 cm->use_highbitdepth,
871#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700872 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
873 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700874 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 "Failed to allocate scaled source buffer");
876
Yaowu Xuf883b422016-08-30 14:01:10 -0700877 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200879#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880 cm->use_highbitdepth,
881#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700882 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
883 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700884 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700885 "Failed to allocate scaled last source buffer");
886}
887
Cheng Chen46f30c72017-09-07 11:13:33 -0700888static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700889 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700890
Yaowu Xuf883b422016-08-30 14:01:10 -0700891 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700892
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700893#if CONFIG_LV_MAP
894 av1_alloc_txb_buf(cpi);
895#endif
896
Yaowu Xuc27fc142016-08-22 16:08:15 -0700897 alloc_context_buffers_ext(cpi);
898
Yaowu Xuf883b422016-08-30 14:01:10 -0700899 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700900
901 {
902 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
903 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700904 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700905 }
906
Yaowu Xuf883b422016-08-30 14:01:10 -0700907 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908}
909
Yaowu Xuf883b422016-08-30 14:01:10 -0700910void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700911 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000912#if CONFIG_XIPHRC
913 if (!cpi->od_rc.cur_frame) return;
914 cpi->od_rc.framerate = cpi->framerate;
915 od_enc_rc_resize(&cpi->od_rc);
916#else
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700917 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000918#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700919}
920
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200921#if CONFIG_MAX_TILE
922
923static void set_tile_info_max_tile(AV1_COMP *cpi) {
924 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200925 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200926
927 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200928
929 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200930 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200931 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200932 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
933 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200934 } else {
935 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
936 int sb_cols = mi_cols >> MAX_MIB_SIZE_LOG2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200937 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200938 cm->uniform_tile_spacing_flag = 0;
939 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
940 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200941 size_sb = cpi->oxcf.tile_widths[j++];
942 if (j >= cpi->oxcf.tile_width_count) j = 0;
943 start_sb += AOMMIN(size_sb, MAX_TILE_WIDTH_SB);
Dominic Symesf58f1112017-09-25 12:47:40 +0200944 }
945 cm->tile_cols = i;
946 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200947 }
948 av1_calculate_tile_cols(cm);
949
950 // configure tile rows
951 if (cm->uniform_tile_spacing_flag) {
952 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
953 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200954 } else {
955 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
956 int sb_rows = mi_rows >> MAX_MIB_SIZE_LOG2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200957 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200958 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
959 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200960 size_sb = cpi->oxcf.tile_heights[j++];
961 if (j >= cpi->oxcf.tile_height_count) j = 0;
962 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200963 }
964 cm->tile_rows = i;
965 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200966 }
967 av1_calculate_tile_rows(cm);
968}
969
970#endif
971
Yaowu Xuf883b422016-08-30 14:01:10 -0700972static void set_tile_info(AV1_COMP *cpi) {
973 AV1_COMMON *const cm = &cpi->common;
Thomas Daviesb25ba502017-07-18 10:18:24 +0100974#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -0800975 int tile_row, tile_col, num_tiles_in_tg;
976 int tg_row_start, tg_col_start;
977#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700978#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700979 if (cpi->oxcf.large_scale_tile) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700980#if CONFIG_EXT_PARTITION
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700981 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
982 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
983 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
984 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
985 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
986 } else {
987 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
988 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
989 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
990 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
991 }
992#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700993 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
994 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700995 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
996 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700997#endif // CONFIG_EXT_PARTITION
998
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700999 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
1000 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001001
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001002 assert(cm->tile_width >> MAX_MIB_SIZE <= 32);
1003 assert(cm->tile_height >> MAX_MIB_SIZE <= 32);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001004
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001005 // Get the number of tiles
1006 cm->tile_cols = 1;
1007 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001008
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001009 cm->tile_rows = 1;
1010 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
1011 } else {
1012#endif // CONFIG_EXT_TILE
Yaowu Xuc27fc142016-08-22 16:08:15 -07001013
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001014#if CONFIG_MAX_TILE
1015 set_tile_info_max_tile(cpi);
1016#else
1017 int min_log2_tile_cols, max_log2_tile_cols;
1018 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001019
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001020 cm->log2_tile_cols =
1021 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
1022 cm->log2_tile_rows = cpi->oxcf.tile_rows;
Rupert Swarbrick940942b2017-09-01 12:05:55 +01001023
Rupert Swarbrick5a010aa2017-09-26 16:16:48 +01001024 cm->tile_width =
1025 get_tile_size(cm->mi_cols, cm->log2_tile_cols, &cm->tile_cols);
1026 cm->tile_height =
1027 get_tile_size(cm->mi_rows, cm->log2_tile_rows, &cm->tile_rows);
Dominic Symesdb5d66f2017-08-18 18:11:34 +02001028#endif // CONFIG_MAX_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001029#if CONFIG_EXT_TILE
1030 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07001031#endif // CONFIG_EXT_TILE
Ryan Lei7386eda2016-12-08 21:08:31 -08001032
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08001033#if CONFIG_DEPENDENT_HORZTILES
1034 cm->dependent_horz_tiles = cpi->oxcf.dependent_horz_tiles;
Fangwen Fu70bcb892017-05-06 17:05:19 -07001035#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001036 if (cm->large_scale_tile) {
1037 // May not needed since cpi->oxcf.dependent_horz_tiles is already adjusted.
1038 cm->dependent_horz_tiles = 0;
Fangwen Fu73126c02017-02-08 22:37:47 -08001039 } else {
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001040#endif // CONFIG_EXT_TILE
1041 if (cm->log2_tile_rows == 0) cm->dependent_horz_tiles = 0;
1042#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -08001043 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001044#endif // CONFIG_EXT_TILE
1045
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001046#if CONFIG_EXT_TILE
1047 if (!cm->large_scale_tile) {
1048#endif // CONFIG_EXT_TILE
1049 if (cpi->oxcf.mtu == 0) {
1050 cm->num_tg = cpi->oxcf.num_tile_groups;
1051 } else {
1052 // Use a default value for the purposes of weighting costs in probability
1053 // updates
1054 cm->num_tg = DEFAULT_MAX_NUM_TG;
Fangwen Fu73126c02017-02-08 22:37:47 -08001055 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001056 num_tiles_in_tg =
1057 (cm->tile_cols * cm->tile_rows + cm->num_tg - 1) / cm->num_tg;
1058 tg_row_start = 0;
1059 tg_col_start = 0;
1060 for (tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
1061 for (tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
1062 if ((tile_row * cm->tile_cols + tile_col) % num_tiles_in_tg == 0) {
1063 tg_row_start = tile_row;
1064 tg_col_start = tile_col;
1065 }
1066 cm->tile_group_start_row[tile_row][tile_col] = tg_row_start;
1067 cm->tile_group_start_col[tile_row][tile_col] = tg_col_start;
1068 }
1069 }
1070#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -08001071 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -07001072#endif // CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -08001073#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -08001074
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001075#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -08001076 cm->loop_filter_across_tiles_enabled =
1077 cpi->oxcf.loop_filter_across_tiles_enabled;
Ryan Lei9b02b0e2017-01-30 15:52:20 -08001078#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001079}
1080
Yaowu Xuf883b422016-08-30 14:01:10 -07001081static void update_frame_size(AV1_COMP *cpi) {
1082 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001083 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
1084
Yaowu Xuf883b422016-08-30 14:01:10 -07001085 av1_set_mb_mi(cm, cm->width, cm->height);
1086 av1_init_context_buffers(cm);
Yushin Cho77bba8d2016-11-04 16:36:56 -07001087 av1_init_macroblockd(cm, xd,
1088#if CONFIG_PVQ
1089 NULL,
1090#endif
Luc Trudeauf8164152017-04-11 16:20:51 -04001091#if CONFIG_CFL
1092 &NULL_CFL,
1093#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -07001094 NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001095 memset(cpi->mbmi_ext_base, 0,
1096 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001097 set_tile_info(cpi);
1098}
1099
Yaowu Xuf883b422016-08-30 14:01:10 -07001100static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001101#if CONFIG_EXT_REFS
1102 int fb_idx;
1103 for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
1104 cpi->lst_fb_idxes[fb_idx] = fb_idx;
1105 cpi->gld_fb_idx = LAST_REF_FRAMES;
1106 cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07001107 cpi->alt2_fb_idx = LAST_REF_FRAMES + 2;
1108 cpi->alt_fb_idx = LAST_REF_FRAMES + 3;
Zoe Liu8dd1c982017-09-11 10:14:35 -07001109 cpi->ext_fb_idx = LAST_REF_FRAMES + 4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001110 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
1111 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
Zoe Liue9b15e22017-07-19 15:53:01 -07001112#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07001113 cpi->lst_fb_idx = 0;
1114 cpi->gld_fb_idx = 1;
1115 cpi->alt_fb_idx = 2;
1116#endif // CONFIG_EXT_REFS
RogerZhou3b635242017-09-19 10:06:46 -07001117#if CONFIG_AMVR
1118 cpi->rate_index = 0;
1119 cpi->rate_size = 0;
1120 cpi->cur_poc = -1;
1121#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001122}
1123
Yaowu Xuf883b422016-08-30 14:01:10 -07001124static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1125 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001126
1127 cpi->oxcf = *oxcf;
1128 cpi->framerate = oxcf->init_framerate;
1129
1130 cm->profile = oxcf->profile;
1131 cm->bit_depth = oxcf->bit_depth;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001132#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001133 cm->use_highbitdepth = oxcf->use_highbitdepth;
1134#endif
1135 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07001136#if CONFIG_COLORSPACE_HEADERS
1137 cm->transfer_function = oxcf->transfer_function;
1138 cm->chroma_sample_position = oxcf->chroma_sample_position;
1139#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001140 cm->color_range = oxcf->color_range;
1141
1142 cm->width = oxcf->width;
1143 cm->height = oxcf->height;
Cheng Chen46f30c72017-09-07 11:13:33 -07001144 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001145
1146 // Single thread case: use counts in common.
1147 cpi->td.counts = &cm->counts;
1148
1149 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001150 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001151
1152 cpi->static_mb_pct = 0;
1153 cpi->ref_frame_flags = 0;
1154
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001155 // Reset resize pending flags
1156 cpi->resize_pending_width = 0;
1157 cpi->resize_pending_height = 0;
1158
Yaowu Xuc27fc142016-08-22 16:08:15 -07001159 init_buffer_indices(cpi);
1160}
1161
1162static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001163 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001164 const int64_t bandwidth = oxcf->target_bandwidth;
1165 const int64_t starting = oxcf->starting_buffer_level_ms;
1166 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1167 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1168
1169 rc->starting_buffer_level = starting * bandwidth / 1000;
1170 rc->optimal_buffer_level =
1171 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1172 rc->maximum_buffer_size =
1173 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1174}
1175
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001176#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001177#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
1178 cpi->fn_ptr[BT].sdf = SDF; \
1179 cpi->fn_ptr[BT].sdaf = SDAF; \
1180 cpi->fn_ptr[BT].vf = VF; \
1181 cpi->fn_ptr[BT].svf = SVF; \
1182 cpi->fn_ptr[BT].svaf = SVAF; \
1183 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1184 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1185 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1186
1187#define MAKE_BFP_SAD_WRAPPER(fnname) \
1188 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1189 int source_stride, \
1190 const uint8_t *ref_ptr, int ref_stride) { \
1191 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1192 } \
1193 static unsigned int fnname##_bits10( \
1194 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1195 int ref_stride) { \
1196 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1197 } \
1198 static unsigned int fnname##_bits12( \
1199 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1200 int ref_stride) { \
1201 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1202 }
1203
1204#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1205 static unsigned int fnname##_bits8( \
1206 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1207 int ref_stride, const uint8_t *second_pred) { \
1208 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1209 } \
1210 static unsigned int fnname##_bits10( \
1211 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1212 int ref_stride, const uint8_t *second_pred) { \
1213 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1214 2; \
1215 } \
1216 static unsigned int fnname##_bits12( \
1217 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1218 int ref_stride, const uint8_t *second_pred) { \
1219 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1220 4; \
1221 }
1222
1223#define MAKE_BFP_SAD3_WRAPPER(fnname) \
1224 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1225 const uint8_t *ref_ptr, int ref_stride, \
1226 unsigned int *sad_array) { \
1227 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1228 } \
1229 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1230 const uint8_t *ref_ptr, int ref_stride, \
1231 unsigned int *sad_array) { \
1232 int i; \
1233 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1234 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1235 } \
1236 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1237 const uint8_t *ref_ptr, int ref_stride, \
1238 unsigned int *sad_array) { \
1239 int i; \
1240 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1241 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1242 }
1243
1244#define MAKE_BFP_SAD8_WRAPPER(fnname) \
1245 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1246 const uint8_t *ref_ptr, int ref_stride, \
1247 unsigned int *sad_array) { \
1248 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1249 } \
1250 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1251 const uint8_t *ref_ptr, int ref_stride, \
1252 unsigned int *sad_array) { \
1253 int i; \
1254 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1255 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1256 } \
1257 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1258 const uint8_t *ref_ptr, int ref_stride, \
1259 unsigned int *sad_array) { \
1260 int i; \
1261 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1262 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1263 }
1264#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1265 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1266 const uint8_t *const ref_ptr[], int ref_stride, \
1267 unsigned int *sad_array) { \
1268 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1269 } \
1270 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1271 const uint8_t *const ref_ptr[], int ref_stride, \
1272 unsigned int *sad_array) { \
1273 int i; \
1274 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1275 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1276 } \
1277 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1278 const uint8_t *const ref_ptr[], int ref_stride, \
1279 unsigned int *sad_array) { \
1280 int i; \
1281 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1282 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1283 }
1284
1285#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001286MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1287MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1288MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
1289MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
1290MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1291MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1292MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1293MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1294MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1295MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1296MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001297#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001298MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1299MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1300MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1301MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1302MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1303MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1304MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1305MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1306MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1307MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1308MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1309MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1310MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1311MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1312MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
1313MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
1314MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1315MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1316MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1317MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
1318MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
1319MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1320MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1321MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1322MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
1323MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
1324MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1325MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1326MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1327MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
1328MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
1329MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1330MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1331MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1332MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
1333MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
1334MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1335MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1336MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1337MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
1338MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
1339MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1340MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1341MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1342MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
1343MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1344MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1345MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1346MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
1347MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1348MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1349MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1350MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
1351MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
1352MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001353
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001354#if CONFIG_EXT_PARTITION_TYPES
1355MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1356MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1357MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1358MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1359MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1360MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1361MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1362MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1363MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1364MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1365MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1366MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001367MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1368MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1369MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1370MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1371MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1372MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001373#if CONFIG_EXT_PARTITION
1374MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x128)
1375MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x128_avg)
1376MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x128x4d)
1377MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x32)
1378MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x32_avg)
1379MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x32x4d)
1380#endif // CONFIG_EXT_PARTITION
1381#endif // CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001382
David Barker0f3c94e2017-05-16 15:21:50 +01001383#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001384 cpi->fn_ptr[BT].msdf = MCSDF; \
1385 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386
David Barkerc155e012017-05-11 13:54:54 +01001387#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1388 static unsigned int fnname##_bits8( \
1389 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1390 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1391 int m_stride, int invert_mask) { \
1392 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1393 second_pred_ptr, m, m_stride, invert_mask); \
1394 } \
1395 static unsigned int fnname##_bits10( \
1396 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1397 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1398 int m_stride, int invert_mask) { \
1399 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1400 second_pred_ptr, m, m_stride, invert_mask) >> \
1401 2; \
1402 } \
1403 static unsigned int fnname##_bits12( \
1404 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1405 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1406 int m_stride, int invert_mask) { \
1407 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1408 second_pred_ptr, m, m_stride, invert_mask) >> \
1409 4; \
1410 }
1411
Yaowu Xuc27fc142016-08-22 16:08:15 -07001412#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001413MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1414MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1415MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001416#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001417MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1418MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1419MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1420MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1421MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1422MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1423MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1424MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1425MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1426MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1427MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1428MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1429MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001430
1431#if CONFIG_EXT_PARTITION_TYPES
1432MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1433MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1434MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1435MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001436MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1437MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001438#if CONFIG_EXT_PARTITION
1439MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x128)
1440MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x32)
1441#endif // CONFIG_EXT_PARTITION
1442#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07001443
Yue Chencb60b182016-10-13 15:18:22 -07001444#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001445#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1446 cpi->fn_ptr[BT].osdf = OSDF; \
1447 cpi->fn_ptr[BT].ovf = OVF; \
1448 cpi->fn_ptr[BT].osvf = OSVF;
1449
1450#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1451 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1452 const int32_t *wsrc, \
1453 const int32_t *msk) { \
1454 return fnname(ref, ref_stride, wsrc, msk); \
1455 } \
1456 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1457 const int32_t *wsrc, \
1458 const int32_t *msk) { \
1459 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1460 } \
1461 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1462 const int32_t *wsrc, \
1463 const int32_t *msk) { \
1464 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1465 }
1466
1467#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001468MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1469MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1470MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001471#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001472MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1473MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1474MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1475MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1476MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1477MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1478MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1479MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1480MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1481MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1482MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1483MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1484MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001485
1486#if CONFIG_EXT_PARTITION_TYPES
1487MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1488MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1489MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1490MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001491MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1492MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001493#if CONFIG_EXT_PARTITION
1494MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x128)
1495MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x32)
1496#endif // CONFIG_EXT_PARTITION
1497#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chencb60b182016-10-13 15:18:22 -07001498#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001499
Yaowu Xuf883b422016-08-30 14:01:10 -07001500static void highbd_set_var_fns(AV1_COMP *const cpi) {
1501 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001502 if (cm->use_highbitdepth) {
1503 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001504 case AOM_BITS_8:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001505#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001506#if CONFIG_EXT_PARTITION
1507 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits8,
1508 aom_highbd_sad128x32_avg_bits8, aom_highbd_8_variance128x32,
1509 aom_highbd_8_sub_pixel_variance128x32,
1510 aom_highbd_8_sub_pixel_avg_variance128x32, NULL, NULL,
1511 aom_highbd_sad128x32x4d_bits8)
1512
1513 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits8,
1514 aom_highbd_sad32x128_avg_bits8, aom_highbd_8_variance32x128,
1515 aom_highbd_8_sub_pixel_variance32x128,
1516 aom_highbd_8_sub_pixel_avg_variance32x128, NULL, NULL,
1517 aom_highbd_sad32x128x4d_bits8)
1518#endif // CONFIG_EXT_PARTITION
1519
Rupert Swarbrick72678572017-08-02 12:05:26 +01001520 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1521 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1522 aom_highbd_8_sub_pixel_variance64x16,
1523 aom_highbd_8_sub_pixel_avg_variance64x16, NULL, NULL,
1524 aom_highbd_sad64x16x4d_bits8)
1525
1526 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1527 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1528 aom_highbd_8_sub_pixel_variance16x64,
1529 aom_highbd_8_sub_pixel_avg_variance16x64, NULL, NULL,
1530 aom_highbd_sad16x64x4d_bits8)
1531
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001532 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits8,
1533 aom_highbd_sad32x8_avg_bits8, aom_highbd_8_variance32x8,
1534 aom_highbd_8_sub_pixel_variance32x8,
1535 aom_highbd_8_sub_pixel_avg_variance32x8, NULL, NULL,
1536 aom_highbd_sad32x8x4d_bits8)
1537
1538 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits8,
1539 aom_highbd_sad8x32_avg_bits8, aom_highbd_8_variance8x32,
1540 aom_highbd_8_sub_pixel_variance8x32,
1541 aom_highbd_8_sub_pixel_avg_variance8x32, NULL, NULL,
1542 aom_highbd_sad8x32x4d_bits8)
1543
1544 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits8,
1545 aom_highbd_sad16x4_avg_bits8, aom_highbd_8_variance16x4,
1546 aom_highbd_8_sub_pixel_variance16x4,
1547 aom_highbd_8_sub_pixel_avg_variance16x4, NULL, NULL,
1548 aom_highbd_sad16x4x4d_bits8)
1549
1550 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits8,
1551 aom_highbd_sad4x16_avg_bits8, aom_highbd_8_variance4x16,
1552 aom_highbd_8_sub_pixel_variance4x16,
1553 aom_highbd_8_sub_pixel_avg_variance4x16, NULL, NULL,
1554 aom_highbd_sad4x16x4d_bits8)
1555#endif
1556
Yaowu Xuf883b422016-08-30 14:01:10 -07001557 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1558 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1559 aom_highbd_8_sub_pixel_variance32x16,
1560 aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1561 aom_highbd_sad32x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001562
Yaowu Xuf883b422016-08-30 14:01:10 -07001563 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1564 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1565 aom_highbd_8_sub_pixel_variance16x32,
1566 aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1567 aom_highbd_sad16x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001568
Yaowu Xuf883b422016-08-30 14:01:10 -07001569 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1570 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1571 aom_highbd_8_sub_pixel_variance64x32,
1572 aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1573 aom_highbd_sad64x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001574
Yaowu Xuf883b422016-08-30 14:01:10 -07001575 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1576 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1577 aom_highbd_8_sub_pixel_variance32x64,
1578 aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1579 aom_highbd_sad32x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001580
Yaowu Xuf883b422016-08-30 14:01:10 -07001581 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1582 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1583 aom_highbd_8_sub_pixel_variance32x32,
1584 aom_highbd_8_sub_pixel_avg_variance32x32,
1585 aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
1586 aom_highbd_sad32x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001587
Yaowu Xuf883b422016-08-30 14:01:10 -07001588 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1589 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1590 aom_highbd_8_sub_pixel_variance64x64,
1591 aom_highbd_8_sub_pixel_avg_variance64x64,
1592 aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
1593 aom_highbd_sad64x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001594
Yaowu Xuf883b422016-08-30 14:01:10 -07001595 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1596 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1597 aom_highbd_8_sub_pixel_variance16x16,
1598 aom_highbd_8_sub_pixel_avg_variance16x16,
1599 aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
1600 aom_highbd_sad16x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001601
1602 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001603 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1604 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1605 aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
1606 aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001607
1608 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001609 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1610 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1611 aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
1612 aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001613
1614 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001615 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1616 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1617 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits8,
1618 aom_highbd_sad8x8x8_bits8, aom_highbd_sad8x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001619
Yaowu Xuf883b422016-08-30 14:01:10 -07001620 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1621 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1622 aom_highbd_8_sub_pixel_variance8x4,
1623 aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
1624 aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001625
Yaowu Xuf883b422016-08-30 14:01:10 -07001626 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1627 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1628 aom_highbd_8_sub_pixel_variance4x8,
1629 aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
1630 aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001631
1632 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001633 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1634 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1635 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits8,
1636 aom_highbd_sad4x4x8_bits8, aom_highbd_sad4x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001637
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001638#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Hancc5bdf42016-12-19 11:14:30 -08001639 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_8_variance2x2, NULL, NULL,
1640 NULL, NULL, NULL)
1641 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_8_variance4x2, NULL, NULL,
1642 NULL, NULL, NULL)
1643 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_8_variance2x4, NULL, NULL,
1644 NULL, NULL, NULL)
1645#endif
1646
Yaowu Xuc27fc142016-08-22 16:08:15 -07001647#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001648 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1649 aom_highbd_sad128x128_avg_bits8,
1650 aom_highbd_8_variance128x128,
1651 aom_highbd_8_sub_pixel_variance128x128,
1652 aom_highbd_8_sub_pixel_avg_variance128x128,
1653 aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
1654 aom_highbd_sad128x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001655
Yaowu Xuf883b422016-08-30 14:01:10 -07001656 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1657 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1658 aom_highbd_8_sub_pixel_variance128x64,
1659 aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
1660 aom_highbd_sad128x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001661
Yaowu Xuf883b422016-08-30 14:01:10 -07001662 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1663 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1664 aom_highbd_8_sub_pixel_variance64x128,
1665 aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
1666 aom_highbd_sad64x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001667#endif // CONFIG_EXT_PARTITION
1668
Yaowu Xuc27fc142016-08-22 16:08:15 -07001669#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001670 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1671 aom_highbd_8_masked_sub_pixel_variance128x128)
1672 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1673 aom_highbd_8_masked_sub_pixel_variance128x64)
1674 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1675 aom_highbd_8_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001676#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001677 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1678 aom_highbd_8_masked_sub_pixel_variance64x64)
1679 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1680 aom_highbd_8_masked_sub_pixel_variance64x32)
1681 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1682 aom_highbd_8_masked_sub_pixel_variance32x64)
1683 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1684 aom_highbd_8_masked_sub_pixel_variance32x32)
1685 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1686 aom_highbd_8_masked_sub_pixel_variance32x16)
1687 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1688 aom_highbd_8_masked_sub_pixel_variance16x32)
1689 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1690 aom_highbd_8_masked_sub_pixel_variance16x16)
1691 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1692 aom_highbd_8_masked_sub_pixel_variance8x16)
1693 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1694 aom_highbd_8_masked_sub_pixel_variance16x8)
1695 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1696 aom_highbd_8_masked_sub_pixel_variance8x8)
1697 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1698 aom_highbd_8_masked_sub_pixel_variance4x8)
1699 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1700 aom_highbd_8_masked_sub_pixel_variance8x4)
1701 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1702 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001703#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001704#if CONFIG_EXT_PARTITION
1705 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits8,
1706 aom_highbd_8_masked_sub_pixel_variance128x32)
1707
1708 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits8,
1709 aom_highbd_8_masked_sub_pixel_variance32x128)
1710#endif // CONFIG_EXT_PARTITION
1711
Rupert Swarbrick72678572017-08-02 12:05:26 +01001712 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1713 aom_highbd_8_masked_sub_pixel_variance64x16)
1714
1715 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1716 aom_highbd_8_masked_sub_pixel_variance16x64)
1717
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001718 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1719 aom_highbd_8_masked_sub_pixel_variance32x8)
1720
1721 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1722 aom_highbd_8_masked_sub_pixel_variance8x32)
1723
1724 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1725 aom_highbd_8_masked_sub_pixel_variance16x4)
1726
1727 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1728 aom_highbd_8_masked_sub_pixel_variance4x16)
1729#endif
Yue Chencb60b182016-10-13 15:18:22 -07001730#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001731#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001732 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1733 aom_highbd_obmc_variance128x128,
1734 aom_highbd_obmc_sub_pixel_variance128x128)
1735 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1736 aom_highbd_obmc_variance128x64,
1737 aom_highbd_obmc_sub_pixel_variance128x64)
1738 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1739 aom_highbd_obmc_variance64x128,
1740 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001741#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001742 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1743 aom_highbd_obmc_variance64x64,
1744 aom_highbd_obmc_sub_pixel_variance64x64)
1745 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1746 aom_highbd_obmc_variance64x32,
1747 aom_highbd_obmc_sub_pixel_variance64x32)
1748 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1749 aom_highbd_obmc_variance32x64,
1750 aom_highbd_obmc_sub_pixel_variance32x64)
1751 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1752 aom_highbd_obmc_variance32x32,
1753 aom_highbd_obmc_sub_pixel_variance32x32)
1754 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1755 aom_highbd_obmc_variance32x16,
1756 aom_highbd_obmc_sub_pixel_variance32x16)
1757 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1758 aom_highbd_obmc_variance16x32,
1759 aom_highbd_obmc_sub_pixel_variance16x32)
1760 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1761 aom_highbd_obmc_variance16x16,
1762 aom_highbd_obmc_sub_pixel_variance16x16)
1763 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1764 aom_highbd_obmc_variance8x16,
1765 aom_highbd_obmc_sub_pixel_variance8x16)
1766 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1767 aom_highbd_obmc_variance16x8,
1768 aom_highbd_obmc_sub_pixel_variance16x8)
1769 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1770 aom_highbd_obmc_variance8x8,
1771 aom_highbd_obmc_sub_pixel_variance8x8)
1772 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1773 aom_highbd_obmc_variance4x8,
1774 aom_highbd_obmc_sub_pixel_variance4x8)
1775 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1776 aom_highbd_obmc_variance8x4,
1777 aom_highbd_obmc_sub_pixel_variance8x4)
1778 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1779 aom_highbd_obmc_variance4x4,
1780 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001781#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001782#if CONFIG_EXT_PARTITION
1783 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits8,
1784 aom_highbd_obmc_variance128x32,
1785 aom_highbd_obmc_sub_pixel_variance128x32)
1786
1787 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits8,
1788 aom_highbd_obmc_variance32x128,
1789 aom_highbd_obmc_sub_pixel_variance32x128)
1790#endif // CONFIG_EXT_PARTITION
1791
Rupert Swarbrick72678572017-08-02 12:05:26 +01001792 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1793 aom_highbd_obmc_variance64x16,
1794 aom_highbd_obmc_sub_pixel_variance64x16)
1795
1796 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1797 aom_highbd_obmc_variance16x64,
1798 aom_highbd_obmc_sub_pixel_variance16x64)
1799
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001800 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1801 aom_highbd_obmc_variance32x8,
1802 aom_highbd_obmc_sub_pixel_variance32x8)
1803
1804 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1805 aom_highbd_obmc_variance8x32,
1806 aom_highbd_obmc_sub_pixel_variance8x32)
1807
1808 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1809 aom_highbd_obmc_variance16x4,
1810 aom_highbd_obmc_sub_pixel_variance16x4)
1811
1812 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1813 aom_highbd_obmc_variance4x16,
1814 aom_highbd_obmc_sub_pixel_variance4x16)
1815#endif
Yue Chencb60b182016-10-13 15:18:22 -07001816#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001817 break;
1818
Yaowu Xuf883b422016-08-30 14:01:10 -07001819 case AOM_BITS_10:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001820#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001821#if CONFIG_EXT_PARTITION
1822 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits10,
1823 aom_highbd_sad128x32_avg_bits10,
1824 aom_highbd_10_variance128x32,
1825 aom_highbd_10_sub_pixel_variance128x32,
1826 aom_highbd_10_sub_pixel_avg_variance128x32, NULL, NULL,
1827 aom_highbd_sad128x32x4d_bits10)
1828
1829 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits10,
1830 aom_highbd_sad32x128_avg_bits10,
1831 aom_highbd_10_variance32x128,
1832 aom_highbd_10_sub_pixel_variance32x128,
1833 aom_highbd_10_sub_pixel_avg_variance32x128, NULL, NULL,
1834 aom_highbd_sad32x128x4d_bits10)
1835#endif // CONFIG_EXT_PARTITION
1836
Rupert Swarbrick72678572017-08-02 12:05:26 +01001837 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1838 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1839 aom_highbd_10_sub_pixel_variance64x16,
1840 aom_highbd_10_sub_pixel_avg_variance64x16, NULL, NULL,
1841 aom_highbd_sad64x16x4d_bits10)
1842
1843 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1844 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1845 aom_highbd_10_sub_pixel_variance16x64,
1846 aom_highbd_10_sub_pixel_avg_variance16x64, NULL, NULL,
1847 aom_highbd_sad16x64x4d_bits10)
1848
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001849 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1850 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1851 aom_highbd_10_sub_pixel_variance32x8,
1852 aom_highbd_10_sub_pixel_avg_variance32x8, NULL, NULL,
1853 aom_highbd_sad32x8x4d_bits10)
1854
1855 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1856 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1857 aom_highbd_10_sub_pixel_variance8x32,
1858 aom_highbd_10_sub_pixel_avg_variance8x32, NULL, NULL,
1859 aom_highbd_sad8x32x4d_bits10)
1860
1861 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1862 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1863 aom_highbd_10_sub_pixel_variance16x4,
1864 aom_highbd_10_sub_pixel_avg_variance16x4, NULL, NULL,
1865 aom_highbd_sad16x4x4d_bits10)
1866
1867 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1868 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1869 aom_highbd_10_sub_pixel_variance4x16,
1870 aom_highbd_10_sub_pixel_avg_variance4x16, NULL, NULL,
1871 aom_highbd_sad4x16x4d_bits10)
1872#endif
1873
Yaowu Xuf883b422016-08-30 14:01:10 -07001874 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1875 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1876 aom_highbd_10_sub_pixel_variance32x16,
1877 aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1878 aom_highbd_sad32x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001879
Yaowu Xuf883b422016-08-30 14:01:10 -07001880 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1881 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1882 aom_highbd_10_sub_pixel_variance16x32,
1883 aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1884 aom_highbd_sad16x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001885
Yaowu Xuf883b422016-08-30 14:01:10 -07001886 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1887 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1888 aom_highbd_10_sub_pixel_variance64x32,
1889 aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1890 aom_highbd_sad64x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001891
Yaowu Xuf883b422016-08-30 14:01:10 -07001892 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1893 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1894 aom_highbd_10_sub_pixel_variance32x64,
1895 aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1896 aom_highbd_sad32x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001897
Yaowu Xuf883b422016-08-30 14:01:10 -07001898 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1899 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1900 aom_highbd_10_sub_pixel_variance32x32,
1901 aom_highbd_10_sub_pixel_avg_variance32x32,
1902 aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
1903 aom_highbd_sad32x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001904
Yaowu Xuf883b422016-08-30 14:01:10 -07001905 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1906 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1907 aom_highbd_10_sub_pixel_variance64x64,
1908 aom_highbd_10_sub_pixel_avg_variance64x64,
1909 aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
1910 aom_highbd_sad64x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001911
Yaowu Xuf883b422016-08-30 14:01:10 -07001912 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1913 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1914 aom_highbd_10_sub_pixel_variance16x16,
1915 aom_highbd_10_sub_pixel_avg_variance16x16,
1916 aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
1917 aom_highbd_sad16x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001918
Yaowu Xuf883b422016-08-30 14:01:10 -07001919 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1920 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1921 aom_highbd_10_sub_pixel_variance16x8,
1922 aom_highbd_10_sub_pixel_avg_variance16x8,
1923 aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
1924 aom_highbd_sad16x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001925
Yaowu Xuf883b422016-08-30 14:01:10 -07001926 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1927 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1928 aom_highbd_10_sub_pixel_variance8x16,
1929 aom_highbd_10_sub_pixel_avg_variance8x16,
1930 aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
1931 aom_highbd_sad8x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001932
1933 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001934 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1935 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1936 aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
1937 aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001938
Yaowu Xuf883b422016-08-30 14:01:10 -07001939 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
1940 aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
1941 aom_highbd_10_sub_pixel_variance8x4,
1942 aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
1943 aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001944
Yaowu Xuf883b422016-08-30 14:01:10 -07001945 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
1946 aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
1947 aom_highbd_10_sub_pixel_variance4x8,
1948 aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
1949 aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001950
1951 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001952 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1953 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1954 aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
1955 aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001956
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001957#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Hancc5bdf42016-12-19 11:14:30 -08001958 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_10_variance2x2, NULL, NULL,
1959 NULL, NULL, NULL)
1960 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_10_variance4x2, NULL, NULL,
1961 NULL, NULL, NULL)
1962 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_10_variance2x4, NULL, NULL,
1963 NULL, NULL, NULL)
1964#endif
1965
Yaowu Xuc27fc142016-08-22 16:08:15 -07001966#if CONFIG_EXT_PARTITION
1967 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001968 BLOCK_128X128, aom_highbd_sad128x128_bits10,
1969 aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
1970 aom_highbd_10_sub_pixel_variance128x128,
1971 aom_highbd_10_sub_pixel_avg_variance128x128,
1972 aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
1973 aom_highbd_sad128x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001974
Yaowu Xuf883b422016-08-30 14:01:10 -07001975 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1976 aom_highbd_sad128x64_avg_bits10,
1977 aom_highbd_10_variance128x64,
1978 aom_highbd_10_sub_pixel_variance128x64,
1979 aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
1980 aom_highbd_sad128x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001981
Yaowu Xuf883b422016-08-30 14:01:10 -07001982 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1983 aom_highbd_sad64x128_avg_bits10,
1984 aom_highbd_10_variance64x128,
1985 aom_highbd_10_sub_pixel_variance64x128,
1986 aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
1987 aom_highbd_sad64x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988#endif // CONFIG_EXT_PARTITION
1989
Yaowu Xuc27fc142016-08-22 16:08:15 -07001990#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001991 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1992 aom_highbd_10_masked_sub_pixel_variance128x128)
1993 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1994 aom_highbd_10_masked_sub_pixel_variance128x64)
1995 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1996 aom_highbd_10_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001997#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001998 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1999 aom_highbd_10_masked_sub_pixel_variance64x64)
2000 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
2001 aom_highbd_10_masked_sub_pixel_variance64x32)
2002 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
2003 aom_highbd_10_masked_sub_pixel_variance32x64)
2004 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
2005 aom_highbd_10_masked_sub_pixel_variance32x32)
2006 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
2007 aom_highbd_10_masked_sub_pixel_variance32x16)
2008 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
2009 aom_highbd_10_masked_sub_pixel_variance16x32)
2010 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
2011 aom_highbd_10_masked_sub_pixel_variance16x16)
2012 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
2013 aom_highbd_10_masked_sub_pixel_variance8x16)
2014 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
2015 aom_highbd_10_masked_sub_pixel_variance16x8)
2016 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
2017 aom_highbd_10_masked_sub_pixel_variance8x8)
2018 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2019 aom_highbd_10_masked_sub_pixel_variance4x8)
2020 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2021 aom_highbd_10_masked_sub_pixel_variance8x4)
2022 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2023 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002024#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002025#if CONFIG_EXT_PARTITION
2026 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits10,
2027 aom_highbd_10_masked_sub_pixel_variance128x32)
2028
2029 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits10,
2030 aom_highbd_10_masked_sub_pixel_variance32x128)
2031#endif // CONFIG_EXT_PARTITION
2032
Rupert Swarbrick72678572017-08-02 12:05:26 +01002033 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2034 aom_highbd_10_masked_sub_pixel_variance64x16)
2035
2036 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2037 aom_highbd_10_masked_sub_pixel_variance16x64)
2038
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002039 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2040 aom_highbd_10_masked_sub_pixel_variance32x8)
2041
2042 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2043 aom_highbd_10_masked_sub_pixel_variance8x32)
2044
2045 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2046 aom_highbd_10_masked_sub_pixel_variance16x4)
2047
2048 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2049 aom_highbd_10_masked_sub_pixel_variance4x16)
2050#endif
Yue Chencb60b182016-10-13 15:18:22 -07002051#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002052#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002053 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2054 aom_highbd_10_obmc_variance128x128,
2055 aom_highbd_10_obmc_sub_pixel_variance128x128)
2056 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2057 aom_highbd_10_obmc_variance128x64,
2058 aom_highbd_10_obmc_sub_pixel_variance128x64)
2059 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2060 aom_highbd_10_obmc_variance64x128,
2061 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002062#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002063 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2064 aom_highbd_10_obmc_variance64x64,
2065 aom_highbd_10_obmc_sub_pixel_variance64x64)
2066 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2067 aom_highbd_10_obmc_variance64x32,
2068 aom_highbd_10_obmc_sub_pixel_variance64x32)
2069 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2070 aom_highbd_10_obmc_variance32x64,
2071 aom_highbd_10_obmc_sub_pixel_variance32x64)
2072 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2073 aom_highbd_10_obmc_variance32x32,
2074 aom_highbd_10_obmc_sub_pixel_variance32x32)
2075 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2076 aom_highbd_10_obmc_variance32x16,
2077 aom_highbd_10_obmc_sub_pixel_variance32x16)
2078 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2079 aom_highbd_10_obmc_variance16x32,
2080 aom_highbd_10_obmc_sub_pixel_variance16x32)
2081 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2082 aom_highbd_10_obmc_variance16x16,
2083 aom_highbd_10_obmc_sub_pixel_variance16x16)
2084 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2085 aom_highbd_10_obmc_variance8x16,
2086 aom_highbd_10_obmc_sub_pixel_variance8x16)
2087 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2088 aom_highbd_10_obmc_variance16x8,
2089 aom_highbd_10_obmc_sub_pixel_variance16x8)
2090 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2091 aom_highbd_10_obmc_variance8x8,
2092 aom_highbd_10_obmc_sub_pixel_variance8x8)
2093 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2094 aom_highbd_10_obmc_variance4x8,
2095 aom_highbd_10_obmc_sub_pixel_variance4x8)
2096 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2097 aom_highbd_10_obmc_variance8x4,
2098 aom_highbd_10_obmc_sub_pixel_variance8x4)
2099 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2100 aom_highbd_10_obmc_variance4x4,
2101 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002102#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002103#if CONFIG_EXT_PARTITION
2104 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits10,
2105 aom_highbd_10_obmc_variance128x32,
2106 aom_highbd_10_obmc_sub_pixel_variance128x32)
2107
2108 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits10,
2109 aom_highbd_10_obmc_variance32x128,
2110 aom_highbd_10_obmc_sub_pixel_variance32x128)
2111#endif // CONFIG_EXT_PARTITION
2112
Rupert Swarbrick72678572017-08-02 12:05:26 +01002113 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2114 aom_highbd_10_obmc_variance64x16,
2115 aom_highbd_10_obmc_sub_pixel_variance64x16)
2116
2117 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2118 aom_highbd_10_obmc_variance16x64,
2119 aom_highbd_10_obmc_sub_pixel_variance16x64)
2120
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002121 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2122 aom_highbd_10_obmc_variance32x8,
2123 aom_highbd_10_obmc_sub_pixel_variance32x8)
2124
2125 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2126 aom_highbd_10_obmc_variance8x32,
2127 aom_highbd_10_obmc_sub_pixel_variance8x32)
2128
2129 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2130 aom_highbd_10_obmc_variance16x4,
2131 aom_highbd_10_obmc_sub_pixel_variance16x4)
2132
2133 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2134 aom_highbd_10_obmc_variance4x16,
2135 aom_highbd_10_obmc_sub_pixel_variance4x16)
2136#endif
Yue Chencb60b182016-10-13 15:18:22 -07002137#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002138 break;
2139
Yaowu Xuf883b422016-08-30 14:01:10 -07002140 case AOM_BITS_12:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002141#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002142#if CONFIG_EXT_PARTITION
2143 HIGHBD_BFP(BLOCK_128X32, aom_highbd_sad128x32_bits12,
2144 aom_highbd_sad128x32_avg_bits12,
2145 aom_highbd_12_variance128x32,
2146 aom_highbd_12_sub_pixel_variance128x32,
2147 aom_highbd_12_sub_pixel_avg_variance128x32, NULL, NULL,
2148 aom_highbd_sad128x32x4d_bits12)
2149
2150 HIGHBD_BFP(BLOCK_32X128, aom_highbd_sad32x128_bits12,
2151 aom_highbd_sad32x128_avg_bits12,
2152 aom_highbd_12_variance32x128,
2153 aom_highbd_12_sub_pixel_variance32x128,
2154 aom_highbd_12_sub_pixel_avg_variance32x128, NULL, NULL,
2155 aom_highbd_sad32x128x4d_bits12)
2156#endif // CONFIG_EXT_PARTITION
2157
Rupert Swarbrick72678572017-08-02 12:05:26 +01002158 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2159 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2160 aom_highbd_12_sub_pixel_variance64x16,
2161 aom_highbd_12_sub_pixel_avg_variance64x16, NULL, NULL,
2162 aom_highbd_sad64x16x4d_bits12)
2163
2164 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2165 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2166 aom_highbd_12_sub_pixel_variance16x64,
2167 aom_highbd_12_sub_pixel_avg_variance16x64, NULL, NULL,
2168 aom_highbd_sad16x64x4d_bits12)
2169
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002170 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2171 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2172 aom_highbd_12_sub_pixel_variance32x8,
2173 aom_highbd_12_sub_pixel_avg_variance32x8, NULL, NULL,
2174 aom_highbd_sad32x8x4d_bits12)
2175
2176 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2177 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2178 aom_highbd_12_sub_pixel_variance8x32,
2179 aom_highbd_12_sub_pixel_avg_variance8x32, NULL, NULL,
2180 aom_highbd_sad8x32x4d_bits12)
2181
2182 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2183 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2184 aom_highbd_12_sub_pixel_variance16x4,
2185 aom_highbd_12_sub_pixel_avg_variance16x4, NULL, NULL,
2186 aom_highbd_sad16x4x4d_bits12)
2187
2188 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2189 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2190 aom_highbd_12_sub_pixel_variance4x16,
2191 aom_highbd_12_sub_pixel_avg_variance4x16, NULL, NULL,
2192 aom_highbd_sad4x16x4d_bits12)
2193#endif
2194
Yaowu Xuf883b422016-08-30 14:01:10 -07002195 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2196 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2197 aom_highbd_12_sub_pixel_variance32x16,
2198 aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
2199 aom_highbd_sad32x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002200
Yaowu Xuf883b422016-08-30 14:01:10 -07002201 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2202 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2203 aom_highbd_12_sub_pixel_variance16x32,
2204 aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
2205 aom_highbd_sad16x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002206
Yaowu Xuf883b422016-08-30 14:01:10 -07002207 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2208 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2209 aom_highbd_12_sub_pixel_variance64x32,
2210 aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
2211 aom_highbd_sad64x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002212
Yaowu Xuf883b422016-08-30 14:01:10 -07002213 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2214 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2215 aom_highbd_12_sub_pixel_variance32x64,
2216 aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
2217 aom_highbd_sad32x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002218
Yaowu Xuf883b422016-08-30 14:01:10 -07002219 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2220 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2221 aom_highbd_12_sub_pixel_variance32x32,
2222 aom_highbd_12_sub_pixel_avg_variance32x32,
2223 aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
2224 aom_highbd_sad32x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002225
Yaowu Xuf883b422016-08-30 14:01:10 -07002226 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2227 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2228 aom_highbd_12_sub_pixel_variance64x64,
2229 aom_highbd_12_sub_pixel_avg_variance64x64,
2230 aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
2231 aom_highbd_sad64x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002232
Yaowu Xuf883b422016-08-30 14:01:10 -07002233 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2234 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2235 aom_highbd_12_sub_pixel_variance16x16,
2236 aom_highbd_12_sub_pixel_avg_variance16x16,
2237 aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
2238 aom_highbd_sad16x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002239
Yaowu Xuf883b422016-08-30 14:01:10 -07002240 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2241 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2242 aom_highbd_12_sub_pixel_variance16x8,
2243 aom_highbd_12_sub_pixel_avg_variance16x8,
2244 aom_highbd_sad16x8x3_bits12, aom_highbd_sad16x8x8_bits12,
2245 aom_highbd_sad16x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002246
Yaowu Xuf883b422016-08-30 14:01:10 -07002247 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2248 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2249 aom_highbd_12_sub_pixel_variance8x16,
2250 aom_highbd_12_sub_pixel_avg_variance8x16,
2251 aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
2252 aom_highbd_sad8x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002253
2254 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002255 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2256 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
2257 aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
2258 aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259
Yaowu Xuf883b422016-08-30 14:01:10 -07002260 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
2261 aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
2262 aom_highbd_12_sub_pixel_variance8x4,
2263 aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
2264 aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265
Yaowu Xuf883b422016-08-30 14:01:10 -07002266 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
2267 aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
2268 aom_highbd_12_sub_pixel_variance4x8,
2269 aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
2270 aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002271
2272 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002273 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2274 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
2275 aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
2276 aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002277
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07002278#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Hancc5bdf42016-12-19 11:14:30 -08002279 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_12_variance2x2, NULL, NULL,
2280 NULL, NULL, NULL)
2281 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_12_variance4x2, NULL, NULL,
2282 NULL, NULL, NULL)
2283 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_12_variance2x4, NULL, NULL,
2284 NULL, NULL, NULL)
2285#endif
2286
Yaowu Xuc27fc142016-08-22 16:08:15 -07002287#if CONFIG_EXT_PARTITION
2288 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002289 BLOCK_128X128, aom_highbd_sad128x128_bits12,
2290 aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
2291 aom_highbd_12_sub_pixel_variance128x128,
2292 aom_highbd_12_sub_pixel_avg_variance128x128,
2293 aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
2294 aom_highbd_sad128x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002295
Yaowu Xuf883b422016-08-30 14:01:10 -07002296 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2297 aom_highbd_sad128x64_avg_bits12,
2298 aom_highbd_12_variance128x64,
2299 aom_highbd_12_sub_pixel_variance128x64,
2300 aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
2301 aom_highbd_sad128x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002302
Yaowu Xuf883b422016-08-30 14:01:10 -07002303 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2304 aom_highbd_sad64x128_avg_bits12,
2305 aom_highbd_12_variance64x128,
2306 aom_highbd_12_sub_pixel_variance64x128,
2307 aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
2308 aom_highbd_sad64x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002309#endif // CONFIG_EXT_PARTITION
2310
Yaowu Xuc27fc142016-08-22 16:08:15 -07002311#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002312 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2313 aom_highbd_12_masked_sub_pixel_variance128x128)
2314 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2315 aom_highbd_12_masked_sub_pixel_variance128x64)
2316 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2317 aom_highbd_12_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002318#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002319 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2320 aom_highbd_12_masked_sub_pixel_variance64x64)
2321 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2322 aom_highbd_12_masked_sub_pixel_variance64x32)
2323 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2324 aom_highbd_12_masked_sub_pixel_variance32x64)
2325 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2326 aom_highbd_12_masked_sub_pixel_variance32x32)
2327 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2328 aom_highbd_12_masked_sub_pixel_variance32x16)
2329 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2330 aom_highbd_12_masked_sub_pixel_variance16x32)
2331 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2332 aom_highbd_12_masked_sub_pixel_variance16x16)
2333 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2334 aom_highbd_12_masked_sub_pixel_variance8x16)
2335 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2336 aom_highbd_12_masked_sub_pixel_variance16x8)
2337 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2338 aom_highbd_12_masked_sub_pixel_variance8x8)
2339 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2340 aom_highbd_12_masked_sub_pixel_variance4x8)
2341 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2342 aom_highbd_12_masked_sub_pixel_variance8x4)
2343 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2344 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002345#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002346#if CONFIG_EXT_PARTITION
2347 HIGHBD_MBFP(BLOCK_128X32, aom_highbd_masked_sad128x32_bits12,
2348 aom_highbd_12_masked_sub_pixel_variance128x32)
2349
2350 HIGHBD_MBFP(BLOCK_32X128, aom_highbd_masked_sad32x128_bits12,
2351 aom_highbd_12_masked_sub_pixel_variance32x128)
2352#endif // CONFIG_EXT_PARTITION
2353
Rupert Swarbrick72678572017-08-02 12:05:26 +01002354 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2355 aom_highbd_12_masked_sub_pixel_variance64x16)
2356
2357 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2358 aom_highbd_12_masked_sub_pixel_variance16x64)
2359
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002360 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2361 aom_highbd_12_masked_sub_pixel_variance32x8)
2362
2363 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2364 aom_highbd_12_masked_sub_pixel_variance8x32)
2365
2366 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2367 aom_highbd_12_masked_sub_pixel_variance16x4)
2368
2369 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2370 aom_highbd_12_masked_sub_pixel_variance4x16)
2371#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002372
Yue Chencb60b182016-10-13 15:18:22 -07002373#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002374#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002375 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2376 aom_highbd_12_obmc_variance128x128,
2377 aom_highbd_12_obmc_sub_pixel_variance128x128)
2378 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2379 aom_highbd_12_obmc_variance128x64,
2380 aom_highbd_12_obmc_sub_pixel_variance128x64)
2381 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2382 aom_highbd_12_obmc_variance64x128,
2383 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002384#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002385 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2386 aom_highbd_12_obmc_variance64x64,
2387 aom_highbd_12_obmc_sub_pixel_variance64x64)
2388 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2389 aom_highbd_12_obmc_variance64x32,
2390 aom_highbd_12_obmc_sub_pixel_variance64x32)
2391 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2392 aom_highbd_12_obmc_variance32x64,
2393 aom_highbd_12_obmc_sub_pixel_variance32x64)
2394 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2395 aom_highbd_12_obmc_variance32x32,
2396 aom_highbd_12_obmc_sub_pixel_variance32x32)
2397 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2398 aom_highbd_12_obmc_variance32x16,
2399 aom_highbd_12_obmc_sub_pixel_variance32x16)
2400 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2401 aom_highbd_12_obmc_variance16x32,
2402 aom_highbd_12_obmc_sub_pixel_variance16x32)
2403 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2404 aom_highbd_12_obmc_variance16x16,
2405 aom_highbd_12_obmc_sub_pixel_variance16x16)
2406 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2407 aom_highbd_12_obmc_variance8x16,
2408 aom_highbd_12_obmc_sub_pixel_variance8x16)
2409 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2410 aom_highbd_12_obmc_variance16x8,
2411 aom_highbd_12_obmc_sub_pixel_variance16x8)
2412 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2413 aom_highbd_12_obmc_variance8x8,
2414 aom_highbd_12_obmc_sub_pixel_variance8x8)
2415 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2416 aom_highbd_12_obmc_variance4x8,
2417 aom_highbd_12_obmc_sub_pixel_variance4x8)
2418 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2419 aom_highbd_12_obmc_variance8x4,
2420 aom_highbd_12_obmc_sub_pixel_variance8x4)
2421 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2422 aom_highbd_12_obmc_variance4x4,
2423 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002424#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002425#if CONFIG_EXT_PARTITION
2426 HIGHBD_OBFP(BLOCK_128X32, aom_highbd_obmc_sad128x32_bits12,
2427 aom_highbd_12_obmc_variance128x32,
2428 aom_highbd_12_obmc_sub_pixel_variance128x32)
2429
2430 HIGHBD_OBFP(BLOCK_32X128, aom_highbd_obmc_sad32x128_bits12,
2431 aom_highbd_12_obmc_variance32x128,
2432 aom_highbd_12_obmc_sub_pixel_variance32x128)
2433#endif // CONFIG_EXT_PARTITION
2434
Rupert Swarbrick72678572017-08-02 12:05:26 +01002435 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2436 aom_highbd_12_obmc_variance64x16,
2437 aom_highbd_12_obmc_sub_pixel_variance64x16)
2438
2439 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2440 aom_highbd_12_obmc_variance16x64,
2441 aom_highbd_12_obmc_sub_pixel_variance16x64)
2442
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002443 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2444 aom_highbd_12_obmc_variance32x8,
2445 aom_highbd_12_obmc_sub_pixel_variance32x8)
2446
2447 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2448 aom_highbd_12_obmc_variance8x32,
2449 aom_highbd_12_obmc_sub_pixel_variance8x32)
2450
2451 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2452 aom_highbd_12_obmc_variance16x4,
2453 aom_highbd_12_obmc_sub_pixel_variance16x4)
2454
2455 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2456 aom_highbd_12_obmc_variance4x16,
2457 aom_highbd_12_obmc_sub_pixel_variance4x16)
2458#endif
Yue Chencb60b182016-10-13 15:18:22 -07002459#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002460 break;
2461
2462 default:
2463 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07002464 "cm->bit_depth should be AOM_BITS_8, "
2465 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002466 }
2467 }
2468}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002469#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002470
Yaowu Xuf883b422016-08-30 14:01:10 -07002471static void realloc_segmentation_maps(AV1_COMP *cpi) {
2472 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002473
2474 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002475 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002476 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002477 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002478
2479 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002480 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002481 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002482 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002483
2484 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002485 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002486 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002487 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002488}
2489
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002490void set_compound_tools(AV1_COMMON *cm) {
2491 (void)cm;
2492#if CONFIG_INTERINTRA
2493 cm->allow_interintra_compound = 1;
2494#endif // CONFIG_INTERINTRA
2495#if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
2496 cm->allow_masked_compound = 1;
2497#endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
2498}
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002499
Yaowu Xuf883b422016-08-30 14:01:10 -07002500void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2501 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002502 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002503 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002504
2505 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
2506 cm->bit_depth = oxcf->bit_depth;
2507 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07002508#if CONFIG_COLORSPACE_HEADERS
2509 cm->transfer_function = oxcf->transfer_function;
2510 cm->chroma_sample_position = oxcf->chroma_sample_position;
2511#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002512 cm->color_range = oxcf->color_range;
2513
2514 if (cm->profile <= PROFILE_1)
Yaowu Xuf883b422016-08-30 14:01:10 -07002515 assert(cm->bit_depth == AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002516 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002517 assert(cm->bit_depth > AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002518
2519 cpi->oxcf = *oxcf;
hui sud9a812b2017-07-06 14:34:37 -07002520 x->e_mbd.bd = (int)cm->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002521#if CONFIG_GLOBAL_MOTION
hui sud9a812b2017-07-06 14:34:37 -07002522 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002523#endif // CONFIG_GLOBAL_MOTION
2524
Yaowu Xuf883b422016-08-30 14:01:10 -07002525 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002526 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2527 } else {
2528 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2529 }
2530
2531 cpi->refresh_last_frame = 1;
2532 cpi->refresh_golden_frame = 0;
2533#if CONFIG_EXT_REFS
2534 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002535 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002536#endif // CONFIG_EXT_REFS
2537
2538 cm->refresh_frame_context =
2539 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
2540 ? REFRESH_FRAME_CONTEXT_FORWARD
2541 : REFRESH_FRAME_CONTEXT_BACKWARD;
Thomas Daedea6a854b2017-06-22 17:49:11 -07002542#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07002543 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07002544#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002545
Alex Converse74ad0912017-07-18 10:22:58 -07002546 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002547 CHECK_MEM_ERROR(cm, x->palette_buffer,
2548 aom_memalign(16, sizeof(*x->palette_buffer)));
2549 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002550 set_compound_tools(cm);
Yaowu Xuf883b422016-08-30 14:01:10 -07002551 av1_reset_segment_features(cm);
RogerZhou3b635242017-09-19 10:06:46 -07002552#if CONFIG_AMVR
2553 set_high_precision_mv(cpi, 0, 0);
2554#else
Cheng Chen46f30c72017-09-07 11:13:33 -07002555 set_high_precision_mv(cpi, 0);
RogerZhou3b635242017-09-19 10:06:46 -07002556#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002557
Yaowu Xuc27fc142016-08-22 16:08:15 -07002558 set_rc_buffer_sizes(rc, &cpi->oxcf);
2559
2560 // Under a configuration change, where maximum_buffer_size may change,
2561 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002562 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2563 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002564
2565 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002566 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002567
2568 // Set absolute upper and lower quality limits
2569 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2570 rc->best_quality = cpi->oxcf.best_allowed_q;
2571
2572 cm->interp_filter = cpi->sf.default_interp_filter;
2573
2574 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2575 cm->render_width = cpi->oxcf.render_width;
2576 cm->render_height = cpi->oxcf.render_height;
2577 } else {
2578 cm->render_width = cpi->oxcf.width;
2579 cm->render_height = cpi->oxcf.height;
2580 }
2581 cm->width = cpi->oxcf.width;
2582 cm->height = cpi->oxcf.height;
2583
2584 if (cpi->initial_width) {
2585 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002586 av1_free_context_buffers(cm);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07002587 av1_free_pc_tree(&cpi->td);
Cheng Chen46f30c72017-09-07 11:13:33 -07002588 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002589 realloc_segmentation_maps(cpi);
2590 cpi->initial_width = cpi->initial_height = 0;
2591 }
2592 }
2593 update_frame_size(cpi);
2594
2595 cpi->alt_ref_source = NULL;
2596 rc->is_src_frame_alt_ref = 0;
2597
2598#if CONFIG_EXT_REFS
2599 rc->is_bwd_ref_frame = 0;
2600 rc->is_last_bipred_frame = 0;
2601 rc->is_bipred_frame = 0;
2602#endif // CONFIG_EXT_REFS
2603
2604#if 0
2605 // Experimental RD Code
2606 cpi->frame_distortion = 0;
2607 cpi->last_frame_distortion = 0;
2608#endif
2609
2610 set_tile_info(cpi);
2611
2612 cpi->ext_refresh_frame_flags_pending = 0;
2613 cpi->ext_refresh_frame_context_pending = 0;
2614
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002615#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002616 highbd_set_var_fns(cpi);
2617#endif
Alex Converseeb780e72016-12-13 12:46:41 -08002618#if CONFIG_ANS && ANS_MAX_SYMBOLS
2619 cpi->common.ans_window_size_log2 = cpi->oxcf.ans_window_size_log2;
Alex Converseeb780e72016-12-13 12:46:41 -08002620#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
RogerZhou3b635242017-09-19 10:06:46 -07002621#if CONFIG_AMVR
2622 cm->seq_mv_precision_level = 2;
2623#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002624}
2625
Yaowu Xuf883b422016-08-30 14:01:10 -07002626AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2627 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002628 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002629 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2630 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631
2632 if (!cm) return NULL;
2633
Yaowu Xuf883b422016-08-30 14:01:10 -07002634 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002635
2636 if (setjmp(cm->error.jmp)) {
2637 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002638 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639 return 0;
2640 }
2641
2642 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002643 cm->alloc_mi = enc_alloc_mi;
2644 cm->free_mi = enc_free_mi;
2645 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002646
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07002647#if CONFIG_NCOBMC_ADAPT_WEIGHT
2648 get_default_ncobmc_kernels(cm);
2649#endif // CONFIG_NCOBMC_ADAPT_WEIGHT
2650
Angie Chianga5d96c42016-10-21 16:16:56 -07002651 CHECK_MEM_ERROR(cm, cm->fc,
2652 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2653 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2654 (FRAME_CONTEXT *)aom_memalign(
2655 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2656 memset(cm->fc, 0, sizeof(*cm->fc));
2657 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002658
2659 cpi->resize_state = 0;
2660 cpi->resize_avg_qp = 0;
2661 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002662
Yaowu Xuc27fc142016-08-22 16:08:15 -07002663 cpi->common.buffer_pool = pool;
2664
2665 init_config(cpi, oxcf);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002666#if CONFIG_XIPHRC
2667 cpi->od_rc.framerate = cpi->framerate;
2668 cpi->od_rc.frame_width = cm->render_width;
2669 cpi->od_rc.frame_height = cm->render_height;
2670 cpi->od_rc.keyframe_rate = oxcf->key_freq;
2671 cpi->od_rc.goldenframe_rate = FIXED_GF_INTERVAL;
2672 cpi->od_rc.altref_rate = 25;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002673 cpi->od_rc.firstpass_quant = 1;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002674 cpi->od_rc.bit_depth = cm->bit_depth;
2675 cpi->od_rc.minq = oxcf->best_allowed_q;
2676 cpi->od_rc.maxq = oxcf->worst_allowed_q;
2677 if (cpi->oxcf.rc_mode == AOM_CQ) cpi->od_rc.minq = cpi->od_rc.quality;
2678 cpi->od_rc.quality = cpi->oxcf.rc_mode == AOM_Q ? oxcf->cq_level : -1;
2679 cpi->od_rc.periodic_boosts = oxcf->frame_periodic_boost;
2680 od_enc_rc_init(&cpi->od_rc,
2681 cpi->oxcf.rc_mode == AOM_Q ? -1 : oxcf->target_bandwidth,
2682 oxcf->maximum_buffer_size_ms);
2683#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002684 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002685#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002686
2687 cm->current_video_frame = 0;
2688 cpi->partition_search_skippable_frame = 0;
2689 cpi->tile_data = NULL;
2690 cpi->last_show_frame_buf_idx = INVALID_IDX;
2691
2692 realloc_segmentation_maps(cpi);
2693
James Zern01a9d702017-08-25 19:09:33 +00002694 for (i = 0; i < NMV_CONTEXTS; ++i) {
2695 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2696 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
2697 }
2698
Yaowu Xuc27fc142016-08-22 16:08:15 -07002699 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2700 i++) {
2701 CHECK_MEM_ERROR(
2702 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002703 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002704 }
2705
2706#if CONFIG_FP_MB_STATS
2707 cpi->use_fp_mb_stats = 0;
2708 if (cpi->use_fp_mb_stats) {
2709 // a place holder used to store the first pass mb stats in the first pass
2710 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002711 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002712 } else {
2713 cpi->twopass.frame_mb_stats_buf = NULL;
2714 }
2715#endif
2716
2717 cpi->refresh_alt_ref_frame = 0;
2718 cpi->multi_arf_last_grp_enabled = 0;
2719
2720 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2721#if CONFIG_INTERNAL_STATS
2722 cpi->b_calculate_blockiness = 1;
2723 cpi->b_calculate_consistency = 1;
2724 cpi->total_inconsistency = 0;
2725 cpi->psnr.worst = 100.0;
2726 cpi->worst_ssim = 100.0;
2727
2728 cpi->count = 0;
2729 cpi->bytes = 0;
2730
2731 if (cpi->b_calculate_psnr) {
2732 cpi->total_sq_error = 0;
2733 cpi->total_samples = 0;
2734 cpi->tot_recode_hits = 0;
2735 cpi->summed_quality = 0;
2736 cpi->summed_weights = 0;
2737 }
2738
2739 cpi->fastssim.worst = 100.0;
2740 cpi->psnrhvs.worst = 100.0;
2741
2742 if (cpi->b_calculate_blockiness) {
2743 cpi->total_blockiness = 0;
2744 cpi->worst_blockiness = 0.0;
2745 }
2746
2747 if (cpi->b_calculate_consistency) {
2748 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002749 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002750 cpi->common.mi_rows * cpi->common.mi_cols));
2751 cpi->worst_consistency = 100.0;
2752 }
2753#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002754#if CONFIG_ENTROPY_STATS
2755 av1_zero(aggregate_fc);
Zoe Liua56f9162017-06-21 22:49:57 -07002756 av1_zero_array(aggregate_fc_per_type, FRAME_CONTEXTS);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002757#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002758
2759 cpi->first_time_stamp_ever = INT64_MAX;
2760
James Zern01a9d702017-08-25 19:09:33 +00002761 for (i = 0; i < NMV_CONTEXTS; ++i) {
2762 cpi->td.mb.nmvcost[i][0] = &cpi->nmv_costs[i][0][MV_MAX];
2763 cpi->td.mb.nmvcost[i][1] = &cpi->nmv_costs[i][1][MV_MAX];
2764 cpi->td.mb.nmvcost_hp[i][0] = &cpi->nmv_costs_hp[i][0][MV_MAX];
2765 cpi->td.mb.nmvcost_hp[i][1] = &cpi->nmv_costs_hp[i][1][MV_MAX];
2766 }
2767
Yaowu Xuc27fc142016-08-22 16:08:15 -07002768#ifdef OUTPUT_YUV_SKINMAP
2769 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2770#endif
2771#ifdef OUTPUT_YUV_REC
2772 yuv_rec_file = fopen("rec.yuv", "wb");
2773#endif
2774
2775#if 0
2776 framepsnr = fopen("framepsnr.stt", "a");
2777 kf_list = fopen("kf_list.stt", "w");
2778#endif
2779
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002780#if CONFIG_XIPHRC
2781 if (oxcf->pass == 2) {
2782 cpi->od_rc.twopass_allframes_buf = oxcf->two_pass_stats_in.buf;
2783 cpi->od_rc.twopass_allframes_buf_size = oxcf->two_pass_stats_in.sz;
2784 }
2785#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002786 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002787 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002788 } else if (oxcf->pass == 2) {
2789 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2790 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2791
2792#if CONFIG_FP_MB_STATS
2793 if (cpi->use_fp_mb_stats) {
2794 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2795 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2796
2797 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2798 oxcf->firstpass_mb_stats_in.buf;
2799 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2800 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2801 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2802 }
2803#endif
2804
2805 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2806 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2807 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2808
Yaowu Xuf883b422016-08-30 14:01:10 -07002809 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002810 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002811#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002812
Jingning Hand064cf02017-06-01 10:00:39 -07002813#if CONFIG_MOTION_VAR
2814#if CONFIG_HIGHBITDEPTH
2815 int buf_scaler = 2;
2816#else
2817 int buf_scaler = 1;
2818#endif
2819 CHECK_MEM_ERROR(
2820 cm, cpi->td.mb.above_pred_buf,
clang-format4eafefe2017-09-04 12:51:20 -07002821 (uint8_t *)aom_memalign(16,
2822 buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2823 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002824 CHECK_MEM_ERROR(
2825 cm, cpi->td.mb.left_pred_buf,
clang-format4eafefe2017-09-04 12:51:20 -07002826 (uint8_t *)aom_memalign(16,
2827 buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2828 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002829
2830 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2831 (int32_t *)aom_memalign(
2832 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2833
2834 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2835 (int32_t *)aom_memalign(
2836 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2837
2838#endif
2839
Yaowu Xuf883b422016-08-30 14:01:10 -07002840 av1_set_speed_features_framesize_independent(cpi);
2841 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002842
Yaowu Xuc27fc142016-08-22 16:08:15 -07002843#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
2844 cpi->fn_ptr[BT].sdf = SDF; \
2845 cpi->fn_ptr[BT].sdaf = SDAF; \
2846 cpi->fn_ptr[BT].vf = VF; \
2847 cpi->fn_ptr[BT].svf = SVF; \
2848 cpi->fn_ptr[BT].svaf = SVAF; \
2849 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2850 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2851 cpi->fn_ptr[BT].sdx4df = SDX4DF;
2852
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002853#if CONFIG_EXT_PARTITION_TYPES
2854 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2855 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, NULL, NULL,
2856 aom_sad4x16x4d)
2857
2858 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2859 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, NULL, NULL,
2860 aom_sad16x4x4d)
2861
2862 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2863 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, NULL, NULL,
2864 aom_sad8x32x4d)
2865
2866 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2867 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, NULL, NULL,
2868 aom_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002869
2870 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2871 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, NULL, NULL,
2872 aom_sad16x64x4d)
2873
2874 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2875 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, NULL, NULL,
2876 aom_sad64x16x4d)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002877
2878#if CONFIG_EXT_PARTITION
2879 BFP(BLOCK_32X128, aom_sad32x128, aom_sad32x128_avg, aom_variance32x128,
2880 aom_sub_pixel_variance32x128, aom_sub_pixel_avg_variance32x128, NULL,
2881 NULL, aom_sad32x128x4d)
2882
2883 BFP(BLOCK_128X32, aom_sad128x32, aom_sad128x32_avg, aom_variance128x32,
2884 aom_sub_pixel_variance128x32, aom_sub_pixel_avg_variance128x32, NULL,
2885 NULL, aom_sad128x32x4d)
2886#endif // CONFIG_EXT_PARTITION
2887#endif // CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002888
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002890 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2891 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2892 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002893
Yaowu Xuf883b422016-08-30 14:01:10 -07002894 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2895 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
2896 NULL, aom_sad128x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002897
Yaowu Xuf883b422016-08-30 14:01:10 -07002898 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2899 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
2900 NULL, aom_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002901#endif // CONFIG_EXT_PARTITION
2902
Yaowu Xuf883b422016-08-30 14:01:10 -07002903 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2904 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2905 aom_sad32x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002906
Yaowu Xuf883b422016-08-30 14:01:10 -07002907 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2908 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2909 aom_sad16x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002910
Yaowu Xuf883b422016-08-30 14:01:10 -07002911 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2912 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2913 aom_sad64x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002914
Yaowu Xuf883b422016-08-30 14:01:10 -07002915 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2916 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2917 aom_sad32x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002918
Yaowu Xuf883b422016-08-30 14:01:10 -07002919 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2920 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2921 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002922
Yaowu Xuf883b422016-08-30 14:01:10 -07002923 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2924 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2925 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002926
Yaowu Xuf883b422016-08-30 14:01:10 -07002927 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2928 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2929 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930
Yaowu Xuf883b422016-08-30 14:01:10 -07002931 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2932 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
2933 aom_sad16x8x8, aom_sad16x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002934
Yaowu Xuf883b422016-08-30 14:01:10 -07002935 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2936 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
2937 aom_sad8x16x8, aom_sad8x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002938
Yaowu Xuf883b422016-08-30 14:01:10 -07002939 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2940 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
2941 aom_sad8x8x8, aom_sad8x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002942
Yaowu Xuf883b422016-08-30 14:01:10 -07002943 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2944 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
2945 aom_sad8x4x8, aom_sad8x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002946
Yaowu Xuf883b422016-08-30 14:01:10 -07002947 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2948 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
2949 aom_sad4x8x8, aom_sad4x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002950
Yaowu Xuf883b422016-08-30 14:01:10 -07002951 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2952 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
2953 aom_sad4x4x8, aom_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002954
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07002955#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Han9e7c49f2016-12-06 11:20:10 -08002956 BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL)
Jingning Hane2ffaf82016-12-14 15:26:30 -08002957 BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL)
2958 BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL)
Jingning Han9e7c49f2016-12-06 11:20:10 -08002959#endif
2960
Yue Chencb60b182016-10-13 15:18:22 -07002961#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002962#define OBFP(BT, OSDF, OVF, OSVF) \
2963 cpi->fn_ptr[BT].osdf = OSDF; \
2964 cpi->fn_ptr[BT].ovf = OVF; \
2965 cpi->fn_ptr[BT].osvf = OSVF;
2966
2967#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002968 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2969 aom_obmc_sub_pixel_variance128x128)
2970 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2971 aom_obmc_sub_pixel_variance128x64)
2972 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2973 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002974#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002975 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2976 aom_obmc_sub_pixel_variance64x64)
2977 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2978 aom_obmc_sub_pixel_variance64x32)
2979 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2980 aom_obmc_sub_pixel_variance32x64)
2981 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2982 aom_obmc_sub_pixel_variance32x32)
2983 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2984 aom_obmc_sub_pixel_variance32x16)
2985 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2986 aom_obmc_sub_pixel_variance16x32)
2987 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2988 aom_obmc_sub_pixel_variance16x16)
2989 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2990 aom_obmc_sub_pixel_variance16x8)
2991 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2992 aom_obmc_sub_pixel_variance8x16)
2993 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2994 aom_obmc_sub_pixel_variance8x8)
2995 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2996 aom_obmc_sub_pixel_variance4x8)
2997 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2998 aom_obmc_sub_pixel_variance8x4)
2999 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3000 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003001
3002#if CONFIG_EXT_PARTITION_TYPES
3003 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3004 aom_obmc_sub_pixel_variance4x16)
3005
3006 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3007 aom_obmc_sub_pixel_variance16x4)
3008
3009 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3010 aom_obmc_sub_pixel_variance8x32)
3011
3012 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3013 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003014
3015 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3016 aom_obmc_sub_pixel_variance16x64)
3017
3018 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3019 aom_obmc_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003020
3021#if CONFIG_EXT_PARTITION
3022 OBFP(BLOCK_32X128, aom_obmc_sad32x128, aom_obmc_variance32x128,
3023 aom_obmc_sub_pixel_variance32x128)
3024
3025 OBFP(BLOCK_128X32, aom_obmc_sad128x32, aom_obmc_variance128x32,
3026 aom_obmc_sub_pixel_variance128x32)
3027#endif // CONFIG_EXT_PARTITION
3028#endif // CONFIG_EXT_PARTITION_TYPES
Yue Chencb60b182016-10-13 15:18:22 -07003029#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07003030
David Barkerf19f35f2017-05-22 16:33:22 +01003031#define MBFP(BT, MCSDF, MCSVF) \
3032 cpi->fn_ptr[BT].msdf = MCSDF; \
3033 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003034
3035#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01003036 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3037 aom_masked_sub_pixel_variance128x128)
3038 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3039 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003040#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01003041 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3042 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3043 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3044 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3045 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3046 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3047 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3048 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3049 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3050 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3051 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3052 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3053 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003054
3055#if CONFIG_EXT_PARTITION_TYPES
3056 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3057
3058 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3059
3060 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3061
3062 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003063
3064 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3065
3066 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003067
3068#if CONFIG_EXT_PARTITION
3069 MBFP(BLOCK_32X128, aom_masked_sad32x128, aom_masked_sub_pixel_variance32x128)
3070
3071 MBFP(BLOCK_128X32, aom_masked_sad128x32, aom_masked_sub_pixel_variance128x32)
3072#endif // CONFIG_EXT_PARTITION
3073#endif // CONFIG_EXT_PARTITION_TYPES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003074
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003075#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003076 highbd_set_var_fns(cpi);
3077#endif
3078
Yaowu Xuf883b422016-08-30 14:01:10 -07003079 /* av1_init_quantizer() is first called here. Add check in
3080 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003081 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003082 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003083 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003084 av1_init_quantizer(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003085#if CONFIG_AOM_QM
3086 aom_qm_init(cm);
3087#endif
3088
Yaowu Xuf883b422016-08-30 14:01:10 -07003089 av1_loop_filter_init(cm);
Fergus Simpsondac6aff2017-05-18 15:11:38 -07003090#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07003091 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003092 cm->superres_upscaled_width = oxcf->width;
3093 cm->superres_upscaled_height = oxcf->height;
Fergus Simpsondac6aff2017-05-18 15:11:38 -07003094#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuc27fc142016-08-22 16:08:15 -07003095#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07003096 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003097#endif // CONFIG_LOOP_RESTORATION
3098
3099 cm->error.setjmp = 0;
3100
3101 return cpi;
3102}
3103
3104#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3105
3106#define SNPRINT2(H, T, V) \
3107 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
3108
Yaowu Xuf883b422016-08-30 14:01:10 -07003109void av1_remove_compressor(AV1_COMP *cpi) {
3110 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003111 unsigned int i;
3112 int t;
3113
3114 if (!cpi) return;
3115
3116 cm = &cpi->common;
3117 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003118#if CONFIG_ENTROPY_STATS
3119 if (cpi->oxcf.pass != 1) {
3120 fprintf(stderr, "Writing counts.stt\n");
3121 FILE *f = fopen("counts.stt", "wb");
3122 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
Zoe Liua56f9162017-06-21 22:49:57 -07003123 fwrite(aggregate_fc_per_type, sizeof(aggregate_fc_per_type[0]),
3124 FRAME_CONTEXTS, f);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003125 fclose(f);
3126 }
3127#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003128#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003129 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003130
3131 if (cpi->oxcf.pass != 1) {
3132 char headings[512] = { 0 };
3133 char results[512] = { 0 };
3134 FILE *f = fopen("opsnr.stt", "a");
3135 double time_encoded =
3136 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3137 10000000.000;
3138 double total_encode_time =
3139 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3140 const double dr =
3141 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3142 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3143 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3144 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3145
3146 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003147 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003148 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3149 const double total_ssim =
3150 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3151 snprintf(headings, sizeof(headings),
3152 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003153 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Yaowu Xuc27fc142016-08-22 16:08:15 -07003154 "WstPsnr\tWstSsim\tWstFast\tWstHVS");
3155 snprintf(results, sizeof(results),
3156 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3157 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3158 "%7.3f\t%7.3f\t%7.3f\t%7.3f",
3159 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
3160 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
3161 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
3162 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
3163 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
3164
3165 if (cpi->b_calculate_blockiness) {
3166 SNPRINT(headings, "\t Block\tWstBlck");
3167 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3168 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3169 }
3170
3171 if (cpi->b_calculate_consistency) {
3172 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003173 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003174 (double)cpi->total_inconsistency);
3175
3176 SNPRINT(headings, "\tConsist\tWstCons");
3177 SNPRINT2(results, "\t%7.3f", consistency);
3178 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3179 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003180 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3181 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003182 rate_err, fabs(rate_err));
3183 }
3184
3185 fclose(f);
3186 }
3187
3188#endif
3189
3190#if 0
3191 {
3192 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
3193 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
3194 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
3195 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
3196 cpi->time_compress_data / 1000,
3197 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
3198 }
3199#endif
3200 }
3201
3202 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003203 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003204 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3205
3206 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003207 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003208
3209 // Deallocate allocated thread data.
3210 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07003211 aom_free(thread_data->td->palette_buffer);
Jingning Hand064cf02017-06-01 10:00:39 -07003212#if CONFIG_MOTION_VAR
3213 aom_free(thread_data->td->above_pred_buf);
3214 aom_free(thread_data->td->left_pred_buf);
3215 aom_free(thread_data->td->wsrc_buf);
3216 aom_free(thread_data->td->mask_buf);
3217#endif // CONFIG_MOTION_VAR
Yaowu Xuf883b422016-08-30 14:01:10 -07003218 aom_free(thread_data->td->counts);
3219 av1_free_pc_tree(thread_data->td);
Yaowu Xuf883b422016-08-30 14:01:10 -07003220 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003221 }
3222 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003223 aom_free(cpi->tile_thr_data);
3224 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003225
Yaowu Xuf883b422016-08-30 14:01:10 -07003226 if (cpi->num_workers > 1) av1_loop_filter_dealloc(&cpi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003227
3228 dealloc_compressor_data(cpi);
3229
3230 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3231 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003232 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003233 }
3234
3235#if CONFIG_FP_MB_STATS
3236 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003237 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003238 cpi->twopass.frame_mb_stats_buf = NULL;
3239 }
3240#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003241#if CONFIG_INTERNAL_STATS
3242 aom_free(cpi->ssim_vars);
3243 cpi->ssim_vars = NULL;
3244#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003245
Yaowu Xuf883b422016-08-30 14:01:10 -07003246 av1_remove_common(cm);
3247 av1_free_ref_frame_buffers(cm->buffer_pool);
3248 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003249
3250#ifdef OUTPUT_YUV_SKINMAP
3251 fclose(yuv_skinmap_file);
3252#endif
3253#ifdef OUTPUT_YUV_REC
3254 fclose(yuv_rec_file);
3255#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003256#if 0
3257
3258 if (keyfile)
3259 fclose(keyfile);
3260
3261 if (framepsnr)
3262 fclose(framepsnr);
3263
3264 if (kf_list)
3265 fclose(kf_list);
3266
3267#endif
3268}
3269
Yaowu Xuf883b422016-08-30 14:01:10 -07003270static void generate_psnr_packet(AV1_COMP *cpi) {
3271 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003272 int i;
3273 PSNR_STATS psnr;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003274#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07003275 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003276 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3277#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07003278 aom_calc_psnr(cpi->source, cpi->common.frame_to_show, &psnr);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003279#endif
3280
3281 for (i = 0; i < 4; ++i) {
3282 pkt.data.psnr.samples[i] = psnr.samples[i];
3283 pkt.data.psnr.sse[i] = psnr.sse[i];
3284 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3285 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003286 pkt.kind = AOM_CODEC_PSNR_PKT;
3287 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003288}
3289
Yaowu Xuf883b422016-08-30 14:01:10 -07003290int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003291 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3292
3293 cpi->ref_frame_flags = ref_frame_flags;
3294 return 0;
3295}
3296
Yaowu Xuf883b422016-08-30 14:01:10 -07003297void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags) {
3298 cpi->ext_refresh_golden_frame = (ref_frame_flags & AOM_GOLD_FLAG) != 0;
3299 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & AOM_ALT_FLAG) != 0;
3300 cpi->ext_refresh_last_frame = (ref_frame_flags & AOM_LAST_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003301 cpi->ext_refresh_frame_flags_pending = 1;
3302}
3303
Thomas Daede497d1952017-08-08 17:33:06 -07003304int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3305 AV1_COMMON *const cm = &cpi->common;
3306 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003307 if (cfg) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003308 aom_yv12_copy_frame(cfg, sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003309 return 0;
3310 } else {
3311 return -1;
3312 }
3313}
3314
Thomas Daede497d1952017-08-08 17:33:06 -07003315int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3316 AV1_COMMON *const cm = &cpi->common;
3317 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003318 if (cfg) {
3319 aom_yv12_copy_frame(sd, cfg);
3320 return 0;
3321 } else {
3322 return -1;
3323 }
3324}
3325
3326int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003327 cpi->ext_refresh_frame_context = update;
3328 cpi->ext_refresh_frame_context_pending = 1;
3329 return 0;
3330}
3331
3332#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3333// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3334// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3335// not denoise the UV channels at this time. If ever we implement UV channel
3336// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003337void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003338 uint8_t *src = s->y_buffer;
3339 int h = s->y_height;
3340
3341 do {
3342 fwrite(src, s->y_width, 1, f);
3343 src += s->y_stride;
3344 } while (--h);
3345
3346 src = s->u_buffer;
3347 h = s->uv_height;
3348
3349 do {
3350 fwrite(src, s->uv_width, 1, f);
3351 src += s->uv_stride;
3352 } while (--h);
3353
3354 src = s->v_buffer;
3355 h = s->uv_height;
3356
3357 do {
3358 fwrite(src, s->uv_width, 1, f);
3359 src += s->uv_stride;
3360 } while (--h);
3361}
3362#endif
3363
Zoe Liue04abf72017-04-19 15:37:11 -07003364#if CONFIG_EXT_REFS && !CONFIG_XIPHRC
Zoe Liu8dd1c982017-09-11 10:14:35 -07003365#if USE_GF16_MULTI_LAYER
3366static void check_show_existing_frame_gf16(AV1_COMP *cpi) {
3367 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3368 AV1_COMMON *const cm = &cpi->common;
3369 const FRAME_UPDATE_TYPE next_frame_update_type =
3370 gf_group->update_type[gf_group->index];
3371
3372 if (cm->show_existing_frame == 1) {
3373 cm->show_existing_frame = 0;
3374 } else if (cpi->rc.is_last_bipred_frame) {
3375 cpi->rc.is_last_bipred_frame = 0;
3376 cm->show_existing_frame = 1;
3377 cpi->existing_fb_idx_to_show = cpi->bwd_fb_idx;
3378 } else if (next_frame_update_type == OVERLAY_UPDATE ||
3379 next_frame_update_type == INTNL_OVERLAY_UPDATE) {
3380 // Check the temporal filtering status for the next OVERLAY frame
3381 const int num_arfs_in_gf = cpi->num_extra_arfs + 1;
3382 int which_arf = 0, arf_idx;
3383 // Identify the index to the next overlay frame.
3384 for (arf_idx = 0; arf_idx < num_arfs_in_gf; arf_idx++) {
3385 if (gf_group->index == cpi->arf_pos_for_ovrly[arf_idx]) {
3386 which_arf = arf_idx;
3387 break;
3388 }
3389 }
3390 assert(arf_idx < num_arfs_in_gf);
3391 if (cpi->is_arf_filter_off[which_arf]) {
3392 cm->show_existing_frame = 1;
3393 cpi->rc.is_src_frame_alt_ref = 1;
3394 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3395 ? cpi->alt_fb_idx
3396 : cpi->bwd_fb_idx;
3397 cpi->is_arf_filter_off[which_arf] = 0;
3398 }
3399 }
3400 cpi->rc.is_src_frame_ext_arf = 0;
3401}
3402#endif // USE_GF16_MULTI_LAYER
3403
Yaowu Xuf883b422016-08-30 14:01:10 -07003404static void check_show_existing_frame(AV1_COMP *cpi) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003405#if USE_GF16_MULTI_LAYER
3406 if (cpi->rc.baseline_gf_interval == 16) {
3407 check_show_existing_frame_gf16(cpi);
3408 return;
3409 }
3410#endif // USE_GF16_MULTI_LAYER
3411
Yaowu Xuc27fc142016-08-22 16:08:15 -07003412 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003413 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003414 const FRAME_UPDATE_TYPE next_frame_update_type =
3415 gf_group->update_type[gf_group->index];
3416 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07003417
3418 if (cm->show_existing_frame == 1) {
3419 cm->show_existing_frame = 0;
3420 } else if (cpi->rc.is_last_bipred_frame) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003421 // NOTE: If the current frame is a last bi-predictive frame, it is
3422 // needed next to show the BWDREF_FRAME, which is pointed by
3423 // the last_fb_idxes[0] after reference frame buffer update
Yaowu Xuc27fc142016-08-22 16:08:15 -07003424 cpi->rc.is_last_bipred_frame = 0;
3425 cm->show_existing_frame = 1;
3426 cpi->existing_fb_idx_to_show = cpi->lst_fb_idxes[0];
3427 } else if (cpi->is_arf_filter_off[which_arf] &&
3428 (next_frame_update_type == OVERLAY_UPDATE ||
3429 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
3430 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003431 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003432 cm->show_existing_frame = 1;
3433 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003434 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3435 ? cpi->alt_fb_idx
3436 : cpi->alt2_fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003437 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003438 }
3439 cpi->rc.is_src_frame_ext_arf = 0;
3440}
Zoe Liue04abf72017-04-19 15:37:11 -07003441#endif // CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07003442
3443#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003444void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003445 uint8_t *src = s->y_buffer;
3446 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003447 if (yuv_rec_file == NULL) return;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003448#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003449 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3450 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3451
3452 do {
3453 fwrite(src16, s->y_width, 2, yuv_rec_file);
3454 src16 += s->y_stride;
3455 } while (--h);
3456
3457 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3458 h = s->uv_height;
3459
3460 do {
3461 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3462 src16 += s->uv_stride;
3463 } while (--h);
3464
3465 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3466 h = s->uv_height;
3467
3468 do {
3469 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3470 src16 += s->uv_stride;
3471 } while (--h);
3472
3473 fflush(yuv_rec_file);
3474 return;
3475 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003476#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477
3478 do {
3479 fwrite(src, s->y_width, 1, yuv_rec_file);
3480 src += s->y_stride;
3481 } while (--h);
3482
3483 src = s->u_buffer;
3484 h = s->uv_height;
3485
3486 do {
3487 fwrite(src, s->uv_width, 1, yuv_rec_file);
3488 src += s->uv_stride;
3489 } while (--h);
3490
3491 src = s->v_buffer;
3492 h = s->uv_height;
3493
3494 do {
3495 fwrite(src, s->uv_width, 1, yuv_rec_file);
3496 src += s->uv_stride;
3497 } while (--h);
3498
3499 fflush(yuv_rec_file);
3500}
3501#endif // OUTPUT_YUV_REC
3502
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003503#if CONFIG_GLOBAL_MOTION
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003504#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003505static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3506 int i;
3507 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003508 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003509 AV1_COMMON *const cm = &cpi->common;
3510 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3511 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003512 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003513 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003514 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003515 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003516 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003517 recode = 1;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003518 recode |= (rdc->global_motion_used[i] > 0);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003519 }
3520 }
3521 return recode;
3522}
3523#endif // CONFIG_GLOBAL_MOTION
3524
Yaowu Xuc27fc142016-08-22 16:08:15 -07003525// Function to test for conditions that indicate we should loop
3526// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003527static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3528 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003529 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003530 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003531 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3532 int force_recode = 0;
3533
3534 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3535 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3536 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003537 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3538 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3539 (rc->projected_frame_size < low_limit && q > minq)) {
3540 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003541 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003542 // Deal with frame undershoot and whether or not we are
3543 // below the automatically set cq level.
3544 if (q > oxcf->cq_level &&
3545 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3546 force_recode = 1;
3547 }
3548 }
3549 }
3550 return force_recode;
3551}
3552
Yaowu Xuc27fc142016-08-22 16:08:15 -07003553#define DUMP_REF_FRAME_IMAGES 0
3554
3555#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003556static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003557 const YV12_BUFFER_CONFIG *const ref_buf,
3558 char *file_name) {
3559 int h;
3560 FILE *f_ref = NULL;
3561
3562 if (ref_buf == NULL) {
3563 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003564 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003565 }
3566
3567 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3568 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003569 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003570 }
3571
3572 // --- Y ---
3573 for (h = 0; h < cm->height; ++h) {
3574 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3575 }
3576 // --- U ---
3577 for (h = 0; h < (cm->height >> 1); ++h) {
3578 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3579 f_ref);
3580 }
3581 // --- V ---
3582 for (h = 0; h < (cm->height >> 1); ++h) {
3583 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3584 f_ref);
3585 }
3586
3587 fclose(f_ref);
3588
Yaowu Xuf883b422016-08-30 14:01:10 -07003589 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003590}
3591
Yaowu Xuf883b422016-08-30 14:01:10 -07003592static void dump_ref_frame_images(AV1_COMP *cpi) {
3593 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003594 MV_REFERENCE_FRAME ref_frame;
3595
3596 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3597 char file_name[256] = "";
3598 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3599 cm->current_video_frame, ref_frame);
3600 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3601 }
3602}
3603#endif // DUMP_REF_FRAME_IMAGES == 1
3604
3605#if CONFIG_EXT_REFS
3606// This function is used to shift the virtual indices of last reference frames
3607// as follows:
3608// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3609// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003610static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003611 int ref_frame;
3612 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
3613 cpi->lst_fb_idxes[ref_frame] = cpi->lst_fb_idxes[ref_frame - 1];
3614
3615 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003616 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003617 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003618 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3619 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3620 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003621 }
3622 }
3623}
Zoe Liuf0e46692016-10-12 12:31:43 -07003624#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003625
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003626#if CONFIG_VAR_REFS
3627static void enc_check_valid_ref_frames(AV1_COMP *const cpi) {
3628 AV1_COMMON *const cm = &cpi->common;
3629 MV_REFERENCE_FRAME ref_frame;
3630
3631 // TODO(zoeliu): To handle ALTREF_FRAME the same way as do with other
3632 // reference frames. Current encoder invalid ALTREF when ALTREF
3633 // is the same as LAST, but invalid all the other references
3634 // when they are the same as ALTREF.
3635 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3636 int ref_buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3637 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3638
3639 if (ref_buf_idx != INVALID_IDX) {
3640 ref_buf->is_valid = 1;
3641
3642 MV_REFERENCE_FRAME ref;
3643 for (ref = LAST_FRAME; ref < ref_frame; ++ref) {
3644 int buf_idx = get_ref_frame_buf_idx(cpi, ref);
3645 RefBuffer *const buf = &cm->frame_refs[ref - LAST_FRAME];
3646 if (buf->is_valid && buf_idx == ref_buf_idx) {
3647 if (ref_frame != ALTREF_FRAME || ref == LAST_FRAME) {
3648 ref_buf->is_valid = 0;
3649 break;
3650 } else {
3651 buf->is_valid = 0;
3652 }
3653 }
3654 }
3655 } else {
3656 ref_buf->is_valid = 0;
3657 }
3658 }
3659}
3660#endif // CONFIG_VAR_REFS
3661
Zoe Liu8dd1c982017-09-11 10:14:35 -07003662#if CONFIG_EXT_REFS
3663#if USE_GF16_MULTI_LAYER
3664static void update_reference_frames_gf16(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003665 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003666 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003667
Zoe Liu8dd1c982017-09-11 10:14:35 -07003668 if (cm->frame_type == KEY_FRAME) {
3669 for (int ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3670 ref_cnt_fb(pool->frame_bufs,
3671 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3672 cm->new_fb_idx);
3673 }
3674 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3675 cm->new_fb_idx);
3676 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3677 cm->new_fb_idx);
3678 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3679 cm->new_fb_idx);
3680 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3681 cm->new_fb_idx);
3682 } else {
3683 if (cpi->refresh_last_frame || cpi->refresh_golden_frame ||
3684 cpi->refresh_bwd_ref_frame || cpi->refresh_alt2_ref_frame ||
3685 cpi->refresh_alt_ref_frame) {
3686 assert(cpi->refresh_fb_idx >= 0 && cpi->refresh_fb_idx < REF_FRAMES);
3687 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->refresh_fb_idx],
3688 cm->new_fb_idx);
3689 }
3690
3691 // TODO(zoeliu): To handle cpi->interp_filter_selected[].
3692
3693 // For GF of 16, an additional ref frame index mapping needs to be handled
3694 // if this is the last frame to encode in the current GF group.
3695 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3696 if (gf_group->update_type[gf_group->index + 1] == OVERLAY_UPDATE)
3697 av1_ref_frame_map_idx_updates(cpi, gf_group->index + 1);
3698 }
3699
3700#if DUMP_REF_FRAME_IMAGES == 1
3701 // Dump out all reference frame images.
3702 dump_ref_frame_images(cpi);
3703#endif // DUMP_REF_FRAME_IMAGES
3704}
3705#endif // USE_GF16_MULTI_LAYER
3706#endif // CONFIG_EXT_REFS
3707
3708static void update_reference_frames(AV1_COMP *cpi) {
3709 AV1_COMMON *const cm = &cpi->common;
3710
Yaowu Xuc27fc142016-08-22 16:08:15 -07003711 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3712 // for the purpose to verify no mismatch between encoder and decoder.
3713 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3714
Zoe Liu8dd1c982017-09-11 10:14:35 -07003715#if CONFIG_EXT_REFS
3716#if USE_GF16_MULTI_LAYER
3717 if (cpi->rc.baseline_gf_interval == 16) {
3718 update_reference_frames_gf16(cpi);
3719 return;
3720 }
3721#endif // USE_GF16_MULTI_LAYER
3722#endif // CONFIG_EXT_REFS
3723
3724 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003725 // At this point the new frame has been encoded.
3726 // If any buffer copy / swapping is signaled it should be done here.
3727 if (cm->frame_type == KEY_FRAME) {
3728 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3729 cm->new_fb_idx);
3730#if CONFIG_EXT_REFS
3731 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3732 cm->new_fb_idx);
Zoe Liue9b15e22017-07-19 15:53:01 -07003733 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3734 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003735#endif // CONFIG_EXT_REFS
3736 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3737 cm->new_fb_idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003738 } else if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003739 // We have decided to preserve the previously existing golden frame as our
3740 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003741 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742 // we're updating the GF with the current decoded frame, we save it to the
3743 // ARF slot instead.
3744 // We now have to update the ARF with the current frame and swap gld_fb_idx
3745 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3746 // slot and, if we're updating the GF, the current frame becomes the new GF.
3747 int tmp;
3748
3749 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3750 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003751 tmp = cpi->alt_fb_idx;
3752 cpi->alt_fb_idx = cpi->gld_fb_idx;
3753 cpi->gld_fb_idx = tmp;
3754
3755#if CONFIG_EXT_REFS
3756 // We need to modify the mapping accordingly
3757 cpi->arf_map[0] = cpi->alt_fb_idx;
Zoe Liue9b15e22017-07-19 15:53:01 -07003758#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003759// TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3760// cpi->interp_filter_selected[GOLDEN_FRAME]?
3761#if CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003762 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3763 // Deal with the special case for showing existing internal ALTREF_FRAME
3764 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3765 // by updating the virtual indices.
3766 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -07003767 const int which_arf = gf_group->arf_ref_idx[gf_group->index];
3768 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003769
Zoe Liue9b15e22017-07-19 15:53:01 -07003770 const int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003771 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003772
Zoe Liue9b15e22017-07-19 15:53:01 -07003773 cpi->lst_fb_idxes[0] = cpi->alt2_fb_idx;
3774 cpi->alt2_fb_idx = tmp;
3775 // We need to modify the mapping accordingly
3776 cpi->arf_map[which_arf] = cpi->alt2_fb_idx;
3777
3778 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3779 cpi->interp_filter_selected[ALTREF2_FRAME],
3780 sizeof(cpi->interp_filter_selected[ALTREF2_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003781#endif // CONFIG_EXT_REFS
3782 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003783 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003784 if (cpi->refresh_alt_ref_frame) {
3785 int arf_idx = cpi->alt_fb_idx;
3786 int which_arf = 0;
Zoe Liu3ac20932017-08-30 16:35:55 -07003787#if !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003788 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3789 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3790 arf_idx = gf_group->arf_update_idx[gf_group->index];
3791 }
Zoe Liu3ac20932017-08-30 16:35:55 -07003792#endif // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003793 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003794
3795 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3796 cpi->interp_filter_selected[0],
3797 sizeof(cpi->interp_filter_selected[0]));
3798 }
3799
Zoe Liue9b15e22017-07-19 15:53:01 -07003800 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003801 if (cpi->refresh_golden_frame) {
3802 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3803 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003804
Zoe Liuf0e46692016-10-12 12:31:43 -07003805#if !CONFIG_EXT_REFS
3806 if (!cpi->rc.is_src_frame_alt_ref)
3807#endif // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003808 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3809 cpi->interp_filter_selected[0],
3810 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003811 }
3812
3813#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07003814 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003815 if (cpi->refresh_bwd_ref_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003816 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3817 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003818
3819 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3820 cpi->interp_filter_selected[0],
3821 sizeof(cpi->interp_filter_selected[0]));
3822 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003823
Zoe Liue9b15e22017-07-19 15:53:01 -07003824 // === ALTREF2_FRAME ===
3825 if (cpi->refresh_alt2_ref_frame) {
3826 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3827 cm->new_fb_idx);
3828
3829 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3830 cpi->interp_filter_selected[0],
3831 sizeof(cpi->interp_filter_selected[0]));
3832 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003833#endif // CONFIG_EXT_REFS
3834 }
3835
3836 if (cpi->refresh_last_frame) {
3837#if CONFIG_EXT_REFS
3838 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3839 // reference to the reference frame buffer virtual index; and then (2) from
3840 // the virtual index to the reference frame buffer physical index:
3841 //
3842 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3843 // | | |
3844 // v v v
3845 // lst_fb_idxes[0], ..., lst_fb_idxes[2], ..., alt_fb_idx
3846 // | | |
3847 // v v v
3848 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3849 //
3850 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3851 // have the other 2 LAST reference frames shifted as follows:
3852 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3853 // , and then have LAST_FRAME refreshed by the newly coded frame.
3854 //
3855 // To fulfill it, the decoder will be notified to execute following 2 steps:
3856 //
3857 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3858 // to point to the newly coded frame, i.e.
3859 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3860 //
3861 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3862 // original virtual index of LAST3_FRAME and have the other mappings
3863 // shifted as follows:
3864 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3865 // | | |
3866 // v v v
3867 // lst_fb_idxes[2], lst_fb_idxes[0], lst_fb_idxes[1]
3868 int ref_frame;
Zoe Liu5fca7242016-10-10 17:18:57 -07003869
Yaowu Xuc27fc142016-08-22 16:08:15 -07003870 if (cm->frame_type == KEY_FRAME) {
3871 for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3872 ref_cnt_fb(pool->frame_bufs,
3873 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3874 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003875 }
3876 } else {
3877 int tmp;
3878
3879 ref_cnt_fb(pool->frame_bufs,
3880 &cm->ref_frame_map[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3881 cm->new_fb_idx);
3882
Yaowu Xuc27fc142016-08-22 16:08:15 -07003883 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3884
3885 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003886 cpi->lst_fb_idxes[0] = tmp;
3887
Zoe Liuf0e46692016-10-12 12:31:43 -07003888 assert(cm->show_existing_frame == 0);
Zoe Liuf0e46692016-10-12 12:31:43 -07003889 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3890 cpi->interp_filter_selected[0],
3891 sizeof(cpi->interp_filter_selected[0]));
Zoe Liuaff92d52017-07-11 21:35:08 -07003892
3893 if (cpi->rc.is_last_bipred_frame) {
3894 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3895 // LAST3_FRAME by updating the virtual indices.
3896 //
3897 // NOTE: The source frame for BWDREF does not have a holding position as
3898 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3899 // virtual index reshuffling for BWDREF, the encoder always
3900 // specifies a LAST_BIPRED right before BWDREF and completes the
3901 // reshuffling job accordingly.
3902 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3903
3904 shift_last_ref_frames(cpi);
3905 cpi->lst_fb_idxes[0] = cpi->bwd_fb_idx;
3906 cpi->bwd_fb_idx = tmp;
3907
3908 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3909 cpi->interp_filter_selected[BWDREF_FRAME],
3910 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
3911 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003912 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003913#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003914 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
3915 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003916 if (!cpi->rc.is_src_frame_alt_ref) {
3917 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3918 cpi->interp_filter_selected[0],
3919 sizeof(cpi->interp_filter_selected[0]));
3920 }
3921#endif // CONFIG_EXT_REFS
3922 }
3923
3924#if DUMP_REF_FRAME_IMAGES == 1
3925 // Dump out all reference frame images.
3926 dump_ref_frame_images(cpi);
3927#endif // DUMP_REF_FRAME_IMAGES
3928}
3929
Yaowu Xuf883b422016-08-30 14:01:10 -07003930static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003931 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003932 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003933 ensure_mv_buffer(new_fb_ptr, cm);
3934 new_fb_ptr->width = cm->width;
3935 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003936}
3937
Cheng Chen46f30c72017-09-07 11:13:33 -07003938static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003939 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003940 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003941 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
3942 AOM_LAST_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003943#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003944 AOM_LAST2_FLAG,
3945 AOM_LAST3_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003946#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003947 AOM_GOLD_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003948#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003949 AOM_BWD_FLAG,
Zoe Liue9b15e22017-07-19 15:53:01 -07003950 AOM_ALT2_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003951#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003952 AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003953 };
3954
3955 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003956 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003957 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3958 BufferPool *const pool = cm->buffer_pool;
3959 const YV12_BUFFER_CONFIG *const ref =
3960 get_ref_frame_buffer(cpi, ref_frame);
3961
3962 if (ref == NULL) {
3963 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3964 continue;
3965 }
3966
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003967#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003968 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3969 RefCntBuffer *new_fb_ptr = NULL;
3970 int force_scaling = 0;
3971 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3972 if (new_fb == INVALID_IDX) {
3973 new_fb = get_free_fb(cm);
3974 force_scaling = 1;
3975 }
3976 if (new_fb == INVALID_IDX) return;
3977 new_fb_ptr = &pool->frame_bufs[new_fb];
3978 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3979 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003980 if (aom_realloc_frame_buffer(
3981 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3982 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3983 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003984 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003985 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003986 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf,
3987 (int)cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003988 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3989 alloc_frame_mvs(cm, new_fb);
3990 }
3991#else
3992 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3993 RefCntBuffer *new_fb_ptr = NULL;
3994 int force_scaling = 0;
3995 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3996 if (new_fb == INVALID_IDX) {
3997 new_fb = get_free_fb(cm);
3998 force_scaling = 1;
3999 }
4000 if (new_fb == INVALID_IDX) return;
4001 new_fb_ptr = &pool->frame_bufs[new_fb];
4002 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
4003 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004004 if (aom_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004005 cm->subsampling_x, cm->subsampling_y,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07004006 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
4007 NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004008 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004009 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07004010 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004011 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
4012 alloc_frame_mvs(cm, new_fb);
4013 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004014#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004015 } else {
4016 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4017 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
4018 buf->buf.y_crop_width = ref->y_crop_width;
4019 buf->buf.y_crop_height = ref->y_crop_height;
4020 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
4021 ++buf->ref_count;
4022 }
4023 } else {
4024 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
4025 }
4026 }
4027}
4028
Yaowu Xuf883b422016-08-30 14:01:10 -07004029static void release_scaled_references(AV1_COMP *cpi) {
4030 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004031 int i;
4032 if (cpi->oxcf.pass == 0) {
4033 // Only release scaled references under certain conditions:
4034 // if reference will be updated, or if scaled reference has same resolution.
4035 int refresh[INTER_REFS_PER_FRAME];
4036 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
4037#if CONFIG_EXT_REFS
4038 refresh[1] = refresh[2] = 0;
4039 refresh[3] = (cpi->refresh_golden_frame) ? 1 : 0;
4040 refresh[4] = (cpi->refresh_bwd_ref_frame) ? 1 : 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07004041 refresh[5] = (cpi->refresh_alt2_ref_frame) ? 1 : 0;
4042 refresh[6] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07004043#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004044 refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
4045 refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
4046#endif // CONFIG_EXT_REFS
4047 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
4048 const int idx = cpi->scaled_ref_idx[i - 1];
4049 RefCntBuffer *const buf =
4050 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
4051 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
4052 if (buf != NULL &&
4053 (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
4054 buf->buf.y_crop_height == ref->y_crop_height))) {
4055 --buf->ref_count;
4056 cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
4057 }
4058 }
4059 } else {
4060 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) {
4061 const int idx = cpi->scaled_ref_idx[i];
4062 RefCntBuffer *const buf =
4063 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
4064 if (buf != NULL) {
4065 --buf->ref_count;
4066 cpi->scaled_ref_idx[i] = INVALID_IDX;
4067 }
4068 }
4069 }
4070}
4071
Yaowu Xuc27fc142016-08-22 16:08:15 -07004072#if 0 && CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07004073static void output_frame_level_debug_stats(AV1_COMP *cpi) {
4074 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004075 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
4076 int64_t recon_err;
4077
Yaowu Xuf883b422016-08-30 14:01:10 -07004078 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004079
Alex Conversef77fd0b2017-04-20 11:00:24 -07004080 recon_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004081
4082 if (cpi->twopass.total_left_stats.coded_error != 0.0)
Yunqing Wang8c1e57c2016-10-25 15:15:23 -07004083 fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
Yaowu Xuc27fc142016-08-22 16:08:15 -07004084 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
4085 "%10"PRId64" %10"PRId64" %10d "
4086 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
4087 "%6d %6d %5d %5d %5d "
4088 "%10"PRId64" %10.3lf"
4089 "%10lf %8u %10"PRId64" %10d %10d %10d\n",
4090 cpi->common.current_video_frame,
4091 cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004092 cpi->rc.source_alt_ref_pending,
4093 cpi->rc.source_alt_ref_active,
4094 cpi->rc.this_frame_target,
4095 cpi->rc.projected_frame_size,
4096 cpi->rc.projected_frame_size / cpi->common.MBs,
4097 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
4098 cpi->rc.vbr_bits_off_target,
4099 cpi->rc.vbr_bits_off_target_fast,
4100 cpi->twopass.extend_minq,
4101 cpi->twopass.extend_minq_fast,
4102 cpi->rc.total_target_vs_actual,
4103 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
4104 cpi->rc.total_actual_bits, cm->base_qindex,
Yaowu Xuf883b422016-08-30 14:01:10 -07004105 av1_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
4106 (double)av1_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
4107 av1_convert_qindex_to_q(cpi->twopass.active_worst_quality,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004108 cm->bit_depth),
4109 cpi->rc.avg_q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004110 av1_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
Yaowu Xuc27fc142016-08-22 16:08:15 -07004111 cpi->refresh_last_frame, cpi->refresh_golden_frame,
4112 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
4113 cpi->twopass.bits_left,
4114 cpi->twopass.total_left_stats.coded_error,
4115 cpi->twopass.bits_left /
4116 (1 + cpi->twopass.total_left_stats.coded_error),
4117 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
4118 cpi->twopass.kf_zeromotion_pct,
4119 cpi->twopass.fr_content_type);
4120
4121 fclose(f);
4122
4123 if (0) {
4124 FILE *const fmodes = fopen("Modes.stt", "a");
4125 int i;
4126
4127 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
4128 cm->frame_type, cpi->refresh_golden_frame,
4129 cpi->refresh_alt_ref_frame);
4130
4131 for (i = 0; i < MAX_MODES; ++i)
4132 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
4133
4134 fprintf(fmodes, "\n");
4135
4136 fclose(fmodes);
4137 }
4138}
4139#endif
4140
Yaowu Xuf883b422016-08-30 14:01:10 -07004141static void set_mv_search_params(AV1_COMP *cpi) {
4142 const AV1_COMMON *const cm = &cpi->common;
4143 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004144
4145 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07004146 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004147
4148 if (cpi->sf.mv.auto_mv_step_size) {
4149 if (frame_is_intra_only(cm)) {
4150 // Initialize max_mv_magnitude for use in the first INTER frame
4151 // after a key/intra-only frame.
4152 cpi->max_mv_magnitude = max_mv_def;
4153 } else {
4154 if (cm->show_frame) {
4155 // Allow mv_steps to correspond to twice the max mv magnitude found
4156 // in the previous frame, capped by the default max_mv_magnitude based
4157 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07004158 cpi->mv_step_param = av1_init_search_range(
4159 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004160 }
4161 cpi->max_mv_magnitude = 0;
4162 }
4163 }
4164}
4165
Yaowu Xuf883b422016-08-30 14:01:10 -07004166static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004167#if CONFIG_GLOBAL_MOTION
4168 int i;
4169 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01004170 cpi->common.global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004171 }
4172 cpi->global_motion_search_done = 0;
4173#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuf883b422016-08-30 14:01:10 -07004174 av1_set_speed_features_framesize_independent(cpi);
4175 av1_set_rd_speed_thresholds(cpi);
4176 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004177 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07004178 if (!frame_is_intra_only(&cpi->common)) set_compound_tools(&cpi->common);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004179}
4180
Yaowu Xuf883b422016-08-30 14:01:10 -07004181static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004182 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004183 AV1_COMMON *const cm = &cpi->common;
4184 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004185
4186 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004187 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004188
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004189// Decide q and q bounds.
4190#if CONFIG_XIPHRC
4191 int frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
4192 *q = od_enc_rc_select_quantizers_and_lambdas(
4193 &cpi->od_rc, cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
4194 frame_type, bottom_index, top_index);
4195#else
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004196 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
4197 top_index);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004198#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004199
James Zern01a9d702017-08-25 19:09:33 +00004200 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07004201#if CONFIG_AMVR
4202 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
4203 cpi->common.cur_frame_mv_precision_level);
4204#else
Cheng Chen46f30c72017-09-07 11:13:33 -07004205 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
RogerZhou3b635242017-09-19 10:06:46 -07004206#endif
James Zern01a9d702017-08-25 19:09:33 +00004207 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004208
4209 // Configure experimental use of segmentation for enhanced coding of
4210 // static regions if indicated.
4211 // Only allowed in the second pass of a two pass encode, as it requires
4212 // lagged coding, and if the relevant speed feature flag is set.
4213 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
4214 configure_static_seg_features(cpi);
4215}
4216
Yaowu Xuf883b422016-08-30 14:01:10 -07004217static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004218 int y_stride = cpi->scaled_source.y_stride;
4219
4220 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004221 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004222 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004223 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004224 }
4225}
4226
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004227#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004228#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
4229static void set_restoration_tilesize(int width, int height, int sx, int sy,
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004230 RestorationInfo *rst) {
4231 (void)width;
4232 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004233 (void)sx;
4234 (void)sy;
4235#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
4236 int s = AOMMIN(sx, sy);
4237#else
4238 int s = 0;
4239#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
4240
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004241 rst[0].restoration_tilesize = (RESTORATION_TILESIZE_MAX >> 1);
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004242 rst[1].restoration_tilesize = rst[0].restoration_tilesize >> s;
4243 rst[2].restoration_tilesize = rst[1].restoration_tilesize;
Debargha Mukherjee7a5587a2017-08-31 07:41:30 -07004244
4245 rst[0].procunit_width = rst[0].procunit_height = RESTORATION_PROC_UNIT_SIZE;
4246 rst[1].procunit_width = rst[2].procunit_width =
4247 RESTORATION_PROC_UNIT_SIZE >> sx;
4248 rst[1].procunit_height = rst[2].procunit_height =
4249 RESTORATION_PROC_UNIT_SIZE >> sy;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004250}
4251#endif // CONFIG_LOOP_RESTORATION
4252
Cheng Chen46f30c72017-09-07 11:13:33 -07004253static void init_ref_frame_bufs(AV1_COMMON *cm) {
4254 int i;
4255 BufferPool *const pool = cm->buffer_pool;
4256 cm->new_fb_idx = INVALID_IDX;
4257 for (i = 0; i < REF_FRAMES; ++i) {
4258 cm->ref_frame_map[i] = INVALID_IDX;
4259 pool->frame_bufs[i].ref_count = 0;
4260 }
4261#if CONFIG_HASH_ME
4262 for (i = 0; i < FRAME_BUFFERS; ++i) {
4263 av1_hash_table_init(&pool->frame_bufs[i].hash_table);
4264 }
4265#endif
4266}
4267
4268static void check_initial_width(AV1_COMP *cpi,
4269#if CONFIG_HIGHBITDEPTH
4270 int use_highbitdepth,
4271#endif
4272 int subsampling_x, int subsampling_y) {
4273 AV1_COMMON *const cm = &cpi->common;
4274
4275 if (!cpi->initial_width ||
4276#if CONFIG_HIGHBITDEPTH
4277 cm->use_highbitdepth != use_highbitdepth ||
4278#endif
4279 cm->subsampling_x != subsampling_x ||
4280 cm->subsampling_y != subsampling_y) {
4281 cm->subsampling_x = subsampling_x;
4282 cm->subsampling_y = subsampling_y;
4283#if CONFIG_HIGHBITDEPTH
4284 cm->use_highbitdepth = use_highbitdepth;
4285#endif
4286
4287 alloc_raw_frame_buffers(cpi);
4288 init_ref_frame_bufs(cm);
4289 alloc_util_frame_buffers(cpi);
4290
4291 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
4292
4293 cpi->initial_width = cm->width;
4294 cpi->initial_height = cm->height;
4295 cpi->initial_mbs = cm->MBs;
4296 }
4297}
4298
4299// Returns 1 if the assigned width or height was <= 0.
4300static int set_size_literal(AV1_COMP *cpi, int width, int height) {
4301 AV1_COMMON *cm = &cpi->common;
4302#if CONFIG_HIGHBITDEPTH
4303 check_initial_width(cpi, cm->use_highbitdepth, cm->subsampling_x,
4304 cm->subsampling_y);
4305#else
4306 check_initial_width(cpi, cm->subsampling_x, cm->subsampling_y);
4307#endif // CONFIG_HIGHBITDEPTH
4308
4309 if (width <= 0 || height <= 0) return 1;
4310
4311 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07004312 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004313
4314 if (cpi->initial_width && cpi->initial_height &&
4315 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
4316 av1_free_context_buffers(cm);
4317 av1_free_pc_tree(&cpi->td);
4318 alloc_compressor_data(cpi);
4319 realloc_segmentation_maps(cpi);
4320 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07004321 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004322 update_frame_size(cpi);
4323
4324 return 0;
4325}
4326
Fergus Simpsonbc189932017-05-16 17:02:39 -07004327static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07004328 AV1_COMMON *const cm = &cpi->common;
Fergus Simpsonbc189932017-05-16 17:02:39 -07004329 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004330 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004331
Fergus Simpsonbc189932017-05-16 17:02:39 -07004332 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07004333 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07004334 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004335 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004336 }
4337
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004338#if !CONFIG_XIPHRC
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004339 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004340 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004341 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004342#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004343
4344 alloc_frame_mvs(cm, cm->new_fb_idx);
4345
4346 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07004347 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004348 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004349#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004350 cm->use_highbitdepth,
4351#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07004352 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
4353 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07004354 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004355 "Failed to allocate frame buffer");
4356
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004357#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004358 set_restoration_tilesize(
4359#if CONFIG_FRAME_SUPERRES
4360 cm->superres_upscaled_width, cm->superres_upscaled_height,
4361#else
4362 cm->width, cm->height,
4363#endif // CONFIG_FRAME_SUPERRES
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07004364 cm->subsampling_x, cm->subsampling_y, cm->rst_info);
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004365 for (int i = 0; i < MAX_MB_PLANE; ++i)
4366 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
4367 av1_alloc_restoration_buffers(cm);
4368 for (int i = 0; i < MAX_MB_PLANE; ++i) {
4369 cpi->rst_search[i].restoration_tilesize =
4370 cm->rst_info[i].restoration_tilesize;
Debargha Mukherjee7a5587a2017-08-31 07:41:30 -07004371 cpi->rst_search[i].procunit_width = cm->rst_info[i].procunit_width;
4372 cpi->rst_search[i].procunit_height = cm->rst_info[i].procunit_height;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004373 av1_alloc_restoration_struct(cm, &cpi->rst_search[i],
4374#if CONFIG_FRAME_SUPERRES
4375 cm->superres_upscaled_width,
4376 cm->superres_upscaled_height);
4377#else
4378 cm->width, cm->height);
4379#endif // CONFIG_FRAME_SUPERRES
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08004380 }
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07004381#endif // CONFIG_LOOP_RESTORATION
4382 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004383 init_motion_estimation(cpi);
4384
4385 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4386 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
4387 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
4388
4389 ref_buf->idx = buf_idx;
4390
4391 if (buf_idx != INVALID_IDX) {
4392 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
4393 ref_buf->buf = buf;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004394#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004395 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07004396 &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
4397 cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
4398#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004399 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
4400 buf->y_crop_height, cm->width,
4401 cm->height);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004402#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07004403 if (av1_is_scaled(&ref_buf->sf)) aom_extend_frame_borders(buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004404 } else {
4405 ref_buf->buf = NULL;
4406 }
4407 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004408
4409#if CONFIG_VAR_REFS
4410 // Check duplicate reference frames
4411 enc_check_valid_ref_frames(cpi);
4412#endif // CONFIG_VAR_REFS
4413
Alex Conversee816b312017-05-01 09:51:24 -07004414#if CONFIG_INTRABC
4415#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan1dcb7072017-05-12 11:13:05 +02004416 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4417 cm->width, cm->height,
4418 cm->use_highbitdepth);
Alex Conversee816b312017-05-01 09:51:24 -07004419#else
4420 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
4421 cm->width, cm->height);
4422#endif // CONFIG_HIGHBITDEPTH
4423#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004424
4425 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4426}
4427
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004428static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4429 // Choose an arbitrary random number
4430 static unsigned int seed = 56789;
4431 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004432 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4433 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004434
4435 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004436 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004437 case RESIZE_FIXED:
4438 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004439 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004440 else
Urvang Joshide71d142017-10-05 12:12:15 -07004441 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004442 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004443 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004444 default: assert(0);
4445 }
Urvang Joshide71d142017-10-05 12:12:15 -07004446 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004447}
4448
4449#if CONFIG_FRAME_SUPERRES
Urvang Joshie58b5642017-10-05 17:59:43 -07004450
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004451static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4452 // Choose an arbitrary random number
4453 static unsigned int seed = 34567;
4454 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004455 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4456 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004457 int bottom_index, top_index, q, qthresh;
4458
4459 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004460 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004461 case SUPERRES_FIXED:
4462 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004463 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004464 else
Urvang Joshide71d142017-10-05 12:12:15 -07004465 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004466 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004467 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004468 case SUPERRES_QTHRESH:
4469 qthresh = (cpi->common.frame_type == KEY_FRAME ? oxcf->superres_kf_qthresh
4470 : oxcf->superres_qthresh);
4471 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
4472 q = av1_rc_pick_q_and_bounds(cpi, cpi->oxcf.width, cpi->oxcf.height,
4473 &bottom_index, &top_index);
4474 if (q < qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004475 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004476 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004477 new_denom = SCALE_NUMERATOR + 1 + ((q - qthresh) >> 3);
4478 new_denom = AOMMIN(SCALE_NUMERATOR << 1, new_denom);
4479 // printf("SUPERRES: q %d, qthresh %d: denom %d\n", q, qthresh,
4480 // new_denom);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004481 }
4482 break;
4483 default: assert(0);
4484 }
Urvang Joshide71d142017-10-05 12:12:15 -07004485 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004486}
4487
Urvang Joshide71d142017-10-05 12:12:15 -07004488static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4489 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4490}
4491
4492static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
4493 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom) &&
Urvang Joshie58b5642017-10-05 17:59:43 -07004494 (SCALE_HORIZONTAL_ONLY ||
4495 dimension_is_ok(oheight, rsz->resize_height, rsz->superres_denom));
Urvang Joshide71d142017-10-05 12:12:15 -07004496}
4497
4498#define DIVIDE_AND_ROUND(x, y) (((x) + ((y) >> 1)) / (y))
4499
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004500static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004501 SUPERRES_MODE superres_mode, int owidth,
4502 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004503 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004504 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004505 }
4506
Urvang Joshie58b5642017-10-05 17:59:43 -07004507// Calculate current resize scale.
4508#if SCALE_HORIZONTAL_ONLY
4509 int resize_denom =
4510 DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width);
4511#else
Urvang Joshide71d142017-10-05 12:12:15 -07004512 int resize_denom =
4513 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4514 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
Urvang Joshie58b5642017-10-05 17:59:43 -07004515#endif // SCALE_HORIZONTAL_ONLY
Urvang Joshide71d142017-10-05 12:12:15 -07004516
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004517 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004518 // Alter superres scale as needed to enforce conformity.
4519 rsz->superres_denom =
4520 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4521 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4522 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004523 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004524 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004525 // Alter resize scale as needed to enforce conformity.
4526 resize_denom =
4527 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004528 rsz->resize_width = owidth;
4529 rsz->resize_height = oheight;
4530 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004531 resize_denom);
4532 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4533 if (resize_denom > SCALE_NUMERATOR) {
4534 --resize_denom;
4535 rsz->resize_width = owidth;
4536 rsz->resize_height = oheight;
4537 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4538 resize_denom);
4539 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004540 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004541 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004542 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004543 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004544 if (resize_denom > rsz->superres_denom)
4545 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004546 else
Urvang Joshide71d142017-10-05 12:12:15 -07004547 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004548 rsz->resize_width = owidth;
4549 rsz->resize_height = oheight;
4550 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004551 resize_denom);
4552 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4553 (resize_denom > SCALE_NUMERATOR ||
4554 rsz->superres_denom > SCALE_NUMERATOR));
4555 } else { // We are allowed to alter neither resize scale nor superres scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004556 return 0;
4557 }
Urvang Joshide71d142017-10-05 12:12:15 -07004558 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004559}
Urvang Joshide71d142017-10-05 12:12:15 -07004560#undef DIVIDE_AND_ROUND
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004561#endif // CONFIG_FRAME_SUPERRES
4562
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004563// Calculates resize and superres params for next frame
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004564size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
4565 const AV1EncoderConfig *oxcf = &cpi->oxcf;
4566 size_params_type rsz = {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004567 oxcf->width,
4568 oxcf->height,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004569#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07004570 SCALE_NUMERATOR
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004571#endif // CONFIG_FRAME_SUPERRES
4572 };
Urvang Joshide71d142017-10-05 12:12:15 -07004573 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004574 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004575 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4576 rsz.resize_width = cpi->resize_pending_width;
4577 rsz.resize_height = cpi->resize_pending_height;
4578 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4579 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004580 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004581 rsz.resize_width = cpi->oxcf.width;
4582 rsz.resize_height = cpi->oxcf.height;
4583 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004584 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004585 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004586#if CONFIG_FRAME_SUPERRES
Urvang Joshide71d142017-10-05 12:12:15 -07004587 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004588 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4589 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004590 assert(0 && "Invalid scale parameters");
4591#endif // CONFIG_FRAME_SUPERRES
4592 return rsz;
4593}
4594
4595static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004596 int encode_width = rsz->resize_width;
4597 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004598
4599#if CONFIG_FRAME_SUPERRES
4600 AV1_COMMON *cm = &cpi->common;
4601 cm->superres_upscaled_width = encode_width;
4602 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004603 cm->superres_scale_denominator = rsz->superres_denom;
4604 av1_calculate_scaled_size(&encode_width, &encode_height, rsz->superres_denom);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004605#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004606 set_frame_size(cpi, encode_width, encode_height);
4607}
4608
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004609static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004610 size_params_type rsz = av1_calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004611 setup_frame_size_from_params(cpi, &rsz);
4612}
4613
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004614#if CONFIG_FRAME_SUPERRES
4615static void superres_post_encode(AV1_COMP *cpi) {
4616 AV1_COMMON *cm = &cpi->common;
4617
4618 if (av1_superres_unscaled(cm)) return;
4619
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004620 av1_superres_upscale(cm, NULL);
4621
4622 // If regular resizing is occurring the source will need to be downscaled to
4623 // match the upscaled superres resolution. Otherwise the original source is
4624 // used.
4625 if (av1_resize_unscaled(cm)) {
4626 cpi->source = cpi->unscaled_source;
4627 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4628 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004629 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4630 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004631 // Do downscale. cm->(width|height) has been updated by av1_superres_upscale
4632 if (aom_realloc_frame_buffer(
4633 &cpi->scaled_source, cm->superres_upscaled_width,
4634 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
4635#if CONFIG_HIGHBITDEPTH
4636 cm->use_highbitdepth,
4637#endif // CONFIG_HIGHBITDEPTH
4638 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
4639 aom_internal_error(
4640 &cm->error, AOM_CODEC_MEM_ERROR,
4641 "Failed to reallocate scaled source buffer for superres");
4642 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4643 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
4644#if CONFIG_HIGHBITDEPTH
4645 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
4646 (int)cm->bit_depth);
4647#else
4648 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source);
4649#endif // CONFIG_HIGHBITDEPTH
4650 cpi->source = &cpi->scaled_source;
4651 }
4652}
4653#endif // CONFIG_FRAME_SUPERRES
4654
4655static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4656 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4657 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004658 int no_loopfilter = 0;
4659
4660 if (is_lossless_requested(&cpi->oxcf)) no_loopfilter = 1;
4661
4662#if CONFIG_EXT_TILE
4663 // 0 loopfilter level is only necessary if individual tile
4664 // decoding is required.
4665 if (cm->single_tile_decoding) no_loopfilter = 1;
4666#endif // CONFIG_EXT_TILE
4667
4668 if (no_loopfilter) {
Cheng Chen13fc8192017-08-19 11:49:28 -07004669#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004670 lf->filter_level[0] = 0;
4671 lf->filter_level[1] = 0;
4672#else
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004673 lf->filter_level = 0;
Cheng Chen179479f2017-08-04 10:56:39 -07004674#endif
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004675 } else {
4676 struct aom_usec_timer timer;
4677
4678 aom_clear_system_state();
4679
4680 aom_usec_timer_start(&timer);
4681
4682 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4683
4684 aom_usec_timer_mark(&timer);
4685 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4686 }
4687
Cheng Chenf572cd32017-08-25 18:34:51 -07004688#if !CONFIG_LPF_SB
Cheng Chen13fc8192017-08-19 11:49:28 -07004689#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004690 if (lf->filter_level[0] || lf->filter_level[1])
4691#else
4692 if (lf->filter_level > 0)
4693#endif
Cheng Chenf572cd32017-08-25 18:34:51 -07004694#endif // CONFIG_LPF_SB
Cheng Chen179479f2017-08-04 10:56:39 -07004695 {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004696#if CONFIG_VAR_TX || CONFIG_EXT_PARTITION || CONFIG_CB4X4
Cheng Chenf572cd32017-08-25 18:34:51 -07004697#if CONFIG_LPF_SB
4698 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0, 0,
4699 0);
4700#else
Cheng Chen13fc8192017-08-19 11:49:28 -07004701#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004702 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level[0],
4703 lf->filter_level[1], 0, 0);
4704 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_u,
4705 lf->filter_level_u, 1, 0);
4706 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_v,
4707 lf->filter_level_v, 2, 0);
4708
Cheng Chene94df5c2017-07-19 17:25:33 -07004709#else
4710 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
Cheng Chen13fc8192017-08-19 11:49:28 -07004711#endif // CONFIG_LOOPFILTER_LEVEL
Cheng Chenf572cd32017-08-25 18:34:51 -07004712#endif // CONFIG_LPF_SB
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004713#else
4714 if (cpi->num_workers > 1)
4715 av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
4716 lf->filter_level, 0, 0, cpi->workers,
4717 cpi->num_workers, &cpi->lf_row_sync);
4718 else
4719 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
4720#endif
4721 }
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004722
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004723#if CONFIG_STRIPED_LOOP_RESTORATION
4724 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm);
4725#endif
4726
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004727#if CONFIG_CDEF
4728 if (is_lossless_requested(&cpi->oxcf)) {
4729 cm->cdef_bits = 0;
4730 cm->cdef_strengths[0] = 0;
4731 cm->nb_cdef_strengths = 1;
4732 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004733 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004734 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
4735 cpi->oxcf.speed > 0);
4736
4737 // Apply the filter
4738 av1_cdef_frame(cm->frame_to_show, cm, xd);
4739 }
4740#endif
4741
4742#if CONFIG_FRAME_SUPERRES
4743 superres_post_encode(cpi);
4744#endif // CONFIG_FRAME_SUPERRES
4745
4746#if CONFIG_LOOP_RESTORATION
Debargha Mukherjeefc9f3cc2017-09-11 14:51:14 -07004747 aom_extend_frame_borders(cm->frame_to_show);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004748 av1_pick_filter_restoration(cpi->source, cpi, cpi->sf.lpf_pick);
4749 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4750 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4751 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4752 av1_loop_restoration_frame(cm->frame_to_show, cm, cm->rst_info, 7, 0, NULL);
4753 }
4754#endif // CONFIG_LOOP_RESTORATION
4755 // TODO(debargha): Fix mv search range on encoder side
4756 // aom_extend_frame_inner_borders(cm->frame_to_show);
4757 aom_extend_frame_borders(cm->frame_to_show);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004758}
4759
Yaowu Xuf883b422016-08-30 14:01:10 -07004760static void encode_without_recode_loop(AV1_COMP *cpi) {
4761 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004762 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004763
Yaowu Xuf883b422016-08-30 14:01:10 -07004764 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004765
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004766 set_size_independent_vars(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004767
Fergus Simpsonbc189932017-05-16 17:02:39 -07004768 setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004769
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004770 assert(cm->width == cpi->scaled_source.y_crop_width);
4771 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004772
Yaowu Xuc27fc142016-08-22 16:08:15 -07004773 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4774
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004775 cpi->source =
4776 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4777 if (cpi->unscaled_last_source != NULL)
4778 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4779 &cpi->scaled_last_source);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004780#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
4781 cpi->source->buf_8bit_valid = 0;
4782#endif
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004783
4784 if (frame_is_intra_only(cm) == 0) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004785 scale_references(cpi);
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004786 }
4787
Yaowu Xuf883b422016-08-30 14:01:10 -07004788 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004789 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004790 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004791
Yaowu Xuc27fc142016-08-22 16:08:15 -07004792 // Variance adaptive and in frame q adjustment experiments are mutually
4793 // exclusive.
4794 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004795 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004796 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004797 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004798 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004799 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004800 }
4801 apply_active_map(cpi);
4802
4803 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004804 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004805
4806 // Update some stats from cyclic refresh, and check if we should not update
4807 // golden reference, for 1 pass CBR.
4808 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004809 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4810 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004811
4812 // Update the skip mb flag probabilities based on the distribution
4813 // seen in the last encoder iteration.
4814 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004815 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004816}
4817
Yaowu Xuf883b422016-08-30 14:01:10 -07004818static void encode_with_recode_loop(AV1_COMP *cpi, size_t *size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004820 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004821 RATE_CONTROL *const rc = &cpi->rc;
4822 int bottom_index, top_index;
4823 int loop_count = 0;
4824 int loop_at_this_size = 0;
4825 int loop = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004826#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004827 int overshoot_seen = 0;
4828 int undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004829#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004830 int frame_over_shoot_limit;
4831 int frame_under_shoot_limit;
4832 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004833
4834 set_size_independent_vars(cpi);
4835
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004836#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
4837 cpi->source->buf_8bit_valid = 0;
4838#endif
4839
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004840 aom_clear_system_state();
4841 setup_frame_size(cpi);
4842 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4843
Yaowu Xuc27fc142016-08-22 16:08:15 -07004844 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004845 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004846
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004847 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004848 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4849 set_mv_search_params(cpi);
4850
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004851#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004852 // Reset the loop state for new frame size.
4853 overshoot_seen = 0;
4854 undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004855#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004856
Yaowu Xuc27fc142016-08-22 16:08:15 -07004857 q_low = bottom_index;
4858 q_high = top_index;
4859
4860 loop_at_this_size = 0;
4861 }
4862
4863 // Decide frame size bounds first time through.
4864 if (loop_count == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004865 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4866 &frame_under_shoot_limit,
4867 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004868 }
4869
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004870#if CONFIG_GLOBAL_MOTION
4871 // if frame was scaled calculate global_motion_search again if already done
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004872 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4873 if (cpi->source->y_crop_width != cm->width ||
4874 cpi->source->y_crop_height != cm->height)
4875 cpi->global_motion_search_done = 0;
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004876#endif // CONFIG_GLOBAL_MOTION
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004877 cpi->source =
4878 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004879 if (cpi->unscaled_last_source != NULL)
4880 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4881 &cpi->scaled_last_source);
4882
Yaowu Xuc27fc142016-08-22 16:08:15 -07004883 if (frame_is_intra_only(cm) == 0) {
4884 if (loop_count > 0) {
4885 release_scaled_references(cpi);
4886 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004887 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004888 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004889 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004890
4891 if (loop_count == 0) setup_frame(cpi);
4892
hui su0d103572017-03-01 17:58:01 -08004893#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004894 // Base q-index may have changed, so we need to assign proper default coef
4895 // probs before every iteration.
4896 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
4897 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07004898 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004899 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4900 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4901 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4902 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
Thomas Daededa4d8b92017-06-05 15:44:14 -07004903#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
4904 if (cm->frame_refs[0].idx >= 0) {
4905 cm->frame_contexts[cm->frame_refs[0].idx] = *cm->fc;
4906 }
4907#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004908 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07004909#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004910 }
4911 }
hui su0d103572017-03-01 17:58:01 -08004912#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004913
Yaowu Xuc27fc142016-08-22 16:08:15 -07004914 // Variance adaptive and in frame q adjustment experiments are mutually
4915 // exclusive.
4916 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004917 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004918 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004919 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004920 }
4921
4922 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004923 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004924 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004925
4926 // Update the skip mb flag probabilities based on the distribution
4927 // seen in the last encoder iteration.
4928 // update_base_skip_probs(cpi);
4929
Yaowu Xuf883b422016-08-30 14:01:10 -07004930 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004931
4932 // Dummy pack of the bitstream using up to date stats to get an
4933 // accurate estimate of output frame size to determine if we need
4934 // to recode.
4935 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004936 restore_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004937 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004938
4939 rc->projected_frame_size = (int)(*size) << 3;
4940 restore_coding_context(cpi);
4941
4942 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4943 }
4944
Yaowu Xuf883b422016-08-30 14:01:10 -07004945 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004946 loop = 0;
4947 } else {
4948 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4949 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4950 int last_q = q;
4951 int64_t kf_err;
4952
4953 int64_t high_err_target = cpi->ambient_err;
4954 int64_t low_err_target = cpi->ambient_err >> 1;
4955
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004956#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004957 if (cm->use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004958 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004959 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004960 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004961 }
4962#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07004963 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004964#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004965
4966 // Prevent possible divide by zero error below for perfect KF
4967 kf_err += !kf_err;
4968
4969 // The key frame is not good enough or we can afford
4970 // to make it better without undue risk of popping.
4971 if ((kf_err > high_err_target &&
4972 rc->projected_frame_size <= frame_over_shoot_limit) ||
4973 (kf_err > low_err_target &&
4974 rc->projected_frame_size <= frame_under_shoot_limit)) {
4975 // Lower q_high
4976 q_high = q > q_low ? q - 1 : q_low;
4977
4978 // Adjust Q
4979 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004980 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004981 } else if (kf_err < low_err_target &&
4982 rc->projected_frame_size >= frame_under_shoot_limit) {
4983 // The key frame is much better than the previous frame
4984 // Raise q_low
4985 q_low = q < q_high ? q + 1 : q_high;
4986
4987 // Adjust Q
4988 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004989 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004990 }
4991
4992 // Clamp Q to upper and lower limits:
4993 q = clamp(q, q_low, q_high);
4994
4995 loop = q != last_q;
4996 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4997 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004998 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004999 // Is the projected frame size out of range and are we allowed
5000 // to attempt to recode.
5001 int last_q = q;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005002#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005003 int retries = 0;
5004
Yaowu Xuc27fc142016-08-22 16:08:15 -07005005 // Frame size out of permitted range:
5006 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07005007 // Frame is too large
5008 if (rc->projected_frame_size > rc->this_frame_target) {
5009 // Special case if the projected size is > the max allowed.
5010 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
5011 q_high = rc->worst_quality;
5012
5013 // Raise Qlow as to at least the current value
5014 q_low = q < q_high ? q + 1 : q_high;
5015
5016 if (undershoot_seen || loop_at_this_size > 1) {
5017 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005018 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005019
5020 q = (q_high + q_low + 1) / 2;
5021 } else {
5022 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005023 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005024
Yaowu Xuf883b422016-08-30 14:01:10 -07005025 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005026 AOMMAX(q_high, top_index), cm->width,
5027 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005028
5029 while (q < q_low && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005030 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005031 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005032 AOMMAX(q_high, top_index), cm->width,
5033 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005034 retries++;
5035 }
5036 }
5037
5038 overshoot_seen = 1;
5039 } else {
5040 // Frame is too small
5041 q_high = q > q_low ? q - 1 : q_low;
5042
5043 if (overshoot_seen || loop_at_this_size > 1) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005044 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005045 q = (q_high + q_low) / 2;
5046 } else {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005047 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005048 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005049 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005050 // Special case reset for qlow for constrained quality.
5051 // This should only trigger where there is very substantial
5052 // undershoot on a frame and the auto cq level is above
5053 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07005054 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005055 q_low = q;
5056 }
5057
5058 while (q > q_high && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005059 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005060 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005061 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005062 retries++;
5063 }
5064 }
5065
5066 undershoot_seen = 1;
5067 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005068#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005069
5070 // Clamp Q to upper and lower limits:
5071 q = clamp(q, q_low, q_high);
5072
5073 loop = (q != last_q);
5074 } else {
5075 loop = 0;
5076 }
5077 }
5078
5079 // Special case for overlay frame.
5080 if (rc->is_src_frame_alt_ref &&
5081 rc->projected_frame_size < rc->max_frame_bandwidth)
5082 loop = 0;
5083
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08005084#if CONFIG_GLOBAL_MOTION
5085 if (recode_loop_test_global_motion(cpi)) {
5086 loop = 1;
5087 }
5088#endif // CONFIG_GLOBAL_MOTION
5089
Yaowu Xuc27fc142016-08-22 16:08:15 -07005090 if (loop) {
5091 ++loop_count;
5092 ++loop_at_this_size;
5093
5094#if CONFIG_INTERNAL_STATS
5095 ++cpi->tot_recode_hits;
5096#endif
5097 }
5098 } while (loop);
5099}
5100
Yaowu Xuf883b422016-08-30 14:01:10 -07005101static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005102 const int *const map = cpi->common.ref_frame_map;
5103
5104#if CONFIG_EXT_REFS
5105 const int last2_is_last =
5106 map[cpi->lst_fb_idxes[1]] == map[cpi->lst_fb_idxes[0]];
5107 const int last3_is_last =
5108 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[0]];
5109 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[0]];
Zoe Liu5a978832017-08-15 16:33:34 -07005110#if CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02005111 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
5112 const int last3_is_last2 =
5113 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
5114 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
5115 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
Zoe Liu5a978832017-08-15 16:33:34 -07005116#else // !CONFIG_ONE_SIDED_COMPOUND || CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005117 const int bwd_is_last = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[0]];
5118 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
5119
5120 const int last3_is_last2 =
5121 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
5122 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
5123 const int bwd_is_last2 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[1]];
5124
5125 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
5126 const int bwd_is_last3 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[2]];
5127
5128 const int bwd_is_gld = map[cpi->bwd_fb_idx] == map[cpi->gld_fb_idx];
Zoe Liu5a978832017-08-15 16:33:34 -07005129#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005130
Zoe Liue9b15e22017-07-19 15:53:01 -07005131 const int alt2_is_last = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[0]];
5132 const int alt2_is_last2 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[1]];
5133 const int alt2_is_last3 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[2]];
5134 const int alt2_is_gld = map[cpi->alt2_fb_idx] == map[cpi->gld_fb_idx];
5135 const int alt2_is_bwd = map[cpi->alt2_fb_idx] == map[cpi->bwd_fb_idx];
Zoe Liue9b15e22017-07-19 15:53:01 -07005136
Yaowu Xuc27fc142016-08-22 16:08:15 -07005137 const int last2_is_alt = map[cpi->lst_fb_idxes[1]] == map[cpi->alt_fb_idx];
5138 const int last3_is_alt = map[cpi->lst_fb_idxes[2]] == map[cpi->alt_fb_idx];
5139 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
5140 const int bwd_is_alt = map[cpi->bwd_fb_idx] == map[cpi->alt_fb_idx];
Zoe Liue9b15e22017-07-19 15:53:01 -07005141 const int alt2_is_alt = map[cpi->alt2_fb_idx] == map[cpi->alt_fb_idx];
Zoe Liufcf5fa22017-06-26 16:00:38 -07005142#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005143 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
5144 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
5145 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
5146#endif // CONFIG_EXT_REFS
5147
Yaowu Xuf883b422016-08-30 14:01:10 -07005148 int flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005149
Yaowu Xuf883b422016-08-30 14:01:10 -07005150 if (gld_is_last || gld_is_alt) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005151
Yaowu Xuf883b422016-08-30 14:01:10 -07005152 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005153
Yaowu Xuf883b422016-08-30 14:01:10 -07005154 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005155
5156#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005157 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005158
Yaowu Xuf883b422016-08-30 14:01:10 -07005159 if (last3_is_last || last3_is_last2 || last3_is_alt) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005160
Yaowu Xuf883b422016-08-30 14:01:10 -07005161 if (gld_is_last2 || gld_is_last3) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005162
Zoe Liu5a978832017-08-15 16:33:34 -07005163#if CONFIG_ONE_SIDED_COMPOUND && \
5164 !CONFIG_EXT_COMP_REFS // Changes LL & HL bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02005165 /* Allow biprediction between two identical frames (e.g. bwd_is_last = 1) */
5166 if (bwd_is_alt && (flags & AOM_BWD_FLAG)) flags &= ~AOM_BWD_FLAG;
Zoe Liu5a978832017-08-15 16:33:34 -07005167#else // !CONFIG_ONE_SIDED_COMPOUND || CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005168 if ((bwd_is_last || bwd_is_last2 || bwd_is_last3 || bwd_is_gld ||
5169 bwd_is_alt) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07005170 (flags & AOM_BWD_FLAG))
5171 flags &= ~AOM_BWD_FLAG;
Zoe Liu5a978832017-08-15 16:33:34 -07005172#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005173
Zoe Liue9b15e22017-07-19 15:53:01 -07005174 if ((alt2_is_last || alt2_is_last2 || alt2_is_last3 || alt2_is_gld ||
5175 alt2_is_bwd || alt2_is_alt) &&
5176 (flags & AOM_ALT2_FLAG))
5177 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005178#endif // CONFIG_EXT_REFS
5179
5180 return flags;
5181}
5182
Yaowu Xuf883b422016-08-30 14:01:10 -07005183static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005184 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07005185 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07005186 // Note: The overrides are valid only for the next frame passed
5187 // to encode_frame_to_data_rate() function
5188 if (cpi->ext_refresh_frame_context_pending) {
5189 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
5190 cpi->ext_refresh_frame_context_pending = 0;
5191 }
5192 if (cpi->ext_refresh_frame_flags_pending) {
5193 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
5194 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
5195 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
5196 cpi->ext_refresh_frame_flags_pending = 0;
5197 }
5198}
5199
Zoe Liu17af2742017-10-06 10:36:42 -07005200#if !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuf883b422016-08-30 14:01:10 -07005201static void set_arf_sign_bias(AV1_COMP *cpi) {
5202 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005203 int arf_sign_bias;
5204#if CONFIG_EXT_REFS
5205 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liu3ac20932017-08-30 16:35:55 -07005206 // The arf_sign_bias will be one for internal ARFs'
Zoe Liue9b15e22017-07-19 15:53:01 -07005207 arf_sign_bias = cpi->rc.source_alt_ref_active &&
5208 (!cpi->refresh_alt_ref_frame ||
5209 gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07005210#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005211 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
5212 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5213 arf_sign_bias = cpi->rc.source_alt_ref_active &&
5214 (!cpi->refresh_alt_ref_frame ||
5215 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
5216 } else {
5217 arf_sign_bias =
5218 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
5219 }
Zoe Liu6cfaff92016-10-18 17:12:11 -07005220#endif // CONFIG_EXT_REFS
5221
Yaowu Xuc27fc142016-08-22 16:08:15 -07005222 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
5223#if CONFIG_EXT_REFS
5224 cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
Zoe Liue9b15e22017-07-19 15:53:01 -07005225 cm->ref_frame_sign_bias[ALTREF2_FRAME] =
5226 cm->ref_frame_sign_bias[ALTREF_FRAME];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005227#endif // CONFIG_EXT_REFS
5228}
Zoe Liu17af2742017-10-06 10:36:42 -07005229#endif // !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005230
Yaowu Xuf883b422016-08-30 14:01:10 -07005231static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07005232 InterpFilter ifilter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005233 int ref_total[TOTAL_REFS_PER_FRAME] = { 0 };
5234 MV_REFERENCE_FRAME ref;
5235 int mask = 0;
5236 int arf_idx = ALTREF_FRAME;
5237
Zoe Liu3ac20932017-08-30 16:35:55 -07005238#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005239 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
5240 cpi->refresh_alt2_ref_frame)
Zoe Liu3ac20932017-08-30 16:35:55 -07005241#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005242 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Zoe Liu3ac20932017-08-30 16:35:55 -07005243#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005244 return mask;
5245
Yaowu Xuc27fc142016-08-22 16:08:15 -07005246 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
5247 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
5248 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005249
5250 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
5251 if ((ref_total[LAST_FRAME] &&
5252 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
5253#if CONFIG_EXT_REFS
5254 (ref_total[LAST2_FRAME] == 0 ||
5255 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
5256 ref_total[LAST2_FRAME]) &&
5257 (ref_total[LAST3_FRAME] == 0 ||
5258 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
5259 ref_total[LAST3_FRAME]) &&
5260#endif // CONFIG_EXT_REFS
5261 (ref_total[GOLDEN_FRAME] == 0 ||
5262 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
5263 ref_total[GOLDEN_FRAME]) &&
5264#if CONFIG_EXT_REFS
5265 (ref_total[BWDREF_FRAME] == 0 ||
5266 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
5267 ref_total[BWDREF_FRAME]) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07005268 (ref_total[ALTREF2_FRAME] == 0 ||
5269 cpi->interp_filter_selected[ALTREF2_FRAME][ifilter] * 50 <
5270 ref_total[ALTREF2_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005271#endif // CONFIG_EXT_REFS
5272 (ref_total[ALTREF_FRAME] == 0 ||
5273 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
5274 ref_total[ALTREF_FRAME]))
5275 mask |= 1 << ifilter;
5276 }
5277 return mask;
5278}
5279
5280#define DUMP_RECON_FRAMES 0
5281
5282#if DUMP_RECON_FRAMES == 1
5283// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005284static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5285 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005286 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
5287 int h;
5288 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5289 FILE *f_recon = NULL;
5290
5291 if (recon_buf == NULL || !cm->show_frame) {
5292 printf("Frame %d is not ready or no show to dump.\n",
5293 cm->current_video_frame);
5294 return;
5295 }
5296
5297 if (cm->current_video_frame == 0) {
5298 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5299 printf("Unable to open file %s to write.\n", file_name);
5300 return;
5301 }
5302 } else {
5303 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5304 printf("Unable to open file %s to append.\n", file_name);
5305 return;
5306 }
5307 }
5308 printf(
5309 "\nFrame=%5d, encode_update_type[%5d]=%1d, show_existing_frame=%d, "
Zoe Liufcf5fa22017-06-26 16:00:38 -07005310 "source_alt_ref_active=%d, refresh_alt_ref_frame=%d, rf_level=%d, "
5311 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07005312 cm->current_video_frame, cpi->twopass.gf_group.index,
5313 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liufcf5fa22017-06-26 16:00:38 -07005314 cm->show_existing_frame, cpi->rc.source_alt_ref_active,
5315 cpi->refresh_alt_ref_frame,
5316 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
5317 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07005318#if 0
5319 int ref_frame;
5320 printf("get_ref_frame_map_idx: [");
5321 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
5322 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
5323 printf(" ]\n");
5324 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
5325 printf("cm->ref_frame_map = [");
5326 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
5327 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
5328 }
5329 printf(" ]\n");
5330#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005331
5332 // --- Y ---
5333 for (h = 0; h < cm->height; ++h) {
5334 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5335 f_recon);
5336 }
5337 // --- U ---
5338 for (h = 0; h < (cm->height >> 1); ++h) {
5339 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5340 f_recon);
5341 }
5342 // --- V ---
5343 for (h = 0; h < (cm->height >> 1); ++h) {
5344 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5345 f_recon);
5346 }
5347
5348 fclose(f_recon);
5349}
5350#endif // DUMP_RECON_FRAMES
5351
Thomas Davies028b57f2017-02-22 16:42:11 +00005352static void make_update_tile_list_enc(AV1_COMP *cpi, const int tile_rows,
5353 const int tile_cols,
5354 FRAME_CONTEXT *ec_ctxs[]) {
5355 int i;
5356 for (i = 0; i < tile_rows * tile_cols; ++i)
5357 ec_ctxs[i] = &cpi->tile_data[i].tctx;
5358}
5359
Yaowu Xuf883b422016-08-30 14:01:10 -07005360static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005361 uint8_t *dest, int skip_adapt,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005362 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005363 AV1_COMMON *const cm = &cpi->common;
5364 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005365 struct segmentation *const seg = &cm->seg;
Thomas Davies493623e2017-03-31 16:12:25 +01005366 FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
5367 sizeof(&cpi->tile_data[0].tctx));
5368 aom_cdf_prob **cdf_ptrs =
5369 aom_malloc(cm->tile_rows * cm->tile_cols *
5370 sizeof(&cpi->tile_data[0].tctx.partition_cdf[0][0]));
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005371#if CONFIG_XIPHRC
5372 int frame_type;
5373 int drop_this_frame = 0;
Zoe Liue04abf72017-04-19 15:37:11 -07005374#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005375 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07005376 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005377
Zoe Liu17af2742017-10-06 10:36:42 -07005378#if !CONFIG_FRAME_SIGN_BIAS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005379 // Set the arf sign bias for this frame.
5380 set_arf_sign_bias(cpi);
Zoe Liu17af2742017-10-06 10:36:42 -07005381#endif // !CONFIG_FRAME_SIGN_BIAS
5382
Fangwen Fu8d164de2016-12-14 13:40:54 -08005383#if CONFIG_TEMPMV_SIGNALING
5384 // frame type has been decided outside of this function call
5385 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
5386 cm->use_prev_frame_mvs =
5387 !cpi->oxcf.disable_tempmv && !cm->cur_frame->intra_only;
5388#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005389
5390#if CONFIG_EXT_REFS
5391 // NOTE:
5392 // (1) Move the setup of the ref_frame_flags upfront as it would be
5393 // determined by the current frame properties;
5394 // (2) The setup of the ref_frame_flags applies to both show_existing_frame's
5395 // and the other cases.
5396 if (cm->current_video_frame > 0)
5397 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5398
5399 if (cm->show_existing_frame) {
5400 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
5401 // BWDREF_FRAME in the reference frame buffer.
5402 cm->frame_type = INTER_FRAME;
5403 cm->show_frame = 1;
5404 cpi->frame_flags = *frame_flags;
5405
5406 // In the case of show_existing frame, we will not send fresh flag
5407 // to decoder. Any change in the reference frame buffer can be done by
5408 // switching the virtual indices.
5409
5410 cpi->refresh_last_frame = 0;
5411 cpi->refresh_golden_frame = 0;
5412 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005413 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005414 cpi->refresh_alt_ref_frame = 0;
5415
5416 cpi->rc.is_bwd_ref_frame = 0;
5417 cpi->rc.is_last_bipred_frame = 0;
5418 cpi->rc.is_bipred_frame = 0;
5419
Jingning Han8f661602017-08-19 08:16:50 -07005420 restore_coding_context(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005421 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07005422 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005423
5424 // Set up frame to show to get ready for stats collection.
5425 cm->frame_to_show = get_frame_new_buffer(cm);
5426
5427#if DUMP_RECON_FRAMES == 1
5428 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5429 dump_filtered_recon_frames(cpi);
5430#endif // DUMP_RECON_FRAMES
5431
5432 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07005433 // NOTE:
5434 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
5435 // update has been done previously when handling the LAST_BIPRED_FRAME
5436 // right before BWDREF_FRAME (in the display order);
5437 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
5438 // update will be done when the following is called, which will exchange
5439 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
5440 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2, and
5441 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Cheng Chen46f30c72017-09-07 11:13:33 -07005442 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005443
5444 // Update frame flags
5445 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5446 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
5447 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5448
5449 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5450
5451 // Update the frame type
5452 cm->last_frame_type = cm->frame_type;
5453
Yaowu Xuc27fc142016-08-22 16:08:15 -07005454 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5455 // to do post-encoding update accordingly.
5456 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005457 av1_set_target_rate(cpi, cm->width, cm->height);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005458#if CONFIG_XIPHRC
Zoe Liue04abf72017-04-19 15:37:11 -07005459 frame_type = cm->frame_type == INTER_FRAME ? OD_P_FRAME : OD_I_FRAME;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005460 drop_this_frame = od_enc_rc_update_state(
5461 &cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5462 cpi->refresh_alt_ref_frame, frame_type, cpi->droppable);
5463#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005464 av1_rc_postencode_update(cpi, *size);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005465#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005466 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005467
Yaowu Xuc27fc142016-08-22 16:08:15 -07005468 ++cm->current_video_frame;
5469
Jingning Hanf6214b92017-04-12 11:43:37 -07005470 aom_free(tile_ctxs);
5471 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005472 return;
5473 }
5474#endif // CONFIG_EXT_REFS
5475
5476 // Set default state for segment based loop filter update flags.
5477 cm->lf.mode_ref_delta_update = 0;
5478
5479 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5480 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
5481
5482 // Set various flags etc to special state if it is a key frame.
5483 if (frame_is_intra_only(cm)) {
5484 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005485 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005486
5487 // If segmentation is enabled force a map update for key frames.
5488 if (seg->enabled) {
5489 seg->update_map = 1;
5490 seg->update_data = 1;
5491 }
5492
5493 // The alternate reference frame cannot be active for a key frame.
5494 cpi->rc.source_alt_ref_active = 0;
5495
5496 cm->error_resilient_mode = oxcf->error_resilient_mode;
5497
Thomas Daedea6a854b2017-06-22 17:49:11 -07005498#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005499 // By default, encoder assumes decoder can use prev_mi.
5500 if (cm->error_resilient_mode) {
5501 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
5502 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
5503 } else if (cm->intra_only) {
5504 // Only reset the current context.
5505 cm->reset_frame_context = RESET_FRAME_CONTEXT_CURRENT;
5506 }
Thomas Daedea6a854b2017-06-22 17:49:11 -07005507#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005508 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005509 if (cpi->oxcf.mtu == 0) {
5510 cm->num_tg = cpi->oxcf.num_tile_groups;
5511 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005512 // Use a default value for the purposes of weighting costs in probability
5513 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005514 cm->num_tg = DEFAULT_MAX_NUM_TG;
5515 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005516
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005517#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07005518 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5519 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08005520#endif // CONFIG_EXT_TILE
5521
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005522#if CONFIG_XIPHRC
5523 if (drop_this_frame) {
5524 av1_rc_postencode_update_drop_frame(cpi);
5525 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005526 aom_free(tile_ctxs);
5527 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005528 return;
5529 }
5530#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07005531 // For 1 pass CBR, check if we are dropping this frame.
5532 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005533 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005534 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005535 if (av1_rc_drop_frame(cpi)) {
5536 av1_rc_postencode_update_drop_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005537 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005538 aom_free(tile_ctxs);
5539 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005540 return;
5541 }
5542 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005543#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005544
Yaowu Xuf883b422016-08-30 14:01:10 -07005545 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005546
5547#if CONFIG_INTERNAL_STATS
5548 memset(cpi->mode_chosen_counts, 0,
5549 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5550#endif
5551
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005552#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01005553 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005554 /* Non-normative definition of current_frame_id ("frame counter" with
5555 * wraparound) */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005556 const int frame_id_length = FRAME_ID_LENGTH_MINUS7 + 7;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005557 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005558 int lsb, msb;
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005559/* quasi-random initialization of current_frame_id for a key frame */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005560#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07005561 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5562 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5563 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005564 } else {
5565#endif
Alex Conversef77fd0b2017-04-20 11:00:24 -07005566 lsb = cpi->source->y_buffer[0] & 0xff;
5567 msb = cpi->source->y_buffer[1] & 0xff;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005568#if CONFIG_HIGHBITDEPTH
David Barker49a76562016-12-07 14:50:21 +00005569 }
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01005570#endif
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005571 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005572 } else {
5573 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005574 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
5575 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005576 }
5577 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005578#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005579
Fangwen Fu6160df22017-04-24 09:45:51 -07005580#if CONFIG_EXT_DELTA_Q
5581 cm->delta_q_present_flag = cpi->oxcf.deltaq_mode != NO_DELTA_Q;
Fangwen Fu231fe422017-04-24 17:52:29 -07005582 cm->delta_lf_present_flag = cpi->oxcf.deltaq_mode == DELTA_Q_LF;
Cheng Chen880166a2017-10-02 17:48:48 -07005583#if CONFIG_LOOPFILTER_LEVEL
5584 cm->delta_lf_multi = DEFAULT_DELTA_LF_MULTI;
5585#endif // CONFIG_LOOPFILTER_LEVEL
Fangwen Fu6160df22017-04-24 09:45:51 -07005586#endif
5587
Yaowu Xuc27fc142016-08-22 16:08:15 -07005588 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
5589 encode_without_recode_loop(cpi);
5590 } else {
5591 encode_with_recode_loop(cpi, size, dest);
5592 }
5593
Yi Luo10e23002017-07-31 11:54:43 -07005594 cm->last_tile_cols = cm->tile_cols;
5595 cm->last_tile_rows = cm->tile_rows;
5596
Yaowu Xuc27fc142016-08-22 16:08:15 -07005597#ifdef OUTPUT_YUV_SKINMAP
5598 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005599 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005600 }
5601#endif // OUTPUT_YUV_SKINMAP
5602
5603 // Special case code to reduce pulsing when key frames are forced at a
5604 // fixed interval. Note the reconstruction error if it is the frame before
5605 // the force key frame
5606 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005607#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005608 if (cm->use_highbitdepth) {
5609 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07005610 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005611 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005612 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005613 }
5614#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07005615 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005616#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005617 }
5618
5619 // If the encoder forced a KEY_FRAME decision
5620 if (cm->frame_type == KEY_FRAME) {
5621 cpi->refresh_last_frame = 1;
5622 }
5623
5624 cm->frame_to_show = get_frame_new_buffer(cm);
5625 cm->frame_to_show->color_space = cm->color_space;
anorkin76fb1262017-03-22 15:12:12 -07005626#if CONFIG_COLORSPACE_HEADERS
5627 cm->frame_to_show->transfer_function = cm->transfer_function;
5628 cm->frame_to_show->chroma_sample_position = cm->chroma_sample_position;
5629#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005630 cm->frame_to_show->color_range = cm->color_range;
5631 cm->frame_to_show->render_width = cm->render_width;
5632 cm->frame_to_show->render_height = cm->render_height;
5633
5634#if CONFIG_EXT_REFS
5635// TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5636// off.
5637#endif // CONFIG_EXT_REFS
5638
5639 // Pick the loop filter level for the frame.
5640 loopfilter_frame(cpi, cm);
5641
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005642#ifdef OUTPUT_YUV_REC
5643 aom_write_one_yuv_frame(cm, cm->frame_to_show);
5644#endif
5645
Yaowu Xuc27fc142016-08-22 16:08:15 -07005646 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07005647 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005648
Jingning Hanf6214b92017-04-12 11:43:37 -07005649 if (skip_adapt) {
Jingning Hanf6214b92017-04-12 11:43:37 -07005650 aom_free(tile_ctxs);
5651 aom_free(cdf_ptrs);
Jingning Hanf6214b92017-04-12 11:43:37 -07005652 return;
5653 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005654
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005655#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01005656 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005657 int i;
5658 /* Update reference frame id values based on the value of refresh_mask */
5659 for (i = 0; i < REF_FRAMES; i++) {
5660 if ((cm->refresh_mask >> i) & 1) {
5661 cm->ref_frame_id[i] = cm->current_frame_id;
5662 }
5663 }
5664 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005665#endif // CONFIG_REFERENCE_BUFFER
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005666
Yaowu Xuc27fc142016-08-22 16:08:15 -07005667#if DUMP_RECON_FRAMES == 1
5668 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5669 if (cm->show_frame) dump_filtered_recon_frames(cpi);
5670#endif // DUMP_RECON_FRAMES
5671
5672 if (cm->seg.update_map) update_reference_segmentation_map(cpi);
5673
5674 if (frame_is_intra_only(cm) == 0) {
5675 release_scaled_references(cpi);
5676 }
5677
Cheng Chen46f30c72017-09-07 11:13:33 -07005678 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005679
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005680#if CONFIG_ENTROPY_STATS
5681 av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
Zoe Liua56f9162017-06-21 22:49:57 -07005682 assert(cm->frame_context_idx < FRAME_CONTEXTS);
5683 av1_accumulate_frame_counts(&aggregate_fc_per_type[cm->frame_context_idx],
5684 &cm->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005685#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005686 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui sub53682f2017-08-01 17:09:18 -07005687#if CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07005688 av1_adapt_coef_probs(cm);
hui sub53682f2017-08-01 17:09:18 -07005689#endif // CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07005690 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00005691 make_update_tile_list_enc(cpi, cm->tile_rows, cm->tile_cols, tile_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01005692 av1_average_tile_coef_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005693 cm->tile_rows * cm->tile_cols);
Thomas Davies493623e2017-03-31 16:12:25 +01005694 av1_average_tile_intra_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005695 cm->tile_rows * cm->tile_cols);
Yushin Chob188ea12017-03-13 13:45:23 -07005696#if CONFIG_PVQ
5697 av1_average_tile_pvq_cdfs(cpi->common.fc, tile_ctxs,
5698 cm->tile_rows * cm->tile_cols);
5699#endif // CONFIG_PVQ
hui suff0da2b2017-03-07 15:51:37 -08005700#if CONFIG_ADAPT_SCAN
5701 av1_adapt_scan_order(cm);
5702#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005703 }
5704
5705 if (!frame_is_intra_only(cm)) {
5706 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005707 av1_adapt_inter_frame_probs(cm);
5708 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00005709 av1_average_tile_inter_cdfs(&cpi->common, cpi->common.fc, tile_ctxs,
Thomas Davies493623e2017-03-31 16:12:25 +01005710 cdf_ptrs, cm->tile_rows * cm->tile_cols);
5711 av1_average_tile_mv_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005712 cm->tile_rows * cm->tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005713 }
5714 }
5715
5716 if (cpi->refresh_golden_frame == 1)
5717 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5718 else
5719 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5720
5721 if (cpi->refresh_alt_ref_frame == 1)
5722 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5723 else
5724 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5725
5726#if CONFIG_EXT_REFS
5727 if (cpi->refresh_bwd_ref_frame == 1)
5728 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5729 else
5730 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
5731#endif // CONFIG_EXT_REFS
5732
5733#if !CONFIG_EXT_REFS
5734 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5735#endif // !CONFIG_EXT_REFS
5736
Yaowu Xuc27fc142016-08-22 16:08:15 -07005737 cm->last_frame_type = cm->frame_type;
5738
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005739#if CONFIG_XIPHRC
5740 frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
5741
5742 drop_this_frame =
5743 od_enc_rc_update_state(&cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5744 cpi->refresh_alt_ref_frame, frame_type, 0);
5745 if (drop_this_frame) {
5746 av1_rc_postencode_update_drop_frame(cpi);
5747 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005748 aom_free(tile_ctxs);
5749 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005750 return;
5751 }
Zoe Liue04abf72017-04-19 15:37:11 -07005752#else // !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005753 av1_rc_postencode_update(cpi, *size);
Zoe Liue04abf72017-04-19 15:37:11 -07005754#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005755
5756#if 0
5757 output_frame_level_debug_stats(cpi);
5758#endif
5759
5760 if (cm->frame_type == KEY_FRAME) {
5761 // Tell the caller that the frame was coded as a key frame
5762 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5763 } else {
5764 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5765 }
5766
5767 // Clear the one shot update flags for segmentation map and mode/ref loop
5768 // filter deltas.
5769 cm->seg.update_map = 0;
5770 cm->seg.update_data = 0;
5771 cm->lf.mode_ref_delta_update = 0;
5772
Yaowu Xuc27fc142016-08-22 16:08:15 -07005773 if (cm->show_frame) {
5774#if CONFIG_EXT_REFS
5775// TODO(zoeliu): We may only swamp mi and prev_mi for those frames that are
5776// being used as reference.
5777#endif // CONFIG_EXT_REFS
Cheng Chen46f30c72017-09-07 11:13:33 -07005778 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005779 // Don't increment frame counters if this was an altref buffer
5780 // update not a real frame
5781 ++cm->current_video_frame;
5782 }
5783
5784#if CONFIG_EXT_REFS
5785 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005786 if (cm->is_reference_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005787#endif // CONFIG_EXT_REFS
5788 cm->prev_frame = cm->cur_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005789 // keep track of the last coded dimensions
5790 cm->last_width = cm->width;
5791 cm->last_height = cm->height;
5792
5793 // reset to normal state now that we are done.
5794 cm->last_show_frame = cm->show_frame;
5795#if CONFIG_EXT_REFS
5796 }
5797#endif // CONFIG_EXT_REFS
Yi Luo10e23002017-07-31 11:54:43 -07005798
Thomas Davies493623e2017-03-31 16:12:25 +01005799 aom_free(tile_ctxs);
5800 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005801}
5802
Yaowu Xuf883b422016-08-30 14:01:10 -07005803static void Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005804 int skip_adapt, unsigned int *frame_flags) {
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005805#if CONFIG_XIPHRC
5806 int64_t ip_count;
5807 int frame_type, is_golden, is_altref;
5808
5809 /* Not updated during init so update it here */
5810 if (cpi->oxcf.rc_mode == AOM_Q) cpi->od_rc.quality = cpi->oxcf.cq_level;
5811
5812 frame_type = od_frame_type(&cpi->od_rc, cpi->od_rc.cur_frame, &is_golden,
5813 &is_altref, &ip_count);
5814
5815 if (frame_type == OD_I_FRAME) {
5816 frame_type = KEY_FRAME;
5817 cpi->frame_flags &= FRAMEFLAGS_KEY;
5818 } else if (frame_type == OD_P_FRAME) {
5819 frame_type = INTER_FRAME;
5820 }
5821
5822 if (is_altref) {
5823 cpi->refresh_alt_ref_frame = 1;
5824 cpi->rc.source_alt_ref_active = 1;
5825 }
5826
5827 cpi->refresh_golden_frame = is_golden;
5828 cpi->common.frame_type = frame_type;
5829 if (is_golden) cpi->frame_flags &= FRAMEFLAGS_GOLDEN;
5830#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005831 if (cpi->oxcf.rc_mode == AOM_CBR) {
5832 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005833 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005834 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005835 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005836#endif
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005837 encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005838}
5839
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005840#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005841static void Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005842 unsigned int *frame_flags) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005843 encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005844
5845#if CONFIG_EXT_REFS
5846 // Do not do post-encoding update for those frames that do not have a spot in
5847 // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
5848 // even when show_existing_frame is used.
5849 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005850 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005851 }
5852 check_show_existing_frame(cpi);
5853#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005854 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005855#endif // CONFIG_EXT_REFS
5856}
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005857#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005858
James Zern3e2613b2017-03-30 23:14:40 -07005859int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005860 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5861 int64_t end_time) {
5862 AV1_COMMON *const cm = &cpi->common;
5863 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005864 int res = 0;
5865 const int subsampling_x = sd->subsampling_x;
5866 const int subsampling_y = sd->subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005867#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005868 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5869#endif
5870
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005871#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005872 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5873#else
5874 check_initial_width(cpi, subsampling_x, subsampling_y);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005875#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005876
Yaowu Xuf883b422016-08-30 14:01:10 -07005877 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005878
Yaowu Xuf883b422016-08-30 14:01:10 -07005879 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005880#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005881 use_highbitdepth,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005882#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005883 frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005884 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005885 aom_usec_timer_mark(&timer);
5886 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005887
5888 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
5889 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005890 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005891 "Non-4:2:0 color format requires profile 1 or 3");
5892 res = -1;
5893 }
5894 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
5895 (subsampling_x == 1 && subsampling_y == 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005896 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005897 "4:2:0 color format requires profile 0 or 2");
5898 res = -1;
5899 }
5900
5901 return res;
5902}
5903
Yaowu Xuf883b422016-08-30 14:01:10 -07005904static int frame_is_reference(const AV1_COMP *cpi) {
5905 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005906
5907 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
5908 cpi->refresh_golden_frame ||
5909#if CONFIG_EXT_REFS
Zoe Liu3ac20932017-08-30 16:35:55 -07005910 cpi->refresh_bwd_ref_frame || cpi->refresh_alt2_ref_frame ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07005911#endif // CONFIG_EXT_REFS
5912 cpi->refresh_alt_ref_frame || !cm->error_resilient_mode ||
5913 cm->lf.mode_ref_delta_update || cm->seg.update_map ||
5914 cm->seg.update_data;
5915}
5916
Yaowu Xuf883b422016-08-30 14:01:10 -07005917static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005918 const struct lookahead_entry *source) {
5919 int64_t this_duration;
5920 int step = 0;
5921
5922 if (source->ts_start == cpi->first_time_stamp_ever) {
5923 this_duration = source->ts_end - source->ts_start;
5924 step = 1;
5925 } else {
5926 int64_t last_duration =
5927 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5928
5929 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5930
5931 // do a step update if the duration changes by 10%
5932 if (last_duration)
5933 step = (int)((this_duration - last_duration) * 10 / last_duration);
5934 }
5935
5936 if (this_duration) {
5937 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005938 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005939 } else {
5940 // Average this frame's rate into the last second's average
5941 // frame rate. If we haven't seen 1 second yet, then average
5942 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005943 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005944 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5945 double avg_duration = 10000000.0 / cpi->framerate;
5946 avg_duration *= (interval - avg_duration + this_duration);
5947 avg_duration /= interval;
5948
Yaowu Xuf883b422016-08-30 14:01:10 -07005949 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005950 }
5951 }
5952 cpi->last_time_stamp_seen = source->ts_start;
5953 cpi->last_end_time_stamp_seen = source->ts_end;
5954}
5955
5956// Returns 0 if this is not an alt ref else the offset of the source frame
5957// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005958static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005959 RATE_CONTROL *const rc = &cpi->rc;
5960 int arf_src_index = 0;
5961 if (is_altref_enabled(cpi)) {
5962 if (cpi->oxcf.pass == 2) {
5963 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5964 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5965 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5966 }
5967 } else if (rc->source_alt_ref_pending) {
5968 arf_src_index = rc->frames_till_gf_update_due;
5969 }
5970 }
5971 return arf_src_index;
5972}
5973
5974#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005975static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005976 int brf_src_index = 0;
5977 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5978
5979 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5980 // flag.
5981 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5982 if (cpi->oxcf.pass == 2) {
5983 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5984 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5985 } else {
5986 // TODO(zoeliu): To re-visit the setup for this scenario
5987 brf_src_index = cpi->rc.bipred_group_interval - 1;
5988 }
5989 }
5990
5991 return brf_src_index;
5992}
Zoe Liue9b15e22017-07-19 15:53:01 -07005993
Zoe Liue9b15e22017-07-19 15:53:01 -07005994// Returns 0 if this is not an alt ref else the offset of the source frame
5995// used as the arf midpoint.
5996static int get_arf2_src_index(AV1_COMP *cpi) {
5997 int arf2_src_index = 0;
5998 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5999 if (cpi->oxcf.pass == 2) {
6000 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
6001 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
6002 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
6003 }
6004 }
6005 }
6006 return arf2_src_index;
6007}
Yaowu Xuc27fc142016-08-22 16:08:15 -07006008#endif // CONFIG_EXT_REFS
6009
Yaowu Xuf883b422016-08-30 14:01:10 -07006010static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006011 const struct lookahead_entry *source) {
6012 RATE_CONTROL *const rc = &cpi->rc;
6013
6014 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07006015 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07006016
6017 if (cpi->oxcf.pass == 2) {
6018 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
6019 rc->is_src_frame_alt_ref =
6020#if CONFIG_EXT_REFS
6021 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Zoe Liu6cfaff92016-10-18 17:12:11 -07006022#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006023 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liu3ac20932017-08-30 16:35:55 -07006024#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07006025 rc->is_src_frame_ext_arf =
6026 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Zoe Liu3ac20932017-08-30 16:35:55 -07006027#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006028 } else {
6029 rc->is_src_frame_alt_ref =
6030 cpi->alt_ref_source && (source == cpi->alt_ref_source);
6031 }
6032
6033 if (rc->is_src_frame_alt_ref) {
6034 // Current frame is an ARF overlay frame.
6035 cpi->alt_ref_source = NULL;
6036
Zoe Liu3ac20932017-08-30 16:35:55 -07006037#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07006038 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
6039 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
6040 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
6041 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
6042 cpi->refresh_last_frame = 1;
6043 } else {
Zoe Liu3ac20932017-08-30 16:35:55 -07006044#endif // CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07006045 // Don't refresh the last buffer for an ARF overlay frame. It will
6046 // become the GF so preserve last as an alternative prediction option.
6047 cpi->refresh_last_frame = 0;
Zoe Liu3ac20932017-08-30 16:35:55 -07006048#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07006049 }
Zoe Liu3ac20932017-08-30 16:35:55 -07006050#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006051 }
6052}
6053
6054#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006055extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6056 const unsigned char *img2, int img2_pitch,
6057 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006058
6059static void adjust_image_stat(double y, double u, double v, double all,
6060 ImageStat *s) {
6061 s->stat[Y] += y;
6062 s->stat[U] += u;
6063 s->stat[V] += v;
6064 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006065 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006066}
6067
Angie Chiang08a22a62017-07-17 17:29:17 -07006068static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006069 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006070 double samples = 0.0;
6071 uint32_t in_bit_depth = 8;
6072 uint32_t bit_depth = 8;
6073
Angie Chiang08a22a62017-07-17 17:29:17 -07006074#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07006075 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006076#endif
6077 cpi->bytes += frame_bytes;
6078
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006079#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006080 if (cm->use_highbitdepth) {
6081 in_bit_depth = cpi->oxcf.input_bit_depth;
6082 bit_depth = cm->bit_depth;
6083 }
6084#endif
6085 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006086 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006087 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
6088 double y, u, v, frame_all;
6089
6090 cpi->count++;
6091 if (cpi->b_calculate_psnr) {
6092 PSNR_STATS psnr;
6093 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006094 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006095// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006096#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07006097 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006098#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006099 aom_calc_psnr(orig, recon, &psnr);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006100#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006101
6102 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6103 &cpi->psnr);
6104 cpi->total_sq_error += psnr.sse[0];
6105 cpi->total_samples += psnr.samples[0];
6106 samples = psnr.samples[0];
6107// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006108#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006109 if (cm->use_highbitdepth)
6110 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006111 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006112 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006113 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006114#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006115 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006116#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006117
Yaowu Xuf883b422016-08-30 14:01:10 -07006118 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006119 cpi->summed_quality += frame_ssim2 * weight;
6120 cpi->summed_weights += weight;
6121
6122#if 0
6123 {
6124 FILE *f = fopen("q_used.stt", "a");
6125 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
6126 cpi->common.current_video_frame, y2, u2, v2,
6127 frame_psnr2, frame_ssim2);
6128 fclose(f);
6129 }
6130#endif
6131 }
6132 if (cpi->b_calculate_blockiness) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006133#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006134 if (!cm->use_highbitdepth)
6135#endif
6136 {
6137 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006138 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6139 recon->y_stride, orig->y_width, orig->y_height);
6140 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006141 cpi->total_blockiness += frame_blockiness;
6142 }
6143
6144 if (cpi->b_calculate_consistency) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006145#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006146 if (!cm->use_highbitdepth)
6147#endif
6148 {
Yaowu Xuf883b422016-08-30 14:01:10 -07006149 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006150 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6151 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6152
6153 const double peak = (double)((1 << in_bit_depth) - 1);
6154 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006155 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006156 if (consistency > 0.0)
6157 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006158 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006159 cpi->total_inconsistency += this_inconsistency;
6160 }
6161 }
6162 }
6163
6164 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006165 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006166 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006167 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006168 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6169 }
6170}
6171#endif // CONFIG_INTERNAL_STATS
6172
RogerZhou3b635242017-09-19 10:06:46 -07006173#if CONFIG_AMVR
6174static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
6175 const YV12_BUFFER_CONFIG *last_picture,
6176 hash_table *last_hash_table) {
6177 aom_clear_system_state();
6178 // check use hash ME
6179 int k;
6180 uint32_t hash_value_1;
6181 uint32_t hash_value_2;
6182
6183 const int block_size = 8;
6184 const double threshold_current = 0.8;
6185 const double threshold_average = 0.95;
6186 const int max_history_size = 32;
6187 int T = 0; // total block
6188 int C = 0; // match with collocated block
6189 int S = 0; // smooth region but not match with collocated block
6190 int M = 0; // match with other block
6191
6192 const int pic_width = cur_picture->y_width;
6193 const int pic_height = cur_picture->y_height;
6194 for (int i = 0; i + block_size <= pic_height; i += block_size) {
6195 for (int j = 0; j + block_size <= pic_width; j += block_size) {
6196 const int x_pos = j;
6197 const int y_pos = i;
6198 int match = 1;
6199 T++;
6200
6201 // check whether collocated block match with current
6202 uint8_t *p_cur = cur_picture->y_buffer;
6203 uint8_t *p_ref = last_picture->y_buffer;
6204 int stride_cur = cur_picture->y_stride;
6205 int stride_ref = last_picture->y_stride;
6206 p_cur += (y_pos * stride_cur + x_pos);
6207 p_ref += (y_pos * stride_ref + x_pos);
6208
6209 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
6210 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
6211 if (p_cur[tmpX] != p_ref[tmpX]) {
6212 match = 0;
6213 }
6214 }
6215 p_cur += stride_cur;
6216 p_ref += stride_ref;
6217 }
6218
6219 if (match) {
6220 C++;
6221 continue;
6222 }
6223
6224 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
6225 y_pos) ||
6226 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
6227 S++;
6228 continue;
6229 }
6230
6231 av1_get_block_hash_value(
6232 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
6233 block_size, &hash_value_1, &hash_value_2);
6234
6235 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
6236 M++;
6237 }
6238 }
6239 }
6240
6241 assert(T > 0);
6242 double csm_rate = ((double)(C + S + M)) / ((double)(T));
6243 double m_rate = ((double)(M)) / ((double)(T));
6244
6245 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
6246 cpi->m_rate_array[cpi->rate_index] = m_rate;
6247
6248 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
6249 cpi->rate_size++;
6250 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
6251
6252 if (csm_rate < threshold_current) {
6253 return 0;
6254 }
6255
6256 if (C == T) {
6257 return 1;
6258 }
6259
6260 double csm_average = 0.0;
6261 double m_average = 0.0;
6262
6263 for (k = 0; k < cpi->rate_size; k++) {
6264 csm_average += cpi->csm_rate_array[k];
6265 m_average += cpi->m_rate_array[k];
6266 }
6267 csm_average /= cpi->rate_size;
6268 m_average /= cpi->rate_size;
6269
6270 if (csm_average < threshold_average) {
6271 return 0;
6272 }
6273
6274 if (M > (T - C - S) / 3) {
6275 return 1;
6276 }
6277
6278 if (csm_rate > 0.99 && m_rate > 0.01) {
6279 return 1;
6280 }
6281
6282 if (csm_average + m_average > 1.01) {
6283 return 1;
6284 }
6285
6286 return 0;
6287}
6288#endif
6289
Yaowu Xuf883b422016-08-30 14:01:10 -07006290int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6291 size_t *size, uint8_t *dest, int64_t *time_stamp,
6292 int64_t *time_end, int flush) {
6293 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6294 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006295 BufferPool *const pool = cm->buffer_pool;
6296 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07006297 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006298 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
6299 struct lookahead_entry *last_source = NULL;
6300 struct lookahead_entry *source = NULL;
6301 int arf_src_index;
6302#if CONFIG_EXT_REFS
6303 int brf_src_index;
6304#endif // CONFIG_EXT_REFS
6305 int i;
6306
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006307#if CONFIG_XIPHRC
6308 cpi->od_rc.end_of_input = flush;
6309#endif
6310
Yaowu Xuc27fc142016-08-22 16:08:15 -07006311#if CONFIG_BITSTREAM_DEBUG
6312 assert(cpi->oxcf.max_threads == 0 &&
6313 "bitstream debug tool does not support multithreading");
6314 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07006315 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006316#endif
6317
Yaowu Xuf883b422016-08-30 14:01:10 -07006318 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006319
RogerZhou3b635242017-09-19 10:06:46 -07006320#if CONFIG_AMVR
6321 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
6322#else
Cheng Chen46f30c72017-09-07 11:13:33 -07006323 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
RogerZhou3b635242017-09-19 10:06:46 -07006324#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006325
6326 // Is multi-arf enabled.
6327 // Note that at the moment multi_arf is only configured for 2 pass VBR
6328 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
6329 cpi->multi_arf_allowed = 1;
6330 else
6331 cpi->multi_arf_allowed = 0;
6332
Thomas Daedea6a854b2017-06-22 17:49:11 -07006333// Normal defaults
6334#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07006335 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07006336#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006337 cm->refresh_frame_context =
6338 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
6339 ? REFRESH_FRAME_CONTEXT_FORWARD
6340 : REFRESH_FRAME_CONTEXT_BACKWARD;
6341
6342 cpi->refresh_last_frame = 1;
6343 cpi->refresh_golden_frame = 0;
6344#if CONFIG_EXT_REFS
6345 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006346 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006347#endif // CONFIG_EXT_REFS
6348 cpi->refresh_alt_ref_frame = 0;
6349
Zoe Liue04abf72017-04-19 15:37:11 -07006350#if CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07006351 if (oxcf->pass == 2 && cm->show_existing_frame) {
6352 // Manage the source buffer and flush out the source frame that has been
6353 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07006354 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006355 *size = 0;
6356 return -1;
6357 }
Alex Conversef77fd0b2017-04-20 11:00:24 -07006358 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006359 // TODO(zoeliu): To track down to determine whether it's needed to adjust
6360 // the frame rate.
6361 *time_stamp = source->ts_start;
6362 *time_end = source->ts_end;
6363
6364 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006365 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006366
6367 // Find a free buffer for the new frame, releasing the reference previously
6368 // held.
6369 if (cm->new_fb_idx != INVALID_IDX) {
6370 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6371 }
6372 cm->new_fb_idx = get_free_fb(cm);
6373
6374 if (cm->new_fb_idx == INVALID_IDX) return -1;
6375
6376 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006377 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006378
6379 // Start with a 0 size frame.
6380 *size = 0;
6381
6382 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07006383 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006384
6385 Pass2Encode(cpi, size, dest, frame_flags);
6386
6387 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
6388
6389#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07006390 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006391#endif // CONFIG_INTERNAL_STATS
6392
6393 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006394 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006395
6396 cm->show_existing_frame = 0;
6397 return 0;
6398 }
Zoe Liue04abf72017-04-19 15:37:11 -07006399#endif // CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07006400
6401 // Should we encode an arf frame.
6402 arf_src_index = get_arf_src_index(cpi);
6403 if (arf_src_index) {
6404 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006405 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006406 // Avoid creating an alt-ref if there's a forced keyframe pending.
6407 if (e == NULL) {
6408 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07006409 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006410 arf_src_index = 0;
6411 flush = 1;
6412 break;
6413 }
6414 }
6415 }
6416
6417 if (arf_src_index) {
6418 assert(arf_src_index <= rc->frames_to_key);
6419
Yaowu Xuf883b422016-08-30 14:01:10 -07006420 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006421 cpi->alt_ref_source = source;
6422
6423 if (oxcf->arnr_max_frames > 0) {
Todd Nguyen302d0972017-06-16 16:16:29 -07006424// Produce the filtered ARF frame.
6425#if CONFIG_BGSPRITE
6426 int bgsprite_ret = av1_background_sprite(cpi, arf_src_index);
Todd Nguyen2fc23092017-07-11 12:00:36 -07006427 // Do temporal filter if bgsprite not generated.
6428 if (bgsprite_ret != 0)
Todd Nguyen302d0972017-06-16 16:16:29 -07006429#endif // CONFIG_BGSPRITE
Todd Nguyen8493f912017-07-20 12:45:12 -07006430 av1_temporal_filter(cpi,
6431#if CONFIG_BGSPRITE
Todd Nguyen1fd99c22017-07-31 17:50:41 -07006432 NULL, &cpi->alt_ref_buffer,
Todd Nguyen8493f912017-07-20 12:45:12 -07006433#endif // CONFIG_BGSPRITE
6434 arf_src_index);
Yaowu Xuf883b422016-08-30 14:01:10 -07006435 aom_extend_frame_borders(&cpi->alt_ref_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006436 force_src_buffer = &cpi->alt_ref_buffer;
6437 }
6438
6439 cm->show_frame = 0;
6440 cm->intra_only = 0;
6441 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006442 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006443 cpi->refresh_golden_frame = 0;
6444#if CONFIG_EXT_REFS
6445 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006446 cpi->refresh_alt2_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006447#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006448 rc->is_src_frame_alt_ref = 0;
6449 }
6450 rc->source_alt_ref_pending = 0;
6451 }
6452
6453#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07006454 // Should we encode an arf2 frame.
6455 arf_src_index = get_arf2_src_index(cpi);
6456 if (arf_src_index) {
6457 for (i = 0; i <= arf_src_index; ++i) {
6458 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
6459 // Avoid creating an alt-ref if there's a forced keyframe pending.
6460 if (e == NULL) {
6461 break;
6462 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
6463 arf_src_index = 0;
6464 flush = 1;
6465 break;
6466 }
6467 }
6468 }
6469
6470 if (arf_src_index) {
6471 assert(arf_src_index <= rc->frames_to_key);
6472
6473 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
6474 cpi->alt_ref_source = source;
6475
6476 if (oxcf->arnr_max_frames > 0) {
6477 // Produce the filtered ARF frame.
Zoe Liu3ac20932017-08-30 16:35:55 -07006478 av1_temporal_filter(cpi,
6479#if CONFIG_BGSPRITE
6480 NULL, NULL,
6481#endif // CONFIG_BGSPRITE
6482 arf_src_index);
Zoe Liue9b15e22017-07-19 15:53:01 -07006483 aom_extend_frame_borders(&cpi->alt_ref_buffer);
6484 force_src_buffer = &cpi->alt_ref_buffer;
6485 }
6486
6487 cm->show_frame = 0;
6488 cm->intra_only = 0;
6489 cpi->refresh_alt2_ref_frame = 1;
6490 cpi->refresh_last_frame = 0;
6491 cpi->refresh_golden_frame = 0;
6492 cpi->refresh_bwd_ref_frame = 0;
6493 cpi->refresh_alt_ref_frame = 0;
6494 rc->is_src_frame_alt_ref = 0;
6495 rc->is_src_frame_ext_arf = 0;
6496 }
6497 rc->source_alt_ref_pending = 0;
6498 }
Zoe Liue9b15e22017-07-19 15:53:01 -07006499
Yaowu Xuc27fc142016-08-22 16:08:15 -07006500 rc->is_bwd_ref_frame = 0;
6501 brf_src_index = get_brf_src_index(cpi);
6502 if (brf_src_index) {
6503 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07006504 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006505 cm->show_frame = 0;
6506 cm->intra_only = 0;
6507
6508 cpi->refresh_bwd_ref_frame = 1;
6509 cpi->refresh_last_frame = 0;
6510 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07006511 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006512 cpi->refresh_alt_ref_frame = 0;
6513
6514 rc->is_bwd_ref_frame = 1;
6515 }
6516 }
6517#endif // CONFIG_EXT_REFS
6518
6519 if (!source) {
6520 // Get last frame source.
6521 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006522 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006523 return -1;
6524 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006525 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006526 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07006527 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006528
6529 if (source != NULL) {
6530 cm->show_frame = 1;
6531 cm->intra_only = 0;
6532
6533 // Check to see if the frame should be encoded as an arf overlay.
6534 check_src_altref(cpi, source);
6535 }
6536 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006537 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006538 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07006539 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006540 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
6541
6542 *time_stamp = source->ts_start;
6543 *time_end = source->ts_end;
Yaowu Xuf883b422016-08-30 14:01:10 -07006544 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006545
6546 } else {
6547 *size = 0;
6548 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006549#if CONFIG_XIPHRC
6550 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 1);
6551#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006552 av1_end_first_pass(cpi); /* get last stats packet */
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006553#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006554 cpi->twopass.first_pass_done = 1;
6555 }
6556 return -1;
6557 }
6558
6559 if (source->ts_start < cpi->first_time_stamp_ever) {
6560 cpi->first_time_stamp_ever = source->ts_start;
6561 cpi->last_end_time_stamp_seen = source->ts_start;
6562 }
6563
6564 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006565 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006566
6567 // adjust frame rates based on timestamps given
6568 if (cm->show_frame) adjust_frame_rate(cpi, source);
6569
6570 // Find a free buffer for the new frame, releasing the reference previously
6571 // held.
6572 if (cm->new_fb_idx != INVALID_IDX) {
6573 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6574 }
6575 cm->new_fb_idx = get_free_fb(cm);
6576
6577 if (cm->new_fb_idx == INVALID_IDX) return -1;
6578
6579 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Yaowu Xu9b0f7032017-07-31 11:01:19 -07006580#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
6581 cm->cur_frame->buf.buf_8bit_valid = 0;
6582#endif
Zoe Liu3ac20932017-08-30 16:35:55 -07006583#if !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006584 if (cpi->multi_arf_allowed) {
6585 if (cm->frame_type == KEY_FRAME) {
6586 init_buffer_indices(cpi);
6587 } else if (oxcf->pass == 2) {
6588 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
6589 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
6590 }
6591 }
Zoe Liu3ac20932017-08-30 16:35:55 -07006592#endif // !CONFIG_EXT_REFS
Zoe Liu6cfaff92016-10-18 17:12:11 -07006593
Yaowu Xuc27fc142016-08-22 16:08:15 -07006594 // Start with a 0 size frame.
6595 *size = 0;
6596
6597 cpi->frame_flags = *frame_flags;
6598
6599 if (oxcf->pass == 2) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006600#if CONFIG_XIPHRC
6601 if (od_enc_rc_2pass_in(&cpi->od_rc) < 0) return -1;
6602 }
6603#else
Yaowu Xuf883b422016-08-30 14:01:10 -07006604 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006605 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07006606 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006607 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006608#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006609
6610 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
6611 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i)
6612 cpi->scaled_ref_idx[i] = INVALID_IDX;
6613 }
6614
6615#if CONFIG_AOM_QM
6616 cm->using_qmatrix = cpi->oxcf.using_qm;
6617 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
6618 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
6619#endif
6620
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006621#if CONFIG_REFERENCE_BUFFER
David Barker5e70a112017-10-03 14:28:17 +01006622 if (cm->seq_params.frame_id_numbers_present_flag) {
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006623 if (*time_stamp == 0) {
6624 cpi->common.current_frame_id = -1;
6625 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006626 }
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006627#endif // CONFIG_REFERENCE_BUFFER
RogerZhou3b635242017-09-19 10:06:46 -07006628#if CONFIG_AMVR
6629 cpi->cur_poc++;
6630 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
6631 if (cpi->common.seq_mv_precision_level == 2) {
6632 struct lookahead_entry *previous_entry =
6633 cpi->lookahead->buf + cpi->previsous_index;
6634 cpi->common.cur_frame_mv_precision_level = is_integer_mv(
6635 cpi, cpi->source, &previous_entry->img, cpi->previsou_hash_table);
6636 } else {
6637 cpi->common.cur_frame_mv_precision_level =
6638 cpi->common.seq_mv_precision_level;
6639 }
6640 } else {
6641 cpi->common.cur_frame_mv_precision_level = 0;
6642 }
6643#endif
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006644
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006645#if CONFIG_XIPHRC
6646 if (oxcf->pass == 1) {
6647 size_t tmp;
6648 if (cpi->od_rc.cur_frame == 0) Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6649 cpi->od_rc.firstpass_quant = cpi->od_rc.target_quantizer;
6650 Pass0Encode(cpi, &tmp, dest, 0, frame_flags);
6651 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 0);
6652 } else if (oxcf->pass == 2) {
6653 Pass0Encode(cpi, size, dest, 0, frame_flags);
6654 } else {
6655 if (cpi->od_rc.cur_frame == 0) {
6656 size_t tmp;
6657 Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
6658 }
6659 Pass0Encode(cpi, size, dest, 0, frame_flags);
6660 }
6661#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006662 if (oxcf->pass == 1) {
6663 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07006664 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006665 } else if (oxcf->pass == 2) {
6666 Pass2Encode(cpi, size, dest, frame_flags);
6667 } else {
6668 // One pass encode
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006669 Pass0Encode(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006670 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00006671#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07006672#if CONFIG_HASH_ME
6673 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
RogerZhou3b635242017-09-19 10:06:46 -07006674#if CONFIG_AMVR
6675 cpi->previsou_hash_table = &cm->cur_frame->hash_table;
6676 {
6677 int l;
6678 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
6679 if ((cpi->lookahead->buf + l) == source) {
6680 cpi->previsous_index = l;
6681 break;
6682 }
6683 }
6684
6685 if (l == cpi->lookahead->max_sz) {
6686 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6687 "Failed to find last frame original buffer");
6688 }
6689 }
6690#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07006691 }
6692
6693#endif
6694
Thomas Daededa4d8b92017-06-05 15:44:14 -07006695#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -07006696 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006697#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07006698 if (!cm->error_resilient_mode)
6699 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07006700#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07006701
6702 // No frame encoded, or frame was dropped, release scaled references.
6703 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6704 release_scaled_references(cpi);
6705 }
6706
6707 if (*size > 0) {
6708 cpi->droppable = !frame_is_reference(cpi);
6709 }
6710
Yaowu Xuf883b422016-08-30 14:01:10 -07006711 aom_usec_timer_mark(&cmptimer);
6712 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006713
6714 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6715 generate_psnr_packet(cpi);
6716
6717#if CONFIG_INTERNAL_STATS
6718 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006719 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006720 }
6721#endif // CONFIG_INTERNAL_STATS
6722
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006723#if CONFIG_XIPHRC
6724 cpi->od_rc.cur_frame++;
6725#endif
6726
Yaowu Xuf883b422016-08-30 14:01:10 -07006727 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006728
6729 return 0;
6730}
6731
Yaowu Xuf883b422016-08-30 14:01:10 -07006732int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6733 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006734 if (!cm->show_frame) {
6735 return -1;
6736 } else {
6737 int ret;
6738 if (cm->frame_to_show) {
6739 *dest = *cm->frame_to_show;
6740 dest->y_width = cm->width;
6741 dest->y_height = cm->height;
6742 dest->uv_width = cm->width >> cm->subsampling_x;
6743 dest->uv_height = cm->height >> cm->subsampling_y;
6744 ret = 0;
6745 } else {
6746 ret = -1;
6747 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006748 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006749 return ret;
6750 }
6751}
6752
Yaowu Xuf883b422016-08-30 14:01:10 -07006753int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006754 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6755
6756 *frame =
6757 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6758 return 0;
6759}
6760
Yaowu Xuf883b422016-08-30 14:01:10 -07006761int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6762 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006763 int hr = 0, hs = 0, vr = 0, vs = 0;
6764
6765 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6766
6767 Scale2Ratio(horiz_mode, &hr, &hs);
6768 Scale2Ratio(vert_mode, &vr, &vs);
6769
6770 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006771 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6772 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006773
6774 return 0;
6775}
6776
Yaowu Xuf883b422016-08-30 14:01:10 -07006777int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006778
Yaowu Xuf883b422016-08-30 14:01:10 -07006779void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006780 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07006781 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF)) {
6782 int ref = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006783
Yaowu Xuf883b422016-08-30 14:01:10 -07006784 if (flags & AOM_EFLAG_NO_REF_LAST) {
6785 ref ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006786#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006787 ref ^= AOM_LAST2_FLAG;
6788 ref ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006789#endif // CONFIG_EXT_REFS
6790 }
6791
Yaowu Xuf883b422016-08-30 14:01:10 -07006792 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006793
Yaowu Xuf883b422016-08-30 14:01:10 -07006794 if (flags & AOM_EFLAG_NO_REF_ARF) ref ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006795
Yaowu Xuf883b422016-08-30 14:01:10 -07006796 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006797 }
6798
6799 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07006800 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
6801 AOM_EFLAG_FORCE_GF | AOM_EFLAG_FORCE_ARF)) {
6802 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006803
Yaowu Xuf883b422016-08-30 14:01:10 -07006804 if (flags & AOM_EFLAG_NO_UPD_LAST) {
6805 upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006806#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006807 upd ^= AOM_LAST2_FLAG;
6808 upd ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006809#endif // CONFIG_EXT_REFS
6810 }
6811
Yaowu Xuf883b422016-08-30 14:01:10 -07006812 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006813
Yaowu Xuf883b422016-08-30 14:01:10 -07006814 if (flags & AOM_EFLAG_NO_UPD_ARF) upd ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006815
Yaowu Xuf883b422016-08-30 14:01:10 -07006816 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006817 }
6818
Yaowu Xuf883b422016-08-30 14:01:10 -07006819 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6820 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006821 }
6822}