blob: d0ec1f7b12f81328bab5adba05fd8efb2e603ef8 [file] [log] [blame]
Yaowu Xuc27fc142016-08-22 16:08:15 -07001/*
Urvang Joshi8a02d762016-07-28 15:51:12 -07002 * Copyright (c) 2016, Alliance for Open Media. All rights reserved
Yaowu Xuc27fc142016-08-22 16:08:15 -07003 *
Urvang Joshi8a02d762016-07-28 15:51:12 -07004 * This source code is subject to the terms of the BSD 2 Clause License and
5 * the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
6 * was not distributed with this source code in the LICENSE file, you can
7 * obtain it at www.aomedia.org/license/software. If the Alliance for Open
8 * Media Patent License 1.0 was not distributed with this source code in the
9 * PATENTS file, you can obtain it at www.aomedia.org/license/patent.
Yaowu Xuc27fc142016-08-22 16:08:15 -070010 */
11
12#include <limits.h>
13#include <math.h>
14#include <stdio.h>
15
Yaowu Xuf883b422016-08-30 14:01:10 -070016#include "./aom_config.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070017
18#include "av1/common/alloccommon.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050019#if CONFIG_CDEF
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010020#include "av1/common/cdef.h"
Jean-Marc Valin01435132017-02-18 14:12:53 -050021#endif // CONFIG_CDEF
Yaowu Xuc27fc142016-08-22 16:08:15 -070022#include "av1/common/filter.h"
23#include "av1/common/idct.h"
24#include "av1/common/reconinter.h"
25#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070026#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070027#include "av1/common/tile_common.h"
28
29#include "av1/encoder/aq_complexity.h"
30#include "av1/encoder/aq_cyclicrefresh.h"
31#include "av1/encoder/aq_variance.h"
32#include "av1/encoder/bitstream.h"
Todd Nguyen302d0972017-06-16 16:16:29 -070033#if CONFIG_BGSPRITE
34#include "av1/encoder/bgsprite.h"
35#endif // CONFIG_BGSPRITE
Yaowu Xuc27fc142016-08-22 16:08:15 -070036#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -070037#include "aom_dsp/buf_ans.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070038#endif
39#include "av1/encoder/context_tree.h"
40#include "av1/encoder/encodeframe.h"
41#include "av1/encoder/encodemv.h"
42#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070043#if CONFIG_LV_MAP
44#include "av1/encoder/encodetxb.h"
45#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070046#include "av1/encoder/ethread.h"
47#include "av1/encoder/firstpass.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070048#if CONFIG_HASH_ME
49#include "av1/encoder/hash_motion.h"
50#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -070051#include "av1/encoder/mbgraph.h"
52#include "av1/encoder/picklpf.h"
53#if CONFIG_LOOP_RESTORATION
54#include "av1/encoder/pickrst.h"
55#endif // CONFIG_LOOP_RESTORATION
56#include "av1/encoder/ratectrl.h"
57#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070058#include "av1/encoder/segmentation.h"
59#include "av1/encoder/speed_features.h"
60#include "av1/encoder/temporal_filter.h"
61
Yaowu Xuf883b422016-08-30 14:01:10 -070062#include "./av1_rtcd.h"
63#include "./aom_dsp_rtcd.h"
64#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070065#include "aom_dsp/psnr.h"
66#if CONFIG_INTERNAL_STATS
67#include "aom_dsp/ssim.h"
68#endif
Yaowu Xuf883b422016-08-30 14:01:10 -070069#include "aom_dsp/aom_dsp_common.h"
70#include "aom_dsp/aom_filter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070071#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070072#include "aom_ports/mem.h"
73#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070074#include "aom_scale/aom_scale.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070075#if CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070076#include "aom_util/debug_util.h"
Angie Chiang6062a8b2016-09-21 16:01:04 -070077#endif // CONFIG_BITSTREAM_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070078
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080079#if CONFIG_ENTROPY_STATS
80FRAME_COUNTS aggregate_fc;
Zoe Liua56f9162017-06-21 22:49:57 -070081// Aggregate frame counts per frame context type
82FRAME_COUNTS aggregate_fc_per_type[FRAME_CONTEXTS];
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080083#endif // CONFIG_ENTROPY_STATS
84
Yaowu Xuc27fc142016-08-22 16:08:15 -070085#define AM_SEGMENT_ID_INACTIVE 7
86#define AM_SEGMENT_ID_ACTIVE 0
87
88#define SHARP_FILTER_QTHRESH 0 /* Q threshold for 8-tap sharp filter */
89
90#define ALTREF_HIGH_PRECISION_MV 1 // Whether to use high precision mv
91 // for altref computation.
92#define HIGH_PRECISION_MV_QTHRESH 200 // Q threshold for high precision
93 // mv. Choose a very high value for
94 // now so that HIGH_PRECISION is always
95 // chosen.
96// #define OUTPUT_YUV_REC
97#ifdef OUTPUT_YUV_DENOISED
98FILE *yuv_denoised_file = NULL;
99#endif
100#ifdef OUTPUT_YUV_SKINMAP
101FILE *yuv_skinmap_file = NULL;
102#endif
103#ifdef OUTPUT_YUV_REC
104FILE *yuv_rec_file;
105#define FILE_NAME_LEN 100
106#endif
107
108#if 0
109FILE *framepsnr;
110FILE *kf_list;
111FILE *keyfile;
112#endif
113
Luc Trudeauf8164152017-04-11 16:20:51 -0400114#if CONFIG_CFL
115CFL_CTX NULL_CFL;
116#endif
117
Urvang Joshib5ed3502016-10-17 16:38:05 -0700118#if CONFIG_INTERNAL_STATS
119typedef enum { Y, U, V, ALL } STAT_TYPE;
120#endif // CONFIG_INTERNAL_STATS
121
Yaowu Xuf883b422016-08-30 14:01:10 -0700122static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700123 switch (mode) {
124 case NORMAL:
125 *hr = 1;
126 *hs = 1;
127 break;
128 case FOURFIVE:
129 *hr = 4;
130 *hs = 5;
131 break;
132 case THREEFIVE:
133 *hr = 3;
134 *hs = 5;
135 break;
136 case ONETWO:
137 *hr = 1;
138 *hs = 2;
139 break;
140 default:
141 *hr = 1;
142 *hs = 1;
143 assert(0);
144 break;
145 }
146}
147
148// Mark all inactive blocks as active. Other segmentation features may be set
149// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700150static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700151 unsigned char *const seg_map = cpi->segmentation_map;
152 int i;
153 if (cpi->active_map.enabled || cpi->active_map.update)
154 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
155 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
156 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
157}
158
Yaowu Xuf883b422016-08-30 14:01:10 -0700159static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700160 struct segmentation *const seg = &cpi->common.seg;
161 unsigned char *const seg_map = cpi->segmentation_map;
162 const unsigned char *const active_map = cpi->active_map.map;
163 int i;
164
165 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
166
167 if (frame_is_intra_only(&cpi->common)) {
168 cpi->active_map.enabled = 0;
169 cpi->active_map.update = 1;
170 }
171
172 if (cpi->active_map.update) {
173 if (cpi->active_map.enabled) {
174 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
175 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700176 av1_enable_segmentation(seg);
177 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
178 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700179 // Setting the data to -MAX_LOOP_FILTER will result in the computed loop
180 // filter level being zero regardless of the value of seg->abs_delta.
Yaowu Xuf883b422016-08-30 14:01:10 -0700181 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF,
182 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700183 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700184 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
185 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 if (seg->enabled) {
187 seg->update_data = 1;
188 seg->update_map = 1;
189 }
190 }
191 cpi->active_map.update = 0;
192 }
193}
194
Yaowu Xuf883b422016-08-30 14:01:10 -0700195int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
196 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700197 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
198 unsigned char *const active_map_8x8 = cpi->active_map.map;
199 const int mi_rows = cpi->common.mi_rows;
200 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700201 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
202 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700203 cpi->active_map.update = 1;
204 if (new_map_16x16) {
205 int r, c;
206 for (r = 0; r < mi_rows; ++r) {
207 for (c = 0; c < mi_cols; ++c) {
208 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700209 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700210 ? AM_SEGMENT_ID_ACTIVE
211 : AM_SEGMENT_ID_INACTIVE;
212 }
213 }
214 cpi->active_map.enabled = 1;
215 } else {
216 cpi->active_map.enabled = 0;
217 }
218 return 0;
219 } else {
220 return -1;
221 }
222}
223
Yaowu Xuf883b422016-08-30 14:01:10 -0700224int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
225 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
227 new_map_16x16) {
228 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
229 const int mi_rows = cpi->common.mi_rows;
230 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700231 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
232 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
233
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
235 if (cpi->active_map.enabled) {
236 int r, c;
237 for (r = 0; r < mi_rows; ++r) {
238 for (c = 0; c < mi_cols; ++c) {
239 // Cyclic refresh segments are considered active despite not having
240 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700241 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700242 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
243 }
244 }
245 }
246 return 0;
247 } else {
248 return -1;
249 }
250}
251
James Zern01a9d702017-08-25 19:09:33 +0000252void av1_set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv) {
253 MACROBLOCK *const mb = &cpi->td.mb;
254 cpi->common.allow_high_precision_mv = allow_high_precision_mv;
255
256 if (cpi->common.allow_high_precision_mv) {
257 int i;
258 for (i = 0; i < NMV_CONTEXTS; ++i) {
259 mb->mv_cost_stack[i] = mb->nmvcost_hp[i];
260 }
261 } else {
262 int i;
263 for (i = 0; i < NMV_CONTEXTS; ++i) {
264 mb->mv_cost_stack[i] = mb->nmvcost[i];
265 }
266 }
267}
268
Yaowu Xuf883b422016-08-30 14:01:10 -0700269static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700270#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -0700271 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700272 return BLOCK_64X64;
273
Yaowu Xuf883b422016-08-30 14:01:10 -0700274 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700275 return BLOCK_128X128;
276
Yaowu Xuf883b422016-08-30 14:01:10 -0700277 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700278
279 assert(IMPLIES(cpi->common.tile_cols > 1,
280 cpi->common.tile_width % MAX_MIB_SIZE == 0));
281 assert(IMPLIES(cpi->common.tile_rows > 1,
282 cpi->common.tile_height % MAX_MIB_SIZE == 0));
283
284 // TODO(any): Possibly could improve this with a heuristic.
285 return BLOCK_128X128;
286#else
287 (void)cpi;
288 return BLOCK_64X64;
289#endif // CONFIG_EXT_PARTITION
290}
291
Yaowu Xuf883b422016-08-30 14:01:10 -0700292static void setup_frame(AV1_COMP *cpi) {
293 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 // Set up entropy context depending on frame type. The decoder mandates
295 // the use of the default context, index 0, for keyframes and inter
296 // frames where the error_resilient_mode or intra_only flag is set. For
297 // other inter-frames the encoder currently uses only two contexts;
298 // context 1 for ALTREF frames and context 0 for the others.
299 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700300 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700301 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700302#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
303// Just use frame context from first signaled reference frame.
304// This will always be LAST_FRAME for now.
305#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700306#if CONFIG_EXT_REFS
307 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -0700308 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700309 cm->frame_context_idx = EXT_ARF_FRAME;
310 else if (cpi->refresh_alt_ref_frame)
311 cm->frame_context_idx = ARF_FRAME;
Zoe Liue9b15e22017-07-19 15:53:01 -0700312#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700313 if (cpi->refresh_alt_ref_frame) cm->frame_context_idx = ARF_FRAME;
Zoe Liu6cfaff92016-10-18 17:12:11 -0700314#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700315 else if (cpi->rc.is_src_frame_alt_ref)
316 cm->frame_context_idx = OVERLAY_FRAME;
317 else if (cpi->refresh_golden_frame)
318 cm->frame_context_idx = GLD_FRAME;
319#if CONFIG_EXT_REFS
320 else if (cpi->refresh_bwd_ref_frame)
321 cm->frame_context_idx = BRF_FRAME;
Zoe Liu6cfaff92016-10-18 17:12:11 -0700322#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700323 else
324 cm->frame_context_idx = REGULAR_FRAME;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700325#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326 }
327
328 if (cm->frame_type == KEY_FRAME) {
329 cpi->refresh_golden_frame = 1;
330 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700331 av1_zero(cpi->interp_filter_selected);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700332 } else {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700333#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
334 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
335 cm->frame_refs[0].idx < 0) {
336 *cm->fc = cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
337 } else {
338 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
339 }
340#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700341 *cm->fc = cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700342#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuf883b422016-08-30 14:01:10 -0700343 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344 }
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200345#if CONFIG_EXT_REFS
Zoe Liu5a978832017-08-15 16:33:34 -0700346#if CONFIG_ONE_SIDED_COMPOUND && \
347 !CONFIG_EXT_COMP_REFS // No change to bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +0200348 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
349 cpi->refresh_bwd_ref_frame = cpi->refresh_last_frame;
350 cpi->rc.is_bipred_frame = 1;
351 }
Zoe Liu5a978832017-08-15 16:33:34 -0700352#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
353#endif // CONFIG_EXT_REFS
Thomas Daededa4d8b92017-06-05 15:44:14 -0700354#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -0700355 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
356 cm->frame_refs[0].idx < 0) {
Thomas Daededa4d8b92017-06-05 15:44:14 -0700357 // use default frame context values
358 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
359 } else {
Thomas Daedea6a854b2017-06-22 17:49:11 -0700360 *cm->fc = cm->frame_contexts[cm->frame_refs[0].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700361 cm->pre_fc = &cm->frame_contexts[cm->frame_refs[0].idx];
362 }
363#else
Thomas Daede10e1da92017-04-26 13:22:21 -0700364 cm->pre_fc = &cm->frame_contexts[cm->frame_context_idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700365#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -0700366
367 cpi->vaq_refresh = 0;
368
369 set_sb_size(cm, select_sb_size(cpi));
370}
371
Yaowu Xuf883b422016-08-30 14:01:10 -0700372static void av1_enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700373 int i;
374 cm->mi = cm->mip + cm->mi_stride + 1;
375 memset(cm->mip, 0, cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mip));
376 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
377 // Clear top border row
378 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
379 // Clear left border column
380 for (i = 1; i < cm->mi_rows + 1; ++i)
381 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700382 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
383 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
384
385 memset(cm->mi_grid_base, 0,
386 cm->mi_stride * (cm->mi_rows + 1) * sizeof(*cm->mi_grid_base));
387}
388
Yaowu Xuf883b422016-08-30 14:01:10 -0700389static int av1_enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
390 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700391 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700392 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700393 if (!cm->prev_mip) return 1;
394 cm->mi_alloc_size = mi_size;
395
Yaowu Xuf883b422016-08-30 14:01:10 -0700396 cm->mi_grid_base = (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397 if (!cm->mi_grid_base) return 1;
398 cm->prev_mi_grid_base =
Yaowu Xuf883b422016-08-30 14:01:10 -0700399 (MODE_INFO **)aom_calloc(mi_size, sizeof(MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400 if (!cm->prev_mi_grid_base) return 1;
401
402 return 0;
403}
404
Yaowu Xuf883b422016-08-30 14:01:10 -0700405static void av1_enc_free_mi(AV1_COMMON *cm) {
406 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700407 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700408 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700409 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700410 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700411 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700412 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700413 cm->prev_mi_grid_base = NULL;
414}
415
Yaowu Xuf883b422016-08-30 14:01:10 -0700416static void av1_swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417 // Current mip will be the prev_mip for the next frame.
418 MODE_INFO **temp_base = cm->prev_mi_grid_base;
419 MODE_INFO *temp = cm->prev_mip;
420 cm->prev_mip = cm->mip;
421 cm->mip = temp;
422
423 // Update the upper left visible macroblock ptrs.
424 cm->mi = cm->mip + cm->mi_stride + 1;
425 cm->prev_mi = cm->prev_mip + cm->mi_stride + 1;
426
427 cm->prev_mi_grid_base = cm->mi_grid_base;
428 cm->mi_grid_base = temp_base;
429 cm->mi_grid_visible = cm->mi_grid_base + cm->mi_stride + 1;
430 cm->prev_mi_grid_visible = cm->prev_mi_grid_base + cm->mi_stride + 1;
431}
432
Yaowu Xuf883b422016-08-30 14:01:10 -0700433void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700434 static volatile int init_done = 0;
435
436 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700437 av1_rtcd();
438 aom_dsp_rtcd();
439 aom_scale_rtcd();
440 av1_init_intra_predictors();
441 av1_init_me_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000442#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -0700443 av1_rc_init_minq_luts();
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000444#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700445 av1_entropy_mv_init();
446 av1_encode_token_init();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700447#if CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -0700448 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700449#endif
450 init_done = 1;
451 }
452}
453
Yaowu Xuf883b422016-08-30 14:01:10 -0700454static void dealloc_compressor_data(AV1_COMP *cpi) {
455 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700456
Yaowu Xuf883b422016-08-30 14:01:10 -0700457 aom_free(cpi->mbmi_ext_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700458 cpi->mbmi_ext_base = NULL;
459
Yushin Cho77bba8d2016-11-04 16:36:56 -0700460#if CONFIG_PVQ
461 if (cpi->oxcf.pass != 1) {
Yushin Cho749c0572017-04-07 10:36:47 -0700462 const int tile_cols = cm->tile_cols;
463 const int tile_rows = cm->tile_rows;
Yushin Cho77bba8d2016-11-04 16:36:56 -0700464 int tile_col, tile_row;
465
466 for (tile_row = 0; tile_row < tile_rows; ++tile_row)
467 for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
468 TileDataEnc *tile_data =
469 &cpi->tile_data[tile_row * tile_cols + tile_col];
470 aom_free(tile_data->pvq_q.buf);
471 }
472 }
473#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700474 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475 cpi->tile_data = NULL;
476
477 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700478 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700479 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700480
Yaowu Xuf883b422016-08-30 14:01:10 -0700481 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700482 cpi->cyclic_refresh = NULL;
483
Yaowu Xuf883b422016-08-30 14:01:10 -0700484 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700485 cpi->active_map.map = NULL;
486
Jingning Hand064cf02017-06-01 10:00:39 -0700487#if CONFIG_MOTION_VAR
488 aom_free(cpi->td.mb.above_pred_buf);
489 cpi->td.mb.above_pred_buf = NULL;
490
491 aom_free(cpi->td.mb.left_pred_buf);
492 cpi->td.mb.left_pred_buf = NULL;
493
494 aom_free(cpi->td.mb.wsrc_buf);
495 cpi->td.mb.wsrc_buf = NULL;
496
497 aom_free(cpi->td.mb.mask_buf);
498 cpi->td.mb.mask_buf = NULL;
499#endif
500
Yaowu Xuf883b422016-08-30 14:01:10 -0700501 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700502#if CONFIG_LV_MAP
503 av1_free_txb_buf(cpi);
504#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700505 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700506
Yaowu Xuf883b422016-08-30 14:01:10 -0700507 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700508#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700509 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800510 aom_free_frame_buffer(&cpi->last_frame_db);
511 aom_free_frame_buffer(&cpi->trial_frame_rst);
512 aom_free(cpi->extra_rstbuf);
Timothy B. Terriberry5d24b6f2017-06-15 13:39:35 -0700513 {
514 int i;
515 for (i = 0; i < MAX_MB_PLANE; ++i)
516 av1_free_restoration_struct(&cpi->rst_search[i]);
517 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518#endif // CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700519 aom_free_frame_buffer(&cpi->scaled_source);
520 aom_free_frame_buffer(&cpi->scaled_last_source);
521 aom_free_frame_buffer(&cpi->alt_ref_buffer);
522 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700523
Yaowu Xuf883b422016-08-30 14:01:10 -0700524 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700525 cpi->tile_tok[0][0] = 0;
526
Yaowu Xuf883b422016-08-30 14:01:10 -0700527 av1_free_pc_tree(&cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700528
hui sud9a812b2017-07-06 14:34:37 -0700529 aom_free(cpi->td.mb.palette_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700530
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531#if CONFIG_ANS
Alex Converse1ac1ae72016-09-17 15:11:16 -0700532 aom_buf_ans_free(&cpi->buf_ans);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533#endif // CONFIG_ANS
534}
535
Yaowu Xuf883b422016-08-30 14:01:10 -0700536static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700538 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000539 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200541 // Stores a snapshot of key state variables which can subsequently be
542 // restored with a call to av1_restore_coding_context. These functions are
543 // intended for use in a re-code loop in av1_compress_frame where the
544 // quantizer value is adjusted between loop iterations.
James Zern01a9d702017-08-25 19:09:33 +0000545 for (i = 0; i < NMV_CONTEXTS; ++i) {
546 av1_copy(cc->nmv_vec_cost[i], cpi->td.mb.nmv_vec_cost[i]);
547 av1_copy(cc->nmv_costs, cpi->nmv_costs);
548 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
549 }
550
Yaowu Xuf883b422016-08-30 14:01:10 -0700551 av1_copy(cc->last_ref_lf_deltas, cm->lf.last_ref_deltas);
552 av1_copy(cc->last_mode_lf_deltas, cm->lf.last_mode_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700553
554 cc->fc = *cm->fc;
555}
556
Yaowu Xuf883b422016-08-30 14:01:10 -0700557static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700558 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700559 AV1_COMMON *cm = &cpi->common;
James Zern01a9d702017-08-25 19:09:33 +0000560 int i;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700561
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200562 // Restore key state variables to the snapshot state stored in the
563 // previous call to av1_save_coding_context.
James Zern01a9d702017-08-25 19:09:33 +0000564 for (i = 0; i < NMV_CONTEXTS; ++i) {
565 av1_copy(cpi->td.mb.nmv_vec_cost[i], cc->nmv_vec_cost[i]);
566 av1_copy(cpi->nmv_costs, cc->nmv_costs);
567 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
568 }
569
Yaowu Xuf883b422016-08-30 14:01:10 -0700570 av1_copy(cm->lf.last_ref_deltas, cc->last_ref_lf_deltas);
571 av1_copy(cm->lf.last_mode_deltas, cc->last_mode_lf_deltas);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700572
573 *cm->fc = cc->fc;
574}
575
Yaowu Xuf883b422016-08-30 14:01:10 -0700576static void configure_static_seg_features(AV1_COMP *cpi) {
577 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700578 const RATE_CONTROL *const rc = &cpi->rc;
579 struct segmentation *const seg = &cm->seg;
580
581 int high_q = (int)(rc->avg_q > 48.0);
582 int qi_delta;
583
584 // Disable and clear down for KF
585 if (cm->frame_type == KEY_FRAME) {
586 // Clear down the global segmentation map
587 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
588 seg->update_map = 0;
589 seg->update_data = 0;
590 cpi->static_mb_pct = 0;
591
592 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700593 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700594
595 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700596 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700597 } else if (cpi->refresh_alt_ref_frame) {
598 // If this is an alt ref frame
599 // Clear down the global segmentation map
600 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
601 seg->update_map = 0;
602 seg->update_data = 0;
603 cpi->static_mb_pct = 0;
604
605 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700606 av1_disable_segmentation(seg);
607 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608
609 // Scan frames from current to arf frame.
610 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700611 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700612
613 // If segmentation was enabled set those features needed for the
614 // arf itself.
615 if (seg->enabled) {
616 seg->update_map = 1;
617 seg->update_data = 1;
618
619 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700620 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
621 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
622 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623
Yaowu Xuf883b422016-08-30 14:01:10 -0700624 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
625 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626
627 // Where relevant assume segment data is delta data
628 seg->abs_delta = SEGMENT_DELTADATA;
629 }
630 } else if (seg->enabled) {
631 // All other frames if segmentation has been enabled
632
633 // First normal frame in a valid gf or alt ref group
634 if (rc->frames_since_golden == 0) {
635 // Set up segment features for normal frames in an arf group
636 if (rc->source_alt_ref_active) {
637 seg->update_map = 0;
638 seg->update_data = 1;
639 seg->abs_delta = SEGMENT_DELTADATA;
640
Yaowu Xuf883b422016-08-30 14:01:10 -0700641 qi_delta =
642 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
643 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
644 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700645
Yaowu Xuf883b422016-08-30 14:01:10 -0700646 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF, -2);
647 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700648
649 // Segment coding disabled for compred testing
650 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700651 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
652 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
653 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700654 }
655 } else {
656 // Disable segmentation and clear down features if alt ref
657 // is not active for this group
658
Yaowu Xuf883b422016-08-30 14:01:10 -0700659 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700660
661 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
662
663 seg->update_map = 0;
664 seg->update_data = 0;
665
Yaowu Xuf883b422016-08-30 14:01:10 -0700666 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700667 }
668 } else if (rc->is_src_frame_alt_ref) {
669 // Special case where we are coding over the top of a previous
670 // alt ref frame.
671 // Segment coding disabled for compred testing
672
673 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700674 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
675 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700676
677 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700678 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
679 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
680 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
681 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700682
683 // Skip all MBs if high Q (0,0 mv and skip coeffs)
684 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700685 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
686 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700687 }
688 // Enable data update
689 seg->update_data = 1;
690 } else {
691 // All other frames.
692
693 // No updates.. leave things as they are.
694 seg->update_map = 0;
695 seg->update_data = 0;
696 }
697 }
698}
699
Yaowu Xuf883b422016-08-30 14:01:10 -0700700static void update_reference_segmentation_map(AV1_COMP *cpi) {
701 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702 MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
703 uint8_t *cache_ptr = cm->last_frame_seg_map;
704 int row, col;
705
706 for (row = 0; row < cm->mi_rows; row++) {
707 MODE_INFO **mi_8x8 = mi_8x8_ptr;
708 uint8_t *cache = cache_ptr;
709 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
710 cache[0] = mi_8x8[0]->mbmi.segment_id;
711 mi_8x8_ptr += cm->mi_stride;
712 cache_ptr += cm->mi_cols;
713 }
714}
715
Yaowu Xuf883b422016-08-30 14:01:10 -0700716static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
717 AV1_COMMON *cm = &cpi->common;
718 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719
720 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700721 cpi->lookahead = av1_lookahead_init(oxcf->width, oxcf->height,
722 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200723#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -0700724 cm->use_highbitdepth,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700725#endif
Yaowu Xuf883b422016-08-30 14:01:10 -0700726 oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700728 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729 "Failed to allocate lag buffers");
730
731 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700732 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700733 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200734#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 cm->use_highbitdepth,
736#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700737 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
738 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740 "Failed to allocate altref buffer");
741}
742
Yaowu Xuf883b422016-08-30 14:01:10 -0700743static void alloc_util_frame_buffers(AV1_COMP *cpi) {
744 AV1_COMMON *const cm = &cpi->common;
745 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700746 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200747#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700748 cm->use_highbitdepth,
749#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700750 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
751 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700752 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700753 "Failed to allocate last frame buffer");
754
755#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -0700756 if (aom_realloc_frame_buffer(&cpi->last_frame_db, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700757 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200758#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700759 cm->use_highbitdepth,
760#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700761 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
762 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700763 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700764 "Failed to allocate last frame deblocked buffer");
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700765 if (aom_realloc_frame_buffer(
766 &cpi->trial_frame_rst,
767#if CONFIG_FRAME_SUPERRES
768 cm->superres_upscaled_width, cm->superres_upscaled_height,
769#else
770 cm->width, cm->height,
771#endif // CONFIG_FRAME_SUPERRES
772 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200773#if CONFIG_HIGHBITDEPTH
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700774 cm->use_highbitdepth,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800775#endif
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700776 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800777 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800778 "Failed to allocate trial restored frame buffer");
Alex Converse232e3842017-02-24 12:24:36 -0800779 int extra_rstbuf_sz = RESTORATION_EXTBUF_SIZE;
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -0800780 if (extra_rstbuf_sz > 0) {
Alex Converse7f094f12017-02-23 17:29:40 -0800781 aom_free(cpi->extra_rstbuf);
Alex Converse232e3842017-02-24 12:24:36 -0800782 CHECK_MEM_ERROR(cm, cpi->extra_rstbuf,
Alex Converse7f094f12017-02-23 17:29:40 -0800783 (uint8_t *)aom_malloc(extra_rstbuf_sz));
Debargha Mukherjeeb3c43bc2017-02-01 13:09:03 -0800784 } else {
785 cpi->extra_rstbuf = NULL;
786 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700787#endif // CONFIG_LOOP_RESTORATION
788
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200791#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700792 cm->use_highbitdepth,
793#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700794 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
795 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700796 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797 "Failed to allocate scaled source buffer");
798
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +0200801#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802 cm->use_highbitdepth,
803#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -0700804 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
805 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700806 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700807 "Failed to allocate scaled last source buffer");
808}
809
Yunqing Wangf5dc71e2017-05-04 18:20:17 -0700810static void alloc_context_buffers_ext(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812 int mi_size = cm->mi_cols * cm->mi_rows;
813
Yunqing Wangf5dc71e2017-05-04 18:20:17 -0700814 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
815 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700816}
817
Yaowu Xuf883b422016-08-30 14:01:10 -0700818void av1_alloc_compressor_data(AV1_COMP *cpi) {
819 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820
Yaowu Xuf883b422016-08-30 14:01:10 -0700821 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700823#if CONFIG_LV_MAP
824 av1_alloc_txb_buf(cpi);
825#endif
826
Yaowu Xuc27fc142016-08-22 16:08:15 -0700827 alloc_context_buffers_ext(cpi);
828
Yaowu Xuf883b422016-08-30 14:01:10 -0700829 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700830
831 {
832 unsigned int tokens = get_token_alloc(cm->mb_rows, cm->mb_cols);
833 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700834 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700835 }
836
Yaowu Xuf883b422016-08-30 14:01:10 -0700837 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700838}
839
Yaowu Xuf883b422016-08-30 14:01:10 -0700840void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700841 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000842#if CONFIG_XIPHRC
843 if (!cpi->od_rc.cur_frame) return;
844 cpi->od_rc.framerate = cpi->framerate;
845 od_enc_rc_resize(&cpi->od_rc);
846#else
Yaowu Xuf883b422016-08-30 14:01:10 -0700847 av1_rc_update_framerate(cpi);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +0000848#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849}
850
Yaowu Xuf883b422016-08-30 14:01:10 -0700851static void set_tile_info(AV1_COMP *cpi) {
852 AV1_COMMON *const cm = &cpi->common;
Thomas Daviesb25ba502017-07-18 10:18:24 +0100853#if CONFIG_DEPENDENT_HORZTILES
Fangwen Fu73126c02017-02-08 22:37:47 -0800854 int tile_row, tile_col, num_tiles_in_tg;
855 int tg_row_start, tg_col_start;
856#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700857#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700858 if (cpi->oxcf.large_scale_tile) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859#if CONFIG_EXT_PARTITION
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700860 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
861 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
862 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
863 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
864 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
865 } else {
866 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
867 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
868 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
869 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
870 }
871#else
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
873 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700874 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
875 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700876#endif // CONFIG_EXT_PARTITION
877
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700878 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
879 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700881 assert(cm->tile_width >> MAX_MIB_SIZE <= 32);
882 assert(cm->tile_height >> MAX_MIB_SIZE <= 32);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700884 // Get the number of tiles
885 cm->tile_cols = 1;
886 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700888 cm->tile_rows = 1;
889 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
890 } else {
891#endif // CONFIG_EXT_TILE
892 int min_log2_tile_cols, max_log2_tile_cols;
893 av1_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700894
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700895 cm->log2_tile_cols =
896 clamp(cpi->oxcf.tile_columns, min_log2_tile_cols, max_log2_tile_cols);
897 cm->log2_tile_rows = cpi->oxcf.tile_rows;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700898
Rupert Swarbrick9a3640d2017-09-01 13:54:41 +0100899 cm->tile_width = get_tile_size(cm->mi_cols, cm->log2_tile_cols);
900 cm->tile_height = get_tile_size(cm->mi_rows, cm->log2_tile_rows);
Rupert Swarbrick940942b2017-09-01 12:05:55 +0100901
902 const int max_cols = (cm->mi_cols + cm->tile_width - 1) / cm->tile_width;
903 const int max_rows = (cm->mi_rows + cm->tile_height - 1) / cm->tile_height;
904
905 cm->tile_cols = AOMMIN(1 << cm->log2_tile_cols, max_cols);
906 cm->tile_rows = AOMMIN(1 << cm->log2_tile_rows, max_rows);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700907#if CONFIG_EXT_TILE
908 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700909#endif // CONFIG_EXT_TILE
Ryan Lei7386eda2016-12-08 21:08:31 -0800910
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800911#if CONFIG_DEPENDENT_HORZTILES
912 cm->dependent_horz_tiles = cpi->oxcf.dependent_horz_tiles;
Fangwen Fu70bcb892017-05-06 17:05:19 -0700913#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700914 if (cm->large_scale_tile) {
915 // May not needed since cpi->oxcf.dependent_horz_tiles is already adjusted.
916 cm->dependent_horz_tiles = 0;
Fangwen Fu73126c02017-02-08 22:37:47 -0800917 } else {
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700918#endif // CONFIG_EXT_TILE
919 if (cm->log2_tile_rows == 0) cm->dependent_horz_tiles = 0;
920#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -0800921 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700922#endif // CONFIG_EXT_TILE
923
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700924#if CONFIG_EXT_TILE
925 if (!cm->large_scale_tile) {
926#endif // CONFIG_EXT_TILE
927 if (cpi->oxcf.mtu == 0) {
928 cm->num_tg = cpi->oxcf.num_tile_groups;
929 } else {
930 // Use a default value for the purposes of weighting costs in probability
931 // updates
932 cm->num_tg = DEFAULT_MAX_NUM_TG;
Fangwen Fu73126c02017-02-08 22:37:47 -0800933 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700934 num_tiles_in_tg =
935 (cm->tile_cols * cm->tile_rows + cm->num_tg - 1) / cm->num_tg;
936 tg_row_start = 0;
937 tg_col_start = 0;
938 for (tile_row = 0; tile_row < cm->tile_rows; ++tile_row) {
939 for (tile_col = 0; tile_col < cm->tile_cols; ++tile_col) {
940 if ((tile_row * cm->tile_cols + tile_col) % num_tiles_in_tg == 0) {
941 tg_row_start = tile_row;
942 tg_col_start = tile_col;
943 }
944 cm->tile_group_start_row[tile_row][tile_col] = tg_row_start;
945 cm->tile_group_start_col[tile_row][tile_col] = tg_col_start;
946 }
947 }
948#if CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -0800949 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700950#endif // CONFIG_EXT_TILE
Fangwen Fu73126c02017-02-08 22:37:47 -0800951#endif
Fangwen Fu7b9f2b32017-01-17 14:01:52 -0800952
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800953#if CONFIG_LOOPFILTERING_ACROSS_TILES
Ryan Lei7386eda2016-12-08 21:08:31 -0800954 cm->loop_filter_across_tiles_enabled =
955 cpi->oxcf.loop_filter_across_tiles_enabled;
Ryan Lei9b02b0e2017-01-30 15:52:20 -0800956#endif // CONFIG_LOOPFILTERING_ACROSS_TILES
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957}
958
Yaowu Xuf883b422016-08-30 14:01:10 -0700959static void update_frame_size(AV1_COMP *cpi) {
960 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700961 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
962
Yaowu Xuf883b422016-08-30 14:01:10 -0700963 av1_set_mb_mi(cm, cm->width, cm->height);
964 av1_init_context_buffers(cm);
Yushin Cho77bba8d2016-11-04 16:36:56 -0700965 av1_init_macroblockd(cm, xd,
966#if CONFIG_PVQ
967 NULL,
968#endif
Luc Trudeauf8164152017-04-11 16:20:51 -0400969#if CONFIG_CFL
970 &NULL_CFL,
971#endif
Yushin Cho77bba8d2016-11-04 16:36:56 -0700972 NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700973 memset(cpi->mbmi_ext_base, 0,
974 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
975
976 set_tile_info(cpi);
977}
978
Yaowu Xuf883b422016-08-30 14:01:10 -0700979static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700980#if CONFIG_EXT_REFS
981 int fb_idx;
982 for (fb_idx = 0; fb_idx < LAST_REF_FRAMES; ++fb_idx)
983 cpi->lst_fb_idxes[fb_idx] = fb_idx;
984 cpi->gld_fb_idx = LAST_REF_FRAMES;
985 cpi->bwd_fb_idx = LAST_REF_FRAMES + 1;
Zoe Liue9b15e22017-07-19 15:53:01 -0700986 cpi->alt2_fb_idx = LAST_REF_FRAMES + 2;
987 cpi->alt_fb_idx = LAST_REF_FRAMES + 3;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700988 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
989 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
Zoe Liue9b15e22017-07-19 15:53:01 -0700990#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -0700991 cpi->lst_fb_idx = 0;
992 cpi->gld_fb_idx = 1;
993 cpi->alt_fb_idx = 2;
994#endif // CONFIG_EXT_REFS
995}
996
Yaowu Xuf883b422016-08-30 14:01:10 -0700997static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
998 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700999
1000 cpi->oxcf = *oxcf;
1001 cpi->framerate = oxcf->init_framerate;
1002
1003 cm->profile = oxcf->profile;
1004 cm->bit_depth = oxcf->bit_depth;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001005#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001006 cm->use_highbitdepth = oxcf->use_highbitdepth;
1007#endif
1008 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07001009#if CONFIG_COLORSPACE_HEADERS
1010 cm->transfer_function = oxcf->transfer_function;
1011 cm->chroma_sample_position = oxcf->chroma_sample_position;
1012#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001013 cm->color_range = oxcf->color_range;
1014
1015 cm->width = oxcf->width;
1016 cm->height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07001017 av1_alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001018
1019 // Single thread case: use counts in common.
1020 cpi->td.counts = &cm->counts;
1021
1022 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001023 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001024
1025 cpi->static_mb_pct = 0;
1026 cpi->ref_frame_flags = 0;
1027
1028 init_buffer_indices(cpi);
1029}
1030
1031static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001032 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001033 const int64_t bandwidth = oxcf->target_bandwidth;
1034 const int64_t starting = oxcf->starting_buffer_level_ms;
1035 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1036 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1037
1038 rc->starting_buffer_level = starting * bandwidth / 1000;
1039 rc->optimal_buffer_level =
1040 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1041 rc->maximum_buffer_size =
1042 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1043}
1044
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02001045#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07001046#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
1047 cpi->fn_ptr[BT].sdf = SDF; \
1048 cpi->fn_ptr[BT].sdaf = SDAF; \
1049 cpi->fn_ptr[BT].vf = VF; \
1050 cpi->fn_ptr[BT].svf = SVF; \
1051 cpi->fn_ptr[BT].svaf = SVAF; \
1052 cpi->fn_ptr[BT].sdx3f = SDX3F; \
1053 cpi->fn_ptr[BT].sdx8f = SDX8F; \
1054 cpi->fn_ptr[BT].sdx4df = SDX4DF;
1055
1056#define MAKE_BFP_SAD_WRAPPER(fnname) \
1057 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1058 int source_stride, \
1059 const uint8_t *ref_ptr, int ref_stride) { \
1060 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1061 } \
1062 static unsigned int fnname##_bits10( \
1063 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1064 int ref_stride) { \
1065 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1066 } \
1067 static unsigned int fnname##_bits12( \
1068 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1069 int ref_stride) { \
1070 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1071 }
1072
1073#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1074 static unsigned int fnname##_bits8( \
1075 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1076 int ref_stride, const uint8_t *second_pred) { \
1077 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1078 } \
1079 static unsigned int fnname##_bits10( \
1080 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1081 int ref_stride, const uint8_t *second_pred) { \
1082 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1083 2; \
1084 } \
1085 static unsigned int fnname##_bits12( \
1086 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1087 int ref_stride, const uint8_t *second_pred) { \
1088 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1089 4; \
1090 }
1091
1092#define MAKE_BFP_SAD3_WRAPPER(fnname) \
1093 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1094 const uint8_t *ref_ptr, int ref_stride, \
1095 unsigned int *sad_array) { \
1096 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1097 } \
1098 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1099 const uint8_t *ref_ptr, int ref_stride, \
1100 unsigned int *sad_array) { \
1101 int i; \
1102 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1103 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1104 } \
1105 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1106 const uint8_t *ref_ptr, int ref_stride, \
1107 unsigned int *sad_array) { \
1108 int i; \
1109 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1110 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1111 }
1112
1113#define MAKE_BFP_SAD8_WRAPPER(fnname) \
1114 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1115 const uint8_t *ref_ptr, int ref_stride, \
1116 unsigned int *sad_array) { \
1117 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1118 } \
1119 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1120 const uint8_t *ref_ptr, int ref_stride, \
1121 unsigned int *sad_array) { \
1122 int i; \
1123 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1124 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1125 } \
1126 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1127 const uint8_t *ref_ptr, int ref_stride, \
1128 unsigned int *sad_array) { \
1129 int i; \
1130 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1131 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1132 }
1133#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1134 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1135 const uint8_t *const ref_ptr[], int ref_stride, \
1136 unsigned int *sad_array) { \
1137 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1138 } \
1139 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1140 const uint8_t *const ref_ptr[], int ref_stride, \
1141 unsigned int *sad_array) { \
1142 int i; \
1143 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1144 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1145 } \
1146 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1147 const uint8_t *const ref_ptr[], int ref_stride, \
1148 unsigned int *sad_array) { \
1149 int i; \
1150 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1151 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1152 }
1153
1154#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001155MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1156MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1157MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
1158MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
1159MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1160MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1161MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1162MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1163MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1164MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1165MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001166#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001167MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1168MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1169MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1170MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1171MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1172MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1173MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1174MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1175MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1176MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1177MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1178MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1179MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1180MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1181MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
1182MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
1183MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1184MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1185MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1186MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
1187MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
1188MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1189MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1190MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1191MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
1192MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
1193MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1194MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1195MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1196MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
1197MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
1198MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1199MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1200MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1201MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
1202MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
1203MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1204MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1205MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1206MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
1207MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
1208MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1209MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1210MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1211MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
1212MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1213MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1214MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1215MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
1216MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1217MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1218MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1219MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
1220MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
1221MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001222
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001223#if CONFIG_EXT_PARTITION_TYPES
1224MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1225MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1226MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1227MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1228MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1229MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1230MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1231MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1232MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1233MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1234MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1235MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001236MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1237MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1238MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1239MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1240MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1241MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001242#endif
1243
Yaowu Xuc27fc142016-08-22 16:08:15 -07001244#if CONFIG_EXT_INTER
David Barker0f3c94e2017-05-16 15:21:50 +01001245#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001246 cpi->fn_ptr[BT].msdf = MCSDF; \
1247 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001248
David Barkerc155e012017-05-11 13:54:54 +01001249#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1250 static unsigned int fnname##_bits8( \
1251 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1252 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1253 int m_stride, int invert_mask) { \
1254 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1255 second_pred_ptr, m, m_stride, invert_mask); \
1256 } \
1257 static unsigned int fnname##_bits10( \
1258 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1259 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1260 int m_stride, int invert_mask) { \
1261 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1262 second_pred_ptr, m, m_stride, invert_mask) >> \
1263 2; \
1264 } \
1265 static unsigned int fnname##_bits12( \
1266 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1267 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1268 int m_stride, int invert_mask) { \
1269 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1270 second_pred_ptr, m, m_stride, invert_mask) >> \
1271 4; \
1272 }
1273
Yaowu Xuc27fc142016-08-22 16:08:15 -07001274#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001275MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1276MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1277MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001278#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001279MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1280MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1281MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1282MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1283MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1284MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1285MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1286MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1287MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1288MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1289MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1290MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1291MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001292
1293#if CONFIG_EXT_PARTITION_TYPES
1294MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1295MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1296MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1297MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001298MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1299MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001300#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001301#endif // CONFIG_EXT_INTER
1302
Yue Chencb60b182016-10-13 15:18:22 -07001303#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001304#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1305 cpi->fn_ptr[BT].osdf = OSDF; \
1306 cpi->fn_ptr[BT].ovf = OVF; \
1307 cpi->fn_ptr[BT].osvf = OSVF;
1308
1309#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1310 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1311 const int32_t *wsrc, \
1312 const int32_t *msk) { \
1313 return fnname(ref, ref_stride, wsrc, msk); \
1314 } \
1315 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1316 const int32_t *wsrc, \
1317 const int32_t *msk) { \
1318 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1319 } \
1320 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1321 const int32_t *wsrc, \
1322 const int32_t *msk) { \
1323 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1324 }
1325
1326#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001327MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1328MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1329MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001330#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001331MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1332MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1333MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1334MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1335MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1336MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1337MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1338MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1339MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1340MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1341MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1342MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1343MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001344
1345#if CONFIG_EXT_PARTITION_TYPES
1346MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1347MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1348MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1349MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001350MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1351MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001352#endif
Yue Chencb60b182016-10-13 15:18:22 -07001353#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001354
Yaowu Xuf883b422016-08-30 14:01:10 -07001355static void highbd_set_var_fns(AV1_COMP *const cpi) {
1356 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001357 if (cm->use_highbitdepth) {
1358 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001359 case AOM_BITS_8:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001360#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001361 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1362 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1363 aom_highbd_8_sub_pixel_variance64x16,
1364 aom_highbd_8_sub_pixel_avg_variance64x16, NULL, NULL,
1365 aom_highbd_sad64x16x4d_bits8)
1366
1367 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1368 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1369 aom_highbd_8_sub_pixel_variance16x64,
1370 aom_highbd_8_sub_pixel_avg_variance16x64, NULL, NULL,
1371 aom_highbd_sad16x64x4d_bits8)
1372
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001373 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits8,
1374 aom_highbd_sad32x8_avg_bits8, aom_highbd_8_variance32x8,
1375 aom_highbd_8_sub_pixel_variance32x8,
1376 aom_highbd_8_sub_pixel_avg_variance32x8, NULL, NULL,
1377 aom_highbd_sad32x8x4d_bits8)
1378
1379 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits8,
1380 aom_highbd_sad8x32_avg_bits8, aom_highbd_8_variance8x32,
1381 aom_highbd_8_sub_pixel_variance8x32,
1382 aom_highbd_8_sub_pixel_avg_variance8x32, NULL, NULL,
1383 aom_highbd_sad8x32x4d_bits8)
1384
1385 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits8,
1386 aom_highbd_sad16x4_avg_bits8, aom_highbd_8_variance16x4,
1387 aom_highbd_8_sub_pixel_variance16x4,
1388 aom_highbd_8_sub_pixel_avg_variance16x4, NULL, NULL,
1389 aom_highbd_sad16x4x4d_bits8)
1390
1391 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits8,
1392 aom_highbd_sad4x16_avg_bits8, aom_highbd_8_variance4x16,
1393 aom_highbd_8_sub_pixel_variance4x16,
1394 aom_highbd_8_sub_pixel_avg_variance4x16, NULL, NULL,
1395 aom_highbd_sad4x16x4d_bits8)
1396#endif
1397
Yaowu Xuf883b422016-08-30 14:01:10 -07001398 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1399 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1400 aom_highbd_8_sub_pixel_variance32x16,
1401 aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1402 aom_highbd_sad32x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001403
Yaowu Xuf883b422016-08-30 14:01:10 -07001404 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1405 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1406 aom_highbd_8_sub_pixel_variance16x32,
1407 aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1408 aom_highbd_sad16x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001409
Yaowu Xuf883b422016-08-30 14:01:10 -07001410 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1411 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1412 aom_highbd_8_sub_pixel_variance64x32,
1413 aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1414 aom_highbd_sad64x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001415
Yaowu Xuf883b422016-08-30 14:01:10 -07001416 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1417 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1418 aom_highbd_8_sub_pixel_variance32x64,
1419 aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1420 aom_highbd_sad32x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001421
Yaowu Xuf883b422016-08-30 14:01:10 -07001422 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1423 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1424 aom_highbd_8_sub_pixel_variance32x32,
1425 aom_highbd_8_sub_pixel_avg_variance32x32,
1426 aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
1427 aom_highbd_sad32x32x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001428
Yaowu Xuf883b422016-08-30 14:01:10 -07001429 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1430 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1431 aom_highbd_8_sub_pixel_variance64x64,
1432 aom_highbd_8_sub_pixel_avg_variance64x64,
1433 aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
1434 aom_highbd_sad64x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001435
Yaowu Xuf883b422016-08-30 14:01:10 -07001436 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1437 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1438 aom_highbd_8_sub_pixel_variance16x16,
1439 aom_highbd_8_sub_pixel_avg_variance16x16,
1440 aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
1441 aom_highbd_sad16x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001442
1443 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001444 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1445 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1446 aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
1447 aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001448
1449 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001450 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1451 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1452 aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
1453 aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001454
1455 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001456 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1457 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1458 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits8,
1459 aom_highbd_sad8x8x8_bits8, aom_highbd_sad8x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001460
Yaowu Xuf883b422016-08-30 14:01:10 -07001461 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1462 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1463 aom_highbd_8_sub_pixel_variance8x4,
1464 aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
1465 aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001466
Yaowu Xuf883b422016-08-30 14:01:10 -07001467 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1468 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1469 aom_highbd_8_sub_pixel_variance4x8,
1470 aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
1471 aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001472
1473 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001474 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1475 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1476 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits8,
1477 aom_highbd_sad4x4x8_bits8, aom_highbd_sad4x4x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001478
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001479#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Hancc5bdf42016-12-19 11:14:30 -08001480 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_8_variance2x2, NULL, NULL,
1481 NULL, NULL, NULL)
1482 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_8_variance4x2, NULL, NULL,
1483 NULL, NULL, NULL)
1484 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_8_variance2x4, NULL, NULL,
1485 NULL, NULL, NULL)
1486#endif
1487
Yaowu Xuc27fc142016-08-22 16:08:15 -07001488#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001489 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1490 aom_highbd_sad128x128_avg_bits8,
1491 aom_highbd_8_variance128x128,
1492 aom_highbd_8_sub_pixel_variance128x128,
1493 aom_highbd_8_sub_pixel_avg_variance128x128,
1494 aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
1495 aom_highbd_sad128x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001496
Yaowu Xuf883b422016-08-30 14:01:10 -07001497 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1498 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1499 aom_highbd_8_sub_pixel_variance128x64,
1500 aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
1501 aom_highbd_sad128x64x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001502
Yaowu Xuf883b422016-08-30 14:01:10 -07001503 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1504 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1505 aom_highbd_8_sub_pixel_variance64x128,
1506 aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
1507 aom_highbd_sad64x128x4d_bits8)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001508#endif // CONFIG_EXT_PARTITION
1509
1510#if CONFIG_EXT_INTER
1511#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001512 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1513 aom_highbd_8_masked_sub_pixel_variance128x128)
1514 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1515 aom_highbd_8_masked_sub_pixel_variance128x64)
1516 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1517 aom_highbd_8_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001518#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001519 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1520 aom_highbd_8_masked_sub_pixel_variance64x64)
1521 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1522 aom_highbd_8_masked_sub_pixel_variance64x32)
1523 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1524 aom_highbd_8_masked_sub_pixel_variance32x64)
1525 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1526 aom_highbd_8_masked_sub_pixel_variance32x32)
1527 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1528 aom_highbd_8_masked_sub_pixel_variance32x16)
1529 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1530 aom_highbd_8_masked_sub_pixel_variance16x32)
1531 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1532 aom_highbd_8_masked_sub_pixel_variance16x16)
1533 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1534 aom_highbd_8_masked_sub_pixel_variance8x16)
1535 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1536 aom_highbd_8_masked_sub_pixel_variance16x8)
1537 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1538 aom_highbd_8_masked_sub_pixel_variance8x8)
1539 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1540 aom_highbd_8_masked_sub_pixel_variance4x8)
1541 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1542 aom_highbd_8_masked_sub_pixel_variance8x4)
1543 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1544 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001545#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001546 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1547 aom_highbd_8_masked_sub_pixel_variance64x16)
1548
1549 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1550 aom_highbd_8_masked_sub_pixel_variance16x64)
1551
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001552 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1553 aom_highbd_8_masked_sub_pixel_variance32x8)
1554
1555 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1556 aom_highbd_8_masked_sub_pixel_variance8x32)
1557
1558 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1559 aom_highbd_8_masked_sub_pixel_variance16x4)
1560
1561 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1562 aom_highbd_8_masked_sub_pixel_variance4x16)
1563#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001564#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07001565#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001566#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001567 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1568 aom_highbd_obmc_variance128x128,
1569 aom_highbd_obmc_sub_pixel_variance128x128)
1570 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1571 aom_highbd_obmc_variance128x64,
1572 aom_highbd_obmc_sub_pixel_variance128x64)
1573 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1574 aom_highbd_obmc_variance64x128,
1575 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001576#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001577 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1578 aom_highbd_obmc_variance64x64,
1579 aom_highbd_obmc_sub_pixel_variance64x64)
1580 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1581 aom_highbd_obmc_variance64x32,
1582 aom_highbd_obmc_sub_pixel_variance64x32)
1583 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1584 aom_highbd_obmc_variance32x64,
1585 aom_highbd_obmc_sub_pixel_variance32x64)
1586 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1587 aom_highbd_obmc_variance32x32,
1588 aom_highbd_obmc_sub_pixel_variance32x32)
1589 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1590 aom_highbd_obmc_variance32x16,
1591 aom_highbd_obmc_sub_pixel_variance32x16)
1592 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1593 aom_highbd_obmc_variance16x32,
1594 aom_highbd_obmc_sub_pixel_variance16x32)
1595 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1596 aom_highbd_obmc_variance16x16,
1597 aom_highbd_obmc_sub_pixel_variance16x16)
1598 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1599 aom_highbd_obmc_variance8x16,
1600 aom_highbd_obmc_sub_pixel_variance8x16)
1601 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1602 aom_highbd_obmc_variance16x8,
1603 aom_highbd_obmc_sub_pixel_variance16x8)
1604 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1605 aom_highbd_obmc_variance8x8,
1606 aom_highbd_obmc_sub_pixel_variance8x8)
1607 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1608 aom_highbd_obmc_variance4x8,
1609 aom_highbd_obmc_sub_pixel_variance4x8)
1610 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1611 aom_highbd_obmc_variance8x4,
1612 aom_highbd_obmc_sub_pixel_variance8x4)
1613 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1614 aom_highbd_obmc_variance4x4,
1615 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001616#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001617 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1618 aom_highbd_obmc_variance64x16,
1619 aom_highbd_obmc_sub_pixel_variance64x16)
1620
1621 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1622 aom_highbd_obmc_variance16x64,
1623 aom_highbd_obmc_sub_pixel_variance16x64)
1624
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001625 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1626 aom_highbd_obmc_variance32x8,
1627 aom_highbd_obmc_sub_pixel_variance32x8)
1628
1629 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1630 aom_highbd_obmc_variance8x32,
1631 aom_highbd_obmc_sub_pixel_variance8x32)
1632
1633 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1634 aom_highbd_obmc_variance16x4,
1635 aom_highbd_obmc_sub_pixel_variance16x4)
1636
1637 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1638 aom_highbd_obmc_variance4x16,
1639 aom_highbd_obmc_sub_pixel_variance4x16)
1640#endif
Yue Chencb60b182016-10-13 15:18:22 -07001641#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001642 break;
1643
Yaowu Xuf883b422016-08-30 14:01:10 -07001644 case AOM_BITS_10:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001645#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001646 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1647 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1648 aom_highbd_10_sub_pixel_variance64x16,
1649 aom_highbd_10_sub_pixel_avg_variance64x16, NULL, NULL,
1650 aom_highbd_sad64x16x4d_bits10)
1651
1652 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1653 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1654 aom_highbd_10_sub_pixel_variance16x64,
1655 aom_highbd_10_sub_pixel_avg_variance16x64, NULL, NULL,
1656 aom_highbd_sad16x64x4d_bits10)
1657
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001658 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1659 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1660 aom_highbd_10_sub_pixel_variance32x8,
1661 aom_highbd_10_sub_pixel_avg_variance32x8, NULL, NULL,
1662 aom_highbd_sad32x8x4d_bits10)
1663
1664 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1665 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1666 aom_highbd_10_sub_pixel_variance8x32,
1667 aom_highbd_10_sub_pixel_avg_variance8x32, NULL, NULL,
1668 aom_highbd_sad8x32x4d_bits10)
1669
1670 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1671 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1672 aom_highbd_10_sub_pixel_variance16x4,
1673 aom_highbd_10_sub_pixel_avg_variance16x4, NULL, NULL,
1674 aom_highbd_sad16x4x4d_bits10)
1675
1676 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1677 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1678 aom_highbd_10_sub_pixel_variance4x16,
1679 aom_highbd_10_sub_pixel_avg_variance4x16, NULL, NULL,
1680 aom_highbd_sad4x16x4d_bits10)
1681#endif
1682
Yaowu Xuf883b422016-08-30 14:01:10 -07001683 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1684 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1685 aom_highbd_10_sub_pixel_variance32x16,
1686 aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1687 aom_highbd_sad32x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001688
Yaowu Xuf883b422016-08-30 14:01:10 -07001689 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1690 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1691 aom_highbd_10_sub_pixel_variance16x32,
1692 aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1693 aom_highbd_sad16x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001694
Yaowu Xuf883b422016-08-30 14:01:10 -07001695 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1696 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1697 aom_highbd_10_sub_pixel_variance64x32,
1698 aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1699 aom_highbd_sad64x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001700
Yaowu Xuf883b422016-08-30 14:01:10 -07001701 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1702 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1703 aom_highbd_10_sub_pixel_variance32x64,
1704 aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1705 aom_highbd_sad32x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001706
Yaowu Xuf883b422016-08-30 14:01:10 -07001707 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1708 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1709 aom_highbd_10_sub_pixel_variance32x32,
1710 aom_highbd_10_sub_pixel_avg_variance32x32,
1711 aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
1712 aom_highbd_sad32x32x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001713
Yaowu Xuf883b422016-08-30 14:01:10 -07001714 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1715 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1716 aom_highbd_10_sub_pixel_variance64x64,
1717 aom_highbd_10_sub_pixel_avg_variance64x64,
1718 aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
1719 aom_highbd_sad64x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001720
Yaowu Xuf883b422016-08-30 14:01:10 -07001721 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1722 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1723 aom_highbd_10_sub_pixel_variance16x16,
1724 aom_highbd_10_sub_pixel_avg_variance16x16,
1725 aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
1726 aom_highbd_sad16x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001727
Yaowu Xuf883b422016-08-30 14:01:10 -07001728 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1729 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1730 aom_highbd_10_sub_pixel_variance16x8,
1731 aom_highbd_10_sub_pixel_avg_variance16x8,
1732 aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
1733 aom_highbd_sad16x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001734
Yaowu Xuf883b422016-08-30 14:01:10 -07001735 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1736 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1737 aom_highbd_10_sub_pixel_variance8x16,
1738 aom_highbd_10_sub_pixel_avg_variance8x16,
1739 aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
1740 aom_highbd_sad8x16x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001741
1742 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001743 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1744 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1745 aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
1746 aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001747
Yaowu Xuf883b422016-08-30 14:01:10 -07001748 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
1749 aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
1750 aom_highbd_10_sub_pixel_variance8x4,
1751 aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
1752 aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001753
Yaowu Xuf883b422016-08-30 14:01:10 -07001754 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
1755 aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
1756 aom_highbd_10_sub_pixel_variance4x8,
1757 aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
1758 aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001759
1760 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001761 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1762 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1763 aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
1764 aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001765
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07001766#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Hancc5bdf42016-12-19 11:14:30 -08001767 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_10_variance2x2, NULL, NULL,
1768 NULL, NULL, NULL)
1769 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_10_variance4x2, NULL, NULL,
1770 NULL, NULL, NULL)
1771 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_10_variance2x4, NULL, NULL,
1772 NULL, NULL, NULL)
1773#endif
1774
Yaowu Xuc27fc142016-08-22 16:08:15 -07001775#if CONFIG_EXT_PARTITION
1776 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07001777 BLOCK_128X128, aom_highbd_sad128x128_bits10,
1778 aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
1779 aom_highbd_10_sub_pixel_variance128x128,
1780 aom_highbd_10_sub_pixel_avg_variance128x128,
1781 aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
1782 aom_highbd_sad128x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001783
Yaowu Xuf883b422016-08-30 14:01:10 -07001784 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1785 aom_highbd_sad128x64_avg_bits10,
1786 aom_highbd_10_variance128x64,
1787 aom_highbd_10_sub_pixel_variance128x64,
1788 aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
1789 aom_highbd_sad128x64x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001790
Yaowu Xuf883b422016-08-30 14:01:10 -07001791 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1792 aom_highbd_sad64x128_avg_bits10,
1793 aom_highbd_10_variance64x128,
1794 aom_highbd_10_sub_pixel_variance64x128,
1795 aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
1796 aom_highbd_sad64x128x4d_bits10)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001797#endif // CONFIG_EXT_PARTITION
1798
1799#if CONFIG_EXT_INTER
1800#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001801 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1802 aom_highbd_10_masked_sub_pixel_variance128x128)
1803 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1804 aom_highbd_10_masked_sub_pixel_variance128x64)
1805 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1806 aom_highbd_10_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001807#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01001808 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1809 aom_highbd_10_masked_sub_pixel_variance64x64)
1810 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1811 aom_highbd_10_masked_sub_pixel_variance64x32)
1812 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1813 aom_highbd_10_masked_sub_pixel_variance32x64)
1814 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1815 aom_highbd_10_masked_sub_pixel_variance32x32)
1816 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1817 aom_highbd_10_masked_sub_pixel_variance32x16)
1818 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1819 aom_highbd_10_masked_sub_pixel_variance16x32)
1820 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1821 aom_highbd_10_masked_sub_pixel_variance16x16)
1822 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1823 aom_highbd_10_masked_sub_pixel_variance8x16)
1824 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1825 aom_highbd_10_masked_sub_pixel_variance16x8)
1826 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1827 aom_highbd_10_masked_sub_pixel_variance8x8)
1828 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1829 aom_highbd_10_masked_sub_pixel_variance4x8)
1830 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1831 aom_highbd_10_masked_sub_pixel_variance8x4)
1832 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1833 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001834#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001835 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1836 aom_highbd_10_masked_sub_pixel_variance64x16)
1837
1838 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1839 aom_highbd_10_masked_sub_pixel_variance16x64)
1840
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001841 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1842 aom_highbd_10_masked_sub_pixel_variance32x8)
1843
1844 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1845 aom_highbd_10_masked_sub_pixel_variance8x32)
1846
1847 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1848 aom_highbd_10_masked_sub_pixel_variance16x4)
1849
1850 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1851 aom_highbd_10_masked_sub_pixel_variance4x16)
1852#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001853#endif // CONFIG_EXT_INTER
Yue Chencb60b182016-10-13 15:18:22 -07001854#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001855#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001856 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1857 aom_highbd_10_obmc_variance128x128,
1858 aom_highbd_10_obmc_sub_pixel_variance128x128)
1859 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1860 aom_highbd_10_obmc_variance128x64,
1861 aom_highbd_10_obmc_sub_pixel_variance128x64)
1862 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1863 aom_highbd_10_obmc_variance64x128,
1864 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001865#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07001866 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1867 aom_highbd_10_obmc_variance64x64,
1868 aom_highbd_10_obmc_sub_pixel_variance64x64)
1869 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1870 aom_highbd_10_obmc_variance64x32,
1871 aom_highbd_10_obmc_sub_pixel_variance64x32)
1872 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
1873 aom_highbd_10_obmc_variance32x64,
1874 aom_highbd_10_obmc_sub_pixel_variance32x64)
1875 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
1876 aom_highbd_10_obmc_variance32x32,
1877 aom_highbd_10_obmc_sub_pixel_variance32x32)
1878 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
1879 aom_highbd_10_obmc_variance32x16,
1880 aom_highbd_10_obmc_sub_pixel_variance32x16)
1881 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
1882 aom_highbd_10_obmc_variance16x32,
1883 aom_highbd_10_obmc_sub_pixel_variance16x32)
1884 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
1885 aom_highbd_10_obmc_variance16x16,
1886 aom_highbd_10_obmc_sub_pixel_variance16x16)
1887 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
1888 aom_highbd_10_obmc_variance8x16,
1889 aom_highbd_10_obmc_sub_pixel_variance8x16)
1890 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
1891 aom_highbd_10_obmc_variance16x8,
1892 aom_highbd_10_obmc_sub_pixel_variance16x8)
1893 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
1894 aom_highbd_10_obmc_variance8x8,
1895 aom_highbd_10_obmc_sub_pixel_variance8x8)
1896 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
1897 aom_highbd_10_obmc_variance4x8,
1898 aom_highbd_10_obmc_sub_pixel_variance4x8)
1899 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
1900 aom_highbd_10_obmc_variance8x4,
1901 aom_highbd_10_obmc_sub_pixel_variance8x4)
1902 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
1903 aom_highbd_10_obmc_variance4x4,
1904 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001905#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001906 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
1907 aom_highbd_10_obmc_variance64x16,
1908 aom_highbd_10_obmc_sub_pixel_variance64x16)
1909
1910 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
1911 aom_highbd_10_obmc_variance16x64,
1912 aom_highbd_10_obmc_sub_pixel_variance16x64)
1913
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001914 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
1915 aom_highbd_10_obmc_variance32x8,
1916 aom_highbd_10_obmc_sub_pixel_variance32x8)
1917
1918 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
1919 aom_highbd_10_obmc_variance8x32,
1920 aom_highbd_10_obmc_sub_pixel_variance8x32)
1921
1922 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
1923 aom_highbd_10_obmc_variance16x4,
1924 aom_highbd_10_obmc_sub_pixel_variance16x4)
1925
1926 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
1927 aom_highbd_10_obmc_variance4x16,
1928 aom_highbd_10_obmc_sub_pixel_variance4x16)
1929#endif
Yue Chencb60b182016-10-13 15:18:22 -07001930#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07001931 break;
1932
Yaowu Xuf883b422016-08-30 14:01:10 -07001933 case AOM_BITS_12:
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001934#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01001935 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
1936 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
1937 aom_highbd_12_sub_pixel_variance64x16,
1938 aom_highbd_12_sub_pixel_avg_variance64x16, NULL, NULL,
1939 aom_highbd_sad64x16x4d_bits12)
1940
1941 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
1942 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
1943 aom_highbd_12_sub_pixel_variance16x64,
1944 aom_highbd_12_sub_pixel_avg_variance16x64, NULL, NULL,
1945 aom_highbd_sad16x64x4d_bits12)
1946
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001947 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
1948 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
1949 aom_highbd_12_sub_pixel_variance32x8,
1950 aom_highbd_12_sub_pixel_avg_variance32x8, NULL, NULL,
1951 aom_highbd_sad32x8x4d_bits12)
1952
1953 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
1954 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
1955 aom_highbd_12_sub_pixel_variance8x32,
1956 aom_highbd_12_sub_pixel_avg_variance8x32, NULL, NULL,
1957 aom_highbd_sad8x32x4d_bits12)
1958
1959 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
1960 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
1961 aom_highbd_12_sub_pixel_variance16x4,
1962 aom_highbd_12_sub_pixel_avg_variance16x4, NULL, NULL,
1963 aom_highbd_sad16x4x4d_bits12)
1964
1965 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
1966 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
1967 aom_highbd_12_sub_pixel_variance4x16,
1968 aom_highbd_12_sub_pixel_avg_variance4x16, NULL, NULL,
1969 aom_highbd_sad4x16x4d_bits12)
1970#endif
1971
Yaowu Xuf883b422016-08-30 14:01:10 -07001972 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
1973 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
1974 aom_highbd_12_sub_pixel_variance32x16,
1975 aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
1976 aom_highbd_sad32x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001977
Yaowu Xuf883b422016-08-30 14:01:10 -07001978 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
1979 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
1980 aom_highbd_12_sub_pixel_variance16x32,
1981 aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
1982 aom_highbd_sad16x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001983
Yaowu Xuf883b422016-08-30 14:01:10 -07001984 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
1985 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
1986 aom_highbd_12_sub_pixel_variance64x32,
1987 aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
1988 aom_highbd_sad64x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001989
Yaowu Xuf883b422016-08-30 14:01:10 -07001990 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
1991 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
1992 aom_highbd_12_sub_pixel_variance32x64,
1993 aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
1994 aom_highbd_sad32x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001995
Yaowu Xuf883b422016-08-30 14:01:10 -07001996 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
1997 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
1998 aom_highbd_12_sub_pixel_variance32x32,
1999 aom_highbd_12_sub_pixel_avg_variance32x32,
2000 aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
2001 aom_highbd_sad32x32x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002002
Yaowu Xuf883b422016-08-30 14:01:10 -07002003 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2004 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2005 aom_highbd_12_sub_pixel_variance64x64,
2006 aom_highbd_12_sub_pixel_avg_variance64x64,
2007 aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
2008 aom_highbd_sad64x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002009
Yaowu Xuf883b422016-08-30 14:01:10 -07002010 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2011 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2012 aom_highbd_12_sub_pixel_variance16x16,
2013 aom_highbd_12_sub_pixel_avg_variance16x16,
2014 aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
2015 aom_highbd_sad16x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002016
Yaowu Xuf883b422016-08-30 14:01:10 -07002017 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2018 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2019 aom_highbd_12_sub_pixel_variance16x8,
2020 aom_highbd_12_sub_pixel_avg_variance16x8,
2021 aom_highbd_sad16x8x3_bits12, aom_highbd_sad16x8x8_bits12,
2022 aom_highbd_sad16x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002023
Yaowu Xuf883b422016-08-30 14:01:10 -07002024 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2025 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2026 aom_highbd_12_sub_pixel_variance8x16,
2027 aom_highbd_12_sub_pixel_avg_variance8x16,
2028 aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
2029 aom_highbd_sad8x16x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002030
2031 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002032 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2033 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
2034 aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
2035 aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002036
Yaowu Xuf883b422016-08-30 14:01:10 -07002037 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
2038 aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
2039 aom_highbd_12_sub_pixel_variance8x4,
2040 aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
2041 aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002042
Yaowu Xuf883b422016-08-30 14:01:10 -07002043 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
2044 aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
2045 aom_highbd_12_sub_pixel_variance4x8,
2046 aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
2047 aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002048
2049 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002050 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2051 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
2052 aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
2053 aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002054
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07002055#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Hancc5bdf42016-12-19 11:14:30 -08002056 HIGHBD_BFP(BLOCK_2X2, NULL, NULL, aom_highbd_12_variance2x2, NULL, NULL,
2057 NULL, NULL, NULL)
2058 HIGHBD_BFP(BLOCK_4X2, NULL, NULL, aom_highbd_12_variance4x2, NULL, NULL,
2059 NULL, NULL, NULL)
2060 HIGHBD_BFP(BLOCK_2X4, NULL, NULL, aom_highbd_12_variance2x4, NULL, NULL,
2061 NULL, NULL, NULL)
2062#endif
2063
Yaowu Xuc27fc142016-08-22 16:08:15 -07002064#if CONFIG_EXT_PARTITION
2065 HIGHBD_BFP(
Yaowu Xuf883b422016-08-30 14:01:10 -07002066 BLOCK_128X128, aom_highbd_sad128x128_bits12,
2067 aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
2068 aom_highbd_12_sub_pixel_variance128x128,
2069 aom_highbd_12_sub_pixel_avg_variance128x128,
2070 aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
2071 aom_highbd_sad128x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002072
Yaowu Xuf883b422016-08-30 14:01:10 -07002073 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2074 aom_highbd_sad128x64_avg_bits12,
2075 aom_highbd_12_variance128x64,
2076 aom_highbd_12_sub_pixel_variance128x64,
2077 aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
2078 aom_highbd_sad128x64x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002079
Yaowu Xuf883b422016-08-30 14:01:10 -07002080 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2081 aom_highbd_sad64x128_avg_bits12,
2082 aom_highbd_12_variance64x128,
2083 aom_highbd_12_sub_pixel_variance64x128,
2084 aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
2085 aom_highbd_sad64x128x4d_bits12)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002086#endif // CONFIG_EXT_PARTITION
2087
2088#if CONFIG_EXT_INTER
2089#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002090 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2091 aom_highbd_12_masked_sub_pixel_variance128x128)
2092 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2093 aom_highbd_12_masked_sub_pixel_variance128x64)
2094 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2095 aom_highbd_12_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002096#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002097 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2098 aom_highbd_12_masked_sub_pixel_variance64x64)
2099 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2100 aom_highbd_12_masked_sub_pixel_variance64x32)
2101 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2102 aom_highbd_12_masked_sub_pixel_variance32x64)
2103 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2104 aom_highbd_12_masked_sub_pixel_variance32x32)
2105 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2106 aom_highbd_12_masked_sub_pixel_variance32x16)
2107 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2108 aom_highbd_12_masked_sub_pixel_variance16x32)
2109 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2110 aom_highbd_12_masked_sub_pixel_variance16x16)
2111 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2112 aom_highbd_12_masked_sub_pixel_variance8x16)
2113 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2114 aom_highbd_12_masked_sub_pixel_variance16x8)
2115 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2116 aom_highbd_12_masked_sub_pixel_variance8x8)
2117 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2118 aom_highbd_12_masked_sub_pixel_variance4x8)
2119 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2120 aom_highbd_12_masked_sub_pixel_variance8x4)
2121 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2122 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002123#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01002124 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2125 aom_highbd_12_masked_sub_pixel_variance64x16)
2126
2127 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2128 aom_highbd_12_masked_sub_pixel_variance16x64)
2129
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002130 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2131 aom_highbd_12_masked_sub_pixel_variance32x8)
2132
2133 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2134 aom_highbd_12_masked_sub_pixel_variance8x32)
2135
2136 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2137 aom_highbd_12_masked_sub_pixel_variance16x4)
2138
2139 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2140 aom_highbd_12_masked_sub_pixel_variance4x16)
2141#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002142#endif // CONFIG_EXT_INTER
2143
Yue Chencb60b182016-10-13 15:18:22 -07002144#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002145#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002146 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2147 aom_highbd_12_obmc_variance128x128,
2148 aom_highbd_12_obmc_sub_pixel_variance128x128)
2149 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2150 aom_highbd_12_obmc_variance128x64,
2151 aom_highbd_12_obmc_sub_pixel_variance128x64)
2152 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2153 aom_highbd_12_obmc_variance64x128,
2154 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002155#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002156 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2157 aom_highbd_12_obmc_variance64x64,
2158 aom_highbd_12_obmc_sub_pixel_variance64x64)
2159 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2160 aom_highbd_12_obmc_variance64x32,
2161 aom_highbd_12_obmc_sub_pixel_variance64x32)
2162 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2163 aom_highbd_12_obmc_variance32x64,
2164 aom_highbd_12_obmc_sub_pixel_variance32x64)
2165 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2166 aom_highbd_12_obmc_variance32x32,
2167 aom_highbd_12_obmc_sub_pixel_variance32x32)
2168 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2169 aom_highbd_12_obmc_variance32x16,
2170 aom_highbd_12_obmc_sub_pixel_variance32x16)
2171 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2172 aom_highbd_12_obmc_variance16x32,
2173 aom_highbd_12_obmc_sub_pixel_variance16x32)
2174 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2175 aom_highbd_12_obmc_variance16x16,
2176 aom_highbd_12_obmc_sub_pixel_variance16x16)
2177 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2178 aom_highbd_12_obmc_variance8x16,
2179 aom_highbd_12_obmc_sub_pixel_variance8x16)
2180 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2181 aom_highbd_12_obmc_variance16x8,
2182 aom_highbd_12_obmc_sub_pixel_variance16x8)
2183 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2184 aom_highbd_12_obmc_variance8x8,
2185 aom_highbd_12_obmc_sub_pixel_variance8x8)
2186 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2187 aom_highbd_12_obmc_variance4x8,
2188 aom_highbd_12_obmc_sub_pixel_variance4x8)
2189 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2190 aom_highbd_12_obmc_variance8x4,
2191 aom_highbd_12_obmc_sub_pixel_variance8x4)
2192 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2193 aom_highbd_12_obmc_variance4x4,
2194 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002195#if CONFIG_EXT_PARTITION_TYPES
Rupert Swarbrick72678572017-08-02 12:05:26 +01002196 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2197 aom_highbd_12_obmc_variance64x16,
2198 aom_highbd_12_obmc_sub_pixel_variance64x16)
2199
2200 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2201 aom_highbd_12_obmc_variance16x64,
2202 aom_highbd_12_obmc_sub_pixel_variance16x64)
2203
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002204 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2205 aom_highbd_12_obmc_variance32x8,
2206 aom_highbd_12_obmc_sub_pixel_variance32x8)
2207
2208 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2209 aom_highbd_12_obmc_variance8x32,
2210 aom_highbd_12_obmc_sub_pixel_variance8x32)
2211
2212 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2213 aom_highbd_12_obmc_variance16x4,
2214 aom_highbd_12_obmc_sub_pixel_variance16x4)
2215
2216 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2217 aom_highbd_12_obmc_variance4x16,
2218 aom_highbd_12_obmc_sub_pixel_variance4x16)
2219#endif
Yue Chencb60b182016-10-13 15:18:22 -07002220#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002221 break;
2222
2223 default:
2224 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07002225 "cm->bit_depth should be AOM_BITS_8, "
2226 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002227 }
2228 }
2229}
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002230#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002231
Yaowu Xuf883b422016-08-30 14:01:10 -07002232static void realloc_segmentation_maps(AV1_COMP *cpi) {
2233 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002234
2235 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002236 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002237 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002238 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002239
2240 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002241 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002242 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002243 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002244
2245 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002246 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002247 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002248 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002249}
2250
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002251#if CONFIG_EXT_INTER
2252void set_compound_tools(AV1_COMMON *cm) {
2253 (void)cm;
2254#if CONFIG_INTERINTRA
2255 cm->allow_interintra_compound = 1;
2256#endif // CONFIG_INTERINTRA
2257#if CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
2258 cm->allow_masked_compound = 1;
2259#endif // CONFIG_WEDGE || CONFIG_COMPOUND_SEGMENT
2260}
2261#endif // CONFIG_EXT_INTER
2262
Yaowu Xuf883b422016-08-30 14:01:10 -07002263void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2264 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002265 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002266 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002267
2268 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
2269 cm->bit_depth = oxcf->bit_depth;
2270 cm->color_space = oxcf->color_space;
anorkin76fb1262017-03-22 15:12:12 -07002271#if CONFIG_COLORSPACE_HEADERS
2272 cm->transfer_function = oxcf->transfer_function;
2273 cm->chroma_sample_position = oxcf->chroma_sample_position;
2274#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002275 cm->color_range = oxcf->color_range;
2276
2277 if (cm->profile <= PROFILE_1)
Yaowu Xuf883b422016-08-30 14:01:10 -07002278 assert(cm->bit_depth == AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002279 else
Yaowu Xuf883b422016-08-30 14:01:10 -07002280 assert(cm->bit_depth > AOM_BITS_8);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002281
2282 cpi->oxcf = *oxcf;
hui sud9a812b2017-07-06 14:34:37 -07002283 x->e_mbd.bd = (int)cm->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002284#if CONFIG_GLOBAL_MOTION
hui sud9a812b2017-07-06 14:34:37 -07002285 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002286#endif // CONFIG_GLOBAL_MOTION
2287
Yaowu Xuf883b422016-08-30 14:01:10 -07002288 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002289 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2290 } else {
2291 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2292 }
2293
2294 cpi->refresh_last_frame = 1;
2295 cpi->refresh_golden_frame = 0;
2296#if CONFIG_EXT_REFS
2297 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002298 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002299#endif // CONFIG_EXT_REFS
2300
2301 cm->refresh_frame_context =
2302 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
2303 ? REFRESH_FRAME_CONTEXT_FORWARD
2304 : REFRESH_FRAME_CONTEXT_BACKWARD;
Thomas Daedea6a854b2017-06-22 17:49:11 -07002305#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07002306 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07002307#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002308
Alex Converse74ad0912017-07-18 10:22:58 -07002309 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002310 CHECK_MEM_ERROR(cm, x->palette_buffer,
2311 aom_memalign(16, sizeof(*x->palette_buffer)));
2312 }
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07002313#if CONFIG_EXT_INTER
2314 set_compound_tools(cm);
2315#endif // CONFIG_EXT_INTER
Yaowu Xuf883b422016-08-30 14:01:10 -07002316 av1_reset_segment_features(cm);
James Zern01a9d702017-08-25 19:09:33 +00002317 av1_set_high_precision_mv(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002318
Yaowu Xuc27fc142016-08-22 16:08:15 -07002319 set_rc_buffer_sizes(rc, &cpi->oxcf);
2320
2321 // Under a configuration change, where maximum_buffer_size may change,
2322 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002323 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2324 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002325
2326 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002327 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002328
2329 // Set absolute upper and lower quality limits
2330 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2331 rc->best_quality = cpi->oxcf.best_allowed_q;
2332
2333 cm->interp_filter = cpi->sf.default_interp_filter;
2334
2335 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2336 cm->render_width = cpi->oxcf.render_width;
2337 cm->render_height = cpi->oxcf.render_height;
2338 } else {
2339 cm->render_width = cpi->oxcf.width;
2340 cm->render_height = cpi->oxcf.height;
2341 }
2342 cm->width = cpi->oxcf.width;
2343 cm->height = cpi->oxcf.height;
2344
2345 if (cpi->initial_width) {
2346 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002347 av1_free_context_buffers(cm);
2348 av1_alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002349 realloc_segmentation_maps(cpi);
2350 cpi->initial_width = cpi->initial_height = 0;
2351 }
2352 }
2353 update_frame_size(cpi);
2354
2355 cpi->alt_ref_source = NULL;
2356 rc->is_src_frame_alt_ref = 0;
2357
2358#if CONFIG_EXT_REFS
2359 rc->is_bwd_ref_frame = 0;
2360 rc->is_last_bipred_frame = 0;
2361 rc->is_bipred_frame = 0;
2362#endif // CONFIG_EXT_REFS
2363
2364#if 0
2365 // Experimental RD Code
2366 cpi->frame_distortion = 0;
2367 cpi->last_frame_distortion = 0;
2368#endif
2369
2370 set_tile_info(cpi);
2371
2372 cpi->ext_refresh_frame_flags_pending = 0;
2373 cpi->ext_refresh_frame_context_pending = 0;
2374
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002375#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002376 highbd_set_var_fns(cpi);
2377#endif
Alex Converseeb780e72016-12-13 12:46:41 -08002378#if CONFIG_ANS && ANS_MAX_SYMBOLS
2379 cpi->common.ans_window_size_log2 = cpi->oxcf.ans_window_size_log2;
Alex Converseeb780e72016-12-13 12:46:41 -08002380#endif // CONFIG_ANS && ANS_MAX_SYMBOLS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002381}
2382
Yaowu Xuf883b422016-08-30 14:01:10 -07002383AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2384 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002385 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002386 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2387 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002388
2389 if (!cm) return NULL;
2390
Yaowu Xuf883b422016-08-30 14:01:10 -07002391 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392
2393 if (setjmp(cm->error.jmp)) {
2394 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002395 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002396 return 0;
2397 }
2398
2399 cm->error.setjmp = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07002400 cm->alloc_mi = av1_enc_alloc_mi;
2401 cm->free_mi = av1_enc_free_mi;
2402 cm->setup_mi = av1_enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002403
Angie Chianga5d96c42016-10-21 16:16:56 -07002404 CHECK_MEM_ERROR(cm, cm->fc,
2405 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2406 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2407 (FRAME_CONTEXT *)aom_memalign(
2408 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2409 memset(cm->fc, 0, sizeof(*cm->fc));
2410 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002411
2412 cpi->resize_state = 0;
2413 cpi->resize_avg_qp = 0;
2414 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002415
Yaowu Xuc27fc142016-08-22 16:08:15 -07002416 cpi->common.buffer_pool = pool;
2417
2418 init_config(cpi, oxcf);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002419#if CONFIG_XIPHRC
2420 cpi->od_rc.framerate = cpi->framerate;
2421 cpi->od_rc.frame_width = cm->render_width;
2422 cpi->od_rc.frame_height = cm->render_height;
2423 cpi->od_rc.keyframe_rate = oxcf->key_freq;
2424 cpi->od_rc.goldenframe_rate = FIXED_GF_INTERVAL;
2425 cpi->od_rc.altref_rate = 25;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002426 cpi->od_rc.firstpass_quant = 1;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002427 cpi->od_rc.bit_depth = cm->bit_depth;
2428 cpi->od_rc.minq = oxcf->best_allowed_q;
2429 cpi->od_rc.maxq = oxcf->worst_allowed_q;
2430 if (cpi->oxcf.rc_mode == AOM_CQ) cpi->od_rc.minq = cpi->od_rc.quality;
2431 cpi->od_rc.quality = cpi->oxcf.rc_mode == AOM_Q ? oxcf->cq_level : -1;
2432 cpi->od_rc.periodic_boosts = oxcf->frame_periodic_boost;
2433 od_enc_rc_init(&cpi->od_rc,
2434 cpi->oxcf.rc_mode == AOM_Q ? -1 : oxcf->target_bandwidth,
2435 oxcf->maximum_buffer_size_ms);
2436#else
Yaowu Xuf883b422016-08-30 14:01:10 -07002437 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00002438#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002439
2440 cm->current_video_frame = 0;
2441 cpi->partition_search_skippable_frame = 0;
2442 cpi->tile_data = NULL;
2443 cpi->last_show_frame_buf_idx = INVALID_IDX;
2444
2445 realloc_segmentation_maps(cpi);
2446
James Zern01a9d702017-08-25 19:09:33 +00002447 for (i = 0; i < NMV_CONTEXTS; ++i) {
2448 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2449 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
2450 }
2451
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2453 i++) {
2454 CHECK_MEM_ERROR(
2455 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002456 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002457 }
2458
2459#if CONFIG_FP_MB_STATS
2460 cpi->use_fp_mb_stats = 0;
2461 if (cpi->use_fp_mb_stats) {
2462 // a place holder used to store the first pass mb stats in the first pass
2463 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002464 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465 } else {
2466 cpi->twopass.frame_mb_stats_buf = NULL;
2467 }
2468#endif
2469
2470 cpi->refresh_alt_ref_frame = 0;
2471 cpi->multi_arf_last_grp_enabled = 0;
2472
2473 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2474#if CONFIG_INTERNAL_STATS
2475 cpi->b_calculate_blockiness = 1;
2476 cpi->b_calculate_consistency = 1;
2477 cpi->total_inconsistency = 0;
2478 cpi->psnr.worst = 100.0;
2479 cpi->worst_ssim = 100.0;
2480
2481 cpi->count = 0;
2482 cpi->bytes = 0;
2483
2484 if (cpi->b_calculate_psnr) {
2485 cpi->total_sq_error = 0;
2486 cpi->total_samples = 0;
2487 cpi->tot_recode_hits = 0;
2488 cpi->summed_quality = 0;
2489 cpi->summed_weights = 0;
2490 }
2491
2492 cpi->fastssim.worst = 100.0;
2493 cpi->psnrhvs.worst = 100.0;
2494
2495 if (cpi->b_calculate_blockiness) {
2496 cpi->total_blockiness = 0;
2497 cpi->worst_blockiness = 0.0;
2498 }
2499
2500 if (cpi->b_calculate_consistency) {
2501 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002502 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002503 cpi->common.mi_rows * cpi->common.mi_cols));
2504 cpi->worst_consistency = 100.0;
2505 }
2506#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002507#if CONFIG_ENTROPY_STATS
2508 av1_zero(aggregate_fc);
Zoe Liua56f9162017-06-21 22:49:57 -07002509 av1_zero_array(aggregate_fc_per_type, FRAME_CONTEXTS);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002510#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002511
2512 cpi->first_time_stamp_ever = INT64_MAX;
2513
James Zern01a9d702017-08-25 19:09:33 +00002514 for (i = 0; i < NMV_CONTEXTS; ++i) {
2515 cpi->td.mb.nmvcost[i][0] = &cpi->nmv_costs[i][0][MV_MAX];
2516 cpi->td.mb.nmvcost[i][1] = &cpi->nmv_costs[i][1][MV_MAX];
2517 cpi->td.mb.nmvcost_hp[i][0] = &cpi->nmv_costs_hp[i][0][MV_MAX];
2518 cpi->td.mb.nmvcost_hp[i][1] = &cpi->nmv_costs_hp[i][1][MV_MAX];
2519 }
2520
Yaowu Xuc27fc142016-08-22 16:08:15 -07002521#ifdef OUTPUT_YUV_SKINMAP
2522 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2523#endif
2524#ifdef OUTPUT_YUV_REC
2525 yuv_rec_file = fopen("rec.yuv", "wb");
2526#endif
2527
2528#if 0
2529 framepsnr = fopen("framepsnr.stt", "a");
2530 kf_list = fopen("kf_list.stt", "w");
2531#endif
2532
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002533#if CONFIG_XIPHRC
2534 if (oxcf->pass == 2) {
2535 cpi->od_rc.twopass_allframes_buf = oxcf->two_pass_stats_in.buf;
2536 cpi->od_rc.twopass_allframes_buf_size = oxcf->two_pass_stats_in.sz;
2537 }
2538#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07002539 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002540 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002541 } else if (oxcf->pass == 2) {
2542 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2543 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2544
2545#if CONFIG_FP_MB_STATS
2546 if (cpi->use_fp_mb_stats) {
2547 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2548 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2549
2550 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2551 oxcf->firstpass_mb_stats_in.buf;
2552 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2553 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2554 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2555 }
2556#endif
2557
2558 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2559 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2560 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2561
Yaowu Xuf883b422016-08-30 14:01:10 -07002562 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002563 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00002564#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002565
Jingning Hand064cf02017-06-01 10:00:39 -07002566#if CONFIG_MOTION_VAR
2567#if CONFIG_HIGHBITDEPTH
2568 int buf_scaler = 2;
2569#else
2570 int buf_scaler = 1;
2571#endif
2572 CHECK_MEM_ERROR(
2573 cm, cpi->td.mb.above_pred_buf,
clang-format4eafefe2017-09-04 12:51:20 -07002574 (uint8_t *)aom_memalign(16,
2575 buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2576 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002577 CHECK_MEM_ERROR(
2578 cm, cpi->td.mb.left_pred_buf,
clang-format4eafefe2017-09-04 12:51:20 -07002579 (uint8_t *)aom_memalign(16,
2580 buf_scaler * MAX_MB_PLANE * MAX_SB_SQUARE *
2581 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002582
2583 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2584 (int32_t *)aom_memalign(
2585 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2586
2587 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2588 (int32_t *)aom_memalign(
2589 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2590
2591#endif
2592
Yaowu Xuf883b422016-08-30 14:01:10 -07002593 av1_set_speed_features_framesize_independent(cpi);
2594 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002595
Yaowu Xuc27fc142016-08-22 16:08:15 -07002596#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF) \
2597 cpi->fn_ptr[BT].sdf = SDF; \
2598 cpi->fn_ptr[BT].sdaf = SDAF; \
2599 cpi->fn_ptr[BT].vf = VF; \
2600 cpi->fn_ptr[BT].svf = SVF; \
2601 cpi->fn_ptr[BT].svaf = SVAF; \
2602 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2603 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2604 cpi->fn_ptr[BT].sdx4df = SDX4DF;
2605
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002606#if CONFIG_EXT_PARTITION_TYPES
2607 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2608 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, NULL, NULL,
2609 aom_sad4x16x4d)
2610
2611 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2612 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, NULL, NULL,
2613 aom_sad16x4x4d)
2614
2615 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2616 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, NULL, NULL,
2617 aom_sad8x32x4d)
2618
2619 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2620 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, NULL, NULL,
2621 aom_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002622
2623 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2624 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, NULL, NULL,
2625 aom_sad16x64x4d)
2626
2627 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2628 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, NULL, NULL,
2629 aom_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002630#endif
2631
Yaowu Xuc27fc142016-08-22 16:08:15 -07002632#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002633 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2634 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2635 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002636
Yaowu Xuf883b422016-08-30 14:01:10 -07002637 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2638 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
2639 NULL, aom_sad128x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002640
Yaowu Xuf883b422016-08-30 14:01:10 -07002641 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2642 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
2643 NULL, aom_sad64x128x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002644#endif // CONFIG_EXT_PARTITION
2645
Yaowu Xuf883b422016-08-30 14:01:10 -07002646 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2647 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
2648 aom_sad32x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002649
Yaowu Xuf883b422016-08-30 14:01:10 -07002650 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2651 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
2652 aom_sad16x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653
Yaowu Xuf883b422016-08-30 14:01:10 -07002654 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2655 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
2656 aom_sad64x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002657
Yaowu Xuf883b422016-08-30 14:01:10 -07002658 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2659 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
2660 aom_sad32x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002661
Yaowu Xuf883b422016-08-30 14:01:10 -07002662 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2663 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
2664 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665
Yaowu Xuf883b422016-08-30 14:01:10 -07002666 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2667 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
2668 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002669
Yaowu Xuf883b422016-08-30 14:01:10 -07002670 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2671 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
2672 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002673
Yaowu Xuf883b422016-08-30 14:01:10 -07002674 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2675 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
2676 aom_sad16x8x8, aom_sad16x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002677
Yaowu Xuf883b422016-08-30 14:01:10 -07002678 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2679 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
2680 aom_sad8x16x8, aom_sad8x16x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681
Yaowu Xuf883b422016-08-30 14:01:10 -07002682 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2683 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
2684 aom_sad8x8x8, aom_sad8x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002685
Yaowu Xuf883b422016-08-30 14:01:10 -07002686 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2687 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
2688 aom_sad8x4x8, aom_sad8x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002689
Yaowu Xuf883b422016-08-30 14:01:10 -07002690 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2691 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
2692 aom_sad4x8x8, aom_sad4x8x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002693
Yaowu Xuf883b422016-08-30 14:01:10 -07002694 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2695 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
2696 aom_sad4x4x8, aom_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002697
Timothy B. Terriberry81ec2612017-04-26 16:53:47 -07002698#if CONFIG_CHROMA_2X2 || CONFIG_CHROMA_SUB8X8
Jingning Han9e7c49f2016-12-06 11:20:10 -08002699 BFP(BLOCK_2X2, NULL, NULL, aom_variance2x2, NULL, NULL, NULL, NULL, NULL)
Jingning Hane2ffaf82016-12-14 15:26:30 -08002700 BFP(BLOCK_2X4, NULL, NULL, aom_variance2x4, NULL, NULL, NULL, NULL, NULL)
2701 BFP(BLOCK_4X2, NULL, NULL, aom_variance4x2, NULL, NULL, NULL, NULL, NULL)
Jingning Han9e7c49f2016-12-06 11:20:10 -08002702#endif
2703
Yue Chencb60b182016-10-13 15:18:22 -07002704#if CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002705#define OBFP(BT, OSDF, OVF, OSVF) \
2706 cpi->fn_ptr[BT].osdf = OSDF; \
2707 cpi->fn_ptr[BT].ovf = OVF; \
2708 cpi->fn_ptr[BT].osvf = OSVF;
2709
2710#if CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002711 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2712 aom_obmc_sub_pixel_variance128x128)
2713 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2714 aom_obmc_sub_pixel_variance128x64)
2715 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2716 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002717#endif // CONFIG_EXT_PARTITION
Yaowu Xuf883b422016-08-30 14:01:10 -07002718 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2719 aom_obmc_sub_pixel_variance64x64)
2720 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2721 aom_obmc_sub_pixel_variance64x32)
2722 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2723 aom_obmc_sub_pixel_variance32x64)
2724 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2725 aom_obmc_sub_pixel_variance32x32)
2726 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2727 aom_obmc_sub_pixel_variance32x16)
2728 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2729 aom_obmc_sub_pixel_variance16x32)
2730 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2731 aom_obmc_sub_pixel_variance16x16)
2732 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2733 aom_obmc_sub_pixel_variance16x8)
2734 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2735 aom_obmc_sub_pixel_variance8x16)
2736 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2737 aom_obmc_sub_pixel_variance8x8)
2738 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2739 aom_obmc_sub_pixel_variance4x8)
2740 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2741 aom_obmc_sub_pixel_variance8x4)
2742 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2743 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002744
2745#if CONFIG_EXT_PARTITION_TYPES
2746 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2747 aom_obmc_sub_pixel_variance4x16)
2748
2749 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2750 aom_obmc_sub_pixel_variance16x4)
2751
2752 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2753 aom_obmc_sub_pixel_variance8x32)
2754
2755 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2756 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002757
2758 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2759 aom_obmc_sub_pixel_variance16x64)
2760
2761 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2762 aom_obmc_sub_pixel_variance64x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002763#endif
Yue Chencb60b182016-10-13 15:18:22 -07002764#endif // CONFIG_MOTION_VAR
Yaowu Xuc27fc142016-08-22 16:08:15 -07002765
2766#if CONFIG_EXT_INTER
David Barkerf19f35f2017-05-22 16:33:22 +01002767#define MBFP(BT, MCSDF, MCSVF) \
2768 cpi->fn_ptr[BT].msdf = MCSDF; \
2769 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002770
2771#if CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002772 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2773 aom_masked_sub_pixel_variance128x128)
2774 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2775 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002776#endif // CONFIG_EXT_PARTITION
David Barkerf19f35f2017-05-22 16:33:22 +01002777 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2778 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2779 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2780 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2781 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2782 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2783 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2784 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2785 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2786 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2787 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2788 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2789 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002790
2791#if CONFIG_EXT_PARTITION_TYPES
2792 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2793
2794 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2795
2796 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2797
2798 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002799
2800 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2801
2802 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002803#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002804#endif // CONFIG_EXT_INTER
2805
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02002806#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002807 highbd_set_var_fns(cpi);
2808#endif
2809
Yaowu Xuf883b422016-08-30 14:01:10 -07002810 /* av1_init_quantizer() is first called here. Add check in
2811 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002812 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002813 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002814 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002815 av1_init_quantizer(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002816#if CONFIG_AOM_QM
2817 aom_qm_init(cm);
2818#endif
2819
Yaowu Xuf883b422016-08-30 14:01:10 -07002820 av1_loop_filter_init(cm);
Fergus Simpsondac6aff2017-05-18 15:11:38 -07002821#if CONFIG_FRAME_SUPERRES
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07002822 cm->superres_scale_numerator = SCALE_DENOMINATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002823 cm->superres_upscaled_width = oxcf->width;
2824 cm->superres_upscaled_height = oxcf->height;
Fergus Simpsondac6aff2017-05-18 15:11:38 -07002825#endif // CONFIG_FRAME_SUPERRES
Yaowu Xuc27fc142016-08-22 16:08:15 -07002826#if CONFIG_LOOP_RESTORATION
Yaowu Xuf883b422016-08-30 14:01:10 -07002827 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002828#endif // CONFIG_LOOP_RESTORATION
2829
2830 cm->error.setjmp = 0;
2831
2832 return cpi;
2833}
2834
2835#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2836
2837#define SNPRINT2(H, T, V) \
2838 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2839
Yaowu Xuf883b422016-08-30 14:01:10 -07002840void av1_remove_compressor(AV1_COMP *cpi) {
2841 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002842 unsigned int i;
2843 int t;
2844
2845 if (!cpi) return;
2846
2847 cm = &cpi->common;
2848 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002849#if CONFIG_ENTROPY_STATS
2850 if (cpi->oxcf.pass != 1) {
2851 fprintf(stderr, "Writing counts.stt\n");
2852 FILE *f = fopen("counts.stt", "wb");
2853 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
Zoe Liua56f9162017-06-21 22:49:57 -07002854 fwrite(aggregate_fc_per_type, sizeof(aggregate_fc_per_type[0]),
2855 FRAME_CONTEXTS, f);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002856 fclose(f);
2857 }
2858#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002859#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07002860 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002861
2862 if (cpi->oxcf.pass != 1) {
2863 char headings[512] = { 0 };
2864 char results[512] = { 0 };
2865 FILE *f = fopen("opsnr.stt", "a");
2866 double time_encoded =
2867 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2868 10000000.000;
2869 double total_encode_time =
2870 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2871 const double dr =
2872 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2873 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2874 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2875 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2876
2877 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002878 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002879 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2880 const double total_ssim =
2881 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2882 snprintf(headings, sizeof(headings),
2883 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07002884 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Yaowu Xuc27fc142016-08-22 16:08:15 -07002885 "WstPsnr\tWstSsim\tWstFast\tWstHVS");
2886 snprintf(results, sizeof(results),
2887 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2888 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2889 "%7.3f\t%7.3f\t%7.3f\t%7.3f",
2890 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
2891 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
2892 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2893 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
2894 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst);
2895
2896 if (cpi->b_calculate_blockiness) {
2897 SNPRINT(headings, "\t Block\tWstBlck");
2898 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2899 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2900 }
2901
2902 if (cpi->b_calculate_consistency) {
2903 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07002904 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002905 (double)cpi->total_inconsistency);
2906
2907 SNPRINT(headings, "\tConsist\tWstCons");
2908 SNPRINT2(results, "\t%7.3f", consistency);
2909 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2910 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07002911 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2912 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002913 rate_err, fabs(rate_err));
2914 }
2915
2916 fclose(f);
2917 }
2918
2919#endif
2920
2921#if 0
2922 {
2923 printf("\n_pick_loop_filter_level:%d\n", cpi->time_pick_lpf / 1000);
2924 printf("\n_frames recive_data encod_mb_row compress_frame Total\n");
2925 printf("%6d %10ld %10ld %10ld %10ld\n", cpi->common.current_video_frame,
2926 cpi->time_receive_data / 1000, cpi->time_encode_sb_row / 1000,
2927 cpi->time_compress_data / 1000,
2928 (cpi->time_receive_data + cpi->time_compress_data) / 1000);
2929 }
2930#endif
2931 }
2932
2933 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002934 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002935 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2936
2937 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07002938 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002939
2940 // Deallocate allocated thread data.
2941 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07002942 aom_free(thread_data->td->palette_buffer);
Jingning Hand064cf02017-06-01 10:00:39 -07002943#if CONFIG_MOTION_VAR
2944 aom_free(thread_data->td->above_pred_buf);
2945 aom_free(thread_data->td->left_pred_buf);
2946 aom_free(thread_data->td->wsrc_buf);
2947 aom_free(thread_data->td->mask_buf);
2948#endif // CONFIG_MOTION_VAR
Yaowu Xuf883b422016-08-30 14:01:10 -07002949 aom_free(thread_data->td->counts);
2950 av1_free_pc_tree(thread_data->td);
Yaowu Xuf883b422016-08-30 14:01:10 -07002951 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002952 }
2953 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002954 aom_free(cpi->tile_thr_data);
2955 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002956
Yaowu Xuf883b422016-08-30 14:01:10 -07002957 if (cpi->num_workers > 1) av1_loop_filter_dealloc(&cpi->lf_row_sync);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002958
2959 dealloc_compressor_data(cpi);
2960
2961 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2962 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002963 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002964 }
2965
2966#if CONFIG_FP_MB_STATS
2967 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002968 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969 cpi->twopass.frame_mb_stats_buf = NULL;
2970 }
2971#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08002972#if CONFIG_INTERNAL_STATS
2973 aom_free(cpi->ssim_vars);
2974 cpi->ssim_vars = NULL;
2975#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002976
Yaowu Xuf883b422016-08-30 14:01:10 -07002977 av1_remove_common(cm);
2978 av1_free_ref_frame_buffers(cm->buffer_pool);
2979 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002980
2981#ifdef OUTPUT_YUV_SKINMAP
2982 fclose(yuv_skinmap_file);
2983#endif
2984#ifdef OUTPUT_YUV_REC
2985 fclose(yuv_rec_file);
2986#endif
2987
2988#if 0
2989
2990 if (keyfile)
2991 fclose(keyfile);
2992
2993 if (framepsnr)
2994 fclose(framepsnr);
2995
2996 if (kf_list)
2997 fclose(kf_list);
2998
2999#endif
3000}
3001
Yaowu Xuf883b422016-08-30 14:01:10 -07003002static void generate_psnr_packet(AV1_COMP *cpi) {
3003 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003004 int i;
3005 PSNR_STATS psnr;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003006#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07003007 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003008 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3009#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07003010 aom_calc_psnr(cpi->source, cpi->common.frame_to_show, &psnr);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003011#endif
3012
3013 for (i = 0; i < 4; ++i) {
3014 pkt.data.psnr.samples[i] = psnr.samples[i];
3015 pkt.data.psnr.sse[i] = psnr.sse[i];
3016 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3017 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003018 pkt.kind = AOM_CODEC_PSNR_PKT;
3019 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003020}
3021
Yaowu Xuf883b422016-08-30 14:01:10 -07003022int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003023 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3024
3025 cpi->ref_frame_flags = ref_frame_flags;
3026 return 0;
3027}
3028
Yaowu Xuf883b422016-08-30 14:01:10 -07003029void av1_update_reference(AV1_COMP *cpi, int ref_frame_flags) {
3030 cpi->ext_refresh_golden_frame = (ref_frame_flags & AOM_GOLD_FLAG) != 0;
3031 cpi->ext_refresh_alt_ref_frame = (ref_frame_flags & AOM_ALT_FLAG) != 0;
3032 cpi->ext_refresh_last_frame = (ref_frame_flags & AOM_LAST_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003033 cpi->ext_refresh_frame_flags_pending = 1;
3034}
3035
Thomas Daede497d1952017-08-08 17:33:06 -07003036int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3037 AV1_COMMON *const cm = &cpi->common;
3038 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003039 if (cfg) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003040 aom_yv12_copy_frame(cfg, sd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003041 return 0;
3042 } else {
3043 return -1;
3044 }
3045}
3046
Thomas Daede497d1952017-08-08 17:33:06 -07003047int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3048 AV1_COMMON *const cm = &cpi->common;
3049 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003050 if (cfg) {
3051 aom_yv12_copy_frame(sd, cfg);
3052 return 0;
3053 } else {
3054 return -1;
3055 }
3056}
3057
3058int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003059 cpi->ext_refresh_frame_context = update;
3060 cpi->ext_refresh_frame_context_pending = 1;
3061 return 0;
3062}
3063
3064#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3065// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3066// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3067// not denoise the UV channels at this time. If ever we implement UV channel
3068// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003069void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003070 uint8_t *src = s->y_buffer;
3071 int h = s->y_height;
3072
3073 do {
3074 fwrite(src, s->y_width, 1, f);
3075 src += s->y_stride;
3076 } while (--h);
3077
3078 src = s->u_buffer;
3079 h = s->uv_height;
3080
3081 do {
3082 fwrite(src, s->uv_width, 1, f);
3083 src += s->uv_stride;
3084 } while (--h);
3085
3086 src = s->v_buffer;
3087 h = s->uv_height;
3088
3089 do {
3090 fwrite(src, s->uv_width, 1, f);
3091 src += s->uv_stride;
3092 } while (--h);
3093}
3094#endif
3095
Zoe Liue04abf72017-04-19 15:37:11 -07003096#if CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07003097static void check_show_existing_frame(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003098 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003099 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003100 const FRAME_UPDATE_TYPE next_frame_update_type =
3101 gf_group->update_type[gf_group->index];
3102 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07003103
3104 if (cm->show_existing_frame == 1) {
3105 cm->show_existing_frame = 0;
3106 } else if (cpi->rc.is_last_bipred_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003107 // NOTE(zoeliu): If the current frame is a last bi-predictive frame, it is
3108 // needed next to show the BWDREF_FRAME, which is pointed by
3109 // the last_fb_idxes[0] after reference frame buffer update
3110 cpi->rc.is_last_bipred_frame = 0;
3111 cm->show_existing_frame = 1;
3112 cpi->existing_fb_idx_to_show = cpi->lst_fb_idxes[0];
3113 } else if (cpi->is_arf_filter_off[which_arf] &&
3114 (next_frame_update_type == OVERLAY_UPDATE ||
3115 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
3116 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003117 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003118 cm->show_existing_frame = 1;
3119 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003120 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
3121 ? cpi->alt_fb_idx
3122 : cpi->alt2_fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003123 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003124 }
3125 cpi->rc.is_src_frame_ext_arf = 0;
3126}
Zoe Liue04abf72017-04-19 15:37:11 -07003127#endif // CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07003128
3129#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003130void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003131 uint8_t *src = s->y_buffer;
3132 int h = cm->height;
3133
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003134#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003135 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3136 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3137
3138 do {
3139 fwrite(src16, s->y_width, 2, yuv_rec_file);
3140 src16 += s->y_stride;
3141 } while (--h);
3142
3143 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3144 h = s->uv_height;
3145
3146 do {
3147 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3148 src16 += s->uv_stride;
3149 } while (--h);
3150
3151 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3152 h = s->uv_height;
3153
3154 do {
3155 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3156 src16 += s->uv_stride;
3157 } while (--h);
3158
3159 fflush(yuv_rec_file);
3160 return;
3161 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003162#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003163
3164 do {
3165 fwrite(src, s->y_width, 1, yuv_rec_file);
3166 src += s->y_stride;
3167 } while (--h);
3168
3169 src = s->u_buffer;
3170 h = s->uv_height;
3171
3172 do {
3173 fwrite(src, s->uv_width, 1, yuv_rec_file);
3174 src += s->uv_stride;
3175 } while (--h);
3176
3177 src = s->v_buffer;
3178 h = s->uv_height;
3179
3180 do {
3181 fwrite(src, s->uv_width, 1, yuv_rec_file);
3182 src += s->uv_stride;
3183 } while (--h);
3184
3185 fflush(yuv_rec_file);
3186}
3187#endif // OUTPUT_YUV_REC
3188
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003189#if CONFIG_GLOBAL_MOTION
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003190#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003191static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3192 int i;
3193 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003194 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003195 AV1_COMMON *const cm = &cpi->common;
3196 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3197 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003198 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003199 cpi->gmparams_cost[i]) {
Sarah Parker4c10a3c2017-04-10 19:37:59 -07003200 set_default_warp_params(&cm->global_motion[i]);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003201 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003202 recode = 1;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003203 recode |= (rdc->global_motion_used[i] > 0);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003204 }
3205 }
3206 return recode;
3207}
3208#endif // CONFIG_GLOBAL_MOTION
3209
Yaowu Xuc27fc142016-08-22 16:08:15 -07003210// Function to test for conditions that indicate we should loop
3211// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003212static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3213 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003214 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003215 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003216 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3217 int force_recode = 0;
3218
3219 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3220 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3221 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003222 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3223 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3224 (rc->projected_frame_size < low_limit && q > minq)) {
3225 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003226 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003227 // Deal with frame undershoot and whether or not we are
3228 // below the automatically set cq level.
3229 if (q > oxcf->cq_level &&
3230 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3231 force_recode = 1;
3232 }
3233 }
3234 }
3235 return force_recode;
3236}
3237
Yaowu Xuc27fc142016-08-22 16:08:15 -07003238#define DUMP_REF_FRAME_IMAGES 0
3239
3240#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003241static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003242 const YV12_BUFFER_CONFIG *const ref_buf,
3243 char *file_name) {
3244 int h;
3245 FILE *f_ref = NULL;
3246
3247 if (ref_buf == NULL) {
3248 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003249 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003250 }
3251
3252 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3253 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003254 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003255 }
3256
3257 // --- Y ---
3258 for (h = 0; h < cm->height; ++h) {
3259 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3260 }
3261 // --- U ---
3262 for (h = 0; h < (cm->height >> 1); ++h) {
3263 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3264 f_ref);
3265 }
3266 // --- V ---
3267 for (h = 0; h < (cm->height >> 1); ++h) {
3268 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3269 f_ref);
3270 }
3271
3272 fclose(f_ref);
3273
Yaowu Xuf883b422016-08-30 14:01:10 -07003274 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003275}
3276
Yaowu Xuf883b422016-08-30 14:01:10 -07003277static void dump_ref_frame_images(AV1_COMP *cpi) {
3278 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003279 MV_REFERENCE_FRAME ref_frame;
3280
3281 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3282 char file_name[256] = "";
3283 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3284 cm->current_video_frame, ref_frame);
3285 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3286 }
3287}
3288#endif // DUMP_REF_FRAME_IMAGES == 1
3289
3290#if CONFIG_EXT_REFS
3291// This function is used to shift the virtual indices of last reference frames
3292// as follows:
3293// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3294// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003295static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003296 int ref_frame;
3297 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
3298 cpi->lst_fb_idxes[ref_frame] = cpi->lst_fb_idxes[ref_frame - 1];
3299
3300 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003301 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003302 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003303 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3304 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3305 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003306 }
3307 }
3308}
Zoe Liuf0e46692016-10-12 12:31:43 -07003309#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003310
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003311#if CONFIG_VAR_REFS
3312static void enc_check_valid_ref_frames(AV1_COMP *const cpi) {
3313 AV1_COMMON *const cm = &cpi->common;
3314 MV_REFERENCE_FRAME ref_frame;
3315
3316 // TODO(zoeliu): To handle ALTREF_FRAME the same way as do with other
3317 // reference frames. Current encoder invalid ALTREF when ALTREF
3318 // is the same as LAST, but invalid all the other references
3319 // when they are the same as ALTREF.
3320 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3321 int ref_buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3322 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3323
3324 if (ref_buf_idx != INVALID_IDX) {
3325 ref_buf->is_valid = 1;
3326
3327 MV_REFERENCE_FRAME ref;
3328 for (ref = LAST_FRAME; ref < ref_frame; ++ref) {
3329 int buf_idx = get_ref_frame_buf_idx(cpi, ref);
3330 RefBuffer *const buf = &cm->frame_refs[ref - LAST_FRAME];
3331 if (buf->is_valid && buf_idx == ref_buf_idx) {
3332 if (ref_frame != ALTREF_FRAME || ref == LAST_FRAME) {
3333 ref_buf->is_valid = 0;
3334 break;
3335 } else {
3336 buf->is_valid = 0;
3337 }
3338 }
3339 }
3340 } else {
3341 ref_buf->is_valid = 0;
3342 }
3343 }
3344}
3345#endif // CONFIG_VAR_REFS
3346
Yaowu Xuf883b422016-08-30 14:01:10 -07003347void av1_update_reference_frames(AV1_COMP *cpi) {
3348 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003349 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003350
3351 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3352 // for the purpose to verify no mismatch between encoder and decoder.
3353 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3354
Yaowu Xuc27fc142016-08-22 16:08:15 -07003355 // At this point the new frame has been encoded.
3356 // If any buffer copy / swapping is signaled it should be done here.
3357 if (cm->frame_type == KEY_FRAME) {
3358 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3359 cm->new_fb_idx);
3360#if CONFIG_EXT_REFS
3361 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3362 cm->new_fb_idx);
Zoe Liue9b15e22017-07-19 15:53:01 -07003363 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3364 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003365#endif // CONFIG_EXT_REFS
3366 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3367 cm->new_fb_idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003368 } else if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003369 // We have decided to preserve the previously existing golden frame as our
3370 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003371 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003372 // we're updating the GF with the current decoded frame, we save it to the
3373 // ARF slot instead.
3374 // We now have to update the ARF with the current frame and swap gld_fb_idx
3375 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3376 // slot and, if we're updating the GF, the current frame becomes the new GF.
3377 int tmp;
3378
3379 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt_fb_idx],
3380 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003381 tmp = cpi->alt_fb_idx;
3382 cpi->alt_fb_idx = cpi->gld_fb_idx;
3383 cpi->gld_fb_idx = tmp;
3384
3385#if CONFIG_EXT_REFS
3386 // We need to modify the mapping accordingly
3387 cpi->arf_map[0] = cpi->alt_fb_idx;
Zoe Liue9b15e22017-07-19 15:53:01 -07003388#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003389// TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3390// cpi->interp_filter_selected[GOLDEN_FRAME]?
3391#if CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003392 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3393 // Deal with the special case for showing existing internal ALTREF_FRAME
3394 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3395 // by updating the virtual indices.
3396 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -07003397 const int which_arf = gf_group->arf_ref_idx[gf_group->index];
3398 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003399
Zoe Liue9b15e22017-07-19 15:53:01 -07003400 const int tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003401 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003402
Zoe Liue9b15e22017-07-19 15:53:01 -07003403 cpi->lst_fb_idxes[0] = cpi->alt2_fb_idx;
3404 cpi->alt2_fb_idx = tmp;
3405 // We need to modify the mapping accordingly
3406 cpi->arf_map[which_arf] = cpi->alt2_fb_idx;
3407
3408 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3409 cpi->interp_filter_selected[ALTREF2_FRAME],
3410 sizeof(cpi->interp_filter_selected[ALTREF2_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411#endif // CONFIG_EXT_REFS
3412 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003413 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003414 if (cpi->refresh_alt_ref_frame) {
3415 int arf_idx = cpi->alt_fb_idx;
3416 int which_arf = 0;
Zoe Liu3ac20932017-08-30 16:35:55 -07003417#if !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003418 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
3419 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3420 arf_idx = gf_group->arf_update_idx[gf_group->index];
3421 }
Zoe Liu3ac20932017-08-30 16:35:55 -07003422#endif // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003423 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003424
3425 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3426 cpi->interp_filter_selected[0],
3427 sizeof(cpi->interp_filter_selected[0]));
3428 }
3429
Zoe Liue9b15e22017-07-19 15:53:01 -07003430 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003431 if (cpi->refresh_golden_frame) {
3432 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->gld_fb_idx],
3433 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003434
Zoe Liuf0e46692016-10-12 12:31:43 -07003435#if !CONFIG_EXT_REFS
3436 if (!cpi->rc.is_src_frame_alt_ref)
3437#endif // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003438 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3439 cpi->interp_filter_selected[0],
3440 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003441 }
3442
3443#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07003444 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003445 if (cpi->refresh_bwd_ref_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003446 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->bwd_fb_idx],
3447 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003448
3449 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3450 cpi->interp_filter_selected[0],
3451 sizeof(cpi->interp_filter_selected[0]));
3452 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003453
Zoe Liue9b15e22017-07-19 15:53:01 -07003454 // === ALTREF2_FRAME ===
3455 if (cpi->refresh_alt2_ref_frame) {
3456 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->alt2_fb_idx],
3457 cm->new_fb_idx);
3458
3459 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3460 cpi->interp_filter_selected[0],
3461 sizeof(cpi->interp_filter_selected[0]));
3462 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003463#endif // CONFIG_EXT_REFS
3464 }
3465
3466 if (cpi->refresh_last_frame) {
3467#if CONFIG_EXT_REFS
3468 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3469 // reference to the reference frame buffer virtual index; and then (2) from
3470 // the virtual index to the reference frame buffer physical index:
3471 //
3472 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3473 // | | |
3474 // v v v
3475 // lst_fb_idxes[0], ..., lst_fb_idxes[2], ..., alt_fb_idx
3476 // | | |
3477 // v v v
3478 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3479 //
3480 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3481 // have the other 2 LAST reference frames shifted as follows:
3482 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3483 // , and then have LAST_FRAME refreshed by the newly coded frame.
3484 //
3485 // To fulfill it, the decoder will be notified to execute following 2 steps:
3486 //
3487 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3488 // to point to the newly coded frame, i.e.
3489 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3490 //
3491 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3492 // original virtual index of LAST3_FRAME and have the other mappings
3493 // shifted as follows:
3494 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3495 // | | |
3496 // v v v
3497 // lst_fb_idxes[2], lst_fb_idxes[0], lst_fb_idxes[1]
3498 int ref_frame;
Zoe Liu5fca7242016-10-10 17:18:57 -07003499
Yaowu Xuc27fc142016-08-22 16:08:15 -07003500 if (cm->frame_type == KEY_FRAME) {
3501 for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3502 ref_cnt_fb(pool->frame_bufs,
3503 &cm->ref_frame_map[cpi->lst_fb_idxes[ref_frame]],
3504 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003505 }
3506 } else {
3507 int tmp;
3508
3509 ref_cnt_fb(pool->frame_bufs,
3510 &cm->ref_frame_map[cpi->lst_fb_idxes[LAST_REF_FRAMES - 1]],
3511 cm->new_fb_idx);
3512
Yaowu Xuc27fc142016-08-22 16:08:15 -07003513 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3514
3515 shift_last_ref_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516 cpi->lst_fb_idxes[0] = tmp;
3517
Zoe Liuf0e46692016-10-12 12:31:43 -07003518 assert(cm->show_existing_frame == 0);
Zoe Liuf0e46692016-10-12 12:31:43 -07003519 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3520 cpi->interp_filter_selected[0],
3521 sizeof(cpi->interp_filter_selected[0]));
Zoe Liuaff92d52017-07-11 21:35:08 -07003522
3523 if (cpi->rc.is_last_bipred_frame) {
3524 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3525 // LAST3_FRAME by updating the virtual indices.
3526 //
3527 // NOTE: The source frame for BWDREF does not have a holding position as
3528 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3529 // virtual index reshuffling for BWDREF, the encoder always
3530 // specifies a LAST_BIPRED right before BWDREF and completes the
3531 // reshuffling job accordingly.
3532 tmp = cpi->lst_fb_idxes[LAST_REF_FRAMES - 1];
3533
3534 shift_last_ref_frames(cpi);
3535 cpi->lst_fb_idxes[0] = cpi->bwd_fb_idx;
3536 cpi->bwd_fb_idx = tmp;
3537
3538 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3539 cpi->interp_filter_selected[BWDREF_FRAME],
3540 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
3541 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003542 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003543#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003544 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->lst_fb_idx],
3545 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003546 if (!cpi->rc.is_src_frame_alt_ref) {
3547 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3548 cpi->interp_filter_selected[0],
3549 sizeof(cpi->interp_filter_selected[0]));
3550 }
3551#endif // CONFIG_EXT_REFS
3552 }
3553
3554#if DUMP_REF_FRAME_IMAGES == 1
3555 // Dump out all reference frame images.
3556 dump_ref_frame_images(cpi);
3557#endif // DUMP_REF_FRAME_IMAGES
3558}
3559
Yaowu Xuf883b422016-08-30 14:01:10 -07003560static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003561 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003562 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003563 ensure_mv_buffer(new_fb_ptr, cm);
3564 new_fb_ptr->width = cm->width;
3565 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003566}
3567
Yaowu Xuf883b422016-08-30 14:01:10 -07003568void av1_scale_references(AV1_COMP *cpi) {
3569 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003570 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003571 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
3572 AOM_LAST_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003573#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003574 AOM_LAST2_FLAG,
3575 AOM_LAST3_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003576#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003577 AOM_GOLD_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003578#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003579 AOM_BWD_FLAG,
Zoe Liue9b15e22017-07-19 15:53:01 -07003580 AOM_ALT2_FLAG,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003581#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07003582 AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003583 };
3584
3585 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003586 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003587 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3588 BufferPool *const pool = cm->buffer_pool;
3589 const YV12_BUFFER_CONFIG *const ref =
3590 get_ref_frame_buffer(cpi, ref_frame);
3591
3592 if (ref == NULL) {
3593 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3594 continue;
3595 }
3596
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003597#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003598 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3599 RefCntBuffer *new_fb_ptr = NULL;
3600 int force_scaling = 0;
3601 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3602 if (new_fb == INVALID_IDX) {
3603 new_fb = get_free_fb(cm);
3604 force_scaling = 1;
3605 }
3606 if (new_fb == INVALID_IDX) return;
3607 new_fb_ptr = &pool->frame_bufs[new_fb];
3608 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3609 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003610 if (aom_realloc_frame_buffer(
3611 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3612 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3613 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003614 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003615 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003616 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf,
3617 (int)cm->bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003618 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3619 alloc_frame_mvs(cm, new_fb);
3620 }
3621#else
3622 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3623 RefCntBuffer *new_fb_ptr = NULL;
3624 int force_scaling = 0;
3625 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3626 if (new_fb == INVALID_IDX) {
3627 new_fb = get_free_fb(cm);
3628 force_scaling = 1;
3629 }
3630 if (new_fb == INVALID_IDX) return;
3631 new_fb_ptr = &pool->frame_bufs[new_fb];
3632 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3633 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003634 if (aom_realloc_frame_buffer(&new_fb_ptr->buf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003635 cm->subsampling_x, cm->subsampling_y,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003636 AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3637 NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003638 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003639 "Failed to allocate frame buffer");
Debargha Mukherjee405c8572017-07-10 09:29:17 -07003640 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003641 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3642 alloc_frame_mvs(cm, new_fb);
3643 }
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003644#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003645 } else {
3646 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3647 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3648 buf->buf.y_crop_width = ref->y_crop_width;
3649 buf->buf.y_crop_height = ref->y_crop_height;
3650 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3651 ++buf->ref_count;
3652 }
3653 } else {
3654 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3655 }
3656 }
3657}
3658
Yaowu Xuf883b422016-08-30 14:01:10 -07003659static void release_scaled_references(AV1_COMP *cpi) {
3660 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003661 int i;
3662 if (cpi->oxcf.pass == 0) {
3663 // Only release scaled references under certain conditions:
3664 // if reference will be updated, or if scaled reference has same resolution.
3665 int refresh[INTER_REFS_PER_FRAME];
3666 refresh[0] = (cpi->refresh_last_frame) ? 1 : 0;
3667#if CONFIG_EXT_REFS
3668 refresh[1] = refresh[2] = 0;
3669 refresh[3] = (cpi->refresh_golden_frame) ? 1 : 0;
3670 refresh[4] = (cpi->refresh_bwd_ref_frame) ? 1 : 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07003671 refresh[5] = (cpi->refresh_alt2_ref_frame) ? 1 : 0;
3672 refresh[6] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07003673#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003674 refresh[1] = (cpi->refresh_golden_frame) ? 1 : 0;
3675 refresh[2] = (cpi->refresh_alt_ref_frame) ? 1 : 0;
3676#endif // CONFIG_EXT_REFS
3677 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3678 const int idx = cpi->scaled_ref_idx[i - 1];
3679 RefCntBuffer *const buf =
3680 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3681 const YV12_BUFFER_CONFIG *const ref = get_ref_frame_buffer(cpi, i);
3682 if (buf != NULL &&
3683 (refresh[i - 1] || (buf->buf.y_crop_width == ref->y_crop_width &&
3684 buf->buf.y_crop_height == ref->y_crop_height))) {
3685 --buf->ref_count;
3686 cpi->scaled_ref_idx[i - 1] = INVALID_IDX;
3687 }
3688 }
3689 } else {
3690 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i) {
3691 const int idx = cpi->scaled_ref_idx[i];
3692 RefCntBuffer *const buf =
3693 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3694 if (buf != NULL) {
3695 --buf->ref_count;
3696 cpi->scaled_ref_idx[i] = INVALID_IDX;
3697 }
3698 }
3699 }
3700}
3701
Yaowu Xuc27fc142016-08-22 16:08:15 -07003702#if 0 && CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003703static void output_frame_level_debug_stats(AV1_COMP *cpi) {
3704 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003705 FILE *const f = fopen("tmp.stt", cm->current_video_frame ? "a" : "w");
3706 int64_t recon_err;
3707
Yaowu Xuf883b422016-08-30 14:01:10 -07003708 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003709
Alex Conversef77fd0b2017-04-20 11:00:24 -07003710 recon_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003711
3712 if (cpi->twopass.total_left_stats.coded_error != 0.0)
Yunqing Wang8c1e57c2016-10-25 15:15:23 -07003713 fprintf(f, "%10u %dx%d %d %d %10d %10d %10d %10d"
Yaowu Xuc27fc142016-08-22 16:08:15 -07003714 "%10"PRId64" %10"PRId64" %5d %5d %10"PRId64" "
3715 "%10"PRId64" %10"PRId64" %10d "
3716 "%7.2lf %7.2lf %7.2lf %7.2lf %7.2lf"
3717 "%6d %6d %5d %5d %5d "
3718 "%10"PRId64" %10.3lf"
3719 "%10lf %8u %10"PRId64" %10d %10d %10d\n",
3720 cpi->common.current_video_frame,
3721 cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003722 cpi->rc.source_alt_ref_pending,
3723 cpi->rc.source_alt_ref_active,
3724 cpi->rc.this_frame_target,
3725 cpi->rc.projected_frame_size,
3726 cpi->rc.projected_frame_size / cpi->common.MBs,
3727 (cpi->rc.projected_frame_size - cpi->rc.this_frame_target),
3728 cpi->rc.vbr_bits_off_target,
3729 cpi->rc.vbr_bits_off_target_fast,
3730 cpi->twopass.extend_minq,
3731 cpi->twopass.extend_minq_fast,
3732 cpi->rc.total_target_vs_actual,
3733 (cpi->rc.starting_buffer_level - cpi->rc.bits_off_target),
3734 cpi->rc.total_actual_bits, cm->base_qindex,
Yaowu Xuf883b422016-08-30 14:01:10 -07003735 av1_convert_qindex_to_q(cm->base_qindex, cm->bit_depth),
3736 (double)av1_dc_quant(cm->base_qindex, 0, cm->bit_depth) / 4.0,
3737 av1_convert_qindex_to_q(cpi->twopass.active_worst_quality,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003738 cm->bit_depth),
3739 cpi->rc.avg_q,
Yaowu Xuf883b422016-08-30 14:01:10 -07003740 av1_convert_qindex_to_q(cpi->oxcf.cq_level, cm->bit_depth),
Yaowu Xuc27fc142016-08-22 16:08:15 -07003741 cpi->refresh_last_frame, cpi->refresh_golden_frame,
3742 cpi->refresh_alt_ref_frame, cm->frame_type, cpi->rc.gfu_boost,
3743 cpi->twopass.bits_left,
3744 cpi->twopass.total_left_stats.coded_error,
3745 cpi->twopass.bits_left /
3746 (1 + cpi->twopass.total_left_stats.coded_error),
3747 cpi->tot_recode_hits, recon_err, cpi->rc.kf_boost,
3748 cpi->twopass.kf_zeromotion_pct,
3749 cpi->twopass.fr_content_type);
3750
3751 fclose(f);
3752
3753 if (0) {
3754 FILE *const fmodes = fopen("Modes.stt", "a");
3755 int i;
3756
3757 fprintf(fmodes, "%6d:%1d:%1d:%1d ", cpi->common.current_video_frame,
3758 cm->frame_type, cpi->refresh_golden_frame,
3759 cpi->refresh_alt_ref_frame);
3760
3761 for (i = 0; i < MAX_MODES; ++i)
3762 fprintf(fmodes, "%5d ", cpi->mode_chosen_counts[i]);
3763
3764 fprintf(fmodes, "\n");
3765
3766 fclose(fmodes);
3767 }
3768}
3769#endif
3770
Yaowu Xuf883b422016-08-30 14:01:10 -07003771static void set_mv_search_params(AV1_COMP *cpi) {
3772 const AV1_COMMON *const cm = &cpi->common;
3773 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003774
3775 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003776 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003777
3778 if (cpi->sf.mv.auto_mv_step_size) {
3779 if (frame_is_intra_only(cm)) {
3780 // Initialize max_mv_magnitude for use in the first INTER frame
3781 // after a key/intra-only frame.
3782 cpi->max_mv_magnitude = max_mv_def;
3783 } else {
3784 if (cm->show_frame) {
3785 // Allow mv_steps to correspond to twice the max mv magnitude found
3786 // in the previous frame, capped by the default max_mv_magnitude based
3787 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003788 cpi->mv_step_param = av1_init_search_range(
3789 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003790 }
3791 cpi->max_mv_magnitude = 0;
3792 }
3793 }
3794}
3795
Yaowu Xuf883b422016-08-30 14:01:10 -07003796static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003797#if CONFIG_GLOBAL_MOTION
3798 int i;
3799 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Sarah Parker4c10a3c2017-04-10 19:37:59 -07003800 set_default_warp_params(&cpi->common.global_motion[i]);
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003801 }
3802 cpi->global_motion_search_done = 0;
3803#endif // CONFIG_GLOBAL_MOTION
Yaowu Xuf883b422016-08-30 14:01:10 -07003804 av1_set_speed_features_framesize_independent(cpi);
3805 av1_set_rd_speed_thresholds(cpi);
3806 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003807 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Debargha Mukherjee9e2c7a62017-05-23 21:18:42 -07003808#if CONFIG_EXT_INTER
3809 if (!frame_is_intra_only(&cpi->common)) set_compound_tools(&cpi->common);
3810#endif // CONFIG_EXT_INTER
Yaowu Xuc27fc142016-08-22 16:08:15 -07003811}
3812
Yaowu Xuf883b422016-08-30 14:01:10 -07003813static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003814 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003815 AV1_COMMON *const cm = &cpi->common;
3816 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003817
3818 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003819 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003820
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00003821// Decide q and q bounds.
3822#if CONFIG_XIPHRC
3823 int frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
3824 *q = od_enc_rc_select_quantizers_and_lambdas(
3825 &cpi->od_rc, cpi->refresh_golden_frame, cpi->refresh_alt_ref_frame,
3826 frame_type, bottom_index, top_index);
3827#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003828 *q = av1_rc_pick_q_and_bounds(cpi, bottom_index, top_index);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00003829#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003830
James Zern01a9d702017-08-25 19:09:33 +00003831 if (!frame_is_intra_only(cm)) {
3832 av1_set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH);
3833 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003834
3835 // Configure experimental use of segmentation for enhanced coding of
3836 // static regions if indicated.
3837 // Only allowed in the second pass of a two pass encode, as it requires
3838 // lagged coding, and if the relevant speed feature flag is set.
3839 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3840 configure_static_seg_features(cpi);
3841}
3842
Yaowu Xuf883b422016-08-30 14:01:10 -07003843static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003844 int y_stride = cpi->scaled_source.y_stride;
3845
3846 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003847 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003848 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003849 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003850 }
3851}
3852
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003853#if CONFIG_LOOP_RESTORATION
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003854#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
3855static void set_restoration_tilesize(int width, int height, int sx, int sy,
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003856 RestorationInfo *rst) {
3857 (void)width;
3858 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003859 (void)sx;
3860 (void)sy;
3861#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3862 int s = AOMMIN(sx, sy);
3863#else
3864 int s = 0;
3865#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3866
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003867 rst[0].restoration_tilesize = (RESTORATION_TILESIZE_MAX >> 1);
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003868 rst[1].restoration_tilesize = rst[0].restoration_tilesize >> s;
3869 rst[2].restoration_tilesize = rst[1].restoration_tilesize;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003870}
3871#endif // CONFIG_LOOP_RESTORATION
3872
Fergus Simpsonbc189932017-05-16 17:02:39 -07003873static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003874 AV1_COMMON *const cm = &cpi->common;
Fergus Simpsonbc189932017-05-16 17:02:39 -07003875 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003876 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003877
Fergus Simpsonbc189932017-05-16 17:02:39 -07003878 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003879 // There has been a change in the encoded frame size
Fergus Simpsonbc189932017-05-16 17:02:39 -07003880 av1_set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003881 set_mv_search_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003882 }
3883
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00003884#if !CONFIG_XIPHRC
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003885 if (cpi->oxcf.pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003886 av1_set_target_rate(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003887 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00003888#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003889
3890 alloc_frame_mvs(cm, cm->new_fb_idx);
3891
3892 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07003893 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003894 cm->subsampling_x, cm->subsampling_y,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003895#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003896 cm->use_highbitdepth,
3897#endif
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003898 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
3899 NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003900 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003901 "Failed to allocate frame buffer");
3902
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003903#if CONFIG_LOOP_RESTORATION
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003904 set_restoration_tilesize(
3905#if CONFIG_FRAME_SUPERRES
3906 cm->superres_upscaled_width, cm->superres_upscaled_height,
3907#else
3908 cm->width, cm->height,
3909#endif // CONFIG_FRAME_SUPERRES
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003910 cm->subsampling_x, cm->subsampling_y, cm->rst_info);
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003911 for (int i = 0; i < MAX_MB_PLANE; ++i)
3912 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
3913 av1_alloc_restoration_buffers(cm);
3914 for (int i = 0; i < MAX_MB_PLANE; ++i) {
3915 cpi->rst_search[i].restoration_tilesize =
3916 cm->rst_info[i].restoration_tilesize;
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003917 av1_alloc_restoration_struct(cm, &cpi->rst_search[i],
3918#if CONFIG_FRAME_SUPERRES
3919 cm->superres_upscaled_width,
3920 cm->superres_upscaled_height);
3921#else
3922 cm->width, cm->height);
3923#endif // CONFIG_FRAME_SUPERRES
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003924 }
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003925#endif // CONFIG_LOOP_RESTORATION
3926 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003927 init_motion_estimation(cpi);
3928
3929 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3930 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3931 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3932
3933 ref_buf->idx = buf_idx;
3934
3935 if (buf_idx != INVALID_IDX) {
3936 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3937 ref_buf->buf = buf;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003938#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07003939 av1_setup_scale_factors_for_frame(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003940 &ref_buf->sf, buf->y_crop_width, buf->y_crop_height, cm->width,
3941 cm->height, (buf->flags & YV12_FLAG_HIGHBITDEPTH) ? 1 : 0);
3942#else
Yaowu Xuf883b422016-08-30 14:01:10 -07003943 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3944 buf->y_crop_height, cm->width,
3945 cm->height);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02003946#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07003947 if (av1_is_scaled(&ref_buf->sf)) aom_extend_frame_borders(buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003948 } else {
3949 ref_buf->buf = NULL;
3950 }
3951 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003952
3953#if CONFIG_VAR_REFS
3954 // Check duplicate reference frames
3955 enc_check_valid_ref_frames(cpi);
3956#endif // CONFIG_VAR_REFS
3957
Alex Conversee816b312017-05-01 09:51:24 -07003958#if CONFIG_INTRABC
3959#if CONFIG_HIGHBITDEPTH
Sebastien Alaiwan1dcb7072017-05-12 11:13:05 +02003960 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
3961 cm->width, cm->height,
3962 cm->use_highbitdepth);
Alex Conversee816b312017-05-01 09:51:24 -07003963#else
3964 av1_setup_scale_factors_for_frame(&xd->sf_identity, cm->width, cm->height,
3965 cm->width, cm->height);
3966#endif // CONFIG_HIGHBITDEPTH
3967#endif // CONFIG_INTRABC
Yaowu Xuc27fc142016-08-22 16:08:15 -07003968
3969 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3970}
3971
Fergus Simpsonbc189932017-05-16 17:02:39 -07003972static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003973 int encode_width = cpi->oxcf.width;
3974 int encode_height = cpi->oxcf.height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003975
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003976 uint8_t resize_num = av1_calculate_next_resize_scale(cpi);
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07003977 av1_calculate_scaled_size(&encode_width, &encode_height, resize_num);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003978
3979#if CONFIG_FRAME_SUPERRES
3980 AV1_COMMON *cm = &cpi->common;
3981 cm->superres_upscaled_width = encode_width;
3982 cm->superres_upscaled_height = encode_height;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003983 cm->superres_scale_numerator =
Urvang Joshi0b905402017-08-02 10:56:23 -07003984 av1_calculate_next_superres_scale(cpi, encode_width, encode_height);
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003985 av1_calculate_scaled_size(&encode_width, &encode_height,
Fergus Simpsonbfbf6a52017-06-14 23:13:12 -07003986 cm->superres_scale_numerator);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003987#endif // CONFIG_FRAME_SUPERRES
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003988
3989 set_frame_size(cpi, encode_width, encode_height);
3990}
3991
3992#if CONFIG_FRAME_SUPERRES
3993static void superres_post_encode(AV1_COMP *cpi) {
3994 AV1_COMMON *cm = &cpi->common;
3995
3996 if (av1_superres_unscaled(cm)) return;
3997
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003998 av1_superres_upscale(cm, NULL);
3999
4000 // If regular resizing is occurring the source will need to be downscaled to
4001 // match the upscaled superres resolution. Otherwise the original source is
4002 // used.
4003 if (av1_resize_unscaled(cm)) {
4004 cpi->source = cpi->unscaled_source;
4005 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4006 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004007 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4008 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004009 // Do downscale. cm->(width|height) has been updated by av1_superres_upscale
4010 if (aom_realloc_frame_buffer(
4011 &cpi->scaled_source, cm->superres_upscaled_width,
4012 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
4013#if CONFIG_HIGHBITDEPTH
4014 cm->use_highbitdepth,
4015#endif // CONFIG_HIGHBITDEPTH
4016 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
4017 aom_internal_error(
4018 &cm->error, AOM_CODEC_MEM_ERROR,
4019 "Failed to reallocate scaled source buffer for superres");
4020 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4021 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
4022#if CONFIG_HIGHBITDEPTH
4023 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
4024 (int)cm->bit_depth);
4025#else
4026 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source);
4027#endif // CONFIG_HIGHBITDEPTH
4028 cpi->source = &cpi->scaled_source;
4029 }
4030}
4031#endif // CONFIG_FRAME_SUPERRES
4032
4033static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4034 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4035 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004036 int no_loopfilter = 0;
4037
4038 if (is_lossless_requested(&cpi->oxcf)) no_loopfilter = 1;
4039
4040#if CONFIG_EXT_TILE
4041 // 0 loopfilter level is only necessary if individual tile
4042 // decoding is required.
4043 if (cm->single_tile_decoding) no_loopfilter = 1;
4044#endif // CONFIG_EXT_TILE
4045
4046 if (no_loopfilter) {
Cheng Chen13fc8192017-08-19 11:49:28 -07004047#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004048 lf->filter_level[0] = 0;
4049 lf->filter_level[1] = 0;
4050#else
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004051 lf->filter_level = 0;
Cheng Chen179479f2017-08-04 10:56:39 -07004052#endif
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004053 } else {
4054 struct aom_usec_timer timer;
4055
4056 aom_clear_system_state();
4057
4058 aom_usec_timer_start(&timer);
4059
4060 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4061
4062 aom_usec_timer_mark(&timer);
4063 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4064 }
4065
Cheng Chen13fc8192017-08-19 11:49:28 -07004066#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004067 if (lf->filter_level[0] || lf->filter_level[1])
4068#else
4069 if (lf->filter_level > 0)
4070#endif
4071 {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004072#if CONFIG_VAR_TX || CONFIG_EXT_PARTITION || CONFIG_CB4X4
Cheng Chen13fc8192017-08-19 11:49:28 -07004073#if CONFIG_LOOPFILTER_LEVEL
Cheng Chen179479f2017-08-04 10:56:39 -07004074 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level[0],
4075 lf->filter_level[1], 0, 0);
4076 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_u,
4077 lf->filter_level_u, 1, 0);
4078 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_v,
4079 lf->filter_level_v, 2, 0);
4080
Cheng Chene94df5c2017-07-19 17:25:33 -07004081#else
4082 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
Cheng Chen13fc8192017-08-19 11:49:28 -07004083#endif // CONFIG_LOOPFILTER_LEVEL
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004084#else
4085 if (cpi->num_workers > 1)
4086 av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd->plane,
4087 lf->filter_level, 0, 0, cpi->workers,
4088 cpi->num_workers, &cpi->lf_row_sync);
4089 else
4090 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level, 0, 0);
4091#endif
4092 }
4093#if CONFIG_CDEF
4094 if (is_lossless_requested(&cpi->oxcf)) {
4095 cm->cdef_bits = 0;
4096 cm->cdef_strengths[0] = 0;
4097 cm->nb_cdef_strengths = 1;
4098 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004099 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004100 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
4101 cpi->oxcf.speed > 0);
4102
4103 // Apply the filter
4104 av1_cdef_frame(cm->frame_to_show, cm, xd);
4105 }
4106#endif
4107
4108#if CONFIG_FRAME_SUPERRES
4109 superres_post_encode(cpi);
4110#endif // CONFIG_FRAME_SUPERRES
4111
4112#if CONFIG_LOOP_RESTORATION
4113 av1_pick_filter_restoration(cpi->source, cpi, cpi->sf.lpf_pick);
4114 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4115 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4116 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
4117 av1_loop_restoration_frame(cm->frame_to_show, cm, cm->rst_info, 7, 0, NULL);
4118 }
4119#endif // CONFIG_LOOP_RESTORATION
4120 // TODO(debargha): Fix mv search range on encoder side
4121 // aom_extend_frame_inner_borders(cm->frame_to_show);
4122 aom_extend_frame_borders(cm->frame_to_show);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004123}
4124
Yaowu Xuf883b422016-08-30 14:01:10 -07004125static void encode_without_recode_loop(AV1_COMP *cpi) {
4126 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004127 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004128
Yaowu Xuf883b422016-08-30 14:01:10 -07004129 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004130
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004131 set_size_independent_vars(cpi);
Fergus Simpsonbc189932017-05-16 17:02:39 -07004132 setup_frame_size(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004133 assert(cm->width == cpi->scaled_source.y_crop_width);
4134 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004135
Yaowu Xuc27fc142016-08-22 16:08:15 -07004136 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4137
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004138 cpi->source =
4139 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4140 if (cpi->unscaled_last_source != NULL)
4141 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4142 &cpi->scaled_last_source);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004143#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
4144 cpi->source->buf_8bit_valid = 0;
4145#endif
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004146
4147 if (frame_is_intra_only(cm) == 0) {
4148 av1_scale_references(cpi);
4149 }
4150
Yaowu Xuf883b422016-08-30 14:01:10 -07004151 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004152 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004153 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004154
Yaowu Xuc27fc142016-08-22 16:08:15 -07004155 // Variance adaptive and in frame q adjustment experiments are mutually
4156 // exclusive.
4157 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004158 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004159 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004160 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004161 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004162 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004163 }
4164 apply_active_map(cpi);
4165
4166 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004167 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004168
4169 // Update some stats from cyclic refresh, and check if we should not update
4170 // golden reference, for 1 pass CBR.
4171 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004172 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4173 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004174
4175 // Update the skip mb flag probabilities based on the distribution
4176 // seen in the last encoder iteration.
4177 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004178 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004179}
4180
Yaowu Xuf883b422016-08-30 14:01:10 -07004181static void encode_with_recode_loop(AV1_COMP *cpi, size_t *size,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004182 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004183 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004184 RATE_CONTROL *const rc = &cpi->rc;
4185 int bottom_index, top_index;
4186 int loop_count = 0;
4187 int loop_at_this_size = 0;
4188 int loop = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004189#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004190 int overshoot_seen = 0;
4191 int undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004192#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004193 int frame_over_shoot_limit;
4194 int frame_under_shoot_limit;
4195 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004196
4197 set_size_independent_vars(cpi);
4198
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004199#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
4200 cpi->source->buf_8bit_valid = 0;
4201#endif
4202
Yaowu Xuc27fc142016-08-22 16:08:15 -07004203 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004204 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004205
Fergus Simpsonbc189932017-05-16 17:02:39 -07004206 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004207
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004208 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004209 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4210
4211 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4212 set_mv_search_params(cpi);
4213
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004214#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004215 // Reset the loop state for new frame size.
4216 overshoot_seen = 0;
4217 undershoot_seen = 0;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004218#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004219
Yaowu Xuc27fc142016-08-22 16:08:15 -07004220 q_low = bottom_index;
4221 q_high = top_index;
4222
4223 loop_at_this_size = 0;
4224 }
4225
4226 // Decide frame size bounds first time through.
4227 if (loop_count == 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004228 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4229 &frame_under_shoot_limit,
4230 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004231 }
4232
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004233 cpi->source =
4234 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4235#if CONFIG_GLOBAL_MOTION
4236 // if frame was scaled calculate global_motion_search again if already done
4237 if (cpi->source != cpi->unscaled_source) cpi->global_motion_search_done = 0;
4238#endif // CONFIG_GLOBAL_MOTION
4239 if (cpi->unscaled_last_source != NULL)
4240 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4241 &cpi->scaled_last_source);
4242
Yaowu Xuc27fc142016-08-22 16:08:15 -07004243 if (frame_is_intra_only(cm) == 0) {
4244 if (loop_count > 0) {
4245 release_scaled_references(cpi);
4246 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004247 av1_scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004248 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004249 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004250
4251 if (loop_count == 0) setup_frame(cpi);
4252
hui su0d103572017-03-01 17:58:01 -08004253#if CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004254 // Base q-index may have changed, so we need to assign proper default coef
4255 // probs before every iteration.
4256 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
4257 int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07004258 av1_default_coef_probs(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004259 if (cm->frame_type == KEY_FRAME || cm->error_resilient_mode ||
4260 cm->reset_frame_context == RESET_FRAME_CONTEXT_ALL) {
4261 for (i = 0; i < FRAME_CONTEXTS; ++i) cm->frame_contexts[i] = *cm->fc;
4262 } else if (cm->reset_frame_context == RESET_FRAME_CONTEXT_CURRENT) {
Thomas Daededa4d8b92017-06-05 15:44:14 -07004263#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
4264 if (cm->frame_refs[0].idx >= 0) {
4265 cm->frame_contexts[cm->frame_refs[0].idx] = *cm->fc;
4266 }
4267#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004268 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07004269#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004270 }
4271 }
hui su0d103572017-03-01 17:58:01 -08004272#endif // CONFIG_Q_ADAPT_PROBS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004273
Yaowu Xuc27fc142016-08-22 16:08:15 -07004274 // Variance adaptive and in frame q adjustment experiments are mutually
4275 // exclusive.
4276 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004277 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004278 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004279 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004280 }
4281
4282 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004283 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004284 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004285
4286 // Update the skip mb flag probabilities based on the distribution
4287 // seen in the last encoder iteration.
4288 // update_base_skip_probs(cpi);
4289
Yaowu Xuf883b422016-08-30 14:01:10 -07004290 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004291
4292 // Dummy pack of the bitstream using up to date stats to get an
4293 // accurate estimate of output frame size to determine if we need
4294 // to recode.
4295 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004296 restore_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004297 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004298
4299 rc->projected_frame_size = (int)(*size) << 3;
4300 restore_coding_context(cpi);
4301
4302 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4303 }
4304
Yaowu Xuf883b422016-08-30 14:01:10 -07004305 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004306 loop = 0;
4307 } else {
4308 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4309 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4310 int last_q = q;
4311 int64_t kf_err;
4312
4313 int64_t high_err_target = cpi->ambient_err;
4314 int64_t low_err_target = cpi->ambient_err >> 1;
4315
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004316#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004317 if (cm->use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004318 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004319 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004320 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004321 }
4322#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07004323 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004324#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004325
4326 // Prevent possible divide by zero error below for perfect KF
4327 kf_err += !kf_err;
4328
4329 // The key frame is not good enough or we can afford
4330 // to make it better without undue risk of popping.
4331 if ((kf_err > high_err_target &&
4332 rc->projected_frame_size <= frame_over_shoot_limit) ||
4333 (kf_err > low_err_target &&
4334 rc->projected_frame_size <= frame_under_shoot_limit)) {
4335 // Lower q_high
4336 q_high = q > q_low ? q - 1 : q_low;
4337
4338 // Adjust Q
4339 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004340 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004341 } else if (kf_err < low_err_target &&
4342 rc->projected_frame_size >= frame_under_shoot_limit) {
4343 // The key frame is much better than the previous frame
4344 // Raise q_low
4345 q_low = q < q_high ? q + 1 : q_high;
4346
4347 // Adjust Q
4348 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004349 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004350 }
4351
4352 // Clamp Q to upper and lower limits:
4353 q = clamp(q, q_low, q_high);
4354
4355 loop = q != last_q;
4356 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4357 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004358 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004359 // Is the projected frame size out of range and are we allowed
4360 // to attempt to recode.
4361 int last_q = q;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004362#if !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363 int retries = 0;
4364
Yaowu Xuc27fc142016-08-22 16:08:15 -07004365 // Frame size out of permitted range:
4366 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004367 // Frame is too large
4368 if (rc->projected_frame_size > rc->this_frame_target) {
4369 // Special case if the projected size is > the max allowed.
4370 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4371 q_high = rc->worst_quality;
4372
4373 // Raise Qlow as to at least the current value
4374 q_low = q < q_high ? q + 1 : q_high;
4375
4376 if (undershoot_seen || loop_at_this_size > 1) {
4377 // Update rate_correction_factor unless
Yaowu Xuf883b422016-08-30 14:01:10 -07004378 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004379
4380 q = (q_high + q_low + 1) / 2;
4381 } else {
4382 // Update rate_correction_factor unless
Yaowu Xuf883b422016-08-30 14:01:10 -07004383 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004384
Yaowu Xuf883b422016-08-30 14:01:10 -07004385 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4386 AOMMAX(q_high, top_index));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004387
4388 while (q < q_low && retries < 10) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004389 av1_rc_update_rate_correction_factors(cpi);
4390 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4391 AOMMAX(q_high, top_index));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004392 retries++;
4393 }
4394 }
4395
4396 overshoot_seen = 1;
4397 } else {
4398 // Frame is too small
4399 q_high = q > q_low ? q - 1 : q_low;
4400
4401 if (overshoot_seen || loop_at_this_size > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004402 av1_rc_update_rate_correction_factors(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004403 q = (q_high + q_low) / 2;
4404 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07004405 av1_rc_update_rate_correction_factors(cpi);
4406 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4407 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004408 // Special case reset for qlow for constrained quality.
4409 // This should only trigger where there is very substantial
4410 // undershoot on a frame and the auto cq level is above
4411 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004412 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004413 q_low = q;
4414 }
4415
4416 while (q > q_high && retries < 10) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004417 av1_rc_update_rate_correction_factors(cpi);
4418 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4419 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004420 retries++;
4421 }
4422 }
4423
4424 undershoot_seen = 1;
4425 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004426#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004427
4428 // Clamp Q to upper and lower limits:
4429 q = clamp(q, q_low, q_high);
4430
4431 loop = (q != last_q);
4432 } else {
4433 loop = 0;
4434 }
4435 }
4436
4437 // Special case for overlay frame.
4438 if (rc->is_src_frame_alt_ref &&
4439 rc->projected_frame_size < rc->max_frame_bandwidth)
4440 loop = 0;
4441
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004442#if CONFIG_GLOBAL_MOTION
4443 if (recode_loop_test_global_motion(cpi)) {
4444 loop = 1;
4445 }
4446#endif // CONFIG_GLOBAL_MOTION
4447
Yaowu Xuc27fc142016-08-22 16:08:15 -07004448 if (loop) {
4449 ++loop_count;
4450 ++loop_at_this_size;
4451
4452#if CONFIG_INTERNAL_STATS
4453 ++cpi->tot_recode_hits;
4454#endif
4455 }
4456 } while (loop);
4457}
4458
Yaowu Xuf883b422016-08-30 14:01:10 -07004459static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004460 const int *const map = cpi->common.ref_frame_map;
4461
4462#if CONFIG_EXT_REFS
4463 const int last2_is_last =
4464 map[cpi->lst_fb_idxes[1]] == map[cpi->lst_fb_idxes[0]];
4465 const int last3_is_last =
4466 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[0]];
4467 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[0]];
Zoe Liu5a978832017-08-15 16:33:34 -07004468#if CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02004469 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
4470 const int last3_is_last2 =
4471 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
4472 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
4473 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
Zoe Liu5a978832017-08-15 16:33:34 -07004474#else // !CONFIG_ONE_SIDED_COMPOUND || CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004475 const int bwd_is_last = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[0]];
4476 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idxes[0]];
4477
4478 const int last3_is_last2 =
4479 map[cpi->lst_fb_idxes[2]] == map[cpi->lst_fb_idxes[1]];
4480 const int gld_is_last2 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[1]];
4481 const int bwd_is_last2 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[1]];
4482
4483 const int gld_is_last3 = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idxes[2]];
4484 const int bwd_is_last3 = map[cpi->bwd_fb_idx] == map[cpi->lst_fb_idxes[2]];
4485
4486 const int bwd_is_gld = map[cpi->bwd_fb_idx] == map[cpi->gld_fb_idx];
Zoe Liu5a978832017-08-15 16:33:34 -07004487#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07004488
Zoe Liue9b15e22017-07-19 15:53:01 -07004489 const int alt2_is_last = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[0]];
4490 const int alt2_is_last2 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[1]];
4491 const int alt2_is_last3 = map[cpi->alt2_fb_idx] == map[cpi->lst_fb_idxes[2]];
4492 const int alt2_is_gld = map[cpi->alt2_fb_idx] == map[cpi->gld_fb_idx];
4493 const int alt2_is_bwd = map[cpi->alt2_fb_idx] == map[cpi->bwd_fb_idx];
Zoe Liue9b15e22017-07-19 15:53:01 -07004494
Yaowu Xuc27fc142016-08-22 16:08:15 -07004495 const int last2_is_alt = map[cpi->lst_fb_idxes[1]] == map[cpi->alt_fb_idx];
4496 const int last3_is_alt = map[cpi->lst_fb_idxes[2]] == map[cpi->alt_fb_idx];
4497 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4498 const int bwd_is_alt = map[cpi->bwd_fb_idx] == map[cpi->alt_fb_idx];
Zoe Liue9b15e22017-07-19 15:53:01 -07004499 const int alt2_is_alt = map[cpi->alt2_fb_idx] == map[cpi->alt_fb_idx];
Zoe Liufcf5fa22017-06-26 16:00:38 -07004500#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004501 const int gld_is_last = map[cpi->gld_fb_idx] == map[cpi->lst_fb_idx];
4502 const int gld_is_alt = map[cpi->gld_fb_idx] == map[cpi->alt_fb_idx];
4503 const int alt_is_last = map[cpi->alt_fb_idx] == map[cpi->lst_fb_idx];
4504#endif // CONFIG_EXT_REFS
4505
Yaowu Xuf883b422016-08-30 14:01:10 -07004506 int flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004507
Yaowu Xuf883b422016-08-30 14:01:10 -07004508 if (gld_is_last || gld_is_alt) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004509
Yaowu Xuf883b422016-08-30 14:01:10 -07004510 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004511
Yaowu Xuf883b422016-08-30 14:01:10 -07004512 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004513
4514#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07004515 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004516
Yaowu Xuf883b422016-08-30 14:01:10 -07004517 if (last3_is_last || last3_is_last2 || last3_is_alt) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004518
Yaowu Xuf883b422016-08-30 14:01:10 -07004519 if (gld_is_last2 || gld_is_last3) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004520
Zoe Liu5a978832017-08-15 16:33:34 -07004521#if CONFIG_ONE_SIDED_COMPOUND && \
4522 !CONFIG_EXT_COMP_REFS // Changes LL & HL bitstream
Arild Fuldseth (arilfuld)3f429082017-04-28 15:54:28 +02004523 /* Allow biprediction between two identical frames (e.g. bwd_is_last = 1) */
4524 if (bwd_is_alt && (flags & AOM_BWD_FLAG)) flags &= ~AOM_BWD_FLAG;
Zoe Liu5a978832017-08-15 16:33:34 -07004525#else // !CONFIG_ONE_SIDED_COMPOUND || CONFIG_EXT_COMP_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004526 if ((bwd_is_last || bwd_is_last2 || bwd_is_last3 || bwd_is_gld ||
4527 bwd_is_alt) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004528 (flags & AOM_BWD_FLAG))
4529 flags &= ~AOM_BWD_FLAG;
Zoe Liu5a978832017-08-15 16:33:34 -07004530#endif // CONFIG_ONE_SIDED_COMPOUND && !CONFIG_EXT_COMP_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07004531
Zoe Liue9b15e22017-07-19 15:53:01 -07004532 if ((alt2_is_last || alt2_is_last2 || alt2_is_last3 || alt2_is_gld ||
4533 alt2_is_bwd || alt2_is_alt) &&
4534 (flags & AOM_ALT2_FLAG))
4535 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004536#endif // CONFIG_EXT_REFS
4537
4538 return flags;
4539}
4540
Yaowu Xuf883b422016-08-30 14:01:10 -07004541static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004542 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07004543 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07004544 // Note: The overrides are valid only for the next frame passed
4545 // to encode_frame_to_data_rate() function
4546 if (cpi->ext_refresh_frame_context_pending) {
4547 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4548 cpi->ext_refresh_frame_context_pending = 0;
4549 }
4550 if (cpi->ext_refresh_frame_flags_pending) {
4551 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4552 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4553 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
4554 cpi->ext_refresh_frame_flags_pending = 0;
4555 }
4556}
4557
Yaowu Xuf883b422016-08-30 14:01:10 -07004558static void set_arf_sign_bias(AV1_COMP *cpi) {
4559 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004560 int arf_sign_bias;
4561#if CONFIG_EXT_REFS
4562 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liu3ac20932017-08-30 16:35:55 -07004563 // The arf_sign_bias will be one for internal ARFs'
Zoe Liue9b15e22017-07-19 15:53:01 -07004564 arf_sign_bias = cpi->rc.source_alt_ref_active &&
4565 (!cpi->refresh_alt_ref_frame ||
4566 gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07004567#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004568 if ((cpi->oxcf.pass == 2) && cpi->multi_arf_allowed) {
4569 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4570 arf_sign_bias = cpi->rc.source_alt_ref_active &&
4571 (!cpi->refresh_alt_ref_frame ||
4572 (gf_group->rf_level[gf_group->index] == GF_ARF_LOW));
4573 } else {
4574 arf_sign_bias =
4575 (cpi->rc.source_alt_ref_active && !cpi->refresh_alt_ref_frame);
4576 }
Zoe Liu6cfaff92016-10-18 17:12:11 -07004577#endif // CONFIG_EXT_REFS
4578
Yaowu Xuc27fc142016-08-22 16:08:15 -07004579 cm->ref_frame_sign_bias[ALTREF_FRAME] = arf_sign_bias;
4580#if CONFIG_EXT_REFS
4581 cm->ref_frame_sign_bias[BWDREF_FRAME] = cm->ref_frame_sign_bias[ALTREF_FRAME];
Zoe Liue9b15e22017-07-19 15:53:01 -07004582 cm->ref_frame_sign_bias[ALTREF2_FRAME] =
4583 cm->ref_frame_sign_bias[ALTREF_FRAME];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004584#endif // CONFIG_EXT_REFS
4585}
4586
Yaowu Xuf883b422016-08-30 14:01:10 -07004587static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07004588 InterpFilter ifilter;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004589 int ref_total[TOTAL_REFS_PER_FRAME] = { 0 };
4590 MV_REFERENCE_FRAME ref;
4591 int mask = 0;
4592 int arf_idx = ALTREF_FRAME;
4593
Zoe Liu3ac20932017-08-30 16:35:55 -07004594#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07004595 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
4596 cpi->refresh_alt2_ref_frame)
Zoe Liu3ac20932017-08-30 16:35:55 -07004597#else // !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004598 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Zoe Liu3ac20932017-08-30 16:35:55 -07004599#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004600 return mask;
4601
Yaowu Xuc27fc142016-08-22 16:08:15 -07004602 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4603 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4604 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004605
4606 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
4607 if ((ref_total[LAST_FRAME] &&
4608 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
4609#if CONFIG_EXT_REFS
4610 (ref_total[LAST2_FRAME] == 0 ||
4611 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
4612 ref_total[LAST2_FRAME]) &&
4613 (ref_total[LAST3_FRAME] == 0 ||
4614 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
4615 ref_total[LAST3_FRAME]) &&
4616#endif // CONFIG_EXT_REFS
4617 (ref_total[GOLDEN_FRAME] == 0 ||
4618 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4619 ref_total[GOLDEN_FRAME]) &&
4620#if CONFIG_EXT_REFS
4621 (ref_total[BWDREF_FRAME] == 0 ||
4622 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
4623 ref_total[BWDREF_FRAME]) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004624 (ref_total[ALTREF2_FRAME] == 0 ||
4625 cpi->interp_filter_selected[ALTREF2_FRAME][ifilter] * 50 <
4626 ref_total[ALTREF2_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004627#endif // CONFIG_EXT_REFS
4628 (ref_total[ALTREF_FRAME] == 0 ||
4629 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
4630 ref_total[ALTREF_FRAME]))
4631 mask |= 1 << ifilter;
4632 }
4633 return mask;
4634}
4635
4636#define DUMP_RECON_FRAMES 0
4637
4638#if DUMP_RECON_FRAMES == 1
4639// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004640static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4641 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004642 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
4643 int h;
4644 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4645 FILE *f_recon = NULL;
4646
4647 if (recon_buf == NULL || !cm->show_frame) {
4648 printf("Frame %d is not ready or no show to dump.\n",
4649 cm->current_video_frame);
4650 return;
4651 }
4652
4653 if (cm->current_video_frame == 0) {
4654 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4655 printf("Unable to open file %s to write.\n", file_name);
4656 return;
4657 }
4658 } else {
4659 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4660 printf("Unable to open file %s to append.\n", file_name);
4661 return;
4662 }
4663 }
4664 printf(
4665 "\nFrame=%5d, encode_update_type[%5d]=%1d, show_existing_frame=%d, "
Zoe Liufcf5fa22017-06-26 16:00:38 -07004666 "source_alt_ref_active=%d, refresh_alt_ref_frame=%d, rf_level=%d, "
4667 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004668 cm->current_video_frame, cpi->twopass.gf_group.index,
4669 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liufcf5fa22017-06-26 16:00:38 -07004670 cm->show_existing_frame, cpi->rc.source_alt_ref_active,
4671 cpi->refresh_alt_ref_frame,
4672 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4673 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004674#if 0
4675 int ref_frame;
4676 printf("get_ref_frame_map_idx: [");
4677 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
4678 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
4679 printf(" ]\n");
4680 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
4681 printf("cm->ref_frame_map = [");
4682 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4683 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
4684 }
4685 printf(" ]\n");
4686#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004687
4688 // --- Y ---
4689 for (h = 0; h < cm->height; ++h) {
4690 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4691 f_recon);
4692 }
4693 // --- U ---
4694 for (h = 0; h < (cm->height >> 1); ++h) {
4695 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4696 f_recon);
4697 }
4698 // --- V ---
4699 for (h = 0; h < (cm->height >> 1); ++h) {
4700 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4701 f_recon);
4702 }
4703
4704 fclose(f_recon);
4705}
4706#endif // DUMP_RECON_FRAMES
4707
Thomas Davies028b57f2017-02-22 16:42:11 +00004708static void make_update_tile_list_enc(AV1_COMP *cpi, const int tile_rows,
4709 const int tile_cols,
4710 FRAME_CONTEXT *ec_ctxs[]) {
4711 int i;
4712 for (i = 0; i < tile_rows * tile_cols; ++i)
4713 ec_ctxs[i] = &cpi->tile_data[i].tctx;
4714}
4715
Yaowu Xuf883b422016-08-30 14:01:10 -07004716static void encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004717 uint8_t *dest, int skip_adapt,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004718 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004719 AV1_COMMON *const cm = &cpi->common;
4720 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004721 struct segmentation *const seg = &cm->seg;
Thomas Davies493623e2017-03-31 16:12:25 +01004722 FRAME_CONTEXT **tile_ctxs = aom_malloc(cm->tile_rows * cm->tile_cols *
4723 sizeof(&cpi->tile_data[0].tctx));
4724 aom_cdf_prob **cdf_ptrs =
4725 aom_malloc(cm->tile_rows * cm->tile_cols *
4726 sizeof(&cpi->tile_data[0].tctx.partition_cdf[0][0]));
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004727#if CONFIG_XIPHRC
4728 int frame_type;
4729 int drop_this_frame = 0;
Zoe Liue04abf72017-04-19 15:37:11 -07004730#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07004731 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004732 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004733
4734 // Set the arf sign bias for this frame.
4735 set_arf_sign_bias(cpi);
Fangwen Fu8d164de2016-12-14 13:40:54 -08004736#if CONFIG_TEMPMV_SIGNALING
4737 // frame type has been decided outside of this function call
4738 cm->cur_frame->intra_only = cm->frame_type == KEY_FRAME || cm->intra_only;
4739 cm->use_prev_frame_mvs =
4740 !cpi->oxcf.disable_tempmv && !cm->cur_frame->intra_only;
4741#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004742
4743#if CONFIG_EXT_REFS
4744 // NOTE:
4745 // (1) Move the setup of the ref_frame_flags upfront as it would be
4746 // determined by the current frame properties;
4747 // (2) The setup of the ref_frame_flags applies to both show_existing_frame's
4748 // and the other cases.
4749 if (cm->current_video_frame > 0)
4750 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4751
4752 if (cm->show_existing_frame) {
4753 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
4754 // BWDREF_FRAME in the reference frame buffer.
4755 cm->frame_type = INTER_FRAME;
4756 cm->show_frame = 1;
4757 cpi->frame_flags = *frame_flags;
4758
4759 // In the case of show_existing frame, we will not send fresh flag
4760 // to decoder. Any change in the reference frame buffer can be done by
4761 // switching the virtual indices.
4762
4763 cpi->refresh_last_frame = 0;
4764 cpi->refresh_golden_frame = 0;
4765 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07004766 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004767 cpi->refresh_alt_ref_frame = 0;
4768
4769 cpi->rc.is_bwd_ref_frame = 0;
4770 cpi->rc.is_last_bipred_frame = 0;
4771 cpi->rc.is_bipred_frame = 0;
4772
Jingning Han8f661602017-08-19 08:16:50 -07004773 restore_coding_context(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004774 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07004775 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004776
4777 // Set up frame to show to get ready for stats collection.
4778 cm->frame_to_show = get_frame_new_buffer(cm);
4779
4780#if DUMP_RECON_FRAMES == 1
4781 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4782 dump_filtered_recon_frames(cpi);
4783#endif // DUMP_RECON_FRAMES
4784
4785 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07004786 // NOTE:
4787 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
4788 // update has been done previously when handling the LAST_BIPRED_FRAME
4789 // right before BWDREF_FRAME (in the display order);
4790 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
4791 // update will be done when the following is called, which will exchange
4792 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
4793 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2, and
4794 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Yaowu Xuf883b422016-08-30 14:01:10 -07004795 av1_update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004796
4797 // Update frame flags
4798 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4799 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4800 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4801
4802 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4803
4804 // Update the frame type
4805 cm->last_frame_type = cm->frame_type;
4806
Yaowu Xuc27fc142016-08-22 16:08:15 -07004807 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4808 // to do post-encoding update accordingly.
4809 if (cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004810 av1_set_target_rate(cpi);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004811#if CONFIG_XIPHRC
Zoe Liue04abf72017-04-19 15:37:11 -07004812 frame_type = cm->frame_type == INTER_FRAME ? OD_P_FRAME : OD_I_FRAME;
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004813 drop_this_frame = od_enc_rc_update_state(
4814 &cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
4815 cpi->refresh_alt_ref_frame, frame_type, cpi->droppable);
4816#else
Yaowu Xuf883b422016-08-30 14:01:10 -07004817 av1_rc_postencode_update(cpi, *size);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004818#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004820
Yaowu Xuc27fc142016-08-22 16:08:15 -07004821 ++cm->current_video_frame;
4822
Jingning Hanf6214b92017-04-12 11:43:37 -07004823 aom_free(tile_ctxs);
4824 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004825 return;
4826 }
4827#endif // CONFIG_EXT_REFS
4828
4829 // Set default state for segment based loop filter update flags.
4830 cm->lf.mode_ref_delta_update = 0;
4831
4832 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4833 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4834
4835 // Set various flags etc to special state if it is a key frame.
4836 if (frame_is_intra_only(cm)) {
4837 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004838 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004839
4840 // If segmentation is enabled force a map update for key frames.
4841 if (seg->enabled) {
4842 seg->update_map = 1;
4843 seg->update_data = 1;
4844 }
4845
4846 // The alternate reference frame cannot be active for a key frame.
4847 cpi->rc.source_alt_ref_active = 0;
4848
4849 cm->error_resilient_mode = oxcf->error_resilient_mode;
4850
Thomas Daedea6a854b2017-06-22 17:49:11 -07004851#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07004852 // By default, encoder assumes decoder can use prev_mi.
4853 if (cm->error_resilient_mode) {
4854 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
4855 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_FORWARD;
4856 } else if (cm->intra_only) {
4857 // Only reset the current context.
4858 cm->reset_frame_context = RESET_FRAME_CONTEXT_CURRENT;
4859 }
Thomas Daedea6a854b2017-06-22 17:49:11 -07004860#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004861 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00004862 if (cpi->oxcf.mtu == 0) {
4863 cm->num_tg = cpi->oxcf.num_tile_groups;
4864 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08004865 // Use a default value for the purposes of weighting costs in probability
4866 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00004867 cm->num_tg = DEFAULT_MAX_NUM_TG;
4868 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004869
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08004870#if CONFIG_EXT_TILE
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004871 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
4872 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08004873#endif // CONFIG_EXT_TILE
4874
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004875#if CONFIG_XIPHRC
4876 if (drop_this_frame) {
4877 av1_rc_postencode_update_drop_frame(cpi);
4878 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07004879 aom_free(tile_ctxs);
4880 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004881 return;
4882 }
4883#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07004884 // For 1 pass CBR, check if we are dropping this frame.
4885 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004886 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004887 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004888 if (av1_rc_drop_frame(cpi)) {
4889 av1_rc_postencode_update_drop_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004890 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07004891 aom_free(tile_ctxs);
4892 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004893 return;
4894 }
4895 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00004896#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004897
Yaowu Xuf883b422016-08-30 14:01:10 -07004898 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004899
4900#if CONFIG_INTERNAL_STATS
4901 memset(cpi->mode_chosen_counts, 0,
4902 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4903#endif
4904
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004905#if CONFIG_REFERENCE_BUFFER
4906 {
4907 /* Non-normative definition of current_frame_id ("frame counter" with
4908 * wraparound) */
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004909 const int frame_id_length = FRAME_ID_LENGTH_MINUS7 + 7;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004910 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00004911 int lsb, msb;
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01004912/* quasi-random initialization of current_frame_id for a key frame */
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004913#if CONFIG_HIGHBITDEPTH
Alex Conversef77fd0b2017-04-20 11:00:24 -07004914 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
4915 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
4916 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004917 } else {
4918#endif
Alex Conversef77fd0b2017-04-20 11:00:24 -07004919 lsb = cpi->source->y_buffer[0] & 0xff;
4920 msb = cpi->source->y_buffer[1] & 0xff;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004921#if CONFIG_HIGHBITDEPTH
David Barker49a76562016-12-07 14:50:21 +00004922 }
Arild Fuldseth (arilfuld)bac17c12016-12-02 12:01:06 +01004923#endif
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004924 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004925 } else {
4926 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004927 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
4928 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004929 }
4930 }
4931#endif
4932
Fangwen Fu6160df22017-04-24 09:45:51 -07004933#if CONFIG_EXT_DELTA_Q
4934 cm->delta_q_present_flag = cpi->oxcf.deltaq_mode != NO_DELTA_Q;
Fangwen Fu231fe422017-04-24 17:52:29 -07004935 cm->delta_lf_present_flag = cpi->oxcf.deltaq_mode == DELTA_Q_LF;
Fangwen Fu6160df22017-04-24 09:45:51 -07004936#endif
4937
Yaowu Xuc27fc142016-08-22 16:08:15 -07004938 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
4939 encode_without_recode_loop(cpi);
4940 } else {
4941 encode_with_recode_loop(cpi, size, dest);
4942 }
4943
Yi Luo10e23002017-07-31 11:54:43 -07004944 cm->last_tile_cols = cm->tile_cols;
4945 cm->last_tile_rows = cm->tile_rows;
4946
Yaowu Xuc27fc142016-08-22 16:08:15 -07004947#ifdef OUTPUT_YUV_SKINMAP
4948 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004949 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004950 }
4951#endif // OUTPUT_YUV_SKINMAP
4952
4953 // Special case code to reduce pulsing when key frames are forced at a
4954 // fixed interval. Note the reconstruction error if it is the frame before
4955 // the force key frame
4956 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004957#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004958 if (cm->use_highbitdepth) {
4959 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07004960 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004961 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004962 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004963 }
4964#else
Alex Conversef77fd0b2017-04-20 11:00:24 -07004965 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02004966#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07004967 }
4968
4969 // If the encoder forced a KEY_FRAME decision
4970 if (cm->frame_type == KEY_FRAME) {
4971 cpi->refresh_last_frame = 1;
4972 }
4973
4974 cm->frame_to_show = get_frame_new_buffer(cm);
4975 cm->frame_to_show->color_space = cm->color_space;
anorkin76fb1262017-03-22 15:12:12 -07004976#if CONFIG_COLORSPACE_HEADERS
4977 cm->frame_to_show->transfer_function = cm->transfer_function;
4978 cm->frame_to_show->chroma_sample_position = cm->chroma_sample_position;
4979#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004980 cm->frame_to_show->color_range = cm->color_range;
4981 cm->frame_to_show->render_width = cm->render_width;
4982 cm->frame_to_show->render_height = cm->render_height;
4983
4984#if CONFIG_EXT_REFS
4985// TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
4986// off.
4987#endif // CONFIG_EXT_REFS
4988
4989 // Pick the loop filter level for the frame.
4990 loopfilter_frame(cpi, cm);
4991
4992 // Build the bitstream
Yaowu Xuf883b422016-08-30 14:01:10 -07004993 av1_pack_bitstream(cpi, dest, size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004994
Jingning Hanf6214b92017-04-12 11:43:37 -07004995 if (skip_adapt) {
Jingning Hanf6214b92017-04-12 11:43:37 -07004996 aom_free(tile_ctxs);
4997 aom_free(cdf_ptrs);
Jingning Hanf6214b92017-04-12 11:43:37 -07004998 return;
4999 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005000
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005001#if CONFIG_REFERENCE_BUFFER
5002 {
5003 int i;
5004 /* Update reference frame id values based on the value of refresh_mask */
5005 for (i = 0; i < REF_FRAMES; i++) {
5006 if ((cm->refresh_mask >> i) & 1) {
5007 cm->ref_frame_id[i] = cm->current_frame_id;
5008 }
5009 }
5010 }
5011#endif
5012
Yaowu Xuc27fc142016-08-22 16:08:15 -07005013#if DUMP_RECON_FRAMES == 1
5014 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5015 if (cm->show_frame) dump_filtered_recon_frames(cpi);
5016#endif // DUMP_RECON_FRAMES
5017
5018 if (cm->seg.update_map) update_reference_segmentation_map(cpi);
5019
5020 if (frame_is_intra_only(cm) == 0) {
5021 release_scaled_references(cpi);
5022 }
5023
Yaowu Xuf883b422016-08-30 14:01:10 -07005024 av1_update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005025
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005026#if CONFIG_ENTROPY_STATS
5027 av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
Zoe Liua56f9162017-06-21 22:49:57 -07005028 assert(cm->frame_context_idx < FRAME_CONTEXTS);
5029 av1_accumulate_frame_counts(&aggregate_fc_per_type[cm->frame_context_idx],
5030 &cm->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005031#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005032 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
hui sub53682f2017-08-01 17:09:18 -07005033#if CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07005034 av1_adapt_coef_probs(cm);
hui sub53682f2017-08-01 17:09:18 -07005035#endif // CONFIG_LV_MAP
Yaowu Xuf883b422016-08-30 14:01:10 -07005036 av1_adapt_intra_frame_probs(cm);
Thomas Davies028b57f2017-02-22 16:42:11 +00005037 make_update_tile_list_enc(cpi, cm->tile_rows, cm->tile_cols, tile_ctxs);
Thomas Davies493623e2017-03-31 16:12:25 +01005038 av1_average_tile_coef_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005039 cm->tile_rows * cm->tile_cols);
Thomas Davies493623e2017-03-31 16:12:25 +01005040 av1_average_tile_intra_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005041 cm->tile_rows * cm->tile_cols);
Yushin Chob188ea12017-03-13 13:45:23 -07005042#if CONFIG_PVQ
5043 av1_average_tile_pvq_cdfs(cpi->common.fc, tile_ctxs,
5044 cm->tile_rows * cm->tile_cols);
5045#endif // CONFIG_PVQ
hui suff0da2b2017-03-07 15:51:37 -08005046#if CONFIG_ADAPT_SCAN
5047 av1_adapt_scan_order(cm);
5048#endif // CONFIG_ADAPT_SCAN
Yaowu Xuc27fc142016-08-22 16:08:15 -07005049 }
5050
5051 if (!frame_is_intra_only(cm)) {
5052 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005053 av1_adapt_inter_frame_probs(cm);
5054 av1_adapt_mv_probs(cm, cm->allow_high_precision_mv);
Thomas Davies028b57f2017-02-22 16:42:11 +00005055 av1_average_tile_inter_cdfs(&cpi->common, cpi->common.fc, tile_ctxs,
Thomas Davies493623e2017-03-31 16:12:25 +01005056 cdf_ptrs, cm->tile_rows * cm->tile_cols);
5057 av1_average_tile_mv_cdfs(cpi->common.fc, tile_ctxs, cdf_ptrs,
Thomas Davies028b57f2017-02-22 16:42:11 +00005058 cm->tile_rows * cm->tile_cols);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005059 }
5060 }
5061
5062 if (cpi->refresh_golden_frame == 1)
5063 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5064 else
5065 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5066
5067 if (cpi->refresh_alt_ref_frame == 1)
5068 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5069 else
5070 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5071
5072#if CONFIG_EXT_REFS
5073 if (cpi->refresh_bwd_ref_frame == 1)
5074 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5075 else
5076 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
5077#endif // CONFIG_EXT_REFS
5078
5079#if !CONFIG_EXT_REFS
5080 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
5081#endif // !CONFIG_EXT_REFS
5082
Yaowu Xuc27fc142016-08-22 16:08:15 -07005083 cm->last_frame_type = cm->frame_type;
5084
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005085#if CONFIG_XIPHRC
5086 frame_type = cm->frame_type == KEY_FRAME ? OD_I_FRAME : OD_P_FRAME;
5087
5088 drop_this_frame =
5089 od_enc_rc_update_state(&cpi->od_rc, *size << 3, cpi->refresh_golden_frame,
5090 cpi->refresh_alt_ref_frame, frame_type, 0);
5091 if (drop_this_frame) {
5092 av1_rc_postencode_update_drop_frame(cpi);
5093 ++cm->current_video_frame;
Jingning Hanf6214b92017-04-12 11:43:37 -07005094 aom_free(tile_ctxs);
5095 aom_free(cdf_ptrs);
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005096 return;
5097 }
Zoe Liue04abf72017-04-19 15:37:11 -07005098#else // !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005099 av1_rc_postencode_update(cpi, *size);
Zoe Liue04abf72017-04-19 15:37:11 -07005100#endif // CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005101
5102#if 0
5103 output_frame_level_debug_stats(cpi);
5104#endif
5105
5106 if (cm->frame_type == KEY_FRAME) {
5107 // Tell the caller that the frame was coded as a key frame
5108 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5109 } else {
5110 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5111 }
5112
5113 // Clear the one shot update flags for segmentation map and mode/ref loop
5114 // filter deltas.
5115 cm->seg.update_map = 0;
5116 cm->seg.update_data = 0;
5117 cm->lf.mode_ref_delta_update = 0;
5118
Yaowu Xuc27fc142016-08-22 16:08:15 -07005119 if (cm->show_frame) {
5120#if CONFIG_EXT_REFS
5121// TODO(zoeliu): We may only swamp mi and prev_mi for those frames that are
5122// being used as reference.
5123#endif // CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005124 av1_swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005125 // Don't increment frame counters if this was an altref buffer
5126 // update not a real frame
5127 ++cm->current_video_frame;
5128 }
5129
5130#if CONFIG_EXT_REFS
5131 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005132 if (cm->is_reference_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005133#endif // CONFIG_EXT_REFS
5134 cm->prev_frame = cm->cur_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005135 // keep track of the last coded dimensions
5136 cm->last_width = cm->width;
5137 cm->last_height = cm->height;
5138
5139 // reset to normal state now that we are done.
5140 cm->last_show_frame = cm->show_frame;
5141#if CONFIG_EXT_REFS
5142 }
5143#endif // CONFIG_EXT_REFS
Yi Luo10e23002017-07-31 11:54:43 -07005144
Thomas Davies493623e2017-03-31 16:12:25 +01005145 aom_free(tile_ctxs);
5146 aom_free(cdf_ptrs);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005147}
5148
Yaowu Xuf883b422016-08-30 14:01:10 -07005149static void Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005150 int skip_adapt, unsigned int *frame_flags) {
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005151#if CONFIG_XIPHRC
5152 int64_t ip_count;
5153 int frame_type, is_golden, is_altref;
5154
5155 /* Not updated during init so update it here */
5156 if (cpi->oxcf.rc_mode == AOM_Q) cpi->od_rc.quality = cpi->oxcf.cq_level;
5157
5158 frame_type = od_frame_type(&cpi->od_rc, cpi->od_rc.cur_frame, &is_golden,
5159 &is_altref, &ip_count);
5160
5161 if (frame_type == OD_I_FRAME) {
5162 frame_type = KEY_FRAME;
5163 cpi->frame_flags &= FRAMEFLAGS_KEY;
5164 } else if (frame_type == OD_P_FRAME) {
5165 frame_type = INTER_FRAME;
5166 }
5167
5168 if (is_altref) {
5169 cpi->refresh_alt_ref_frame = 1;
5170 cpi->rc.source_alt_ref_active = 1;
5171 }
5172
5173 cpi->refresh_golden_frame = is_golden;
5174 cpi->common.frame_type = frame_type;
5175 if (is_golden) cpi->frame_flags &= FRAMEFLAGS_GOLDEN;
5176#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005177 if (cpi->oxcf.rc_mode == AOM_CBR) {
5178 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005179 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005180 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005181 }
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005182#endif
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005183 encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005184}
5185
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005186#if !CONFIG_XIPHRC
Yaowu Xuf883b422016-08-30 14:01:10 -07005187static void Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005188 unsigned int *frame_flags) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005189 encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005190
5191#if CONFIG_EXT_REFS
5192 // Do not do post-encoding update for those frames that do not have a spot in
5193 // a gf group, but note that an OVERLAY frame always has a spot in a gf group,
5194 // even when show_existing_frame is used.
5195 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005196 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005197 }
5198 check_show_existing_frame(cpi);
5199#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005200 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005201#endif // CONFIG_EXT_REFS
5202}
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005203#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005204
Yaowu Xuf883b422016-08-30 14:01:10 -07005205static void init_ref_frame_bufs(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005206 int i;
5207 BufferPool *const pool = cm->buffer_pool;
5208 cm->new_fb_idx = INVALID_IDX;
5209 for (i = 0; i < REF_FRAMES; ++i) {
5210 cm->ref_frame_map[i] = INVALID_IDX;
5211 pool->frame_bufs[i].ref_count = 0;
5212 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07005213#if CONFIG_HASH_ME
5214 for (i = 0; i < FRAME_BUFFERS; ++i) {
5215 av1_hash_table_init(&pool->frame_bufs[i].hash_table);
5216 }
5217#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005218}
5219
Yaowu Xuf883b422016-08-30 14:01:10 -07005220static void check_initial_width(AV1_COMP *cpi,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005221#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005222 int use_highbitdepth,
5223#endif
5224 int subsampling_x, int subsampling_y) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005225 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005226
5227 if (!cpi->initial_width ||
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005228#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005229 cm->use_highbitdepth != use_highbitdepth ||
5230#endif
5231 cm->subsampling_x != subsampling_x ||
5232 cm->subsampling_y != subsampling_y) {
5233 cm->subsampling_x = subsampling_x;
5234 cm->subsampling_y = subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005235#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005236 cm->use_highbitdepth = use_highbitdepth;
5237#endif
5238
5239 alloc_raw_frame_buffers(cpi);
5240 init_ref_frame_bufs(cm);
5241 alloc_util_frame_buffers(cpi);
5242
5243 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
5244
5245 cpi->initial_width = cm->width;
5246 cpi->initial_height = cm->height;
5247 cpi->initial_mbs = cm->MBs;
5248 }
5249}
5250
James Zern3e2613b2017-03-30 23:14:40 -07005251int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005252 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5253 int64_t end_time) {
5254 AV1_COMMON *const cm = &cpi->common;
5255 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005256 int res = 0;
5257 const int subsampling_x = sd->subsampling_x;
5258 const int subsampling_y = sd->subsampling_y;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005259#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005260 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
5261#endif
5262
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005263#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005264 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
5265#else
5266 check_initial_width(cpi, subsampling_x, subsampling_y);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005267#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005268
Yaowu Xuf883b422016-08-30 14:01:10 -07005269 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005270
Yaowu Xuf883b422016-08-30 14:01:10 -07005271 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005272#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005273 use_highbitdepth,
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005274#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005275 frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005276 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005277 aom_usec_timer_mark(&timer);
5278 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005279
5280 if ((cm->profile == PROFILE_0 || cm->profile == PROFILE_2) &&
5281 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005282 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005283 "Non-4:2:0 color format requires profile 1 or 3");
5284 res = -1;
5285 }
5286 if ((cm->profile == PROFILE_1 || cm->profile == PROFILE_3) &&
5287 (subsampling_x == 1 && subsampling_y == 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005288 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005289 "4:2:0 color format requires profile 0 or 2");
5290 res = -1;
5291 }
5292
5293 return res;
5294}
5295
Yaowu Xuf883b422016-08-30 14:01:10 -07005296static int frame_is_reference(const AV1_COMP *cpi) {
5297 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005298
5299 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
5300 cpi->refresh_golden_frame ||
5301#if CONFIG_EXT_REFS
Zoe Liu3ac20932017-08-30 16:35:55 -07005302 cpi->refresh_bwd_ref_frame || cpi->refresh_alt2_ref_frame ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07005303#endif // CONFIG_EXT_REFS
5304 cpi->refresh_alt_ref_frame || !cm->error_resilient_mode ||
5305 cm->lf.mode_ref_delta_update || cm->seg.update_map ||
5306 cm->seg.update_data;
5307}
5308
Yaowu Xuf883b422016-08-30 14:01:10 -07005309static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005310 const struct lookahead_entry *source) {
5311 int64_t this_duration;
5312 int step = 0;
5313
5314 if (source->ts_start == cpi->first_time_stamp_ever) {
5315 this_duration = source->ts_end - source->ts_start;
5316 step = 1;
5317 } else {
5318 int64_t last_duration =
5319 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5320
5321 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5322
5323 // do a step update if the duration changes by 10%
5324 if (last_duration)
5325 step = (int)((this_duration - last_duration) * 10 / last_duration);
5326 }
5327
5328 if (this_duration) {
5329 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005330 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005331 } else {
5332 // Average this frame's rate into the last second's average
5333 // frame rate. If we haven't seen 1 second yet, then average
5334 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005335 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005336 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5337 double avg_duration = 10000000.0 / cpi->framerate;
5338 avg_duration *= (interval - avg_duration + this_duration);
5339 avg_duration /= interval;
5340
Yaowu Xuf883b422016-08-30 14:01:10 -07005341 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005342 }
5343 }
5344 cpi->last_time_stamp_seen = source->ts_start;
5345 cpi->last_end_time_stamp_seen = source->ts_end;
5346}
5347
5348// Returns 0 if this is not an alt ref else the offset of the source frame
5349// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005350static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005351 RATE_CONTROL *const rc = &cpi->rc;
5352 int arf_src_index = 0;
5353 if (is_altref_enabled(cpi)) {
5354 if (cpi->oxcf.pass == 2) {
5355 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5356 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5357 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5358 }
5359 } else if (rc->source_alt_ref_pending) {
5360 arf_src_index = rc->frames_till_gf_update_due;
5361 }
5362 }
5363 return arf_src_index;
5364}
5365
5366#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07005367static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005368 int brf_src_index = 0;
5369 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5370
5371 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5372 // flag.
5373 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5374 if (cpi->oxcf.pass == 2) {
5375 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5376 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5377 } else {
5378 // TODO(zoeliu): To re-visit the setup for this scenario
5379 brf_src_index = cpi->rc.bipred_group_interval - 1;
5380 }
5381 }
5382
5383 return brf_src_index;
5384}
Zoe Liue9b15e22017-07-19 15:53:01 -07005385
Zoe Liue9b15e22017-07-19 15:53:01 -07005386// Returns 0 if this is not an alt ref else the offset of the source frame
5387// used as the arf midpoint.
5388static int get_arf2_src_index(AV1_COMP *cpi) {
5389 int arf2_src_index = 0;
5390 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5391 if (cpi->oxcf.pass == 2) {
5392 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5393 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5394 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5395 }
5396 }
5397 }
5398 return arf2_src_index;
5399}
Yaowu Xuc27fc142016-08-22 16:08:15 -07005400#endif // CONFIG_EXT_REFS
5401
Yaowu Xuf883b422016-08-30 14:01:10 -07005402static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005403 const struct lookahead_entry *source) {
5404 RATE_CONTROL *const rc = &cpi->rc;
5405
5406 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005407 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005408
5409 if (cpi->oxcf.pass == 2) {
5410 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5411 rc->is_src_frame_alt_ref =
5412#if CONFIG_EXT_REFS
5413 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Zoe Liu6cfaff92016-10-18 17:12:11 -07005414#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005415 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liu3ac20932017-08-30 16:35:55 -07005416#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005417 rc->is_src_frame_ext_arf =
5418 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Zoe Liu3ac20932017-08-30 16:35:55 -07005419#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005420 } else {
5421 rc->is_src_frame_alt_ref =
5422 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5423 }
5424
5425 if (rc->is_src_frame_alt_ref) {
5426 // Current frame is an ARF overlay frame.
5427 cpi->alt_ref_source = NULL;
5428
Zoe Liu3ac20932017-08-30 16:35:55 -07005429#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005430 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
5431 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
5432 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
5433 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
5434 cpi->refresh_last_frame = 1;
5435 } else {
Zoe Liu3ac20932017-08-30 16:35:55 -07005436#endif // CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005437 // Don't refresh the last buffer for an ARF overlay frame. It will
5438 // become the GF so preserve last as an alternative prediction option.
5439 cpi->refresh_last_frame = 0;
Zoe Liu3ac20932017-08-30 16:35:55 -07005440#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005441 }
Zoe Liu3ac20932017-08-30 16:35:55 -07005442#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005443 }
5444}
5445
5446#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005447extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5448 const unsigned char *img2, int img2_pitch,
5449 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005450
5451static void adjust_image_stat(double y, double u, double v, double all,
5452 ImageStat *s) {
5453 s->stat[Y] += y;
5454 s->stat[U] += u;
5455 s->stat[V] += v;
5456 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005457 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005458}
5459
Angie Chiang08a22a62017-07-17 17:29:17 -07005460static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005461 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005462 double samples = 0.0;
5463 uint32_t in_bit_depth = 8;
5464 uint32_t bit_depth = 8;
5465
Angie Chiang08a22a62017-07-17 17:29:17 -07005466#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07005467 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005468#endif
5469 cpi->bytes += frame_bytes;
5470
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005471#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005472 if (cm->use_highbitdepth) {
5473 in_bit_depth = cpi->oxcf.input_bit_depth;
5474 bit_depth = cm->bit_depth;
5475 }
5476#endif
5477 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005478 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005479 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5480 double y, u, v, frame_all;
5481
5482 cpi->count++;
5483 if (cpi->b_calculate_psnr) {
5484 PSNR_STATS psnr;
5485 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005486 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005487// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005488#if CONFIG_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07005489 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005490#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005491 aom_calc_psnr(orig, recon, &psnr);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005492#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005493
5494 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5495 &cpi->psnr);
5496 cpi->total_sq_error += psnr.sse[0];
5497 cpi->total_samples += psnr.samples[0];
5498 samples = psnr.samples[0];
5499// TODO(yaowu): unify these two versions into one.
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005500#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005501 if (cm->use_highbitdepth)
5502 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005503 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005504 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005505 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005506#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005507 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005508#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005509
Yaowu Xuf883b422016-08-30 14:01:10 -07005510 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005511 cpi->summed_quality += frame_ssim2 * weight;
5512 cpi->summed_weights += weight;
5513
5514#if 0
5515 {
5516 FILE *f = fopen("q_used.stt", "a");
5517 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
5518 cpi->common.current_video_frame, y2, u2, v2,
5519 frame_psnr2, frame_ssim2);
5520 fclose(f);
5521 }
5522#endif
5523 }
5524 if (cpi->b_calculate_blockiness) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005525#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005526 if (!cm->use_highbitdepth)
5527#endif
5528 {
5529 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005530 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5531 recon->y_stride, orig->y_width, orig->y_height);
5532 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005533 cpi->total_blockiness += frame_blockiness;
5534 }
5535
5536 if (cpi->b_calculate_consistency) {
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02005537#if CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07005538 if (!cm->use_highbitdepth)
5539#endif
5540 {
Yaowu Xuf883b422016-08-30 14:01:10 -07005541 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005542 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5543 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5544
5545 const double peak = (double)((1 << in_bit_depth) - 1);
5546 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005547 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005548 if (consistency > 0.0)
5549 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005550 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005551 cpi->total_inconsistency += this_inconsistency;
5552 }
5553 }
5554 }
5555
5556 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005557 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005558 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005559 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005560 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5561 }
5562}
5563#endif // CONFIG_INTERNAL_STATS
5564
Yaowu Xuf883b422016-08-30 14:01:10 -07005565int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5566 size_t *size, uint8_t *dest, int64_t *time_stamp,
5567 int64_t *time_end, int flush) {
5568 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5569 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005570 BufferPool *const pool = cm->buffer_pool;
5571 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07005572 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005573 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5574 struct lookahead_entry *last_source = NULL;
5575 struct lookahead_entry *source = NULL;
5576 int arf_src_index;
5577#if CONFIG_EXT_REFS
5578 int brf_src_index;
5579#endif // CONFIG_EXT_REFS
5580 int i;
5581
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005582#if CONFIG_XIPHRC
5583 cpi->od_rc.end_of_input = flush;
5584#endif
5585
Yaowu Xuc27fc142016-08-22 16:08:15 -07005586#if CONFIG_BITSTREAM_DEBUG
5587 assert(cpi->oxcf.max_threads == 0 &&
5588 "bitstream debug tool does not support multithreading");
5589 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07005590 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005591#endif
5592
Yaowu Xuf883b422016-08-30 14:01:10 -07005593 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005594
James Zern01a9d702017-08-25 19:09:33 +00005595 av1_set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005596
5597 // Is multi-arf enabled.
5598 // Note that at the moment multi_arf is only configured for 2 pass VBR
5599 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
5600 cpi->multi_arf_allowed = 1;
5601 else
5602 cpi->multi_arf_allowed = 0;
5603
Thomas Daedea6a854b2017-06-22 17:49:11 -07005604// Normal defaults
5605#if !CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005606 cm->reset_frame_context = RESET_FRAME_CONTEXT_NONE;
Thomas Daedea6a854b2017-06-22 17:49:11 -07005607#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005608 cm->refresh_frame_context =
5609 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
5610 ? REFRESH_FRAME_CONTEXT_FORWARD
5611 : REFRESH_FRAME_CONTEXT_BACKWARD;
5612
5613 cpi->refresh_last_frame = 1;
5614 cpi->refresh_golden_frame = 0;
5615#if CONFIG_EXT_REFS
5616 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005617 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005618#endif // CONFIG_EXT_REFS
5619 cpi->refresh_alt_ref_frame = 0;
5620
Zoe Liue04abf72017-04-19 15:37:11 -07005621#if CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005622 if (oxcf->pass == 2 && cm->show_existing_frame) {
5623 // Manage the source buffer and flush out the source frame that has been
5624 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07005625 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005626 *size = 0;
5627 return -1;
5628 }
Alex Conversef77fd0b2017-04-20 11:00:24 -07005629 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005630 // TODO(zoeliu): To track down to determine whether it's needed to adjust
5631 // the frame rate.
5632 *time_stamp = source->ts_start;
5633 *time_end = source->ts_end;
5634
5635 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005636 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005637
5638 // Find a free buffer for the new frame, releasing the reference previously
5639 // held.
5640 if (cm->new_fb_idx != INVALID_IDX) {
5641 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5642 }
5643 cm->new_fb_idx = get_free_fb(cm);
5644
5645 if (cm->new_fb_idx == INVALID_IDX) return -1;
5646
5647 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005648 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005649
5650 // Start with a 0 size frame.
5651 *size = 0;
5652
5653 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005654 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005655
5656 Pass2Encode(cpi, size, dest, frame_flags);
5657
5658 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
5659
5660#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07005661 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005662#endif // CONFIG_INTERNAL_STATS
5663
5664 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005665 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005666
5667 cm->show_existing_frame = 0;
5668 return 0;
5669 }
Zoe Liue04abf72017-04-19 15:37:11 -07005670#endif // CONFIG_EXT_REFS && !CONFIG_XIPHRC
Yaowu Xuc27fc142016-08-22 16:08:15 -07005671
5672 // Should we encode an arf frame.
5673 arf_src_index = get_arf_src_index(cpi);
5674 if (arf_src_index) {
5675 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005676 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005677 // Avoid creating an alt-ref if there's a forced keyframe pending.
5678 if (e == NULL) {
5679 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07005680 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005681 arf_src_index = 0;
5682 flush = 1;
5683 break;
5684 }
5685 }
5686 }
5687
5688 if (arf_src_index) {
5689 assert(arf_src_index <= rc->frames_to_key);
5690
Yaowu Xuf883b422016-08-30 14:01:10 -07005691 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005692 cpi->alt_ref_source = source;
5693
5694 if (oxcf->arnr_max_frames > 0) {
Todd Nguyen302d0972017-06-16 16:16:29 -07005695// Produce the filtered ARF frame.
5696#if CONFIG_BGSPRITE
5697 int bgsprite_ret = av1_background_sprite(cpi, arf_src_index);
Todd Nguyen2fc23092017-07-11 12:00:36 -07005698 // Do temporal filter if bgsprite not generated.
5699 if (bgsprite_ret != 0)
Todd Nguyen302d0972017-06-16 16:16:29 -07005700#endif // CONFIG_BGSPRITE
Todd Nguyen8493f912017-07-20 12:45:12 -07005701 av1_temporal_filter(cpi,
5702#if CONFIG_BGSPRITE
Todd Nguyen1fd99c22017-07-31 17:50:41 -07005703 NULL, &cpi->alt_ref_buffer,
Todd Nguyen8493f912017-07-20 12:45:12 -07005704#endif // CONFIG_BGSPRITE
5705 arf_src_index);
Yaowu Xuf883b422016-08-30 14:01:10 -07005706 aom_extend_frame_borders(&cpi->alt_ref_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005707 force_src_buffer = &cpi->alt_ref_buffer;
5708 }
5709
5710 cm->show_frame = 0;
5711 cm->intra_only = 0;
5712 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005713 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005714 cpi->refresh_golden_frame = 0;
5715#if CONFIG_EXT_REFS
5716 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005717 cpi->refresh_alt2_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005718#endif // CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005719 rc->is_src_frame_alt_ref = 0;
5720 }
5721 rc->source_alt_ref_pending = 0;
5722 }
5723
5724#if CONFIG_EXT_REFS
Zoe Liue9b15e22017-07-19 15:53:01 -07005725 // Should we encode an arf2 frame.
5726 arf_src_index = get_arf2_src_index(cpi);
5727 if (arf_src_index) {
5728 for (i = 0; i <= arf_src_index; ++i) {
5729 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
5730 // Avoid creating an alt-ref if there's a forced keyframe pending.
5731 if (e == NULL) {
5732 break;
5733 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
5734 arf_src_index = 0;
5735 flush = 1;
5736 break;
5737 }
5738 }
5739 }
5740
5741 if (arf_src_index) {
5742 assert(arf_src_index <= rc->frames_to_key);
5743
5744 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
5745 cpi->alt_ref_source = source;
5746
5747 if (oxcf->arnr_max_frames > 0) {
5748 // Produce the filtered ARF frame.
Zoe Liu3ac20932017-08-30 16:35:55 -07005749 av1_temporal_filter(cpi,
5750#if CONFIG_BGSPRITE
5751 NULL, NULL,
5752#endif // CONFIG_BGSPRITE
5753 arf_src_index);
Zoe Liue9b15e22017-07-19 15:53:01 -07005754 aom_extend_frame_borders(&cpi->alt_ref_buffer);
5755 force_src_buffer = &cpi->alt_ref_buffer;
5756 }
5757
5758 cm->show_frame = 0;
5759 cm->intra_only = 0;
5760 cpi->refresh_alt2_ref_frame = 1;
5761 cpi->refresh_last_frame = 0;
5762 cpi->refresh_golden_frame = 0;
5763 cpi->refresh_bwd_ref_frame = 0;
5764 cpi->refresh_alt_ref_frame = 0;
5765 rc->is_src_frame_alt_ref = 0;
5766 rc->is_src_frame_ext_arf = 0;
5767 }
5768 rc->source_alt_ref_pending = 0;
5769 }
Zoe Liue9b15e22017-07-19 15:53:01 -07005770
Yaowu Xuc27fc142016-08-22 16:08:15 -07005771 rc->is_bwd_ref_frame = 0;
5772 brf_src_index = get_brf_src_index(cpi);
5773 if (brf_src_index) {
5774 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07005775 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005776 cm->show_frame = 0;
5777 cm->intra_only = 0;
5778
5779 cpi->refresh_bwd_ref_frame = 1;
5780 cpi->refresh_last_frame = 0;
5781 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005782 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005783 cpi->refresh_alt_ref_frame = 0;
5784
5785 rc->is_bwd_ref_frame = 1;
5786 }
5787 }
5788#endif // CONFIG_EXT_REFS
5789
5790 if (!source) {
5791 // Get last frame source.
5792 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005793 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005794 return -1;
5795 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005796 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005797 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005798 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005799
5800 if (source != NULL) {
5801 cm->show_frame = 1;
5802 cm->intra_only = 0;
5803
5804 // Check to see if the frame should be encoded as an arf overlay.
5805 check_src_altref(cpi, source);
5806 }
5807 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005808 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005809 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005810 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005811 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
5812
5813 *time_stamp = source->ts_start;
5814 *time_end = source->ts_end;
Yaowu Xuf883b422016-08-30 14:01:10 -07005815 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005816
5817 } else {
5818 *size = 0;
5819 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005820#if CONFIG_XIPHRC
5821 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 1);
5822#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005823 av1_end_first_pass(cpi); /* get last stats packet */
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00005824#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005825 cpi->twopass.first_pass_done = 1;
5826 }
5827 return -1;
5828 }
5829
5830 if (source->ts_start < cpi->first_time_stamp_ever) {
5831 cpi->first_time_stamp_ever = source->ts_start;
5832 cpi->last_end_time_stamp_seen = source->ts_start;
5833 }
5834
5835 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005836 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005837
5838 // adjust frame rates based on timestamps given
5839 if (cm->show_frame) adjust_frame_rate(cpi, source);
5840
5841 // Find a free buffer for the new frame, releasing the reference previously
5842 // held.
5843 if (cm->new_fb_idx != INVALID_IDX) {
5844 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5845 }
5846 cm->new_fb_idx = get_free_fb(cm);
5847
5848 if (cm->new_fb_idx == INVALID_IDX) return -1;
5849
5850 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005851#if CONFIG_HIGHBITDEPTH && CONFIG_GLOBAL_MOTION
5852 cm->cur_frame->buf.buf_8bit_valid = 0;
5853#endif
Zoe Liu3ac20932017-08-30 16:35:55 -07005854#if !CONFIG_EXT_REFS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005855 if (cpi->multi_arf_allowed) {
5856 if (cm->frame_type == KEY_FRAME) {
5857 init_buffer_indices(cpi);
5858 } else if (oxcf->pass == 2) {
5859 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5860 cpi->alt_fb_idx = gf_group->arf_ref_idx[gf_group->index];
5861 }
5862 }
Zoe Liu3ac20932017-08-30 16:35:55 -07005863#endif // !CONFIG_EXT_REFS
Zoe Liu6cfaff92016-10-18 17:12:11 -07005864
Yaowu Xuc27fc142016-08-22 16:08:15 -07005865 // Start with a 0 size frame.
5866 *size = 0;
5867
5868 cpi->frame_flags = *frame_flags;
5869
5870 if (oxcf->pass == 2) {
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005871#if CONFIG_XIPHRC
5872 if (od_enc_rc_2pass_in(&cpi->od_rc) < 0) return -1;
5873 }
5874#else
Yaowu Xuf883b422016-08-30 14:01:10 -07005875 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005876 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07005877 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005878 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005879#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005880
5881 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
5882 for (i = 0; i < TOTAL_REFS_PER_FRAME; ++i)
5883 cpi->scaled_ref_idx[i] = INVALID_IDX;
5884 }
5885
5886#if CONFIG_AOM_QM
5887 cm->using_qmatrix = cpi->oxcf.using_qm;
5888 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
5889 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
5890#endif
5891
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005892#if CONFIG_REFERENCE_BUFFER
5893 if (*time_stamp == 0) {
5894 cpi->common.current_frame_id = -1;
5895 }
5896#endif
5897
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005898#if CONFIG_XIPHRC
5899 if (oxcf->pass == 1) {
5900 size_t tmp;
5901 if (cpi->od_rc.cur_frame == 0) Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
5902 cpi->od_rc.firstpass_quant = cpi->od_rc.target_quantizer;
5903 Pass0Encode(cpi, &tmp, dest, 0, frame_flags);
5904 od_enc_rc_2pass_out(&cpi->od_rc, cpi->output_pkt_list, 0);
5905 } else if (oxcf->pass == 2) {
5906 Pass0Encode(cpi, size, dest, 0, frame_flags);
5907 } else {
5908 if (cpi->od_rc.cur_frame == 0) {
5909 size_t tmp;
5910 Pass0Encode(cpi, &tmp, dest, 1, frame_flags);
5911 }
5912 Pass0Encode(cpi, size, dest, 0, frame_flags);
5913 }
5914#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07005915 if (oxcf->pass == 1) {
5916 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07005917 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005918 } else if (oxcf->pass == 2) {
5919 Pass2Encode(cpi, size, dest, frame_flags);
5920 } else {
5921 // One pass encode
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005922 Pass0Encode(cpi, size, dest, 0, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005923 }
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005924#endif
RogerZhoucc5d35d2017-08-07 22:20:15 -07005925#if CONFIG_HASH_ME
5926 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
5927 // add to hash table
5928 const int pic_width = cpi->source->y_crop_width;
5929 const int pic_height = cpi->source->y_crop_height;
5930 uint32_t *block_hash_values[2][2];
5931 int8_t *is_block_same[2][3];
5932 int k, j;
5933
5934 for (k = 0; k < 2; k++) {
5935 for (j = 0; j < 2; j++) {
5936 CHECK_MEM_ERROR(cm, block_hash_values[k][j],
5937 aom_malloc(sizeof(uint32_t) * pic_width * pic_height));
5938 }
5939
5940 for (j = 0; j < 3; j++) {
5941 CHECK_MEM_ERROR(cm, is_block_same[k][j],
5942 aom_malloc(sizeof(int8_t) * pic_width * pic_height));
5943 }
5944 }
5945
5946 av1_hash_table_create(&cm->cur_frame->hash_table);
5947 av1_generate_block_2x2_hash_value(cpi->source, block_hash_values[0],
5948 is_block_same[0]);
5949 av1_generate_block_hash_value(cpi->source, 4, block_hash_values[0],
5950 block_hash_values[1], is_block_same[0],
5951 is_block_same[1]);
5952 av1_generate_block_hash_value(cpi->source, 8, block_hash_values[1],
5953 block_hash_values[0], is_block_same[1],
5954 is_block_same[0]);
5955 av1_add_to_hash_map_by_row_with_precal_data(
5956 &cm->cur_frame->hash_table, block_hash_values[0], is_block_same[0][2],
5957 pic_width, pic_height, 8);
5958 av1_generate_block_hash_value(cpi->source, 16, block_hash_values[0],
5959 block_hash_values[1], is_block_same[0],
5960 is_block_same[1]);
5961 av1_add_to_hash_map_by_row_with_precal_data(
5962 &cm->cur_frame->hash_table, block_hash_values[1], is_block_same[1][2],
5963 pic_width, pic_height, 16);
5964 av1_generate_block_hash_value(cpi->source, 32, block_hash_values[1],
5965 block_hash_values[0], is_block_same[1],
5966 is_block_same[0]);
5967 av1_add_to_hash_map_by_row_with_precal_data(
5968 &cm->cur_frame->hash_table, block_hash_values[0], is_block_same[0][2],
5969 pic_width, pic_height, 32);
5970 av1_generate_block_hash_value(cpi->source, 64, block_hash_values[0],
5971 block_hash_values[1], is_block_same[0],
5972 is_block_same[1]);
5973 av1_add_to_hash_map_by_row_with_precal_data(
5974 &cm->cur_frame->hash_table, block_hash_values[1], is_block_same[1][2],
5975 pic_width, pic_height, 64);
5976
5977 for (k = 0; k < 2; k++) {
5978 for (j = 0; j < 2; j++) {
5979 aom_free(block_hash_values[k][j]);
5980 }
5981
5982 for (j = 0; j < 3; j++) {
5983 aom_free(is_block_same[k][j]);
5984 }
5985 }
5986 }
5987
5988#endif
5989
Thomas Daededa4d8b92017-06-05 15:44:14 -07005990#if CONFIG_NO_FRAME_CONTEXT_SIGNALING
Thomas Daedea6a854b2017-06-22 17:49:11 -07005991 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07005992#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07005993 if (!cm->error_resilient_mode)
5994 cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
Thomas Daededa4d8b92017-06-05 15:44:14 -07005995#endif // CONFIG_NO_FRAME_CONTEXT_SIGNALING
Yaowu Xuc27fc142016-08-22 16:08:15 -07005996
5997 // No frame encoded, or frame was dropped, release scaled references.
5998 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
5999 release_scaled_references(cpi);
6000 }
6001
6002 if (*size > 0) {
6003 cpi->droppable = !frame_is_reference(cpi);
6004 }
6005
Yaowu Xuf883b422016-08-30 14:01:10 -07006006 aom_usec_timer_mark(&cmptimer);
6007 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006008
6009 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6010 generate_psnr_packet(cpi);
6011
6012#if CONFIG_INTERNAL_STATS
6013 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006014 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006015 }
6016#endif // CONFIG_INTERNAL_STATS
6017
Rostislav Pehlivanov002e7b72017-02-15 19:45:54 +00006018#if CONFIG_XIPHRC
6019 cpi->od_rc.cur_frame++;
6020#endif
6021
Yaowu Xuf883b422016-08-30 14:01:10 -07006022 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006023
6024 return 0;
6025}
6026
Yaowu Xuf883b422016-08-30 14:01:10 -07006027int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6028 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006029 if (!cm->show_frame) {
6030 return -1;
6031 } else {
6032 int ret;
6033 if (cm->frame_to_show) {
6034 *dest = *cm->frame_to_show;
6035 dest->y_width = cm->width;
6036 dest->y_height = cm->height;
6037 dest->uv_width = cm->width >> cm->subsampling_x;
6038 dest->uv_height = cm->height >> cm->subsampling_y;
6039 ret = 0;
6040 } else {
6041 ret = -1;
6042 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006043 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006044 return ret;
6045 }
6046}
6047
Yaowu Xuf883b422016-08-30 14:01:10 -07006048int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006049 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6050
6051 *frame =
6052 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6053 return 0;
6054}
6055
Yaowu Xuf883b422016-08-30 14:01:10 -07006056int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6057 AOM_SCALING vert_mode) {
6058 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006059 int hr = 0, hs = 0, vr = 0, vs = 0;
6060
6061 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6062
6063 Scale2Ratio(horiz_mode, &hr, &hs);
6064 Scale2Ratio(vert_mode, &vr, &vs);
6065
6066 // always go to the next whole number
6067 cm->width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6068 cm->height = (vs - 1 + cpi->oxcf.height * vr) / vs;
6069 assert(cm->width <= cpi->initial_width);
6070 assert(cm->height <= cpi->initial_height);
6071
6072 update_frame_size(cpi);
6073
6074 return 0;
6075}
6076
Fergus Simpson0a30c882017-05-16 16:54:51 -07006077int av1_set_size_literal(AV1_COMP *cpi, int width, int height) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006078 AV1_COMMON *cm = &cpi->common;
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006079#if CONFIG_HIGHBITDEPTH
Debargha Mukherjee887069f2017-06-16 18:54:36 -07006080 check_initial_width(cpi, cm->use_highbitdepth, cm->subsampling_x,
6081 cm->subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006082#else
Debargha Mukherjee887069f2017-06-16 18:54:36 -07006083 check_initial_width(cpi, cm->subsampling_x, cm->subsampling_y);
Sebastien Alaiwan71e87842017-04-12 16:03:28 +02006084#endif // CONFIG_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07006085
Fergus Simpson0a30c882017-05-16 16:54:51 -07006086 if (width <= 0 || height <= 0) return 1;
6087
6088 cm->width = width;
6089 if (cm->width > cpi->initial_width) {
6090 cm->width = cpi->initial_width;
6091 printf("Warning: Desired width too large, changed to %d\n", cm->width);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006092 }
6093
Fergus Simpson0a30c882017-05-16 16:54:51 -07006094 cm->height = height;
6095 if (cm->height > cpi->initial_height) {
6096 cm->height = cpi->initial_height;
6097 printf("Warning: Desired height too large, changed to %d\n", cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006098 }
Fergus Simpson0a30c882017-05-16 16:54:51 -07006099
Yaowu Xuc27fc142016-08-22 16:08:15 -07006100 assert(cm->width <= cpi->initial_width);
6101 assert(cm->height <= cpi->initial_height);
6102
6103 update_frame_size(cpi);
6104
6105 return 0;
6106}
6107
Yaowu Xuf883b422016-08-30 14:01:10 -07006108int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006109
Yaowu Xuf883b422016-08-30 14:01:10 -07006110void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006111 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07006112 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF)) {
6113 int ref = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006114
Yaowu Xuf883b422016-08-30 14:01:10 -07006115 if (flags & AOM_EFLAG_NO_REF_LAST) {
6116 ref ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006117#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006118 ref ^= AOM_LAST2_FLAG;
6119 ref ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006120#endif // CONFIG_EXT_REFS
6121 }
6122
Yaowu Xuf883b422016-08-30 14:01:10 -07006123 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006124
Yaowu Xuf883b422016-08-30 14:01:10 -07006125 if (flags & AOM_EFLAG_NO_REF_ARF) ref ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006126
Yaowu Xuf883b422016-08-30 14:01:10 -07006127 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006128 }
6129
6130 if (flags &
Yaowu Xuf883b422016-08-30 14:01:10 -07006131 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF |
6132 AOM_EFLAG_FORCE_GF | AOM_EFLAG_FORCE_ARF)) {
6133 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006134
Yaowu Xuf883b422016-08-30 14:01:10 -07006135 if (flags & AOM_EFLAG_NO_UPD_LAST) {
6136 upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006137#if CONFIG_EXT_REFS
Yaowu Xuf883b422016-08-30 14:01:10 -07006138 upd ^= AOM_LAST2_FLAG;
6139 upd ^= AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006140#endif // CONFIG_EXT_REFS
6141 }
6142
Yaowu Xuf883b422016-08-30 14:01:10 -07006143 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006144
Yaowu Xuf883b422016-08-30 14:01:10 -07006145 if (flags & AOM_EFLAG_NO_UPD_ARF) upd ^= AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006146
Yaowu Xuf883b422016-08-30 14:01:10 -07006147 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006148 }
6149
Yaowu Xuf883b422016-08-30 14:01:10 -07006150 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6151 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006152 }
6153}