blob: 30d1188db1160d3ce7082793a6dd0b72b8f976af [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"
Yaowu Xuc27fc142016-08-22 16:08:15 -070021#include "av1/common/clpf.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050022#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -070023#include "av1/common/filter.h"
24#include "av1/common/idct.h"
25#include "av1/common/reconinter.h"
26#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070027#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070028#include "av1/common/tile_common.h"
29
30#include "av1/encoder/aq_complexity.h"
31#include "av1/encoder/aq_cyclicrefresh.h"
32#include "av1/encoder/aq_variance.h"
33#include "av1/encoder/bitstream.h"
34#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -070035#include "aom_dsp/buf_ans.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070036#endif
37#include "av1/encoder/context_tree.h"
38#include "av1/encoder/encodeframe.h"
39#include "av1/encoder/encodemv.h"
40#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070041#if CONFIG_LV_MAP
42#include "av1/encoder/encodetxb.h"
43#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070044#include "av1/encoder/ethread.h"
45#include "av1/encoder/firstpass.h"
46#include "av1/encoder/mbgraph.h"
47#include "av1/encoder/picklpf.h"
48#if CONFIG_LOOP_RESTORATION
49#include "av1/encoder/pickrst.h"
50#endif // CONFIG_LOOP_RESTORATION
51#include "av1/encoder/ratectrl.h"
52#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070053#include "av1/encoder/segmentation.h"
54#include "av1/encoder/speed_features.h"
55#include "av1/encoder/temporal_filter.h"
56
Yaowu Xuf883b422016-08-30 14:01:10 -070057#include "./av1_rtcd.h"
58#include "./aom_dsp_rtcd.h"
59#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070060#include "aom_dsp/psnr.h"
61#if CONFIG_INTERNAL_STATS
62#include "aom_dsp/ssim.h"
63#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070064#include "aom_dsp/aom_dsp_common.h"
65#include "aom_dsp/aom_filter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070066#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#include "aom_ports/mem.h"
68#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070069#include "aom_scale/aom_scale.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070070#if CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070071#include "aom_util/debug_util.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070072#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070073
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080074#if CONFIG_ENTROPY_STATS
75FRAME_COUNTS aggregate_fc;
76#endif // CONFIG_ENTROPY_STATS
77
Yaowu Xuc27fc142016-08-22 16:08:15 -070078#define AM_SEGMENT_ID_INACTIVE 7
79#define AM_SEGMENT_ID_ACTIVE 0
80
81#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
82
83#define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
84 // for altref computation.
85#define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
86 // mv. Choose a very high value for
87 // now so that HIGH_PRECISION is always
88 // chosen.
89// #define OUTPUT_YUV_REC
90#ifdef OUTPUT_YUV_DENOISED
91FILE *yuv_denoised_file = NULL;
92#endif
93#ifdef OUTPUT_YUV_SKINMAP
94FILE *yuv_skinmap_file = NULL;
95#endif
96#ifdef OUTPUT_YUV_REC
97FILE *yuv_rec_file;
98#define FILE_NAME_LEN 100
99#endif
100
101#if 0
102FILE *framepsnr;
103FILE *kf_list;
104FILE *keyfile;
105#endif
106
Luc Trudeauf8164152017-04-11 16:20:51 -0400107#if CONFIG_CFL
108CFL_CTX NULL_CFL;
109#endif
110
Urvang Joshib5ed3502016-10-17 16:38:05 -0700111#if CONFIG_INTERNAL_STATS
112typedef enum { Y, U, V, ALL } STAT_TYPE;
113#endif // CONFIG_INTERNAL_STATS
114
Yaowu Xuf883b422016-08-30 14:01:10 -0700115static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700116 switch (mode) {
117 case NORMAL:
118 *hr = 1;
119 *hs = 1;
120 break;
121 case FOURFIVE:
122 *hr = 4;
123 *hs = 5;
124 break;
125 case THREEFIVE:
126 *hr = 3;
127 *hs = 5;
128 break;
129 case ONETWO:
130 *hr = 1;
131 *hs = 2;
132 break;
133 default:
134 *hr = 1;
135 *hs = 1;
136 assert(0);
137 break;
138 }
139}
140
141// Mark all inactive blocks as active. Other segmentation features may be set
142// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700143static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700144 unsigned char *const seg_map = cpi->segmentation_map;
145 int i;
146 if (cpi->active_map.enabled || cpi->active_map.update)
147 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
148 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
149 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
150}
151
Yaowu Xuf883b422016-08-30 14:01:10 -0700152static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700153 struct segmentation *const seg = &cpi->common.seg;
154 unsigned char *const seg_map = cpi->segmentation_map;
155 const unsigned char *const active_map = cpi->active_map.map;
156 int i;
157
158 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
159
160 if (frame_is_intra_only(&cpi->common)) {
161 cpi->active_map.enabled = 0;
162 cpi->active_map.update = 1;
163 }
164
165 if (cpi->active_map.update) {
166 if (cpi->active_map.enabled) {
167 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
168 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700169 av1_enable_segmentation(seg);
170 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
171 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172 // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
173 // filter level being zero regardless of the value of seg->abs_delta.
Yaowu Xuf883b422016-08-30 14:01:10 -0700174 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
175 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700176 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700177 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
178 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179 if (seg->enabled) {
180 seg->update_data = 1;
181 seg->update_map = 1;
182 }
183 }
184 cpi->active_map.update = 0;
185 }
186}
187
Yaowu Xuf883b422016-08-30 14:01:10 -0700188int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
189 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700190 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
191 unsigned char *const active_map_8x8 = cpi->active_map.map;
192 const int mi_rows = cpi->common.mi_rows;
193 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700194 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
195 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700196 cpi->active_map.update = 1;
197 if (new_map_16x16) {
198 int r, c;
199 for (r = 0; r < mi_rows; ++r) {
200 for (c = 0; c < mi_cols; ++c) {
201 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700202 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 ? AM_SEGMENT_ID_ACTIVE
204 : AM_SEGMENT_ID_INACTIVE;
205 }
206 }
207 cpi->active_map.enabled = 1;
208 } else {
209 cpi->active_map.enabled = 0;
210 }
211 return 0;
212 } else {
213 return -1;
214 }
215}
216
Yaowu Xuf883b422016-08-30 14:01:10 -0700217int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
218 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
220 new_map_16x16) {
221 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
222 const int mi_rows = cpi->common.mi_rows;
223 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700224 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
225 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
226
Yaowu Xuc27fc142016-08-22 16:08:15 -0700227 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
228 if (cpi->active_map.enabled) {
229 int r, c;
230 for (r = 0; r < mi_rows; ++r) {
231 for (c = 0; c < mi_cols; ++c) {
232 // Cyclic refresh segments are considered active despite not having
233 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700234 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700235 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
236 }
237 }
238 }
239 return 0;
240 } else {
241 return -1;
242 }
243}
244
Yaowu Xuf883b422016-08-30 14:01:10 -0700245void av1_set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700246 MACROBLOCK *const mb = &cpi->td.mb;
247 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
248
Yaowu Xuc27fc142016-08-22 16:08:15 -0700249 if (cpi->common.allow_high_precision_mv) {
250 int i;
251 for (i = 0; i < NMV_CONTEXTS; ++i) {
252 mb->mv_cost_stack[i] = mb->nmvcost_hp[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700253 }
254 } else {
255 int i;
256 for (i = 0; i < NMV_CONTEXTS; ++i) {
257 mb->mv_cost_stack[i] = mb->nmvcost[i];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258 }
259 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700260}
261
Yaowu Xuf883b422016-08-30 14:01:10 -0700262static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700263#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700264 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265 return BLOCK_64X64;
266
Yaowu Xuf883b422016-08-30 14:01:10 -0700267 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700268 return BLOCK_128X128;
269
Yaowu Xuf883b422016-08-30 14:01:10 -0700270 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700271
272 assert(IMPLIES(cpi->common.tile_cols > 1,
273 cpi->common.tile_width % MAX_MIB_SIZE == 0));
274 assert(IMPLIES(cpi->common.tile_rows > 1,
275 cpi->common.tile_height % MAX_MIB_SIZE == 0));
276
277 // TODO(any): Possibly could improve this with a heuristic.
278 return BLOCK_128X128;
279#else
280 (void)cpi;
281 return BLOCK_64X64;
282#endif // CONFIG_EXT_PARTITION
283}
284
Yaowu Xuf883b422016-08-30 14:01:10 -0700285static void setup_frame(AV1_COMP *cpi) {
286 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287 // Set up entropy context depending on frame type. The decoder mandates
288 // the use of the default context, index 0, for keyframes and inter
289 // frames where the error_resilient_mode or intra_only flag is set. For
290 // other inter-frames the encoder currently uses only two contexts;
291 // context 1 for ALTREF frames and context 0 for the others.
292 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700293 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 } else {
295#if CONFIG_EXT_REFS
296 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
297 if (gf_group->rf_level[gf_group->index] == GF_ARF_LOW)
298 cm->frame_context_idx = EXT_ARF_FRAME;
299 else if (cpi->refresh_alt_ref_frame)
300 cm->frame_context_idx = ARF_FRAME;
301#else
302 if (cpi->refresh_alt_ref_frame) cm->frame_context_idx = ARF_FRAME;
Zoe Liu6cfaff92016-10-18 17:12:11 -0700303#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304 else if (cpi->rc.is_src_frame_alt_ref)
305 cm->frame_context_idx = OVERLAY_FRAME;
306 else if (cpi->refresh_golden_frame)
307 cm->frame_context_idx = GLD_FRAME;
308#if CONFIG_EXT_REFS
309 else if (cpi->refresh_bwd_ref_frame)
310 cm->frame_context_idx = BRF_FRAME;
Zoe Liu6cfaff92016-10-18 17:12:11 -0700311#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700312 else
313 cm->frame_context_idx = REGULAR_FRAME;
314 }
315
316 if (cm->frame_type == KEY_FRAME) {
317 cpi->refresh_golden_frame = 1;
318 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700319 av1_zero(cpi->interp_filter_selected);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700320 } else {
321 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
Yaowu Xuf883b422016-08-30 14:01:10 -0700322 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323 }
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200324#if CONFIG_EXT_REFS
Arild Fuldseth (arilfuld)78bfc282017-05-24 12:06:35 +0200325#if CONFIG_ONE_SIDED_COMPOUND // No change to bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200326 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
327 cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
328 cpi->rc.is_bipred_frame = 1;
329 }
330#endif
331#endif
Thomas Daede10e1da92017-04-26 13:22:21 -0700332 cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -0700333
334 cpi->vaq_refresh = 0;
335
336 set_sb_size(cm, select_sb_size(cpi));
337}
338
Yaowu Xuf883b422016-08-30 14:01:10 -0700339static void av1_enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700340 int i;
341 cm->mi = cm->mip + cm->mi_stride + 1;
342 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
343 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
344 // Clear top border row
345 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
346 // Clear left border column
347 for (i = 1; i < cm->mi_rows + 1; ++i)
348 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
349
350 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
351 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
352
353 memset(cm->mi_grid_base, 0,
354 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
355}
356
Yaowu Xuf883b422016-08-30 14:01:10 -0700357static int av1_enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
358 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700359 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700360 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700361 if (!cm->prev_mip) return 1;
362 cm->mi_alloc_size = mi_size;
363
Yaowu Xuf883b422016-08-30 14:01:10 -0700364 cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700365 if (!cm->mi_grid_base) return 1;
366 cm->prev_mi_grid_base =
Yaowu Xuf883b422016-08-30 14:01:10 -0700367 (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700368 if (!cm->prev_mi_grid_base) return 1;
369
370 return 0;
371}
372
Yaowu Xuf883b422016-08-30 14:01:10 -0700373static void av1_enc_free_mi(AV1_COMMON *cm) {
374 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700375 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700376 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700378 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700379 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700380 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 cm->prev_mi_grid_base = NULL;
382}
383
Yaowu Xuf883b422016-08-30 14:01:10 -0700384static void av1_swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700385 // Current mip will be the prev_mip for the next frame.
386 MODE_INFO **temp_base = cm->prev_mi_grid_base;
387 MODE_INFO *temp = cm->prev_mip;
388 cm->prev_mip = cm->mip;
389 cm->mip = temp;
390
391 // Update the upper left visible macroblock ptrs.
392 cm->mi = cm->mip + cm->mi_stride + 1;
393 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
394
395 cm->prev_mi_grid_base = cm->mi_grid_base;
396 cm->mi_grid_base = temp_base;
397 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
398 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
399}
400
Yaowu Xuf883b422016-08-30 14:01:10 -0700401void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402 static volatile int init_done = 0;
403
404 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700405 av1_rtcd();
406 aom_dsp_rtcd();
407 aom_scale_rtcd();
408 av1_init_intra_predictors();
409 av1_init_me_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000410#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -0700411 av1_rc_init_minq_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000412#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700413 av1_entropy_mv_init();
414 av1_encode_token_init();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700415#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700416 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417#endif
418 init_done = 1;
419 }
420}
421
Yaowu Xuf883b422016-08-30 14:01:10 -0700422static void dealloc_compressor_data(AV1_COMP *cpi) {
423 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700424 int i;
425
Yaowu Xuf883b422016-08-30 14:01:10 -0700426 aom_free(cpi->mbmi_ext_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700427 cpi->mbmi_ext_base = NULL;
428
Yushin Cho77bba8d2016-11-04 16:36:56 -0700429#if CONFIG_PVQ
430 if (cpi->oxcf.pass != 1) {
Yushin Cho749c0572017-04-07 10:36:47 -0700431 const int tile_cols = cm->tile_cols;
432 const int tile_rows = cm->tile_rows;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700433 int tile_col, tile_row;
434
435 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
436 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
437 TileDataEnc *tile_data =
438 &cpi->tile_data[tile_row * tile_cols + tile_col];
439 aom_free(tile_data->pvq_q.buf);
440 }
441 }
442#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700443 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700444 cpi->tile_data = NULL;
445
446 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700447 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700448 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700449
Yaowu Xuf883b422016-08-30 14:01:10 -0700450 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700451 cpi->cyclic_refresh = NULL;
452
Yaowu Xuf883b422016-08-30 14:01:10 -0700453 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700454 cpi->active_map.map = NULL;
455
Jingning Hand064cf02017-06-01 10:00:39 -0700456#if CONFIG_MOTION_VAR
457 aom_free(cpi->td.mb.above_pred_buf);
458 cpi->td.mb.above_pred_buf = NULL;
459
460 aom_free(cpi->td.mb.left_pred_buf);
461 cpi->td.mb.left_pred_buf = NULL;
462
463 aom_free(cpi->td.mb.wsrc_buf);
464 cpi->td.mb.wsrc_buf = NULL;
465
466 aom_free(cpi->td.mb.mask_buf);
467 cpi->td.mb.mask_buf = NULL;
468#endif
469
Yaowu Xuc27fc142016-08-22 16:08:15 -0700470 // Free up-sampled reference buffers.
471 for (i = 0; i < (REF_FRAMES + 1); i++)
Yaowu Xuf883b422016-08-30 14:01:10 -0700472 aom_free_frame_buffer(&cpi->upsampled_ref_bufs[i].buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700473
Yaowu Xuf883b422016-08-30 14:01:10 -0700474 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700475#if CONFIG_LV_MAP
476 av1_free_txb_buf(cpi);
477#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700478 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700479
Yaowu Xuf883b422016-08-30 14:01:10 -0700480 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700481#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700482 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800483 aom_free_frame_buffer(&cpi->last_frame_db);
484 aom_free_frame_buffer(&cpi->trial_frame_rst);
485 aom_free(cpi->extra_rstbuf);
Debargha Mukherjeea43a2d92017-01-03 15:14:57 -0800486 for (i = 0; i < MAX_MB_PLANE; ++i)
487 av1_free_restoration_struct(&cpi->rst_search[i]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700488#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700489 aom_free_frame_buffer(&cpi->scaled_source);
490 aom_free_frame_buffer(&cpi->scaled_last_source);
491 aom_free_frame_buffer(&cpi->alt_ref_buffer);
492 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700493
Yaowu Xuf883b422016-08-30 14:01:10 -0700494 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700495 cpi->tile_tok[0][0] = 0;
496
Yaowu Xuf883b422016-08-30 14:01:10 -0700497 av1_free_pc_tree(&cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700498
Urvang Joshib100db72016-10-12 16:28:56 -0700499#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700500 if (cpi->common.allow_screen_content_tools)
Yaowu Xuf883b422016-08-30 14:01:10 -0700501 aom_free(cpi->td.mb.palette_buffer);
Urvang Joshib100db72016-10-12 16:28:56 -0700502#endif // CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700503
Yaowu Xuc27fc142016-08-22 16:08:15 -0700504#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -0700505 aom_buf_ans_free(&cpi->buf_ans);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506#endif // CONFIG_ANS
507}
508
Yaowu Xuf883b422016-08-30 14:01:10 -0700509static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700510 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700511 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700512 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700513
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200514 // Stores a snapshot of key state variables which can subsequently be
515 // restored with a call to av1_restore_coding_context. These functions are
516 // intended for use in a re-code loop in av1_compress_frame where the
517 // quantizer value is adjusted between loop iterations.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518 for (i = 0; i < NMV_CONTEXTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700519 av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
Urvang Joshibffc0b52016-07-25 13:38:49 -0700520 av1_copy(cc->nmv_costs, cpi->nmv_costs);
521 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700523
Yaowu Xuf883b422016-08-30 14:01:10 -0700524 av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
525 av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700526
527 cc->fc = *cm->fc;
528}
529
Yaowu Xuf883b422016-08-30 14:01:10 -0700530static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700532 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200535 // Restore key state variables to the snapshot state stored in the
536 // previous call to av1_save_coding_context.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537 for (i = 0; i < NMV_CONTEXTS; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700538 av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
Urvang Joshibffc0b52016-07-25 13:38:49 -0700539 av1_copy(cpi->nmv_costs, cc->nmv_costs);
540 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700541 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542
Yaowu Xuf883b422016-08-30 14:01:10 -0700543 av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
544 av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700545
546 *cm->fc = cc->fc;
547}
548
Yaowu Xuf883b422016-08-30 14:01:10 -0700549static void configure_static_seg_features(AV1_COMP *cpi) {
550 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700551 const RATE_CONTROL *const rc = &cpi->rc;
552 struct segmentation *const seg = &cm->seg;
553
554 int high_q = (int)(rc->avg_q > 48.0);
555 int qi_delta;
556
557 // Disable and clear down for KF
558 if (cm->frame_type == KEY_FRAME) {
559 // Clear down the global segmentation map
560 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
561 seg->update_map = 0;
562 seg->update_data = 0;
563 cpi->static_mb_pct = 0;
564
565 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700566 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700567
568 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700569 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700570 } else if (cpi->refresh_alt_ref_frame) {
571 // If this is an alt ref frame
572 // Clear down the global segmentation map
573 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
574 seg->update_map = 0;
575 seg->update_data = 0;
576 cpi->static_mb_pct = 0;
577
578 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700579 av1_disable_segmentation(seg);
580 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581
582 // Scan frames from current to arf frame.
583 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700584 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585
586 // If segmentation was enabled set those features needed for the
587 // arf itself.
588 if (seg->enabled) {
589 seg->update_map = 1;
590 seg->update_data = 1;
591
592 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700593 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
594 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
595 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700596
Yaowu Xuf883b422016-08-30 14:01:10 -0700597 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
598 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700599
600 // Where relevant assume segment data is delta data
601 seg->abs_delta = SEGMENT_DELTADATA;
602 }
603 } else if (seg->enabled) {
604 // All other frames if segmentation has been enabled
605
606 // First normal frame in a valid gf or alt ref group
607 if (rc->frames_since_golden == 0) {
608 // Set up segment features for normal frames in an arf group
609 if (rc->source_alt_ref_active) {
610 seg->update_map = 0;
611 seg->update_data = 1;
612 seg->abs_delta = SEGMENT_DELTADATA;
613
Yaowu Xuf883b422016-08-30 14:01:10 -0700614 qi_delta =
615 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
616 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
617 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700618
Yaowu Xuf883b422016-08-30 14:01:10 -0700619 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
620 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621
622 // Segment coding disabled for compred testing
623 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700624 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
625 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
626 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700627 }
628 } else {
629 // Disable segmentation and clear down features if alt ref
630 // is not active for this group
631
Yaowu Xuf883b422016-08-30 14:01:10 -0700632 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700633
634 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
635
636 seg->update_map = 0;
637 seg->update_data = 0;
638
Yaowu Xuf883b422016-08-30 14:01:10 -0700639 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700640 }
641 } else if (rc->is_src_frame_alt_ref) {
642 // Special case where we are coding over the top of a previous
643 // alt ref frame.
644 // Segment coding disabled for compred testing
645
646 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700647 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
648 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700649
650 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700651 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
652 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
653 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
654 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655
656 // Skip all MBs if high Q (0,0 mv and skip coeffs)
657 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700658 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
659 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660 }
661 // Enable data update
662 seg->update_data = 1;
663 } else {
664 // All other frames.
665
666 // No updates.. leave things as they are.
667 seg->update_map = 0;
668 seg->update_data = 0;
669 }
670 }
671}
672
Yaowu Xuf883b422016-08-30 14:01:10 -0700673static void update_reference_segmentation_map(AV1_COMP *cpi) {
674 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700675 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
676 uint8_t *cache_ptr = cm->last_frame_seg_map;
677 int row, col;
678
679 for (row = 0; row < cm->mi_rows; row++) {
680 MODE_INFO **mi_8x8 = mi_8x8_ptr;
681 uint8_t *cache = cache_ptr;
682 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
683 cache[0] = mi_8x8[0]->mbmi.segment_id;
684 mi_8x8_ptr += cm->mi_stride;
685 cache_ptr += cm->mi_cols;
686 }
687}
688
Yaowu Xuf883b422016-08-30 14:01:10 -0700689static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
690 AV1_COMMON *cm = &cpi->common;
691 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700692
693 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700694 cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
695 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200696#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700697 cm->use_highbitdepth,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700699 oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700701 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 "Failed to allocate lag buffers");
703
704 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700705 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200707#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700708 cm->use_highbitdepth,
709#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700710 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
711 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700712 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700713 "Failed to allocate altref buffer");
714}
715
Yaowu Xuf883b422016-08-30 14:01:10 -0700716static void alloc_util_frame_buffers(AV1_COMP *cpi) {
717 AV1_COMMON *const cm = &cpi->common;
718 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200720#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700721 cm->use_highbitdepth,
722#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700723 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
724 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700725 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726 "Failed to allocate last frame buffer");
727
728#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 if (aom_realloc_frame_buffer(&cpi->last_frame_db, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200731#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700732 cm->use_highbitdepth,
733#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700734 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
735 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700736 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737 "Failed to allocate last frame deblocked buffer");
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700738 if (aom_realloc_frame_buffer(
739 &cpi->trial_frame_rst,
740#if CONFIG_FRAME_SUPERRES
741 cm->superres_upscaled_width, cm->superres_upscaled_height,
742#else
743 cm->width, cm->height,
744#endif // CONFIG_FRAME_SUPERRES
745 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200746#if CONFIG_HIGHBITDEPTH
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700747 cm->use_highbitdepth,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800748#endif
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700749 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800750 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800751 "Failed to allocate trial restored frame buffer");
Alex Converse232e3842017-02-24 12:24:36 -0800752 int extra_rstbuf_sz = RESTORATION_EXTBUF_SIZE;
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -0800753 if (extra_rstbuf_sz > 0) {
Alex Converse7f094f12017-02-23 17:29:40 -0800754 aom_free(cpi->extra_rstbuf);
Alex Converse232e3842017-02-24 12:24:36 -0800755 CHECK_MEM_ERROR(cm, cpi->extra_rstbuf,
Alex Converse7f094f12017-02-23 17:29:40 -0800756 (uint8_t *)aom_malloc(extra_rstbuf_sz));
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -0800757 } else {
758 cpi->extra_rstbuf = NULL;
759 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760#endif // CONFIG_LOOP_RESTORATION
761
Yaowu Xuf883b422016-08-30 14:01:10 -0700762 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200764#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 cm->use_highbitdepth,
766#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700767 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
768 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700769 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770 "Failed to allocate scaled source buffer");
771
Yaowu Xuf883b422016-08-30 14:01:10 -0700772 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700773 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200774#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700775 cm->use_highbitdepth,
776#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700777 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
778 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700779 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 "Failed to allocate scaled last source buffer");
781}
782
Yunqing Wangf5dc71e2017-05-04 18:20:17 -0700783static void alloc_context_buffers_ext(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700784 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700785 int mi_size = cm->mi_cols * cm->mi_rows;
786
Yunqing Wangf5dc71e2017-05-04 18:20:17 -0700787 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
788 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700789}
790
Yaowu Xuf883b422016-08-30 14:01:10 -0700791void av1_alloc_compressor_data(AV1_COMP *cpi) {
792 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793
Yaowu Xuf883b422016-08-30 14:01:10 -0700794 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700796#if CONFIG_LV_MAP
797 av1_alloc_txb_buf(cpi);
798#endif
799
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 alloc_context_buffers_ext(cpi);
801
Yaowu Xuf883b422016-08-30 14:01:10 -0700802 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803
804 {
805 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
806 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700807 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Alex Converseeb780e72016-12-13 12:46:41 -0800808#if CONFIG_ANS && !ANS_MAX_SYMBOLS
809 aom_buf_ans_alloc(&cpi->buf_ans, &cm->error, (int)tokens);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810#endif // CONFIG_ANS
811 }
812
Yaowu Xuf883b422016-08-30 14:01:10 -0700813 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700814}
815
Yaowu Xuf883b422016-08-30 14:01:10 -0700816void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000818#if CONFIG_XIPHRC
819 if (!cpi->od_rc.cur_frame) return;
820 cpi->od_rc.framerate = cpi->framerate;
821 od_enc_rc_resize(&cpi->od_rc);
822#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700823 av1_rc_update_framerate(cpi);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000824#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700825}
826
Yaowu Xuf883b422016-08-30 14:01:10 -0700827static void set_tile_info(AV1_COMP *cpi) {
828 AV1_COMMON *const cm = &cpi->common;
Fangwen Fu73126c02017-02-08 22:37:47 -0800829#if CONFIG_TILE_GROUPS && CONFIG_DEPENDENT_HORZTILES
830 int tile_row, tile_col, num_tiles_in_tg;
831 int tg_row_start, tg_col_start;
832#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833#if CONFIG_EXT_TILE
834#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700835 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700836 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
837 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
838 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
839 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
840 } else {
841 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
842 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
843 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
844 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
845 }
846#else
847 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
848 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
849 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
850 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
851#endif // CONFIG_EXT_PARTITION
852
Yaowu Xuf883b422016-08-30 14:01:10 -0700853 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
854 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855
856 assert(cm->tile_width >> MAX_MIB_SIZE <= 32);
857 assert(cm->tile_height >> MAX_MIB_SIZE <= 32);
858
859 // Get the number of tiles
860 cm->tile_cols = 1;
861 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
862
863 cm->tile_rows = 1;
864 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
865#else
866 int min_log2_tile_cols, max_log2_tile_cols;
Yaowu Xuf883b422016-08-30 14:01:10 -0700867 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868
869 cm->log2_tile_cols =
870 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
871 cm->log2_tile_rows = cpi->oxcf.tile_rows;
872
873 cm->tile_cols = 1 << cm->log2_tile_cols;
874 cm->tile_rows = 1 << cm->log2_tile_rows;
875
876 cm->tile_width = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
877 cm->tile_width >>= cm->log2_tile_cols;
878 cm->tile_height = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
879 cm->tile_height >>= cm->log2_tile_rows;
880
881 // round to integer multiples of max superblock size
882 cm->tile_width = ALIGN_POWER_OF_TWO(cm->tile_width, MAX_MIB_SIZE_LOG2);
883 cm->tile_height = ALIGN_POWER_OF_TWO(cm->tile_height, MAX_MIB_SIZE_LOG2);
884#endif // CONFIG_EXT_TILE
Ryan Lei7386eda2016-12-08 21:08:31 -0800885
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800886#if CONFIG_DEPENDENT_HORZTILES
887 cm->dependent_horz_tiles = cpi->oxcf.dependent_horz_tiles;
Fangwen Fu70bcb892017-05-06 17:05:19 -0700888#if CONFIG_EXT_TILE
889 if (cm->tile_rows <= 1) cm->dependent_horz_tiles = 0;
890#else
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800891 if (cm->log2_tile_rows == 0) cm->dependent_horz_tiles = 0;
Fangwen Fu70bcb892017-05-06 17:05:19 -0700892#endif
Fangwen Fu73126c02017-02-08 22:37:47 -0800893#if CONFIG_TILE_GROUPS
894 if (cpi->oxcf.mtu == 0) {
895 cm->num_tg = cpi->oxcf.num_tile_groups;
896 } else {
897 // Use a default value for the purposes of weighting costs in probability
898 // updates
899 cm->num_tg = DEFAULT_MAX_NUM_TG;
900 }
901 num_tiles_in_tg =
902 (cm->tile_cols * cm->tile_rows + cm->num_tg - 1) / cm->num_tg;
903 tg_row_start = 0;
904 tg_col_start = 0;
905 for (tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
906 for (tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
907 if ((tile_row * cm->tile_cols + tile_col) % num_tiles_in_tg == 0) {
908 tg_row_start = tile_row;
909 tg_col_start = tile_col;
910 }
911 cm->tile_group_start_row[tile_row][tile_col] = tg_row_start;
912 cm->tile_group_start_col[tile_row][tile_col] = tg_col_start;
913 }
914 }
915#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800916#endif
917
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800918#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800919 cm->loop_filter_across_tiles_enabled =
920 cpi->oxcf.loop_filter_across_tiles_enabled;
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800921#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922}
923
Yaowu Xuf883b422016-08-30 14:01:10 -0700924static void update_frame_size(AV1_COMP *cpi) {
925 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700926 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
927
Yaowu Xuf883b422016-08-30 14:01:10 -0700928 av1_set_mb_mi(cm, cm->width, cm->height);
929 av1_init_context_buffers(cm);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700930 av1_init_macroblockd(cm, xd,
931#if CONFIG_PVQ
932 NULL,
933#endif
Luc Trudeauf8164152017-04-11 16:20:51 -0400934#if CONFIG_CFL
935 &NULL_CFL,
936#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700937 NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700938 memset(cpi->mbmi_ext_base, 0,
939 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
940
941 set_tile_info(cpi);
942}
943
Yaowu Xuf883b422016-08-30 14:01:10 -0700944static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945#if CONFIG_EXT_REFS
946 int fb_idx;
947 for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
948 cpi->lst_fb_idxes[fb_idx] = fb_idx;
949 cpi->gld_fb_idx = LAST_REF_FRAMES;
950 cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
951 cpi->alt_fb_idx = LAST_REF_FRAMES + 2;
952 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
953 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
954#else
955 cpi->lst_fb_idx = 0;
956 cpi->gld_fb_idx = 1;
957 cpi->alt_fb_idx = 2;
958#endif // CONFIG_EXT_REFS
959}
960
Yaowu Xuf883b422016-08-30 14:01:10 -0700961static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
962 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700963
964 cpi->oxcf = *oxcf;
965 cpi->framerate = oxcf->init_framerate;
966
967 cm->profile = oxcf->profile;
968 cm->bit_depth = oxcf->bit_depth;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200969#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700970 cm->use_highbitdepth = oxcf->use_highbitdepth;
971#endif
972 cm->color_space = oxcf->color_space;
973 cm->color_range = oxcf->color_range;
974
975 cm->width = oxcf->width;
976 cm->height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -0700977 av1_alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700978
979 // Single thread case: use counts in common.
980 cpi->td.counts = &cm->counts;
981
982 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -0700983 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700984
985 cpi->static_mb_pct = 0;
986 cpi->ref_frame_flags = 0;
987
988 init_buffer_indices(cpi);
989}
990
991static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -0700992 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700993 const int64_t bandwidth = oxcf->target_bandwidth;
994 const int64_t starting = oxcf->starting_buffer_level_ms;
995 const int64_t optimal = oxcf->optimal_buffer_level_ms;
996 const int64_t maximum = oxcf->maximum_buffer_size_ms;
997
998 rc->starting_buffer_level = starting * bandwidth / 1000;
999 rc->optimal_buffer_level =
1000 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1001 rc->maximum_buffer_size =
1002 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1003}
1004
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001005#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001006#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
1007 cpi->fn_ptr[BT].sdf = SDF; \
1008 cpi->fn_ptr[BT].sdaf = SDAF; \
1009 cpi->fn_ptr[BT].vf = VF; \
1010 cpi->fn_ptr[BT].svf = SVF; \
1011 cpi->fn_ptr[BT].svaf = SVAF; \
1012 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1013 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1014 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1015
1016#define MAKE_BFP_SAD_WRAPPER(fnname) \
1017 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1018 int source_stride, \
1019 const uint8_t *ref_ptr, int ref_stride) { \
1020 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1021 } \
1022 static unsigned int fnname##_bits10( \
1023 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1024 int ref_stride) { \
1025 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1026 } \
1027 static unsigned int fnname##_bits12( \
1028 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1029 int ref_stride) { \
1030 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1031 }
1032
1033#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1034 static unsigned int fnname##_bits8( \
1035 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1036 int ref_stride, const uint8_t *second_pred) { \
1037 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1038 } \
1039 static unsigned int fnname##_bits10( \
1040 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1041 int ref_stride, const uint8_t *second_pred) { \
1042 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1043 2; \
1044 } \
1045 static unsigned int fnname##_bits12( \
1046 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1047 int ref_stride, const uint8_t *second_pred) { \
1048 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1049 4; \
1050 }
1051
1052#define MAKE_BFP_SAD3_WRAPPER(fnname) \
1053 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1054 const uint8_t *ref_ptr, int ref_stride, \
1055 unsigned int *sad_array) { \
1056 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1057 } \
1058 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1059 const uint8_t *ref_ptr, int ref_stride, \
1060 unsigned int *sad_array) { \
1061 int i; \
1062 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1063 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1064 } \
1065 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1066 const uint8_t *ref_ptr, int ref_stride, \
1067 unsigned int *sad_array) { \
1068 int i; \
1069 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1070 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1071 }
1072
1073#define MAKE_BFP_SAD8_WRAPPER(fnname) \
1074 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1075 const uint8_t *ref_ptr, int ref_stride, \
1076 unsigned int *sad_array) { \
1077 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1078 } \
1079 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1080 const uint8_t *ref_ptr, int ref_stride, \
1081 unsigned int *sad_array) { \
1082 int i; \
1083 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1084 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1085 } \
1086 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1087 const uint8_t *ref_ptr, int ref_stride, \
1088 unsigned int *sad_array) { \
1089 int i; \
1090 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1091 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1092 }
1093#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1094 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1095 const uint8_t *const ref_ptr[], int ref_stride, \
1096 unsigned int *sad_array) { \
1097 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1098 } \
1099 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1100 const uint8_t *const ref_ptr[], int ref_stride, \
1101 unsigned int *sad_array) { \
1102 int i; \
1103 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1104 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1105 } \
1106 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1107 const uint8_t *const ref_ptr[], int ref_stride, \
1108 unsigned int *sad_array) { \
1109 int i; \
1110 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1111 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1112 }
1113
1114#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001115MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1116MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1117MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
1118MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
1119MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1120MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1121MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1122MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1123MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1124MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1125MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001126#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001127MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1128MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1129MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1130MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1131MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1132MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1133MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1134MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1135MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1136MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1137MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1138MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1139MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1140MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1141MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
1142MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
1143MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1144MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1145MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1146MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
1147MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
1148MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1149MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1150MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1151MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
1152MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
1153MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1154MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1155MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1156MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
1157MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
1158MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1159MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1160MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1161MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
1162MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
1163MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1164MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1165MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1166MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
1167MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
1168MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1169MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1170MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1171MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
1172MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1173MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1174MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1175MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
1176MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1177MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1178MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1179MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
1180MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
1181MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001182
1183#if CONFIG_EXT_INTER
David Barker0f3c94e2017-05-16 15:21:50 +01001184#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001185 cpi->fn_ptr[BT].msdf = MCSDF; \
1186 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001187
David Barkerc155e012017-05-11 13:54:54 +01001188#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1189 static unsigned int fnname##_bits8( \
1190 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1191 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1192 int m_stride, int invert_mask) { \
1193 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1194 second_pred_ptr, m, m_stride, invert_mask); \
1195 } \
1196 static unsigned int fnname##_bits10( \
1197 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1198 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1199 int m_stride, int invert_mask) { \
1200 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1201 second_pred_ptr, m, m_stride, invert_mask) >> \
1202 2; \
1203 } \
1204 static unsigned int fnname##_bits12( \
1205 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1206 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1207 int m_stride, int invert_mask) { \
1208 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1209 second_pred_ptr, m, m_stride, invert_mask) >> \
1210 4; \
1211 }
1212
Yaowu Xuc27fc142016-08-22 16:08:15 -07001213#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001214MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1215MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1216MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001217#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001218MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1219MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1220MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1221MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1222MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1223MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1224MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1225MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1226MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1227MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1228MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1229MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1230MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001231#endif // CONFIG_EXT_INTER
1232
Yue Chencb60b182016-10-13 15:18:22 -07001233#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001234#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1235 cpi->fn_ptr[BT].osdf = OSDF; \
1236 cpi->fn_ptr[BT].ovf = OVF; \
1237 cpi->fn_ptr[BT].osvf = OSVF;
1238
1239#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1240 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1241 const int32_t *wsrc, \
1242 const int32_t *msk) { \
1243 return fnname(ref, ref_stride, wsrc, msk); \
1244 } \
1245 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1246 const int32_t *wsrc, \
1247 const int32_t *msk) { \
1248 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1249 } \
1250 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1251 const int32_t *wsrc, \
1252 const int32_t *msk) { \
1253 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1254 }
1255
1256#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001257MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1258MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1259MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001260#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001261MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1262MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1263MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1264MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1265MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1266MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1267MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1268MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1269MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1270MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1271MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1272MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1273MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Yue Chencb60b182016-10-13 15:18:22 -07001274#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001275
Yaowu Xuf883b422016-08-30 14:01:10 -07001276static void highbd_set_var_fns(AV1_COMP *const cpi) {
1277 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001278 if (cm->use_highbitdepth) {
1279 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001280 case AOM_BITS_8:
1281 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1282 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1283 aom_highbd_8_sub_pixel_variance32x16,
1284 aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1285 aom_highbd_sad32x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001286
Yaowu Xuf883b422016-08-30 14:01:10 -07001287 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1288 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1289 aom_highbd_8_sub_pixel_variance16x32,
1290 aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1291 aom_highbd_sad16x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001292
Yaowu Xuf883b422016-08-30 14:01:10 -07001293 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1294 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1295 aom_highbd_8_sub_pixel_variance64x32,
1296 aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1297 aom_highbd_sad64x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001298
Yaowu Xuf883b422016-08-30 14:01:10 -07001299 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1300 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1301 aom_highbd_8_sub_pixel_variance32x64,
1302 aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1303 aom_highbd_sad32x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001304
Yaowu Xuf883b422016-08-30 14:01:10 -07001305 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1306 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1307 aom_highbd_8_sub_pixel_variance32x32,
1308 aom_highbd_8_sub_pixel_avg_variance32x32,
1309 aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
1310 aom_highbd_sad32x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001311
Yaowu Xuf883b422016-08-30 14:01:10 -07001312 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1313 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1314 aom_highbd_8_sub_pixel_variance64x64,
1315 aom_highbd_8_sub_pixel_avg_variance64x64,
1316 aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
1317 aom_highbd_sad64x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001318
Yaowu Xuf883b422016-08-30 14:01:10 -07001319 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1320 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1321 aom_highbd_8_sub_pixel_variance16x16,
1322 aom_highbd_8_sub_pixel_avg_variance16x16,
1323 aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
1324 aom_highbd_sad16x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001325
1326 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001327 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1328 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1329 aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
1330 aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001331
1332 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001333 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1334 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1335 aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
1336 aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001337
1338 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001339 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1340 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1341 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits8,
1342 aom_highbd_sad8x8x8_bits8, aom_highbd_sad8x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001343
Yaowu Xuf883b422016-08-30 14:01:10 -07001344 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1345 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1346 aom_highbd_8_sub_pixel_variance8x4,
1347 aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
1348 aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001349
Yaowu Xuf883b422016-08-30 14:01:10 -07001350 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1351 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1352 aom_highbd_8_sub_pixel_variance4x8,
1353 aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
1354 aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001355
1356 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001357 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1358 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1359 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits8,
1360 aom_highbd_sad4x4x8_bits8, aom_highbd_sad4x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001361
Jingning Hancc5bdf42016-12-19 11:14:30 -08001362#if CONFIG_CB4X4
1363 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_8_variance2x2, NULL, NULL,
1364 NULL, NULL, NULL)
1365 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_8_variance4x2, NULL, NULL,
1366 NULL, NULL, NULL)
1367 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_8_variance2x4, NULL, NULL,
1368 NULL, NULL, NULL)
1369#endif
1370
Yaowu Xuc27fc142016-08-22 16:08:15 -07001371#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001372 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1373 aom_highbd_sad128x128_avg_bits8,
1374 aom_highbd_8_variance128x128,
1375 aom_highbd_8_sub_pixel_variance128x128,
1376 aom_highbd_8_sub_pixel_avg_variance128x128,
1377 aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
1378 aom_highbd_sad128x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001379
Yaowu Xuf883b422016-08-30 14:01:10 -07001380 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1381 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1382 aom_highbd_8_sub_pixel_variance128x64,
1383 aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
1384 aom_highbd_sad128x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001385
Yaowu Xuf883b422016-08-30 14:01:10 -07001386 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1387 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1388 aom_highbd_8_sub_pixel_variance64x128,
1389 aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
1390 aom_highbd_sad64x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001391#endif // CONFIG_EXT_PARTITION
1392
1393#if CONFIG_EXT_INTER
1394#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001395 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1396 aom_highbd_8_masked_sub_pixel_variance128x128)
1397 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1398 aom_highbd_8_masked_sub_pixel_variance128x64)
1399 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1400 aom_highbd_8_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001401#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001402 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1403 aom_highbd_8_masked_sub_pixel_variance64x64)
1404 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1405 aom_highbd_8_masked_sub_pixel_variance64x32)
1406 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1407 aom_highbd_8_masked_sub_pixel_variance32x64)
1408 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1409 aom_highbd_8_masked_sub_pixel_variance32x32)
1410 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1411 aom_highbd_8_masked_sub_pixel_variance32x16)
1412 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1413 aom_highbd_8_masked_sub_pixel_variance16x32)
1414 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1415 aom_highbd_8_masked_sub_pixel_variance16x16)
1416 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1417 aom_highbd_8_masked_sub_pixel_variance8x16)
1418 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1419 aom_highbd_8_masked_sub_pixel_variance16x8)
1420 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1421 aom_highbd_8_masked_sub_pixel_variance8x8)
1422 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1423 aom_highbd_8_masked_sub_pixel_variance4x8)
1424 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1425 aom_highbd_8_masked_sub_pixel_variance8x4)
1426 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1427 aom_highbd_8_masked_sub_pixel_variance4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001428#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07001429#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001430#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001431 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1432 aom_highbd_obmc_variance128x128,
1433 aom_highbd_obmc_sub_pixel_variance128x128)
1434 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1435 aom_highbd_obmc_variance128x64,
1436 aom_highbd_obmc_sub_pixel_variance128x64)
1437 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1438 aom_highbd_obmc_variance64x128,
1439 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001440#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001441 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1442 aom_highbd_obmc_variance64x64,
1443 aom_highbd_obmc_sub_pixel_variance64x64)
1444 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1445 aom_highbd_obmc_variance64x32,
1446 aom_highbd_obmc_sub_pixel_variance64x32)
1447 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1448 aom_highbd_obmc_variance32x64,
1449 aom_highbd_obmc_sub_pixel_variance32x64)
1450 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1451 aom_highbd_obmc_variance32x32,
1452 aom_highbd_obmc_sub_pixel_variance32x32)
1453 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1454 aom_highbd_obmc_variance32x16,
1455 aom_highbd_obmc_sub_pixel_variance32x16)
1456 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1457 aom_highbd_obmc_variance16x32,
1458 aom_highbd_obmc_sub_pixel_variance16x32)
1459 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1460 aom_highbd_obmc_variance16x16,
1461 aom_highbd_obmc_sub_pixel_variance16x16)
1462 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1463 aom_highbd_obmc_variance8x16,
1464 aom_highbd_obmc_sub_pixel_variance8x16)
1465 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1466 aom_highbd_obmc_variance16x8,
1467 aom_highbd_obmc_sub_pixel_variance16x8)
1468 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1469 aom_highbd_obmc_variance8x8,
1470 aom_highbd_obmc_sub_pixel_variance8x8)
1471 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1472 aom_highbd_obmc_variance4x8,
1473 aom_highbd_obmc_sub_pixel_variance4x8)
1474 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1475 aom_highbd_obmc_variance8x4,
1476 aom_highbd_obmc_sub_pixel_variance8x4)
1477 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1478 aom_highbd_obmc_variance4x4,
1479 aom_highbd_obmc_sub_pixel_variance4x4)
Yue Chencb60b182016-10-13 15:18:22 -07001480#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001481 break;
1482
Yaowu Xuf883b422016-08-30 14:01:10 -07001483 case AOM_BITS_10:
1484 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1485 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1486 aom_highbd_10_sub_pixel_variance32x16,
1487 aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1488 aom_highbd_sad32x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001489
Yaowu Xuf883b422016-08-30 14:01:10 -07001490 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1491 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1492 aom_highbd_10_sub_pixel_variance16x32,
1493 aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1494 aom_highbd_sad16x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001495
Yaowu Xuf883b422016-08-30 14:01:10 -07001496 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1497 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1498 aom_highbd_10_sub_pixel_variance64x32,
1499 aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1500 aom_highbd_sad64x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001501
Yaowu Xuf883b422016-08-30 14:01:10 -07001502 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1503 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1504 aom_highbd_10_sub_pixel_variance32x64,
1505 aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1506 aom_highbd_sad32x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001507
Yaowu Xuf883b422016-08-30 14:01:10 -07001508 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1509 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1510 aom_highbd_10_sub_pixel_variance32x32,
1511 aom_highbd_10_sub_pixel_avg_variance32x32,
1512 aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
1513 aom_highbd_sad32x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001514
Yaowu Xuf883b422016-08-30 14:01:10 -07001515 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1516 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1517 aom_highbd_10_sub_pixel_variance64x64,
1518 aom_highbd_10_sub_pixel_avg_variance64x64,
1519 aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
1520 aom_highbd_sad64x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001521
Yaowu Xuf883b422016-08-30 14:01:10 -07001522 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1523 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1524 aom_highbd_10_sub_pixel_variance16x16,
1525 aom_highbd_10_sub_pixel_avg_variance16x16,
1526 aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
1527 aom_highbd_sad16x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001528
Yaowu Xuf883b422016-08-30 14:01:10 -07001529 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1530 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1531 aom_highbd_10_sub_pixel_variance16x8,
1532 aom_highbd_10_sub_pixel_avg_variance16x8,
1533 aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
1534 aom_highbd_sad16x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001535
Yaowu Xuf883b422016-08-30 14:01:10 -07001536 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1537 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1538 aom_highbd_10_sub_pixel_variance8x16,
1539 aom_highbd_10_sub_pixel_avg_variance8x16,
1540 aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
1541 aom_highbd_sad8x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001542
1543 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001544 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1545 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1546 aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
1547 aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001548
Yaowu Xuf883b422016-08-30 14:01:10 -07001549 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
1550 aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
1551 aom_highbd_10_sub_pixel_variance8x4,
1552 aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
1553 aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001554
Yaowu Xuf883b422016-08-30 14:01:10 -07001555 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
1556 aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
1557 aom_highbd_10_sub_pixel_variance4x8,
1558 aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
1559 aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001560
1561 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001562 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1563 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1564 aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
1565 aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001566
Jingning Hancc5bdf42016-12-19 11:14:30 -08001567#if CONFIG_CB4X4
1568 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_10_variance2x2, NULL, NULL,
1569 NULL, NULL, NULL)
1570 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_10_variance4x2, NULL, NULL,
1571 NULL, NULL, NULL)
1572 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_10_variance2x4, NULL, NULL,
1573 NULL, NULL, NULL)
1574#endif
1575
Yaowu Xuc27fc142016-08-22 16:08:15 -07001576#if CONFIG_EXT_PARTITION
1577 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001578 BLOCK_128X128, aom_highbd_sad128x128_bits10,
1579 aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
1580 aom_highbd_10_sub_pixel_variance128x128,
1581 aom_highbd_10_sub_pixel_avg_variance128x128,
1582 aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
1583 aom_highbd_sad128x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001584
Yaowu Xuf883b422016-08-30 14:01:10 -07001585 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1586 aom_highbd_sad128x64_avg_bits10,
1587 aom_highbd_10_variance128x64,
1588 aom_highbd_10_sub_pixel_variance128x64,
1589 aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
1590 aom_highbd_sad128x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001591
Yaowu Xuf883b422016-08-30 14:01:10 -07001592 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1593 aom_highbd_sad64x128_avg_bits10,
1594 aom_highbd_10_variance64x128,
1595 aom_highbd_10_sub_pixel_variance64x128,
1596 aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
1597 aom_highbd_sad64x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001598#endif // CONFIG_EXT_PARTITION
1599
1600#if CONFIG_EXT_INTER
1601#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001602 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1603 aom_highbd_10_masked_sub_pixel_variance128x128)
1604 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1605 aom_highbd_10_masked_sub_pixel_variance128x64)
1606 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1607 aom_highbd_10_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001608#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001609 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1610 aom_highbd_10_masked_sub_pixel_variance64x64)
1611 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1612 aom_highbd_10_masked_sub_pixel_variance64x32)
1613 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1614 aom_highbd_10_masked_sub_pixel_variance32x64)
1615 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1616 aom_highbd_10_masked_sub_pixel_variance32x32)
1617 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1618 aom_highbd_10_masked_sub_pixel_variance32x16)
1619 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1620 aom_highbd_10_masked_sub_pixel_variance16x32)
1621 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1622 aom_highbd_10_masked_sub_pixel_variance16x16)
1623 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1624 aom_highbd_10_masked_sub_pixel_variance8x16)
1625 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1626 aom_highbd_10_masked_sub_pixel_variance16x8)
1627 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1628 aom_highbd_10_masked_sub_pixel_variance8x8)
1629 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1630 aom_highbd_10_masked_sub_pixel_variance4x8)
1631 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1632 aom_highbd_10_masked_sub_pixel_variance8x4)
1633 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1634 aom_highbd_10_masked_sub_pixel_variance4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001635#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07001636#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001637#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001638 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1639 aom_highbd_10_obmc_variance128x128,
1640 aom_highbd_10_obmc_sub_pixel_variance128x128)
1641 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1642 aom_highbd_10_obmc_variance128x64,
1643 aom_highbd_10_obmc_sub_pixel_variance128x64)
1644 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1645 aom_highbd_10_obmc_variance64x128,
1646 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001647#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001648 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1649 aom_highbd_10_obmc_variance64x64,
1650 aom_highbd_10_obmc_sub_pixel_variance64x64)
1651 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1652 aom_highbd_10_obmc_variance64x32,
1653 aom_highbd_10_obmc_sub_pixel_variance64x32)
1654 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
1655 aom_highbd_10_obmc_variance32x64,
1656 aom_highbd_10_obmc_sub_pixel_variance32x64)
1657 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
1658 aom_highbd_10_obmc_variance32x32,
1659 aom_highbd_10_obmc_sub_pixel_variance32x32)
1660 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
1661 aom_highbd_10_obmc_variance32x16,
1662 aom_highbd_10_obmc_sub_pixel_variance32x16)
1663 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
1664 aom_highbd_10_obmc_variance16x32,
1665 aom_highbd_10_obmc_sub_pixel_variance16x32)
1666 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
1667 aom_highbd_10_obmc_variance16x16,
1668 aom_highbd_10_obmc_sub_pixel_variance16x16)
1669 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
1670 aom_highbd_10_obmc_variance8x16,
1671 aom_highbd_10_obmc_sub_pixel_variance8x16)
1672 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
1673 aom_highbd_10_obmc_variance16x8,
1674 aom_highbd_10_obmc_sub_pixel_variance16x8)
1675 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
1676 aom_highbd_10_obmc_variance8x8,
1677 aom_highbd_10_obmc_sub_pixel_variance8x8)
1678 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
1679 aom_highbd_10_obmc_variance4x8,
1680 aom_highbd_10_obmc_sub_pixel_variance4x8)
1681 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
1682 aom_highbd_10_obmc_variance8x4,
1683 aom_highbd_10_obmc_sub_pixel_variance8x4)
1684 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
1685 aom_highbd_10_obmc_variance4x4,
1686 aom_highbd_10_obmc_sub_pixel_variance4x4)
Yue Chencb60b182016-10-13 15:18:22 -07001687#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001688 break;
1689
Yaowu Xuf883b422016-08-30 14:01:10 -07001690 case AOM_BITS_12:
1691 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
1692 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
1693 aom_highbd_12_sub_pixel_variance32x16,
1694 aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
1695 aom_highbd_sad32x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001696
Yaowu Xuf883b422016-08-30 14:01:10 -07001697 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
1698 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
1699 aom_highbd_12_sub_pixel_variance16x32,
1700 aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
1701 aom_highbd_sad16x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001702
Yaowu Xuf883b422016-08-30 14:01:10 -07001703 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
1704 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
1705 aom_highbd_12_sub_pixel_variance64x32,
1706 aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
1707 aom_highbd_sad64x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001708
Yaowu Xuf883b422016-08-30 14:01:10 -07001709 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
1710 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
1711 aom_highbd_12_sub_pixel_variance32x64,
1712 aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
1713 aom_highbd_sad32x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001714
Yaowu Xuf883b422016-08-30 14:01:10 -07001715 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
1716 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
1717 aom_highbd_12_sub_pixel_variance32x32,
1718 aom_highbd_12_sub_pixel_avg_variance32x32,
1719 aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
1720 aom_highbd_sad32x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001721
Yaowu Xuf883b422016-08-30 14:01:10 -07001722 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
1723 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
1724 aom_highbd_12_sub_pixel_variance64x64,
1725 aom_highbd_12_sub_pixel_avg_variance64x64,
1726 aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
1727 aom_highbd_sad64x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001728
Yaowu Xuf883b422016-08-30 14:01:10 -07001729 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
1730 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
1731 aom_highbd_12_sub_pixel_variance16x16,
1732 aom_highbd_12_sub_pixel_avg_variance16x16,
1733 aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
1734 aom_highbd_sad16x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001735
Yaowu Xuf883b422016-08-30 14:01:10 -07001736 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
1737 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
1738 aom_highbd_12_sub_pixel_variance16x8,
1739 aom_highbd_12_sub_pixel_avg_variance16x8,
1740 aom_highbd_sad16x8x3_bits12, aom_highbd_sad16x8x8_bits12,
1741 aom_highbd_sad16x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001742
Yaowu Xuf883b422016-08-30 14:01:10 -07001743 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
1744 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
1745 aom_highbd_12_sub_pixel_variance8x16,
1746 aom_highbd_12_sub_pixel_avg_variance8x16,
1747 aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
1748 aom_highbd_sad8x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001749
1750 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001751 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
1752 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
1753 aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
1754 aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001755
Yaowu Xuf883b422016-08-30 14:01:10 -07001756 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
1757 aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
1758 aom_highbd_12_sub_pixel_variance8x4,
1759 aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
1760 aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001761
Yaowu Xuf883b422016-08-30 14:01:10 -07001762 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
1763 aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
1764 aom_highbd_12_sub_pixel_variance4x8,
1765 aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
1766 aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001767
1768 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001769 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
1770 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
1771 aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
1772 aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001773
Jingning Hancc5bdf42016-12-19 11:14:30 -08001774#if CONFIG_CB4X4
1775 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_12_variance2x2, NULL, NULL,
1776 NULL, NULL, NULL)
1777 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_12_variance4x2, NULL, NULL,
1778 NULL, NULL, NULL)
1779 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_12_variance2x4, NULL, NULL,
1780 NULL, NULL, NULL)
1781#endif
1782
Yaowu Xuc27fc142016-08-22 16:08:15 -07001783#if CONFIG_EXT_PARTITION
1784 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001785 BLOCK_128X128, aom_highbd_sad128x128_bits12,
1786 aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
1787 aom_highbd_12_sub_pixel_variance128x128,
1788 aom_highbd_12_sub_pixel_avg_variance128x128,
1789 aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
1790 aom_highbd_sad128x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001791
Yaowu Xuf883b422016-08-30 14:01:10 -07001792 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
1793 aom_highbd_sad128x64_avg_bits12,
1794 aom_highbd_12_variance128x64,
1795 aom_highbd_12_sub_pixel_variance128x64,
1796 aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
1797 aom_highbd_sad128x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001798
Yaowu Xuf883b422016-08-30 14:01:10 -07001799 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
1800 aom_highbd_sad64x128_avg_bits12,
1801 aom_highbd_12_variance64x128,
1802 aom_highbd_12_sub_pixel_variance64x128,
1803 aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
1804 aom_highbd_sad64x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001805#endif // CONFIG_EXT_PARTITION
1806
1807#if CONFIG_EXT_INTER
1808#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001809 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
1810 aom_highbd_12_masked_sub_pixel_variance128x128)
1811 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
1812 aom_highbd_12_masked_sub_pixel_variance128x64)
1813 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
1814 aom_highbd_12_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001815#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001816 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
1817 aom_highbd_12_masked_sub_pixel_variance64x64)
1818 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
1819 aom_highbd_12_masked_sub_pixel_variance64x32)
1820 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
1821 aom_highbd_12_masked_sub_pixel_variance32x64)
1822 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
1823 aom_highbd_12_masked_sub_pixel_variance32x32)
1824 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
1825 aom_highbd_12_masked_sub_pixel_variance32x16)
1826 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
1827 aom_highbd_12_masked_sub_pixel_variance16x32)
1828 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
1829 aom_highbd_12_masked_sub_pixel_variance16x16)
1830 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
1831 aom_highbd_12_masked_sub_pixel_variance8x16)
1832 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
1833 aom_highbd_12_masked_sub_pixel_variance16x8)
1834 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
1835 aom_highbd_12_masked_sub_pixel_variance8x8)
1836 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
1837 aom_highbd_12_masked_sub_pixel_variance4x8)
1838 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
1839 aom_highbd_12_masked_sub_pixel_variance8x4)
1840 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
1841 aom_highbd_12_masked_sub_pixel_variance4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001842#endif // CONFIG_EXT_INTER
1843
Yue Chencb60b182016-10-13 15:18:22 -07001844#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001845#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001846 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
1847 aom_highbd_12_obmc_variance128x128,
1848 aom_highbd_12_obmc_sub_pixel_variance128x128)
1849 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
1850 aom_highbd_12_obmc_variance128x64,
1851 aom_highbd_12_obmc_sub_pixel_variance128x64)
1852 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
1853 aom_highbd_12_obmc_variance64x128,
1854 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001855#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001856 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
1857 aom_highbd_12_obmc_variance64x64,
1858 aom_highbd_12_obmc_sub_pixel_variance64x64)
1859 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
1860 aom_highbd_12_obmc_variance64x32,
1861 aom_highbd_12_obmc_sub_pixel_variance64x32)
1862 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
1863 aom_highbd_12_obmc_variance32x64,
1864 aom_highbd_12_obmc_sub_pixel_variance32x64)
1865 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
1866 aom_highbd_12_obmc_variance32x32,
1867 aom_highbd_12_obmc_sub_pixel_variance32x32)
1868 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
1869 aom_highbd_12_obmc_variance32x16,
1870 aom_highbd_12_obmc_sub_pixel_variance32x16)
1871 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
1872 aom_highbd_12_obmc_variance16x32,
1873 aom_highbd_12_obmc_sub_pixel_variance16x32)
1874 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
1875 aom_highbd_12_obmc_variance16x16,
1876 aom_highbd_12_obmc_sub_pixel_variance16x16)
1877 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
1878 aom_highbd_12_obmc_variance8x16,
1879 aom_highbd_12_obmc_sub_pixel_variance8x16)
1880 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
1881 aom_highbd_12_obmc_variance16x8,
1882 aom_highbd_12_obmc_sub_pixel_variance16x8)
1883 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
1884 aom_highbd_12_obmc_variance8x8,
1885 aom_highbd_12_obmc_sub_pixel_variance8x8)
1886 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
1887 aom_highbd_12_obmc_variance4x8,
1888 aom_highbd_12_obmc_sub_pixel_variance4x8)
1889 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
1890 aom_highbd_12_obmc_variance8x4,
1891 aom_highbd_12_obmc_sub_pixel_variance8x4)
1892 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
1893 aom_highbd_12_obmc_variance4x4,
1894 aom_highbd_12_obmc_sub_pixel_variance4x4)
Yue Chencb60b182016-10-13 15:18:22 -07001895#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001896 break;
1897
1898 default:
1899 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07001900 "cm->bit_depth should be AOM_BITS_8, "
1901 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07001902 }
1903 }
1904}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001905#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001906
Yaowu Xuf883b422016-08-30 14:01:10 -07001907static void realloc_segmentation_maps(AV1_COMP *cpi) {
1908 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001909
1910 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07001911 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001912 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07001913 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001914
1915 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07001916 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001917 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07001918 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001919
1920 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07001921 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001922 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07001923 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001924}
1925
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07001926#if CONFIG_EXT_INTER
1927void set_compound_tools(AV1_COMMON *cm) {
1928 (void)cm;
1929#if CONFIG_INTERINTRA
1930 cm->allow_interintra_compound = 1;
1931#endif // CONFIG_INTERINTRA
1932#if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
1933 cm->allow_masked_compound = 1;
1934#endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
1935}
1936#endif // CONFIG_EXT_INTER
1937
Yaowu Xuf883b422016-08-30 14:01:10 -07001938void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
1939 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001940 RATE_CONTROL *const rc = &cpi->rc;
1941
1942 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
1943 cm->bit_depth = oxcf->bit_depth;
1944 cm->color_space = oxcf->color_space;
1945 cm->color_range = oxcf->color_range;
1946
1947 if (cm->profile <= PROFILE_1)
Yaowu Xuf883b422016-08-30 14:01:10 -07001948 assert(cm->bit_depth == AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001949 else
Yaowu Xuf883b422016-08-30 14:01:10 -07001950 assert(cm->bit_depth > AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001951
1952 cpi->oxcf = *oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001953 cpi->td.mb.e_mbd.bd = (int)cm->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001954#if CONFIG_GLOBAL_MOTION
1955 cpi->td.mb.e_mbd.global_motion = cm->global_motion;
1956#endif // CONFIG_GLOBAL_MOTION
1957
Yaowu Xuf883b422016-08-30 14:01:10 -07001958 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001959 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
1960 } else {
1961 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
1962 }
1963
1964 cpi->refresh_last_frame = 1;
1965 cpi->refresh_golden_frame = 0;
1966#if CONFIG_EXT_REFS
1967 cpi->refresh_bwd_ref_frame = 0;
1968#endif // CONFIG_EXT_REFS
1969
1970 cm->refresh_frame_context =
1971 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
1972 ? REFRESH_FRAME_CONTEXT_FORWARD
1973 : REFRESH_FRAME_CONTEXT_BACKWARD;
1974 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
1975
Urvang Joshib100db72016-10-12 16:28:56 -07001976#if CONFIG_PALETTE
Yaowu Xuf883b422016-08-30 14:01:10 -07001977 cm->allow_screen_content_tools = (cpi->oxcf.content == AOM_CONTENT_SCREEN);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001978 if (cm->allow_screen_content_tools) {
1979 MACROBLOCK *x = &cpi->td.mb;
1980 if (x->palette_buffer == 0) {
1981 CHECK_MEM_ERROR(cm, x->palette_buffer,
Yaowu Xuf883b422016-08-30 14:01:10 -07001982 aom_memalign(16, sizeof(*x->palette_buffer)));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001983 }
1984 // Reallocate the pc_tree, as it's contents depends on
1985 // the state of cm->allow_screen_content_tools
Yaowu Xuf883b422016-08-30 14:01:10 -07001986 av1_free_pc_tree(&cpi->td);
1987 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001988 }
Urvang Joshib100db72016-10-12 16:28:56 -07001989#endif // CONFIG_PALETTE
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07001990#if CONFIG_EXT_INTER
1991 set_compound_tools(cm);
1992#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07001993 av1_reset_segment_features(cm);
1994 av1_set_high_precision_mv(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001995
Yaowu Xuc27fc142016-08-22 16:08:15 -07001996 set_rc_buffer_sizes(rc, &cpi->oxcf);
1997
1998 // Under a configuration change, where maximum_buffer_size may change,
1999 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002000 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2001 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002002
2003 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002004 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002005
2006 // Set absolute upper and lower quality limits
2007 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2008 rc->best_quality = cpi->oxcf.best_allowed_q;
2009
2010 cm->interp_filter = cpi->sf.default_interp_filter;
2011
2012 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2013 cm->render_width = cpi->oxcf.render_width;
2014 cm->render_height = cpi->oxcf.render_height;
2015 } else {
2016 cm->render_width = cpi->oxcf.width;
2017 cm->render_height = cpi->oxcf.height;
2018 }
2019 cm->width = cpi->oxcf.width;
2020 cm->height = cpi->oxcf.height;
2021
2022 if (cpi->initial_width) {
2023 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002024 av1_free_context_buffers(cm);
2025 av1_alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002026 realloc_segmentation_maps(cpi);
2027 cpi->initial_width = cpi->initial_height = 0;
2028 }
2029 }
2030 update_frame_size(cpi);
2031
2032 cpi->alt_ref_source = NULL;
2033 rc->is_src_frame_alt_ref = 0;
2034
2035#if CONFIG_EXT_REFS
2036 rc->is_bwd_ref_frame = 0;
2037 rc->is_last_bipred_frame = 0;
2038 rc->is_bipred_frame = 0;
2039#endif // CONFIG_EXT_REFS
2040
2041#if 0
2042 // Experimental RD Code
2043 cpi->frame_distortion = 0;
2044 cpi->last_frame_distortion = 0;
2045#endif
2046
2047 set_tile_info(cpi);
2048
2049 cpi->ext_refresh_frame_flags_pending = 0;
2050 cpi->ext_refresh_frame_context_pending = 0;
2051
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002052#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002053 highbd_set_var_fns(cpi);
2054#endif
Alex Converseeb780e72016-12-13 12:46:41 -08002055
2056#if CONFIG_ANS && ANS_MAX_SYMBOLS
2057 cpi->common.ans_window_size_log2 = cpi->oxcf.ans_window_size_log2;
2058 if (cpi->buf_ans.size != (1 << cpi->common.ans_window_size_log2)) {
2059 aom_buf_ans_free(&cpi->buf_ans);
2060 aom_buf_ans_alloc(&cpi->buf_ans, &cpi->common.error,
2061 1 << cpi->common.ans_window_size_log2);
2062 }
2063#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002064}
2065
Yaowu Xuf883b422016-08-30 14:01:10 -07002066static INLINE void init_upsampled_ref_frame_bufs(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002067 int i;
2068
2069 for (i = 0; i < (REF_FRAMES + 1); ++i) {
2070 cpi->upsampled_ref_bufs[i].ref_count = 0;
2071 cpi->upsampled_ref_idx[i] = INVALID_IDX;
2072 }
2073}
2074
Yaowu Xuf883b422016-08-30 14:01:10 -07002075AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2076 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002077 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002078 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2079 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002080
2081 if (!cm) return NULL;
2082
Yaowu Xuf883b422016-08-30 14:01:10 -07002083 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002084
2085 if (setjmp(cm->error.jmp)) {
2086 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002087 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002088 return 0;
2089 }
2090
2091 cm->error.setjmp = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002092 cm->alloc_mi = av1_enc_alloc_mi;
2093 cm->free_mi = av1_enc_free_mi;
2094 cm->setup_mi = av1_enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002095
Angie Chianga5d96c42016-10-21 16:16:56 -07002096 CHECK_MEM_ERROR(cm, cm->fc,
2097 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2098 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2099 (FRAME_CONTEXT *)aom_memalign(
2100 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2101 memset(cm->fc, 0, sizeof(*cm->fc));
2102 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002103
2104 cpi->resize_state = 0;
2105 cpi->resize_avg_qp = 0;
2106 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002107
Yaowu Xuc27fc142016-08-22 16:08:15 -07002108 cpi->common.buffer_pool = pool;
2109
2110 init_config(cpi, oxcf);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002111#if CONFIG_XIPHRC
2112 cpi->od_rc.framerate = cpi->framerate;
2113 cpi->od_rc.frame_width = cm->render_width;
2114 cpi->od_rc.frame_height = cm->render_height;
2115 cpi->od_rc.keyframe_rate = oxcf->key_freq;
2116 cpi->od_rc.goldenframe_rate = FIXED_GF_INTERVAL;
2117 cpi->od_rc.altref_rate = 25;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002118 cpi->od_rc.firstpass_quant = 1;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002119 cpi->od_rc.bit_depth = cm->bit_depth;
2120 cpi->od_rc.minq = oxcf->best_allowed_q;
2121 cpi->od_rc.maxq = oxcf->worst_allowed_q;
2122 if (cpi->oxcf.rc_mode == AOM_CQ) cpi->od_rc.minq = cpi->od_rc.quality;
2123 cpi->od_rc.quality = cpi->oxcf.rc_mode == AOM_Q ? oxcf->cq_level : -1;
2124 cpi->od_rc.periodic_boosts = oxcf->frame_periodic_boost;
2125 od_enc_rc_init(&cpi->od_rc,
2126 cpi->oxcf.rc_mode == AOM_Q ? -1 : oxcf->target_bandwidth,
2127 oxcf->maximum_buffer_size_ms);
2128#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002129 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002130#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002131
2132 cm->current_video_frame = 0;
2133 cpi->partition_search_skippable_frame = 0;
2134 cpi->tile_data = NULL;
2135 cpi->last_show_frame_buf_idx = INVALID_IDX;
2136
2137 realloc_segmentation_maps(cpi);
2138
Yaowu Xuc27fc142016-08-22 16:08:15 -07002139 for (i = 0; i < NMV_CONTEXTS; ++i) {
Urvang Joshibffc0b52016-07-25 13:38:49 -07002140 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2141 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002142 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002143
Yaowu Xuc27fc142016-08-22 16:08:15 -07002144 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2145 i++) {
2146 CHECK_MEM_ERROR(
2147 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002148 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002149 }
2150
2151#if CONFIG_FP_MB_STATS
2152 cpi->use_fp_mb_stats = 0;
2153 if (cpi->use_fp_mb_stats) {
2154 // a place holder used to store the first pass mb stats in the first pass
2155 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002156 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002157 } else {
2158 cpi->twopass.frame_mb_stats_buf = NULL;
2159 }
2160#endif
2161
2162 cpi->refresh_alt_ref_frame = 0;
2163 cpi->multi_arf_last_grp_enabled = 0;
2164
2165 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2166#if CONFIG_INTERNAL_STATS
2167 cpi->b_calculate_blockiness = 1;
2168 cpi->b_calculate_consistency = 1;
2169 cpi->total_inconsistency = 0;
2170 cpi->psnr.worst = 100.0;
2171 cpi->worst_ssim = 100.0;
2172
2173 cpi->count = 0;
2174 cpi->bytes = 0;
2175
2176 if (cpi->b_calculate_psnr) {
2177 cpi->total_sq_error = 0;
2178 cpi->total_samples = 0;
2179 cpi->tot_recode_hits = 0;
2180 cpi->summed_quality = 0;
2181 cpi->summed_weights = 0;
2182 }
2183
2184 cpi->fastssim.worst = 100.0;
2185 cpi->psnrhvs.worst = 100.0;
2186
2187 if (cpi->b_calculate_blockiness) {
2188 cpi->total_blockiness = 0;
2189 cpi->worst_blockiness = 0.0;
2190 }
2191
2192 if (cpi->b_calculate_consistency) {
2193 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002194 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002195 cpi->common.mi_rows * cpi->common.mi_cols));
2196 cpi->worst_consistency = 100.0;
2197 }
2198#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002199#if CONFIG_ENTROPY_STATS
2200 av1_zero(aggregate_fc);
2201#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002202
2203 cpi->first_time_stamp_ever = INT64_MAX;
2204
Yaowu Xuc27fc142016-08-22 16:08:15 -07002205 for (i = 0; i < NMV_CONTEXTS; ++i) {
2206 cpi->td.mb.nmvcost[i][0] = &cpi->nmv_costs[i][0][MV_MAX];
2207 cpi->td.mb.nmvcost[i][1] = &cpi->nmv_costs[i][1][MV_MAX];
2208 cpi->td.mb.nmvcost_hp[i][0] = &cpi->nmv_costs_hp[i][0][MV_MAX];
2209 cpi->td.mb.nmvcost_hp[i][1] = &cpi->nmv_costs_hp[i][1][MV_MAX];
2210 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002211
2212#ifdef OUTPUT_YUV_SKINMAP
2213 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2214#endif
2215#ifdef OUTPUT_YUV_REC
2216 yuv_rec_file = fopen("rec.yuv", "wb");
2217#endif
2218
2219#if 0
2220 framepsnr = fopen("framepsnr.stt", "a");
2221 kf_list = fopen("kf_list.stt", "w");
2222#endif
2223
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002224#if CONFIG_XIPHRC
2225 if (oxcf->pass == 2) {
2226 cpi->od_rc.twopass_allframes_buf = oxcf->two_pass_stats_in.buf;
2227 cpi->od_rc.twopass_allframes_buf_size = oxcf->two_pass_stats_in.sz;
2228 }
2229#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002230 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002231 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002232 } else if (oxcf->pass == 2) {
2233 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2234 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2235
2236#if CONFIG_FP_MB_STATS
2237 if (cpi->use_fp_mb_stats) {
2238 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2239 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2240
2241 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2242 oxcf->firstpass_mb_stats_in.buf;
2243 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2244 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2245 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2246 }
2247#endif
2248
2249 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2250 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2251 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2252
Yaowu Xuf883b422016-08-30 14:01:10 -07002253 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002254 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002255#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256
Jingning Hand064cf02017-06-01 10:00:39 -07002257#if CONFIG_MOTION_VAR
2258#if CONFIG_HIGHBITDEPTH
2259 int buf_scaler = 2;
2260#else
2261 int buf_scaler = 1;
2262#endif
2263 CHECK_MEM_ERROR(
2264 cm, cpi->td.mb.above_pred_buf,
2265 (uint8_t *)aom_memalign(16, buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2266 sizeof(*cpi->td.mb.above_pred_buf)));
2267 CHECK_MEM_ERROR(
2268 cm, cpi->td.mb.left_pred_buf,
2269 (uint8_t *)aom_memalign(16, buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2270 sizeof(*cpi->td.mb.left_pred_buf)));
2271
2272 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2273 (int32_t *)aom_memalign(
2274 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2275
2276 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2277 (int32_t *)aom_memalign(
2278 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2279
2280#endif
2281
Yaowu Xuc27fc142016-08-22 16:08:15 -07002282 init_upsampled_ref_frame_bufs(cpi);
2283
Yaowu Xuf883b422016-08-30 14:01:10 -07002284 av1_set_speed_features_framesize_independent(cpi);
2285 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002286
Yaowu Xuc27fc142016-08-22 16:08:15 -07002287#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
2288 cpi->fn_ptr[BT].sdf = SDF; \
2289 cpi->fn_ptr[BT].sdaf = SDAF; \
2290 cpi->fn_ptr[BT].vf = VF; \
2291 cpi->fn_ptr[BT].svf = SVF; \
2292 cpi->fn_ptr[BT].svaf = SVAF; \
2293 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2294 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2295 cpi->fn_ptr[BT].sdx4df = SDX4DF;
2296
2297#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002298 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2299 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2300 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002301
Yaowu Xuf883b422016-08-30 14:01:10 -07002302 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2303 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
2304 NULL, aom_sad128x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002305
Yaowu Xuf883b422016-08-30 14:01:10 -07002306 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2307 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
2308 NULL, aom_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002309#endif // CONFIG_EXT_PARTITION
2310
Yaowu Xuf883b422016-08-30 14:01:10 -07002311 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2312 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2313 aom_sad32x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002314
Yaowu Xuf883b422016-08-30 14:01:10 -07002315 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2316 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2317 aom_sad16x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002318
Yaowu Xuf883b422016-08-30 14:01:10 -07002319 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2320 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2321 aom_sad64x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002322
Yaowu Xuf883b422016-08-30 14:01:10 -07002323 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2324 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2325 aom_sad32x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002326
Yaowu Xuf883b422016-08-30 14:01:10 -07002327 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2328 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2329 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002330
Yaowu Xuf883b422016-08-30 14:01:10 -07002331 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2332 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2333 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002334
Yaowu Xuf883b422016-08-30 14:01:10 -07002335 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2336 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2337 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002338
Yaowu Xuf883b422016-08-30 14:01:10 -07002339 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2340 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
2341 aom_sad16x8x8, aom_sad16x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002342
Yaowu Xuf883b422016-08-30 14:01:10 -07002343 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2344 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
2345 aom_sad8x16x8, aom_sad8x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002346
Yaowu Xuf883b422016-08-30 14:01:10 -07002347 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2348 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
2349 aom_sad8x8x8, aom_sad8x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002350
Yaowu Xuf883b422016-08-30 14:01:10 -07002351 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2352 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
2353 aom_sad8x4x8, aom_sad8x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002354
Yaowu Xuf883b422016-08-30 14:01:10 -07002355 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2356 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
2357 aom_sad4x8x8, aom_sad4x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002358
Yaowu Xuf883b422016-08-30 14:01:10 -07002359 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2360 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
2361 aom_sad4x4x8, aom_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002362
Jingning Han9e7c49f2016-12-06 11:20:10 -08002363#if CONFIG_CB4X4
2364 BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL)
Jingning Hane2ffaf82016-12-14 15:26:30 -08002365 BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL)
2366 BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL)
Jingning Han9e7c49f2016-12-06 11:20:10 -08002367#endif
2368
Yue Chencb60b182016-10-13 15:18:22 -07002369#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370#define OBFP(BT, OSDF, OVF, OSVF) \
2371 cpi->fn_ptr[BT].osdf = OSDF; \
2372 cpi->fn_ptr[BT].ovf = OVF; \
2373 cpi->fn_ptr[BT].osvf = OSVF;
2374
2375#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002376 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2377 aom_obmc_sub_pixel_variance128x128)
2378 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2379 aom_obmc_sub_pixel_variance128x64)
2380 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2381 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002382#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002383 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2384 aom_obmc_sub_pixel_variance64x64)
2385 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2386 aom_obmc_sub_pixel_variance64x32)
2387 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2388 aom_obmc_sub_pixel_variance32x64)
2389 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2390 aom_obmc_sub_pixel_variance32x32)
2391 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2392 aom_obmc_sub_pixel_variance32x16)
2393 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2394 aom_obmc_sub_pixel_variance16x32)
2395 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2396 aom_obmc_sub_pixel_variance16x16)
2397 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2398 aom_obmc_sub_pixel_variance16x8)
2399 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2400 aom_obmc_sub_pixel_variance8x16)
2401 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2402 aom_obmc_sub_pixel_variance8x8)
2403 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2404 aom_obmc_sub_pixel_variance4x8)
2405 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2406 aom_obmc_sub_pixel_variance8x4)
2407 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2408 aom_obmc_sub_pixel_variance4x4)
Yue Chencb60b182016-10-13 15:18:22 -07002409#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002410
2411#if CONFIG_EXT_INTER
David Barkerf19f35f2017-05-22 16:33:22 +01002412#define MBFP(BT, MCSDF, MCSVF) \
2413 cpi->fn_ptr[BT].msdf = MCSDF; \
2414 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002415
2416#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002417 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2418 aom_masked_sub_pixel_variance128x128)
2419 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2420 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002421#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002422 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2423 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2424 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2425 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2426 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2427 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2428 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2429 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2430 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2431 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2432 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2433 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2434 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002435#endif // CONFIG_EXT_INTER
2436
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002437#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002438 highbd_set_var_fns(cpi);
2439#endif
2440
Yaowu Xuf883b422016-08-30 14:01:10 -07002441 /* av1_init_quantizer() is first called here. Add check in
2442 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002444 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002445 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002446 av1_init_quantizer(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002447#if CONFIG_AOM_QM
2448 aom_qm_init(cm);
2449#endif
2450
Yaowu Xuf883b422016-08-30 14:01:10 -07002451 av1_loop_filter_init(cm);
Fergus Simpsondac6aff2017-05-18 15:11:38 -07002452#if CONFIG_FRAME_SUPERRES
2453 cm->superres_scale_numerator = SUPERRES_SCALE_DENOMINATOR;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07002454 cm->superres_upscaled_width = oxcf->scaled_frame_width;
2455 cm->superres_upscaled_height = oxcf->scaled_frame_height;
Fergus Simpsondac6aff2017-05-18 15:11:38 -07002456#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuc27fc142016-08-22 16:08:15 -07002457#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07002458 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002459#endif // CONFIG_LOOP_RESTORATION
2460
2461 cm->error.setjmp = 0;
2462
2463 return cpi;
2464}
2465
2466#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2467
2468#define SNPRINT2(H, T, V) \
2469 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2470
Yaowu Xuf883b422016-08-30 14:01:10 -07002471void av1_remove_compressor(AV1_COMP *cpi) {
2472 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002473 unsigned int i;
2474 int t;
2475
2476 if (!cpi) return;
2477
2478 cm = &cpi->common;
2479 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002480#if CONFIG_ENTROPY_STATS
2481 if (cpi->oxcf.pass != 1) {
2482 fprintf(stderr, "Writing counts.stt\n");
2483 FILE *f = fopen("counts.stt", "wb");
2484 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
2485 fclose(f);
2486 }
2487#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002488#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07002489 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002490
2491 if (cpi->oxcf.pass != 1) {
2492 char headings[512] = { 0 };
2493 char results[512] = { 0 };
2494 FILE *f = fopen("opsnr.stt", "a");
2495 double time_encoded =
2496 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2497 10000000.000;
2498 double total_encode_time =
2499 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2500 const double dr =
2501 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2502 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2503 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2504 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2505
2506 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002507 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002508 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2509 const double total_ssim =
2510 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2511 snprintf(headings, sizeof(headings),
2512 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07002513 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Yaowu Xuc27fc142016-08-22 16:08:15 -07002514 "WstPsnr\tWstSsim\tWstFast\tWstHVS");
2515 snprintf(results, sizeof(results),
2516 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2517 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2518 "%7.3f\t%7.3f\t%7.3f\t%7.3f",
2519 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
2520 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
2521 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2522 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
2523 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
2524
2525 if (cpi->b_calculate_blockiness) {
2526 SNPRINT(headings, "\t Block\tWstBlck");
2527 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2528 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2529 }
2530
2531 if (cpi->b_calculate_consistency) {
2532 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07002533 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002534 (double)cpi->total_inconsistency);
2535
2536 SNPRINT(headings, "\tConsist\tWstCons");
2537 SNPRINT2(results, "\t%7.3f", consistency);
2538 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2539 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07002540 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2541 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002542 rate_err, fabs(rate_err));
2543 }
2544
2545 fclose(f);
2546 }
2547
2548#endif
2549
2550#if 0
2551 {
2552 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2553 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2554 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2555 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2556 cpi->time_compress_data / 1000,
2557 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2558 }
2559#endif
2560 }
2561
2562 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002563 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002564 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2565
2566 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07002567 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002568
2569 // Deallocate allocated thread data.
2570 if (t < cpi->num_workers - 1) {
Urvang Joshib100db72016-10-12 16:28:56 -07002571#if CONFIG_PALETTE
Yaowu Xuc27fc142016-08-22 16:08:15 -07002572 if (cpi->common.allow_screen_content_tools)
hui su5d493142017-05-08 12:06:12 -07002573 aom_free(thread_data->td->palette_buffer);
Urvang Joshib100db72016-10-12 16:28:56 -07002574#endif // CONFIG_PALETTE
Jingning Hand064cf02017-06-01 10:00:39 -07002575#if CONFIG_MOTION_VAR
2576 aom_free(thread_data->td->above_pred_buf);
2577 aom_free(thread_data->td->left_pred_buf);
2578 aom_free(thread_data->td->wsrc_buf);
2579 aom_free(thread_data->td->mask_buf);
2580#endif // CONFIG_MOTION_VAR
Yaowu Xuf883b422016-08-30 14:01:10 -07002581 aom_free(thread_data->td->counts);
2582 av1_free_pc_tree(thread_data->td);
Yaowu Xuf883b422016-08-30 14:01:10 -07002583 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002584 }
2585 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002586 aom_free(cpi->tile_thr_data);
2587 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002588
Yaowu Xuf883b422016-08-30 14:01:10 -07002589 if (cpi->num_workers > 1) av1_loop_filter_dealloc(&cpi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002590
2591 dealloc_compressor_data(cpi);
2592
2593 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2594 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002595 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002596 }
2597
2598#if CONFIG_FP_MB_STATS
2599 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002600 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002601 cpi->twopass.frame_mb_stats_buf = NULL;
2602 }
2603#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08002604#if CONFIG_INTERNAL_STATS
2605 aom_free(cpi->ssim_vars);
2606 cpi->ssim_vars = NULL;
2607#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002608
Yaowu Xuf883b422016-08-30 14:01:10 -07002609 av1_remove_common(cm);
2610 av1_free_ref_frame_buffers(cm->buffer_pool);
2611 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002612
2613#ifdef OUTPUT_YUV_SKINMAP
2614 fclose(yuv_skinmap_file);
2615#endif
2616#ifdef OUTPUT_YUV_REC
2617 fclose(yuv_rec_file);
2618#endif
2619
2620#if 0
2621
2622 if (keyfile)
2623 fclose(keyfile);
2624
2625 if (framepsnr)
2626 fclose(framepsnr);
2627
2628 if (kf_list)
2629 fclose(kf_list);
2630
2631#endif
2632}
2633
Yaowu Xuf883b422016-08-30 14:01:10 -07002634static void generate_psnr_packet(AV1_COMP *cpi) {
2635 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002636 int i;
2637 PSNR_STATS psnr;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002638#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07002639 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002640 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
2641#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07002642 aom_calc_psnr(cpi->source, cpi->common.frame_to_show, &psnr);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002643#endif
2644
2645 for (i = 0; i < 4; ++i) {
2646 pkt.data.psnr.samples[i] = psnr.samples[i];
2647 pkt.data.psnr.sse[i] = psnr.sse[i];
2648 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2649 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002650 pkt.kind = AOM_CODEC_PSNR_PKT;
2651 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002652}
2653
Yaowu Xuf883b422016-08-30 14:01:10 -07002654int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
2656
2657 cpi->ref_frame_flags = ref_frame_flags;
2658 return 0;
2659}
2660
Yaowu Xuf883b422016-08-30 14:01:10 -07002661void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags) {
2662 cpi->ext_refresh_golden_frame = (ref_frame_flags & AOM_GOLD_FLAG) != 0;
2663 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & AOM_ALT_FLAG) != 0;
2664 cpi->ext_refresh_last_frame = (ref_frame_flags & AOM_LAST_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665 cpi->ext_refresh_frame_flags_pending = 1;
2666}
2667
Yaowu Xuf883b422016-08-30 14:01:10 -07002668static YV12_BUFFER_CONFIG *get_av1_ref_frame_buffer(
2669 AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag) {
Emil Keyder01770b32017-01-20 18:03:11 -05002670 MV_REFERENCE_FRAME ref_frame = NONE_FRAME;
Yaowu Xuf883b422016-08-30 14:01:10 -07002671 if (ref_frame_flag == AOM_LAST_FLAG) ref_frame = LAST_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07002673 else if (ref_frame_flag == AOM_LAST2_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002674 ref_frame = LAST2_FRAME;
Yaowu Xuf883b422016-08-30 14:01:10 -07002675 else if (ref_frame_flag == AOM_LAST3_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002676 ref_frame = LAST3_FRAME;
2677#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07002678 else if (ref_frame_flag == AOM_GOLD_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002679 ref_frame = GOLDEN_FRAME;
2680#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07002681 else if (ref_frame_flag == AOM_BWD_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002682 ref_frame = BWDREF_FRAME;
2683#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07002684 else if (ref_frame_flag == AOM_ALT_FLAG)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002685 ref_frame = ALTREF_FRAME;
2686
Emil Keyder01770b32017-01-20 18:03:11 -05002687 return ref_frame == NONE_FRAME ? NULL : get_ref_frame_buffer(cpi, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002688}
2689
Yaowu Xuf883b422016-08-30 14:01:10 -07002690int av1_copy_reference_enc(AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002691 YV12_BUFFER_CONFIG *sd) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002692 YV12_BUFFER_CONFIG *cfg = get_av1_ref_frame_buffer(cpi, ref_frame_flag);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002693 if (cfg) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002694 aom_yv12_copy_frame(cfg, sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002695 return 0;
2696 } else {
2697 return -1;
2698 }
2699}
2700
Yaowu Xuf883b422016-08-30 14:01:10 -07002701int av1_set_reference_enc(AV1_COMP *cpi, AOM_REFFRAME ref_frame_flag,
2702 YV12_BUFFER_CONFIG *sd) {
2703 YV12_BUFFER_CONFIG *cfg = get_av1_ref_frame_buffer(cpi, ref_frame_flag);
2704 if (cfg) {
2705 aom_yv12_copy_frame(sd, cfg);
2706 return 0;
2707 } else {
2708 return -1;
2709 }
2710}
2711
2712int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002713 cpi->ext_refresh_frame_context = update;
2714 cpi->ext_refresh_frame_context_pending = 1;
2715 return 0;
2716}
2717
2718#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
2719// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2720// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2721// not denoise the UV channels at this time. If ever we implement UV channel
2722// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07002723void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002724 uint8_t *src = s->y_buffer;
2725 int h = s->y_height;
2726
2727 do {
2728 fwrite(src, s->y_width, 1, f);
2729 src += s->y_stride;
2730 } while (--h);
2731
2732 src = s->u_buffer;
2733 h = s->uv_height;
2734
2735 do {
2736 fwrite(src, s->uv_width, 1, f);
2737 src += s->uv_stride;
2738 } while (--h);
2739
2740 src = s->v_buffer;
2741 h = s->uv_height;
2742
2743 do {
2744 fwrite(src, s->uv_width, 1, f);
2745 src += s->uv_stride;
2746 } while (--h);
2747}
2748#endif
2749
Zoe Liue04abf72017-04-19 15:37:11 -07002750#if CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07002751static void check_show_existing_frame(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002752 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07002753 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002754 const FRAME_UPDATE_TYPE next_frame_update_type =
2755 gf_group->update_type[gf_group->index];
2756 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07002757
2758 if (cm->show_existing_frame == 1) {
2759 cm->show_existing_frame = 0;
2760 } else if (cpi->rc.is_last_bipred_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002761 // NOTE(zoeliu): If the current frame is a last bi-predictive frame, it is
2762 // needed next to show the BWDREF_FRAME, which is pointed by
2763 // the last_fb_idxes[0] after reference frame buffer update
2764 cpi->rc.is_last_bipred_frame = 0;
2765 cm->show_existing_frame = 1;
2766 cpi->existing_fb_idx_to_show = cpi->lst_fb_idxes[0];
2767 } else if (cpi->is_arf_filter_off[which_arf] &&
2768 (next_frame_update_type == OVERLAY_UPDATE ||
2769 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
2770 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07002771 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002772 cm->show_existing_frame = 1;
2773 cpi->rc.is_src_frame_alt_ref = 1;
2774 cpi->existing_fb_idx_to_show = cpi->alt_fb_idx;
2775 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002776 }
2777 cpi->rc.is_src_frame_ext_arf = 0;
2778}
Zoe Liue04abf72017-04-19 15:37:11 -07002779#endif // CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07002780
2781#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07002782void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002783 uint8_t *src = s->y_buffer;
2784 int h = cm->height;
2785
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002786#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002787 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
2788 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
2789
2790 do {
2791 fwrite(src16, s->y_width, 2, yuv_rec_file);
2792 src16 += s->y_stride;
2793 } while (--h);
2794
2795 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
2796 h = s->uv_height;
2797
2798 do {
2799 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2800 src16 += s->uv_stride;
2801 } while (--h);
2802
2803 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
2804 h = s->uv_height;
2805
2806 do {
2807 fwrite(src16, s->uv_width, 2, yuv_rec_file);
2808 src16 += s->uv_stride;
2809 } while (--h);
2810
2811 fflush(yuv_rec_file);
2812 return;
2813 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002814#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002815
2816 do {
2817 fwrite(src, s->y_width, 1, yuv_rec_file);
2818 src += s->y_stride;
2819 } while (--h);
2820
2821 src = s->u_buffer;
2822 h = s->uv_height;
2823
2824 do {
2825 fwrite(src, s->uv_width, 1, yuv_rec_file);
2826 src += s->uv_stride;
2827 } while (--h);
2828
2829 src = s->v_buffer;
2830 h = s->uv_height;
2831
2832 do {
2833 fwrite(src, s->uv_width, 1, yuv_rec_file);
2834 src += s->uv_stride;
2835 } while (--h);
2836
2837 fflush(yuv_rec_file);
2838}
2839#endif // OUTPUT_YUV_REC
2840
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002841#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002842static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2843 YV12_BUFFER_CONFIG *dst, int planes,
2844 int bd) {
2845#else
2846static void scale_and_extend_frame(const YV12_BUFFER_CONFIG *src,
2847 YV12_BUFFER_CONFIG *dst, int planes) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002848#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002849 const int src_w = src->y_crop_width;
2850 const int src_h = src->y_crop_height;
2851 const int dst_w = dst->y_crop_width;
2852 const int dst_h = dst->y_crop_height;
2853 const uint8_t *const srcs[3] = { src->y_buffer, src->u_buffer,
2854 src->v_buffer };
2855 const int src_strides[3] = { src->y_stride, src->uv_stride, src->uv_stride };
2856 uint8_t *const dsts[3] = { dst->y_buffer, dst->u_buffer, dst->v_buffer };
2857 const int dst_strides[3] = { dst->y_stride, dst->uv_stride, dst->uv_stride };
2858 const InterpFilterParams interp_filter_params =
Yaowu Xuf883b422016-08-30 14:01:10 -07002859 av1_get_interp_filter_params(EIGHTTAP_REGULAR);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002860 const int16_t *kernel = interp_filter_params.filter_ptr;
2861 const int taps = interp_filter_params.taps;
2862 int x, y, i;
2863
Yaowu Xu637590c2016-11-16 15:15:46 -08002864 assert(planes <= 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002865 for (y = 0; y < dst_h; y += 16) {
2866 for (x = 0; x < dst_w; x += 16) {
2867 for (i = 0; i < planes; ++i) {
2868 const int factor = (i == 0 || i == 3 ? 1 : 2);
2869 const int x_q4 = x * (16 / factor) * src_w / dst_w;
2870 const int y_q4 = y * (16 / factor) * src_h / dst_h;
2871 const int src_stride = src_strides[i];
2872 const int dst_stride = dst_strides[i];
2873 const uint8_t *src_ptr = srcs[i] +
2874 (y / factor) * src_h / dst_h * src_stride +
2875 (x / factor) * src_w / dst_w;
2876 uint8_t *dst_ptr = dsts[i] + (y / factor) * dst_stride + (x / factor);
2877
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002878#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002879 if (src->flags & YV12_FLAG_HIGHBITDEPTH) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002880 aom_highbd_convolve8(src_ptr, src_stride, dst_ptr, dst_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002881 &kernel[(x_q4 & 0xf) * taps], 16 * src_w / dst_w,
2882 &kernel[(y_q4 & 0xf) * taps], 16 * src_h / dst_h,
2883 16 / factor, 16 / factor, bd);
2884 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07002885 aom_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002886 &kernel[(x_q4 & 0xf) * taps], 16 * src_w / dst_w,
2887 &kernel[(y_q4 & 0xf) * taps], 16 * src_h / dst_h,
2888 16 / factor, 16 / factor);
2889 }
2890#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002891 aom_scaled_2d(src_ptr, src_stride, dst_ptr, dst_stride,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002892 &kernel[(x_q4 & 0xf) * taps], 16 * src_w / dst_w,
2893 &kernel[(y_q4 & 0xf) * taps], 16 * src_h / dst_h,
2894 16 / factor, 16 / factor);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002895#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002896 }
2897 }
2898 }
2899
2900 if (planes == 1)
Yaowu Xuf883b422016-08-30 14:01:10 -07002901 aom_extend_frame_borders_y(dst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002902 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002903 aom_extend_frame_borders(dst);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002904}
2905
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08002906#if CONFIG_GLOBAL_MOTION
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07002907#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08002908static int recode_loop_test_global_motion(AV1_COMP *cpi) {
2909 int i;
2910 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07002911 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08002912 AV1_COMMON *const cm = &cpi->common;
2913 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
2914 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07002915 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07002916 cpi->gmparams_cost[i]) {
Sarah Parker4c10a3c2017-04-10 19:37:59 -07002917 set_default_warp_params(&cm->global_motion[i]);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07002918 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00002919 recode = 1;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07002920 recode |= (rdc->global_motion_used[i] > 0);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08002921 }
2922 }
2923 return recode;
2924}
2925#endif // CONFIG_GLOBAL_MOTION
2926
Yaowu Xuc27fc142016-08-22 16:08:15 -07002927// Function to test for conditions that indicate we should loop
2928// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07002929static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
2930 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002931 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07002932 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002933 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
2934 int force_recode = 0;
2935
2936 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
2937 (cpi->sf.recode_loop == ALLOW_RECODE) ||
2938 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002939 // TODO(agrange) high_limit could be greater than the scale-down threshold.
2940 if ((rc->projected_frame_size > high_limit && q < maxq) ||
2941 (rc->projected_frame_size < low_limit && q > minq)) {
2942 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002943 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002944 // Deal with frame undershoot and whether or not we are
2945 // below the automatically set cq level.
2946 if (q > oxcf->cq_level &&
2947 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
2948 force_recode = 1;
2949 }
2950 }
2951 }
2952 return force_recode;
2953}
2954
2955static INLINE int get_free_upsampled_ref_buf(EncRefCntBuffer *ubufs) {
2956 int i;
2957
2958 for (i = 0; i < (REF_FRAMES + 1); i++) {
2959 if (!ubufs[i].ref_count) {
2960 return i;
2961 }
2962 }
2963 return INVALID_IDX;
2964}
2965
2966// Up-sample 1 reference frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07002967static INLINE int upsample_ref_frame(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002968 const YV12_BUFFER_CONFIG *const ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002969 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002970 EncRefCntBuffer *ubufs = cpi->upsampled_ref_bufs;
2971 int new_uidx = get_free_upsampled_ref_buf(ubufs);
2972
2973 if (new_uidx == INVALID_IDX) {
2974 return INVALID_IDX;
2975 } else {
2976 YV12_BUFFER_CONFIG *upsampled_ref = &ubufs[new_uidx].buf;
2977
2978 // Can allocate buffer for Y plane only.
2979 if (upsampled_ref->buffer_alloc_sz < (ref->buffer_alloc_sz << 6))
Yaowu Xuf883b422016-08-30 14:01:10 -07002980 if (aom_realloc_frame_buffer(upsampled_ref, (cm->width << 3),
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981 (cm->height << 3), cm->subsampling_x,
2982 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002983#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002984 cm->use_highbitdepth,
2985#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07002986 (AOM_BORDER_IN_PIXELS << 3),
Yaowu Xuc27fc142016-08-22 16:08:15 -07002987 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07002988 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002989 "Failed to allocate up-sampled frame buffer");
2990
2991// Currently, only Y plane is up-sampled, U, V are not used.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002992#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002993 scale_and_extend_frame(ref, upsampled_ref, 1, (int)cm->bit_depth);
2994#else
2995 scale_and_extend_frame(ref, upsampled_ref, 1);
2996#endif
2997 return new_uidx;
2998 }
2999}
3000
3001#define DUMP_REF_FRAME_IMAGES 0
3002
3003#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003004static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003005 const YV12_BUFFER_CONFIG *const ref_buf,
3006 char *file_name) {
3007 int h;
3008 FILE *f_ref = NULL;
3009
3010 if (ref_buf == NULL) {
3011 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003012 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003013 }
3014
3015 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3016 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003017 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003018 }
3019
3020 // --- Y ---
3021 for (h = 0; h < cm->height; ++h) {
3022 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3023 }
3024 // --- U ---
3025 for (h = 0; h < (cm->height >> 1); ++h) {
3026 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3027 f_ref);
3028 }
3029 // --- V ---
3030 for (h = 0; h < (cm->height >> 1); ++h) {
3031 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3032 f_ref);
3033 }
3034
3035 fclose(f_ref);
3036
Yaowu Xuf883b422016-08-30 14:01:10 -07003037 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003038}
3039
Yaowu Xuf883b422016-08-30 14:01:10 -07003040static void dump_ref_frame_images(AV1_COMP *cpi) {
3041 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003042 MV_REFERENCE_FRAME ref_frame;
3043
3044 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3045 char file_name[256] = "";
3046 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3047 cm->current_video_frame, ref_frame);
3048 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3049 }
3050}
3051#endif // DUMP_REF_FRAME_IMAGES == 1
3052
3053#if CONFIG_EXT_REFS
3054// This function is used to shift the virtual indices of last reference frames
3055// as follows:
3056// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3057// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003058static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003059 int ref_frame;
3060 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
3061 cpi->lst_fb_idxes[ref_frame] = cpi->lst_fb_idxes[ref_frame - 1];
3062
3063 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003064 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003065 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003066 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3067 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3068 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003069 }
3070 }
3071}
Zoe Liuf0e46692016-10-12 12:31:43 -07003072#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003073
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003074#if CONFIG_VAR_REFS
3075static void enc_check_valid_ref_frames(AV1_COMP *const cpi) {
3076 AV1_COMMON *const cm = &cpi->common;
3077 MV_REFERENCE_FRAME ref_frame;
3078
3079 // TODO(zoeliu): To handle ALTREF_FRAME the same way as do with other
3080 // reference frames. Current encoder invalid ALTREF when ALTREF
3081 // is the same as LAST, but invalid all the other references
3082 // when they are the same as ALTREF.
3083 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3084 int ref_buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3085 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3086
3087 if (ref_buf_idx != INVALID_IDX) {
3088 ref_buf->is_valid = 1;
3089
3090 MV_REFERENCE_FRAME ref;
3091 for (ref = LAST_FRAME; ref < ref_frame; ++ref) {
3092 int buf_idx = get_ref_frame_buf_idx(cpi, ref);
3093 RefBuffer *const buf = &cm->frame_refs[ref - LAST_FRAME];
3094 if (buf->is_valid && buf_idx == ref_buf_idx) {
3095 if (ref_frame != ALTREF_FRAME || ref == LAST_FRAME) {
3096 ref_buf->is_valid = 0;
3097 break;
3098 } else {
3099 buf->is_valid = 0;
3100 }
3101 }
3102 }
3103 } else {
3104 ref_buf->is_valid = 0;
3105 }
3106 }
3107}
3108#endif // CONFIG_VAR_REFS
3109
Yaowu Xuf883b422016-08-30 14:01:10 -07003110void av1_update_reference_frames(AV1_COMP *cpi) {
3111 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003112 BufferPool *const pool = cm->buffer_pool;
3113 const int use_upsampled_ref = cpi->sf.use_upsampled_references;
3114 int new_uidx = 0;
3115
3116 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3117 // for the purpose to verify no mismatch between encoder and decoder.
3118 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3119
3120 if (use_upsampled_ref) {
3121#if CONFIG_EXT_REFS
3122 if (cm->show_existing_frame) {
3123 new_uidx = cpi->upsampled_ref_idx[cpi->existing_fb_idx_to_show];
3124 // TODO(zoeliu): Once following is confirmed, remove it.
3125 assert(cpi->upsampled_ref_bufs[new_uidx].ref_count > 0);
3126 } else {
3127#endif // CONFIG_EXT_REFS
3128 // Up-sample the current encoded frame.
3129 RefCntBuffer *bufs = pool->frame_bufs;
3130 const YV12_BUFFER_CONFIG *const ref = &bufs[cm->new_fb_idx].buf;
3131
3132 new_uidx = upsample_ref_frame(cpi, ref);
3133#if CONFIG_EXT_REFS
3134 assert(new_uidx != INVALID_IDX);
3135 }
3136#endif // CONFIG_EXT_REFS
3137 }
3138 // At this point the new frame has been encoded.
3139 // If any buffer copy / swapping is signaled it should be done here.
3140 if (cm->frame_type == KEY_FRAME) {
3141 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3142 cm->new_fb_idx);
3143#if CONFIG_EXT_REFS
3144 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3145 cm->new_fb_idx);
3146#endif // CONFIG_EXT_REFS
3147 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3148 cm->new_fb_idx);
3149
3150 if (use_upsampled_ref) {
3151 uref_cnt_fb(cpi->upsampled_ref_bufs,
3152 &cpi->upsampled_ref_idx[cpi->gld_fb_idx], new_uidx);
3153#if CONFIG_EXT_REFS
3154 uref_cnt_fb(cpi->upsampled_ref_bufs,
3155 &cpi->upsampled_ref_idx[cpi->bwd_fb_idx], new_uidx);
3156#endif // CONFIG_EXT_REFS
3157 uref_cnt_fb(cpi->upsampled_ref_bufs,
3158 &cpi->upsampled_ref_idx[cpi->alt_fb_idx], new_uidx);
3159 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003160 } else if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003161 // We have decided to preserve the previously existing golden frame as our
3162 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003163 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003164 // we're updating the GF with the current decoded frame, we save it to the
3165 // ARF slot instead.
3166 // We now have to update the ARF with the current frame and swap gld_fb_idx
3167 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3168 // slot and, if we're updating the GF, the current frame becomes the new GF.
3169 int tmp;
3170
3171 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3172 cm->new_fb_idx);
3173 if (use_upsampled_ref)
3174 uref_cnt_fb(cpi->upsampled_ref_bufs,
3175 &cpi->upsampled_ref_idx[cpi->alt_fb_idx], new_uidx);
3176
3177 tmp = cpi->alt_fb_idx;
3178 cpi->alt_fb_idx = cpi->gld_fb_idx;
3179 cpi->gld_fb_idx = tmp;
3180
3181#if CONFIG_EXT_REFS
3182 // We need to modify the mapping accordingly
3183 cpi->arf_map[0] = cpi->alt_fb_idx;
3184#endif
3185// TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3186// cpi->interp_filter_selected[GOLDEN_FRAME]?
3187#if CONFIG_EXT_REFS
3188 } else if (cpi->rc.is_last_bipred_frame) {
3189 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the LAST3_FRAME
3190 // by updating the virtual indices. Note that the frame BWDREF_FRAME points
3191 // to now should be retired, and it should not be used before refreshed.
3192 int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3193
3194 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003195 cpi->lst_fb_idxes[0] = cpi->bwd_fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003196 cpi->bwd_fb_idx = tmp;
Zoe Liuf0e46692016-10-12 12:31:43 -07003197
3198 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3199 cpi->interp_filter_selected[BWDREF_FRAME],
3200 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003201 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3202 // Deal with the special case for showing existing internal ALTREF_FRAME
3203 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3204 // by updating the virtual indices.
3205 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3206 int which_arf = gf_group->arf_ref_idx[gf_group->index];
3207 int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3208
3209 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003210 cpi->lst_fb_idxes[0] = cpi->alt_fb_idx;
Zoe Liuf0e46692016-10-12 12:31:43 -07003211 cpi->alt_fb_idx = tmp;
3212
3213 // We need to modify the mapping accordingly
3214 cpi->arf_map[which_arf] = cpi->alt_fb_idx;
3215
Yaowu Xuc27fc142016-08-22 16:08:15 -07003216 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3217 cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3218 sizeof(cpi->interp_filter_selected[ALTREF_FRAME + which_arf]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003219#endif // CONFIG_EXT_REFS
3220 } else { /* For non key/golden frames */
3221 if (cpi->refresh_alt_ref_frame) {
3222 int arf_idx = cpi->alt_fb_idx;
3223 int which_arf = 0;
3224#if CONFIG_EXT_REFS
3225 if (cpi->oxcf.pass == 2) {
3226 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3227 which_arf = gf_group->arf_update_idx[gf_group->index];
3228 arf_idx = cpi->arf_map[which_arf];
3229 }
3230#else
3231 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3232 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3233 arf_idx = gf_group->arf_update_idx[gf_group->index];
3234 }
3235#endif // CONFIG_EXT_REFS
3236 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
3237 if (use_upsampled_ref)
3238 uref_cnt_fb(cpi->upsampled_ref_bufs, &cpi->upsampled_ref_idx[arf_idx],
3239 new_uidx);
3240
3241 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3242 cpi->interp_filter_selected[0],
3243 sizeof(cpi->interp_filter_selected[0]));
3244 }
3245
3246 if (cpi->refresh_golden_frame) {
3247 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3248 cm->new_fb_idx);
3249 if (use_upsampled_ref)
3250 uref_cnt_fb(cpi->upsampled_ref_bufs,
3251 &cpi->upsampled_ref_idx[cpi->gld_fb_idx], new_uidx);
3252
Zoe Liuf0e46692016-10-12 12:31:43 -07003253#if !CONFIG_EXT_REFS
3254 if (!cpi->rc.is_src_frame_alt_ref)
3255#endif // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003256 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3257 cpi->interp_filter_selected[0],
3258 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003259 }
3260
3261#if CONFIG_EXT_REFS
3262 if (cpi->refresh_bwd_ref_frame) {
3263 if (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs) {
3264 // We have swapped the virtual indices to allow bwd_ref_frame to use
3265 // ALT0 as reference frame. We need to swap them back.
3266 // NOTE: The ALT_REFs' are indexed reversely, and ALT0 refers to the
3267 // farthest ALT_REF from the first frame in the gf group.
3268 int tmp = cpi->arf_map[0];
3269 cpi->arf_map[0] = cpi->alt_fb_idx;
3270 cpi->alt_fb_idx = cpi->bwd_fb_idx;
3271 cpi->bwd_fb_idx = tmp;
3272 }
Zoe Liuf0e46692016-10-12 12:31:43 -07003273
Yaowu Xuc27fc142016-08-22 16:08:15 -07003274 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3275 cm->new_fb_idx);
3276 if (use_upsampled_ref)
3277 uref_cnt_fb(cpi->upsampled_ref_bufs,
3278 &cpi->upsampled_ref_idx[cpi->bwd_fb_idx], new_uidx);
3279
3280 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3281 cpi->interp_filter_selected[0],
3282 sizeof(cpi->interp_filter_selected[0]));
3283 }
3284#endif // CONFIG_EXT_REFS
3285 }
3286
3287 if (cpi->refresh_last_frame) {
3288#if CONFIG_EXT_REFS
3289 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3290 // reference to the reference frame buffer virtual index; and then (2) from
3291 // the virtual index to the reference frame buffer physical index:
3292 //
3293 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3294 // | | |
3295 // v v v
3296 // lst_fb_idxes[0], ..., lst_fb_idxes[2], ..., alt_fb_idx
3297 // | | |
3298 // v v v
3299 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3300 //
3301 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3302 // have the other 2 LAST reference frames shifted as follows:
3303 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3304 // , and then have LAST_FRAME refreshed by the newly coded frame.
3305 //
3306 // To fulfill it, the decoder will be notified to execute following 2 steps:
3307 //
3308 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3309 // to point to the newly coded frame, i.e.
3310 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3311 //
3312 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3313 // original virtual index of LAST3_FRAME and have the other mappings
3314 // shifted as follows:
3315 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3316 // | | |
3317 // v v v
3318 // lst_fb_idxes[2], lst_fb_idxes[0], lst_fb_idxes[1]
3319 int ref_frame;
Zoe Liu5fca7242016-10-10 17:18:57 -07003320
Yaowu Xuc27fc142016-08-22 16:08:15 -07003321 if (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs) {
3322 // We have swapped the virtual indices to use ALT0 as BWD_REF
3323 // and we need to swap them back.
3324 int tmp = cpi->arf_map[0];
3325 cpi->arf_map[0] = cpi->alt_fb_idx;
3326 cpi->alt_fb_idx = cpi->bwd_fb_idx;
3327 cpi->bwd_fb_idx = tmp;
3328 }
Zoe Liu5fca7242016-10-10 17:18:57 -07003329
Yaowu Xuc27fc142016-08-22 16:08:15 -07003330 if (cm->frame_type == KEY_FRAME) {
3331 for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3332 ref_cnt_fb(pool->frame_bufs,
3333 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3334 cm->new_fb_idx);
3335
3336 if (use_upsampled_ref)
3337 uref_cnt_fb(cpi->upsampled_ref_bufs,
3338 &cpi->upsampled_ref_idx[cpi->lst_fb_idxes[ref_frame]],
3339 new_uidx);
3340 }
3341 } else {
3342 int tmp;
3343
3344 ref_cnt_fb(pool->frame_bufs,
3345 &cm->ref_frame_map[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3346 cm->new_fb_idx);
3347
3348 if (use_upsampled_ref)
3349 uref_cnt_fb(
3350 cpi->upsampled_ref_bufs,
3351 &cpi->upsampled_ref_idx[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3352 new_uidx);
3353
3354 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3355
3356 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003357 cpi->lst_fb_idxes[0] = tmp;
3358
Zoe Liuf0e46692016-10-12 12:31:43 -07003359 assert(cm->show_existing_frame == 0);
3360 // NOTE: Currently only LF_UPDATE and INTNL_OVERLAY_UPDATE frames are to
3361 // refresh the LAST_FRAME.
3362 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3363 cpi->interp_filter_selected[0],
3364 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003365 }
3366#else
3367 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
3368 cm->new_fb_idx);
3369 if (use_upsampled_ref)
3370 uref_cnt_fb(cpi->upsampled_ref_bufs,
3371 &cpi->upsampled_ref_idx[cpi->lst_fb_idx], new_uidx);
3372 if (!cpi->rc.is_src_frame_alt_ref) {
3373 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3374 cpi->interp_filter_selected[0],
3375 sizeof(cpi->interp_filter_selected[0]));
3376 }
3377#endif // CONFIG_EXT_REFS
3378 }
3379
3380#if DUMP_REF_FRAME_IMAGES == 1
3381 // Dump out all reference frame images.
3382 dump_ref_frame_images(cpi);
3383#endif // DUMP_REF_FRAME_IMAGES
3384}
3385
Yaowu Xuf883b422016-08-30 14:01:10 -07003386static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003387 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
3388 if (new_fb_ptr->mvs == NULL || new_fb_ptr->mi_rows < cm->mi_rows ||
3389 new_fb_ptr->mi_cols < cm->mi_cols) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003390 aom_free(new_fb_ptr->mvs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003391 CHECK_MEM_ERROR(cm, new_fb_ptr->mvs,
Yaowu Xuf883b422016-08-30 14:01:10 -07003392 (MV_REF *)aom_calloc(cm->mi_rows * cm->mi_cols,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003393 sizeof(*new_fb_ptr->mvs)));
3394 new_fb_ptr->mi_rows = cm->mi_rows;
3395 new_fb_ptr->mi_cols = cm->mi_cols;
3396 }
3397}
3398
Yaowu Xuf883b422016-08-30 14:01:10 -07003399void av1_scale_references(AV1_COMP *cpi) {
3400 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003401 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003402 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
3403 AOM_LAST_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003404#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003405 AOM_LAST2_FLAG,
3406 AOM_LAST3_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003407#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003408 AOM_GOLD_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003409#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003410 AOM_BWD_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003412 AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003413 };
3414
3415 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003416 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003417 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3418 BufferPool *const pool = cm->buffer_pool;
3419 const YV12_BUFFER_CONFIG *const ref =
3420 get_ref_frame_buffer(cpi, ref_frame);
3421
3422 if (ref == NULL) {
3423 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3424 continue;
3425 }
3426
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003427#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003428 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3429 RefCntBuffer *new_fb_ptr = NULL;
3430 int force_scaling = 0;
3431 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3432 if (new_fb == INVALID_IDX) {
3433 new_fb = get_free_fb(cm);
3434 force_scaling = 1;
3435 }
3436 if (new_fb == INVALID_IDX) return;
3437 new_fb_ptr = &pool->frame_bufs[new_fb];
3438 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3439 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003440 if (aom_realloc_frame_buffer(
3441 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3442 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3443 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003444 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003445 "Failed to allocate frame buffer");
3446 scale_and_extend_frame(ref, &new_fb_ptr->buf, MAX_MB_PLANE,
3447 (int)cm->bit_depth);
3448 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3449 alloc_frame_mvs(cm, new_fb);
3450 }
3451#else
3452 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3453 RefCntBuffer *new_fb_ptr = NULL;
3454 int force_scaling = 0;
3455 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3456 if (new_fb == INVALID_IDX) {
3457 new_fb = get_free_fb(cm);
3458 force_scaling = 1;
3459 }
3460 if (new_fb == INVALID_IDX) return;
3461 new_fb_ptr = &pool->frame_bufs[new_fb];
3462 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3463 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003464 if (aom_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003465 cm->subsampling_x, cm->subsampling_y,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003466 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3467 NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003468 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003469 "Failed to allocate frame buffer");
3470 scale_and_extend_frame(ref, &new_fb_ptr->buf, MAX_MB_PLANE);
3471 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3472 alloc_frame_mvs(cm, new_fb);
3473 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003474#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003475
3476 if (cpi->sf.use_upsampled_references &&
3477 (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3478 new_fb_ptr->buf.y_crop_height != cm->height)) {
3479 const int map_idx = get_ref_frame_map_idx(cpi, ref_frame);
3480 EncRefCntBuffer *ubuf =
3481 &cpi->upsampled_ref_bufs[cpi->upsampled_ref_idx[map_idx]];
3482
Yaowu Xuf883b422016-08-30 14:01:10 -07003483 if (aom_realloc_frame_buffer(&ubuf->buf, (cm->width << 3),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003484 (cm->height << 3), cm->subsampling_x,
3485 cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003486#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003487 cm->use_highbitdepth,
3488#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003489 (AOM_BORDER_IN_PIXELS << 3),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003490 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003491 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003492 "Failed to allocate up-sampled frame buffer");
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003493#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003494 scale_and_extend_frame(&new_fb_ptr->buf, &ubuf->buf, 1,
3495 (int)cm->bit_depth);
3496#else
3497 scale_and_extend_frame(&new_fb_ptr->buf, &ubuf->buf, 1);
3498#endif
3499 }
3500 } else {
3501 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3502 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3503 buf->buf.y_crop_width = ref->y_crop_width;
3504 buf->buf.y_crop_height = ref->y_crop_height;
3505 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3506 ++buf->ref_count;
3507 }
3508 } else {
3509 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3510 }
3511 }
3512}
3513
Yaowu Xuf883b422016-08-30 14:01:10 -07003514static void release_scaled_references(AV1_COMP *cpi) {
3515 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516 int i;
3517 if (cpi->oxcf.pass == 0) {
3518 // Only release scaled references under certain conditions:
3519 // if reference will be updated, or if scaled reference has same resolution.
3520 int refresh[INTER_REFS_PER_FRAME];
3521 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
3522#if CONFIG_EXT_REFS
3523 refresh[1] = refresh[2] = 0;
3524 refresh[3] = (cpi->refresh_golden_frame) ? 1 : 0;
3525 refresh[4] = (cpi->refresh_bwd_ref_frame) ? 1 : 0;
3526 refresh[5] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3527#else
3528 refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
3529 refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3530#endif // CONFIG_EXT_REFS
3531 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3532 const int idx = cpi->scaled_ref_idx[i - 1];
3533 RefCntBuffer *const buf =
3534 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3535 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
3536 if (buf != NULL &&
3537 (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
3538 buf->buf.y_crop_height == ref->y_crop_height))) {
3539 --buf->ref_count;
3540 cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
3541 }
3542 }
3543 } else {
3544 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) {
3545 const int idx = cpi->scaled_ref_idx[i];
3546 RefCntBuffer *const buf =
3547 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3548 if (buf != NULL) {
3549 --buf->ref_count;
3550 cpi->scaled_ref_idx[i] = INVALID_IDX;
3551 }
3552 }
3553 }
3554}
3555
3556static void full_to_model_count(unsigned int *model_count,
3557 unsigned int *full_count) {
3558 int n;
3559 model_count[ZERO_TOKEN] = full_count[ZERO_TOKEN];
3560 model_count[ONE_TOKEN] = full_count[ONE_TOKEN];
3561 model_count[TWO_TOKEN] = full_count[TWO_TOKEN];
3562 for (n = THREE_TOKEN; n < EOB_TOKEN; ++n)
3563 model_count[TWO_TOKEN] += full_count[n];
3564 model_count[EOB_MODEL_TOKEN] = full_count[EOB_TOKEN];
3565}
3566
Yaowu Xuf883b422016-08-30 14:01:10 -07003567void av1_full_to_model_counts(av1_coeff_count_model *model_count,
3568 av1_coeff_count *full_count) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569 int i, j, k, l;
3570
3571 for (i = 0; i < PLANE_TYPES; ++i)
3572 for (j = 0; j < REF_TYPES; ++j)
3573 for (k = 0; k < COEF_BANDS; ++k)
3574 for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
3575 full_to_model_count(model_count[i][j][k][l], full_count[i][j][k][l]);
3576}
3577
3578#if 0 && CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003579static void output_frame_level_debug_stats(AV1_COMP *cpi) {
3580 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003581 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
3582 int64_t recon_err;
3583
Yaowu Xuf883b422016-08-30 14:01:10 -07003584 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003585
Alex Conversef77fd0b2017-04-20 11:00:24 -07003586 recon_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003587
3588 if (cpi->twopass.total_left_stats.coded_error != 0.0)
Yunqing Wang8c1e57c2016-10-25 15:15:23 -07003589 fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
Yaowu Xuc27fc142016-08-22 16:08:15 -07003590 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
3591 "%10"PRId64" %10"PRId64" %10d "
3592 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
3593 "%6d %6d %5d %5d %5d "
3594 "%10"PRId64" %10.3lf"
3595 "%10lf %8u %10"PRId64" %10d %10d %10d\n",
3596 cpi->common.current_video_frame,
3597 cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003598 cpi->rc.source_alt_ref_pending,
3599 cpi->rc.source_alt_ref_active,
3600 cpi->rc.this_frame_target,
3601 cpi->rc.projected_frame_size,
3602 cpi->rc.projected_frame_size / cpi->common.MBs,
3603 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
3604 cpi->rc.vbr_bits_off_target,
3605 cpi->rc.vbr_bits_off_target_fast,
3606 cpi->twopass.extend_minq,
3607 cpi->twopass.extend_minq_fast,
3608 cpi->rc.total_target_vs_actual,
3609 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
3610 cpi->rc.total_actual_bits, cm->base_qindex,
Yaowu Xuf883b422016-08-30 14:01:10 -07003611 av1_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
3612 (double)av1_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
3613 av1_convert_qindex_to_q(cpi->twopass.active_worst_quality,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003614 cm->bit_depth),
3615 cpi->rc.avg_q,
Yaowu Xuf883b422016-08-30 14:01:10 -07003616 av1_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003617 cpi->refresh_last_frame, cpi->refresh_golden_frame,
3618 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
3619 cpi->twopass.bits_left,
3620 cpi->twopass.total_left_stats.coded_error,
3621 cpi->twopass.bits_left /
3622 (1 + cpi->twopass.total_left_stats.coded_error),
3623 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
3624 cpi->twopass.kf_zeromotion_pct,
3625 cpi->twopass.fr_content_type);
3626
3627 fclose(f);
3628
3629 if (0) {
3630 FILE *const fmodes = fopen("Modes.stt", "a");
3631 int i;
3632
3633 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
3634 cm->frame_type, cpi->refresh_golden_frame,
3635 cpi->refresh_alt_ref_frame);
3636
3637 for (i = 0; i < MAX_MODES; ++i)
3638 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
3639
3640 fprintf(fmodes, "\n");
3641
3642 fclose(fmodes);
3643 }
3644}
3645#endif
3646
Yaowu Xuf883b422016-08-30 14:01:10 -07003647static void set_mv_search_params(AV1_COMP *cpi) {
3648 const AV1_COMMON *const cm = &cpi->common;
3649 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003650
3651 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003652 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003653
3654 if (cpi->sf.mv.auto_mv_step_size) {
3655 if (frame_is_intra_only(cm)) {
3656 // Initialize max_mv_magnitude for use in the first INTER frame
3657 // after a key/intra-only frame.
3658 cpi->max_mv_magnitude = max_mv_def;
3659 } else {
3660 if (cm->show_frame) {
3661 // Allow mv_steps to correspond to twice the max mv magnitude found
3662 // in the previous frame, capped by the default max_mv_magnitude based
3663 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003664 cpi->mv_step_param = av1_init_search_range(
3665 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003666 }
3667 cpi->max_mv_magnitude = 0;
3668 }
3669 }
3670}
3671
Yaowu Xuf883b422016-08-30 14:01:10 -07003672static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003673#if CONFIG_GLOBAL_MOTION
3674 int i;
3675 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Sarah Parker4c10a3c2017-04-10 19:37:59 -07003676 set_default_warp_params(&cpi->common.global_motion[i]);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003677 }
3678 cpi->global_motion_search_done = 0;
3679#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuf883b422016-08-30 14:01:10 -07003680 av1_set_speed_features_framesize_independent(cpi);
3681 av1_set_rd_speed_thresholds(cpi);
3682 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003683 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07003684#if CONFIG_EXT_INTER
3685 if (!frame_is_intra_only(&cpi->common)) set_compound_tools(&cpi->common);
3686#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07003687}
3688
Yaowu Xuf883b422016-08-30 14:01:10 -07003689static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003690 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003691 AV1_COMMON *const cm = &cpi->common;
3692 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003693
3694 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003695 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003696
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00003697// Decide q and q bounds.
3698#if CONFIG_XIPHRC
3699 int frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
3700 *q = od_enc_rc_select_quantizers_and_lambdas(
3701 &cpi->od_rc, cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
3702 frame_type, bottom_index, top_index);
3703#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003704 *q = av1_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00003705#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003706
3707 if (!frame_is_intra_only(cm)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003708 av1_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003709 }
3710
3711 // Configure experimental use of segmentation for enhanced coding of
3712 // static regions if indicated.
3713 // Only allowed in the second pass of a two pass encode, as it requires
3714 // lagged coding, and if the relevant speed feature flag is set.
3715 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3716 configure_static_seg_features(cpi);
3717}
3718
Yaowu Xuf883b422016-08-30 14:01:10 -07003719static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003720 int y_stride = cpi->scaled_source.y_stride;
3721
3722 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003723 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003724 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003725 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003726 }
3727}
3728
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003729#if CONFIG_LOOP_RESTORATION
3730static void set_restoration_tilesize(int width, int height,
3731 RestorationInfo *rst) {
3732 (void)width;
3733 (void)height;
3734 rst[0].restoration_tilesize = (RESTORATION_TILESIZE_MAX >> 1);
3735 rst[1].restoration_tilesize = rst[0].restoration_tilesize;
3736 rst[2].restoration_tilesize = rst[0].restoration_tilesize;
3737}
3738#endif // CONFIG_LOOP_RESTORATION
3739
Fergus Simpsonbc189932017-05-16 17:02:39 -07003740static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003741 AV1_COMMON *const cm = &cpi->common;
Fergus Simpsonbc189932017-05-16 17:02:39 -07003742 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003743 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003744
Fergus Simpsonbc189932017-05-16 17:02:39 -07003745 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003746 // There has been a change in the encoded frame size
Fergus Simpsonbc189932017-05-16 17:02:39 -07003747 av1_set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003748 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003749 }
3750
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00003751#if !CONFIG_XIPHRC
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003752 if (cpi->oxcf.pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003753 av1_set_target_rate(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003754 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00003755#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003756
3757 alloc_frame_mvs(cm, cm->new_fb_idx);
3758
3759 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07003760 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003761 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003762#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003763 cm->use_highbitdepth,
3764#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003765 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
3766 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003767 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003768 "Failed to allocate frame buffer");
3769
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003770#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003771 set_restoration_tilesize(
3772#if CONFIG_FRAME_SUPERRES
3773 cm->superres_upscaled_width, cm->superres_upscaled_height,
3774#else
3775 cm->width, cm->height,
3776#endif // CONFIG_FRAME_SUPERRES
3777 cm->rst_info);
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003778 for (int i = 0; i < MAX_MB_PLANE; ++i)
3779 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
3780 av1_alloc_restoration_buffers(cm);
3781 for (int i = 0; i < MAX_MB_PLANE; ++i) {
3782 cpi->rst_search[i].restoration_tilesize =
3783 cm->rst_info[i].restoration_tilesize;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003784 av1_alloc_restoration_struct(cm, &cpi->rst_search[i],
3785#if CONFIG_FRAME_SUPERRES
3786 cm->superres_upscaled_width,
3787 cm->superres_upscaled_height);
3788#else
3789 cm->width, cm->height);
3790#endif // CONFIG_FRAME_SUPERRES
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003791 }
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003792#endif // CONFIG_LOOP_RESTORATION
3793 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003794 init_motion_estimation(cpi);
3795
3796 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3797 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3798 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3799
3800 ref_buf->idx = buf_idx;
3801
3802 if (buf_idx != INVALID_IDX) {
3803 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3804 ref_buf->buf = buf;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003805#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07003806 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003807 &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
3808 cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
3809#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003810 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3811 buf->y_crop_height, cm->width,
3812 cm->height);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003813#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07003814 if (av1_is_scaled(&ref_buf->sf)) aom_extend_frame_borders(buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003815 } else {
3816 ref_buf->buf = NULL;
3817 }
3818 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003819
3820#if CONFIG_VAR_REFS
3821 // Check duplicate reference frames
3822 enc_check_valid_ref_frames(cpi);
3823#endif // CONFIG_VAR_REFS
3824
Alex Conversee816b312017-05-01 09:51:24 -07003825#if CONFIG_INTRABC
3826#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan1dcb7072017-05-12 11:13:05 +02003827 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
3828 cm->width, cm->height,
3829 cm->use_highbitdepth);
Alex Conversee816b312017-05-01 09:51:24 -07003830#else
3831 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
3832 cm->width, cm->height);
3833#endif // CONFIG_HIGHBITDEPTH
3834#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07003835
3836 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3837}
3838
Fergus Simpsonbc189932017-05-16 17:02:39 -07003839static void setup_frame_size(AV1_COMP *cpi) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003840 int encode_width;
3841 int encode_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003842
3843 av1_calculate_next_scaled_size(cpi, &encode_width, &encode_height);
3844
3845#if CONFIG_FRAME_SUPERRES
3846 AV1_COMMON *cm = &cpi->common;
3847 cm->superres_upscaled_width = encode_width;
3848 cm->superres_upscaled_height = encode_height;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003849 cpi->common.superres_scale_numerator =
3850 av1_calculate_next_superres_scale(cpi, encode_width, encode_width);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003851 av1_calculate_superres_size(cm, &encode_width, &encode_height);
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003852// printf("Resize/superres %d x %d -> %d x %d\n", encode_width, encode_height,
3853// cm->superres_upscaled_width, cm->superres_upscaled_height);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003854#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003855
3856 set_frame_size(cpi, encode_width, encode_height);
3857}
3858
3859#if CONFIG_FRAME_SUPERRES
3860static void superres_post_encode(AV1_COMP *cpi) {
3861 AV1_COMMON *cm = &cpi->common;
3862
3863 if (av1_superres_unscaled(cm)) return;
3864
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003865 av1_superres_upscale(cm, NULL);
3866
3867 // If regular resizing is occurring the source will need to be downscaled to
3868 // match the upscaled superres resolution. Otherwise the original source is
3869 // used.
3870 if (av1_resize_unscaled(cm)) {
3871 cpi->source = cpi->unscaled_source;
3872 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
3873 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07003874 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
3875 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003876 // Do downscale. cm->(width|height) has been updated by av1_superres_upscale
3877 if (aom_realloc_frame_buffer(
3878 &cpi->scaled_source, cm->superres_upscaled_width,
3879 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
3880#if CONFIG_HIGHBITDEPTH
3881 cm->use_highbitdepth,
3882#endif // CONFIG_HIGHBITDEPTH
3883 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
3884 aom_internal_error(
3885 &cm->error, AOM_CODEC_MEM_ERROR,
3886 "Failed to reallocate scaled source buffer for superres");
3887 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
3888 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
3889#if CONFIG_HIGHBITDEPTH
3890 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
3891 (int)cm->bit_depth);
3892#else
3893 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source);
3894#endif // CONFIG_HIGHBITDEPTH
3895 cpi->source = &cpi->scaled_source;
3896 }
3897}
3898#endif // CONFIG_FRAME_SUPERRES
3899
3900static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
3901 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
3902 struct loopfilter *lf = &cm->lf;
3903 if (is_lossless_requested(&cpi->oxcf)) {
3904 lf->filter_level = 0;
3905 } else {
3906 struct aom_usec_timer timer;
3907
3908 aom_clear_system_state();
3909
3910 aom_usec_timer_start(&timer);
3911
3912 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
3913
3914 aom_usec_timer_mark(&timer);
3915 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
3916 }
3917
3918 if (lf->filter_level > 0) {
3919#if CONFIG_VAR_TX || CONFIG_EXT_PARTITION || CONFIG_CB4X4
3920 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
3921#else
3922 if (cpi->num_workers > 1)
3923 av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
3924 lf->filter_level, 0, 0, cpi->workers,
3925 cpi->num_workers, &cpi->lf_row_sync);
3926 else
3927 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
3928#endif
3929 }
3930#if CONFIG_CDEF
3931 if (is_lossless_requested(&cpi->oxcf)) {
3932 cm->cdef_bits = 0;
3933 cm->cdef_strengths[0] = 0;
3934 cm->nb_cdef_strengths = 1;
3935 } else {
3936 // Find cm->dering_level, cm->clpf_strength_u and cm->clpf_strength_v
3937 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
3938 cpi->oxcf.speed > 0);
3939
3940 // Apply the filter
3941 av1_cdef_frame(cm->frame_to_show, cm, xd);
3942 }
3943#endif
3944
3945#if CONFIG_FRAME_SUPERRES
3946 superres_post_encode(cpi);
3947#endif // CONFIG_FRAME_SUPERRES
3948
3949#if CONFIG_LOOP_RESTORATION
3950 av1_pick_filter_restoration(cpi->source, cpi, cpi->sf.lpf_pick);
3951 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
3952 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
3953 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
3954 av1_loop_restoration_frame(cm->frame_to_show, cm, cm->rst_info, 7, 0, NULL);
3955 }
3956#endif // CONFIG_LOOP_RESTORATION
3957 // TODO(debargha): Fix mv search range on encoder side
3958 // aom_extend_frame_inner_borders(cm->frame_to_show);
3959 aom_extend_frame_borders(cm->frame_to_show);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003960}
3961
Yaowu Xuf883b422016-08-30 14:01:10 -07003962static void reset_use_upsampled_references(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003963 MV_REFERENCE_FRAME ref_frame;
3964
3965 // reset up-sampled reference buffer structure.
3966 init_upsampled_ref_frame_bufs(cpi);
3967
3968 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3969 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, ref_frame);
3970 int new_uidx = upsample_ref_frame(cpi, ref);
3971
3972 // Update the up-sampled reference index.
3973 cpi->upsampled_ref_idx[get_ref_frame_map_idx(cpi, ref_frame)] = new_uidx;
3974 cpi->upsampled_ref_bufs[new_uidx].ref_count++;
3975 }
3976}
3977
Yaowu Xuf883b422016-08-30 14:01:10 -07003978static void encode_without_recode_loop(AV1_COMP *cpi) {
3979 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003980 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
3981 const int use_upsampled_ref = cpi->sf.use_upsampled_references;
3982
Yaowu Xuf883b422016-08-30 14:01:10 -07003983 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003984
Fergus Simpsonbc189932017-05-16 17:02:39 -07003985 setup_frame_size(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003986 assert(cm->width == cpi->scaled_source.y_crop_width);
3987 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07003988
Yaowu Xuc27fc142016-08-22 16:08:15 -07003989 // For 1 pass CBR under dynamic resize mode: use faster scaling for source.
3990 // Only for 2x2 scaling for now.
Yaowu Xuf883b422016-08-30 14:01:10 -07003991 if (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07003992 cpi->oxcf.resize_mode == RESIZE_DYNAMIC &&
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003993 cpi->unscaled_source->y_width == (cm->width << 1) &&
3994 cpi->unscaled_source->y_height == (cm->height << 1)) {
3995 cpi->source = av1_scale_if_required_fast(cm, cpi->unscaled_source,
Yaowu Xuf883b422016-08-30 14:01:10 -07003996 &cpi->scaled_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003997 if (cpi->unscaled_last_source != NULL)
Alex Conversef77fd0b2017-04-20 11:00:24 -07003998 cpi->last_source = av1_scale_if_required_fast(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003999 cm, cpi->unscaled_last_source, &cpi->scaled_last_source);
4000 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004001 cpi->source =
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004002 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004003 if (cpi->unscaled_last_source != NULL)
Alex Conversef77fd0b2017-04-20 11:00:24 -07004004 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
Yaowu Xuf883b422016-08-30 14:01:10 -07004005 &cpi->scaled_last_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004006 }
4007
4008 if (frame_is_intra_only(cm) == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004009 av1_scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004010 }
4011
4012 set_size_independent_vars(cpi);
4013 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4014
4015 // cpi->sf.use_upsampled_references can be different from frame to frame.
4016 // Every time when cpi->sf.use_upsampled_references is changed from 0 to 1.
4017 // The reference frames for this frame have to be up-sampled before encoding.
Yunqing Wangfe1e58e2016-12-12 14:14:41 -08004018 if (!use_upsampled_ref && cpi->sf.use_upsampled_references &&
4019 cm->frame_type != KEY_FRAME)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004020 reset_use_upsampled_references(cpi);
4021
Yaowu Xuf883b422016-08-30 14:01:10 -07004022 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004023 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004024 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004025
Yaowu Xuc27fc142016-08-22 16:08:15 -07004026 // Variance adaptive and in frame q adjustment experiments are mutually
4027 // exclusive.
4028 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004029 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004030 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004031 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004032 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004033 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004034 }
4035 apply_active_map(cpi);
4036
4037 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004038 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004039
4040 // Update some stats from cyclic refresh, and check if we should not update
4041 // golden reference, for 1 pass CBR.
4042 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004043 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4044 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004045
4046 // Update the skip mb flag probabilities based on the distribution
4047 // seen in the last encoder iteration.
4048 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004049 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004050}
4051
Yaowu Xuf883b422016-08-30 14:01:10 -07004052static void encode_with_recode_loop(AV1_COMP *cpi, size_t *size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004053 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004054 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004055 RATE_CONTROL *const rc = &cpi->rc;
4056 int bottom_index, top_index;
4057 int loop_count = 0;
4058 int loop_at_this_size = 0;
4059 int loop = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004060#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004061 int overshoot_seen = 0;
4062 int undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004063#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004064 int frame_over_shoot_limit;
4065 int frame_under_shoot_limit;
4066 int q = 0, q_low = 0, q_high = 0;
4067 const int use_upsampled_ref = cpi->sf.use_upsampled_references;
4068
4069 set_size_independent_vars(cpi);
4070
Yaowu Xuc27fc142016-08-22 16:08:15 -07004071 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004072 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004073
Fergus Simpsonbc189932017-05-16 17:02:39 -07004074 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004075
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004076 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004077 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4078
Thomas Daede919bd6a2016-10-24 16:31:05 -07004079 // cpi->sf.use_upsampled_references can be different from frame to frame.
4080 // Every time when cpi->sf.use_upsampled_references is changed from 0 to
4081 // 1.
4082 // The reference frames for this frame have to be up-sampled before
4083 // encoding.
Yunqing Wangfe1e58e2016-12-12 14:14:41 -08004084 if (!use_upsampled_ref && cpi->sf.use_upsampled_references &&
4085 cm->frame_type != KEY_FRAME)
Thomas Daede919bd6a2016-10-24 16:31:05 -07004086 reset_use_upsampled_references(cpi);
4087
Yaowu Xuc27fc142016-08-22 16:08:15 -07004088 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4089 set_mv_search_params(cpi);
4090
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004091#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004092 // Reset the loop state for new frame size.
4093 overshoot_seen = 0;
4094 undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004095#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004096
Yaowu Xuc27fc142016-08-22 16:08:15 -07004097 q_low = bottom_index;
4098 q_high = top_index;
4099
4100 loop_at_this_size = 0;
4101 }
4102
4103 // Decide frame size bounds first time through.
4104 if (loop_count == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004105 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4106 &frame_under_shoot_limit,
4107 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004108 }
4109
Alex Conversef77fd0b2017-04-20 11:00:24 -07004110 cpi->source =
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004111 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004112 if (cpi->unscaled_last_source != NULL)
Alex Conversef77fd0b2017-04-20 11:00:24 -07004113 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
Yaowu Xuf883b422016-08-30 14:01:10 -07004114 &cpi->scaled_last_source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004115
4116 if (frame_is_intra_only(cm) == 0) {
4117 if (loop_count > 0) {
4118 release_scaled_references(cpi);
4119 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004120 av1_scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004121 }
4122
Yaowu Xuf883b422016-08-30 14:01:10 -07004123 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004124
4125 if (loop_count == 0) setup_frame(cpi);
4126
hui su0d103572017-03-01 17:58:01 -08004127#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004128 // Base q-index may have changed, so we need to assign proper default coef
4129 // probs before every iteration.
4130 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
4131 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07004132 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004133 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4134 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4135 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4136 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
4137 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
4138 }
4139 }
hui su0d103572017-03-01 17:58:01 -08004140#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004141
Yaowu Xuc27fc142016-08-22 16:08:15 -07004142 // Variance adaptive and in frame q adjustment experiments are mutually
4143 // exclusive.
4144 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004145 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004146 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004147 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004148 }
4149
4150 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004151 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004152
4153 // Update the skip mb flag probabilities based on the distribution
4154 // seen in the last encoder iteration.
4155 // update_base_skip_probs(cpi);
4156
Yaowu Xuf883b422016-08-30 14:01:10 -07004157 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004158
4159 // Dummy pack of the bitstream using up to date stats to get an
4160 // accurate estimate of output frame size to determine if we need
4161 // to recode.
4162 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
4163 save_coding_context(cpi);
4164
Yaowu Xuf883b422016-08-30 14:01:10 -07004165 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004166
4167 rc->projected_frame_size = (int)(*size) << 3;
4168 restore_coding_context(cpi);
4169
4170 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4171 }
4172
Yaowu Xuf883b422016-08-30 14:01:10 -07004173 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004174 loop = 0;
4175 } else {
4176 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4177 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4178 int last_q = q;
4179 int64_t kf_err;
4180
4181 int64_t high_err_target = cpi->ambient_err;
4182 int64_t low_err_target = cpi->ambient_err >> 1;
4183
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004184#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004185 if (cm->use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004186 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004187 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004188 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004189 }
4190#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07004191 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004192#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004193
4194 // Prevent possible divide by zero error below for perfect KF
4195 kf_err += !kf_err;
4196
4197 // The key frame is not good enough or we can afford
4198 // to make it better without undue risk of popping.
4199 if ((kf_err > high_err_target &&
4200 rc->projected_frame_size <= frame_over_shoot_limit) ||
4201 (kf_err > low_err_target &&
4202 rc->projected_frame_size <= frame_under_shoot_limit)) {
4203 // Lower q_high
4204 q_high = q > q_low ? q - 1 : q_low;
4205
4206 // Adjust Q
4207 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004208 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004209 } else if (kf_err < low_err_target &&
4210 rc->projected_frame_size >= frame_under_shoot_limit) {
4211 // The key frame is much better than the previous frame
4212 // Raise q_low
4213 q_low = q < q_high ? q + 1 : q_high;
4214
4215 // Adjust Q
4216 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004217 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004218 }
4219
4220 // Clamp Q to upper and lower limits:
4221 q = clamp(q, q_low, q_high);
4222
4223 loop = q != last_q;
4224 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4225 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004226 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004227 // Is the projected frame size out of range and are we allowed
4228 // to attempt to recode.
4229 int last_q = q;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004230#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004231 int retries = 0;
4232
Yaowu Xuc27fc142016-08-22 16:08:15 -07004233 // Frame size out of permitted range:
4234 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004235 // Frame is too large
4236 if (rc->projected_frame_size > rc->this_frame_target) {
4237 // Special case if the projected size is > the max allowed.
4238 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4239 q_high = rc->worst_quality;
4240
4241 // Raise Qlow as to at least the current value
4242 q_low = q < q_high ? q + 1 : q_high;
4243
4244 if (undershoot_seen || loop_at_this_size > 1) {
4245 // Update rate_correction_factor unless
Yaowu Xuf883b422016-08-30 14:01:10 -07004246 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004247
4248 q = (q_high + q_low + 1) / 2;
4249 } else {
4250 // Update rate_correction_factor unless
Yaowu Xuf883b422016-08-30 14:01:10 -07004251 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004252
Yaowu Xuf883b422016-08-30 14:01:10 -07004253 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4254 AOMMAX(q_high, top_index));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004255
4256 while (q < q_low && retries < 10) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004257 av1_rc_update_rate_correction_factors(cpi);
4258 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4259 AOMMAX(q_high, top_index));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004260 retries++;
4261 }
4262 }
4263
4264 overshoot_seen = 1;
4265 } else {
4266 // Frame is too small
4267 q_high = q > q_low ? q - 1 : q_low;
4268
4269 if (overshoot_seen || loop_at_this_size > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004270 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004271 q = (q_high + q_low) / 2;
4272 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004273 av1_rc_update_rate_correction_factors(cpi);
4274 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4275 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004276 // Special case reset for qlow for constrained quality.
4277 // This should only trigger where there is very substantial
4278 // undershoot on a frame and the auto cq level is above
4279 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004280 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004281 q_low = q;
4282 }
4283
4284 while (q > q_high && retries < 10) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004285 av1_rc_update_rate_correction_factors(cpi);
4286 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4287 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004288 retries++;
4289 }
4290 }
4291
4292 undershoot_seen = 1;
4293 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004294#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004295
4296 // Clamp Q to upper and lower limits:
4297 q = clamp(q, q_low, q_high);
4298
4299 loop = (q != last_q);
4300 } else {
4301 loop = 0;
4302 }
4303 }
4304
4305 // Special case for overlay frame.
4306 if (rc->is_src_frame_alt_ref &&
4307 rc->projected_frame_size < rc->max_frame_bandwidth)
4308 loop = 0;
4309
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004310#if CONFIG_GLOBAL_MOTION
4311 if (recode_loop_test_global_motion(cpi)) {
4312 loop = 1;
4313 }
4314#endif // CONFIG_GLOBAL_MOTION
4315
Yaowu Xuc27fc142016-08-22 16:08:15 -07004316 if (loop) {
4317 ++loop_count;
4318 ++loop_at_this_size;
4319
4320#if CONFIG_INTERNAL_STATS
4321 ++cpi->tot_recode_hits;
4322#endif
4323 }
4324 } while (loop);
4325}
4326
Yaowu Xuf883b422016-08-30 14:01:10 -07004327static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004328 const int *const map = cpi->common.ref_frame_map;
4329
4330#if CONFIG_EXT_REFS
4331 const int last2_is_last =
4332 map[cpi->lst_fb_idxes[1]] == map[cpi->lst_fb_idxes[0]];
4333 const int last3_is_last =
4334 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[0]];
4335 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[0]];
Arild Fuldseth (arilfuld)78bfc282017-05-24 12:06:35 +02004336#if CONFIG_ONE_SIDED_COMPOUND
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02004337 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
4338 const int last3_is_last2 =
4339 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
4340 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
4341 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
4342#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004343 const int bwd_is_last = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[0]];
4344 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
4345
4346 const int last3_is_last2 =
4347 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
4348 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
4349 const int bwd_is_last2 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[1]];
4350
4351 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
4352 const int bwd_is_last3 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[2]];
4353
4354 const int bwd_is_gld = map[cpi->bwd_fb_idx] == map[cpi->gld_fb_idx];
4355
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02004356#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004357 const int last2_is_alt = map[cpi->lst_fb_idxes[1]] == map[cpi->alt_fb_idx];
4358 const int last3_is_alt = map[cpi->lst_fb_idxes[2]] == map[cpi->alt_fb_idx];
4359 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4360 const int bwd_is_alt = map[cpi->bwd_fb_idx] == map[cpi->alt_fb_idx];
4361#else
4362 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
4363 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4364 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
4365#endif // CONFIG_EXT_REFS
4366
Yaowu Xuf883b422016-08-30 14:01:10 -07004367 int flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004368
4369#if CONFIG_EXT_REFS
4370 // Disable the use of BWDREF_FRAME for non-bipredictive frames.
4371 if (!(cpi->rc.is_bipred_frame || cpi->rc.is_last_bipred_frame ||
4372 (cpi->rc.is_bwd_ref_frame && cpi->num_extra_arfs)))
Yaowu Xuf883b422016-08-30 14:01:10 -07004373 flags &= ~AOM_BWD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004374#endif // CONFIG_EXT_REFS
4375
Yaowu Xuf883b422016-08-30 14:01:10 -07004376 if (gld_is_last || gld_is_alt) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004377
Yaowu Xuf883b422016-08-30 14:01:10 -07004378 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004379
Yaowu Xuf883b422016-08-30 14:01:10 -07004380 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004381
4382#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07004383 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004384
Yaowu Xuf883b422016-08-30 14:01:10 -07004385 if (last3_is_last || last3_is_last2 || last3_is_alt) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004386
Yaowu Xuf883b422016-08-30 14:01:10 -07004387 if (gld_is_last2 || gld_is_last3) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004388
Arild Fuldseth (arilfuld)78bfc282017-05-24 12:06:35 +02004389#if CONFIG_ONE_SIDED_COMPOUND // Changes LL & HL bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02004390 /* Allow biprediction between two identical frames (e.g. bwd_is_last = 1) */
4391 if (bwd_is_alt && (flags & AOM_BWD_FLAG)) flags &= ~AOM_BWD_FLAG;
4392#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004393 if ((bwd_is_last || bwd_is_last2 || bwd_is_last3 || bwd_is_gld ||
4394 bwd_is_alt) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004395 (flags & AOM_BWD_FLAG))
4396 flags &= ~AOM_BWD_FLAG;
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02004397#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004398#endif // CONFIG_EXT_REFS
4399
4400 return flags;
4401}
4402
Yaowu Xuf883b422016-08-30 14:01:10 -07004403static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004404 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07004405 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07004406 // Note: The overrides are valid only for the next frame passed
4407 // to encode_frame_to_data_rate() function
4408 if (cpi->ext_refresh_frame_context_pending) {
4409 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4410 cpi->ext_refresh_frame_context_pending = 0;
4411 }
4412 if (cpi->ext_refresh_frame_flags_pending) {
4413 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4414 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4415 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
4416 cpi->ext_refresh_frame_flags_pending = 0;
4417 }
4418}
4419
Yaowu Xuf883b422016-08-30 14:01:10 -07004420static void set_arf_sign_bias(AV1_COMP *cpi) {
4421 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004422 int arf_sign_bias;
4423#if CONFIG_EXT_REFS
4424 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4425 // The arf_sign_bias will be one for internal ARFs'
4426 arf_sign_bias = cpi->rc.source_alt_ref_active &&
4427 (!cpi->refresh_alt_ref_frame ||
4428 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
4429#else
4430 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
4431 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4432 arf_sign_bias = cpi->rc.source_alt_ref_active &&
4433 (!cpi->refresh_alt_ref_frame ||
4434 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
4435 } else {
4436 arf_sign_bias =
4437 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
4438 }
Zoe Liu6cfaff92016-10-18 17:12:11 -07004439#endif // CONFIG_EXT_REFS
4440
Yaowu Xuc27fc142016-08-22 16:08:15 -07004441 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
4442#if CONFIG_EXT_REFS
4443 cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
4444#endif // CONFIG_EXT_REFS
4445}
4446
Yaowu Xuf883b422016-08-30 14:01:10 -07004447static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07004448 InterpFilter ifilter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004449 int ref_total[TOTAL_REFS_PER_FRAME] = { 0 };
4450 MV_REFERENCE_FRAME ref;
4451 int mask = 0;
4452 int arf_idx = ALTREF_FRAME;
4453
4454#if CONFIG_EXT_REFS
4455 // Get which arf used as ALTREF_FRAME
4456 if (cpi->oxcf.pass == 2)
4457 arf_idx += cpi->twopass.gf_group.arf_ref_idx[cpi->twopass.gf_group.index];
Zoe Liu6cfaff92016-10-18 17:12:11 -07004458#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004459
4460 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4461 return mask;
4462
4463#if CONFIG_EXT_REFS
4464 for (ref = LAST_FRAME; ref < ALTREF_FRAME; ++ref)
4465 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4466 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
4467
4468 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4469 ref_total[ref] += cpi->interp_filter_selected[arf_idx][ifilter];
4470#else
4471 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4472 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4473 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Zoe Liu6cfaff92016-10-18 17:12:11 -07004474#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004475
4476 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
4477 if ((ref_total[LAST_FRAME] &&
4478 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
4479#if CONFIG_EXT_REFS
4480 (ref_total[LAST2_FRAME] == 0 ||
4481 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
4482 ref_total[LAST2_FRAME]) &&
4483 (ref_total[LAST3_FRAME] == 0 ||
4484 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
4485 ref_total[LAST3_FRAME]) &&
4486#endif // CONFIG_EXT_REFS
4487 (ref_total[GOLDEN_FRAME] == 0 ||
4488 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4489 ref_total[GOLDEN_FRAME]) &&
4490#if CONFIG_EXT_REFS
4491 (ref_total[BWDREF_FRAME] == 0 ||
4492 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
4493 ref_total[BWDREF_FRAME]) &&
4494#endif // CONFIG_EXT_REFS
4495 (ref_total[ALTREF_FRAME] == 0 ||
4496 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
4497 ref_total[ALTREF_FRAME]))
4498 mask |= 1 << ifilter;
4499 }
4500 return mask;
4501}
4502
4503#define DUMP_RECON_FRAMES 0
4504
4505#if DUMP_RECON_FRAMES == 1
4506// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004507static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4508 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004509 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
4510 int h;
4511 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4512 FILE *f_recon = NULL;
4513
4514 if (recon_buf == NULL || !cm->show_frame) {
4515 printf("Frame %d is not ready or no show to dump.\n",
4516 cm->current_video_frame);
4517 return;
4518 }
4519
4520 if (cm->current_video_frame == 0) {
4521 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4522 printf("Unable to open file %s to write.\n", file_name);
4523 return;
4524 }
4525 } else {
4526 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4527 printf("Unable to open file %s to append.\n", file_name);
4528 return;
4529 }
4530 }
4531 printf(
4532 "\nFrame=%5d, encode_update_type[%5d]=%1d, show_existing_frame=%d, "
4533 "y_stride=%4d, uv_stride=%4d, width=%4d, height=%4d\n",
4534 cm->current_video_frame, cpi->twopass.gf_group.index,
4535 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
4536 cm->show_existing_frame, recon_buf->y_stride, recon_buf->uv_stride,
4537 cm->width, cm->height);
4538
4539 // --- Y ---
4540 for (h = 0; h < cm->height; ++h) {
4541 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4542 f_recon);
4543 }
4544 // --- U ---
4545 for (h = 0; h < (cm->height >> 1); ++h) {
4546 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4547 f_recon);
4548 }
4549 // --- V ---
4550 for (h = 0; h < (cm->height >> 1); ++h) {
4551 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4552 f_recon);
4553 }
4554
4555 fclose(f_recon);
4556}
4557#endif // DUMP_RECON_FRAMES
4558
Thomas Davies028b57f2017-02-22 16:42:11 +00004559#if CONFIG_EC_ADAPT
4560
4561static void make_update_tile_list_enc(AV1_COMP *cpi, const int tile_rows,
4562 const int tile_cols,
4563 FRAME_CONTEXT *ec_ctxs[]) {
4564 int i;
4565 for (i = 0; i < tile_rows * tile_cols; ++i)
4566 ec_ctxs[i] = &cpi->tile_data[i].tctx;
4567}
4568
4569#endif
Yaowu Xuf883b422016-08-30 14:01:10 -07004570static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004571 uint8_t *dest, int skip_adapt,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004572 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004573 AV1_COMMON *const cm = &cpi->common;
4574 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004575 struct segmentation *const seg = &cm->seg;
4576 TX_SIZE t;
Thomas Davies028b57f2017-02-22 16:42:11 +00004577#if CONFIG_EC_ADAPT
Thomas Davies493623e2017-03-31 16:12:25 +01004578 FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
4579 sizeof(&cpi->tile_data[0].tctx));
4580 aom_cdf_prob **cdf_ptrs =
4581 aom_malloc(cm->tile_rows * cm->tile_cols *
4582 sizeof(&cpi->tile_data[0].tctx.partition_cdf[0][0]));
Thomas Davies028b57f2017-02-22 16:42:11 +00004583#endif
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004584#if CONFIG_XIPHRC
4585 int frame_type;
4586 int drop_this_frame = 0;
Zoe Liue04abf72017-04-19 15:37:11 -07004587#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004588 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004589 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004590
4591 // Set the arf sign bias for this frame.
4592 set_arf_sign_bias(cpi);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004593#if CONFIG_TEMPMV_SIGNALING
4594 // frame type has been decided outside of this function call
4595 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4596 cm->use_prev_frame_mvs =
4597 !cpi->oxcf.disable_tempmv && !cm->cur_frame->intra_only;
4598#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004599
4600#if CONFIG_EXT_REFS
4601 // NOTE:
4602 // (1) Move the setup of the ref_frame_flags upfront as it would be
4603 // determined by the current frame properties;
4604 // (2) The setup of the ref_frame_flags applies to both show_existing_frame's
4605 // and the other cases.
4606 if (cm->current_video_frame > 0)
4607 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4608
4609 if (cm->show_existing_frame) {
4610 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
4611 // BWDREF_FRAME in the reference frame buffer.
4612 cm->frame_type = INTER_FRAME;
4613 cm->show_frame = 1;
4614 cpi->frame_flags = *frame_flags;
4615
4616 // In the case of show_existing frame, we will not send fresh flag
4617 // to decoder. Any change in the reference frame buffer can be done by
4618 // switching the virtual indices.
4619
4620 cpi->refresh_last_frame = 0;
4621 cpi->refresh_golden_frame = 0;
4622 cpi->refresh_bwd_ref_frame = 0;
4623 cpi->refresh_alt_ref_frame = 0;
4624
4625 cpi->rc.is_bwd_ref_frame = 0;
4626 cpi->rc.is_last_bipred_frame = 0;
4627 cpi->rc.is_bipred_frame = 0;
4628
4629 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07004630 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004631
4632 // Set up frame to show to get ready for stats collection.
4633 cm->frame_to_show = get_frame_new_buffer(cm);
4634
4635#if DUMP_RECON_FRAMES == 1
4636 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4637 dump_filtered_recon_frames(cpi);
4638#endif // DUMP_RECON_FRAMES
4639
4640 // Update the LAST_FRAME in the reference frame buffer.
Yaowu Xuf883b422016-08-30 14:01:10 -07004641 av1_update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004642
4643 // Update frame flags
4644 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4645 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4646 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4647
4648 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4649
4650 // Update the frame type
4651 cm->last_frame_type = cm->frame_type;
4652
Yaowu Xuc27fc142016-08-22 16:08:15 -07004653 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4654 // to do post-encoding update accordingly.
4655 if (cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004656 av1_set_target_rate(cpi);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004657#if CONFIG_XIPHRC
Zoe Liue04abf72017-04-19 15:37:11 -07004658 frame_type = cm->frame_type == INTER_FRAME ? OD_P_FRAME : OD_I_FRAME;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004659 drop_this_frame = od_enc_rc_update_state(
4660 &cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
4661 cpi->refresh_alt_ref_frame, frame_type, cpi->droppable);
4662#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004663 av1_rc_postencode_update(cpi, *size);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004664#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004665 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004666
4667 cm->last_width = cm->width;
4668 cm->last_height = cm->height;
4669
4670 ++cm->current_video_frame;
4671
Jingning Hanf6214b92017-04-12 11:43:37 -07004672#if CONFIG_EC_ADAPT
4673 aom_free(tile_ctxs);
4674 aom_free(cdf_ptrs);
4675#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004676 return;
4677 }
4678#endif // CONFIG_EXT_REFS
4679
4680 // Set default state for segment based loop filter update flags.
4681 cm->lf.mode_ref_delta_update = 0;
4682
4683 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4684 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4685
4686 // Set various flags etc to special state if it is a key frame.
4687 if (frame_is_intra_only(cm)) {
4688 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004689 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004690
4691 // If segmentation is enabled force a map update for key frames.
4692 if (seg->enabled) {
4693 seg->update_map = 1;
4694 seg->update_data = 1;
4695 }
4696
4697 // The alternate reference frame cannot be active for a key frame.
4698 cpi->rc.source_alt_ref_active = 0;
4699
4700 cm->error_resilient_mode = oxcf->error_resilient_mode;
4701
4702 // By default, encoder assumes decoder can use prev_mi.
4703 if (cm->error_resilient_mode) {
4704 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
4705 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4706 } else if (cm->intra_only) {
4707 // Only reset the current context.
4708 cm->reset_frame_context = RESET_FRAME_CONTEXT_CURRENT;
4709 }
4710 }
Thomas Davies80188d12016-10-26 16:08:35 -07004711#if CONFIG_TILE_GROUPS
Thomas Daviesaf6df172016-11-09 14:04:18 +00004712 if (cpi->oxcf.mtu == 0) {
4713 cm->num_tg = cpi->oxcf.num_tile_groups;
4714 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08004715 // Use a default value for the purposes of weighting costs in probability
4716 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00004717 cm->num_tg = DEFAULT_MAX_NUM_TG;
4718 }
Thomas Davies80188d12016-10-26 16:08:35 -07004719#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004720
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08004721#if CONFIG_EXT_TILE
4722 cm->tile_encoding_mode = cpi->oxcf.tile_encoding_mode;
4723#endif // CONFIG_EXT_TILE
4724
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004725#if CONFIG_XIPHRC
4726 if (drop_this_frame) {
4727 av1_rc_postencode_update_drop_frame(cpi);
4728 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07004729#if CONFIG_EC_ADAPT
4730 aom_free(tile_ctxs);
4731 aom_free(cdf_ptrs);
4732#endif
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004733 return;
4734 }
4735#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004736 // For 1 pass CBR, check if we are dropping this frame.
4737 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004738 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004739 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004740 if (av1_rc_drop_frame(cpi)) {
4741 av1_rc_postencode_update_drop_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004742 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07004743#if CONFIG_EC_ADAPT
4744 aom_free(tile_ctxs);
4745 aom_free(cdf_ptrs);
4746#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004747 return;
4748 }
4749 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004750#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004751
Yaowu Xuf883b422016-08-30 14:01:10 -07004752 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004753
4754#if CONFIG_INTERNAL_STATS
4755 memset(cpi->mode_chosen_counts, 0,
4756 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4757#endif
4758
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004759#if CONFIG_REFERENCE_BUFFER
4760 {
4761 /* Non-normative definition of current_frame_id ("frame counter" with
4762 * wraparound) */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004763 const int frame_id_length = FRAME_ID_LENGTH_MINUS7 + 7;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004764 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00004765 int lsb, msb;
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01004766/* quasi-random initialization of current_frame_id for a key frame */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004767#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07004768 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
4769 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
4770 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004771 } else {
4772#endif
Alex Conversef77fd0b2017-04-20 11:00:24 -07004773 lsb = cpi->source->y_buffer[0] & 0xff;
4774 msb = cpi->source->y_buffer[1] & 0xff;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004775#if CONFIG_HIGHBITDEPTH
David Barker49a76562016-12-07 14:50:21 +00004776 }
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01004777#endif
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004778 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004779 } else {
4780 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004781 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
4782 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004783 }
4784 }
4785#endif
4786
Fangwen Fu6160df22017-04-24 09:45:51 -07004787#if CONFIG_EXT_DELTA_Q
4788 cm->delta_q_present_flag = cpi->oxcf.deltaq_mode != NO_DELTA_Q;
Fangwen Fu231fe422017-04-24 17:52:29 -07004789 cm->delta_lf_present_flag = cpi->oxcf.deltaq_mode == DELTA_Q_LF;
Fangwen Fu6160df22017-04-24 09:45:51 -07004790#endif
4791
Yaowu Xuc27fc142016-08-22 16:08:15 -07004792 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
4793 encode_without_recode_loop(cpi);
4794 } else {
4795 encode_with_recode_loop(cpi, size, dest);
4796 }
4797
4798#ifdef OUTPUT_YUV_SKINMAP
4799 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004800 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004801 }
4802#endif // OUTPUT_YUV_SKINMAP
4803
4804 // Special case code to reduce pulsing when key frames are forced at a
4805 // fixed interval. Note the reconstruction error if it is the frame before
4806 // the force key frame
4807 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004808#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004809 if (cm->use_highbitdepth) {
4810 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07004811 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004812 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004813 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004814 }
4815#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07004816 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004817#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004818 }
4819
4820 // If the encoder forced a KEY_FRAME decision
4821 if (cm->frame_type == KEY_FRAME) {
4822 cpi->refresh_last_frame = 1;
4823 }
4824
4825 cm->frame_to_show = get_frame_new_buffer(cm);
4826 cm->frame_to_show->color_space = cm->color_space;
4827 cm->frame_to_show->color_range = cm->color_range;
4828 cm->frame_to_show->render_width = cm->render_width;
4829 cm->frame_to_show->render_height = cm->render_height;
4830
4831#if CONFIG_EXT_REFS
4832// TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
4833// off.
4834#endif // CONFIG_EXT_REFS
4835
4836 // Pick the loop filter level for the frame.
4837 loopfilter_frame(cpi, cm);
4838
4839 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07004840 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004841
Jingning Hanf6214b92017-04-12 11:43:37 -07004842 if (skip_adapt) {
4843#if CONFIG_EC_ADAPT
4844 aom_free(tile_ctxs);
4845 aom_free(cdf_ptrs);
4846#endif
4847 return;
4848 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004849
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004850#if CONFIG_REFERENCE_BUFFER
4851 {
4852 int i;
4853 /* Update reference frame id values based on the value of refresh_mask */
4854 for (i = 0; i < REF_FRAMES; i++) {
4855 if ((cm->refresh_mask >> i) & 1) {
4856 cm->ref_frame_id[i] = cm->current_frame_id;
4857 }
4858 }
4859 }
4860#endif
4861
Yaowu Xuc27fc142016-08-22 16:08:15 -07004862#if DUMP_RECON_FRAMES == 1
4863 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4864 if (cm->show_frame) dump_filtered_recon_frames(cpi);
4865#endif // DUMP_RECON_FRAMES
4866
4867 if (cm->seg.update_map) update_reference_segmentation_map(cpi);
4868
4869 if (frame_is_intra_only(cm) == 0) {
4870 release_scaled_references(cpi);
4871 }
4872
Yaowu Xuf883b422016-08-30 14:01:10 -07004873 av1_update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004874
Jingning Han6de954c2016-12-16 09:52:04 -08004875 for (t = 0; t < TX_SIZES; t++)
Yaowu Xuf883b422016-08-30 14:01:10 -07004876 av1_full_to_model_counts(cpi->td.counts->coef[t],
4877 cpi->td.rd_counts.coef_counts[t]);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08004878#if CONFIG_ENTROPY_STATS
4879 av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
4880#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004881 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004882 av1_adapt_coef_probs(cm);
4883 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00004884#if CONFIG_EC_ADAPT
4885 make_update_tile_list_enc(cpi, cm->tile_rows, cm->tile_cols, tile_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01004886 av1_average_tile_coef_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00004887 cm->tile_rows * cm->tile_cols);
Thomas Davies493623e2017-03-31 16:12:25 +01004888 av1_average_tile_intra_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00004889 cm->tile_rows * cm->tile_cols);
Yushin Chob188ea12017-03-13 13:45:23 -07004890#if CONFIG_PVQ
4891 av1_average_tile_pvq_cdfs(cpi->common.fc, tile_ctxs,
4892 cm->tile_rows * cm->tile_cols);
4893#endif // CONFIG_PVQ
4894#endif // CONFIG_EC_ADAPT
hui suff0da2b2017-03-07 15:51:37 -08004895#if CONFIG_ADAPT_SCAN
4896 av1_adapt_scan_order(cm);
4897#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07004898 }
4899
4900 if (!frame_is_intra_only(cm)) {
4901 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004902 av1_adapt_inter_frame_probs(cm);
4903 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00004904#if CONFIG_EC_ADAPT
4905 av1_average_tile_inter_cdfs(&cpi->common, cpi->common.fc, tile_ctxs,
Thomas Davies493623e2017-03-31 16:12:25 +01004906 cdf_ptrs, cm->tile_rows * cm->tile_cols);
4907 av1_average_tile_mv_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00004908 cm->tile_rows * cm->tile_cols);
4909#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004910 }
4911 }
4912
4913 if (cpi->refresh_golden_frame == 1)
4914 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
4915 else
4916 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4917
4918 if (cpi->refresh_alt_ref_frame == 1)
4919 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
4920 else
4921 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4922
4923#if CONFIG_EXT_REFS
4924 if (cpi->refresh_bwd_ref_frame == 1)
4925 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
4926 else
4927 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4928#endif // CONFIG_EXT_REFS
4929
4930#if !CONFIG_EXT_REFS
4931 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4932#endif // !CONFIG_EXT_REFS
4933
Yaowu Xuc27fc142016-08-22 16:08:15 -07004934 cm->last_frame_type = cm->frame_type;
4935
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004936#if CONFIG_XIPHRC
4937 frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
4938
4939 drop_this_frame =
4940 od_enc_rc_update_state(&cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
4941 cpi->refresh_alt_ref_frame, frame_type, 0);
4942 if (drop_this_frame) {
4943 av1_rc_postencode_update_drop_frame(cpi);
4944 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07004945#if CONFIG_EC_ADAPT
4946 aom_free(tile_ctxs);
4947 aom_free(cdf_ptrs);
4948#endif
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004949 return;
4950 }
Zoe Liue04abf72017-04-19 15:37:11 -07004951#else // !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07004952 av1_rc_postencode_update(cpi, *size);
Zoe Liue04abf72017-04-19 15:37:11 -07004953#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004954
4955#if 0
4956 output_frame_level_debug_stats(cpi);
4957#endif
4958
4959 if (cm->frame_type == KEY_FRAME) {
4960 // Tell the caller that the frame was coded as a key frame
4961 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
4962 } else {
4963 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4964 }
4965
4966 // Clear the one shot update flags for segmentation map and mode/ref loop
4967 // filter deltas.
4968 cm->seg.update_map = 0;
4969 cm->seg.update_data = 0;
4970 cm->lf.mode_ref_delta_update = 0;
4971
4972 // keep track of the last coded dimensions
4973 cm->last_width = cm->width;
4974 cm->last_height = cm->height;
4975
4976 // reset to normal state now that we are done.
4977 if (!cm->show_existing_frame) cm->last_show_frame = cm->show_frame;
4978
4979 if (cm->show_frame) {
4980#if CONFIG_EXT_REFS
4981// TODO(zoeliu): We may only swamp mi and prev_mi for those frames that are
4982// being used as reference.
4983#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07004984 av1_swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004985 // Don't increment frame counters if this was an altref buffer
4986 // update not a real frame
4987 ++cm->current_video_frame;
4988 }
4989
4990#if CONFIG_EXT_REFS
4991 // NOTE: Shall not refer to any frame not used as reference.
4992 if (cm->is_reference_frame)
4993#endif // CONFIG_EXT_REFS
4994 cm->prev_frame = cm->cur_frame;
Thomas Davies493623e2017-03-31 16:12:25 +01004995#if CONFIG_EC_ADAPT
4996 aom_free(tile_ctxs);
4997 aom_free(cdf_ptrs);
4998#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004999}
5000
Yaowu Xuf883b422016-08-30 14:01:10 -07005001static void Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005002 int skip_adapt, unsigned int *frame_flags) {
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005003#if CONFIG_XIPHRC
5004 int64_t ip_count;
5005 int frame_type, is_golden, is_altref;
5006
5007 /* Not updated during init so update it here */
5008 if (cpi->oxcf.rc_mode == AOM_Q) cpi->od_rc.quality = cpi->oxcf.cq_level;
5009
5010 frame_type = od_frame_type(&cpi->od_rc, cpi->od_rc.cur_frame, &is_golden,
5011 &is_altref, &ip_count);
5012
5013 if (frame_type == OD_I_FRAME) {
5014 frame_type = KEY_FRAME;
5015 cpi->frame_flags &= FRAMEFLAGS_KEY;
5016 } else if (frame_type == OD_P_FRAME) {
5017 frame_type = INTER_FRAME;
5018 }
5019
5020 if (is_altref) {
5021 cpi->refresh_alt_ref_frame = 1;
5022 cpi->rc.source_alt_ref_active = 1;
5023 }
5024
5025 cpi->refresh_golden_frame = is_golden;
5026 cpi->common.frame_type = frame_type;
5027 if (is_golden) cpi->frame_flags &= FRAMEFLAGS_GOLDEN;
5028#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005029 if (cpi->oxcf.rc_mode == AOM_CBR) {
5030 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005031 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005032 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005033 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005034#endif
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005035 encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005036}
5037
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005038#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005039static void Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005040 unsigned int *frame_flags) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005041 encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005042
5043#if CONFIG_EXT_REFS
5044 // Do not do post-encoding update for those frames that do not have a spot in
5045 // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
5046 // even when show_existing_frame is used.
5047 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005048 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005049 }
5050 check_show_existing_frame(cpi);
5051#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005052 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005053#endif // CONFIG_EXT_REFS
5054}
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005055#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005056
Yaowu Xuf883b422016-08-30 14:01:10 -07005057static void init_ref_frame_bufs(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005058 int i;
5059 BufferPool *const pool = cm->buffer_pool;
5060 cm->new_fb_idx = INVALID_IDX;
5061 for (i = 0; i < REF_FRAMES; ++i) {
5062 cm->ref_frame_map[i] = INVALID_IDX;
5063 pool->frame_bufs[i].ref_count = 0;
5064 }
5065}
5066
Yaowu Xuf883b422016-08-30 14:01:10 -07005067static void check_initial_width(AV1_COMP *cpi,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005068#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005069 int use_highbitdepth,
5070#endif
5071 int subsampling_x, int subsampling_y) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005072 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005073
5074 if (!cpi->initial_width ||
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005075#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005076 cm->use_highbitdepth != use_highbitdepth ||
5077#endif
5078 cm->subsampling_x != subsampling_x ||
5079 cm->subsampling_y != subsampling_y) {
5080 cm->subsampling_x = subsampling_x;
5081 cm->subsampling_y = subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005082#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005083 cm->use_highbitdepth = use_highbitdepth;
5084#endif
5085
5086 alloc_raw_frame_buffers(cpi);
5087 init_ref_frame_bufs(cm);
5088 alloc_util_frame_buffers(cpi);
5089
5090 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
5091
5092 cpi->initial_width = cm->width;
5093 cpi->initial_height = cm->height;
5094 cpi->initial_mbs = cm->MBs;
5095 }
5096}
5097
James Zern3e2613b2017-03-30 23:14:40 -07005098int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005099 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5100 int64_t end_time) {
5101 AV1_COMMON *const cm = &cpi->common;
5102 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005103 int res = 0;
5104 const int subsampling_x = sd->subsampling_x;
5105 const int subsampling_y = sd->subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005106#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005107 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5108#endif
5109
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005110#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005111 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5112#else
5113 check_initial_width(cpi, subsampling_x, subsampling_y);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005114#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005115
Yaowu Xuf883b422016-08-30 14:01:10 -07005116 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005117
Yaowu Xuf883b422016-08-30 14:01:10 -07005118 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005119#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005120 use_highbitdepth,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005121#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005122 frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005123 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005124 aom_usec_timer_mark(&timer);
5125 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005126
5127 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
5128 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005129 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005130 "Non-4:2:0 color format requires profile 1 or 3");
5131 res = -1;
5132 }
5133 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
5134 (subsampling_x == 1 && subsampling_y == 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005135 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005136 "4:2:0 color format requires profile 0 or 2");
5137 res = -1;
5138 }
5139
5140 return res;
5141}
5142
Yaowu Xuf883b422016-08-30 14:01:10 -07005143static int frame_is_reference(const AV1_COMP *cpi) {
5144 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005145
5146 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
5147 cpi->refresh_golden_frame ||
5148#if CONFIG_EXT_REFS
5149 cpi->refresh_bwd_ref_frame ||
5150#endif // CONFIG_EXT_REFS
5151 cpi->refresh_alt_ref_frame || !cm->error_resilient_mode ||
5152 cm->lf.mode_ref_delta_update || cm->seg.update_map ||
5153 cm->seg.update_data;
5154}
5155
Yaowu Xuf883b422016-08-30 14:01:10 -07005156static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005157 const struct lookahead_entry *source) {
5158 int64_t this_duration;
5159 int step = 0;
5160
5161 if (source->ts_start == cpi->first_time_stamp_ever) {
5162 this_duration = source->ts_end - source->ts_start;
5163 step = 1;
5164 } else {
5165 int64_t last_duration =
5166 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5167
5168 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5169
5170 // do a step update if the duration changes by 10%
5171 if (last_duration)
5172 step = (int)((this_duration - last_duration) * 10 / last_duration);
5173 }
5174
5175 if (this_duration) {
5176 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005177 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005178 } else {
5179 // Average this frame's rate into the last second's average
5180 // frame rate. If we haven't seen 1 second yet, then average
5181 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005182 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005183 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5184 double avg_duration = 10000000.0 / cpi->framerate;
5185 avg_duration *= (interval - avg_duration + this_duration);
5186 avg_duration /= interval;
5187
Yaowu Xuf883b422016-08-30 14:01:10 -07005188 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005189 }
5190 }
5191 cpi->last_time_stamp_seen = source->ts_start;
5192 cpi->last_end_time_stamp_seen = source->ts_end;
5193}
5194
5195// Returns 0 if this is not an alt ref else the offset of the source frame
5196// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005197static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005198 RATE_CONTROL *const rc = &cpi->rc;
5199 int arf_src_index = 0;
5200 if (is_altref_enabled(cpi)) {
5201 if (cpi->oxcf.pass == 2) {
5202 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5203 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5204 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5205 }
5206 } else if (rc->source_alt_ref_pending) {
5207 arf_src_index = rc->frames_till_gf_update_due;
5208 }
5209 }
5210 return arf_src_index;
5211}
5212
5213#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005214static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005215 int brf_src_index = 0;
5216 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5217
5218 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5219 // flag.
5220 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5221 if (cpi->oxcf.pass == 2) {
5222 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5223 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5224 } else {
5225 // TODO(zoeliu): To re-visit the setup for this scenario
5226 brf_src_index = cpi->rc.bipred_group_interval - 1;
5227 }
5228 }
5229
5230 return brf_src_index;
5231}
5232#endif // CONFIG_EXT_REFS
5233
Yaowu Xuf883b422016-08-30 14:01:10 -07005234static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005235 const struct lookahead_entry *source) {
5236 RATE_CONTROL *const rc = &cpi->rc;
5237
5238 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005239 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005240
5241 if (cpi->oxcf.pass == 2) {
5242 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5243 rc->is_src_frame_alt_ref =
5244#if CONFIG_EXT_REFS
5245 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Zoe Liu6cfaff92016-10-18 17:12:11 -07005246#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005247 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
5248 } else {
5249 rc->is_src_frame_alt_ref =
5250 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5251 }
5252
5253 if (rc->is_src_frame_alt_ref) {
5254 // Current frame is an ARF overlay frame.
5255 cpi->alt_ref_source = NULL;
5256
5257 // Don't refresh the last buffer for an ARF overlay frame. It will
5258 // become the GF so preserve last as an alternative prediction option.
5259 cpi->refresh_last_frame = 0;
5260 }
5261}
5262
5263#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005264extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5265 const unsigned char *img2, int img2_pitch,
5266 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005267
5268static void adjust_image_stat(double y, double u, double v, double all,
5269 ImageStat *s) {
5270 s->stat[Y] += y;
5271 s->stat[U] += u;
5272 s->stat[V] += v;
5273 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005274 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005275}
5276
Yaowu Xuf883b422016-08-30 14:01:10 -07005277static void compute_internal_stats(AV1_COMP *cpi) {
5278 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005279 double samples = 0.0;
5280 uint32_t in_bit_depth = 8;
5281 uint32_t bit_depth = 8;
5282
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005283#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005284 if (cm->use_highbitdepth) {
5285 in_bit_depth = cpi->oxcf.input_bit_depth;
5286 bit_depth = cm->bit_depth;
5287 }
5288#endif
5289 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005290 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005291 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5292 double y, u, v, frame_all;
5293
5294 cpi->count++;
5295 if (cpi->b_calculate_psnr) {
5296 PSNR_STATS psnr;
5297 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005298 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005299// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005300#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005301 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005302#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005303 aom_calc_psnr(orig, recon, &psnr);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005304#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005305
5306 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5307 &cpi->psnr);
5308 cpi->total_sq_error += psnr.sse[0];
5309 cpi->total_samples += psnr.samples[0];
5310 samples = psnr.samples[0];
5311// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005312#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005313 if (cm->use_highbitdepth)
5314 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005315 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005316 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005317 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005318#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005319 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005320#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005321
Yaowu Xuf883b422016-08-30 14:01:10 -07005322 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005323 cpi->summed_quality += frame_ssim2 * weight;
5324 cpi->summed_weights += weight;
5325
5326#if 0
5327 {
5328 FILE *f = fopen("q_used.stt", "a");
5329 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5330 cpi->common.current_video_frame, y2, u2, v2,
5331 frame_psnr2, frame_ssim2);
5332 fclose(f);
5333 }
5334#endif
5335 }
5336 if (cpi->b_calculate_blockiness) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005337#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005338 if (!cm->use_highbitdepth)
5339#endif
5340 {
5341 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005342 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5343 recon->y_stride, orig->y_width, orig->y_height);
5344 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005345 cpi->total_blockiness += frame_blockiness;
5346 }
5347
5348 if (cpi->b_calculate_consistency) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005349#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005350 if (!cm->use_highbitdepth)
5351#endif
5352 {
Yaowu Xuf883b422016-08-30 14:01:10 -07005353 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005354 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5355 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5356
5357 const double peak = (double)((1 << in_bit_depth) - 1);
5358 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005359 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005360 if (consistency > 0.0)
5361 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005362 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005363 cpi->total_inconsistency += this_inconsistency;
5364 }
5365 }
5366 }
5367
5368 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005369 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005370 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005371 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005372 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5373 }
5374}
5375#endif // CONFIG_INTERNAL_STATS
5376
Yaowu Xuf883b422016-08-30 14:01:10 -07005377int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5378 size_t *size, uint8_t *dest, int64_t *time_stamp,
5379 int64_t *time_end, int flush) {
5380 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5381 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005382 BufferPool *const pool = cm->buffer_pool;
5383 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07005384 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005385 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5386 struct lookahead_entry *last_source = NULL;
5387 struct lookahead_entry *source = NULL;
5388 int arf_src_index;
5389#if CONFIG_EXT_REFS
5390 int brf_src_index;
5391#endif // CONFIG_EXT_REFS
5392 int i;
5393
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005394#if CONFIG_XIPHRC
5395 cpi->od_rc.end_of_input = flush;
5396#endif
5397
Yaowu Xuc27fc142016-08-22 16:08:15 -07005398#if CONFIG_BITSTREAM_DEBUG
5399 assert(cpi->oxcf.max_threads == 0 &&
5400 "bitstream debug tool does not support multithreading");
5401 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07005402 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005403#endif
5404
Yaowu Xuf883b422016-08-30 14:01:10 -07005405 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005406
Yaowu Xuf883b422016-08-30 14:01:10 -07005407 av1_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005408
5409 // Is multi-arf enabled.
5410 // Note that at the moment multi_arf is only configured for 2 pass VBR
5411 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
5412 cpi->multi_arf_allowed = 1;
5413 else
5414 cpi->multi_arf_allowed = 0;
5415
5416 // Normal defaults
5417 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
5418 cm->refresh_frame_context =
5419 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
5420 ? REFRESH_FRAME_CONTEXT_FORWARD
5421 : REFRESH_FRAME_CONTEXT_BACKWARD;
5422
5423 cpi->refresh_last_frame = 1;
5424 cpi->refresh_golden_frame = 0;
5425#if CONFIG_EXT_REFS
5426 cpi->refresh_bwd_ref_frame = 0;
5427#endif // CONFIG_EXT_REFS
5428 cpi->refresh_alt_ref_frame = 0;
5429
Zoe Liue04abf72017-04-19 15:37:11 -07005430#if CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005431 if (oxcf->pass == 2 && cm->show_existing_frame) {
5432 // Manage the source buffer and flush out the source frame that has been
5433 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07005434 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005435 *size = 0;
5436 return -1;
5437 }
Alex Conversef77fd0b2017-04-20 11:00:24 -07005438 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005439 // TODO(zoeliu): To track down to determine whether it's needed to adjust
5440 // the frame rate.
5441 *time_stamp = source->ts_start;
5442 *time_end = source->ts_end;
5443
5444 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005445 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005446
5447 // Find a free buffer for the new frame, releasing the reference previously
5448 // held.
5449 if (cm->new_fb_idx != INVALID_IDX) {
5450 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5451 }
5452 cm->new_fb_idx = get_free_fb(cm);
5453
5454 if (cm->new_fb_idx == INVALID_IDX) return -1;
5455
5456 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005457 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005458
5459 // Start with a 0 size frame.
5460 *size = 0;
5461
5462 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005463 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005464
5465 Pass2Encode(cpi, size, dest, frame_flags);
5466
5467 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
5468
5469#if CONFIG_INTERNAL_STATS
5470 compute_internal_stats(cpi);
5471 cpi->bytes += (int)(*size);
5472#endif // CONFIG_INTERNAL_STATS
5473
5474 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005475 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005476
5477 cm->show_existing_frame = 0;
5478 return 0;
5479 }
Zoe Liue04abf72017-04-19 15:37:11 -07005480#endif // CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005481
5482 // Should we encode an arf frame.
5483 arf_src_index = get_arf_src_index(cpi);
5484 if (arf_src_index) {
5485 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005486 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005487 // Avoid creating an alt-ref if there's a forced keyframe pending.
5488 if (e == NULL) {
5489 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07005490 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005491 arf_src_index = 0;
5492 flush = 1;
5493 break;
5494 }
5495 }
5496 }
5497
5498 if (arf_src_index) {
5499 assert(arf_src_index <= rc->frames_to_key);
5500
Yaowu Xuf883b422016-08-30 14:01:10 -07005501 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005502 cpi->alt_ref_source = source;
5503
5504 if (oxcf->arnr_max_frames > 0) {
5505 // Produce the filtered ARF frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005506 av1_temporal_filter(cpi, arf_src_index);
5507 aom_extend_frame_borders(&cpi->alt_ref_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005508 force_src_buffer = &cpi->alt_ref_buffer;
5509 }
5510
5511 cm->show_frame = 0;
5512 cm->intra_only = 0;
5513 cpi->refresh_alt_ref_frame = 1;
5514 cpi->refresh_golden_frame = 0;
5515 cpi->refresh_last_frame = 0;
5516 rc->is_src_frame_alt_ref = 0;
5517 }
5518 rc->source_alt_ref_pending = 0;
5519 }
5520
5521#if CONFIG_EXT_REFS
5522 rc->is_bwd_ref_frame = 0;
5523 brf_src_index = get_brf_src_index(cpi);
5524 if (brf_src_index) {
5525 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07005526 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005527 cm->show_frame = 0;
5528 cm->intra_only = 0;
5529
5530 cpi->refresh_bwd_ref_frame = 1;
5531 cpi->refresh_last_frame = 0;
5532 cpi->refresh_golden_frame = 0;
5533 cpi->refresh_alt_ref_frame = 0;
5534
5535 rc->is_bwd_ref_frame = 1;
5536 }
5537 }
5538#endif // CONFIG_EXT_REFS
5539
5540 if (!source) {
5541 // Get last frame source.
5542 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005543 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005544 return -1;
5545 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005546 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005547 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005548 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005549
5550 if (source != NULL) {
5551 cm->show_frame = 1;
5552 cm->intra_only = 0;
5553
5554 // Check to see if the frame should be encoded as an arf overlay.
5555 check_src_altref(cpi, source);
5556 }
5557 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005558 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005559 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005560 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005561 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
5562
5563 *time_stamp = source->ts_start;
5564 *time_end = source->ts_end;
Yaowu Xuf883b422016-08-30 14:01:10 -07005565 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005566
5567 } else {
5568 *size = 0;
5569 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005570#if CONFIG_XIPHRC
5571 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 1);
5572#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005573 av1_end_first_pass(cpi); /* get last stats packet */
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005574#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005575 cpi->twopass.first_pass_done = 1;
5576 }
5577 return -1;
5578 }
5579
5580 if (source->ts_start < cpi->first_time_stamp_ever) {
5581 cpi->first_time_stamp_ever = source->ts_start;
5582 cpi->last_end_time_stamp_seen = source->ts_start;
5583 }
5584
5585 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005586 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005587
5588 // adjust frame rates based on timestamps given
5589 if (cm->show_frame) adjust_frame_rate(cpi, source);
5590
5591 // Find a free buffer for the new frame, releasing the reference previously
5592 // held.
5593 if (cm->new_fb_idx != INVALID_IDX) {
5594 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5595 }
5596 cm->new_fb_idx = get_free_fb(cm);
5597
5598 if (cm->new_fb_idx == INVALID_IDX) return -1;
5599
5600 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
5601
5602#if CONFIG_EXT_REFS
5603 if (oxcf->pass == 2) {
5604 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5605 cpi->alt_fb_idx = cpi->arf_map[gf_group->arf_ref_idx[gf_group->index]];
5606 }
5607#else
5608 if (cpi->multi_arf_allowed) {
5609 if (cm->frame_type == KEY_FRAME) {
5610 init_buffer_indices(cpi);
5611 } else if (oxcf->pass == 2) {
5612 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5613 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
5614 }
5615 }
Zoe Liu6cfaff92016-10-18 17:12:11 -07005616#endif // CONFIG_EXT_REFS
5617
Yaowu Xuc27fc142016-08-22 16:08:15 -07005618 // Start with a 0 size frame.
5619 *size = 0;
5620
5621 cpi->frame_flags = *frame_flags;
5622
5623 if (oxcf->pass == 2) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005624#if CONFIG_XIPHRC
5625 if (od_enc_rc_2pass_in(&cpi->od_rc) < 0) return -1;
5626 }
5627#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005628 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005629 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07005630 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005631 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005632#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005633
5634 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
5635 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i)
5636 cpi->scaled_ref_idx[i] = INVALID_IDX;
5637 }
5638
5639#if CONFIG_AOM_QM
5640 cm->using_qmatrix = cpi->oxcf.using_qm;
5641 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
5642 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
5643#endif
5644
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005645#if CONFIG_REFERENCE_BUFFER
5646 if (*time_stamp == 0) {
5647 cpi->common.current_frame_id = -1;
5648 }
5649#endif
5650
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005651#if CONFIG_XIPHRC
5652 if (oxcf->pass == 1) {
5653 size_t tmp;
5654 if (cpi->od_rc.cur_frame == 0) Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
5655 cpi->od_rc.firstpass_quant = cpi->od_rc.target_quantizer;
5656 Pass0Encode(cpi, &tmp, dest, 0, frame_flags);
5657 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 0);
5658 } else if (oxcf->pass == 2) {
5659 Pass0Encode(cpi, size, dest, 0, frame_flags);
5660 } else {
5661 if (cpi->od_rc.cur_frame == 0) {
5662 size_t tmp;
5663 Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
5664 }
5665 Pass0Encode(cpi, size, dest, 0, frame_flags);
5666 }
5667#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07005668 if (oxcf->pass == 1) {
5669 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07005670 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005671 } else if (oxcf->pass == 2) {
5672 Pass2Encode(cpi, size, dest, frame_flags);
5673 } else {
5674 // One pass encode
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005675 Pass0Encode(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005676 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005677#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005678
5679 if (!cm->error_resilient_mode)
5680 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
5681
5682 // No frame encoded, or frame was dropped, release scaled references.
5683 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
5684 release_scaled_references(cpi);
5685 }
5686
5687 if (*size > 0) {
5688 cpi->droppable = !frame_is_reference(cpi);
5689 }
5690
Yaowu Xuf883b422016-08-30 14:01:10 -07005691 aom_usec_timer_mark(&cmptimer);
5692 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005693
5694 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
5695 generate_psnr_packet(cpi);
5696
5697#if CONFIG_INTERNAL_STATS
5698 if (oxcf->pass != 1) {
5699 compute_internal_stats(cpi);
5700 cpi->bytes += (int)(*size);
5701 }
5702#endif // CONFIG_INTERNAL_STATS
5703
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005704#if CONFIG_XIPHRC
5705 cpi->od_rc.cur_frame++;
5706#endif
5707
Yaowu Xuf883b422016-08-30 14:01:10 -07005708 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005709
5710 return 0;
5711}
5712
Yaowu Xuf883b422016-08-30 14:01:10 -07005713int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5714 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005715 if (!cm->show_frame) {
5716 return -1;
5717 } else {
5718 int ret;
5719 if (cm->frame_to_show) {
5720 *dest = *cm->frame_to_show;
5721 dest->y_width = cm->width;
5722 dest->y_height = cm->height;
5723 dest->uv_width = cm->width >> cm->subsampling_x;
5724 dest->uv_height = cm->height >> cm->subsampling_y;
5725 ret = 0;
5726 } else {
5727 ret = -1;
5728 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005729 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005730 return ret;
5731 }
5732}
5733
Yaowu Xuf883b422016-08-30 14:01:10 -07005734int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005735 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
5736
5737 *frame =
5738 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
5739 return 0;
5740}
5741
Yaowu Xuf883b422016-08-30 14:01:10 -07005742int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
5743 AOM_SCALING vert_mode) {
5744 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005745 int hr = 0, hs = 0, vr = 0, vs = 0;
5746
5747 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5748
5749 Scale2Ratio(horiz_mode, &hr, &hs);
5750 Scale2Ratio(vert_mode, &vr, &vs);
5751
5752 // always go to the next whole number
5753 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5754 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
5755 assert(cm->width <= cpi->initial_width);
5756 assert(cm->height <= cpi->initial_height);
5757
5758 update_frame_size(cpi);
5759
5760 return 0;
5761}
5762
Fergus Simpson0a30c882017-05-16 16:54:51 -07005763int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005764 AV1_COMMON *cm = &cpi->common;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005765#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005766 check_initial_width(cpi, cm->use_highbitdepth, 1, 1);
5767#else
5768 check_initial_width(cpi, 1, 1);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005769#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005770
Fergus Simpson0a30c882017-05-16 16:54:51 -07005771 if (width <= 0 || height <= 0) return 1;
5772
5773 cm->width = width;
5774 if (cm->width > cpi->initial_width) {
5775 cm->width = cpi->initial_width;
5776 printf("Warning: Desired width too large, changed to %d\n", cm->width);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005777 }
5778
Fergus Simpson0a30c882017-05-16 16:54:51 -07005779 cm->height = height;
5780 if (cm->height > cpi->initial_height) {
5781 cm->height = cpi->initial_height;
5782 printf("Warning: Desired height too large, changed to %d\n", cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005783 }
Fergus Simpson0a30c882017-05-16 16:54:51 -07005784
Yaowu Xuc27fc142016-08-22 16:08:15 -07005785 assert(cm->width <= cpi->initial_width);
5786 assert(cm->height <= cpi->initial_height);
5787
5788 update_frame_size(cpi);
5789
5790 return 0;
5791}
5792
Yaowu Xuf883b422016-08-30 14:01:10 -07005793int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005794
Yaowu Xuf883b422016-08-30 14:01:10 -07005795void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005796 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07005797 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF)) {
5798 int ref = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005799
Yaowu Xuf883b422016-08-30 14:01:10 -07005800 if (flags & AOM_EFLAG_NO_REF_LAST) {
5801 ref ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005802#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005803 ref ^= AOM_LAST2_FLAG;
5804 ref ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005805#endif // CONFIG_EXT_REFS
5806 }
5807
Yaowu Xuf883b422016-08-30 14:01:10 -07005808 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005809
Yaowu Xuf883b422016-08-30 14:01:10 -07005810 if (flags & AOM_EFLAG_NO_REF_ARF) ref ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005811
Yaowu Xuf883b422016-08-30 14:01:10 -07005812 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005813 }
5814
5815 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07005816 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
5817 AOM_EFLAG_FORCE_GF | AOM_EFLAG_FORCE_ARF)) {
5818 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005819
Yaowu Xuf883b422016-08-30 14:01:10 -07005820 if (flags & AOM_EFLAG_NO_UPD_LAST) {
5821 upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005822#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005823 upd ^= AOM_LAST2_FLAG;
5824 upd ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005825#endif // CONFIG_EXT_REFS
5826 }
5827
Yaowu Xuf883b422016-08-30 14:01:10 -07005828 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005829
Yaowu Xuf883b422016-08-30 14:01:10 -07005830 if (flags & AOM_EFLAG_NO_UPD_ARF) upd ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005831
Yaowu Xuf883b422016-08-30 14:01:10 -07005832 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005833 }
5834
Yaowu Xuf883b422016-08-30 14:01:10 -07005835 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
5836 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005837 }
5838}