blob: 42993464f1a5eed7fc3d43589d05deef2efe5709 [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"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010019#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020#include "av1/common/filter.h"
21#include "av1/common/idct.h"
22#include "av1/common/reconinter.h"
23#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070024#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070025#include "av1/common/tile_common.h"
26
27#include "av1/encoder/aq_complexity.h"
28#include "av1/encoder/aq_cyclicrefresh.h"
29#include "av1/encoder/aq_variance.h"
30#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070031#include "av1/encoder/context_tree.h"
32#include "av1/encoder/encodeframe.h"
33#include "av1/encoder/encodemv.h"
34#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070035#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070036#include "av1/encoder/ethread.h"
37#include "av1/encoder/firstpass.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070038#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070039#include "av1/encoder/mbgraph.h"
40#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070041#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070042#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070043#include "av1/encoder/ratectrl.h"
44#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070045#include "av1/encoder/segmentation.h"
46#include "av1/encoder/speed_features.h"
47#include "av1/encoder/temporal_filter.h"
48
Yaowu Xuf883b422016-08-30 14:01:10 -070049#include "./av1_rtcd.h"
50#include "./aom_dsp_rtcd.h"
51#include "./aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070052#include "aom_dsp/psnr.h"
53#if CONFIG_INTERNAL_STATS
54#include "aom_dsp/ssim.h"
55#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -080056#include "av1/encoder/grain_test_vectors.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070057#include "aom_dsp/aom_dsp_common.h"
58#include "aom_dsp/aom_filter.h"
Jingning Han1aab8182016-06-03 11:09:06 -070059#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070060#include "aom_ports/mem.h"
61#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070062#include "aom_scale/aom_scale.h"
Angie Chiang5b5f4df2017-12-06 10:41:12 -080063#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070064#include "aom_util/debug_util.h"
Angie Chiang5b5f4df2017-12-06 10:41:12 -080065#endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070066
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000067#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000068
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080069#if CONFIG_ENTROPY_STATS
70FRAME_COUNTS aggregate_fc;
71#endif // CONFIG_ENTROPY_STATS
72
Yaowu Xuc27fc142016-08-22 16:08:15 -070073#define AM_SEGMENT_ID_INACTIVE 7
74#define AM_SEGMENT_ID_ACTIVE 0
75
Johannb0ef6ff2018-02-08 14:32:21 -080076// Whether to use high precision mv for altref computation.
77#define ALTREF_HIGH_PRECISION_MV 1
78
79// Q threshold for high precision mv. Choose a very high value for now so that
80// HIGH_PRECISION is always chosen.
81#define HIGH_PRECISION_MV_QTHRESH 200
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070082
Yaowu Xuc27fc142016-08-22 16:08:15 -070083// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070084#ifdef OUTPUT_YUV_SKINMAP
85FILE *yuv_skinmap_file = NULL;
86#endif
87#ifdef OUTPUT_YUV_REC
88FILE *yuv_rec_file;
89#define FILE_NAME_LEN 100
90#endif
91
Urvang Joshib5ed3502016-10-17 16:38:05 -070092#if CONFIG_INTERNAL_STATS
93typedef enum { Y, U, V, ALL } STAT_TYPE;
94#endif // CONFIG_INTERNAL_STATS
95
Yaowu Xuf883b422016-08-30 14:01:10 -070096static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -070097 switch (mode) {
98 case NORMAL:
99 *hr = 1;
100 *hs = 1;
101 break;
102 case FOURFIVE:
103 *hr = 4;
104 *hs = 5;
105 break;
106 case THREEFIVE:
107 *hr = 3;
108 *hs = 5;
109 break;
110 case ONETWO:
111 *hr = 1;
112 *hs = 2;
113 break;
114 default:
115 *hr = 1;
116 *hs = 1;
117 assert(0);
118 break;
119 }
120}
121
122// Mark all inactive blocks as active. Other segmentation features may be set
123// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700124static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700125 unsigned char *const seg_map = cpi->segmentation_map;
126 int i;
127 if (cpi->active_map.enabled || cpi->active_map.update)
128 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
129 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
130 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
131}
132
Yaowu Xuf883b422016-08-30 14:01:10 -0700133static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700134 struct segmentation *const seg = &cpi->common.seg;
135 unsigned char *const seg_map = cpi->segmentation_map;
136 const unsigned char *const active_map = cpi->active_map.map;
137 int i;
138
139 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
140
141 if (frame_is_intra_only(&cpi->common)) {
142 cpi->active_map.enabled = 0;
143 cpi->active_map.update = 1;
144 }
145
146 if (cpi->active_map.update) {
147 if (cpi->active_map.enabled) {
148 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
149 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700150 av1_enable_segmentation(seg);
151 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700152 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
153 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
154 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
155 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
156
157 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
158 -MAX_LOOP_FILTER);
159 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
160 -MAX_LOOP_FILTER);
161 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
162 -MAX_LOOP_FILTER);
163 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
164 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700165 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700166 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700167 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
168 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
169 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
170 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171 if (seg->enabled) {
172 seg->update_data = 1;
173 seg->update_map = 1;
174 }
175 }
176 cpi->active_map.update = 0;
177 }
178}
179
Yaowu Xuf883b422016-08-30 14:01:10 -0700180int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
181 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700182 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
183 unsigned char *const active_map_8x8 = cpi->active_map.map;
184 const int mi_rows = cpi->common.mi_rows;
185 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700186 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
187 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188 cpi->active_map.update = 1;
189 if (new_map_16x16) {
190 int r, c;
191 for (r = 0; r < mi_rows; ++r) {
192 for (c = 0; c < mi_cols; ++c) {
193 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700194 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195 ? AM_SEGMENT_ID_ACTIVE
196 : AM_SEGMENT_ID_INACTIVE;
197 }
198 }
199 cpi->active_map.enabled = 1;
200 } else {
201 cpi->active_map.enabled = 0;
202 }
203 return 0;
204 } else {
205 return -1;
206 }
207}
208
Yaowu Xuf883b422016-08-30 14:01:10 -0700209int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
210 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700211 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
212 new_map_16x16) {
213 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
214 const int mi_rows = cpi->common.mi_rows;
215 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700216 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
217 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
218
Yaowu Xuc27fc142016-08-22 16:08:15 -0700219 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
220 if (cpi->active_map.enabled) {
221 int r, c;
222 for (r = 0; r < mi_rows; ++r) {
223 for (c = 0; c < mi_cols; ++c) {
224 // Cyclic refresh segments are considered active despite not having
225 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700226 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700227 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
228 }
229 }
230 }
231 return 0;
232 } else {
233 return -1;
234 }
235}
236
Yaowu Xu45295c32018-03-29 12:06:10 -0700237static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
238 int cur_frame_force_integer_mv) {
James Zern01a9d702017-08-25 19:09:33 +0000239 MACROBLOCK *const mb = &cpi->td.mb;
Hui Su50361152018-03-02 11:01:42 -0800240 cpi->common.allow_high_precision_mv =
241 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Rupert Swarbricka84faf22017-12-11 13:56:40 +0000242 const int copy_hp =
243 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Jingning Hanf050fc12018-03-09 14:53:33 -0800244 int *(*src)[2] = copy_hp ? &mb->nmvcost_hp : &mb->nmvcost;
245 mb->mv_cost_stack = *src;
James Zern01a9d702017-08-25 19:09:33 +0000246}
247
Yaowu Xuf883b422016-08-30 14:01:10 -0700248static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700249 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700250 return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100251#if CONFIG_FILEOPTIONS
252 if (cpi->common.options && cpi->common.options->ext_partition)
253#endif
254 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
255 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256
Yaowu Xuf883b422016-08-30 14:01:10 -0700257 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700258
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100259// TODO(any): Possibly could improve this with a heuristic.
260#if CONFIG_FILEOPTIONS
261 if (cpi->common.options && !cpi->common.options->ext_partition)
262 return BLOCK_64X64;
263#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700265}
266
Yaowu Xuf883b422016-08-30 14:01:10 -0700267static void setup_frame(AV1_COMP *cpi) {
268 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800269 // Set up entropy context depending on frame type. The decoder mandates
270 // the use of the default context, index 0, for keyframes and inter
271 // frames where the error_resilient_mode or intra_only flag is set. For
272 // other inter-frames the encoder currently uses only two contexts;
273 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500274
Thomas Daede51020e12017-12-14 20:12:44 -0800275 cm->primary_ref_frame = PRIMARY_REF_NONE;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700276 if (frame_is_intra_only(cm) || cm->error_resilient_mode) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700277 av1_setup_past_independence(cm);
Thomas Daede51020e12017-12-14 20:12:44 -0800278 for (int i = 0; i < REF_FRAMES; i++) {
279 cm->fb_of_context_type[i] = -1;
280 }
281 cm->fb_of_context_type[REGULAR_FRAME] =
282 get_ref_frame_map_idx(cpi, GOLDEN_FRAME);
Thomas Daede3f2853a2018-03-12 17:40:32 -0700283 } else if (!cm->large_scale_tile) {
284 // for large scale tile we leave the default PRIMARY_REF_NONE
Yaowu Xuc27fc142016-08-22 16:08:15 -0700285 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -0700286 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700287 cm->frame_context_idx = EXT_ARF_FRAME;
288 else if (cpi->refresh_alt_ref_frame)
289 cm->frame_context_idx = ARF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700290 else if (cpi->rc.is_src_frame_alt_ref)
291 cm->frame_context_idx = OVERLAY_FRAME;
292 else if (cpi->refresh_golden_frame)
293 cm->frame_context_idx = GLD_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700294 else if (cpi->refresh_bwd_ref_frame)
295 cm->frame_context_idx = BRF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700296 else
297 cm->frame_context_idx = REGULAR_FRAME;
Thomas Daede51020e12017-12-14 20:12:44 -0800298 int wanted_fb = cm->fb_of_context_type[cm->frame_context_idx];
299 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
300 int fb = get_ref_frame_map_idx(cpi, ref_frame);
301 if (fb == wanted_fb) {
302 cm->primary_ref_frame = ref_frame - LAST_FRAME;
303 }
304 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700305 }
306
307 if (cm->frame_type == KEY_FRAME) {
308 cpi->refresh_golden_frame = 1;
309 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700310 av1_zero(cpi->interp_filter_selected);
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000311 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700312 set_use_reference_buffer(cm, 0);
David Barker11eac7b2017-12-07 12:22:36 +0000313 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
Tarek AMARAc9813852018-03-05 18:40:18 -0500314 } else if (frame_is_sframe(cm)) {
315 cpi->refresh_golden_frame = 1;
316 cpi->refresh_alt_ref_frame = 1;
317 av1_zero(cpi->interp_filter_selected);
318 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319 } else {
Thomas Daede51020e12017-12-14 20:12:44 -0800320 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
321 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
David Barkercc615a82018-03-19 14:38:51 +0000322 av1_setup_past_independence(cm);
323 cm->seg.update_map = 1;
324 cm->seg.update_data = 1;
David Barker11eac7b2017-12-07 12:22:36 +0000325 cm->pre_fc = &cm->frame_contexts[FRAME_CONTEXT_DEFAULTS];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700326 } else {
Thomas Daede51020e12017-12-14 20:12:44 -0800327 *cm->fc = cm->frame_contexts[cm->frame_refs[cm->primary_ref_frame].idx];
328 cm->pre_fc =
329 &cm->frame_contexts[cm->frame_refs[cm->primary_ref_frame].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700330 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700331 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700332 }
333
Yue Chend90d3432018-03-16 11:28:42 -0700334 cm->prev_frame = get_prev_frame(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700336}
337
Cheng Chen46f30c72017-09-07 11:13:33 -0700338static void enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700339 int i;
Yunqing Wang19b9f722018-02-20 16:22:01 -0800340 cm->mi = cm->mip;
341 memset(cm->mip, 0, cm->mi_stride * cm->mi_rows * sizeof(*cm->mip));
342 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343 // Clear top border row
344 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
345 // Clear left border column
Yunqing Wang19b9f722018-02-20 16:22:01 -0800346 for (i = 0; i < cm->mi_rows; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700347 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yunqing Wang19b9f722018-02-20 16:22:01 -0800348 cm->mi_grid_visible = cm->mi_grid_base;
349 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700350
351 memset(cm->mi_grid_base, 0,
Yunqing Wang19b9f722018-02-20 16:22:01 -0800352 cm->mi_stride * cm->mi_rows * sizeof(*cm->mi_grid_base));
Frank Bossen4ef06b52018-01-18 19:51:15 -0500353
354 memset(cm->boundary_info, 0,
355 cm->boundary_info_alloc_size * sizeof(*cm->boundary_info));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700356}
357
Cheng Chen46f30c72017-09-07 11:13:33 -0700358static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700359 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700360 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700361 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700362 if (!cm->prev_mip) return 1;
363 cm->mi_alloc_size = mi_size;
364
Yue Chen53b53f02018-03-29 14:31:23 -0700365 cm->mi_grid_base =
366 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700367 if (!cm->mi_grid_base) return 1;
368 cm->prev_mi_grid_base =
Yue Chen53b53f02018-03-29 14:31:23 -0700369 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700370 if (!cm->prev_mi_grid_base) return 1;
371
372 return 0;
373}
374
Cheng Chen46f30c72017-09-07 11:13:33 -0700375static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700376 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700378 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700379 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700380 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700382 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700383 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700384 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700385}
386
Cheng Chen46f30c72017-09-07 11:13:33 -0700387static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700388 // Current mip will be the prev_mip for the next frame.
Yue Chen53b53f02018-03-29 14:31:23 -0700389 MB_MODE_INFO **temp_base = cm->prev_mi_grid_base;
390 MB_MODE_INFO *temp = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700391 cm->prev_mip = cm->mip;
392 cm->mip = temp;
393
394 // Update the upper left visible macroblock ptrs.
Yunqing Wang19b9f722018-02-20 16:22:01 -0800395 cm->mi = cm->mip;
396 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700397
398 cm->prev_mi_grid_base = cm->mi_grid_base;
399 cm->mi_grid_base = temp_base;
Yunqing Wang19b9f722018-02-20 16:22:01 -0800400 cm->mi_grid_visible = cm->mi_grid_base;
401 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402}
403
Yaowu Xuf883b422016-08-30 14:01:10 -0700404void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700405 static volatile int init_done = 0;
406
407 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700408 av1_rtcd();
409 aom_dsp_rtcd();
410 aom_scale_rtcd();
411 av1_init_intra_predictors();
412 av1_init_me_luts();
413 av1_rc_init_minq_luts();
Yaowu Xuf883b422016-08-30 14:01:10 -0700414 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700415 init_done = 1;
416 }
417}
418
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700419static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
420 if (cpi->mbmi_ext_base) {
421 aom_free(cpi->mbmi_ext_base);
422 cpi->mbmi_ext_base = NULL;
423 }
424}
425
426static void alloc_context_buffers_ext(AV1_COMP *cpi) {
427 AV1_COMMON *cm = &cpi->common;
428 int mi_size = cm->mi_cols * cm->mi_rows;
429
430 dealloc_context_buffers_ext(cpi);
431 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
432 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
433}
434
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800435static void update_film_grain_parameters(struct AV1_COMP *cpi,
436 const AV1EncoderConfig *oxcf) {
437 AV1_COMMON *const cm = &cpi->common;
438 cpi->oxcf = *oxcf;
439
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700440 if (cm->film_grain_table) {
441 aom_film_grain_table_free(cm->film_grain_table);
442 aom_free(cm->film_grain_table);
443 }
444 cm->film_grain_table = 0;
445
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800446 if (oxcf->film_grain_test_vector) {
447 cm->film_grain_params_present = 1;
448 if (cm->frame_type == KEY_FRAME) {
449 memcpy(&cm->film_grain_params,
450 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
451 sizeof(cm->film_grain_params));
452
453 cm->film_grain_params.bit_depth = cm->bit_depth;
454 if (cm->color_range == AOM_CR_FULL_RANGE) {
455 cm->film_grain_params.clip_to_restricted_range = 0;
456 }
457 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700458 } else if (oxcf->film_grain_table_filename) {
459 cm->film_grain_table = aom_malloc(sizeof(*cm->film_grain_table));
460 memset(cm->film_grain_table, 0, sizeof(aom_film_grain_table_t));
461
462 aom_film_grain_table_read(cm->film_grain_table,
463 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800464 } else {
465 cm->film_grain_params_present = 0;
466 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
467 }
468}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800469
Yaowu Xuf883b422016-08-30 14:01:10 -0700470static void dealloc_compressor_data(AV1_COMP *cpi) {
471 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000472 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700473
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700474 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700475
Yaowu Xuf883b422016-08-30 14:01:10 -0700476 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700477 cpi->tile_data = NULL;
478
479 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700480 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700481 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700482
Yaowu Xuf883b422016-08-30 14:01:10 -0700483 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484 cpi->cyclic_refresh = NULL;
485
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487 cpi->active_map.map = NULL;
488
Jingning Hand064cf02017-06-01 10:00:39 -0700489 aom_free(cpi->td.mb.above_pred_buf);
490 cpi->td.mb.above_pred_buf = NULL;
491
492 aom_free(cpi->td.mb.left_pred_buf);
493 cpi->td.mb.left_pred_buf = NULL;
494
495 aom_free(cpi->td.mb.wsrc_buf);
496 cpi->td.mb.wsrc_buf = NULL;
497
498 aom_free(cpi->td.mb.mask_buf);
499 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700500
Jingning Han6cc1fd32017-10-13 09:05:36 -0700501 aom_free(cm->tpl_mvs);
502 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700503
Yaowu Xuf883b422016-08-30 14:01:10 -0700504 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700505 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700506 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700507
Yaowu Xuf883b422016-08-30 14:01:10 -0700508 aom_free_frame_buffer(&cpi->last_frame_uf);
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->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700511 aom_free_frame_buffer(&cpi->scaled_source);
512 aom_free_frame_buffer(&cpi->scaled_last_source);
513 aom_free_frame_buffer(&cpi->alt_ref_buffer);
514 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700515
Yaowu Xuf883b422016-08-30 14:01:10 -0700516 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700517 cpi->tile_tok[0][0] = 0;
518
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000519 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520
hui sud9a812b2017-07-06 14:34:37 -0700521 aom_free(cpi->td.mb.palette_buffer);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522}
523
Yaowu Xuf883b422016-08-30 14:01:10 -0700524static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700525 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700526 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700527
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200528 // Stores a snapshot of key state variables which can subsequently be
529 // restored with a call to av1_restore_coding_context. These functions are
530 // intended for use in a re-code loop in av1_compress_frame where the
531 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800532 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
533 av1_copy(cc->nmv_costs, cpi->nmv_costs);
534 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000535
Yaowu Xuc27fc142016-08-22 16:08:15 -0700536 cc->fc = *cm->fc;
537}
538
Yaowu Xuf883b422016-08-30 14:01:10 -0700539static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700541 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700542
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200543 // Restore key state variables to the snapshot state stored in the
544 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800545 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
546 av1_copy(cpi->nmv_costs, cc->nmv_costs);
547 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000548
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549 *cm->fc = cc->fc;
550}
551
Yaowu Xuf883b422016-08-30 14:01:10 -0700552static void configure_static_seg_features(AV1_COMP *cpi) {
553 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700554 const RATE_CONTROL *const rc = &cpi->rc;
555 struct segmentation *const seg = &cm->seg;
556
557 int high_q = (int)(rc->avg_q > 48.0);
558 int qi_delta;
559
560 // Disable and clear down for KF
561 if (cm->frame_type == KEY_FRAME) {
562 // Clear down the global segmentation map
563 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
564 seg->update_map = 0;
565 seg->update_data = 0;
566 cpi->static_mb_pct = 0;
567
568 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700569 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700570
571 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700572 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700573 } else if (cpi->refresh_alt_ref_frame) {
574 // If this is an alt ref frame
575 // Clear down the global segmentation map
576 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
577 seg->update_map = 0;
578 seg->update_data = 0;
579 cpi->static_mb_pct = 0;
580
581 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700582 av1_disable_segmentation(seg);
583 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700584
585 // Scan frames from current to arf frame.
586 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700587 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700588
589 // If segmentation was enabled set those features needed for the
590 // arf itself.
591 if (seg->enabled) {
592 seg->update_map = 1;
593 seg->update_data = 1;
594
595 qi_delta =
Yaowu Xuf883b422016-08-30 14:01:10 -0700596 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875, cm->bit_depth);
597 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700598 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
599 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
600 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
601 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
602
603 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
604 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
605 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
606 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607
Yaowu Xuf883b422016-08-30 14:01:10 -0700608 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609 }
610 } else if (seg->enabled) {
611 // All other frames if segmentation has been enabled
612
613 // First normal frame in a valid gf or alt ref group
614 if (rc->frames_since_golden == 0) {
615 // Set up segment features for normal frames in an arf group
616 if (rc->source_alt_ref_active) {
617 seg->update_map = 0;
618 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619
Yaowu Xuf883b422016-08-30 14:01:10 -0700620 qi_delta =
621 av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125, cm->bit_depth);
622 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
623 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700624
Cheng Chend8184da2017-09-26 18:15:22 -0700625 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
626 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
627 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
628 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
629
630 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
631 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
632 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
633 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700634
635 // Segment coding disabled for compred testing
636 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700637 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
638 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
639 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700640 }
641 } else {
642 // Disable segmentation and clear down features if alt ref
643 // is not active for this group
644
Yaowu Xuf883b422016-08-30 14:01:10 -0700645 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700646
647 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
648
649 seg->update_map = 0;
650 seg->update_data = 0;
651
Yaowu Xuf883b422016-08-30 14:01:10 -0700652 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700653 }
654 } else if (rc->is_src_frame_alt_ref) {
655 // Special case where we are coding over the top of a previous
656 // alt ref frame.
657 // Segment coding disabled for compred testing
658
659 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700660 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
661 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700662
663 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700664 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
665 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
666 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
667 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668
669 // Skip all MBs if high Q (0,0 mv and skip coeffs)
670 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700671 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
672 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700673 }
674 // Enable data update
675 seg->update_data = 1;
676 } else {
677 // All other frames.
678
679 // No updates.. leave things as they are.
680 seg->update_map = 0;
681 seg->update_data = 0;
682 }
683 }
684}
685
Yaowu Xuf883b422016-08-30 14:01:10 -0700686static void update_reference_segmentation_map(AV1_COMP *cpi) {
687 AV1_COMMON *const cm = &cpi->common;
Yue Chen53b53f02018-03-29 14:31:23 -0700688 MB_MODE_INFO **mi_8x8_ptr = cm->mi_grid_visible;
Soo-Chul Han934af352017-10-15 15:21:51 -0400689 uint8_t *cache_ptr = cm->current_frame_seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690 int row, col;
691
692 for (row = 0; row < cm->mi_rows; row++) {
Yue Chen53b53f02018-03-29 14:31:23 -0700693 MB_MODE_INFO **mi_8x8 = mi_8x8_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694 uint8_t *cache = cache_ptr;
695 for (col = 0; col < cm->mi_cols; col++, mi_8x8++, cache++)
Yue Chen53b53f02018-03-29 14:31:23 -0700696 cache[0] = mi_8x8[0]->segment_id;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700697 mi_8x8_ptr += cm->mi_stride;
698 cache_ptr += cm->mi_cols;
699 }
700}
701
Yaowu Xuf883b422016-08-30 14:01:10 -0700702static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
703 AV1_COMMON *cm = &cpi->common;
704 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700705
706 if (!cpi->lookahead)
Yaowu Xud3e7c682017-12-21 14:08:25 -0800707 cpi->lookahead = av1_lookahead_init(
708 oxcf->width, oxcf->height, cm->subsampling_x, cm->subsampling_y,
709 cm->use_highbitdepth, oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700710 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700711 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700712 "Failed to allocate lag buffers");
713
714 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Yaowu Xuf883b422016-08-30 14:01:10 -0700715 if (aom_realloc_frame_buffer(&cpi->alt_ref_buffer, oxcf->width, oxcf->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700716 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -0800717 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
718 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700719 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700720 "Failed to allocate altref buffer");
721}
722
Yaowu Xuf883b422016-08-30 14:01:10 -0700723static void alloc_util_frame_buffers(AV1_COMP *cpi) {
724 AV1_COMMON *const cm = &cpi->common;
725 if (aom_realloc_frame_buffer(&cpi->last_frame_uf, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700726 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -0800727 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
728 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730 "Failed to allocate last frame buffer");
731
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700732 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800733 &cpi->trial_frame_rst, cm->superres_upscaled_width,
734 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
735 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL,
736 NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800737 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800738 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739
Yaowu Xuf883b422016-08-30 14:01:10 -0700740 if (aom_realloc_frame_buffer(&cpi->scaled_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700741 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -0800742 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
743 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700744 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700745 "Failed to allocate scaled source buffer");
746
Yaowu Xuf883b422016-08-30 14:01:10 -0700747 if (aom_realloc_frame_buffer(&cpi->scaled_last_source, cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700748 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -0800749 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
750 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 "Failed to allocate scaled last source buffer");
753}
754
Cheng Chen46f30c72017-09-07 11:13:33 -0700755static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700756 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000757 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700758
Yaowu Xuf883b422016-08-30 14:01:10 -0700759 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700761 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700762
Yaowu Xuc27fc142016-08-22 16:08:15 -0700763 alloc_context_buffers_ext(cpi);
764
Yaowu Xuf883b422016-08-30 14:01:10 -0700765 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700766
767 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000768 unsigned int tokens =
769 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700770 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700771 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700772 }
773
Yaowu Xuf883b422016-08-30 14:01:10 -0700774 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700775}
776
Yaowu Xuf883b422016-08-30 14:01:10 -0700777void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700779 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780}
781
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200782static void set_tile_info_max_tile(AV1_COMP *cpi) {
783 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200784 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200785
786 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200787
788 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200789 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200790 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200791 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
792 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200793 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000794 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
795 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200796 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200797 cm->uniform_tile_spacing_flag = 0;
798 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
799 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200800 size_sb = cpi->oxcf.tile_widths[j++];
801 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000802 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200803 }
804 cm->tile_cols = i;
805 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200806 }
807 av1_calculate_tile_cols(cm);
808
809 // configure tile rows
810 if (cm->uniform_tile_spacing_flag) {
811 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
812 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200813 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000814 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
815 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200816 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200817 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
818 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200819 size_sb = cpi->oxcf.tile_heights[j++];
820 if (j >= cpi->oxcf.tile_height_count) j = 0;
821 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200822 }
823 cm->tile_rows = i;
824 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200825 }
826 av1_calculate_tile_rows(cm);
827}
828
Yaowu Xuf883b422016-08-30 14:01:10 -0700829static void set_tile_info(AV1_COMP *cpi) {
830 AV1_COMMON *const cm = &cpi->common;
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700831 if (cpi->oxcf.large_scale_tile) {
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700832 if (cpi->oxcf.superblock_size != AOM_SUPERBLOCK_SIZE_64X64) {
833 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 32);
834 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 32);
835 cm->tile_width <<= MAX_MIB_SIZE_LOG2;
836 cm->tile_height <<= MAX_MIB_SIZE_LOG2;
837 } else {
838 cm->tile_width = clamp(cpi->oxcf.tile_columns, 1, 64);
839 cm->tile_height = clamp(cpi->oxcf.tile_rows, 1, 64);
840 cm->tile_width <<= MAX_MIB_SIZE_LOG2 - 1;
841 cm->tile_height <<= MAX_MIB_SIZE_LOG2 - 1;
842 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700843
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700844 cm->tile_width = AOMMIN(cm->tile_width, cm->mi_cols);
845 cm->tile_height = AOMMIN(cm->tile_height, cm->mi_rows);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846
Yaowu Xu8d0039c2017-10-13 07:51:46 -0700847 assert(cm->tile_width >> MAX_MIB_SIZE_LOG2 <= 32);
848 assert(cm->tile_height >> MAX_MIB_SIZE_LOG2 <= 32);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700850 // Get the number of tiles
851 cm->tile_cols = 1;
852 while (cm->tile_cols * cm->tile_width < cm->mi_cols) ++cm->tile_cols;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700854 cm->tile_rows = 1;
855 while (cm->tile_rows * cm->tile_height < cm->mi_rows) ++cm->tile_rows;
Dominic Symes917d6c02017-10-11 18:00:52 +0200856 int i;
857 for (i = 0; i <= cm->tile_cols; i++) {
858 cm->tile_col_start_sb[i] =
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000859 ((i * cm->tile_width - 1) >> cm->seq_params.mib_size_log2) + 1;
Dominic Symes917d6c02017-10-11 18:00:52 +0200860 }
861 for (i = 0; i <= cm->tile_rows; i++) {
862 cm->tile_row_start_sb[i] =
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000863 ((i * cm->tile_height - 1) >> cm->seq_params.mib_size_log2) + 1;
Dominic Symes917d6c02017-10-11 18:00:52 +0200864 }
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700865 } else {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200866 set_tile_info_max_tile(cpi);
Yunqing Wangeeb08a92017-07-07 21:25:18 -0700867 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868}
869
Yaowu Xuf883b422016-08-30 14:01:10 -0700870static void update_frame_size(AV1_COMP *cpi) {
871 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
873
Yaowu Xuf883b422016-08-30 14:01:10 -0700874 av1_set_mb_mi(cm, cm->width, cm->height);
875 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500876 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700877 memset(cpi->mbmi_ext_base, 0,
878 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700879 set_tile_info(cpi);
880}
881
Yaowu Xuf883b422016-08-30 14:01:10 -0700882static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700884 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
885 cpi->ref_fb_idx[fb_idx] = fb_idx;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700886 for (fb_idx = 0; fb_idx < MAX_EXT_ARFS + 1; ++fb_idx)
887 cpi->arf_map[fb_idx] = LAST_REF_FRAMES + 2 + fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700888 cpi->rate_index = 0;
889 cpi->rate_size = 0;
890 cpi->cur_poc = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700891}
892
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700893void init_seq_coding_tools(SequenceHeader *seq, const AV1EncoderConfig *oxcf) {
Debargha Mukherjee5d6e3fb2018-04-03 13:05:54 -0700894 seq->still_picture = 0;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700895 seq->force_screen_content_tools = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700896 seq->force_integer_mv = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700897 seq->order_hint_bits_minus1 = DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700898 seq->enable_dual_filter = oxcf->enable_dual_filter;
899 seq->enable_order_hint = oxcf->enable_order_hint;
900 seq->enable_jnt_comp = oxcf->enable_jnt_comp;
901 seq->enable_jnt_comp &= seq->enable_order_hint;
902 seq->enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
903 seq->enable_ref_frame_mvs &= seq->enable_order_hint;
904 seq->enable_superres = oxcf->enable_superres;
905 seq->enable_cdef = oxcf->enable_cdef;
906 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -0700907 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee97095fb2018-03-26 07:51:48 -0700908 seq->enable_interintra_compound = 1;
909 seq->enable_masked_compound = 1;
Debargha Mukherjee365eae82018-03-26 19:19:55 -0700910 seq->enable_intra_edge_filter = 1;
911 seq->enable_filter_intra = 1;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700912}
913
Yaowu Xuf883b422016-08-30 14:01:10 -0700914static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
915 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916
917 cpi->oxcf = *oxcf;
918 cpi->framerate = oxcf->init_framerate;
919
920 cm->profile = oxcf->profile;
921 cm->bit_depth = oxcf->bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 cm->use_highbitdepth = oxcf->use_highbitdepth;
Andrey Norkin9e694632017-12-21 18:50:57 -0800923 cm->color_primaries = oxcf->color_primaries;
924 cm->transfer_characteristics = oxcf->transfer_characteristics;
925 cm->matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -0800926 cm->seq_params.monochrome = oxcf->monochrome;
anorkin76fb1262017-03-22 15:12:12 -0700927 cm->chroma_sample_position = oxcf->chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700928 cm->color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -0800929 cm->timing_info_present = oxcf->timing_info_present;
930 cm->num_units_in_tick = oxcf->num_units_in_tick;
931 cm->time_scale = oxcf->time_scale;
932 cm->equal_picture_interval = oxcf->equal_picture_interval;
933 cm->num_ticks_per_picture = oxcf->num_ticks_per_picture;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700934 cm->width = oxcf->width;
935 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000936 set_sb_size(&cm->seq_params,
937 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -0700938 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -0700939
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800940 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700941
942 // Single thread case: use counts in common.
943 cpi->td.counts = &cm->counts;
944
945 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -0700946 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700947
948 cpi->static_mb_pct = 0;
949 cpi->ref_frame_flags = 0;
950
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700951 // Reset resize pending flags
952 cpi->resize_pending_width = 0;
953 cpi->resize_pending_height = 0;
954
Yaowu Xuc27fc142016-08-22 16:08:15 -0700955 init_buffer_indices(cpi);
956}
957
958static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -0700959 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700960 const int64_t bandwidth = oxcf->target_bandwidth;
961 const int64_t starting = oxcf->starting_buffer_level_ms;
962 const int64_t optimal = oxcf->optimal_buffer_level_ms;
963 const int64_t maximum = oxcf->maximum_buffer_size_ms;
964
965 rc->starting_buffer_level = starting * bandwidth / 1000;
966 rc->optimal_buffer_level =
967 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
968 rc->maximum_buffer_size =
969 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
970}
971
Cheng Chenbf3d4962017-11-01 14:48:52 -0700972#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF, JSDAF, \
973 JSVAF) \
974 cpi->fn_ptr[BT].sdf = SDF; \
975 cpi->fn_ptr[BT].sdaf = SDAF; \
976 cpi->fn_ptr[BT].vf = VF; \
977 cpi->fn_ptr[BT].svf = SVF; \
978 cpi->fn_ptr[BT].svaf = SVAF; \
979 cpi->fn_ptr[BT].sdx3f = SDX3F; \
980 cpi->fn_ptr[BT].sdx8f = SDX8F; \
981 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
982 cpi->fn_ptr[BT].jsdaf = JSDAF; \
983 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700984
985#define MAKE_BFP_SAD_WRAPPER(fnname) \
986 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
987 int source_stride, \
988 const uint8_t *ref_ptr, int ref_stride) { \
989 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
990 } \
991 static unsigned int fnname##_bits10( \
992 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
993 int ref_stride) { \
994 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
995 } \
996 static unsigned int fnname##_bits12( \
997 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
998 int ref_stride) { \
999 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1000 }
1001
1002#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1003 static unsigned int fnname##_bits8( \
1004 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1005 int ref_stride, const uint8_t *second_pred) { \
1006 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1007 } \
1008 static unsigned int fnname##_bits10( \
1009 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1010 int ref_stride, const uint8_t *second_pred) { \
1011 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1012 2; \
1013 } \
1014 static unsigned int fnname##_bits12( \
1015 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1016 int ref_stride, const uint8_t *second_pred) { \
1017 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1018 4; \
1019 }
1020
1021#define MAKE_BFP_SAD3_WRAPPER(fnname) \
1022 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1023 const uint8_t *ref_ptr, int ref_stride, \
1024 unsigned int *sad_array) { \
1025 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1026 } \
1027 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1028 const uint8_t *ref_ptr, int ref_stride, \
1029 unsigned int *sad_array) { \
1030 int i; \
1031 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1032 for (i = 0; i < 3; i++) sad_array[i] >>= 2; \
1033 } \
1034 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1035 const uint8_t *ref_ptr, int ref_stride, \
1036 unsigned int *sad_array) { \
1037 int i; \
1038 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1039 for (i = 0; i < 3; i++) sad_array[i] >>= 4; \
1040 }
1041
1042#define MAKE_BFP_SAD8_WRAPPER(fnname) \
1043 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1044 const uint8_t *ref_ptr, int ref_stride, \
1045 unsigned int *sad_array) { \
1046 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1047 } \
1048 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1049 const uint8_t *ref_ptr, int ref_stride, \
1050 unsigned int *sad_array) { \
1051 int i; \
1052 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1053 for (i = 0; i < 8; i++) sad_array[i] >>= 2; \
1054 } \
1055 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1056 const uint8_t *ref_ptr, int ref_stride, \
1057 unsigned int *sad_array) { \
1058 int i; \
1059 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1060 for (i = 0; i < 8; i++) sad_array[i] >>= 4; \
1061 }
1062#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1063 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1064 const uint8_t *const ref_ptr[], int ref_stride, \
1065 unsigned int *sad_array) { \
1066 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1067 } \
1068 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1069 const uint8_t *const ref_ptr[], int ref_stride, \
1070 unsigned int *sad_array) { \
1071 int i; \
1072 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1073 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1074 } \
1075 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1076 const uint8_t *const ref_ptr[], int ref_stride, \
1077 unsigned int *sad_array) { \
1078 int i; \
1079 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1080 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1081 }
1082
Cheng Chenbf3d4962017-11-01 14:48:52 -07001083#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1084 static unsigned int fnname##_bits8( \
1085 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1086 int ref_stride, const uint8_t *second_pred, \
1087 const JNT_COMP_PARAMS *jcp_param) { \
1088 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1089 jcp_param); \
1090 } \
1091 static unsigned int fnname##_bits10( \
1092 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1093 int ref_stride, const uint8_t *second_pred, \
1094 const JNT_COMP_PARAMS *jcp_param) { \
1095 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1096 jcp_param) >> \
1097 2; \
1098 } \
1099 static unsigned int fnname##_bits12( \
1100 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1101 int ref_stride, const uint8_t *second_pred, \
1102 const JNT_COMP_PARAMS *jcp_param) { \
1103 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1104 jcp_param) >> \
1105 4; \
1106 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001107
Yaowu Xuf883b422016-08-30 14:01:10 -07001108MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1109MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
1110MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad128x128x3)
1111MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad128x128x8)
1112MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1113MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1114MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1115MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1116MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1117MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1118MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001119MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1120MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1121MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1122MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1123MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1124MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1125MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1126MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1127MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1128MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1129MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1130MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1131MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1132MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
1133MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad32x32x3)
1134MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad32x32x8)
1135MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1136MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1137MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
1138MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad64x64x3)
1139MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad64x64x8)
1140MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1141MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1142MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
1143MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x16x3)
1144MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x16x8)
1145MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1146MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1147MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
1148MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad16x8x3)
1149MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad16x8x8)
1150MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1151MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1152MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
1153MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x16x3)
1154MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x16x8)
1155MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1156MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1157MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
1158MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad8x8x3)
1159MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x8x8)
1160MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1161MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1162MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
1163MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad8x4x8)
1164MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1165MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1166MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
1167MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x8x8)
1168MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1169MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1170MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
1171MAKE_BFP_SAD3_WRAPPER(aom_highbd_sad4x4x3)
1172MAKE_BFP_SAD8_WRAPPER(aom_highbd_sad4x4x8)
1173MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001174
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001175MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1176MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1177MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1178MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1179MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1180MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1181MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1182MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1183MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1184MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1185MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1186MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001187MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1188MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1189MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1190MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1191MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1192MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001193
Cheng Chenbf3d4962017-11-01 14:48:52 -07001194MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x128_avg)
1195MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x64_avg)
1196MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x128_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001197MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x16_avg)
1198MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x32_avg)
1199MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x32_avg)
1200MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x64_avg)
1201MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x32_avg)
1202MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x64_avg)
1203MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x16_avg)
1204MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x8_avg)
1205MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x16_avg)
1206MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x8_avg)
1207MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x4_avg)
1208MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x8_avg)
1209MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x4_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001210MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x16_avg)
1211MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x4_avg)
1212MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x32_avg)
1213MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x8_avg)
1214MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x64_avg)
1215MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001216
David Barker0f3c94e2017-05-16 15:21:50 +01001217#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001218 cpi->fn_ptr[BT].msdf = MCSDF; \
1219 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001220
David Barkerc155e012017-05-11 13:54:54 +01001221#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1222 static unsigned int fnname##_bits8( \
1223 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1224 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1225 int m_stride, int invert_mask) { \
1226 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1227 second_pred_ptr, m, m_stride, invert_mask); \
1228 } \
1229 static unsigned int fnname##_bits10( \
1230 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1231 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1232 int m_stride, int invert_mask) { \
1233 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1234 second_pred_ptr, m, m_stride, invert_mask) >> \
1235 2; \
1236 } \
1237 static unsigned int fnname##_bits12( \
1238 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1239 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1240 int m_stride, int invert_mask) { \
1241 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1242 second_pred_ptr, m, m_stride, invert_mask) >> \
1243 4; \
1244 }
1245
David Barkerf19f35f2017-05-22 16:33:22 +01001246MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1247MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1248MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001249MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1250MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1251MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1252MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1253MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1254MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1255MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1256MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1257MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1258MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1259MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1260MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1261MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001262MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1263MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1264MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1265MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001266MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1267MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001268
Yaowu Xuc27fc142016-08-22 16:08:15 -07001269#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1270 cpi->fn_ptr[BT].osdf = OSDF; \
1271 cpi->fn_ptr[BT].ovf = OVF; \
1272 cpi->fn_ptr[BT].osvf = OSVF;
1273
1274#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1275 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1276 const int32_t *wsrc, \
1277 const int32_t *msk) { \
1278 return fnname(ref, ref_stride, wsrc, msk); \
1279 } \
1280 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1281 const int32_t *wsrc, \
1282 const int32_t *msk) { \
1283 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1284 } \
1285 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1286 const int32_t *wsrc, \
1287 const int32_t *msk) { \
1288 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1289 }
1290
Yaowu Xuf883b422016-08-30 14:01:10 -07001291MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1292MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1293MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001294MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1295MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1296MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1297MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1298MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1299MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1300MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1301MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1302MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1303MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1304MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1305MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1306MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001307MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1308MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1309MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1310MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001311MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1312MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001313
Yaowu Xuf883b422016-08-30 14:01:10 -07001314static void highbd_set_var_fns(AV1_COMP *const cpi) {
1315 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001316 if (cm->use_highbitdepth) {
1317 switch (cm->bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001318 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001319 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1320 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1321 aom_highbd_8_sub_pixel_variance64x16,
1322 aom_highbd_8_sub_pixel_avg_variance64x16, NULL, NULL,
1323 aom_highbd_sad64x16x4d_bits8,
1324 aom_highbd_jnt_sad64x16_avg_bits8,
1325 aom_highbd_8_jnt_sub_pixel_avg_variance64x16)
1326
1327 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1328 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1329 aom_highbd_8_sub_pixel_variance16x64,
1330 aom_highbd_8_sub_pixel_avg_variance16x64, NULL, NULL,
1331 aom_highbd_sad16x64x4d_bits8,
1332 aom_highbd_jnt_sad16x64_avg_bits8,
1333 aom_highbd_8_jnt_sub_pixel_avg_variance16x64)
1334
1335 HIGHBD_BFP(
1336 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1337 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
1338 aom_highbd_8_sub_pixel_avg_variance32x8, NULL, NULL,
1339 aom_highbd_sad32x8x4d_bits8, aom_highbd_jnt_sad32x8_avg_bits8,
1340 aom_highbd_8_jnt_sub_pixel_avg_variance32x8)
1341
1342 HIGHBD_BFP(
1343 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1344 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
1345 aom_highbd_8_sub_pixel_avg_variance8x32, NULL, NULL,
1346 aom_highbd_sad8x32x4d_bits8, aom_highbd_jnt_sad8x32_avg_bits8,
1347 aom_highbd_8_jnt_sub_pixel_avg_variance8x32)
1348
1349 HIGHBD_BFP(
1350 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1351 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
1352 aom_highbd_8_sub_pixel_avg_variance16x4, NULL, NULL,
1353 aom_highbd_sad16x4x4d_bits8, aom_highbd_jnt_sad16x4_avg_bits8,
1354 aom_highbd_8_jnt_sub_pixel_avg_variance16x4)
1355
1356 HIGHBD_BFP(
1357 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1358 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
1359 aom_highbd_8_sub_pixel_avg_variance4x16, NULL, NULL,
1360 aom_highbd_sad4x16x4d_bits8, aom_highbd_jnt_sad4x16_avg_bits8,
1361 aom_highbd_8_jnt_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001362
1363 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1364 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1365 aom_highbd_8_sub_pixel_variance32x16,
1366 aom_highbd_8_sub_pixel_avg_variance32x16, NULL, NULL,
1367 aom_highbd_sad32x16x4d_bits8,
1368 aom_highbd_jnt_sad32x16_avg_bits8,
1369 aom_highbd_8_jnt_sub_pixel_avg_variance32x16)
1370
1371 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1372 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1373 aom_highbd_8_sub_pixel_variance16x32,
1374 aom_highbd_8_sub_pixel_avg_variance16x32, NULL, NULL,
1375 aom_highbd_sad16x32x4d_bits8,
1376 aom_highbd_jnt_sad16x32_avg_bits8,
1377 aom_highbd_8_jnt_sub_pixel_avg_variance16x32)
1378
1379 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1380 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1381 aom_highbd_8_sub_pixel_variance64x32,
1382 aom_highbd_8_sub_pixel_avg_variance64x32, NULL, NULL,
1383 aom_highbd_sad64x32x4d_bits8,
1384 aom_highbd_jnt_sad64x32_avg_bits8,
1385 aom_highbd_8_jnt_sub_pixel_avg_variance64x32)
1386
1387 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1388 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1389 aom_highbd_8_sub_pixel_variance32x64,
1390 aom_highbd_8_sub_pixel_avg_variance32x64, NULL, NULL,
1391 aom_highbd_sad32x64x4d_bits8,
1392 aom_highbd_jnt_sad32x64_avg_bits8,
1393 aom_highbd_8_jnt_sub_pixel_avg_variance32x64)
1394
1395 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1396 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1397 aom_highbd_8_sub_pixel_variance32x32,
1398 aom_highbd_8_sub_pixel_avg_variance32x32,
1399 aom_highbd_sad32x32x3_bits8, aom_highbd_sad32x32x8_bits8,
1400 aom_highbd_sad32x32x4d_bits8,
1401 aom_highbd_jnt_sad32x32_avg_bits8,
1402 aom_highbd_8_jnt_sub_pixel_avg_variance32x32)
1403
1404 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1405 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1406 aom_highbd_8_sub_pixel_variance64x64,
1407 aom_highbd_8_sub_pixel_avg_variance64x64,
1408 aom_highbd_sad64x64x3_bits8, aom_highbd_sad64x64x8_bits8,
1409 aom_highbd_sad64x64x4d_bits8,
1410 aom_highbd_jnt_sad64x64_avg_bits8,
1411 aom_highbd_8_jnt_sub_pixel_avg_variance64x64)
1412
1413 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1414 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1415 aom_highbd_8_sub_pixel_variance16x16,
1416 aom_highbd_8_sub_pixel_avg_variance16x16,
1417 aom_highbd_sad16x16x3_bits8, aom_highbd_sad16x16x8_bits8,
1418 aom_highbd_sad16x16x4d_bits8,
1419 aom_highbd_jnt_sad16x16_avg_bits8,
1420 aom_highbd_8_jnt_sub_pixel_avg_variance16x16)
1421
1422 HIGHBD_BFP(
1423 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1424 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
1425 aom_highbd_8_sub_pixel_avg_variance16x8, aom_highbd_sad16x8x3_bits8,
1426 aom_highbd_sad16x8x8_bits8, aom_highbd_sad16x8x4d_bits8,
1427 aom_highbd_jnt_sad16x8_avg_bits8,
1428 aom_highbd_8_jnt_sub_pixel_avg_variance16x8)
1429
1430 HIGHBD_BFP(
1431 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1432 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
1433 aom_highbd_8_sub_pixel_avg_variance8x16, aom_highbd_sad8x16x3_bits8,
1434 aom_highbd_sad8x16x8_bits8, aom_highbd_sad8x16x4d_bits8,
1435 aom_highbd_jnt_sad8x16_avg_bits8,
1436 aom_highbd_8_jnt_sub_pixel_avg_variance8x16)
1437
1438 HIGHBD_BFP(BLOCK_8X8, aom_highbd_sad8x8_bits8,
1439 aom_highbd_sad8x8_avg_bits8, aom_highbd_8_variance8x8,
1440 aom_highbd_8_sub_pixel_variance8x8,
1441 aom_highbd_8_sub_pixel_avg_variance8x8,
1442 aom_highbd_sad8x8x3_bits8, aom_highbd_sad8x8x8_bits8,
1443 aom_highbd_sad8x8x4d_bits8, aom_highbd_jnt_sad8x8_avg_bits8,
1444 aom_highbd_8_jnt_sub_pixel_avg_variance8x8)
1445
1446 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1447 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1448 aom_highbd_8_sub_pixel_variance8x4,
1449 aom_highbd_8_sub_pixel_avg_variance8x4, NULL,
1450 aom_highbd_sad8x4x8_bits8, aom_highbd_sad8x4x4d_bits8,
1451 aom_highbd_jnt_sad8x4_avg_bits8,
1452 aom_highbd_8_jnt_sub_pixel_avg_variance8x4)
1453
1454 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1455 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1456 aom_highbd_8_sub_pixel_variance4x8,
1457 aom_highbd_8_sub_pixel_avg_variance4x8, NULL,
1458 aom_highbd_sad4x8x8_bits8, aom_highbd_sad4x8x4d_bits8,
1459 aom_highbd_jnt_sad4x8_avg_bits8,
1460 aom_highbd_8_jnt_sub_pixel_avg_variance4x8)
1461
1462 HIGHBD_BFP(BLOCK_4X4, aom_highbd_sad4x4_bits8,
1463 aom_highbd_sad4x4_avg_bits8, aom_highbd_8_variance4x4,
1464 aom_highbd_8_sub_pixel_variance4x4,
1465 aom_highbd_8_sub_pixel_avg_variance4x4,
1466 aom_highbd_sad4x4x3_bits8, aom_highbd_sad4x4x8_bits8,
1467 aom_highbd_sad4x4x4d_bits8, aom_highbd_jnt_sad4x4_avg_bits8,
1468 aom_highbd_8_jnt_sub_pixel_avg_variance4x4)
1469
Cheng Chenbf3d4962017-11-01 14:48:52 -07001470 HIGHBD_BFP(
1471 BLOCK_128X128, aom_highbd_sad128x128_bits8,
1472 aom_highbd_sad128x128_avg_bits8, aom_highbd_8_variance128x128,
1473 aom_highbd_8_sub_pixel_variance128x128,
1474 aom_highbd_8_sub_pixel_avg_variance128x128,
1475 aom_highbd_sad128x128x3_bits8, aom_highbd_sad128x128x8_bits8,
1476 aom_highbd_sad128x128x4d_bits8, aom_highbd_jnt_sad128x128_avg_bits8,
1477 aom_highbd_8_jnt_sub_pixel_avg_variance128x128)
1478
1479 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1480 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1481 aom_highbd_8_sub_pixel_variance128x64,
1482 aom_highbd_8_sub_pixel_avg_variance128x64, NULL, NULL,
1483 aom_highbd_sad128x64x4d_bits8,
1484 aom_highbd_jnt_sad128x64_avg_bits8,
1485 aom_highbd_8_jnt_sub_pixel_avg_variance128x64)
1486
1487 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1488 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1489 aom_highbd_8_sub_pixel_variance64x128,
1490 aom_highbd_8_sub_pixel_avg_variance64x128, NULL, NULL,
1491 aom_highbd_sad64x128x4d_bits8,
1492 aom_highbd_jnt_sad64x128_avg_bits8,
1493 aom_highbd_8_jnt_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001494
David Barkerf19f35f2017-05-22 16:33:22 +01001495 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1496 aom_highbd_8_masked_sub_pixel_variance128x128)
1497 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1498 aom_highbd_8_masked_sub_pixel_variance128x64)
1499 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1500 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001501 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1502 aom_highbd_8_masked_sub_pixel_variance64x64)
1503 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1504 aom_highbd_8_masked_sub_pixel_variance64x32)
1505 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1506 aom_highbd_8_masked_sub_pixel_variance32x64)
1507 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1508 aom_highbd_8_masked_sub_pixel_variance32x32)
1509 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1510 aom_highbd_8_masked_sub_pixel_variance32x16)
1511 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1512 aom_highbd_8_masked_sub_pixel_variance16x32)
1513 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1514 aom_highbd_8_masked_sub_pixel_variance16x16)
1515 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1516 aom_highbd_8_masked_sub_pixel_variance8x16)
1517 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1518 aom_highbd_8_masked_sub_pixel_variance16x8)
1519 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1520 aom_highbd_8_masked_sub_pixel_variance8x8)
1521 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1522 aom_highbd_8_masked_sub_pixel_variance4x8)
1523 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1524 aom_highbd_8_masked_sub_pixel_variance8x4)
1525 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1526 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001527 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1528 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001529 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1530 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001531 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1532 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001533 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1534 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001535 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1536 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001537 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1538 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001539 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1540 aom_highbd_obmc_variance128x128,
1541 aom_highbd_obmc_sub_pixel_variance128x128)
1542 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1543 aom_highbd_obmc_variance128x64,
1544 aom_highbd_obmc_sub_pixel_variance128x64)
1545 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1546 aom_highbd_obmc_variance64x128,
1547 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001548 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1549 aom_highbd_obmc_variance64x64,
1550 aom_highbd_obmc_sub_pixel_variance64x64)
1551 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1552 aom_highbd_obmc_variance64x32,
1553 aom_highbd_obmc_sub_pixel_variance64x32)
1554 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1555 aom_highbd_obmc_variance32x64,
1556 aom_highbd_obmc_sub_pixel_variance32x64)
1557 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1558 aom_highbd_obmc_variance32x32,
1559 aom_highbd_obmc_sub_pixel_variance32x32)
1560 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1561 aom_highbd_obmc_variance32x16,
1562 aom_highbd_obmc_sub_pixel_variance32x16)
1563 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1564 aom_highbd_obmc_variance16x32,
1565 aom_highbd_obmc_sub_pixel_variance16x32)
1566 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1567 aom_highbd_obmc_variance16x16,
1568 aom_highbd_obmc_sub_pixel_variance16x16)
1569 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1570 aom_highbd_obmc_variance8x16,
1571 aom_highbd_obmc_sub_pixel_variance8x16)
1572 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1573 aom_highbd_obmc_variance16x8,
1574 aom_highbd_obmc_sub_pixel_variance16x8)
1575 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1576 aom_highbd_obmc_variance8x8,
1577 aom_highbd_obmc_sub_pixel_variance8x8)
1578 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1579 aom_highbd_obmc_variance4x8,
1580 aom_highbd_obmc_sub_pixel_variance4x8)
1581 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1582 aom_highbd_obmc_variance8x4,
1583 aom_highbd_obmc_sub_pixel_variance8x4)
1584 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1585 aom_highbd_obmc_variance4x4,
1586 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001587 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1588 aom_highbd_obmc_variance64x16,
1589 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001590 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1591 aom_highbd_obmc_variance16x64,
1592 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001593 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1594 aom_highbd_obmc_variance32x8,
1595 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001596 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1597 aom_highbd_obmc_variance8x32,
1598 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001599 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1600 aom_highbd_obmc_variance16x4,
1601 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001602 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1603 aom_highbd_obmc_variance4x16,
1604 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001605 break;
1606
Yaowu Xuf883b422016-08-30 14:01:10 -07001607 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001608 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1609 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1610 aom_highbd_10_sub_pixel_variance64x16,
1611 aom_highbd_10_sub_pixel_avg_variance64x16, NULL, NULL,
1612 aom_highbd_sad64x16x4d_bits10,
1613 aom_highbd_jnt_sad64x16_avg_bits10,
1614 aom_highbd_10_jnt_sub_pixel_avg_variance64x16);
1615
1616 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1617 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1618 aom_highbd_10_sub_pixel_variance16x64,
1619 aom_highbd_10_sub_pixel_avg_variance16x64, NULL, NULL,
1620 aom_highbd_sad16x64x4d_bits10,
1621 aom_highbd_jnt_sad16x64_avg_bits10,
1622 aom_highbd_10_jnt_sub_pixel_avg_variance16x64);
1623
1624 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1625 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1626 aom_highbd_10_sub_pixel_variance32x8,
1627 aom_highbd_10_sub_pixel_avg_variance32x8, NULL, NULL,
1628 aom_highbd_sad32x8x4d_bits10,
1629 aom_highbd_jnt_sad32x8_avg_bits10,
1630 aom_highbd_10_jnt_sub_pixel_avg_variance32x8);
1631
1632 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1633 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1634 aom_highbd_10_sub_pixel_variance8x32,
1635 aom_highbd_10_sub_pixel_avg_variance8x32, NULL, NULL,
1636 aom_highbd_sad8x32x4d_bits10,
1637 aom_highbd_jnt_sad8x32_avg_bits10,
1638 aom_highbd_10_jnt_sub_pixel_avg_variance8x32);
1639
1640 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1641 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1642 aom_highbd_10_sub_pixel_variance16x4,
1643 aom_highbd_10_sub_pixel_avg_variance16x4, NULL, NULL,
1644 aom_highbd_sad16x4x4d_bits10,
1645 aom_highbd_jnt_sad16x4_avg_bits10,
1646 aom_highbd_10_jnt_sub_pixel_avg_variance16x4);
1647
1648 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1649 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1650 aom_highbd_10_sub_pixel_variance4x16,
1651 aom_highbd_10_sub_pixel_avg_variance4x16, NULL, NULL,
1652 aom_highbd_sad4x16x4d_bits10,
1653 aom_highbd_jnt_sad4x16_avg_bits10,
1654 aom_highbd_10_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001655
1656 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1657 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1658 aom_highbd_10_sub_pixel_variance32x16,
1659 aom_highbd_10_sub_pixel_avg_variance32x16, NULL, NULL,
1660 aom_highbd_sad32x16x4d_bits10,
1661 aom_highbd_jnt_sad32x16_avg_bits10,
1662 aom_highbd_10_jnt_sub_pixel_avg_variance32x16);
1663
1664 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1665 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1666 aom_highbd_10_sub_pixel_variance16x32,
1667 aom_highbd_10_sub_pixel_avg_variance16x32, NULL, NULL,
1668 aom_highbd_sad16x32x4d_bits10,
1669 aom_highbd_jnt_sad16x32_avg_bits10,
1670 aom_highbd_10_jnt_sub_pixel_avg_variance16x32);
1671
1672 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1673 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1674 aom_highbd_10_sub_pixel_variance64x32,
1675 aom_highbd_10_sub_pixel_avg_variance64x32, NULL, NULL,
1676 aom_highbd_sad64x32x4d_bits10,
1677 aom_highbd_jnt_sad64x32_avg_bits10,
1678 aom_highbd_10_jnt_sub_pixel_avg_variance64x32);
1679
1680 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1681 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1682 aom_highbd_10_sub_pixel_variance32x64,
1683 aom_highbd_10_sub_pixel_avg_variance32x64, NULL, NULL,
1684 aom_highbd_sad32x64x4d_bits10,
1685 aom_highbd_jnt_sad32x64_avg_bits10,
1686 aom_highbd_10_jnt_sub_pixel_avg_variance32x64);
1687
1688 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1689 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1690 aom_highbd_10_sub_pixel_variance32x32,
1691 aom_highbd_10_sub_pixel_avg_variance32x32,
1692 aom_highbd_sad32x32x3_bits10, aom_highbd_sad32x32x8_bits10,
1693 aom_highbd_sad32x32x4d_bits10,
1694 aom_highbd_jnt_sad32x32_avg_bits10,
1695 aom_highbd_10_jnt_sub_pixel_avg_variance32x32);
1696
1697 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1698 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1699 aom_highbd_10_sub_pixel_variance64x64,
1700 aom_highbd_10_sub_pixel_avg_variance64x64,
1701 aom_highbd_sad64x64x3_bits10, aom_highbd_sad64x64x8_bits10,
1702 aom_highbd_sad64x64x4d_bits10,
1703 aom_highbd_jnt_sad64x64_avg_bits10,
1704 aom_highbd_10_jnt_sub_pixel_avg_variance64x64);
1705
1706 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1707 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1708 aom_highbd_10_sub_pixel_variance16x16,
1709 aom_highbd_10_sub_pixel_avg_variance16x16,
1710 aom_highbd_sad16x16x3_bits10, aom_highbd_sad16x16x8_bits10,
1711 aom_highbd_sad16x16x4d_bits10,
1712 aom_highbd_jnt_sad16x16_avg_bits10,
1713 aom_highbd_10_jnt_sub_pixel_avg_variance16x16);
1714
1715 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1716 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1717 aom_highbd_10_sub_pixel_variance16x8,
1718 aom_highbd_10_sub_pixel_avg_variance16x8,
1719 aom_highbd_sad16x8x3_bits10, aom_highbd_sad16x8x8_bits10,
1720 aom_highbd_sad16x8x4d_bits10,
1721 aom_highbd_jnt_sad16x8_avg_bits10,
1722 aom_highbd_10_jnt_sub_pixel_avg_variance16x8);
1723
1724 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1725 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1726 aom_highbd_10_sub_pixel_variance8x16,
1727 aom_highbd_10_sub_pixel_avg_variance8x16,
1728 aom_highbd_sad8x16x3_bits10, aom_highbd_sad8x16x8_bits10,
1729 aom_highbd_sad8x16x4d_bits10,
1730 aom_highbd_jnt_sad8x16_avg_bits10,
1731 aom_highbd_10_jnt_sub_pixel_avg_variance8x16);
1732
1733 HIGHBD_BFP(
1734 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1735 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
1736 aom_highbd_10_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits10,
1737 aom_highbd_sad8x8x8_bits10, aom_highbd_sad8x8x4d_bits10,
1738 aom_highbd_jnt_sad8x8_avg_bits10,
1739 aom_highbd_10_jnt_sub_pixel_avg_variance8x8);
1740
1741 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits10,
1742 aom_highbd_sad8x4_avg_bits10, aom_highbd_10_variance8x4,
1743 aom_highbd_10_sub_pixel_variance8x4,
1744 aom_highbd_10_sub_pixel_avg_variance8x4, NULL,
1745 aom_highbd_sad8x4x8_bits10, aom_highbd_sad8x4x4d_bits10,
1746 aom_highbd_jnt_sad8x4_avg_bits10,
1747 aom_highbd_10_jnt_sub_pixel_avg_variance8x4);
1748
1749 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits10,
1750 aom_highbd_sad4x8_avg_bits10, aom_highbd_10_variance4x8,
1751 aom_highbd_10_sub_pixel_variance4x8,
1752 aom_highbd_10_sub_pixel_avg_variance4x8, NULL,
1753 aom_highbd_sad4x8x8_bits10, aom_highbd_sad4x8x4d_bits10,
1754 aom_highbd_jnt_sad4x8_avg_bits10,
1755 aom_highbd_10_jnt_sub_pixel_avg_variance4x8);
1756
1757 HIGHBD_BFP(
1758 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1759 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
1760 aom_highbd_10_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits10,
1761 aom_highbd_sad4x4x8_bits10, aom_highbd_sad4x4x4d_bits10,
1762 aom_highbd_jnt_sad4x4_avg_bits10,
1763 aom_highbd_10_jnt_sub_pixel_avg_variance4x4);
1764
Cheng Chenbf3d4962017-11-01 14:48:52 -07001765 HIGHBD_BFP(
1766 BLOCK_128X128, aom_highbd_sad128x128_bits10,
1767 aom_highbd_sad128x128_avg_bits10, aom_highbd_10_variance128x128,
1768 aom_highbd_10_sub_pixel_variance128x128,
1769 aom_highbd_10_sub_pixel_avg_variance128x128,
1770 aom_highbd_sad128x128x3_bits10, aom_highbd_sad128x128x8_bits10,
1771 aom_highbd_sad128x128x4d_bits10,
1772 aom_highbd_jnt_sad128x128_avg_bits10,
1773 aom_highbd_10_jnt_sub_pixel_avg_variance128x128);
1774
1775 HIGHBD_BFP(
1776 BLOCK_128X64, aom_highbd_sad128x64_bits10,
1777 aom_highbd_sad128x64_avg_bits10, aom_highbd_10_variance128x64,
1778 aom_highbd_10_sub_pixel_variance128x64,
1779 aom_highbd_10_sub_pixel_avg_variance128x64, NULL, NULL,
1780 aom_highbd_sad128x64x4d_bits10, aom_highbd_jnt_sad128x64_avg_bits10,
1781 aom_highbd_10_jnt_sub_pixel_avg_variance128x64);
1782
1783 HIGHBD_BFP(
1784 BLOCK_64X128, aom_highbd_sad64x128_bits10,
1785 aom_highbd_sad64x128_avg_bits10, aom_highbd_10_variance64x128,
1786 aom_highbd_10_sub_pixel_variance64x128,
1787 aom_highbd_10_sub_pixel_avg_variance64x128, NULL, NULL,
1788 aom_highbd_sad64x128x4d_bits10, aom_highbd_jnt_sad64x128_avg_bits10,
1789 aom_highbd_10_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001790
David Barkerf19f35f2017-05-22 16:33:22 +01001791 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1792 aom_highbd_10_masked_sub_pixel_variance128x128)
1793 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1794 aom_highbd_10_masked_sub_pixel_variance128x64)
1795 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1796 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001797 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1798 aom_highbd_10_masked_sub_pixel_variance64x64)
1799 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1800 aom_highbd_10_masked_sub_pixel_variance64x32)
1801 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1802 aom_highbd_10_masked_sub_pixel_variance32x64)
1803 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1804 aom_highbd_10_masked_sub_pixel_variance32x32)
1805 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1806 aom_highbd_10_masked_sub_pixel_variance32x16)
1807 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1808 aom_highbd_10_masked_sub_pixel_variance16x32)
1809 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1810 aom_highbd_10_masked_sub_pixel_variance16x16)
1811 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1812 aom_highbd_10_masked_sub_pixel_variance8x16)
1813 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1814 aom_highbd_10_masked_sub_pixel_variance16x8)
1815 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1816 aom_highbd_10_masked_sub_pixel_variance8x8)
1817 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1818 aom_highbd_10_masked_sub_pixel_variance4x8)
1819 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1820 aom_highbd_10_masked_sub_pixel_variance8x4)
1821 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1822 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001823 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1824 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001825 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1826 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001827 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1828 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001829 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1830 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001831 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1832 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001833 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1834 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001835 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1836 aom_highbd_10_obmc_variance128x128,
1837 aom_highbd_10_obmc_sub_pixel_variance128x128)
1838 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1839 aom_highbd_10_obmc_variance128x64,
1840 aom_highbd_10_obmc_sub_pixel_variance128x64)
1841 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1842 aom_highbd_10_obmc_variance64x128,
1843 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001844 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1845 aom_highbd_10_obmc_variance64x64,
1846 aom_highbd_10_obmc_sub_pixel_variance64x64)
1847 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1848 aom_highbd_10_obmc_variance64x32,
1849 aom_highbd_10_obmc_sub_pixel_variance64x32)
1850 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
1851 aom_highbd_10_obmc_variance32x64,
1852 aom_highbd_10_obmc_sub_pixel_variance32x64)
1853 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
1854 aom_highbd_10_obmc_variance32x32,
1855 aom_highbd_10_obmc_sub_pixel_variance32x32)
1856 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
1857 aom_highbd_10_obmc_variance32x16,
1858 aom_highbd_10_obmc_sub_pixel_variance32x16)
1859 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
1860 aom_highbd_10_obmc_variance16x32,
1861 aom_highbd_10_obmc_sub_pixel_variance16x32)
1862 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
1863 aom_highbd_10_obmc_variance16x16,
1864 aom_highbd_10_obmc_sub_pixel_variance16x16)
1865 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
1866 aom_highbd_10_obmc_variance8x16,
1867 aom_highbd_10_obmc_sub_pixel_variance8x16)
1868 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
1869 aom_highbd_10_obmc_variance16x8,
1870 aom_highbd_10_obmc_sub_pixel_variance16x8)
1871 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
1872 aom_highbd_10_obmc_variance8x8,
1873 aom_highbd_10_obmc_sub_pixel_variance8x8)
1874 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
1875 aom_highbd_10_obmc_variance4x8,
1876 aom_highbd_10_obmc_sub_pixel_variance4x8)
1877 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
1878 aom_highbd_10_obmc_variance8x4,
1879 aom_highbd_10_obmc_sub_pixel_variance8x4)
1880 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
1881 aom_highbd_10_obmc_variance4x4,
1882 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001883
Rupert Swarbrick72678572017-08-02 12:05:26 +01001884 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
1885 aom_highbd_10_obmc_variance64x16,
1886 aom_highbd_10_obmc_sub_pixel_variance64x16)
1887
1888 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
1889 aom_highbd_10_obmc_variance16x64,
1890 aom_highbd_10_obmc_sub_pixel_variance16x64)
1891
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001892 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
1893 aom_highbd_10_obmc_variance32x8,
1894 aom_highbd_10_obmc_sub_pixel_variance32x8)
1895
1896 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
1897 aom_highbd_10_obmc_variance8x32,
1898 aom_highbd_10_obmc_sub_pixel_variance8x32)
1899
1900 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
1901 aom_highbd_10_obmc_variance16x4,
1902 aom_highbd_10_obmc_sub_pixel_variance16x4)
1903
1904 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
1905 aom_highbd_10_obmc_variance4x16,
1906 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001907 break;
1908
Yaowu Xuf883b422016-08-30 14:01:10 -07001909 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001910 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
1911 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
1912 aom_highbd_12_sub_pixel_variance64x16,
1913 aom_highbd_12_sub_pixel_avg_variance64x16, NULL, NULL,
1914 aom_highbd_sad64x16x4d_bits12,
1915 aom_highbd_jnt_sad64x16_avg_bits12,
1916 aom_highbd_12_jnt_sub_pixel_avg_variance64x16);
1917
1918 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
1919 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
1920 aom_highbd_12_sub_pixel_variance16x64,
1921 aom_highbd_12_sub_pixel_avg_variance16x64, NULL, NULL,
1922 aom_highbd_sad16x64x4d_bits12,
1923 aom_highbd_jnt_sad16x64_avg_bits12,
1924 aom_highbd_12_jnt_sub_pixel_avg_variance16x64);
1925
1926 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
1927 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
1928 aom_highbd_12_sub_pixel_variance32x8,
1929 aom_highbd_12_sub_pixel_avg_variance32x8, NULL, NULL,
1930 aom_highbd_sad32x8x4d_bits12,
1931 aom_highbd_jnt_sad32x8_avg_bits12,
1932 aom_highbd_12_jnt_sub_pixel_avg_variance32x8);
1933
1934 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
1935 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
1936 aom_highbd_12_sub_pixel_variance8x32,
1937 aom_highbd_12_sub_pixel_avg_variance8x32, NULL, NULL,
1938 aom_highbd_sad8x32x4d_bits12,
1939 aom_highbd_jnt_sad8x32_avg_bits12,
1940 aom_highbd_12_jnt_sub_pixel_avg_variance8x32);
1941
1942 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
1943 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
1944 aom_highbd_12_sub_pixel_variance16x4,
1945 aom_highbd_12_sub_pixel_avg_variance16x4, NULL, NULL,
1946 aom_highbd_sad16x4x4d_bits12,
1947 aom_highbd_jnt_sad16x4_avg_bits12,
1948 aom_highbd_12_jnt_sub_pixel_avg_variance16x4);
1949
1950 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
1951 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
1952 aom_highbd_12_sub_pixel_variance4x16,
1953 aom_highbd_12_sub_pixel_avg_variance4x16, NULL, NULL,
1954 aom_highbd_sad4x16x4d_bits12,
1955 aom_highbd_jnt_sad4x16_avg_bits12,
1956 aom_highbd_12_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001957
1958 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
1959 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
1960 aom_highbd_12_sub_pixel_variance32x16,
1961 aom_highbd_12_sub_pixel_avg_variance32x16, NULL, NULL,
1962 aom_highbd_sad32x16x4d_bits12,
1963 aom_highbd_jnt_sad32x16_avg_bits12,
1964 aom_highbd_12_jnt_sub_pixel_avg_variance32x16);
1965
1966 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
1967 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
1968 aom_highbd_12_sub_pixel_variance16x32,
1969 aom_highbd_12_sub_pixel_avg_variance16x32, NULL, NULL,
1970 aom_highbd_sad16x32x4d_bits12,
1971 aom_highbd_jnt_sad16x32_avg_bits12,
1972 aom_highbd_12_jnt_sub_pixel_avg_variance16x32);
1973
1974 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
1975 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
1976 aom_highbd_12_sub_pixel_variance64x32,
1977 aom_highbd_12_sub_pixel_avg_variance64x32, NULL, NULL,
1978 aom_highbd_sad64x32x4d_bits12,
1979 aom_highbd_jnt_sad64x32_avg_bits12,
1980 aom_highbd_12_jnt_sub_pixel_avg_variance64x32);
1981
1982 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
1983 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
1984 aom_highbd_12_sub_pixel_variance32x64,
1985 aom_highbd_12_sub_pixel_avg_variance32x64, NULL, NULL,
1986 aom_highbd_sad32x64x4d_bits12,
1987 aom_highbd_jnt_sad32x64_avg_bits12,
1988 aom_highbd_12_jnt_sub_pixel_avg_variance32x64);
1989
1990 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
1991 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
1992 aom_highbd_12_sub_pixel_variance32x32,
1993 aom_highbd_12_sub_pixel_avg_variance32x32,
1994 aom_highbd_sad32x32x3_bits12, aom_highbd_sad32x32x8_bits12,
1995 aom_highbd_sad32x32x4d_bits12,
1996 aom_highbd_jnt_sad32x32_avg_bits12,
1997 aom_highbd_12_jnt_sub_pixel_avg_variance32x32);
1998
1999 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2000 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2001 aom_highbd_12_sub_pixel_variance64x64,
2002 aom_highbd_12_sub_pixel_avg_variance64x64,
2003 aom_highbd_sad64x64x3_bits12, aom_highbd_sad64x64x8_bits12,
2004 aom_highbd_sad64x64x4d_bits12,
2005 aom_highbd_jnt_sad64x64_avg_bits12,
2006 aom_highbd_12_jnt_sub_pixel_avg_variance64x64);
2007
2008 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2009 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2010 aom_highbd_12_sub_pixel_variance16x16,
2011 aom_highbd_12_sub_pixel_avg_variance16x16,
2012 aom_highbd_sad16x16x3_bits12, aom_highbd_sad16x16x8_bits12,
2013 aom_highbd_sad16x16x4d_bits12,
2014 aom_highbd_jnt_sad16x16_avg_bits12,
2015 aom_highbd_12_jnt_sub_pixel_avg_variance16x16);
2016
2017 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,
2023 aom_highbd_jnt_sad16x8_avg_bits12,
2024 aom_highbd_12_jnt_sub_pixel_avg_variance16x8);
2025
2026 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2027 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2028 aom_highbd_12_sub_pixel_variance8x16,
2029 aom_highbd_12_sub_pixel_avg_variance8x16,
2030 aom_highbd_sad8x16x3_bits12, aom_highbd_sad8x16x8_bits12,
2031 aom_highbd_sad8x16x4d_bits12,
2032 aom_highbd_jnt_sad8x16_avg_bits12,
2033 aom_highbd_12_jnt_sub_pixel_avg_variance8x16);
2034
2035 HIGHBD_BFP(
2036 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2037 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
2038 aom_highbd_12_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x3_bits12,
2039 aom_highbd_sad8x8x8_bits12, aom_highbd_sad8x8x4d_bits12,
2040 aom_highbd_jnt_sad8x8_avg_bits12,
2041 aom_highbd_12_jnt_sub_pixel_avg_variance8x8);
2042
2043 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits12,
2044 aom_highbd_sad8x4_avg_bits12, aom_highbd_12_variance8x4,
2045 aom_highbd_12_sub_pixel_variance8x4,
2046 aom_highbd_12_sub_pixel_avg_variance8x4, NULL,
2047 aom_highbd_sad8x4x8_bits12, aom_highbd_sad8x4x4d_bits12,
2048 aom_highbd_jnt_sad8x4_avg_bits12,
2049 aom_highbd_12_jnt_sub_pixel_avg_variance8x4);
2050
2051 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits12,
2052 aom_highbd_sad4x8_avg_bits12, aom_highbd_12_variance4x8,
2053 aom_highbd_12_sub_pixel_variance4x8,
2054 aom_highbd_12_sub_pixel_avg_variance4x8, NULL,
2055 aom_highbd_sad4x8x8_bits12, aom_highbd_sad4x8x4d_bits12,
2056 aom_highbd_jnt_sad4x8_avg_bits12,
2057 aom_highbd_12_jnt_sub_pixel_avg_variance4x8);
2058
2059 HIGHBD_BFP(
2060 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2061 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
2062 aom_highbd_12_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x3_bits12,
2063 aom_highbd_sad4x4x8_bits12, aom_highbd_sad4x4x4d_bits12,
2064 aom_highbd_jnt_sad4x4_avg_bits12,
2065 aom_highbd_12_jnt_sub_pixel_avg_variance4x4);
2066
Cheng Chenbf3d4962017-11-01 14:48:52 -07002067 HIGHBD_BFP(
2068 BLOCK_128X128, aom_highbd_sad128x128_bits12,
2069 aom_highbd_sad128x128_avg_bits12, aom_highbd_12_variance128x128,
2070 aom_highbd_12_sub_pixel_variance128x128,
2071 aom_highbd_12_sub_pixel_avg_variance128x128,
2072 aom_highbd_sad128x128x3_bits12, aom_highbd_sad128x128x8_bits12,
2073 aom_highbd_sad128x128x4d_bits12,
2074 aom_highbd_jnt_sad128x128_avg_bits12,
2075 aom_highbd_12_jnt_sub_pixel_avg_variance128x128);
2076
2077 HIGHBD_BFP(
2078 BLOCK_128X64, aom_highbd_sad128x64_bits12,
2079 aom_highbd_sad128x64_avg_bits12, aom_highbd_12_variance128x64,
2080 aom_highbd_12_sub_pixel_variance128x64,
2081 aom_highbd_12_sub_pixel_avg_variance128x64, NULL, NULL,
2082 aom_highbd_sad128x64x4d_bits12, aom_highbd_jnt_sad128x64_avg_bits12,
2083 aom_highbd_12_jnt_sub_pixel_avg_variance128x64);
2084
2085 HIGHBD_BFP(
2086 BLOCK_64X128, aom_highbd_sad64x128_bits12,
2087 aom_highbd_sad64x128_avg_bits12, aom_highbd_12_variance64x128,
2088 aom_highbd_12_sub_pixel_variance64x128,
2089 aom_highbd_12_sub_pixel_avg_variance64x128, NULL, NULL,
2090 aom_highbd_sad64x128x4d_bits12, aom_highbd_jnt_sad64x128_avg_bits12,
2091 aom_highbd_12_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002092
David Barkerf19f35f2017-05-22 16:33:22 +01002093 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2094 aom_highbd_12_masked_sub_pixel_variance128x128)
2095 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2096 aom_highbd_12_masked_sub_pixel_variance128x64)
2097 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2098 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002099 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2100 aom_highbd_12_masked_sub_pixel_variance64x64)
2101 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2102 aom_highbd_12_masked_sub_pixel_variance64x32)
2103 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2104 aom_highbd_12_masked_sub_pixel_variance32x64)
2105 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2106 aom_highbd_12_masked_sub_pixel_variance32x32)
2107 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2108 aom_highbd_12_masked_sub_pixel_variance32x16)
2109 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2110 aom_highbd_12_masked_sub_pixel_variance16x32)
2111 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2112 aom_highbd_12_masked_sub_pixel_variance16x16)
2113 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2114 aom_highbd_12_masked_sub_pixel_variance8x16)
2115 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2116 aom_highbd_12_masked_sub_pixel_variance16x8)
2117 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2118 aom_highbd_12_masked_sub_pixel_variance8x8)
2119 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2120 aom_highbd_12_masked_sub_pixel_variance4x8)
2121 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2122 aom_highbd_12_masked_sub_pixel_variance8x4)
2123 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2124 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002125 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2126 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002127 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2128 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002129 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2130 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002131 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2132 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002133 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2134 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002135 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2136 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002137 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2138 aom_highbd_12_obmc_variance128x128,
2139 aom_highbd_12_obmc_sub_pixel_variance128x128)
2140 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2141 aom_highbd_12_obmc_variance128x64,
2142 aom_highbd_12_obmc_sub_pixel_variance128x64)
2143 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2144 aom_highbd_12_obmc_variance64x128,
2145 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002146 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2147 aom_highbd_12_obmc_variance64x64,
2148 aom_highbd_12_obmc_sub_pixel_variance64x64)
2149 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2150 aom_highbd_12_obmc_variance64x32,
2151 aom_highbd_12_obmc_sub_pixel_variance64x32)
2152 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2153 aom_highbd_12_obmc_variance32x64,
2154 aom_highbd_12_obmc_sub_pixel_variance32x64)
2155 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2156 aom_highbd_12_obmc_variance32x32,
2157 aom_highbd_12_obmc_sub_pixel_variance32x32)
2158 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2159 aom_highbd_12_obmc_variance32x16,
2160 aom_highbd_12_obmc_sub_pixel_variance32x16)
2161 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2162 aom_highbd_12_obmc_variance16x32,
2163 aom_highbd_12_obmc_sub_pixel_variance16x32)
2164 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2165 aom_highbd_12_obmc_variance16x16,
2166 aom_highbd_12_obmc_sub_pixel_variance16x16)
2167 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2168 aom_highbd_12_obmc_variance8x16,
2169 aom_highbd_12_obmc_sub_pixel_variance8x16)
2170 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2171 aom_highbd_12_obmc_variance16x8,
2172 aom_highbd_12_obmc_sub_pixel_variance16x8)
2173 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2174 aom_highbd_12_obmc_variance8x8,
2175 aom_highbd_12_obmc_sub_pixel_variance8x8)
2176 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2177 aom_highbd_12_obmc_variance4x8,
2178 aom_highbd_12_obmc_sub_pixel_variance4x8)
2179 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2180 aom_highbd_12_obmc_variance8x4,
2181 aom_highbd_12_obmc_sub_pixel_variance8x4)
2182 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2183 aom_highbd_12_obmc_variance4x4,
2184 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002185 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2186 aom_highbd_12_obmc_variance64x16,
2187 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002188 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2189 aom_highbd_12_obmc_variance16x64,
2190 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002191 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2192 aom_highbd_12_obmc_variance32x8,
2193 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002194 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2195 aom_highbd_12_obmc_variance8x32,
2196 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002197 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2198 aom_highbd_12_obmc_variance16x4,
2199 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002200 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2201 aom_highbd_12_obmc_variance4x16,
2202 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002203 break;
2204
2205 default:
2206 assert(0 &&
Yaowu Xuf883b422016-08-30 14:01:10 -07002207 "cm->bit_depth should be AOM_BITS_8, "
2208 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002209 }
2210 }
2211}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002212
Yaowu Xuf883b422016-08-30 14:01:10 -07002213static void realloc_segmentation_maps(AV1_COMP *cpi) {
2214 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002215
2216 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002217 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002218 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002219 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002220
2221 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002222 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002223 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002224 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002225
2226 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002227 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002228 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002229 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002230}
2231
Yaowu Xuf883b422016-08-30 14:01:10 -07002232void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2233 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002234 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002235 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002236 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002237
2238 if (cm->profile != oxcf->profile) cm->profile = oxcf->profile;
2239 cm->bit_depth = oxcf->bit_depth;
Andrey Norkin9e694632017-12-21 18:50:57 -08002240 cm->color_primaries = oxcf->color_primaries;
2241 cm->transfer_characteristics = oxcf->transfer_characteristics;
2242 cm->matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08002243 cm->seq_params.monochrome = oxcf->monochrome;
anorkin76fb1262017-03-22 15:12:12 -07002244 cm->chroma_sample_position = oxcf->chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002245 cm->color_range = oxcf->color_range;
2246
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08002247 assert(IMPLIES(cm->profile <= PROFILE_1, cm->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002248
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002249 cm->timing_info_present = oxcf->timing_info_present;
2250 cm->num_units_in_tick = oxcf->num_units_in_tick;
2251 cm->time_scale = oxcf->time_scale;
2252 cm->equal_picture_interval = oxcf->equal_picture_interval;
2253 cm->num_ticks_per_picture = oxcf->num_ticks_per_picture;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002254
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002255 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002256
Yaowu Xuc27fc142016-08-22 16:08:15 -07002257 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002258 cpi->common.options = oxcf->cfg;
hui sud9a812b2017-07-06 14:34:37 -07002259 x->e_mbd.bd = (int)cm->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002260 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002261
Yaowu Xuf883b422016-08-30 14:01:10 -07002262 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002263 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2264 } else {
2265 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2266 }
2267
2268 cpi->refresh_last_frame = 1;
2269 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002270 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002271 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002272
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002273 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2274 ? REFRESH_FRAME_CONTEXT_DISABLED
2275 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002276 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002277 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002278
Alex Converse74ad0912017-07-18 10:22:58 -07002279 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002280 CHECK_MEM_ERROR(cm, x->palette_buffer,
2281 aom_memalign(16, sizeof(*x->palette_buffer)));
2282 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002283 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002284 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002285
Yaowu Xuc27fc142016-08-22 16:08:15 -07002286 set_rc_buffer_sizes(rc, &cpi->oxcf);
2287
2288 // Under a configuration change, where maximum_buffer_size may change,
2289 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002290 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2291 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002292
2293 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002294 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002295
2296 // Set absolute upper and lower quality limits
2297 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2298 rc->best_quality = cpi->oxcf.best_allowed_q;
2299
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002300 if (!oxcf->large_scale_tile)
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002301 cm->interp_filter = cpi->sf.default_interp_filter;
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002302 else
2303 cm->interp_filter = EIGHTTAP_REGULAR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002304
Yue Chen5380cb52018-02-23 15:33:21 -08002305 cm->switchable_motion_mode = 1;
2306
Yaowu Xuc27fc142016-08-22 16:08:15 -07002307 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2308 cm->render_width = cpi->oxcf.render_width;
2309 cm->render_height = cpi->oxcf.render_height;
2310 } else {
2311 cm->render_width = cpi->oxcf.width;
2312 cm->render_height = cpi->oxcf.height;
2313 }
2314 cm->width = cpi->oxcf.width;
2315 cm->height = cpi->oxcf.height;
2316
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00002317 int sb_size = cm->seq_params.sb_size;
2318 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Dominic Symes917d6c02017-10-11 18:00:52 +02002319
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00002320 if (cpi->initial_width || sb_size != cm->seq_params.sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002321 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00002322 cm->seq_params.sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002323 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002324 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002325 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002326 realloc_segmentation_maps(cpi);
2327 cpi->initial_width = cpi->initial_height = 0;
2328 }
2329 }
2330 update_frame_size(cpi);
2331
2332 cpi->alt_ref_source = NULL;
2333 rc->is_src_frame_alt_ref = 0;
2334
Yaowu Xuc27fc142016-08-22 16:08:15 -07002335 rc->is_bwd_ref_frame = 0;
2336 rc->is_last_bipred_frame = 0;
2337 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002338
Yaowu Xuc27fc142016-08-22 16:08:15 -07002339 set_tile_info(cpi);
2340
2341 cpi->ext_refresh_frame_flags_pending = 0;
2342 cpi->ext_refresh_frame_context_pending = 0;
2343
Yaowu Xuc27fc142016-08-22 16:08:15 -07002344 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002345
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002346 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002347 // This should not be called after the first key frame.
2348 if (!cpi->seq_params_locked) {
2349 init_seq_coding_tools(&cm->seq_params, oxcf);
2350 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002351}
2352
Yaowu Xuf883b422016-08-30 14:01:10 -07002353AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2354 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002355 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002356 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2357 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002358
2359 if (!cm) return NULL;
2360
Yaowu Xuf883b422016-08-30 14:01:10 -07002361 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002362
2363 if (setjmp(cm->error.jmp)) {
2364 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002365 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002366 return 0;
2367 }
2368
2369 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002370 cm->alloc_mi = enc_alloc_mi;
2371 cm->free_mi = enc_free_mi;
2372 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002373
Angie Chianga5d96c42016-10-21 16:16:56 -07002374 CHECK_MEM_ERROR(cm, cm->fc,
2375 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2376 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2377 (FRAME_CONTEXT *)aom_memalign(
2378 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2379 memset(cm->fc, 0, sizeof(*cm->fc));
2380 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002381
2382 cpi->resize_state = 0;
2383 cpi->resize_avg_qp = 0;
2384 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002385
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386 cpi->common.buffer_pool = pool;
2387
2388 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002389 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390
2391 cm->current_video_frame = 0;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002392 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002393 cpi->partition_search_skippable_frame = 0;
2394 cpi->tile_data = NULL;
2395 cpi->last_show_frame_buf_idx = INVALID_IDX;
2396
2397 realloc_segmentation_maps(cpi);
2398
Jingning Hanf050fc12018-03-09 14:53:33 -08002399 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2400 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002401
Yaowu Xuc27fc142016-08-22 16:08:15 -07002402 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2403 i++) {
2404 CHECK_MEM_ERROR(
2405 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002406 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002407 }
2408
2409#if CONFIG_FP_MB_STATS
2410 cpi->use_fp_mb_stats = 0;
2411 if (cpi->use_fp_mb_stats) {
2412 // a place holder used to store the first pass mb stats in the first pass
2413 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002414 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002415 } else {
2416 cpi->twopass.frame_mb_stats_buf = NULL;
2417 }
2418#endif
2419
2420 cpi->refresh_alt_ref_frame = 0;
2421 cpi->multi_arf_last_grp_enabled = 0;
2422
2423 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2424#if CONFIG_INTERNAL_STATS
2425 cpi->b_calculate_blockiness = 1;
2426 cpi->b_calculate_consistency = 1;
2427 cpi->total_inconsistency = 0;
2428 cpi->psnr.worst = 100.0;
2429 cpi->worst_ssim = 100.0;
2430
2431 cpi->count = 0;
2432 cpi->bytes = 0;
2433
2434 if (cpi->b_calculate_psnr) {
2435 cpi->total_sq_error = 0;
2436 cpi->total_samples = 0;
2437 cpi->tot_recode_hits = 0;
2438 cpi->summed_quality = 0;
2439 cpi->summed_weights = 0;
2440 }
2441
2442 cpi->fastssim.worst = 100.0;
2443 cpi->psnrhvs.worst = 100.0;
2444
2445 if (cpi->b_calculate_blockiness) {
2446 cpi->total_blockiness = 0;
2447 cpi->worst_blockiness = 0.0;
2448 }
2449
2450 if (cpi->b_calculate_consistency) {
2451 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002452 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453 cpi->common.mi_rows * cpi->common.mi_cols));
2454 cpi->worst_consistency = 100.0;
2455 }
2456#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002457#if CONFIG_ENTROPY_STATS
2458 av1_zero(aggregate_fc);
2459#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002460
2461 cpi->first_time_stamp_ever = INT64_MAX;
2462
Jingning Hanf050fc12018-03-09 14:53:33 -08002463 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2464 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2465 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2466 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002467
Yaowu Xuc27fc142016-08-22 16:08:15 -07002468#ifdef OUTPUT_YUV_SKINMAP
2469 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2470#endif
2471#ifdef OUTPUT_YUV_REC
2472 yuv_rec_file = fopen("rec.yuv", "wb");
2473#endif
2474
Yaowu Xuc27fc142016-08-22 16:08:15 -07002475 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002476 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002477 } else if (oxcf->pass == 2) {
2478 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2479 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2480
2481#if CONFIG_FP_MB_STATS
2482 if (cpi->use_fp_mb_stats) {
2483 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2484 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2485
2486 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2487 oxcf->firstpass_mb_stats_in.buf;
2488 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2489 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2490 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2491 }
2492#endif
2493
2494 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2495 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2496 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2497
Yaowu Xuf883b422016-08-30 14:01:10 -07002498 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002499 }
2500
Jingning Hand064cf02017-06-01 10:00:39 -07002501 CHECK_MEM_ERROR(
2502 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002503 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002504 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002505 CHECK_MEM_ERROR(
2506 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002507 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002508 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002509
2510 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2511 (int32_t *)aom_memalign(
2512 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2513
2514 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2515 (int32_t *)aom_memalign(
2516 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2517
Yaowu Xuf883b422016-08-30 14:01:10 -07002518 av1_set_speed_features_framesize_independent(cpi);
2519 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002520
Cheng Chenf78632e2017-10-20 15:30:51 -07002521#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX3F, SDX8F, SDX4DF, JSDAF, JSVAF) \
2522 cpi->fn_ptr[BT].sdf = SDF; \
2523 cpi->fn_ptr[BT].sdaf = SDAF; \
2524 cpi->fn_ptr[BT].vf = VF; \
2525 cpi->fn_ptr[BT].svf = SVF; \
2526 cpi->fn_ptr[BT].svaf = SVAF; \
2527 cpi->fn_ptr[BT].sdx3f = SDX3F; \
2528 cpi->fn_ptr[BT].sdx8f = SDX8F; \
2529 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2530 cpi->fn_ptr[BT].jsdaf = JSDAF; \
2531 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002532
Cheng Chenf78632e2017-10-20 15:30:51 -07002533 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
2534 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002535 aom_sad4x16x4d, aom_jnt_sad4x16_avg, aom_jnt_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002536
2537 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
2538 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002539 aom_sad16x4x4d, aom_jnt_sad16x4_avg, aom_jnt_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002540
2541 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
2542 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002543 aom_sad8x32x4d, aom_jnt_sad8x32_avg, aom_jnt_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002544
2545 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
2546 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002547 aom_sad32x8x4d, aom_jnt_sad32x8_avg, aom_jnt_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002548
2549 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
2550 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002551 aom_sad16x64x4d, aom_jnt_sad16x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002552 aom_jnt_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002553
2554 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
2555 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002556 aom_sad64x16x4d, aom_jnt_sad64x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002557 aom_jnt_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002558
Cheng Chenf78632e2017-10-20 15:30:51 -07002559 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2560 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
2561 aom_sad128x128x3, aom_sad128x128x8, aom_sad128x128x4d,
Cheng Chend0179a62017-11-16 17:02:53 -08002562 aom_jnt_sad128x128_avg, aom_jnt_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002563
2564 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
2565 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002566 NULL, aom_sad128x64x4d, aom_jnt_sad128x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002567 aom_jnt_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002568
2569 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
2570 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002571 NULL, aom_sad64x128x4d, aom_jnt_sad64x128_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002572 aom_jnt_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002573
2574 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
2575 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002576 aom_sad32x16x4d, aom_jnt_sad32x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002577 aom_jnt_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002578
2579 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
2580 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002581 aom_sad16x32x4d, aom_jnt_sad16x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002582 aom_jnt_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002583
2584 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
2585 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002586 aom_sad64x32x4d, aom_jnt_sad64x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002587 aom_jnt_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002588
2589 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
2590 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64, NULL, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002591 aom_sad32x64x4d, aom_jnt_sad32x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002592 aom_jnt_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002593
2594 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2595 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002596 aom_sad32x32x3, aom_sad32x32x8, aom_sad32x32x4d, aom_jnt_sad32x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002597 aom_jnt_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002598
2599 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2600 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002601 aom_sad64x64x3, aom_sad64x64x8, aom_sad64x64x4d, aom_jnt_sad64x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002602 aom_jnt_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002603
2604 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2605 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002606 aom_sad16x16x3, aom_sad16x16x8, aom_sad16x16x4d, aom_jnt_sad16x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002607 aom_jnt_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002608
2609 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
2610 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8, aom_sad16x8x3,
Cheng Chend0179a62017-11-16 17:02:53 -08002611 aom_sad16x8x8, aom_sad16x8x4d, aom_jnt_sad16x8_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002612 aom_jnt_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002613
2614 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
2615 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16, aom_sad8x16x3,
Cheng Chend0179a62017-11-16 17:02:53 -08002616 aom_sad8x16x8, aom_sad8x16x4d, aom_jnt_sad8x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002617 aom_jnt_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002618
2619 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
2620 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x3,
Cheng Chend0179a62017-11-16 17:02:53 -08002621 aom_sad8x8x8, aom_sad8x8x4d, aom_jnt_sad8x8_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002622 aom_jnt_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002623
2624 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
2625 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002626 aom_sad8x4x8, aom_sad8x4x4d, aom_jnt_sad8x4_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002627 aom_jnt_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002628
2629 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
2630 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, NULL,
Cheng Chend0179a62017-11-16 17:02:53 -08002631 aom_sad4x8x8, aom_sad4x8x4d, aom_jnt_sad4x8_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002632 aom_jnt_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002633
2634 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
2635 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x3,
Cheng Chend0179a62017-11-16 17:02:53 -08002636 aom_sad4x4x8, aom_sad4x4x4d, aom_jnt_sad4x4_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002637 aom_jnt_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002638
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639#define OBFP(BT, OSDF, OVF, OSVF) \
2640 cpi->fn_ptr[BT].osdf = OSDF; \
2641 cpi->fn_ptr[BT].ovf = OVF; \
2642 cpi->fn_ptr[BT].osvf = OSVF;
2643
Yaowu Xuf883b422016-08-30 14:01:10 -07002644 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2645 aom_obmc_sub_pixel_variance128x128)
2646 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2647 aom_obmc_sub_pixel_variance128x64)
2648 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2649 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002650 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2651 aom_obmc_sub_pixel_variance64x64)
2652 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2653 aom_obmc_sub_pixel_variance64x32)
2654 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2655 aom_obmc_sub_pixel_variance32x64)
2656 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2657 aom_obmc_sub_pixel_variance32x32)
2658 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2659 aom_obmc_sub_pixel_variance32x16)
2660 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2661 aom_obmc_sub_pixel_variance16x32)
2662 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2663 aom_obmc_sub_pixel_variance16x16)
2664 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2665 aom_obmc_sub_pixel_variance16x8)
2666 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2667 aom_obmc_sub_pixel_variance8x16)
2668 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2669 aom_obmc_sub_pixel_variance8x8)
2670 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2671 aom_obmc_sub_pixel_variance4x8)
2672 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2673 aom_obmc_sub_pixel_variance8x4)
2674 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2675 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002676 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2677 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002678 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2679 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002680 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2681 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002682 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2683 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002684 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2685 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002686 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2687 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002688
David Barkerf19f35f2017-05-22 16:33:22 +01002689#define MBFP(BT, MCSDF, MCSVF) \
2690 cpi->fn_ptr[BT].msdf = MCSDF; \
2691 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002692
David Barkerf19f35f2017-05-22 16:33:22 +01002693 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2694 aom_masked_sub_pixel_variance128x128)
2695 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2696 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002697 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2698 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2699 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2700 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2701 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2702 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2703 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2704 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2705 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2706 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2707 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2708 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2709 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002710
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002711 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2712
2713 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2714
2715 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2716
2717 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002718
2719 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2720
2721 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002722
Yaowu Xuc27fc142016-08-22 16:08:15 -07002723 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002724
Yaowu Xuf883b422016-08-30 14:01:10 -07002725 /* av1_init_quantizer() is first called here. Add check in
2726 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002727 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002728 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002729 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002730 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08002731 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002732
Yaowu Xuf883b422016-08-30 14:01:10 -07002733 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07002734 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002735 cm->superres_upscaled_width = oxcf->width;
2736 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002737 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002738
2739 cm->error.setjmp = 0;
2740
2741 return cpi;
2742}
2743
2744#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2745
2746#define SNPRINT2(H, T, V) \
2747 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
2748
Yaowu Xuf883b422016-08-30 14:01:10 -07002749void av1_remove_compressor(AV1_COMP *cpi) {
2750 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002751 unsigned int i;
2752 int t;
2753
2754 if (!cpi) return;
2755
2756 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002757 const int num_planes = av1_num_planes(cm);
2758
Yaowu Xuc27fc142016-08-22 16:08:15 -07002759 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002760#if CONFIG_ENTROPY_STATS
2761 if (cpi->oxcf.pass != 1) {
2762 fprintf(stderr, "Writing counts.stt\n");
2763 FILE *f = fopen("counts.stt", "wb");
2764 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
2765 fclose(f);
2766 }
2767#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002768#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07002769 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002770
2771 if (cpi->oxcf.pass != 1) {
2772 char headings[512] = { 0 };
2773 char results[512] = { 0 };
2774 FILE *f = fopen("opsnr.stt", "a");
2775 double time_encoded =
2776 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2777 10000000.000;
2778 double total_encode_time =
2779 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2780 const double dr =
2781 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2782 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2783 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2784 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2785
2786 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002787 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002788 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2789 const double total_ssim =
2790 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2791 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08002792 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07002793 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002794 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08002795 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002796 snprintf(results, sizeof(results),
2797 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2798 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002799 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08002800 "%7.3f\t%7.3f\t%7.3f",
Yaowu Xuc27fc142016-08-22 16:08:15 -07002801 dr, cpi->psnr.stat[ALL] / cpi->count, total_psnr,
Jingning Han87651b22017-11-28 20:02:26 -08002802 cpi->psnr.stat[ALL] / cpi->count, total_psnr, total_ssim,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002803 total_ssim, cpi->fastssim.stat[ALL] / cpi->count,
2804 cpi->psnrhvs.stat[ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002805 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Jingning Han87651b22017-11-28 20:02:26 -08002806 cpi->psnr.stat[Y] / cpi->count, cpi->psnr.stat[U] / cpi->count,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002807 cpi->psnr.stat[V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002808
2809 if (cpi->b_calculate_blockiness) {
2810 SNPRINT(headings, "\t Block\tWstBlck");
2811 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2812 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2813 }
2814
2815 if (cpi->b_calculate_consistency) {
2816 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07002817 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002818 (double)cpi->total_inconsistency);
2819
2820 SNPRINT(headings, "\tConsist\tWstCons");
2821 SNPRINT2(results, "\t%7.3f", consistency);
2822 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2823 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07002824 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2825 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002826 rate_err, fabs(rate_err));
2827 }
2828
2829 fclose(f);
2830 }
2831
2832#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002833 }
2834
2835 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002836 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002837 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2838
2839 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07002840 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002841
2842 // Deallocate allocated thread data.
2843 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07002844 aom_free(thread_data->td->palette_buffer);
Jingning Hand064cf02017-06-01 10:00:39 -07002845 aom_free(thread_data->td->above_pred_buf);
2846 aom_free(thread_data->td->left_pred_buf);
2847 aom_free(thread_data->td->wsrc_buf);
2848 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002849 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002850 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07002851 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002852 }
2853 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002854 aom_free(cpi->tile_thr_data);
2855 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002856
Yaowu Xuc27fc142016-08-22 16:08:15 -07002857 dealloc_compressor_data(cpi);
2858
2859 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2860 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002861 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002862 }
2863
2864#if CONFIG_FP_MB_STATS
2865 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002866 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002867 cpi->twopass.frame_mb_stats_buf = NULL;
2868 }
2869#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08002870#if CONFIG_INTERNAL_STATS
2871 aom_free(cpi->ssim_vars);
2872 cpi->ssim_vars = NULL;
2873#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002874
Yaowu Xuf883b422016-08-30 14:01:10 -07002875 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08002876 for (i = 0; i < FRAME_BUFFERS; ++i) {
2877 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
2878 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08002879 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xuf883b422016-08-30 14:01:10 -07002880 av1_free_ref_frame_buffers(cm->buffer_pool);
2881 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002882
2883#ifdef OUTPUT_YUV_SKINMAP
2884 fclose(yuv_skinmap_file);
2885#endif
2886#ifdef OUTPUT_YUV_REC
2887 fclose(yuv_rec_file);
2888#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002889}
2890
Yaowu Xuf883b422016-08-30 14:01:10 -07002891static void generate_psnr_packet(AV1_COMP *cpi) {
2892 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002893 int i;
2894 PSNR_STATS psnr;
Alex Conversef77fd0b2017-04-20 11:00:24 -07002895 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002896 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002897
2898 for (i = 0; i < 4; ++i) {
2899 pkt.data.psnr.samples[i] = psnr.samples[i];
2900 pkt.data.psnr.sse[i] = psnr.sse[i];
2901 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2902 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002903 pkt.kind = AOM_CODEC_PSNR_PKT;
2904 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002905}
2906
Yaowu Xuf883b422016-08-30 14:01:10 -07002907int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002908 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
2909
Yunqing Wangf2e7a392017-11-08 00:27:21 -08002910 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002911 return 0;
2912}
2913
Yunqing Wang9a50fec2017-11-02 17:02:00 -07002914void av1_update_reference(AV1_COMP *cpi, int ref_frame_upd_flags) {
2915 cpi->ext_refresh_last_frame = (ref_frame_upd_flags & AOM_LAST_FLAG) != 0;
2916 cpi->ext_refresh_golden_frame = (ref_frame_upd_flags & AOM_GOLD_FLAG) != 0;
2917 cpi->ext_refresh_alt_ref_frame = (ref_frame_upd_flags & AOM_ALT_FLAG) != 0;
2918 cpi->ext_refresh_bwd_ref_frame = (ref_frame_upd_flags & AOM_BWD_FLAG) != 0;
2919 cpi->ext_refresh_alt2_ref_frame = (ref_frame_upd_flags & AOM_ALT2_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002920 cpi->ext_refresh_frame_flags_pending = 1;
2921}
2922
Thomas Daede497d1952017-08-08 17:33:06 -07002923int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
2924 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002925 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07002926 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002927 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002928 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002929 return 0;
2930 } else {
2931 return -1;
2932 }
2933}
2934
Thomas Daede497d1952017-08-08 17:33:06 -07002935int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
2936 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002937 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07002938 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07002939 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002940 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07002941 return 0;
2942 } else {
2943 return -1;
2944 }
2945}
2946
2947int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002948 cpi->ext_refresh_frame_context = update;
2949 cpi->ext_refresh_frame_context_pending = 1;
2950 return 0;
2951}
2952
2953#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
2954// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
2955// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
2956// not denoise the UV channels at this time. If ever we implement UV channel
2957// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07002958void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002959 uint8_t *src = s->y_buffer;
2960 int h = s->y_height;
2961
2962 do {
2963 fwrite(src, s->y_width, 1, f);
2964 src += s->y_stride;
2965 } while (--h);
2966
2967 src = s->u_buffer;
2968 h = s->uv_height;
2969
2970 do {
2971 fwrite(src, s->uv_width, 1, f);
2972 src += s->uv_stride;
2973 } while (--h);
2974
2975 src = s->v_buffer;
2976 h = s->uv_height;
2977
2978 do {
2979 fwrite(src, s->uv_width, 1, f);
2980 src += s->uv_stride;
2981 } while (--h);
2982}
2983#endif
2984
Zoe Liu8dd1c982017-09-11 10:14:35 -07002985#if USE_GF16_MULTI_LAYER
2986static void check_show_existing_frame_gf16(AV1_COMP *cpi) {
2987 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
2988 AV1_COMMON *const cm = &cpi->common;
2989 const FRAME_UPDATE_TYPE next_frame_update_type =
2990 gf_group->update_type[gf_group->index];
2991
2992 if (cm->show_existing_frame == 1) {
2993 cm->show_existing_frame = 0;
2994 } else if (cpi->rc.is_last_bipred_frame) {
2995 cpi->rc.is_last_bipred_frame = 0;
2996 cm->show_existing_frame = 1;
Zoe Liu5989a722018-03-29 13:37:36 -07002997 cpi->existing_fb_idx_to_show = cpi->ref_fb_idx[BWDREF_FRAME - 1];
Zoe Liu8dd1c982017-09-11 10:14:35 -07002998 } else if (next_frame_update_type == OVERLAY_UPDATE ||
2999 next_frame_update_type == INTNL_OVERLAY_UPDATE) {
3000 // Check the temporal filtering status for the next OVERLAY frame
3001 const int num_arfs_in_gf = cpi->num_extra_arfs + 1;
3002 int which_arf = 0, arf_idx;
3003 // Identify the index to the next overlay frame.
3004 for (arf_idx = 0; arf_idx < num_arfs_in_gf; arf_idx++) {
3005 if (gf_group->index == cpi->arf_pos_for_ovrly[arf_idx]) {
3006 which_arf = arf_idx;
3007 break;
3008 }
3009 }
3010 assert(arf_idx < num_arfs_in_gf);
3011 if (cpi->is_arf_filter_off[which_arf]) {
3012 cm->show_existing_frame = 1;
3013 cpi->rc.is_src_frame_alt_ref = 1;
3014 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
Zoe Liu5989a722018-03-29 13:37:36 -07003015 ? cpi->ref_fb_idx[ALTREF_FRAME - 1]
3016 : cpi->ref_fb_idx[BWDREF_FRAME - 1];
Zoe Liu8dd1c982017-09-11 10:14:35 -07003017 cpi->is_arf_filter_off[which_arf] = 0;
3018 }
3019 }
3020 cpi->rc.is_src_frame_ext_arf = 0;
3021}
3022#endif // USE_GF16_MULTI_LAYER
3023
Yaowu Xuf883b422016-08-30 14:01:10 -07003024static void check_show_existing_frame(AV1_COMP *cpi) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003025#if USE_GF16_MULTI_LAYER
3026 if (cpi->rc.baseline_gf_interval == 16) {
3027 check_show_existing_frame_gf16(cpi);
3028 return;
3029 }
3030#endif // USE_GF16_MULTI_LAYER
3031
Yaowu Xuc27fc142016-08-22 16:08:15 -07003032 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003033 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003034 const FRAME_UPDATE_TYPE next_frame_update_type =
3035 gf_group->update_type[gf_group->index];
3036 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Zoe Liu5fca7242016-10-10 17:18:57 -07003037
3038 if (cm->show_existing_frame == 1) {
3039 cm->show_existing_frame = 0;
3040 } else if (cpi->rc.is_last_bipred_frame) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003041 // NOTE: If the current frame is a last bi-predictive frame, it is
3042 // needed next to show the BWDREF_FRAME, which is pointed by
3043 // the last_fb_idxes[0] after reference frame buffer update
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044 cpi->rc.is_last_bipred_frame = 0;
3045 cm->show_existing_frame = 1;
Zoe Liu5989a722018-03-29 13:37:36 -07003046 cpi->existing_fb_idx_to_show = cpi->ref_fb_idx[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003047 } else if (cpi->is_arf_filter_off[which_arf] &&
3048 (next_frame_update_type == OVERLAY_UPDATE ||
3049 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
3050 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003051 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003052 cm->show_existing_frame = 1;
3053 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003054 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
Zoe Liu5989a722018-03-29 13:37:36 -07003055 ? cpi->ref_fb_idx[ALTREF_FRAME - 1]
3056 : cpi->ref_fb_idx[ALTREF2_FRAME - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003057 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003058 }
3059 cpi->rc.is_src_frame_ext_arf = 0;
3060}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003061
3062#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003063void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003064 uint8_t *src = s->y_buffer;
3065 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003066 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003067 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3068 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3069
3070 do {
3071 fwrite(src16, s->y_width, 2, yuv_rec_file);
3072 src16 += s->y_stride;
3073 } while (--h);
3074
3075 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3076 h = s->uv_height;
3077
3078 do {
3079 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3080 src16 += s->uv_stride;
3081 } while (--h);
3082
3083 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3084 h = s->uv_height;
3085
3086 do {
3087 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3088 src16 += s->uv_stride;
3089 } while (--h);
3090
3091 fflush(yuv_rec_file);
3092 return;
3093 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003094
3095 do {
3096 fwrite(src, s->y_width, 1, yuv_rec_file);
3097 src += s->y_stride;
3098 } while (--h);
3099
3100 src = s->u_buffer;
3101 h = s->uv_height;
3102
3103 do {
3104 fwrite(src, s->uv_width, 1, yuv_rec_file);
3105 src += s->uv_stride;
3106 } while (--h);
3107
3108 src = s->v_buffer;
3109 h = s->uv_height;
3110
3111 do {
3112 fwrite(src, s->uv_width, 1, yuv_rec_file);
3113 src += s->uv_stride;
3114 } while (--h);
3115
3116 fflush(yuv_rec_file);
3117}
3118#endif // OUTPUT_YUV_REC
3119
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003120#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003121static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3122 int i;
3123 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003124 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003125 AV1_COMMON *const cm = &cpi->common;
3126 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3127 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003128 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003129 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003130 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003131 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003132 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003133 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003134 // TODO(sarahparker): The earlier condition for recoding here was:
3135 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3136 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003137 }
3138 }
3139 return recode;
3140}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003141
Yaowu Xuc27fc142016-08-22 16:08:15 -07003142// Function to test for conditions that indicate we should loop
3143// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003144static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3145 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003146 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003147 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003148 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3149 int force_recode = 0;
3150
3151 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3152 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3153 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003154 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3155 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3156 (rc->projected_frame_size < low_limit && q > minq)) {
3157 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003158 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003159 // Deal with frame undershoot and whether or not we are
3160 // below the automatically set cq level.
3161 if (q > oxcf->cq_level &&
3162 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3163 force_recode = 1;
3164 }
3165 }
3166 }
3167 return force_recode;
3168}
3169
Yaowu Xuc27fc142016-08-22 16:08:15 -07003170#define DUMP_REF_FRAME_IMAGES 0
3171
3172#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003173static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003174 const YV12_BUFFER_CONFIG *const ref_buf,
3175 char *file_name) {
3176 int h;
3177 FILE *f_ref = NULL;
3178
3179 if (ref_buf == NULL) {
3180 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003181 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003182 }
3183
3184 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3185 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003186 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003187 }
3188
3189 // --- Y ---
3190 for (h = 0; h < cm->height; ++h) {
3191 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3192 }
3193 // --- U ---
3194 for (h = 0; h < (cm->height >> 1); ++h) {
3195 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3196 f_ref);
3197 }
3198 // --- V ---
3199 for (h = 0; h < (cm->height >> 1); ++h) {
3200 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3201 f_ref);
3202 }
3203
3204 fclose(f_ref);
3205
Yaowu Xuf883b422016-08-30 14:01:10 -07003206 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003207}
3208
Yaowu Xuf883b422016-08-30 14:01:10 -07003209static void dump_ref_frame_images(AV1_COMP *cpi) {
3210 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003211 MV_REFERENCE_FRAME ref_frame;
3212
3213 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3214 char file_name[256] = "";
3215 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3216 cm->current_video_frame, ref_frame);
3217 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3218 }
3219}
3220#endif // DUMP_REF_FRAME_IMAGES == 1
3221
Yaowu Xuc27fc142016-08-22 16:08:15 -07003222// This function is used to shift the virtual indices of last reference frames
3223// as follows:
3224// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3225// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003226static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003227 // TODO(isbs): shift the scaled indices as well
Yaowu Xuc27fc142016-08-22 16:08:15 -07003228 int ref_frame;
3229 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003230 cpi->ref_fb_idx[ref_frame] = cpi->ref_fb_idx[ref_frame - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003231
3232 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003233 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003234 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003235 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3236 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3237 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003238 }
3239 }
3240}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003241
Zoe Liu8dd1c982017-09-11 10:14:35 -07003242#if USE_GF16_MULTI_LAYER
3243static void update_reference_frames_gf16(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003244 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003245 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003246
Zoe Liu8dd1c982017-09-11 10:14:35 -07003247 if (cm->frame_type == KEY_FRAME) {
Zoe Liu5989a722018-03-29 13:37:36 -07003248 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003249 ref_cnt_fb(pool->frame_bufs,
Zoe Liu5989a722018-03-29 13:37:36 -07003250 &cm->ref_frame_map[cpi->ref_fb_idx[ref_frame]],
Zoe Liu8dd1c982017-09-11 10:14:35 -07003251 cm->new_fb_idx);
3252 }
Zoe Liu8dd1c982017-09-11 10:14:35 -07003253 } else {
3254 if (cpi->refresh_last_frame || cpi->refresh_golden_frame ||
3255 cpi->refresh_bwd_ref_frame || cpi->refresh_alt2_ref_frame ||
3256 cpi->refresh_alt_ref_frame) {
3257 assert(cpi->refresh_fb_idx >= 0 && cpi->refresh_fb_idx < REF_FRAMES);
3258 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->refresh_fb_idx],
3259 cm->new_fb_idx);
3260 }
3261
3262 // TODO(zoeliu): To handle cpi->interp_filter_selected[].
3263
3264 // For GF of 16, an additional ref frame index mapping needs to be handled
3265 // if this is the last frame to encode in the current GF group.
3266 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3267 if (gf_group->update_type[gf_group->index + 1] == OVERLAY_UPDATE)
3268 av1_ref_frame_map_idx_updates(cpi, gf_group->index + 1);
3269 }
3270
3271#if DUMP_REF_FRAME_IMAGES == 1
3272 // Dump out all reference frame images.
3273 dump_ref_frame_images(cpi);
3274#endif // DUMP_REF_FRAME_IMAGES
3275}
3276#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003277
3278static void update_reference_frames(AV1_COMP *cpi) {
3279 AV1_COMMON *const cm = &cpi->common;
3280
Yaowu Xuc27fc142016-08-22 16:08:15 -07003281 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3282 // for the purpose to verify no mismatch between encoder and decoder.
3283 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3284
Zoe Liu8dd1c982017-09-11 10:14:35 -07003285#if USE_GF16_MULTI_LAYER
3286 if (cpi->rc.baseline_gf_interval == 16) {
3287 update_reference_frames_gf16(cpi);
3288 return;
3289 }
3290#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003291
3292 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003293 // At this point the new frame has been encoded.
3294 // If any buffer copy / swapping is signaled it should be done here.
Tarek AMARAc9813852018-03-05 18:40:18 -05003295 if (cm->frame_type == KEY_FRAME || frame_is_sframe(cm)) {
Zoe Liu5989a722018-03-29 13:37:36 -07003296 ref_cnt_fb(pool->frame_bufs,
3297 &cm->ref_frame_map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003298 cm->new_fb_idx);
Zoe Liu5989a722018-03-29 13:37:36 -07003299 ref_cnt_fb(pool->frame_bufs,
3300 &cm->ref_frame_map[cpi->ref_fb_idx[BWDREF_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003301 cm->new_fb_idx);
Zoe Liu5989a722018-03-29 13:37:36 -07003302 ref_cnt_fb(pool->frame_bufs,
3303 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]],
Zoe Liue9b15e22017-07-19 15:53:01 -07003304 cm->new_fb_idx);
Zoe Liu5989a722018-03-29 13:37:36 -07003305 ref_cnt_fb(pool->frame_bufs,
3306 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003307 cm->new_fb_idx);
Zoe Liu5989a722018-03-29 13:37:36 -07003308 ref_cnt_fb(pool->frame_bufs,
3309 &cm->ref_frame_map[cpi->ref_fb_idx[REF_FRAMES - 1]],
Imdad Sardharwalla53462b32018-03-13 18:10:49 +00003310 cm->new_fb_idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003311 } else if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003312 // We have decided to preserve the previously existing golden frame as our
3313 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003314 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003315 // we're updating the GF with the current decoded frame, we save it to the
3316 // ARF slot instead.
3317 // We now have to update the ARF with the current frame and swap gld_fb_idx
3318 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3319 // slot and, if we're updating the GF, the current frame becomes the new GF.
3320 int tmp;
3321
Zoe Liu5989a722018-03-29 13:37:36 -07003322 ref_cnt_fb(pool->frame_bufs,
3323 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003324 cm->new_fb_idx);
Zoe Liu5989a722018-03-29 13:37:36 -07003325 tmp = cpi->ref_fb_idx[ALTREF_FRAME - 1];
3326 cpi->ref_fb_idx[ALTREF_FRAME - 1] = cpi->ref_fb_idx[GOLDEN_FRAME - 1];
3327 cpi->ref_fb_idx[GOLDEN_FRAME - 1] = tmp;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003328
Yaowu Xuc27fc142016-08-22 16:08:15 -07003329 // We need to modify the mapping accordingly
Zoe Liu5989a722018-03-29 13:37:36 -07003330 cpi->arf_map[0] = cpi->ref_fb_idx[ALTREF_FRAME - 1];
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003331 // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3332 // cpi->interp_filter_selected[GOLDEN_FRAME]?
Yaowu Xuc27fc142016-08-22 16:08:15 -07003333 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
3334 // Deal with the special case for showing existing internal ALTREF_FRAME
3335 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3336 // by updating the virtual indices.
3337 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -07003338 const int which_arf = gf_group->arf_ref_idx[gf_group->index];
3339 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003340
Zoe Liu5989a722018-03-29 13:37:36 -07003341 const int tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003342 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003343
Zoe Liu5989a722018-03-29 13:37:36 -07003344 cpi->ref_fb_idx[LAST_FRAME - 1] = cpi->ref_fb_idx[ALTREF2_FRAME - 1];
3345 cpi->ref_fb_idx[ALTREF2_FRAME - 1] = tmp;
Zoe Liue9b15e22017-07-19 15:53:01 -07003346 // We need to modify the mapping accordingly
Zoe Liu5989a722018-03-29 13:37:36 -07003347 cpi->arf_map[which_arf] = cpi->ref_fb_idx[ALTREF2_FRAME - 1];
Zoe Liue9b15e22017-07-19 15:53:01 -07003348
3349 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3350 cpi->interp_filter_selected[ALTREF2_FRAME],
3351 sizeof(cpi->interp_filter_selected[ALTREF2_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003352 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003353 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003354 if (cpi->refresh_alt_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003355 int arf_idx = cpi->ref_fb_idx[ALTREF_FRAME - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003356 int which_arf = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003357 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003358
3359 memcpy(cpi->interp_filter_selected[ALTREF_FRAME + which_arf],
3360 cpi->interp_filter_selected[0],
3361 sizeof(cpi->interp_filter_selected[0]));
3362 }
3363
Zoe Liue9b15e22017-07-19 15:53:01 -07003364 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003365 if (cpi->refresh_golden_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003366 ref_cnt_fb(pool->frame_bufs,
3367 &cm->ref_frame_map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003368 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003369
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003370 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3371 cpi->interp_filter_selected[0],
3372 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003373 }
3374
Zoe Liue9b15e22017-07-19 15:53:01 -07003375 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003376 if (cpi->refresh_bwd_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003377 ref_cnt_fb(pool->frame_bufs,
3378 &cm->ref_frame_map[cpi->ref_fb_idx[BWDREF_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003379 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003380
3381 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3382 cpi->interp_filter_selected[0],
3383 sizeof(cpi->interp_filter_selected[0]));
3384 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003385
Zoe Liue9b15e22017-07-19 15:53:01 -07003386 // === ALTREF2_FRAME ===
3387 if (cpi->refresh_alt2_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003388 ref_cnt_fb(pool->frame_bufs,
3389 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]],
Zoe Liue9b15e22017-07-19 15:53:01 -07003390 cm->new_fb_idx);
3391
3392 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3393 cpi->interp_filter_selected[0],
3394 sizeof(cpi->interp_filter_selected[0]));
3395 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003396 }
3397
3398 if (cpi->refresh_last_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003399 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3400 // reference to the reference frame buffer virtual index; and then (2) from
3401 // the virtual index to the reference frame buffer physical index:
3402 //
3403 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3404 // | | |
3405 // v v v
Zoe Liu5989a722018-03-29 13:37:36 -07003406 // ref_fb_idx[0], ..., ref_fb_idx[2], ..., ref_fb_idx[ALTREF_FRAME-1]
Yaowu Xuc27fc142016-08-22 16:08:15 -07003407 // | | |
3408 // v v v
3409 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3410 //
3411 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3412 // have the other 2 LAST reference frames shifted as follows:
3413 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3414 // , and then have LAST_FRAME refreshed by the newly coded frame.
3415 //
3416 // To fulfill it, the decoder will be notified to execute following 2 steps:
3417 //
3418 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3419 // to point to the newly coded frame, i.e.
3420 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3421 //
3422 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3423 // original virtual index of LAST3_FRAME and have the other mappings
3424 // shifted as follows:
3425 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3426 // | | |
3427 // v v v
Zoe Liu5989a722018-03-29 13:37:36 -07003428 // ref_fb_idx[2], ref_fb_idx[0], ref_fb_idx[1]
Yaowu Xuc27fc142016-08-22 16:08:15 -07003429 int ref_frame;
Zoe Liu5fca7242016-10-10 17:18:57 -07003430
Tarek AMARAc9813852018-03-05 18:40:18 -05003431 if (cm->frame_type == KEY_FRAME || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003432 for (ref_frame = 0; ref_frame < LAST_REF_FRAMES; ++ref_frame) {
3433 ref_cnt_fb(pool->frame_bufs,
Zoe Liu5989a722018-03-29 13:37:36 -07003434 &cm->ref_frame_map[cpi->ref_fb_idx[ref_frame]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003435 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003436 }
3437 } else {
3438 int tmp;
3439
3440 ref_cnt_fb(pool->frame_bufs,
Zoe Liu5989a722018-03-29 13:37:36 -07003441 &cm->ref_frame_map[cpi->ref_fb_idx[LAST_REF_FRAMES - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003442 cm->new_fb_idx);
3443
Zoe Liu5989a722018-03-29 13:37:36 -07003444 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003445
3446 shift_last_ref_frames(cpi);
Zoe Liu5989a722018-03-29 13:37:36 -07003447 cpi->ref_fb_idx[0] = tmp;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003448
Zoe Liuf0e46692016-10-12 12:31:43 -07003449 assert(cm->show_existing_frame == 0);
Zoe Liuf0e46692016-10-12 12:31:43 -07003450 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3451 cpi->interp_filter_selected[0],
3452 sizeof(cpi->interp_filter_selected[0]));
Zoe Liuaff92d52017-07-11 21:35:08 -07003453
3454 if (cpi->rc.is_last_bipred_frame) {
3455 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3456 // LAST3_FRAME by updating the virtual indices.
3457 //
3458 // NOTE: The source frame for BWDREF does not have a holding position as
3459 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3460 // virtual index reshuffling for BWDREF, the encoder always
3461 // specifies a LAST_BIPRED right before BWDREF and completes the
3462 // reshuffling job accordingly.
Zoe Liu5989a722018-03-29 13:37:36 -07003463 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Zoe Liuaff92d52017-07-11 21:35:08 -07003464
3465 shift_last_ref_frames(cpi);
Zoe Liu5989a722018-03-29 13:37:36 -07003466 cpi->ref_fb_idx[0] = cpi->ref_fb_idx[BWDREF_FRAME - 1];
3467 cpi->ref_fb_idx[BWDREF_FRAME - 1] = tmp;
Zoe Liuaff92d52017-07-11 21:35:08 -07003468
3469 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3470 cpi->interp_filter_selected[BWDREF_FRAME],
3471 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
3472 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003473 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003474 }
3475
3476#if DUMP_REF_FRAME_IMAGES == 1
3477 // Dump out all reference frame images.
3478 dump_ref_frame_images(cpi);
3479#endif // DUMP_REF_FRAME_IMAGES
3480}
3481
Yaowu Xuf883b422016-08-30 14:01:10 -07003482static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003483 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003484 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003485 ensure_mv_buffer(new_fb_ptr, cm);
3486 new_fb_ptr->width = cm->width;
3487 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488}
3489
Cheng Chen46f30c72017-09-07 11:13:33 -07003490static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003491 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003492 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003493 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003494 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003495 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3496 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003497 };
3498
3499 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003500 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003501 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3502 BufferPool *const pool = cm->buffer_pool;
3503 const YV12_BUFFER_CONFIG *const ref =
3504 get_ref_frame_buffer(cpi, ref_frame);
3505
3506 if (ref == NULL) {
3507 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3508 continue;
3509 }
3510
Yaowu Xuc27fc142016-08-22 16:08:15 -07003511 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3512 RefCntBuffer *new_fb_ptr = NULL;
3513 int force_scaling = 0;
3514 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3515 if (new_fb == INVALID_IDX) {
3516 new_fb = get_free_fb(cm);
3517 force_scaling = 1;
3518 }
3519 if (new_fb == INVALID_IDX) return;
3520 new_fb_ptr = &pool->frame_bufs[new_fb];
3521 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3522 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003523 if (aom_realloc_frame_buffer(
3524 &new_fb_ptr->buf, cm->width, cm->height, cm->subsampling_x,
3525 cm->subsampling_y, cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3526 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003527 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003528 "Failed to allocate frame buffer");
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003529 av1_resize_and_extend_frame(ref, &new_fb_ptr->buf, (int)cm->bit_depth,
3530 num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003531 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3532 alloc_frame_mvs(cm, new_fb);
3533 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003534 } else {
3535 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3536 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3537 buf->buf.y_crop_width = ref->y_crop_width;
3538 buf->buf.y_crop_height = ref->y_crop_height;
3539 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3540 ++buf->ref_count;
3541 }
3542 } else {
3543 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3544 }
3545 }
3546}
3547
Yaowu Xuf883b422016-08-30 14:01:10 -07003548static void release_scaled_references(AV1_COMP *cpi) {
3549 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003550 int i;
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003551 // TODO(isbs): only refresh the necessary frames, rather than all of them
Zoe Liu27deb382018-03-27 15:13:56 -07003552 for (i = 0; i < REF_FRAMES; ++i) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003553 const int idx = cpi->scaled_ref_idx[i];
3554 RefCntBuffer *const buf =
3555 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3556 if (buf != NULL) {
3557 --buf->ref_count;
3558 cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003559 }
3560 }
3561}
3562
Yaowu Xuf883b422016-08-30 14:01:10 -07003563static void set_mv_search_params(AV1_COMP *cpi) {
3564 const AV1_COMMON *const cm = &cpi->common;
3565 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003566
3567 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003568 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569
3570 if (cpi->sf.mv.auto_mv_step_size) {
3571 if (frame_is_intra_only(cm)) {
3572 // Initialize max_mv_magnitude for use in the first INTER frame
3573 // after a key/intra-only frame.
3574 cpi->max_mv_magnitude = max_mv_def;
3575 } else {
3576 if (cm->show_frame) {
3577 // Allow mv_steps to correspond to twice the max mv magnitude found
3578 // in the previous frame, capped by the default max_mv_magnitude based
3579 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003580 cpi->mv_step_param = av1_init_search_range(
3581 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003582 }
3583 cpi->max_mv_magnitude = 0;
3584 }
3585 }
3586}
3587
Yaowu Xuf883b422016-08-30 14:01:10 -07003588static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003589 int i;
3590 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003591 cpi->common.global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003592 }
3593 cpi->global_motion_search_done = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003594 av1_set_speed_features_framesize_independent(cpi);
3595 av1_set_rd_speed_thresholds(cpi);
3596 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003597 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Yue Chen5380cb52018-02-23 15:33:21 -08003598 cpi->common.switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003599}
3600
Yaowu Xuf883b422016-08-30 14:01:10 -07003601static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003602 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003603 AV1_COMMON *const cm = &cpi->common;
3604 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003605
3606 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003607 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003608
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003609 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003610 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3611 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003612
James Zern01a9d702017-08-25 19:09:33 +00003613 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003614 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003615 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003616 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003617
3618 // Configure experimental use of segmentation for enhanced coding of
3619 // static regions if indicated.
3620 // Only allowed in the second pass of a two pass encode, as it requires
3621 // lagged coding, and if the relevant speed feature flag is set.
3622 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3623 configure_static_seg_features(cpi);
3624}
3625
Yaowu Xuf883b422016-08-30 14:01:10 -07003626static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003627 int y_stride = cpi->scaled_source.y_stride;
3628
3629 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003630 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003631 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003632 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003633 }
3634}
3635
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003636#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003637static void set_restoration_unit_size(int width, int height, int sx, int sy,
3638 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003639 (void)width;
3640 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003641 (void)sx;
3642 (void)sy;
3643#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3644 int s = AOMMIN(sx, sy);
3645#else
3646 int s = 0;
3647#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3648
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003649 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003650 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003651 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003652 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003653 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3654 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003655}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003656
Cheng Chen46f30c72017-09-07 11:13:33 -07003657static void init_ref_frame_bufs(AV1_COMMON *cm) {
3658 int i;
3659 BufferPool *const pool = cm->buffer_pool;
3660 cm->new_fb_idx = INVALID_IDX;
3661 for (i = 0; i < REF_FRAMES; ++i) {
3662 cm->ref_frame_map[i] = INVALID_IDX;
3663 pool->frame_bufs[i].ref_count = 0;
3664 }
RogerZhou86902d02018-02-28 15:29:16 -08003665 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003666 for (i = 0; i < FRAME_BUFFERS; ++i) {
3667 av1_hash_table_init(&pool->frame_bufs[i].hash_table);
3668 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003669 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003670}
3671
Yaowu Xud3e7c682017-12-21 14:08:25 -08003672static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003673 int subsampling_x, int subsampling_y) {
3674 AV1_COMMON *const cm = &cpi->common;
3675
Yaowu Xud3e7c682017-12-21 14:08:25 -08003676 if (!cpi->initial_width || cm->use_highbitdepth != use_highbitdepth ||
Cheng Chen46f30c72017-09-07 11:13:33 -07003677 cm->subsampling_x != subsampling_x ||
3678 cm->subsampling_y != subsampling_y) {
3679 cm->subsampling_x = subsampling_x;
3680 cm->subsampling_y = subsampling_y;
Cheng Chen46f30c72017-09-07 11:13:33 -07003681 cm->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003682
3683 alloc_raw_frame_buffers(cpi);
3684 init_ref_frame_bufs(cm);
3685 alloc_util_frame_buffers(cpi);
3686
3687 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3688
3689 cpi->initial_width = cm->width;
3690 cpi->initial_height = cm->height;
3691 cpi->initial_mbs = cm->MBs;
3692 }
3693}
3694
3695// Returns 1 if the assigned width or height was <= 0.
3696static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3697 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003698 const int num_planes = av1_num_planes(cm);
Cheng Chen46f30c72017-09-07 11:13:33 -07003699 check_initial_width(cpi, cm->use_highbitdepth, cm->subsampling_x,
3700 cm->subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003701
3702 if (width <= 0 || height <= 0) return 1;
3703
3704 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003705 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003706
3707 if (cpi->initial_width && cpi->initial_height &&
3708 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3709 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003710 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003711 alloc_compressor_data(cpi);
3712 realloc_segmentation_maps(cpi);
3713 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003714 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003715 update_frame_size(cpi);
3716
3717 return 0;
3718}
3719
Fergus Simpsonbc189932017-05-16 17:02:39 -07003720static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003721 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003722 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003723 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003724 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003725
Fergus Simpsonbc189932017-05-16 17:02:39 -07003726 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003727 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003728 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003729 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003730 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
3731 cm->all_lossless = cm->coded_lossless && av1_superres_unscaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003732 }
3733
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003734 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003735 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003736 }
3737
3738 alloc_frame_mvs(cm, cm->new_fb_idx);
3739
3740 // Reset the frame pointers to the current frame size.
Yaowu Xuf883b422016-08-30 14:01:10 -07003741 if (aom_realloc_frame_buffer(get_frame_new_buffer(cm), cm->width, cm->height,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742 cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -08003743 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3744 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003745 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003746 "Failed to allocate frame buffer");
3747
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003748 const int frame_width = cm->superres_upscaled_width;
3749 const int frame_height = cm->superres_upscaled_height;
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003750 set_restoration_unit_size(frame_width, frame_height, cm->subsampling_x,
3751 cm->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003752 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003753 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003754
3755 av1_alloc_restoration_buffers(cm);
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003756 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003757 init_motion_estimation(cpi);
3758
3759 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3760 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3761 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3762
3763 ref_buf->idx = buf_idx;
3764
3765 if (buf_idx != INVALID_IDX) {
3766 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3767 ref_buf->buf = buf;
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003768 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3769 buf->y_crop_height, cm->width,
3770 cm->height);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003771 if (av1_is_scaled(&ref_buf->sf))
3772 aom_extend_frame_borders(buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003773 } else {
3774 ref_buf->buf = NULL;
3775 }
3776 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003777
Hui Su5ebd8702018-01-08 18:09:20 -08003778 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003779 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003780
3781 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3782}
3783
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003784static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3785 // Choose an arbitrary random number
3786 static unsigned int seed = 56789;
3787 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003788 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3789 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003790
3791 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003792 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003793 case RESIZE_FIXED:
3794 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003795 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003796 else
Urvang Joshide71d142017-10-05 12:12:15 -07003797 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003798 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003799 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003800 default: assert(0);
3801 }
Urvang Joshide71d142017-10-05 12:12:15 -07003802 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003803}
3804
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003805static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
3806 // Choose an arbitrary random number
3807 static unsigned int seed = 34567;
3808 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003809 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3810 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07003811
3812 // Make sure that superres mode of the frame is consistent with the
3813 // sequence-level flag.
3814 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
3815 cpi->common.seq_params.enable_superres));
3816 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
3817 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003818
3819 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003820 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003821 case SUPERRES_FIXED:
3822 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003823 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003824 else
Urvang Joshide71d142017-10-05 12:12:15 -07003825 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003826 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003827 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003828 case SUPERRES_QTHRESH: {
3829 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3830 const RATE_FACTOR_LEVEL rf_level = gf_group->rf_level[gf_group->index];
3831 const double rate_factor_delta = rate_factor_deltas[rf_level];
Urvang Joshi2c92b072018-03-19 17:23:31 -07003832 const int qthresh = (rate_factor_delta <= 1.0)
3833 ? oxcf->superres_qthresh
3834 : oxcf->superres_kf_qthresh;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003835 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi2c92b072018-03-19 17:23:31 -07003836 int bottom_index, top_index;
3837 const int q = av1_rc_pick_q_and_bounds(
3838 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003839 if (q < qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07003840 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003841 } else {
Urvang Joshi28983f72017-10-25 14:41:06 -07003842 const uint8_t min_denom = SCALE_NUMERATOR + 1;
3843 const uint8_t denom_step = (MAXQ - qthresh + 1) >> 3;
Imdad Sardharwallaf6154fd2018-03-02 16:53:41 +00003844
3845 if (q == qthresh) {
3846 new_denom = min_denom;
3847 } else if (denom_step == 0) {
3848 new_denom = SCALE_NUMERATOR << 1;
3849 } else {
3850 const uint8_t additional_denom = (q - qthresh) / denom_step;
3851 new_denom =
3852 AOMMIN(min_denom + additional_denom, SCALE_NUMERATOR << 1);
3853 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003854 }
3855 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003856 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003857 default: assert(0);
3858 }
Urvang Joshide71d142017-10-05 12:12:15 -07003859 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003860}
3861
Urvang Joshide71d142017-10-05 12:12:15 -07003862static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
3863 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
3864}
3865
3866static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08003867 // Only need to check the width, as scaling is horizontal only.
3868 (void)oheight;
3869 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07003870}
3871
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003872static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003873 SUPERRES_MODE superres_mode, int owidth,
3874 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07003875 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003876 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07003877 }
3878
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003879 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07003880 int resize_denom =
3881 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
3882 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
3883
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003884 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003885 // Alter superres scale as needed to enforce conformity.
3886 rsz->superres_denom =
3887 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
3888 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3889 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003890 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003891 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003892 // Alter resize scale as needed to enforce conformity.
3893 resize_denom =
3894 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003895 rsz->resize_width = owidth;
3896 rsz->resize_height = oheight;
3897 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003898 resize_denom);
3899 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3900 if (resize_denom > SCALE_NUMERATOR) {
3901 --resize_denom;
3902 rsz->resize_width = owidth;
3903 rsz->resize_height = oheight;
3904 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
3905 resize_denom);
3906 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003907 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003908 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003909 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003910 do {
Urvang Joshide71d142017-10-05 12:12:15 -07003911 if (resize_denom > rsz->superres_denom)
3912 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003913 else
Urvang Joshide71d142017-10-05 12:12:15 -07003914 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003915 rsz->resize_width = owidth;
3916 rsz->resize_height = oheight;
3917 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003918 resize_denom);
3919 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
3920 (resize_denom > SCALE_NUMERATOR ||
3921 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08003922 } else { // We are allowed to alter neither resize scale nor superres
3923 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003924 return 0;
3925 }
Urvang Joshide71d142017-10-05 12:12:15 -07003926 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003927}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003928
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003929// Calculates resize and superres params for next frame
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003930size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
3931 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08003932 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshide71d142017-10-05 12:12:15 -07003933 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003934 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003935 if (cpi->resize_pending_width && cpi->resize_pending_height) {
3936 rsz.resize_width = cpi->resize_pending_width;
3937 rsz.resize_height = cpi->resize_pending_height;
3938 cpi->resize_pending_width = cpi->resize_pending_height = 0;
3939 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07003940 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003941 rsz.resize_width = cpi->oxcf.width;
3942 rsz.resize_height = cpi->oxcf.height;
3943 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003944 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003945 }
Urvang Joshide71d142017-10-05 12:12:15 -07003946 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003947 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
3948 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003949 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003950 return rsz;
3951}
3952
3953static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003954 int encode_width = rsz->resize_width;
3955 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003956
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003957 AV1_COMMON *cm = &cpi->common;
3958 cm->superres_upscaled_width = encode_width;
3959 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07003960 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003961 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
3962 rsz->superres_denom);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003963 set_frame_size(cpi, encode_width, encode_height);
3964}
3965
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003966static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003967 size_params_type rsz = av1_calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003968 setup_frame_size_from_params(cpi, &rsz);
3969}
3970
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003971static void superres_post_encode(AV1_COMP *cpi) {
3972 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003973 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003974
3975 if (av1_superres_unscaled(cm)) return;
3976
Urvang Joshid6b5d512018-03-20 13:34:38 -07003977 assert(cpi->oxcf.enable_superres);
3978 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07003979 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07003980
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003981 av1_superres_upscale(cm, NULL);
3982
3983 // If regular resizing is occurring the source will need to be downscaled to
3984 // match the upscaled superres resolution. Otherwise the original source is
3985 // used.
3986 if (av1_resize_unscaled(cm)) {
3987 cpi->source = cpi->unscaled_source;
3988 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
3989 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07003990 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
3991 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08003992 // Do downscale. cm->(width|height) has been updated by
3993 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003994 if (aom_realloc_frame_buffer(
3995 &cpi->scaled_source, cm->superres_upscaled_width,
3996 cm->superres_upscaled_height, cm->subsampling_x, cm->subsampling_y,
Yaowu Xud3e7c682017-12-21 14:08:25 -08003997 cm->use_highbitdepth, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3998 NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003999 aom_internal_error(
4000 &cm->error, AOM_CODEC_MEM_ERROR,
4001 "Failed to reallocate scaled source buffer for superres");
4002 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4003 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004004 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004005 (int)cm->bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004006 cpi->source = &cpi->scaled_source;
4007 }
4008}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004009
4010static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004011 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004012 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004013
Urvang Joshic8b52d52018-03-23 13:16:51 -07004014 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4015 cm->coded_lossless && cm->all_lossless));
4016
4017 const int no_loopfilter = cm->coded_lossless || cm->large_scale_tile;
4018 const int no_cdef =
Debargha Mukherjee98a311c2018-03-25 16:33:11 -07004019 !cm->seq_params.enable_cdef || cm->coded_lossless || cm->large_scale_tile;
4020 const int no_restoration = !cm->seq_params.enable_restoration ||
4021 cm->all_lossless || cm->large_scale_tile;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004022
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004023 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004024
4025 if (no_loopfilter) {
Cheng Chen179479f2017-08-04 10:56:39 -07004026 lf->filter_level[0] = 0;
4027 lf->filter_level[1] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004028 } else {
4029 struct aom_usec_timer timer;
4030
4031 aom_clear_system_state();
4032
4033 aom_usec_timer_start(&timer);
4034
4035 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4036
4037 aom_usec_timer_mark(&timer);
4038 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4039 }
4040
Debargha Mukherjee2382b142018-02-26 14:31:32 -08004041 if (lf->filter_level[0] || lf->filter_level[1]) {
Cheng Chen179479f2017-08-04 10:56:39 -07004042 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level[0],
4043 lf->filter_level[1], 0, 0);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004044 if (num_planes > 1) {
4045 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_u,
4046 lf->filter_level_u, 1, 0);
4047 av1_loop_filter_frame(cm->frame_to_show, cm, xd, lf->filter_level_v,
4048 lf->filter_level_v, 2, 0);
4049 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004050 }
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004051
Yaowu Xu35ee2342017-11-08 11:50:46 -08004052 if (!no_restoration)
4053 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004054
Yaowu Xu35ee2342017-11-08 11:50:46 -08004055 if (no_cdef) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004056 cm->cdef_bits = 0;
4057 cm->cdef_strengths[0] = 0;
4058 cm->nb_cdef_strengths = 1;
Yunqing Wangdad63d42017-12-08 12:45:07 -08004059 cm->cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004060 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004061 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004062 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004063 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004064
4065 // Apply the filter
4066 av1_cdef_frame(cm->frame_to_show, cm, xd);
4067 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004068
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004069 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004070
Yaowu Xu35ee2342017-11-08 11:50:46 -08004071 if (no_restoration) {
4072 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4073 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4074 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4075 } else {
4076 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 1);
4077 av1_pick_filter_restoration(cpi->source, cpi);
4078 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4079 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4080 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Rupert Swarbrick70c399e2017-12-12 09:47:49 +00004081 av1_loop_restoration_filter_frame(cm->frame_to_show, cm);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004082 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004083 }
Fergus Simpsonbc189932017-05-16 17:02:39 -07004084}
4085
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004086static int encode_without_recode_loop(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004087 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004088 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004089
Yaowu Xuf883b422016-08-30 14:01:10 -07004090 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004091
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004092 set_size_independent_vars(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004093
Fergus Simpsonbc189932017-05-16 17:02:39 -07004094 setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004095
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004096 assert(cm->width == cpi->scaled_source.y_crop_width);
4097 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004098
Yaowu Xuc27fc142016-08-22 16:08:15 -07004099 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4100
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004101 cpi->source =
4102 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4103 if (cpi->unscaled_last_source != NULL)
4104 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4105 &cpi->scaled_last_source);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004106 cpi->source->buf_8bit_valid = 0;
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004107 if (frame_is_intra_only(cm) == 0) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004108 scale_references(cpi);
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004109 }
4110
Yaowu Xuf883b422016-08-30 14:01:10 -07004111 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004112 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004113 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004114
Yaowu Xuc27fc142016-08-22 16:08:15 -07004115 // Variance adaptive and in frame q adjustment experiments are mutually
4116 // exclusive.
4117 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004118 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004119 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004120 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004121 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004122 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004123 }
4124 apply_active_map(cpi);
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004125 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004126 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004127 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004128 }
David Barkercab37552018-03-21 11:56:24 +00004129 } else {
4130 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004131 }
David Barkercab37552018-03-21 11:56:24 +00004132 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004133
4134 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004135 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004136
4137 // Update some stats from cyclic refresh, and check if we should not update
4138 // golden reference, for 1 pass CBR.
4139 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004140 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4141 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004142
4143 // Update the skip mb flag probabilities based on the distribution
4144 // seen in the last encoder iteration.
4145 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004146 aom_clear_system_state();
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004147 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004148}
4149
Tom Finegane4099e32018-01-23 12:01:51 -08004150static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004151 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004152 RATE_CONTROL *const rc = &cpi->rc;
4153 int bottom_index, top_index;
4154 int loop_count = 0;
4155 int loop_at_this_size = 0;
4156 int loop = 0;
4157 int overshoot_seen = 0;
4158 int undershoot_seen = 0;
4159 int frame_over_shoot_limit;
4160 int frame_under_shoot_limit;
4161 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004162
4163 set_size_independent_vars(cpi);
4164
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004165 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004166
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004167 aom_clear_system_state();
4168 setup_frame_size(cpi);
4169 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4170
Yaowu Xuc27fc142016-08-22 16:08:15 -07004171 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004172 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004173
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004174 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004175 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4176 set_mv_search_params(cpi);
4177
4178 // Reset the loop state for new frame size.
4179 overshoot_seen = 0;
4180 undershoot_seen = 0;
4181
Yaowu Xuc27fc142016-08-22 16:08:15 -07004182 q_low = bottom_index;
4183 q_high = top_index;
4184
4185 loop_at_this_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004186
Urvang Joshi2a74cf22017-12-18 17:21:41 -08004187 // Decide frame size bounds first time through.
Yaowu Xuf883b422016-08-30 14:01:10 -07004188 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4189 &frame_under_shoot_limit,
4190 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004191 }
4192
Urvang Joshif1fa6862018-01-08 16:39:33 -08004193 // if frame was scaled calculate global_motion_search again if already
4194 // done
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004195 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4196 if (cpi->source->y_crop_width != cm->width ||
4197 cpi->source->y_crop_height != cm->height)
4198 cpi->global_motion_search_done = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004199 cpi->source =
4200 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004201 if (cpi->unscaled_last_source != NULL)
4202 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4203 &cpi->scaled_last_source);
4204
Yaowu Xuc27fc142016-08-22 16:08:15 -07004205 if (frame_is_intra_only(cm) == 0) {
4206 if (loop_count > 0) {
4207 release_scaled_references(cpi);
4208 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004209 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004210 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004211 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004212
4213 if (loop_count == 0) setup_frame(cpi);
4214
Yaowu Xuc27fc142016-08-22 16:08:15 -07004215 // Base q-index may have changed, so we need to assign proper default coef
4216 // probs before every iteration.
David Barkercc615a82018-03-19 14:38:51 +00004217 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
4218 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004219 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004220 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004221 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004222
Yaowu Xuc27fc142016-08-22 16:08:15 -07004223 // Variance adaptive and in frame q adjustment experiments are mutually
4224 // exclusive.
4225 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004226 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004227 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004228 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004229 }
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004230 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004231 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004232 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004233 }
David Barkercab37552018-03-21 11:56:24 +00004234 } else {
4235 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004236 }
David Barkercab37552018-03-21 11:56:24 +00004237 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004238
4239 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004240 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004241 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004242
4243 // Update the skip mb flag probabilities based on the distribution
4244 // seen in the last encoder iteration.
4245 // update_base_skip_probs(cpi);
4246
Yaowu Xuf883b422016-08-30 14:01:10 -07004247 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004248
4249 // Dummy pack of the bitstream using up to date stats to get an
4250 // accurate estimate of output frame size to determine if we need
4251 // to recode.
4252 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004253 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004254
4255 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4256 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004257
4258 rc->projected_frame_size = (int)(*size) << 3;
4259 restore_coding_context(cpi);
4260
4261 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4262 }
4263
Yaowu Xuf883b422016-08-30 14:01:10 -07004264 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004265 loop = 0;
4266 } else {
4267 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4268 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4269 int last_q = q;
4270 int64_t kf_err;
4271
4272 int64_t high_err_target = cpi->ambient_err;
4273 int64_t low_err_target = cpi->ambient_err >> 1;
4274
Yaowu Xuc27fc142016-08-22 16:08:15 -07004275 if (cm->use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004276 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004277 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004278 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004279 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004280 // Prevent possible divide by zero error below for perfect KF
4281 kf_err += !kf_err;
4282
4283 // The key frame is not good enough or we can afford
4284 // to make it better without undue risk of popping.
4285 if ((kf_err > high_err_target &&
4286 rc->projected_frame_size <= frame_over_shoot_limit) ||
4287 (kf_err > low_err_target &&
4288 rc->projected_frame_size <= frame_under_shoot_limit)) {
4289 // Lower q_high
4290 q_high = q > q_low ? q - 1 : q_low;
4291
4292 // Adjust Q
4293 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004294 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004295 } else if (kf_err < low_err_target &&
4296 rc->projected_frame_size >= frame_under_shoot_limit) {
4297 // The key frame is much better than the previous frame
4298 // Raise q_low
4299 q_low = q < q_high ? q + 1 : q_high;
4300
4301 // Adjust Q
4302 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004303 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004304 }
4305
4306 // Clamp Q to upper and lower limits:
4307 q = clamp(q, q_low, q_high);
4308
4309 loop = q != last_q;
4310 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4311 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004312 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004313 // Is the projected frame size out of range and are we allowed
4314 // to attempt to recode.
4315 int last_q = q;
4316 int retries = 0;
4317
Yaowu Xuc27fc142016-08-22 16:08:15 -07004318 // Frame size out of permitted range:
4319 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004320 // Frame is too large
4321 if (rc->projected_frame_size > rc->this_frame_target) {
4322 // Special case if the projected size is > the max allowed.
4323 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4324 q_high = rc->worst_quality;
4325
4326 // Raise Qlow as to at least the current value
4327 q_low = q < q_high ? q + 1 : q_high;
4328
4329 if (undershoot_seen || loop_at_this_size > 1) {
4330 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004331 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004332
4333 q = (q_high + q_low + 1) / 2;
4334 } else {
4335 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004336 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004337
Yaowu Xuf883b422016-08-30 14:01:10 -07004338 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004339 AOMMAX(q_high, top_index), cm->width,
4340 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004341
4342 while (q < q_low && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004343 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004344 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004345 AOMMAX(q_high, top_index), cm->width,
4346 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004347 retries++;
4348 }
4349 }
4350
4351 overshoot_seen = 1;
4352 } else {
4353 // Frame is too small
4354 q_high = q > q_low ? q - 1 : q_low;
4355
4356 if (overshoot_seen || loop_at_this_size > 1) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004357 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004358 q = (q_high + q_low) / 2;
4359 } else {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004360 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004361 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004362 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004363 // Special case reset for qlow for constrained quality.
4364 // This should only trigger where there is very substantial
4365 // undershoot on a frame and the auto cq level is above
4366 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004367 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004368 q_low = q;
4369 }
4370
4371 while (q > q_high && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004372 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004373 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004374 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004375 retries++;
4376 }
4377 }
4378
4379 undershoot_seen = 1;
4380 }
4381
4382 // Clamp Q to upper and lower limits:
4383 q = clamp(q, q_low, q_high);
4384
4385 loop = (q != last_q);
4386 } else {
4387 loop = 0;
4388 }
4389 }
4390
4391 // Special case for overlay frame.
4392 if (rc->is_src_frame_alt_ref &&
4393 rc->projected_frame_size < rc->max_frame_bandwidth)
4394 loop = 0;
4395
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004396 if (recode_loop_test_global_motion(cpi)) {
4397 loop = 1;
4398 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004399
Yaowu Xuc27fc142016-08-22 16:08:15 -07004400 if (loop) {
4401 ++loop_count;
4402 ++loop_at_this_size;
4403
4404#if CONFIG_INTERNAL_STATS
4405 ++cpi->tot_recode_hits;
4406#endif
4407 }
4408 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004409
4410 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004411}
4412
Yaowu Xuf883b422016-08-30 14:01:10 -07004413static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004414 const int *const map = cpi->common.ref_frame_map;
4415
Zoe Liu368bf162017-11-03 20:10:19 -07004416 // No.1 Priority: LAST_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004417 const int last2_is_last = map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[0]];
4418 const int last3_is_last = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[0]];
4419 const int gld_is_last =
4420 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4421 const int bwd_is_last =
4422 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4423 const int alt2_is_last =
4424 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4425 const int alt_is_last =
4426 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004427
Zoe Liu368bf162017-11-03 20:10:19 -07004428 // No.2 Priority: ALTREF_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004429 const int last2_is_alt =
4430 map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4431 const int last3_is_alt =
4432 map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4433 const int gld_is_alt = map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] ==
4434 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4435 const int bwd_is_alt = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4436 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4437 const int alt2_is_alt = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4438 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004439
Zoe Liu368bf162017-11-03 20:10:19 -07004440 // No.3 Priority: LAST2_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004441 const int last3_is_last2 = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[1]];
4442 const int gld_is_last2 =
4443 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4444 const int bwd_is_last2 =
4445 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4446 const int alt2_is_last2 =
4447 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004448
Zoe Liu368bf162017-11-03 20:10:19 -07004449 // No.4 Priority: LAST3_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004450 const int gld_is_last3 =
4451 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4452 const int bwd_is_last3 =
4453 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4454 const int alt2_is_last3 =
4455 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
Zoe Liu368bf162017-11-03 20:10:19 -07004456
4457 // No.5 Priority: GOLDEN_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004458 const int bwd_is_gld = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4459 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
4460 const int alt2_is_gld = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4461 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
Zoe Liu368bf162017-11-03 20:10:19 -07004462
4463 // No.6 Priority: BWDREF_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004464 const int alt2_is_bwd = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4465 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]];
Zoe Liu368bf162017-11-03 20:10:19 -07004466
4467 // No.7 Priority: ALTREF2_FRAME
4468
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004469 // After av1_apply_encoding_flags() is called, cpi->ref_frame_flags might be
4470 // adjusted according to external encoder flags.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08004471 int flags = cpi->ext_ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004472
Yaowu Xuf883b422016-08-30 14:01:10 -07004473 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004474
Yaowu Xuf883b422016-08-30 14:01:10 -07004475 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004476
Yaowu Xuf883b422016-08-30 14:01:10 -07004477 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004478
Zoe Liu368bf162017-11-03 20:10:19 -07004479 if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004480
Zoe Liu368bf162017-11-03 20:10:19 -07004481 if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
4482 flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004483
Zoe Liu368bf162017-11-03 20:10:19 -07004484 if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 ||
4485 bwd_is_gld) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004486 (flags & AOM_BWD_FLAG))
4487 flags &= ~AOM_BWD_FLAG;
Zoe Liue9b15e22017-07-19 15:53:01 -07004488
Zoe Liu368bf162017-11-03 20:10:19 -07004489 if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
4490 alt2_is_gld || alt2_is_bwd) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004491 (flags & AOM_ALT2_FLAG))
4492 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004493
4494 return flags;
4495}
4496
Yaowu Xuf883b422016-08-30 14:01:10 -07004497static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004498 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07004499 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07004500 // Note: The overrides are valid only for the next frame passed
4501 // to encode_frame_to_data_rate() function
Debargha Mukherjee52fb0472018-03-29 15:48:11 -07004502 if (cpi->oxcf.s_frame_mode) cpi->common.frame_type = S_FRAME;
4503
Yaowu Xuc27fc142016-08-22 16:08:15 -07004504 if (cpi->ext_refresh_frame_context_pending) {
4505 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4506 cpi->ext_refresh_frame_context_pending = 0;
4507 }
4508 if (cpi->ext_refresh_frame_flags_pending) {
4509 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4510 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4511 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004512 cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
4513 cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004514 cpi->ext_refresh_frame_flags_pending = 0;
4515 }
4516}
4517
Yaowu Xuf883b422016-08-30 14:01:10 -07004518static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07004519 InterpFilter ifilter;
Zoe Liu27deb382018-03-27 15:13:56 -07004520 int ref_total[REF_FRAMES] = { 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -07004521 MV_REFERENCE_FRAME ref;
4522 int mask = 0;
4523 int arf_idx = ALTREF_FRAME;
4524
Zoe Liue9b15e22017-07-19 15:53:01 -07004525 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
4526 cpi->refresh_alt2_ref_frame)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004527 return mask;
4528
Yaowu Xuc27fc142016-08-22 16:08:15 -07004529 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4530 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4531 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004532
4533 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
4534 if ((ref_total[LAST_FRAME] &&
4535 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004536 (ref_total[LAST2_FRAME] == 0 ||
4537 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
4538 ref_total[LAST2_FRAME]) &&
4539 (ref_total[LAST3_FRAME] == 0 ||
4540 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
4541 ref_total[LAST3_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004542 (ref_total[GOLDEN_FRAME] == 0 ||
4543 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4544 ref_total[GOLDEN_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004545 (ref_total[BWDREF_FRAME] == 0 ||
4546 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
4547 ref_total[BWDREF_FRAME]) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004548 (ref_total[ALTREF2_FRAME] == 0 ||
4549 cpi->interp_filter_selected[ALTREF2_FRAME][ifilter] * 50 <
4550 ref_total[ALTREF2_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004551 (ref_total[ALTREF_FRAME] == 0 ||
4552 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
4553 ref_total[ALTREF_FRAME]))
4554 mask |= 1 << ifilter;
4555 }
4556 return mask;
4557}
4558
4559#define DUMP_RECON_FRAMES 0
4560
4561#if DUMP_RECON_FRAMES == 1
4562// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004563static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4564 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004565 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004566
Zoe Liub4f31032017-11-03 23:48:35 -07004567 if (recon_buf == NULL) {
4568 printf("Frame %d is not ready.\n", cm->current_video_frame);
4569 return;
4570 }
4571
Zoe Liu27deb382018-03-27 15:13:56 -07004572 static const int flag_list[REF_FRAMES] = { 0,
4573 AOM_LAST_FLAG,
4574 AOM_LAST2_FLAG,
4575 AOM_LAST3_FLAG,
4576 AOM_GOLD_FLAG,
4577 AOM_BWD_FLAG,
4578 AOM_ALT2_FLAG,
4579 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004580 printf(
4581 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4582 "show_existing_frame=%d) "
4583 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
4584 cm->current_video_frame, cm->frame_offset, cm->show_frame,
4585 cm->show_existing_frame);
4586 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4587 const int buf_idx = cm->frame_refs[ref_frame - LAST_FRAME].idx;
4588 const int ref_offset =
4589 (buf_idx >= 0)
4590 ? (int)cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset
4591 : -1;
Zoe Liuf452fdf2017-11-02 23:08:12 -07004592 printf(
4593 " %d(%c-%d-%4.2f)", ref_offset,
4594 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N',
4595 (buf_idx >= 0) ? (int)cpi->frame_rf_level[buf_idx] : -1,
4596 (buf_idx >= 0) ? rate_factor_deltas[cpi->frame_rf_level[buf_idx]] : -1);
Zoe Liub4f31032017-11-03 23:48:35 -07004597 }
4598 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004599
4600 if (!cm->show_frame) {
4601 printf("Frame %d is a no show frame, so no image dump.\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004602 cm->current_video_frame);
4603 return;
4604 }
4605
Zoe Liub4f31032017-11-03 23:48:35 -07004606 int h;
4607 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4608 FILE *f_recon = NULL;
4609
Yaowu Xuc27fc142016-08-22 16:08:15 -07004610 if (cm->current_video_frame == 0) {
4611 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4612 printf("Unable to open file %s to write.\n", file_name);
4613 return;
4614 }
4615 } else {
4616 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4617 printf("Unable to open file %s to append.\n", file_name);
4618 return;
4619 }
4620 }
4621 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004622 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4623 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
4624 "refresh_alt_ref_frame=%d, rf_level=%d, "
4625 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004626 cm->current_video_frame, cpi->twopass.gf_group.index,
4627 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liuf40a9572017-10-13 12:37:19 -07004628 cm->frame_offset, cm->show_frame, cm->show_existing_frame,
4629 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
4630 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4631 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004632#if 0
4633 int ref_frame;
4634 printf("get_ref_frame_map_idx: [");
4635 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
4636 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
4637 printf(" ]\n");
4638 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
4639 printf("cm->ref_frame_map = [");
4640 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4641 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
4642 }
4643 printf(" ]\n");
4644#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004645
4646 // --- Y ---
4647 for (h = 0; h < cm->height; ++h) {
4648 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4649 f_recon);
4650 }
4651 // --- U ---
4652 for (h = 0; h < (cm->height >> 1); ++h) {
4653 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4654 f_recon);
4655 }
4656 // --- V ---
4657 for (h = 0; h < (cm->height >> 1); ++h) {
4658 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4659 f_recon);
4660 }
4661
4662 fclose(f_recon);
4663}
4664#endif // DUMP_RECON_FRAMES
4665
Tom Finegane4099e32018-01-23 12:01:51 -08004666static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size, uint8_t *dest,
4667 int skip_adapt,
4668 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004669 AV1_COMMON *const cm = &cpi->common;
4670 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004671 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01004672
Yaowu Xuc27fc142016-08-22 16:08:15 -07004673 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004674 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004675
Fangwen Fu8d164de2016-12-14 13:40:54 -08004676 // frame type has been decided outside of this function call
Zoe Liud78ce2d2018-02-24 05:50:57 -08004677 cm->cur_frame->intra_only = frame_is_intra_only(cm);
Zoe Liu2723a9d2018-02-22 20:17:00 -08004678 cm->cur_frame->frame_type = cm->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07004679
4680 // S_FRAMEs are always error resilient
4681 cm->error_resilient_mode = oxcf->error_resilient_mode || frame_is_sframe(cm);
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07004682 cm->allow_ref_frame_mvs =
4683 cpi->oxcf.allow_ref_frame_mvs && frame_might_allow_ref_frame_mvs(cm);
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07004684 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07004685 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004686
Jingning Hand8a15a62017-10-30 10:53:42 -07004687 // Reset the frame packet stamp index.
4688 if (cm->frame_type == KEY_FRAME) cm->current_video_frame = 0;
4689
Yaowu Xuc27fc142016-08-22 16:08:15 -07004690 // NOTE:
4691 // (1) Move the setup of the ref_frame_flags upfront as it would be
4692 // determined by the current frame properties;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004693 // (2) The setup of the ref_frame_flags applies to both
4694 // show_existing_frame's
Yaowu Xuc27fc142016-08-22 16:08:15 -07004695 // and the other cases.
4696 if (cm->current_video_frame > 0)
4697 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4698
4699 if (cm->show_existing_frame) {
4700 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
4701 // BWDREF_FRAME in the reference frame buffer.
4702 cm->frame_type = INTER_FRAME;
4703 cm->show_frame = 1;
4704 cpi->frame_flags = *frame_flags;
4705
4706 // In the case of show_existing frame, we will not send fresh flag
4707 // to decoder. Any change in the reference frame buffer can be done by
4708 // switching the virtual indices.
4709
4710 cpi->refresh_last_frame = 0;
4711 cpi->refresh_golden_frame = 0;
4712 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07004713 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004714 cpi->refresh_alt_ref_frame = 0;
4715
4716 cpi->rc.is_bwd_ref_frame = 0;
4717 cpi->rc.is_last_bipred_frame = 0;
4718 cpi->rc.is_bipred_frame = 0;
4719
Jingning Han8f661602017-08-19 08:16:50 -07004720 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07004721
Yaowu Xuc27fc142016-08-22 16:08:15 -07004722 // Build the bitstream
Tom Finegane4099e32018-01-23 12:01:51 -08004723 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4724 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004725
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004726 cpi->seq_params_locked = 1;
4727
Yaowu Xuc27fc142016-08-22 16:08:15 -07004728 // Set up frame to show to get ready for stats collection.
4729 cm->frame_to_show = get_frame_new_buffer(cm);
4730
Zoe Liub4f31032017-11-03 23:48:35 -07004731 // Update current frame offset.
4732 cm->frame_offset =
4733 cm->buffer_pool->frame_bufs[cm->new_fb_idx].cur_frame_offset;
Zoe Liub4f31032017-11-03 23:48:35 -07004734
Yaowu Xuc27fc142016-08-22 16:08:15 -07004735#if DUMP_RECON_FRAMES == 1
4736 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4737 dump_filtered_recon_frames(cpi);
4738#endif // DUMP_RECON_FRAMES
4739
4740 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07004741 // NOTE:
4742 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
4743 // update has been done previously when handling the LAST_BIPRED_FRAME
4744 // right before BWDREF_FRAME (in the display order);
4745 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
Urvang Joshif1fa6862018-01-08 16:39:33 -08004746 // update will be done when the following is called, which will
4747 // exchange
Zoe Liue9b15e22017-07-19 15:53:01 -07004748 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
Urvang Joshif1fa6862018-01-08 16:39:33 -08004749 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2,
4750 // and
Zoe Liue9b15e22017-07-19 15:53:01 -07004751 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Cheng Chen46f30c72017-09-07 11:13:33 -07004752 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004753
4754 // Update frame flags
4755 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4756 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4757 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4758
4759 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4760
4761 // Update the frame type
4762 cm->last_frame_type = cm->frame_type;
4763
Yaowu Xuc27fc142016-08-22 16:08:15 -07004764 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4765 // to do post-encoding update accordingly.
4766 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004767 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004768 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004769 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004770
Yaowu Xuc27fc142016-08-22 16:08:15 -07004771 ++cm->current_video_frame;
4772
Tom Finegane4099e32018-01-23 12:01:51 -08004773 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004774 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004775
4776 // Set default state for segment based loop filter update flags.
4777 cm->lf.mode_ref_delta_update = 0;
4778
4779 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4780 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4781
4782 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05004783 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004784 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004785 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004786
4787 // If segmentation is enabled force a map update for key frames.
4788 if (seg->enabled) {
4789 seg->update_map = 1;
4790 seg->update_data = 1;
4791 }
4792
4793 // The alternate reference frame cannot be active for a key frame.
4794 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004795 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00004796 if (cpi->oxcf.mtu == 0) {
4797 cm->num_tg = cpi->oxcf.num_tile_groups;
4798 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08004799 // Use a default value for the purposes of weighting costs in probability
4800 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00004801 cm->num_tg = DEFAULT_MAX_NUM_TG;
4802 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004803
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004804 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
4805 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08004806 if (cm->large_scale_tile) cm->seq_params.frame_id_numbers_present_flag = 0;
Yunqing Wangd8cd55f2017-02-27 12:16:00 -08004807
Yaowu Xuc27fc142016-08-22 16:08:15 -07004808 // For 1 pass CBR, check if we are dropping this frame.
4809 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004810 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004811 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004812 if (av1_rc_drop_frame(cpi)) {
4813 av1_rc_postencode_update_drop_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004814 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004815 }
4816 }
4817
Yaowu Xuf883b422016-08-30 14:01:10 -07004818 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004819
4820#if CONFIG_INTERNAL_STATS
4821 memset(cpi->mode_chosen_counts, 0,
4822 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4823#endif
4824
David Barker5e70a112017-10-03 14:28:17 +01004825 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004826 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08004827 * wraparound) */
Sebastien Alaiwand418f682017-10-19 15:06:52 +02004828 const int frame_id_length = FRAME_ID_LENGTH;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004829 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00004830 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08004831 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07004832 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
4833 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
4834 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004835 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004836 lsb = cpi->source->y_buffer[0] & 0xff;
4837 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004838 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004839 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05004840
4841 // S_frame is meant for stitching different streams of different
4842 // resolutions together, so current_frame_id must be the
4843 // same across different streams of the same content current_frame_id
4844 // should be the same and not random. 0x37 is a chosen number as start
4845 // point
4846 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004847 } else {
4848 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01004849 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
4850 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004851 }
4852 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004853
Hui Su483a8452018-02-26 12:28:48 -08004854 switch (cpi->oxcf.cdf_update_mode) {
4855 case 0: // No CDF update for any frames(4~6% compression loss).
4856 cm->disable_cdf_update = 1;
4857 break;
4858 case 1: // Enable CDF update for all frames.
4859 cm->disable_cdf_update = 0;
4860 break;
4861 case 2:
4862 // Strategically determine at which frames to do CDF update.
4863 // Currently only enable CDF update for all-intra and no-show frames(1.5%
4864 // compression loss).
4865 // TODO(huisu@google.com): design schemes for various trade-offs between
4866 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08004867 cm->disable_cdf_update =
4868 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08004869 break;
4870 }
Hui Su483a8452018-02-26 12:28:48 -08004871
Yaowu Xuc27fc142016-08-22 16:08:15 -07004872 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004873 if (encode_without_recode_loop(cpi) != AOM_CODEC_OK) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004874 } else {
Tom Finegane4099e32018-01-23 12:01:51 -08004875 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
4876 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004877 }
4878
Yi Luo10e23002017-07-31 11:54:43 -07004879 cm->last_tile_cols = cm->tile_cols;
4880 cm->last_tile_rows = cm->tile_rows;
4881
Yaowu Xuc27fc142016-08-22 16:08:15 -07004882#ifdef OUTPUT_YUV_SKINMAP
4883 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004884 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004885 }
4886#endif // OUTPUT_YUV_SKINMAP
4887
4888 // Special case code to reduce pulsing when key frames are forced at a
4889 // fixed interval. Note the reconstruction error if it is the frame before
4890 // the force key frame
4891 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004892 if (cm->use_highbitdepth) {
4893 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07004894 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004895 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004896 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004897 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004898 }
4899
Tarek AMARAc9813852018-03-05 18:40:18 -05004900 // If the encoder forced a KEY_FRAME decision or if frame is an S_FRAME
4901 if (cm->frame_type == KEY_FRAME || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004902 cpi->refresh_last_frame = 1;
4903 }
4904
4905 cm->frame_to_show = get_frame_new_buffer(cm);
Andrey Norkin9e694632017-12-21 18:50:57 -08004906 cm->frame_to_show->color_primaries = cm->color_primaries;
4907 cm->frame_to_show->transfer_characteristics = cm->transfer_characteristics;
4908 cm->frame_to_show->matrix_coefficients = cm->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08004909 cm->frame_to_show->monochrome = cm->seq_params.monochrome;
anorkin76fb1262017-03-22 15:12:12 -07004910 cm->frame_to_show->chroma_sample_position = cm->chroma_sample_position;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004911 cm->frame_to_show->color_range = cm->color_range;
4912 cm->frame_to_show->render_width = cm->render_width;
4913 cm->frame_to_show->render_height = cm->render_height;
4914
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02004915 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
4916 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004917
4918 // Pick the loop filter level for the frame.
Hui Su06463e42018-02-23 22:17:36 -08004919 if (!(cm->allow_intrabc && NO_FILTER_FOR_IBC)) {
David Barker218556e2018-02-14 14:23:12 +00004920 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08004921 } else {
Hui Su06463e42018-02-23 22:17:36 -08004922 cm->lf.filter_level[0] = 0;
4923 cm->lf.filter_level[1] = 0;
Hui Su06463e42018-02-23 22:17:36 -08004924 cm->cdef_bits = 0;
4925 cm->cdef_strengths[0] = 0;
4926 cm->nb_cdef_strengths = 1;
4927 cm->cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08004928 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4929 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4930 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08004931 }
David Barker218556e2018-02-14 14:23:12 +00004932
4933 // TODO(debargha): Fix mv search range on encoder side
4934 // aom_extend_frame_inner_borders(cm->frame_to_show, av1_num_planes(cm));
4935 aom_extend_frame_borders(cm->frame_to_show, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004936
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07004937#ifdef OUTPUT_YUV_REC
4938 aom_write_one_yuv_frame(cm, cm->frame_to_show);
4939#endif
4940
Yaowu Xuc27fc142016-08-22 16:08:15 -07004941 // Build the bitstream
Tom Finegane4099e32018-01-23 12:01:51 -08004942 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4943 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004944
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004945 cpi->seq_params_locked = 1;
4946
Hui Sudc54be62018-03-14 19:14:28 -07004947 if (skip_adapt) return AOM_CODEC_OK;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00004948
David Barker5e70a112017-10-03 14:28:17 +01004949 if (cm->seq_params.frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004950 int i;
4951 /* Update reference frame id values based on the value of refresh_mask */
4952 for (i = 0; i < REF_FRAMES; i++) {
4953 if ((cm->refresh_mask >> i) & 1) {
4954 cm->ref_frame_id[i] = cm->current_frame_id;
4955 }
4956 }
4957 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004958
Yaowu Xuc27fc142016-08-22 16:08:15 -07004959#if DUMP_RECON_FRAMES == 1
4960 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07004961 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004962#endif // DUMP_RECON_FRAMES
4963
Soo-Chul Han934af352017-10-15 15:21:51 -04004964 if (cm->seg.enabled) {
4965 if (cm->seg.update_map) {
4966 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07004967 } else if (cm->last_frame_seg_map) {
Soo-Chul Han934af352017-10-15 15:21:51 -04004968 memcpy(cm->current_frame_seg_map, cm->last_frame_seg_map,
4969 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
4970 }
4971 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004972
4973 if (frame_is_intra_only(cm) == 0) {
4974 release_scaled_references(cpi);
4975 }
4976
Cheng Chen46f30c72017-09-07 11:13:33 -07004977 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004978
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08004979#if CONFIG_ENTROPY_STATS
4980 av1_accumulate_frame_counts(&aggregate_fc, &cm->counts);
4981#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07004982
Hui Sudc54be62018-03-14 19:14:28 -07004983 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
4984 *cm->fc = cpi->tile_data[cm->largest_tile_id].tctx;
4985 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004986 }
4987
4988 if (cpi->refresh_golden_frame == 1)
4989 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
4990 else
4991 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4992
4993 if (cpi->refresh_alt_ref_frame == 1)
4994 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
4995 else
4996 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4997
Yaowu Xuc27fc142016-08-22 16:08:15 -07004998 if (cpi->refresh_bwd_ref_frame == 1)
4999 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5000 else
5001 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005002
Yaowu Xuc27fc142016-08-22 16:08:15 -07005003 cm->last_frame_type = cm->frame_type;
5004
Yaowu Xuf883b422016-08-30 14:01:10 -07005005 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005006
Yaowu Xuc27fc142016-08-22 16:08:15 -07005007 if (cm->frame_type == KEY_FRAME) {
5008 // Tell the caller that the frame was coded as a key frame
5009 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5010 } else {
5011 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5012 }
5013
5014 // Clear the one shot update flags for segmentation map and mode/ref loop
5015 // filter deltas.
5016 cm->seg.update_map = 0;
5017 cm->seg.update_data = 0;
5018 cm->lf.mode_ref_delta_update = 0;
5019
Yaowu Xuc27fc142016-08-22 16:08:15 -07005020 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08005021 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5022 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005023 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005024 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005025 // Don't increment frame counters if this was an altref buffer
5026 // update not a real frame
5027 ++cm->current_video_frame;
5028 }
5029
Yaowu Xuc27fc142016-08-22 16:08:15 -07005030 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005031 if (cm->is_reference_frame) {
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005032 // keep track of the last coded dimensions
5033 cm->last_width = cm->width;
5034 cm->last_height = cm->height;
5035
5036 // reset to normal state now that we are done.
5037 cm->last_show_frame = cm->show_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005038 }
Yi Luo10e23002017-07-31 11:54:43 -07005039
Tom Finegane4099e32018-01-23 12:01:51 -08005040 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005041}
5042
Tom Finegane4099e32018-01-23 12:01:51 -08005043static int Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5044 int skip_adapt, unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005045 if (cpi->oxcf.rc_mode == AOM_CBR) {
5046 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005047 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005048 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005049 }
Tom Finegane4099e32018-01-23 12:01:51 -08005050 return encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005051}
5052
Tom Finegane4099e32018-01-23 12:01:51 -08005053static int Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5054 unsigned int *frame_flags) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -08005055#if CONFIG_MISMATCH_DEBUG
5056 mismatch_move_frame_idx_w();
5057#endif
Angie Chiang4d55d762017-12-13 16:18:37 -08005058#if TXCOEFF_COST_TIMER
5059 AV1_COMMON *cm = &cpi->common;
5060 cm->txcoeff_cost_timer = 0;
5061 cm->txcoeff_cost_count = 0;
5062#endif
Tom Finegane4099e32018-01-23 12:01:51 -08005063
5064 if (encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags) !=
5065 AOM_CODEC_OK) {
5066 return AOM_CODEC_ERROR;
5067 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005068
Angie Chiang4d55d762017-12-13 16:18:37 -08005069#if TXCOEFF_COST_TIMER
5070 cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer;
5071 fprintf(stderr,
5072 "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld "
5073 "in us\n",
5074 cm->txcoeff_cost_count, cm->txcoeff_cost_timer,
5075 cm->cum_txcoeff_cost_timer);
5076#endif
5077
Urvang Joshif1fa6862018-01-08 16:39:33 -08005078 // Do not do post-encoding update for those frames that do not have a spot
5079 // in
5080 // a gf group, but note that an OVERLAY frame always has a spot in a gf
5081 // group,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005082 // even when show_existing_frame is used.
5083 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005084 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005085 }
5086 check_show_existing_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005087 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005088}
5089
James Zern3e2613b2017-03-30 23:14:40 -07005090int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005091 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5092 int64_t end_time) {
5093 AV1_COMMON *const cm = &cpi->common;
5094 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005095 int res = 0;
5096 const int subsampling_x = sd->subsampling_x;
5097 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005098 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005099
Yaowu Xuc27fc142016-08-22 16:08:15 -07005100 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005101
Yaowu Xuf883b422016-08-30 14:01:10 -07005102 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005103
Yaowu Xuf883b422016-08-30 14:01:10 -07005104 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005105 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005106 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005107 aom_usec_timer_mark(&timer);
5108 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005109
Yaowu Xuc1fdb4b2018-03-16 08:44:13 -07005110 if ((cm->profile == PROFILE_0) && !cm->seq_params.monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005111 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005112 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005113 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005114 res = -1;
5115 }
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005116 if ((cm->profile == PROFILE_1) &&
5117 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005118 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005119 "Profile 1 requires 4:4:4 color format");
5120 res = -1;
5121 }
5122 if ((cm->profile == PROFILE_2) && (cm->bit_depth <= AOM_BITS_10) &&
5123 !(subsampling_x == 1 && subsampling_y == 0)) {
5124 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5125 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005126 res = -1;
5127 }
5128
5129 return res;
5130}
5131
Yaowu Xuf883b422016-08-30 14:01:10 -07005132static int frame_is_reference(const AV1_COMP *cpi) {
5133 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005134
5135 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005136 cpi->refresh_golden_frame || cpi->refresh_bwd_ref_frame ||
5137 cpi->refresh_alt2_ref_frame || cpi->refresh_alt_ref_frame ||
5138 !cm->error_resilient_mode || cm->lf.mode_ref_delta_update ||
5139 cm->seg.update_map || cm->seg.update_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005140}
5141
Yaowu Xuf883b422016-08-30 14:01:10 -07005142static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005143 const struct lookahead_entry *source) {
5144 int64_t this_duration;
5145 int step = 0;
5146
5147 if (source->ts_start == cpi->first_time_stamp_ever) {
5148 this_duration = source->ts_end - source->ts_start;
5149 step = 1;
5150 } else {
5151 int64_t last_duration =
5152 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5153
5154 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5155
5156 // do a step update if the duration changes by 10%
5157 if (last_duration)
5158 step = (int)((this_duration - last_duration) * 10 / last_duration);
5159 }
5160
5161 if (this_duration) {
5162 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005163 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005164 } else {
5165 // Average this frame's rate into the last second's average
5166 // frame rate. If we haven't seen 1 second yet, then average
5167 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005168 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005169 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5170 double avg_duration = 10000000.0 / cpi->framerate;
5171 avg_duration *= (interval - avg_duration + this_duration);
5172 avg_duration /= interval;
5173
Yaowu Xuf883b422016-08-30 14:01:10 -07005174 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005175 }
5176 }
5177 cpi->last_time_stamp_seen = source->ts_start;
5178 cpi->last_end_time_stamp_seen = source->ts_end;
5179}
5180
5181// Returns 0 if this is not an alt ref else the offset of the source frame
5182// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005183static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005184 RATE_CONTROL *const rc = &cpi->rc;
5185 int arf_src_index = 0;
5186 if (is_altref_enabled(cpi)) {
5187 if (cpi->oxcf.pass == 2) {
5188 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5189 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5190 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5191 }
5192 } else if (rc->source_alt_ref_pending) {
5193 arf_src_index = rc->frames_till_gf_update_due;
5194 }
5195 }
5196 return arf_src_index;
5197}
5198
Yaowu Xuf883b422016-08-30 14:01:10 -07005199static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005200 int brf_src_index = 0;
5201 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5202
5203 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5204 // flag.
5205 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5206 if (cpi->oxcf.pass == 2) {
5207 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5208 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5209 } else {
5210 // TODO(zoeliu): To re-visit the setup for this scenario
5211 brf_src_index = cpi->rc.bipred_group_interval - 1;
5212 }
5213 }
5214
5215 return brf_src_index;
5216}
Zoe Liue9b15e22017-07-19 15:53:01 -07005217
Zoe Liue9b15e22017-07-19 15:53:01 -07005218// Returns 0 if this is not an alt ref else the offset of the source frame
5219// used as the arf midpoint.
5220static int get_arf2_src_index(AV1_COMP *cpi) {
5221 int arf2_src_index = 0;
5222 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5223 if (cpi->oxcf.pass == 2) {
5224 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5225 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5226 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5227 }
5228 }
5229 }
5230 return arf2_src_index;
5231}
Yaowu Xuc27fc142016-08-22 16:08:15 -07005232
Yaowu Xuf883b422016-08-30 14:01:10 -07005233static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005234 const struct lookahead_entry *source) {
5235 RATE_CONTROL *const rc = &cpi->rc;
5236
5237 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005238 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005239
5240 if (cpi->oxcf.pass == 2) {
5241 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5242 rc->is_src_frame_alt_ref =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005243 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07005244 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07005245 rc->is_src_frame_ext_arf =
5246 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005247 } else {
5248 rc->is_src_frame_alt_ref =
5249 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5250 }
5251
5252 if (rc->is_src_frame_alt_ref) {
5253 // Current frame is an ARF overlay frame.
5254 cpi->alt_ref_source = NULL;
5255
Zoe Liue9b15e22017-07-19 15:53:01 -07005256 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
5257 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
5258 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
5259 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
5260 cpi->refresh_last_frame = 1;
5261 } else {
Zoe Liue9b15e22017-07-19 15:53:01 -07005262 // Don't refresh the last buffer for an ARF overlay frame. It will
5263 // become the GF so preserve last as an alternative prediction option.
5264 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005265 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005266 }
5267}
5268
5269#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005270extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5271 const unsigned char *img2, int img2_pitch,
5272 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005273
5274static void adjust_image_stat(double y, double u, double v, double all,
5275 ImageStat *s) {
5276 s->stat[Y] += y;
5277 s->stat[U] += u;
5278 s->stat[V] += v;
5279 s->stat[ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005280 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005281}
5282
Angie Chiang08a22a62017-07-17 17:29:17 -07005283static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005284 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005285 double samples = 0.0;
5286 uint32_t in_bit_depth = 8;
5287 uint32_t bit_depth = 8;
5288
Angie Chiang08a22a62017-07-17 17:29:17 -07005289#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07005290 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005291#endif
5292 cpi->bytes += frame_bytes;
5293
Yaowu Xuc27fc142016-08-22 16:08:15 -07005294 if (cm->use_highbitdepth) {
5295 in_bit_depth = cpi->oxcf.input_bit_depth;
5296 bit_depth = cm->bit_depth;
5297 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005298 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005299 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005300 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5301 double y, u, v, frame_all;
5302
5303 cpi->count++;
5304 if (cpi->b_calculate_psnr) {
5305 PSNR_STATS psnr;
5306 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005307 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005308 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005309 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005310
5311 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5312 &cpi->psnr);
5313 cpi->total_sq_error += psnr.sse[0];
5314 cpi->total_samples += psnr.samples[0];
5315 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005316 // TODO(yaowu): unify these two versions into one.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005317 if (cm->use_highbitdepth)
5318 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005319 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005320 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005321 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005322
Yaowu Xuf883b422016-08-30 14:01:10 -07005323 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005324 cpi->summed_quality += frame_ssim2 * weight;
5325 cpi->summed_weights += weight;
5326
5327#if 0
5328 {
5329 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005330 double y2 = psnr.psnr[1];
5331 double u2 = psnr.psnr[2];
5332 double v2 = psnr.psnr[3];
5333 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005334 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
Zoe Liuee202be2017-11-17 12:14:33 -08005335 cm->current_video_frame, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005336 frame_psnr2, frame_ssim2);
5337 fclose(f);
5338 }
5339#endif
5340 }
5341 if (cpi->b_calculate_blockiness) {
Yaowu Xud3e7c682017-12-21 14:08:25 -08005342 if (!cm->use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005343 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005344 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5345 recon->y_stride, orig->y_width, orig->y_height);
5346 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005347 cpi->total_blockiness += frame_blockiness;
5348 }
5349
5350 if (cpi->b_calculate_consistency) {
Yaowu Xud3e7c682017-12-21 14:08:25 -08005351 if (!cm->use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005352 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005353 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5354 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5355
5356 const double peak = (double)((1 << in_bit_depth) - 1);
5357 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005358 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005359 if (consistency > 0.0)
5360 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005361 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005362 cpi->total_inconsistency += this_inconsistency;
5363 }
5364 }
5365 }
5366
5367 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005368 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005369 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005370 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005371 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5372 }
5373}
5374#endif // CONFIG_INTERNAL_STATS
5375
RogerZhou3b635242017-09-19 10:06:46 -07005376static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5377 const YV12_BUFFER_CONFIG *last_picture,
5378 hash_table *last_hash_table) {
5379 aom_clear_system_state();
5380 // check use hash ME
5381 int k;
5382 uint32_t hash_value_1;
5383 uint32_t hash_value_2;
5384
5385 const int block_size = 8;
5386 const double threshold_current = 0.8;
5387 const double threshold_average = 0.95;
5388 const int max_history_size = 32;
5389 int T = 0; // total block
5390 int C = 0; // match with collocated block
5391 int S = 0; // smooth region but not match with collocated block
5392 int M = 0; // match with other block
5393
5394 const int pic_width = cur_picture->y_width;
5395 const int pic_height = cur_picture->y_height;
5396 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5397 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5398 const int x_pos = j;
5399 const int y_pos = i;
5400 int match = 1;
5401 T++;
5402
5403 // check whether collocated block match with current
5404 uint8_t *p_cur = cur_picture->y_buffer;
5405 uint8_t *p_ref = last_picture->y_buffer;
5406 int stride_cur = cur_picture->y_stride;
5407 int stride_ref = last_picture->y_stride;
5408 p_cur += (y_pos * stride_cur + x_pos);
5409 p_ref += (y_pos * stride_ref + x_pos);
5410
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005411 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5412 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5413 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5414 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5415 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5416 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5417 match = 0;
5418 }
RogerZhou3b635242017-09-19 10:06:46 -07005419 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005420 p16_cur += stride_cur;
5421 p16_ref += stride_ref;
RogerZhou3b635242017-09-19 10:06:46 -07005422 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005423 } else {
5424 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5425 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5426 if (p_cur[tmpX] != p_ref[tmpX]) {
5427 match = 0;
5428 }
5429 }
5430 p_cur += stride_cur;
5431 p_ref += stride_ref;
5432 }
RogerZhou3b635242017-09-19 10:06:46 -07005433 }
5434
5435 if (match) {
5436 C++;
5437 continue;
5438 }
5439
5440 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5441 y_pos) ||
5442 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5443 S++;
5444 continue;
5445 }
5446
5447 av1_get_block_hash_value(
5448 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005449 block_size, &hash_value_1, &hash_value_2,
5450 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH));
5451 // Hashing does not work for highbitdepth currently.
5452 // TODO(Roger): Make it work for highbitdepth.
5453 if (av1_use_hash_me(&cpi->common)) {
5454 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5455 M++;
5456 }
RogerZhou3b635242017-09-19 10:06:46 -07005457 }
5458 }
5459 }
5460
5461 assert(T > 0);
5462 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5463 double m_rate = ((double)(M)) / ((double)(T));
5464
5465 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5466 cpi->m_rate_array[cpi->rate_index] = m_rate;
5467
5468 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5469 cpi->rate_size++;
5470 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5471
5472 if (csm_rate < threshold_current) {
5473 return 0;
5474 }
5475
5476 if (C == T) {
5477 return 1;
5478 }
5479
5480 double csm_average = 0.0;
5481 double m_average = 0.0;
5482
5483 for (k = 0; k < cpi->rate_size; k++) {
5484 csm_average += cpi->csm_rate_array[k];
5485 m_average += cpi->m_rate_array[k];
5486 }
5487 csm_average /= cpi->rate_size;
5488 m_average /= cpi->rate_size;
5489
5490 if (csm_average < threshold_average) {
5491 return 0;
5492 }
5493
5494 if (M > (T - C - S) / 3) {
5495 return 1;
5496 }
5497
5498 if (csm_rate > 0.99 && m_rate > 0.01) {
5499 return 1;
5500 }
5501
5502 if (csm_average + m_average > 1.01) {
5503 return 1;
5504 }
5505
5506 return 0;
5507}
RogerZhou3b635242017-09-19 10:06:46 -07005508
Yaowu Xuf883b422016-08-30 14:01:10 -07005509int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5510 size_t *size, uint8_t *dest, int64_t *time_stamp,
5511 int64_t *time_end, int flush) {
5512 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5513 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005514 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005515 BufferPool *const pool = cm->buffer_pool;
5516 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07005517 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005518 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5519 struct lookahead_entry *last_source = NULL;
5520 struct lookahead_entry *source = NULL;
5521 int arf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005522 int brf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005523 int i;
5524
5525#if CONFIG_BITSTREAM_DEBUG
5526 assert(cpi->oxcf.max_threads == 0 &&
5527 "bitstream debug tool does not support multithreading");
5528 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07005529 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005530#endif
5531
Dominic Symesd4929012018-01-31 17:32:01 +01005532 cm->showable_frame = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005533 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005534
RogerZhou3b635242017-09-19 10:06:46 -07005535 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005536
5537 // Is multi-arf enabled.
5538 // Note that at the moment multi_arf is only configured for 2 pass VBR
5539 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
5540 cpi->multi_arf_allowed = 1;
5541 else
5542 cpi->multi_arf_allowed = 0;
5543
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07005544 // Normal defaults
Yaowu Xuc27fc142016-08-22 16:08:15 -07005545 cm->refresh_frame_context =
5546 (oxcf->error_resilient_mode || oxcf->frame_parallel_decoding_mode)
James Zernf34dfc82018-02-23 16:53:33 -08005547 ? REFRESH_FRAME_CONTEXT_DISABLED
Yaowu Xuc27fc142016-08-22 16:08:15 -07005548 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005549 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08005550 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005551
5552 cpi->refresh_last_frame = 1;
5553 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005554 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005555 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005556 cpi->refresh_alt_ref_frame = 0;
5557
Zoe Liub4991202017-12-21 15:31:06 -08005558 // TODO(zoeliu@gmail.com): To support forward-KEY_FRAME and set up the
5559 // following flag accordingly.
5560 cm->reset_decoder_state = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005561
Yaowu Xuc27fc142016-08-22 16:08:15 -07005562 if (oxcf->pass == 2 && cm->show_existing_frame) {
5563 // Manage the source buffer and flush out the source frame that has been
5564 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07005565 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005566 *size = 0;
5567 return -1;
5568 }
Alex Conversef77fd0b2017-04-20 11:00:24 -07005569 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005570 // TODO(zoeliu): To track down to determine whether it's needed to adjust
5571 // the frame rate.
5572 *time_stamp = source->ts_start;
5573 *time_end = source->ts_end;
5574
5575 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005576 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005577
Urvang Joshif1fa6862018-01-08 16:39:33 -08005578 // Find a free buffer for the new frame, releasing the reference
5579 // previously
Yaowu Xuc27fc142016-08-22 16:08:15 -07005580 // held.
5581 if (cm->new_fb_idx != INVALID_IDX) {
5582 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5583 }
5584 cm->new_fb_idx = get_free_fb(cm);
5585
5586 if (cm->new_fb_idx == INVALID_IDX) return -1;
5587
5588 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005589 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005590
5591 // Start with a 0 size frame.
5592 *size = 0;
5593
5594 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005595 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005596
Tom Finegane4099e32018-01-23 12:01:51 -08005597 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
5598 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005599
5600 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
5601
5602#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07005603 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005604#endif // CONFIG_INTERNAL_STATS
5605
5606 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005607 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005608
5609 cm->show_existing_frame = 0;
5610 return 0;
5611 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005612
5613 // Should we encode an arf frame.
5614 arf_src_index = get_arf_src_index(cpi);
5615 if (arf_src_index) {
5616 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005617 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005618 // Avoid creating an alt-ref if there's a forced keyframe pending.
5619 if (e == NULL) {
5620 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07005621 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005622 arf_src_index = 0;
5623 flush = 1;
5624 break;
5625 }
5626 }
5627 }
5628
5629 if (arf_src_index) {
5630 assert(arf_src_index <= rc->frames_to_key);
5631
Yaowu Xuf883b422016-08-30 14:01:10 -07005632 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005633 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005634 cpi->alt_ref_source = source;
5635
5636 if (oxcf->arnr_max_frames > 0) {
Sebastien Alaiwan6697acf2018-02-21 16:59:17 +01005637 // Produce the filtered ARF frame.
5638 av1_temporal_filter(cpi, arf_src_index);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005639 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005640 force_src_buffer = &cpi->alt_ref_buffer;
5641 }
5642
5643 cm->show_frame = 0;
5644 cm->intra_only = 0;
5645 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005646 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005647 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005648 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005649 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005650 rc->is_src_frame_alt_ref = 0;
5651 }
5652 rc->source_alt_ref_pending = 0;
5653 }
5654
Zoe Liue9b15e22017-07-19 15:53:01 -07005655 // Should we encode an arf2 frame.
5656 arf_src_index = get_arf2_src_index(cpi);
5657 if (arf_src_index) {
5658 for (i = 0; i <= arf_src_index; ++i) {
5659 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
5660 // Avoid creating an alt-ref if there's a forced keyframe pending.
5661 if (e == NULL) {
5662 break;
5663 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
5664 arf_src_index = 0;
5665 flush = 1;
5666 break;
5667 }
5668 }
5669 }
5670
5671 if (arf_src_index) {
5672 assert(arf_src_index <= rc->frames_to_key);
5673
5674 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005675 cm->showable_frame = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07005676 cpi->alt_ref_source = source;
5677
5678 if (oxcf->arnr_max_frames > 0) {
5679 // Produce the filtered ARF frame.
Sebastien Alaiwan6697acf2018-02-21 16:59:17 +01005680 av1_temporal_filter(cpi, arf_src_index);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005681 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
Zoe Liue9b15e22017-07-19 15:53:01 -07005682 force_src_buffer = &cpi->alt_ref_buffer;
5683 }
5684
5685 cm->show_frame = 0;
5686 cm->intra_only = 0;
5687 cpi->refresh_alt2_ref_frame = 1;
5688 cpi->refresh_last_frame = 0;
5689 cpi->refresh_golden_frame = 0;
5690 cpi->refresh_bwd_ref_frame = 0;
5691 cpi->refresh_alt_ref_frame = 0;
5692 rc->is_src_frame_alt_ref = 0;
5693 rc->is_src_frame_ext_arf = 0;
5694 }
5695 rc->source_alt_ref_pending = 0;
5696 }
Zoe Liue9b15e22017-07-19 15:53:01 -07005697
Yaowu Xuc27fc142016-08-22 16:08:15 -07005698 rc->is_bwd_ref_frame = 0;
5699 brf_src_index = get_brf_src_index(cpi);
5700 if (brf_src_index) {
5701 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07005702 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005703 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005704 cm->show_frame = 0;
5705 cm->intra_only = 0;
5706
5707 cpi->refresh_bwd_ref_frame = 1;
5708 cpi->refresh_last_frame = 0;
5709 cpi->refresh_golden_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005710 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005711 cpi->refresh_alt_ref_frame = 0;
5712
5713 rc->is_bwd_ref_frame = 1;
5714 }
5715 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005716
5717 if (!source) {
5718 // Get last frame source.
5719 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005720 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005721 return -1;
5722 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005723 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005724 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005725 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005726
5727 if (source != NULL) {
5728 cm->show_frame = 1;
5729 cm->intra_only = 0;
5730
5731 // Check to see if the frame should be encoded as an arf overlay.
5732 check_src_altref(cpi, source);
5733 }
5734 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005735 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07005736 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005737 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005738 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
5739
5740 *time_stamp = source->ts_start;
5741 *time_end = source->ts_end;
Sarah Parker73556772018-03-28 18:28:05 -07005742 av1_apply_encoding_flags(cpi, source->flags);
Yaowu Xuf883b422016-08-30 14:01:10 -07005743 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005744
5745 } else {
5746 *size = 0;
5747 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005748 av1_end_first_pass(cpi); /* get last stats packet */
Yaowu Xuc27fc142016-08-22 16:08:15 -07005749 cpi->twopass.first_pass_done = 1;
5750 }
5751 return -1;
5752 }
5753
5754 if (source->ts_start < cpi->first_time_stamp_ever) {
5755 cpi->first_time_stamp_ever = source->ts_start;
5756 cpi->last_end_time_stamp_seen = source->ts_start;
5757 }
5758
5759 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005760 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005761
5762 // adjust frame rates based on timestamps given
5763 if (cm->show_frame) adjust_frame_rate(cpi, source);
5764
5765 // Find a free buffer for the new frame, releasing the reference previously
5766 // held.
5767 if (cm->new_fb_idx != INVALID_IDX) {
5768 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5769 }
5770 cm->new_fb_idx = get_free_fb(cm);
5771
5772 if (cm->new_fb_idx == INVALID_IDX) return -1;
5773
Zoe Liuf452fdf2017-11-02 23:08:12 -07005774 // Retain the RF_LEVEL for the current newly coded frame.
5775 cpi->frame_rf_level[cm->new_fb_idx] =
5776 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
Zoe Liuf452fdf2017-11-02 23:08:12 -07005777
Yaowu Xuc27fc142016-08-22 16:08:15 -07005778 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Yaowu Xu9b0f7032017-07-31 11:01:19 -07005779 cm->cur_frame->buf.buf_8bit_valid = 0;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07005780
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07005781 if (cm->film_grain_table) {
5782 cm->film_grain_params_present = aom_film_grain_table_lookup(
5783 cm->film_grain_table, *time_stamp, *time_end, 0 /* erase */,
5784 &cm->film_grain_params);
5785 }
Dominic Symesd4929012018-01-31 17:32:01 +01005786 cm->cur_frame->film_grain_params_present = cm->film_grain_params_present;
Zoe Liu6cfaff92016-10-18 17:12:11 -07005787
Yaowu Xuc27fc142016-08-22 16:08:15 -07005788 // Start with a 0 size frame.
5789 *size = 0;
5790
5791 cpi->frame_flags = *frame_flags;
5792
5793 if (oxcf->pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005794 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005795 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07005796 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005797 }
5798
5799 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
Zoe Liu27deb382018-03-27 15:13:56 -07005800 for (i = 0; i < REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005801 }
5802
Yaowu Xuc27fc142016-08-22 16:08:15 -07005803 cm->using_qmatrix = cpi->oxcf.using_qm;
5804 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
5805 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005806
David Barker5e70a112017-10-03 14:28:17 +01005807 if (cm->seq_params.frame_id_numbers_present_flag) {
Debargha Mukherjee778023d2017-09-26 17:50:27 -07005808 if (*time_stamp == 0) {
5809 cpi->common.current_frame_id = -1;
5810 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005811 }
Zoe Liuca0cd3f2018-02-26 15:07:50 -08005812
RogerZhou3b635242017-09-19 10:06:46 -07005813 cpi->cur_poc++;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08005814 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
5815 !frame_is_intra_only(cm)) {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00005816 if (cpi->common.seq_params.force_integer_mv == 2) {
RogerZhou3b635242017-09-19 10:06:46 -07005817 struct lookahead_entry *previous_entry =
Debargha Mukherjeea71e3db2018-02-28 07:47:17 -08005818 av1_lookahead_peek(cpi->lookahead, cpi->previous_index);
5819 if (!previous_entry)
5820 cpi->common.cur_frame_force_integer_mv = 0;
5821 else
5822 cpi->common.cur_frame_force_integer_mv = is_integer_mv(
5823 cpi, cpi->source, &previous_entry->img, cpi->previous_hash_table);
RogerZhou3b635242017-09-19 10:06:46 -07005824 } else {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00005825 cpi->common.cur_frame_force_integer_mv =
5826 cpi->common.seq_params.force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07005827 }
5828 } else {
RogerZhou10a03802017-10-26 11:49:48 -07005829 cpi->common.cur_frame_force_integer_mv = 0;
RogerZhou3b635242017-09-19 10:06:46 -07005830 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005831
Yaowu Xuc27fc142016-08-22 16:08:15 -07005832 if (oxcf->pass == 1) {
5833 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07005834 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005835 } else if (oxcf->pass == 2) {
Tom Finegane4099e32018-01-23 12:01:51 -08005836 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
5837 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005838 } else {
5839 // One pass encode
Tom Finegane4099e32018-01-23 12:01:51 -08005840 if (Pass0Encode(cpi, size, dest, 0, frame_flags) != AOM_CODEC_OK)
5841 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005842 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07005843 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08005844 cpi->previous_hash_table = &cm->cur_frame->hash_table;
RogerZhou3b635242017-09-19 10:06:46 -07005845 {
5846 int l;
5847 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
5848 if ((cpi->lookahead->buf + l) == source) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08005849 cpi->previous_index = l;
RogerZhou3b635242017-09-19 10:06:46 -07005850 break;
5851 }
5852 }
5853
5854 if (l == cpi->lookahead->max_sz) {
5855 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5856 "Failed to find last frame original buffer");
5857 }
5858 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07005859 }
5860
Yunqing Wang267e3272017-11-09 14:23:22 -08005861 if (!cm->large_scale_tile) {
Yunqing Wang267e3272017-11-09 14:23:22 -08005862 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Yunqing Wang267e3272017-11-09 14:23:22 -08005863 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005864
Yunqing Wangb041d8a2017-11-15 12:31:18 -08005865#define EXT_TILE_DEBUG 0
5866#if EXT_TILE_DEBUG
5867 if (cm->large_scale_tile && oxcf->pass == 2) {
5868 char fn[20] = "./fc";
5869 fn[4] = cm->current_video_frame / 100 + '0';
5870 fn[5] = (cm->current_video_frame % 100) / 10 + '0';
5871 fn[6] = (cm->current_video_frame % 10) + '0';
5872 fn[7] = '\0';
5873 av1_print_frame_contexts(cm->fc, fn);
5874 }
5875#endif // EXT_TILE_DEBUG
5876#undef EXT_TILE_DEBUG
Yaowu Xuc7119a72018-03-29 09:59:37 -07005877
Dominic Symesd4929012018-01-31 17:32:01 +01005878 cm->showable_frame = !cm->show_frame && cm->showable_frame;
Yunqing Wangb041d8a2017-11-15 12:31:18 -08005879
Yaowu Xuc27fc142016-08-22 16:08:15 -07005880 // No frame encoded, or frame was dropped, release scaled references.
5881 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
5882 release_scaled_references(cpi);
5883 }
5884
5885 if (*size > 0) {
5886 cpi->droppable = !frame_is_reference(cpi);
5887 }
5888
Yaowu Xuf883b422016-08-30 14:01:10 -07005889 aom_usec_timer_mark(&cmptimer);
5890 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005891
5892 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
5893 generate_psnr_packet(cpi);
5894
5895#if CONFIG_INTERNAL_STATS
5896 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07005897 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005898 }
5899#endif // CONFIG_INTERNAL_STATS
5900
Yaowu Xuf883b422016-08-30 14:01:10 -07005901 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005902
5903 return 0;
5904}
5905
Yaowu Xuf883b422016-08-30 14:01:10 -07005906int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5907 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005908 if (!cm->show_frame) {
5909 return -1;
5910 } else {
5911 int ret;
5912 if (cm->frame_to_show) {
5913 *dest = *cm->frame_to_show;
5914 dest->y_width = cm->width;
5915 dest->y_height = cm->height;
5916 dest->uv_width = cm->width >> cm->subsampling_x;
5917 dest->uv_height = cm->height >> cm->subsampling_y;
5918 ret = 0;
5919 } else {
5920 ret = -1;
5921 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005922 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005923 return ret;
5924 }
5925}
5926
Yaowu Xuf883b422016-08-30 14:01:10 -07005927int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005928 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
5929
5930 *frame =
5931 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
5932 return 0;
5933}
5934
Yaowu Xuf883b422016-08-30 14:01:10 -07005935int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
5936 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005937 int hr = 0, hs = 0, vr = 0, vs = 0;
5938
5939 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5940
5941 Scale2Ratio(horiz_mode, &hr, &hs);
5942 Scale2Ratio(vert_mode, &vr, &vs);
5943
5944 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005945 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5946 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005947
5948 return 0;
5949}
5950
Yaowu Xuf883b422016-08-30 14:01:10 -07005951int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005952
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005953int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
5954 size_t output_size = 0;
5955 size_t total_bytes_read = 0;
5956 size_t remaining_size = *frame_size;
5957 uint8_t *buff_ptr = buffer;
5958
5959 // go through each OBUs
5960 while (total_bytes_read < *frame_size) {
5961 uint8_t saved_obu_header[2];
5962 uint64_t obu_payload_size;
5963 size_t length_of_payload_size;
5964 size_t length_of_obu_size;
5965 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
5966 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
5967
5968 // save the obu header (1 or 2 bytes)
5969 memmove(saved_obu_header, buff_ptr, obu_header_size);
5970 // clear the obu_has_size_field
5971 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
5972
5973 // get the payload_size and length of payload_size
5974 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
5975 &obu_payload_size, &length_of_payload_size) != 0) {
5976 return AOM_CODEC_ERROR;
5977 }
5978 obu_bytes_read += length_of_payload_size;
5979
5980 // calculate the length of size of the obu header plus payload
5981 length_of_obu_size =
5982 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
5983
5984 // move the rest of data to new location
5985 memmove(buff_ptr + length_of_obu_size + obu_header_size,
5986 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07005987 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005988
5989 // write the new obu size
5990 const uint64_t obu_size = obu_header_size + obu_payload_size;
5991 size_t coded_obu_size;
5992 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
5993 &coded_obu_size) != 0) {
5994 return AOM_CODEC_ERROR;
5995 }
5996
5997 // write the saved (modified) obu_header following obu size
5998 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
5999
6000 total_bytes_read += obu_bytes_read;
6001 remaining_size -= obu_bytes_read;
6002 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07006003 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006004 }
6005
6006 *frame_size = output_size;
6007 return AOM_CODEC_OK;
6008}
6009
Yaowu Xuf883b422016-08-30 14:01:10 -07006010void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006011 // TODO(yunqingwang): For what references to use, external encoding flags
6012 // should be consistent with internal reference frame selection. Need to
6013 // ensure that there is not conflict between the two. In AV1 encoder, the
6014 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
6015 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
6016 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006017 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006018 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006019 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6020 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6021 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006022 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006023 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006024 } else {
6025 int ref = AOM_REFFRAME_ALL;
6026
6027 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6028 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6029
6030 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6031
6032 if (flags & AOM_EFLAG_NO_REF_ARF) {
6033 ref ^= AOM_ALT_FLAG;
6034 ref ^= AOM_BWD_FLAG;
6035 ref ^= AOM_ALT2_FLAG;
6036 } else {
6037 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6038 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
6039 }
6040
6041 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006042 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006043 }
6044
6045 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006046 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006047 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006048
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006049 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6050 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006051
Yaowu Xuf883b422016-08-30 14:01:10 -07006052 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006053
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006054 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6055 upd ^= AOM_ALT_FLAG;
6056 upd ^= AOM_BWD_FLAG;
6057 upd ^= AOM_ALT2_FLAG;
6058 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006059
Yaowu Xuf883b422016-08-30 14:01:10 -07006060 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006061 }
6062
Yaowu Xuf883b422016-08-30 14:01:10 -07006063 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6064 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006065 }
6066}