blob: 25b84fc27322355c8d1ac7bb354d34a91bf6ca08 [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
Tom Finegan60e653d2018-05-22 11:34:58 -070016#include "config/aom_config.h"
Tom Finegan44702c82018-05-22 13:00:39 -070017#include "config/av1_rtcd.h"
18#include "config/aom_dsp_rtcd.h"
19#include "config/aom_scale_rtcd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070020
21#include "av1/common/alloccommon.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010022#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070023#include "av1/common/filter.h"
24#include "av1/common/idct.h"
25#include "av1/common/reconinter.h"
26#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070027#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070028#include "av1/common/tile_common.h"
29
30#include "av1/encoder/aq_complexity.h"
31#include "av1/encoder/aq_cyclicrefresh.h"
32#include "av1/encoder/aq_variance.h"
33#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070034#include "av1/encoder/context_tree.h"
35#include "av1/encoder/encodeframe.h"
36#include "av1/encoder/encodemv.h"
37#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070038#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070039#include "av1/encoder/ethread.h"
40#include "av1/encoder/firstpass.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070041#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070042#include "av1/encoder/mbgraph.h"
43#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070044#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070045#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070046#include "av1/encoder/ratectrl.h"
47#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070048#include "av1/encoder/segmentation.h"
49#include "av1/encoder/speed_features.h"
50#include "av1/encoder/temporal_filter.h"
51
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"
Neil Birkbecka2893ab2018-06-08 14:45:13 -070059#if CONFIG_DENOISE
60#include "aom_dsp/grain_table.h"
61#include "aom_dsp/noise_util.h"
62#include "aom_dsp/noise_model.h"
63#endif
Jingning Han1aab8182016-06-03 11:09:06 -070064#include "aom_ports/aom_timer.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070065#include "aom_ports/mem.h"
66#include "aom_ports/system_state.h"
Yaowu Xuf883b422016-08-30 14:01:10 -070067#include "aom_scale/aom_scale.h"
Angie Chiang5b5f4df2017-12-06 10:41:12 -080068#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070069#include "aom_util/debug_util.h"
Angie Chiang5b5f4df2017-12-06 10:41:12 -080070#endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070071
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000072#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000073
Andrey Norkin795ba872018-03-06 13:24:14 -080074// av1 uses 10,000,000 ticks/second as time stamp
75#define TICKS_PER_SEC 10000000LL
Andrey Norkin795ba872018-03-06 13:24:14 -080076
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080077#if CONFIG_ENTROPY_STATS
78FRAME_COUNTS aggregate_fc;
79#endif // CONFIG_ENTROPY_STATS
80
Yaowu Xuc27fc142016-08-22 16:08:15 -070081#define AM_SEGMENT_ID_INACTIVE 7
82#define AM_SEGMENT_ID_ACTIVE 0
83
Johannb0ef6ff2018-02-08 14:32:21 -080084// Whether to use high precision mv for altref computation.
85#define ALTREF_HIGH_PRECISION_MV 1
86
87// Q threshold for high precision mv. Choose a very high value for now so that
88// HIGH_PRECISION is always chosen.
89#define HIGH_PRECISION_MV_QTHRESH 200
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070090
Yaowu Xuc27fc142016-08-22 16:08:15 -070091// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070092#ifdef OUTPUT_YUV_SKINMAP
93FILE *yuv_skinmap_file = NULL;
94#endif
95#ifdef OUTPUT_YUV_REC
96FILE *yuv_rec_file;
97#define FILE_NAME_LEN 100
98#endif
99
Yaowu Xuf883b422016-08-30 14:01:10 -0700100static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700101 switch (mode) {
102 case NORMAL:
103 *hr = 1;
104 *hs = 1;
105 break;
106 case FOURFIVE:
107 *hr = 4;
108 *hs = 5;
109 break;
110 case THREEFIVE:
111 *hr = 3;
112 *hs = 5;
113 break;
114 case ONETWO:
115 *hr = 1;
116 *hs = 2;
117 break;
118 default:
119 *hr = 1;
120 *hs = 1;
121 assert(0);
122 break;
123 }
124}
125
126// Mark all inactive blocks as active. Other segmentation features may be set
127// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700128static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700129 unsigned char *const seg_map = cpi->segmentation_map;
130 int i;
131 if (cpi->active_map.enabled || cpi->active_map.update)
132 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
133 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
134 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
135}
136
Yaowu Xuf883b422016-08-30 14:01:10 -0700137static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700138 struct segmentation *const seg = &cpi->common.seg;
139 unsigned char *const seg_map = cpi->segmentation_map;
140 const unsigned char *const active_map = cpi->active_map.map;
141 int i;
142
143 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
144
145 if (frame_is_intra_only(&cpi->common)) {
146 cpi->active_map.enabled = 0;
147 cpi->active_map.update = 1;
148 }
149
150 if (cpi->active_map.update) {
151 if (cpi->active_map.enabled) {
152 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
153 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700154 av1_enable_segmentation(seg);
155 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700156 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
157 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
158 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
159 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
160
161 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
162 -MAX_LOOP_FILTER);
163 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
164 -MAX_LOOP_FILTER);
165 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
166 -MAX_LOOP_FILTER);
167 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
168 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700169 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700170 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700171 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
172 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
173 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
174 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700175 if (seg->enabled) {
176 seg->update_data = 1;
177 seg->update_map = 1;
178 }
179 }
180 cpi->active_map.update = 0;
181 }
182}
183
Yaowu Xuf883b422016-08-30 14:01:10 -0700184int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
185 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700186 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
187 unsigned char *const active_map_8x8 = cpi->active_map.map;
188 const int mi_rows = cpi->common.mi_rows;
189 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700190 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
191 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700192 cpi->active_map.update = 1;
193 if (new_map_16x16) {
194 int r, c;
195 for (r = 0; r < mi_rows; ++r) {
196 for (c = 0; c < mi_cols; ++c) {
197 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700198 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700199 ? AM_SEGMENT_ID_ACTIVE
200 : AM_SEGMENT_ID_INACTIVE;
201 }
202 }
203 cpi->active_map.enabled = 1;
204 } else {
205 cpi->active_map.enabled = 0;
206 }
207 return 0;
208 } else {
209 return -1;
210 }
211}
212
Yaowu Xuf883b422016-08-30 14:01:10 -0700213int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
214 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700215 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
216 new_map_16x16) {
217 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
218 const int mi_rows = cpi->common.mi_rows;
219 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700220 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
221 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
222
Yaowu Xuc27fc142016-08-22 16:08:15 -0700223 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
224 if (cpi->active_map.enabled) {
225 int r, c;
226 for (r = 0; r < mi_rows; ++r) {
227 for (c = 0; c < mi_cols; ++c) {
228 // Cyclic refresh segments are considered active despite not having
229 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700230 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700231 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
232 }
233 }
234 }
235 return 0;
236 } else {
237 return -1;
238 }
239}
240
Yaowu Xu45295c32018-03-29 12:06:10 -0700241static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
242 int cur_frame_force_integer_mv) {
James Zern01a9d702017-08-25 19:09:33 +0000243 MACROBLOCK *const mb = &cpi->td.mb;
Hui Su50361152018-03-02 11:01:42 -0800244 cpi->common.allow_high_precision_mv =
245 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Rupert Swarbricka84faf22017-12-11 13:56:40 +0000246 const int copy_hp =
247 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Jingning Hanf050fc12018-03-09 14:53:33 -0800248 int *(*src)[2] = copy_hp ? &mb->nmvcost_hp : &mb->nmvcost;
249 mb->mv_cost_stack = *src;
James Zern01a9d702017-08-25 19:09:33 +0000250}
251
Yaowu Xuf883b422016-08-30 14:01:10 -0700252static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800253 const AV1_COMMON *const cm = &cpi->common;
254
Yaowu Xuf883b422016-08-30 14:01:10 -0700255 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700256 return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100257#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800258 if (cm->options && cm->options->ext_partition)
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100259#endif
260 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
261 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700262
Yaowu Xuf883b422016-08-30 14:01:10 -0700263 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700264
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100265// TODO(any): Possibly could improve this with a heuristic.
266#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800267 if (cm->options && !cm->options->ext_partition) return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100268#endif
Urvang Joshie4530f82018-01-09 11:43:37 -0800269
Urvang Joshiaab74432018-06-01 12:06:22 -0700270 // When superres / resize is on, 'cm->width / height' can change between
271 // calls, so we don't apply this heuristic there. Also, this heuristic gives
272 // compression gain for speed >= 2 only.
273 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
274 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 2) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800275 return (cm->width >= 480 && cm->height >= 360) ? BLOCK_128X128
276 : BLOCK_64X64;
277 }
278
Yaowu Xuc27fc142016-08-22 16:08:15 -0700279 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700280}
281
Yaowu Xuf883b422016-08-30 14:01:10 -0700282static void setup_frame(AV1_COMP *cpi) {
283 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800284 // Set up entropy context depending on frame type. The decoder mandates
285 // the use of the default context, index 0, for keyframes and inter
286 // frames where the error_resilient_mode or intra_only flag is set. For
287 // other inter-frames the encoder currently uses only two contexts;
288 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500289
Thomas Daede51020e12017-12-14 20:12:44 -0800290 cm->primary_ref_frame = PRIMARY_REF_NONE;
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700291 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
292 cm->force_primary_ref_none) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700293 av1_setup_past_independence(cm);
Thomas Daede51020e12017-12-14 20:12:44 -0800294 for (int i = 0; i < REF_FRAMES; i++) {
295 cm->fb_of_context_type[i] = -1;
296 }
297 cm->fb_of_context_type[REGULAR_FRAME] =
Sarah Parkerb9041612018-05-22 19:06:47 -0700298 cm->show_frame ? get_ref_frame_map_idx(cpi, GOLDEN_FRAME)
299 : get_ref_frame_map_idx(cpi, ALTREF_FRAME);
Sarah Parker167546f2018-04-24 12:14:45 -0700300 cm->frame_context_idx = REGULAR_FRAME;
Yunqing Wang19aefd12018-05-14 15:38:57 -0700301 } else {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700302 const GF_GROUP *gf_group = &cpi->twopass.gf_group;
Zoe Liue9b15e22017-07-19 15:53:01 -0700303 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700304 cm->frame_context_idx = EXT_ARF_FRAME;
305 else if (cpi->refresh_alt_ref_frame)
306 cm->frame_context_idx = ARF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700307 else if (cpi->rc.is_src_frame_alt_ref)
308 cm->frame_context_idx = OVERLAY_FRAME;
309 else if (cpi->refresh_golden_frame)
310 cm->frame_context_idx = GLD_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700311 else if (cpi->refresh_bwd_ref_frame)
312 cm->frame_context_idx = BRF_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700313 else
314 cm->frame_context_idx = REGULAR_FRAME;
Thomas Daede51020e12017-12-14 20:12:44 -0800315 int wanted_fb = cm->fb_of_context_type[cm->frame_context_idx];
316 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ref_frame++) {
317 int fb = get_ref_frame_map_idx(cpi, ref_frame);
318 if (fb == wanted_fb) {
319 cm->primary_ref_frame = ref_frame - LAST_FRAME;
320 }
321 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700322 }
323
Sarah Parkerb9041612018-05-22 19:06:47 -0700324 if (cm->frame_type == KEY_FRAME && cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700325 cpi->refresh_golden_frame = 1;
326 cpi->refresh_alt_ref_frame = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700327 av1_zero(cpi->interp_filter_selected);
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000328 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Debargha Mukherjee778023d2017-09-26 17:50:27 -0700329 set_use_reference_buffer(cm, 0);
Tarek AMARAc9813852018-03-05 18:40:18 -0500330 } else if (frame_is_sframe(cm)) {
331 cpi->refresh_golden_frame = 1;
332 cpi->refresh_alt_ref_frame = 1;
333 av1_zero(cpi->interp_filter_selected);
334 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700335 } else {
Thomas Daede51020e12017-12-14 20:12:44 -0800336 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
337 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
David Barkercc615a82018-03-19 14:38:51 +0000338 av1_setup_past_independence(cm);
339 cm->seg.update_map = 1;
340 cm->seg.update_data = 1;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700341 } else {
Thomas Daede51020e12017-12-14 20:12:44 -0800342 *cm->fc = cm->frame_contexts[cm->frame_refs[cm->primary_ref_frame].idx];
Thomas Daededa4d8b92017-06-05 15:44:14 -0700343 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700344 av1_zero(cpi->interp_filter_selected[0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345 }
346
Yue Chend90d3432018-03-16 11:28:42 -0700347 cm->prev_frame = get_prev_frame(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700348 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700349}
350
Cheng Chen46f30c72017-09-07 11:13:33 -0700351static void enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700352 int i;
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530353 int mi_rows_sb_aligned = calc_mi_size(cm->mi_rows);
Yunqing Wang19b9f722018-02-20 16:22:01 -0800354 cm->mi = cm->mip;
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530355 memset(cm->mip, 0, cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mip));
Yunqing Wang19b9f722018-02-20 16:22:01 -0800356 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700357 // Clear top border row
358 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
359 // Clear left border column
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530360 for (i = 0; i < mi_rows_sb_aligned; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700361 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yunqing Wang19b9f722018-02-20 16:22:01 -0800362 cm->mi_grid_visible = cm->mi_grid_base;
363 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364
365 memset(cm->mi_grid_base, 0,
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530366 cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi_grid_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700367}
368
Cheng Chen46f30c72017-09-07 11:13:33 -0700369static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700370 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700371 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700372 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700373 if (!cm->prev_mip) return 1;
374 cm->mi_alloc_size = mi_size;
375
Yue Chen53b53f02018-03-29 14:31:23 -0700376 cm->mi_grid_base =
377 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700378 if (!cm->mi_grid_base) return 1;
379 cm->prev_mi_grid_base =
Yue Chen53b53f02018-03-29 14:31:23 -0700380 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 if (!cm->prev_mi_grid_base) return 1;
382
383 return 0;
384}
385
Cheng Chen46f30c72017-09-07 11:13:33 -0700386static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700387 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700388 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700389 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700390 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700391 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700393 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700394 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700395 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700396}
397
Cheng Chen46f30c72017-09-07 11:13:33 -0700398static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700399 // Current mip will be the prev_mip for the next frame.
Yue Chen53b53f02018-03-29 14:31:23 -0700400 MB_MODE_INFO **temp_base = cm->prev_mi_grid_base;
401 MB_MODE_INFO *temp = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402 cm->prev_mip = cm->mip;
403 cm->mip = temp;
404
405 // Update the upper left visible macroblock ptrs.
Yunqing Wang19b9f722018-02-20 16:22:01 -0800406 cm->mi = cm->mip;
407 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700408
409 cm->prev_mi_grid_base = cm->mi_grid_base;
410 cm->mi_grid_base = temp_base;
Yunqing Wang19b9f722018-02-20 16:22:01 -0800411 cm->mi_grid_visible = cm->mi_grid_base;
412 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700413}
414
Yaowu Xuf883b422016-08-30 14:01:10 -0700415void av1_initialize_enc(void) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700416 static volatile int init_done = 0;
417
418 if (!init_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700419 av1_rtcd();
420 aom_dsp_rtcd();
421 aom_scale_rtcd();
422 av1_init_intra_predictors();
423 av1_init_me_luts();
424 av1_rc_init_minq_luts();
Yaowu Xuf883b422016-08-30 14:01:10 -0700425 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700426 init_done = 1;
427 }
428}
429
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700430static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
431 if (cpi->mbmi_ext_base) {
432 aom_free(cpi->mbmi_ext_base);
433 cpi->mbmi_ext_base = NULL;
434 }
435}
436
437static void alloc_context_buffers_ext(AV1_COMP *cpi) {
438 AV1_COMMON *cm = &cpi->common;
439 int mi_size = cm->mi_cols * cm->mi_rows;
440
441 dealloc_context_buffers_ext(cpi);
442 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
443 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
444}
445
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800446static void update_film_grain_parameters(struct AV1_COMP *cpi,
447 const AV1EncoderConfig *oxcf) {
448 AV1_COMMON *const cm = &cpi->common;
449 cpi->oxcf = *oxcf;
450
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700451 if (cpi->film_grain_table) {
452 aom_film_grain_table_free(cpi->film_grain_table);
453 aom_free(cpi->film_grain_table);
454 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700455 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700456
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800457 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700458 cm->seq_params.film_grain_params_present = 1;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800459 if (cm->frame_type == KEY_FRAME) {
460 memcpy(&cm->film_grain_params,
461 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
462 sizeof(cm->film_grain_params));
463
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700464 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
465 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800466 cm->film_grain_params.clip_to_restricted_range = 0;
467 }
468 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700469 } else if (oxcf->film_grain_table_filename) {
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700470 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
471 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700472
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700473 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700474 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800475 } else {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700476 cm->seq_params.film_grain_params_present = 0;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800477 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
478 }
479}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800480
Yaowu Xuf883b422016-08-30 14:01:10 -0700481static void dealloc_compressor_data(AV1_COMP *cpi) {
482 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000483 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700484
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700485 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700486
Yaowu Xuf883b422016-08-30 14:01:10 -0700487 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700488 cpi->tile_data = NULL;
489
490 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700491 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700492 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700493
Yaowu Xuf883b422016-08-30 14:01:10 -0700494 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700495 cpi->cyclic_refresh = NULL;
496
Yaowu Xuf883b422016-08-30 14:01:10 -0700497 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700498 cpi->active_map.map = NULL;
499
Jingning Hand064cf02017-06-01 10:00:39 -0700500 aom_free(cpi->td.mb.above_pred_buf);
501 cpi->td.mb.above_pred_buf = NULL;
502
503 aom_free(cpi->td.mb.left_pred_buf);
504 cpi->td.mb.left_pred_buf = NULL;
505
506 aom_free(cpi->td.mb.wsrc_buf);
507 cpi->td.mb.wsrc_buf = NULL;
508
509 aom_free(cpi->td.mb.mask_buf);
510 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700511
Jingning Han6cc1fd32017-10-13 09:05:36 -0700512 aom_free(cm->tpl_mvs);
513 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700514
Yaowu Xuf883b422016-08-30 14:01:10 -0700515 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700516 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700517 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700518
Yaowu Xuf883b422016-08-30 14:01:10 -0700519 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700520 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800521 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700522 aom_free_frame_buffer(&cpi->scaled_source);
523 aom_free_frame_buffer(&cpi->scaled_last_source);
524 aom_free_frame_buffer(&cpi->alt_ref_buffer);
525 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700526
Yaowu Xuf883b422016-08-30 14:01:10 -0700527 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700528 cpi->tile_tok[0][0] = 0;
529
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000530 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531
hui sud9a812b2017-07-06 14:34:37 -0700532 aom_free(cpi->td.mb.palette_buffer);
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700533
534#if CONFIG_DENOISE
535 if (cpi->denoise_and_model) {
536 aom_denoise_and_model_free(cpi->denoise_and_model);
537 cpi->denoise_and_model = NULL;
538 }
539#endif
540 if (cpi->film_grain_table) {
541 aom_film_grain_table_free(cpi->film_grain_table);
542 cpi->film_grain_table = NULL;
543 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700544}
545
Yaowu Xuf883b422016-08-30 14:01:10 -0700546static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700548 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700549
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200550 // Stores a snapshot of key state variables which can subsequently be
551 // restored with a call to av1_restore_coding_context. These functions are
552 // intended for use in a re-code loop in av1_compress_frame where the
553 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800554 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
555 av1_copy(cc->nmv_costs, cpi->nmv_costs);
556 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000557
Yaowu Xuc27fc142016-08-22 16:08:15 -0700558 cc->fc = *cm->fc;
559}
560
Yaowu Xuf883b422016-08-30 14:01:10 -0700561static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700562 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700563 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700564
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200565 // Restore key state variables to the snapshot state stored in the
566 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800567 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
568 av1_copy(cpi->nmv_costs, cc->nmv_costs);
569 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000570
Yaowu Xuc27fc142016-08-22 16:08:15 -0700571 *cm->fc = cc->fc;
572}
573
Yaowu Xuf883b422016-08-30 14:01:10 -0700574static void configure_static_seg_features(AV1_COMP *cpi) {
575 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700576 const RATE_CONTROL *const rc = &cpi->rc;
577 struct segmentation *const seg = &cm->seg;
578
579 int high_q = (int)(rc->avg_q > 48.0);
580 int qi_delta;
581
582 // Disable and clear down for KF
583 if (cm->frame_type == KEY_FRAME) {
584 // Clear down the global segmentation map
585 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
586 seg->update_map = 0;
587 seg->update_data = 0;
588 cpi->static_mb_pct = 0;
589
590 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700591 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700592
593 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700594 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700595 } else if (cpi->refresh_alt_ref_frame) {
596 // If this is an alt ref frame
597 // Clear down the global segmentation map
598 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
599 seg->update_map = 0;
600 seg->update_data = 0;
601 cpi->static_mb_pct = 0;
602
603 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700604 av1_disable_segmentation(seg);
605 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700606
607 // Scan frames from current to arf frame.
608 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700609 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610
611 // If segmentation was enabled set those features needed for the
612 // arf itself.
613 if (seg->enabled) {
614 seg->update_map = 1;
615 seg->update_data = 1;
616
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700617 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
618 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700619 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700620 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
621 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
622 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
623 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
624
625 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
626 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
627 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
628 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700629
Yaowu Xuf883b422016-08-30 14:01:10 -0700630 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700631 }
632 } else if (seg->enabled) {
633 // All other frames if segmentation has been enabled
634
635 // First normal frame in a valid gf or alt ref group
636 if (rc->frames_since_golden == 0) {
637 // Set up segment features for normal frames in an arf group
638 if (rc->source_alt_ref_active) {
639 seg->update_map = 0;
640 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700641
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700642 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
643 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700644 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
645 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700646
Cheng Chend8184da2017-09-26 18:15:22 -0700647 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
648 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
649 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
650 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
651
652 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
653 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
654 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
655 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700656
657 // Segment coding disabled for compred testing
658 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700659 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
660 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
661 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700662 }
663 } else {
664 // Disable segmentation and clear down features if alt ref
665 // is not active for this group
666
Yaowu Xuf883b422016-08-30 14:01:10 -0700667 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668
669 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
670
671 seg->update_map = 0;
672 seg->update_data = 0;
673
Yaowu Xuf883b422016-08-30 14:01:10 -0700674 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700675 }
676 } else if (rc->is_src_frame_alt_ref) {
677 // Special case where we are coding over the top of a previous
678 // alt ref frame.
679 // Segment coding disabled for compred testing
680
681 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700682 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
683 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700684
685 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700686 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
687 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
688 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
689 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690
691 // Skip all MBs if high Q (0,0 mv and skip coeffs)
692 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700693 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
694 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700695 }
696 // Enable data update
697 seg->update_data = 1;
698 } else {
699 // All other frames.
700
701 // No updates.. leave things as they are.
702 seg->update_map = 0;
703 seg->update_data = 0;
704 }
705 }
706}
707
Yaowu Xuf883b422016-08-30 14:01:10 -0700708static void update_reference_segmentation_map(AV1_COMP *cpi) {
709 AV1_COMMON *const cm = &cpi->common;
Yushin Choa7f65922018-04-04 16:06:11 -0700710 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
Soo-Chul Han934af352017-10-15 15:21:51 -0400711 uint8_t *cache_ptr = cm->current_frame_seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700712 int row, col;
713
714 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700715 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700716 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700717 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
718 cache[0] = mi_4x4[0]->segment_id;
719 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700720 cache_ptr += cm->mi_cols;
721 }
722}
723
Yaowu Xuf883b422016-08-30 14:01:10 -0700724static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
725 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700726 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700727 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728
729 if (!cpi->lookahead)
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700730 cpi->lookahead =
731 av1_lookahead_init(oxcf->width, oxcf->height, seq_params->subsampling_x,
732 seq_params->subsampling_y,
733 seq_params->use_highbitdepth, oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700734 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736 "Failed to allocate lag buffers");
737
738 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700739 if (aom_realloc_frame_buffer(
740 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
741 seq_params->subsampling_x, seq_params->subsampling_y,
742 seq_params->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 altref buffer");
746}
747
Yaowu Xuf883b422016-08-30 14:01:10 -0700748static void alloc_util_frame_buffers(AV1_COMP *cpi) {
749 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700750 const SequenceHeader *const seq_params = &cm->seq_params;
751 if (aom_realloc_frame_buffer(
752 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
753 seq_params->subsampling_y, seq_params->use_highbitdepth,
754 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700756 "Failed to allocate last frame buffer");
757
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700758 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800759 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700760 cm->superres_upscaled_height, seq_params->subsampling_x,
761 seq_params->subsampling_y, seq_params->use_highbitdepth,
762 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800763 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800764 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700766 if (aom_realloc_frame_buffer(
767 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
768 seq_params->subsampling_y, seq_params->use_highbitdepth,
769 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700770 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771 "Failed to allocate scaled source buffer");
772
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700773 if (aom_realloc_frame_buffer(
774 &cpi->scaled_last_source, cm->width, cm->height,
775 seq_params->subsampling_x, seq_params->subsampling_y,
776 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
777 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700778 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700779 "Failed to allocate scaled last source buffer");
780}
781
Cheng Chen46f30c72017-09-07 11:13:33 -0700782static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700783 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000784 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700785
Yaowu Xuf883b422016-08-30 14:01:10 -0700786 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700787
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700788 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700789
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790 alloc_context_buffers_ext(cpi);
791
Yaowu Xuf883b422016-08-30 14:01:10 -0700792 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793
794 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000795 unsigned int tokens =
796 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700798 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700799 }
800
Yaowu Xuf883b422016-08-30 14:01:10 -0700801 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802}
803
Yaowu Xuf883b422016-08-30 14:01:10 -0700804void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700806 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700807}
808
Yunqing Wang75e20e82018-06-16 12:10:48 -0700809static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200810 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200811 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200812
813 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200814
815 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200816 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200817 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200818 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
819 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200820 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000821 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
822 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200823 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200824 cm->uniform_tile_spacing_flag = 0;
825 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
826 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200827 size_sb = cpi->oxcf.tile_widths[j++];
828 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000829 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200830 }
831 cm->tile_cols = i;
832 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200833 }
834 av1_calculate_tile_cols(cm);
835
836 // configure tile rows
837 if (cm->uniform_tile_spacing_flag) {
838 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
839 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200840 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000841 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
842 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200843 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200844 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
845 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200846 size_sb = cpi->oxcf.tile_heights[j++];
847 if (j >= cpi->oxcf.tile_height_count) j = 0;
848 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200849 }
850 cm->tile_rows = i;
851 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200852 }
853 av1_calculate_tile_rows(cm);
854}
855
Yaowu Xuf883b422016-08-30 14:01:10 -0700856static void update_frame_size(AV1_COMP *cpi) {
857 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700858 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
859
Yaowu Xuf883b422016-08-30 14:01:10 -0700860 av1_set_mb_mi(cm, cm->width, cm->height);
861 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500862 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863 memset(cpi->mbmi_ext_base, 0,
864 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865 set_tile_info(cpi);
866}
867
Yaowu Xuf883b422016-08-30 14:01:10 -0700868static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700869 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700870 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
871 cpi->ref_fb_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700872 cpi->rate_index = 0;
873 cpi->rate_size = 0;
874 cpi->cur_poc = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875}
876
Debargha Mukherjee57498692018-05-11 13:29:31 -0700877static INLINE int does_level_match(int width, int height, double fps,
878 int lvl_width, int lvl_height,
879 double lvl_fps, int lvl_dim_mult) {
880 const int64_t lvl_luma_pels = lvl_width * lvl_height;
881 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
882 const int64_t luma_pels = width * height;
883 const double display_sample_rate = luma_pels * fps;
884 return luma_pels <= lvl_luma_pels &&
885 display_sample_rate <= lvl_display_sample_rate &&
886 width <= lvl_width * lvl_dim_mult &&
887 height <= lvl_height * lvl_dim_mult;
888}
889
Andrey Norkin26495512018-06-20 17:13:11 -0700890static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -0700891 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700892 // TODO(any): This is a placeholder function that only addresses dimensions
893 // and max display sample rates.
894 // Need to add checks for max bit rate, max decoded luma sample rate, header
895 // rate, etc. that are not covered by this function.
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700896 (void)oxcf;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700897 BitstreamLevel bl = { 9, 3 };
898 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
899 288, 30.0, 4)) {
900 bl.major = 2;
901 bl.minor = 0;
902 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
903 704, 396, 30.0, 4)) {
904 bl.major = 2;
905 bl.minor = 1;
906 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
907 1088, 612, 30.0, 4)) {
908 bl.major = 3;
909 bl.minor = 0;
910 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
911 1376, 774, 30.0, 4)) {
912 bl.major = 3;
913 bl.minor = 1;
914 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
915 2048, 1152, 30.0, 3)) {
916 bl.major = 4;
917 bl.minor = 0;
918 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
919 2048, 1152, 60.0, 3)) {
920 bl.major = 4;
921 bl.minor = 1;
922 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
923 4096, 2176, 30.0, 2)) {
924 bl.major = 5;
925 bl.minor = 0;
926 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
927 4096, 2176, 60.0, 2)) {
928 bl.major = 5;
929 bl.minor = 1;
930 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
931 4096, 2176, 120.0, 2)) {
932 bl.major = 5;
933 bl.minor = 2;
934 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
935 8192, 4352, 30.0, 2)) {
936 bl.major = 6;
937 bl.minor = 0;
938 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
939 8192, 4352, 60.0, 2)) {
940 bl.major = 6;
941 bl.minor = 1;
942 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
943 8192, 4352, 120.0, 2)) {
944 bl.major = 6;
945 bl.minor = 2;
946 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
947 16384, 8704, 30.0, 2)) {
948 bl.major = 7;
949 bl.minor = 0;
950 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
951 16384, 8704, 60.0, 2)) {
952 bl.major = 7;
953 bl.minor = 1;
954 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
955 16384, 8704, 120.0, 2)) {
956 bl.major = 7;
957 bl.minor = 2;
958 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700959 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700960 seq->level[i] = bl;
Andrey Norkinf481d982018-05-15 12:05:31 -0700961 seq->tier[i] = 0; // setting main tier by default
Andrey Norkin26495512018-06-20 17:13:11 -0700962 // Set the maximum parameters for bitrate and buffer size for this profile,
963 // level, and tier
964 cm->op_params[i].bitrate = max_level_bitrate(
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700965 cm->seq_params.profile, major_minor_to_seq_level_idx(seq->level[i]),
966 seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -0700967 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
968 // check
Andrey Norkin26495512018-06-20 17:13:11 -0700969 if (cm->op_params[i].bitrate == 0)
970 aom_internal_error(
971 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
972 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -0700973 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -0700974 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700975 }
976}
977
Andrey Norkin26495512018-06-20 17:13:11 -0700978static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
979 const AV1EncoderConfig *oxcf) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700980 seq->still_picture = (oxcf->limit == 1);
981 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -0700982 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700983 seq->force_screen_content_tools = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700984 seq->force_integer_mv = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700985 seq->enable_order_hint = oxcf->enable_order_hint;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700986 seq->frame_id_numbers_present_flag = oxcf->large_scale_tile;
987 if (seq->still_picture && seq->reduced_still_picture_hdr) {
988 seq->enable_order_hint = 0;
989 seq->frame_id_numbers_present_flag = 0;
990 seq->force_screen_content_tools = 2;
991 seq->force_integer_mv = 2;
992 }
Wan-Teh Chang69582972018-05-15 13:18:52 -0700993 seq->order_hint_bits_minus_1 =
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700994 seq->enable_order_hint ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1 : -1;
995
996 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -0700997 seq->enable_jnt_comp = oxcf->enable_jnt_comp;
998 seq->enable_jnt_comp &= seq->enable_order_hint;
999 seq->enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1000 seq->enable_ref_frame_mvs &= seq->enable_order_hint;
1001 seq->enable_superres = oxcf->enable_superres;
1002 seq->enable_cdef = oxcf->enable_cdef;
1003 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001004 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee97095fb2018-03-26 07:51:48 -07001005 seq->enable_interintra_compound = 1;
1006 seq->enable_masked_compound = 1;
Debargha Mukherjee365eae82018-03-26 19:19:55 -07001007 seq->enable_intra_edge_filter = 1;
1008 seq->enable_filter_intra = 1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001009
Andrey Norkin26495512018-06-20 17:13:11 -07001010 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001011
1012 if (seq->operating_points_cnt_minus_1 == 0) {
1013 seq->operating_point_idc[0] = 0;
1014 } else {
1015 // Set operating_point_idc[] such that for the i-th operating point the
1016 // first (operating_points_cnt-i) spatial layers and the first temporal
1017 // layer are decoded Note that highest quality operating point should come
1018 // first
1019 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1020 seq->operating_point_idc[i] =
1021 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1022 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001023}
1024
Yaowu Xuf883b422016-08-30 14:01:10 -07001025static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1026 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001027
1028 cpi->oxcf = *oxcf;
1029 cpi->framerate = oxcf->init_framerate;
1030
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001031 cm->seq_params.profile = oxcf->profile;
1032 cm->seq_params.bit_depth = oxcf->bit_depth;
1033 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1034 cm->seq_params.color_primaries = oxcf->color_primaries;
1035 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1036 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001037 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001038 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1039 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001040 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001041 cm->timing_info.num_units_in_display_tick =
1042 oxcf->timing_info.num_units_in_display_tick;
1043 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1044 cm->timing_info.equal_picture_interval =
1045 oxcf->timing_info.equal_picture_interval;
1046 cm->timing_info.num_ticks_per_picture =
1047 oxcf->timing_info.num_ticks_per_picture;
1048
Andrey Norkin26495512018-06-20 17:13:11 -07001049 cm->seq_params.display_model_info_present_flag =
1050 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001051 cm->seq_params.decoder_model_info_present_flag =
1052 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001053 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001054 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001055 cm->buffer_model.num_units_in_decoding_tick =
1056 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001057 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08001058 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07001059 set_dec_model_op_parameters(&cm->op_params[0]);
1060 } else if (cm->timing_info_present &&
1061 cm->timing_info.equal_picture_interval &&
1062 !cm->seq_params.decoder_model_info_present_flag) {
1063 // set the decoder model parameters in resource availability mode
1064 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001065 } else {
1066 cm->op_params[0].initial_display_delay =
1067 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001068 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001069
Yaowu Xuc27fc142016-08-22 16:08:15 -07001070 cm->width = oxcf->width;
1071 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001072 set_sb_size(&cm->seq_params,
1073 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001074 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001075
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001076 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001077
1078 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001079 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001080
1081 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001082 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001083
1084 cpi->static_mb_pct = 0;
1085 cpi->ref_frame_flags = 0;
1086
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001087 // Reset resize pending flags
1088 cpi->resize_pending_width = 0;
1089 cpi->resize_pending_height = 0;
1090
Yaowu Xuc27fc142016-08-22 16:08:15 -07001091 init_buffer_indices(cpi);
1092}
1093
1094static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001095 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001096 const int64_t bandwidth = oxcf->target_bandwidth;
1097 const int64_t starting = oxcf->starting_buffer_level_ms;
1098 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1099 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1100
1101 rc->starting_buffer_level = starting * bandwidth / 1000;
1102 rc->optimal_buffer_level =
1103 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1104 rc->maximum_buffer_size =
1105 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1106}
1107
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001108#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1109 cpi->fn_ptr[BT].sdf = SDF; \
1110 cpi->fn_ptr[BT].sdaf = SDAF; \
1111 cpi->fn_ptr[BT].vf = VF; \
1112 cpi->fn_ptr[BT].svf = SVF; \
1113 cpi->fn_ptr[BT].svaf = SVAF; \
1114 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1115 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001116 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001117
1118#define MAKE_BFP_SAD_WRAPPER(fnname) \
1119 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1120 int source_stride, \
1121 const uint8_t *ref_ptr, int ref_stride) { \
1122 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1123 } \
1124 static unsigned int fnname##_bits10( \
1125 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1126 int ref_stride) { \
1127 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1128 } \
1129 static unsigned int fnname##_bits12( \
1130 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1131 int ref_stride) { \
1132 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1133 }
1134
1135#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1136 static unsigned int fnname##_bits8( \
1137 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1138 int ref_stride, const uint8_t *second_pred) { \
1139 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1140 } \
1141 static unsigned int fnname##_bits10( \
1142 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1143 int ref_stride, const uint8_t *second_pred) { \
1144 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1145 2; \
1146 } \
1147 static unsigned int fnname##_bits12( \
1148 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1149 int ref_stride, const uint8_t *second_pred) { \
1150 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1151 4; \
1152 }
1153
Yaowu Xuc27fc142016-08-22 16:08:15 -07001154#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1155 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1156 const uint8_t *const ref_ptr[], int ref_stride, \
1157 unsigned int *sad_array) { \
1158 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1159 } \
1160 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1161 const uint8_t *const ref_ptr[], int ref_stride, \
1162 unsigned int *sad_array) { \
1163 int i; \
1164 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1165 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1166 } \
1167 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1168 const uint8_t *const ref_ptr[], int ref_stride, \
1169 unsigned int *sad_array) { \
1170 int i; \
1171 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1172 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1173 }
1174
Cheng Chenbf3d4962017-11-01 14:48:52 -07001175#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1176 static unsigned int fnname##_bits8( \
1177 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1178 int ref_stride, const uint8_t *second_pred, \
1179 const JNT_COMP_PARAMS *jcp_param) { \
1180 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1181 jcp_param); \
1182 } \
1183 static unsigned int fnname##_bits10( \
1184 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1185 int ref_stride, const uint8_t *second_pred, \
1186 const JNT_COMP_PARAMS *jcp_param) { \
1187 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1188 jcp_param) >> \
1189 2; \
1190 } \
1191 static unsigned int fnname##_bits12( \
1192 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1193 int ref_stride, const uint8_t *second_pred, \
1194 const JNT_COMP_PARAMS *jcp_param) { \
1195 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1196 jcp_param) >> \
1197 4; \
1198 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001199
Yaowu Xuf883b422016-08-30 14:01:10 -07001200MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1201MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001202MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1203MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1204MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1205MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1206MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1207MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1208MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001209MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1210MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1211MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1212MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1213MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1214MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1215MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1216MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1217MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1218MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1219MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1220MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1221MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1222MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001223MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1224MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1225MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001226MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1227MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1228MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001229MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1230MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1231MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001232MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1233MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1234MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001235MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1236MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1237MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001238MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1239MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1240MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001241MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1242MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1243MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001244MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1245MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1246MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001247MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001248
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001249MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1250MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1251MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1252MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1253MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1254MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1255MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1256MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1257MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1258MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1259MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1260MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001261MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1262MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1263MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1264MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1265MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1266MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001267
Cheng Chenbf3d4962017-11-01 14:48:52 -07001268MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x128_avg)
1269MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x64_avg)
1270MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x128_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001271MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x16_avg)
1272MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x32_avg)
1273MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x32_avg)
1274MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x64_avg)
1275MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x32_avg)
1276MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x64_avg)
1277MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x16_avg)
1278MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x8_avg)
1279MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x16_avg)
1280MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x8_avg)
1281MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x4_avg)
1282MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x8_avg)
1283MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x4_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001284MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x16_avg)
1285MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x4_avg)
1286MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x32_avg)
1287MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x8_avg)
1288MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x64_avg)
1289MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001290
David Barker0f3c94e2017-05-16 15:21:50 +01001291#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001292 cpi->fn_ptr[BT].msdf = MCSDF; \
1293 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001294
David Barkerc155e012017-05-11 13:54:54 +01001295#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1296 static unsigned int fnname##_bits8( \
1297 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1298 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1299 int m_stride, int invert_mask) { \
1300 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1301 second_pred_ptr, m, m_stride, invert_mask); \
1302 } \
1303 static unsigned int fnname##_bits10( \
1304 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1305 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1306 int m_stride, int invert_mask) { \
1307 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1308 second_pred_ptr, m, m_stride, invert_mask) >> \
1309 2; \
1310 } \
1311 static unsigned int fnname##_bits12( \
1312 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1313 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1314 int m_stride, int invert_mask) { \
1315 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1316 second_pred_ptr, m, m_stride, invert_mask) >> \
1317 4; \
1318 }
1319
David Barkerf19f35f2017-05-22 16:33:22 +01001320MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1321MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1322MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001323MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1324MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1325MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1326MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1327MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1328MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1329MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1330MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1331MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1332MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1333MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1334MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1335MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001336MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1337MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1338MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1339MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001340MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1341MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001342
Yaowu Xuc27fc142016-08-22 16:08:15 -07001343#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1344 cpi->fn_ptr[BT].osdf = OSDF; \
1345 cpi->fn_ptr[BT].ovf = OVF; \
1346 cpi->fn_ptr[BT].osvf = OSVF;
1347
1348#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1349 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1350 const int32_t *wsrc, \
1351 const int32_t *msk) { \
1352 return fnname(ref, ref_stride, wsrc, msk); \
1353 } \
1354 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1355 const int32_t *wsrc, \
1356 const int32_t *msk) { \
1357 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1358 } \
1359 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1360 const int32_t *wsrc, \
1361 const int32_t *msk) { \
1362 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1363 }
1364
Yaowu Xuf883b422016-08-30 14:01:10 -07001365MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1366MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1367MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001368MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1369MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1370MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1371MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1372MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1373MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1374MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1375MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1376MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1377MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1378MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1379MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1380MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001381MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1382MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1383MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1384MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001385MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1386MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001387
Yaowu Xuf883b422016-08-30 14:01:10 -07001388static void highbd_set_var_fns(AV1_COMP *const cpi) {
1389 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001390 if (cm->seq_params.use_highbitdepth) {
1391 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001392 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001393 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1394 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1395 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001396 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001397 aom_highbd_sad64x16x4d_bits8,
1398 aom_highbd_jnt_sad64x16_avg_bits8,
1399 aom_highbd_8_jnt_sub_pixel_avg_variance64x16)
1400
1401 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1402 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1403 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001404 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001405 aom_highbd_sad16x64x4d_bits8,
1406 aom_highbd_jnt_sad16x64_avg_bits8,
1407 aom_highbd_8_jnt_sub_pixel_avg_variance16x64)
1408
1409 HIGHBD_BFP(
1410 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1411 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001412 aom_highbd_8_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001413 aom_highbd_sad32x8x4d_bits8, aom_highbd_jnt_sad32x8_avg_bits8,
1414 aom_highbd_8_jnt_sub_pixel_avg_variance32x8)
1415
1416 HIGHBD_BFP(
1417 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1418 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001419 aom_highbd_8_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001420 aom_highbd_sad8x32x4d_bits8, aom_highbd_jnt_sad8x32_avg_bits8,
1421 aom_highbd_8_jnt_sub_pixel_avg_variance8x32)
1422
1423 HIGHBD_BFP(
1424 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1425 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001426 aom_highbd_8_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001427 aom_highbd_sad16x4x4d_bits8, aom_highbd_jnt_sad16x4_avg_bits8,
1428 aom_highbd_8_jnt_sub_pixel_avg_variance16x4)
1429
1430 HIGHBD_BFP(
1431 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1432 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001433 aom_highbd_8_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001434 aom_highbd_sad4x16x4d_bits8, aom_highbd_jnt_sad4x16_avg_bits8,
1435 aom_highbd_8_jnt_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001436
1437 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1438 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1439 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001440 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001441 aom_highbd_sad32x16x4d_bits8,
1442 aom_highbd_jnt_sad32x16_avg_bits8,
1443 aom_highbd_8_jnt_sub_pixel_avg_variance32x16)
1444
1445 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1446 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1447 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001448 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001449 aom_highbd_sad16x32x4d_bits8,
1450 aom_highbd_jnt_sad16x32_avg_bits8,
1451 aom_highbd_8_jnt_sub_pixel_avg_variance16x32)
1452
1453 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1454 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1455 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001456 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001457 aom_highbd_sad64x32x4d_bits8,
1458 aom_highbd_jnt_sad64x32_avg_bits8,
1459 aom_highbd_8_jnt_sub_pixel_avg_variance64x32)
1460
1461 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1462 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1463 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001464 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001465 aom_highbd_sad32x64x4d_bits8,
1466 aom_highbd_jnt_sad32x64_avg_bits8,
1467 aom_highbd_8_jnt_sub_pixel_avg_variance32x64)
1468
1469 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1470 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1471 aom_highbd_8_sub_pixel_variance32x32,
1472 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001473 aom_highbd_sad32x32x4d_bits8,
1474 aom_highbd_jnt_sad32x32_avg_bits8,
1475 aom_highbd_8_jnt_sub_pixel_avg_variance32x32)
1476
1477 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1478 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1479 aom_highbd_8_sub_pixel_variance64x64,
1480 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001481 aom_highbd_sad64x64x4d_bits8,
1482 aom_highbd_jnt_sad64x64_avg_bits8,
1483 aom_highbd_8_jnt_sub_pixel_avg_variance64x64)
1484
1485 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1486 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1487 aom_highbd_8_sub_pixel_variance16x16,
1488 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001489 aom_highbd_sad16x16x4d_bits8,
1490 aom_highbd_jnt_sad16x16_avg_bits8,
1491 aom_highbd_8_jnt_sub_pixel_avg_variance16x16)
1492
1493 HIGHBD_BFP(
1494 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1495 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001496 aom_highbd_8_sub_pixel_avg_variance16x8,
1497 aom_highbd_sad16x8x4d_bits8, aom_highbd_jnt_sad16x8_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001498 aom_highbd_8_jnt_sub_pixel_avg_variance16x8)
1499
1500 HIGHBD_BFP(
1501 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1502 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001503 aom_highbd_8_sub_pixel_avg_variance8x16,
1504 aom_highbd_sad8x16x4d_bits8, aom_highbd_jnt_sad8x16_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001505 aom_highbd_8_jnt_sub_pixel_avg_variance8x16)
1506
1507 HIGHBD_BFP(BLOCK_8X8, aom_highbd_sad8x8_bits8,
1508 aom_highbd_sad8x8_avg_bits8, aom_highbd_8_variance8x8,
1509 aom_highbd_8_sub_pixel_variance8x8,
1510 aom_highbd_8_sub_pixel_avg_variance8x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001511 aom_highbd_sad8x8x4d_bits8, aom_highbd_jnt_sad8x8_avg_bits8,
1512 aom_highbd_8_jnt_sub_pixel_avg_variance8x8)
1513
1514 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1515 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1516 aom_highbd_8_sub_pixel_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001517 aom_highbd_8_sub_pixel_avg_variance8x4,
1518 aom_highbd_sad8x4x4d_bits8, aom_highbd_jnt_sad8x4_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001519 aom_highbd_8_jnt_sub_pixel_avg_variance8x4)
1520
1521 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1522 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1523 aom_highbd_8_sub_pixel_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001524 aom_highbd_8_sub_pixel_avg_variance4x8,
1525 aom_highbd_sad4x8x4d_bits8, aom_highbd_jnt_sad4x8_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001526 aom_highbd_8_jnt_sub_pixel_avg_variance4x8)
1527
1528 HIGHBD_BFP(BLOCK_4X4, aom_highbd_sad4x4_bits8,
1529 aom_highbd_sad4x4_avg_bits8, aom_highbd_8_variance4x4,
1530 aom_highbd_8_sub_pixel_variance4x4,
1531 aom_highbd_8_sub_pixel_avg_variance4x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001532 aom_highbd_sad4x4x4d_bits8, aom_highbd_jnt_sad4x4_avg_bits8,
1533 aom_highbd_8_jnt_sub_pixel_avg_variance4x4)
1534
Cheng Chenbf3d4962017-11-01 14:48:52 -07001535 HIGHBD_BFP(
1536 BLOCK_128X128, aom_highbd_sad128x128_bits8,
1537 aom_highbd_sad128x128_avg_bits8, aom_highbd_8_variance128x128,
1538 aom_highbd_8_sub_pixel_variance128x128,
1539 aom_highbd_8_sub_pixel_avg_variance128x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001540 aom_highbd_sad128x128x4d_bits8, aom_highbd_jnt_sad128x128_avg_bits8,
1541 aom_highbd_8_jnt_sub_pixel_avg_variance128x128)
1542
1543 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1544 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1545 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001546 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001547 aom_highbd_sad128x64x4d_bits8,
1548 aom_highbd_jnt_sad128x64_avg_bits8,
1549 aom_highbd_8_jnt_sub_pixel_avg_variance128x64)
1550
1551 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1552 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1553 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001554 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001555 aom_highbd_sad64x128x4d_bits8,
1556 aom_highbd_jnt_sad64x128_avg_bits8,
1557 aom_highbd_8_jnt_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001558
David Barkerf19f35f2017-05-22 16:33:22 +01001559 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1560 aom_highbd_8_masked_sub_pixel_variance128x128)
1561 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1562 aom_highbd_8_masked_sub_pixel_variance128x64)
1563 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1564 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001565 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1566 aom_highbd_8_masked_sub_pixel_variance64x64)
1567 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1568 aom_highbd_8_masked_sub_pixel_variance64x32)
1569 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1570 aom_highbd_8_masked_sub_pixel_variance32x64)
1571 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1572 aom_highbd_8_masked_sub_pixel_variance32x32)
1573 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1574 aom_highbd_8_masked_sub_pixel_variance32x16)
1575 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1576 aom_highbd_8_masked_sub_pixel_variance16x32)
1577 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1578 aom_highbd_8_masked_sub_pixel_variance16x16)
1579 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1580 aom_highbd_8_masked_sub_pixel_variance8x16)
1581 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1582 aom_highbd_8_masked_sub_pixel_variance16x8)
1583 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1584 aom_highbd_8_masked_sub_pixel_variance8x8)
1585 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1586 aom_highbd_8_masked_sub_pixel_variance4x8)
1587 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1588 aom_highbd_8_masked_sub_pixel_variance8x4)
1589 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1590 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001591 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1592 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001593 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1594 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001595 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1596 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001597 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1598 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001599 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1600 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001601 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1602 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001603 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1604 aom_highbd_obmc_variance128x128,
1605 aom_highbd_obmc_sub_pixel_variance128x128)
1606 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1607 aom_highbd_obmc_variance128x64,
1608 aom_highbd_obmc_sub_pixel_variance128x64)
1609 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1610 aom_highbd_obmc_variance64x128,
1611 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001612 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1613 aom_highbd_obmc_variance64x64,
1614 aom_highbd_obmc_sub_pixel_variance64x64)
1615 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1616 aom_highbd_obmc_variance64x32,
1617 aom_highbd_obmc_sub_pixel_variance64x32)
1618 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1619 aom_highbd_obmc_variance32x64,
1620 aom_highbd_obmc_sub_pixel_variance32x64)
1621 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1622 aom_highbd_obmc_variance32x32,
1623 aom_highbd_obmc_sub_pixel_variance32x32)
1624 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1625 aom_highbd_obmc_variance32x16,
1626 aom_highbd_obmc_sub_pixel_variance32x16)
1627 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1628 aom_highbd_obmc_variance16x32,
1629 aom_highbd_obmc_sub_pixel_variance16x32)
1630 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1631 aom_highbd_obmc_variance16x16,
1632 aom_highbd_obmc_sub_pixel_variance16x16)
1633 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1634 aom_highbd_obmc_variance8x16,
1635 aom_highbd_obmc_sub_pixel_variance8x16)
1636 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1637 aom_highbd_obmc_variance16x8,
1638 aom_highbd_obmc_sub_pixel_variance16x8)
1639 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1640 aom_highbd_obmc_variance8x8,
1641 aom_highbd_obmc_sub_pixel_variance8x8)
1642 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1643 aom_highbd_obmc_variance4x8,
1644 aom_highbd_obmc_sub_pixel_variance4x8)
1645 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1646 aom_highbd_obmc_variance8x4,
1647 aom_highbd_obmc_sub_pixel_variance8x4)
1648 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1649 aom_highbd_obmc_variance4x4,
1650 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001651 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1652 aom_highbd_obmc_variance64x16,
1653 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001654 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1655 aom_highbd_obmc_variance16x64,
1656 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001657 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1658 aom_highbd_obmc_variance32x8,
1659 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001660 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1661 aom_highbd_obmc_variance8x32,
1662 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001663 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1664 aom_highbd_obmc_variance16x4,
1665 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001666 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1667 aom_highbd_obmc_variance4x16,
1668 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001669 break;
1670
Yaowu Xuf883b422016-08-30 14:01:10 -07001671 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001672 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1673 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1674 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001675 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001676 aom_highbd_sad64x16x4d_bits10,
1677 aom_highbd_jnt_sad64x16_avg_bits10,
1678 aom_highbd_10_jnt_sub_pixel_avg_variance64x16);
1679
1680 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1681 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1682 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001683 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001684 aom_highbd_sad16x64x4d_bits10,
1685 aom_highbd_jnt_sad16x64_avg_bits10,
1686 aom_highbd_10_jnt_sub_pixel_avg_variance16x64);
1687
1688 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1689 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1690 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001691 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001692 aom_highbd_sad32x8x4d_bits10,
1693 aom_highbd_jnt_sad32x8_avg_bits10,
1694 aom_highbd_10_jnt_sub_pixel_avg_variance32x8);
1695
1696 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1697 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1698 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001699 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001700 aom_highbd_sad8x32x4d_bits10,
1701 aom_highbd_jnt_sad8x32_avg_bits10,
1702 aom_highbd_10_jnt_sub_pixel_avg_variance8x32);
1703
1704 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1705 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1706 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001707 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001708 aom_highbd_sad16x4x4d_bits10,
1709 aom_highbd_jnt_sad16x4_avg_bits10,
1710 aom_highbd_10_jnt_sub_pixel_avg_variance16x4);
1711
1712 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1713 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1714 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001715 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001716 aom_highbd_sad4x16x4d_bits10,
1717 aom_highbd_jnt_sad4x16_avg_bits10,
1718 aom_highbd_10_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001719
1720 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1721 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1722 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001723 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001724 aom_highbd_sad32x16x4d_bits10,
1725 aom_highbd_jnt_sad32x16_avg_bits10,
1726 aom_highbd_10_jnt_sub_pixel_avg_variance32x16);
1727
1728 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1729 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1730 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001731 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001732 aom_highbd_sad16x32x4d_bits10,
1733 aom_highbd_jnt_sad16x32_avg_bits10,
1734 aom_highbd_10_jnt_sub_pixel_avg_variance16x32);
1735
1736 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1737 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1738 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001739 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001740 aom_highbd_sad64x32x4d_bits10,
1741 aom_highbd_jnt_sad64x32_avg_bits10,
1742 aom_highbd_10_jnt_sub_pixel_avg_variance64x32);
1743
1744 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1745 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1746 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001747 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001748 aom_highbd_sad32x64x4d_bits10,
1749 aom_highbd_jnt_sad32x64_avg_bits10,
1750 aom_highbd_10_jnt_sub_pixel_avg_variance32x64);
1751
1752 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1753 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1754 aom_highbd_10_sub_pixel_variance32x32,
1755 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001756 aom_highbd_sad32x32x4d_bits10,
1757 aom_highbd_jnt_sad32x32_avg_bits10,
1758 aom_highbd_10_jnt_sub_pixel_avg_variance32x32);
1759
1760 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1761 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1762 aom_highbd_10_sub_pixel_variance64x64,
1763 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001764 aom_highbd_sad64x64x4d_bits10,
1765 aom_highbd_jnt_sad64x64_avg_bits10,
1766 aom_highbd_10_jnt_sub_pixel_avg_variance64x64);
1767
1768 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1769 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1770 aom_highbd_10_sub_pixel_variance16x16,
1771 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001772 aom_highbd_sad16x16x4d_bits10,
1773 aom_highbd_jnt_sad16x16_avg_bits10,
1774 aom_highbd_10_jnt_sub_pixel_avg_variance16x16);
1775
1776 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1777 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1778 aom_highbd_10_sub_pixel_variance16x8,
1779 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001780 aom_highbd_sad16x8x4d_bits10,
1781 aom_highbd_jnt_sad16x8_avg_bits10,
1782 aom_highbd_10_jnt_sub_pixel_avg_variance16x8);
1783
1784 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1785 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1786 aom_highbd_10_sub_pixel_variance8x16,
1787 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001788 aom_highbd_sad8x16x4d_bits10,
1789 aom_highbd_jnt_sad8x16_avg_bits10,
1790 aom_highbd_10_jnt_sub_pixel_avg_variance8x16);
1791
1792 HIGHBD_BFP(
1793 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1794 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001795 aom_highbd_10_sub_pixel_avg_variance8x8,
1796 aom_highbd_sad8x8x4d_bits10, aom_highbd_jnt_sad8x8_avg_bits10,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001797 aom_highbd_10_jnt_sub_pixel_avg_variance8x8);
1798
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001799 HIGHBD_BFP(
1800 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1801 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1802 aom_highbd_10_sub_pixel_avg_variance8x4,
1803 aom_highbd_sad8x4x4d_bits10, aom_highbd_jnt_sad8x4_avg_bits10,
1804 aom_highbd_10_jnt_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001805
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001806 HIGHBD_BFP(
1807 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1808 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1809 aom_highbd_10_sub_pixel_avg_variance4x8,
1810 aom_highbd_sad4x8x4d_bits10, aom_highbd_jnt_sad4x8_avg_bits10,
1811 aom_highbd_10_jnt_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001812
1813 HIGHBD_BFP(
1814 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1815 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001816 aom_highbd_10_sub_pixel_avg_variance4x4,
1817 aom_highbd_sad4x4x4d_bits10, aom_highbd_jnt_sad4x4_avg_bits10,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001818 aom_highbd_10_jnt_sub_pixel_avg_variance4x4);
1819
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001820 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1821 aom_highbd_sad128x128_avg_bits10,
1822 aom_highbd_10_variance128x128,
1823 aom_highbd_10_sub_pixel_variance128x128,
1824 aom_highbd_10_sub_pixel_avg_variance128x128,
1825 aom_highbd_sad128x128x4d_bits10,
1826 aom_highbd_jnt_sad128x128_avg_bits10,
1827 aom_highbd_10_jnt_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001828
1829 HIGHBD_BFP(
1830 BLOCK_128X64, aom_highbd_sad128x64_bits10,
1831 aom_highbd_sad128x64_avg_bits10, aom_highbd_10_variance128x64,
1832 aom_highbd_10_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001833 aom_highbd_10_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001834 aom_highbd_sad128x64x4d_bits10, aom_highbd_jnt_sad128x64_avg_bits10,
1835 aom_highbd_10_jnt_sub_pixel_avg_variance128x64);
1836
1837 HIGHBD_BFP(
1838 BLOCK_64X128, aom_highbd_sad64x128_bits10,
1839 aom_highbd_sad64x128_avg_bits10, aom_highbd_10_variance64x128,
1840 aom_highbd_10_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001841 aom_highbd_10_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001842 aom_highbd_sad64x128x4d_bits10, aom_highbd_jnt_sad64x128_avg_bits10,
1843 aom_highbd_10_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001844
David Barkerf19f35f2017-05-22 16:33:22 +01001845 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1846 aom_highbd_10_masked_sub_pixel_variance128x128)
1847 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1848 aom_highbd_10_masked_sub_pixel_variance128x64)
1849 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1850 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001851 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1852 aom_highbd_10_masked_sub_pixel_variance64x64)
1853 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1854 aom_highbd_10_masked_sub_pixel_variance64x32)
1855 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1856 aom_highbd_10_masked_sub_pixel_variance32x64)
1857 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1858 aom_highbd_10_masked_sub_pixel_variance32x32)
1859 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1860 aom_highbd_10_masked_sub_pixel_variance32x16)
1861 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1862 aom_highbd_10_masked_sub_pixel_variance16x32)
1863 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1864 aom_highbd_10_masked_sub_pixel_variance16x16)
1865 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1866 aom_highbd_10_masked_sub_pixel_variance8x16)
1867 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1868 aom_highbd_10_masked_sub_pixel_variance16x8)
1869 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1870 aom_highbd_10_masked_sub_pixel_variance8x8)
1871 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1872 aom_highbd_10_masked_sub_pixel_variance4x8)
1873 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1874 aom_highbd_10_masked_sub_pixel_variance8x4)
1875 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1876 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001877 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1878 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001879 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1880 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001881 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1882 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001883 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1884 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001885 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1886 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001887 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1888 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001889 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1890 aom_highbd_10_obmc_variance128x128,
1891 aom_highbd_10_obmc_sub_pixel_variance128x128)
1892 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1893 aom_highbd_10_obmc_variance128x64,
1894 aom_highbd_10_obmc_sub_pixel_variance128x64)
1895 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1896 aom_highbd_10_obmc_variance64x128,
1897 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001898 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1899 aom_highbd_10_obmc_variance64x64,
1900 aom_highbd_10_obmc_sub_pixel_variance64x64)
1901 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1902 aom_highbd_10_obmc_variance64x32,
1903 aom_highbd_10_obmc_sub_pixel_variance64x32)
1904 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
1905 aom_highbd_10_obmc_variance32x64,
1906 aom_highbd_10_obmc_sub_pixel_variance32x64)
1907 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
1908 aom_highbd_10_obmc_variance32x32,
1909 aom_highbd_10_obmc_sub_pixel_variance32x32)
1910 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
1911 aom_highbd_10_obmc_variance32x16,
1912 aom_highbd_10_obmc_sub_pixel_variance32x16)
1913 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
1914 aom_highbd_10_obmc_variance16x32,
1915 aom_highbd_10_obmc_sub_pixel_variance16x32)
1916 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
1917 aom_highbd_10_obmc_variance16x16,
1918 aom_highbd_10_obmc_sub_pixel_variance16x16)
1919 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
1920 aom_highbd_10_obmc_variance8x16,
1921 aom_highbd_10_obmc_sub_pixel_variance8x16)
1922 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
1923 aom_highbd_10_obmc_variance16x8,
1924 aom_highbd_10_obmc_sub_pixel_variance16x8)
1925 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
1926 aom_highbd_10_obmc_variance8x8,
1927 aom_highbd_10_obmc_sub_pixel_variance8x8)
1928 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
1929 aom_highbd_10_obmc_variance4x8,
1930 aom_highbd_10_obmc_sub_pixel_variance4x8)
1931 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
1932 aom_highbd_10_obmc_variance8x4,
1933 aom_highbd_10_obmc_sub_pixel_variance8x4)
1934 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
1935 aom_highbd_10_obmc_variance4x4,
1936 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001937
Rupert Swarbrick72678572017-08-02 12:05:26 +01001938 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
1939 aom_highbd_10_obmc_variance64x16,
1940 aom_highbd_10_obmc_sub_pixel_variance64x16)
1941
1942 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
1943 aom_highbd_10_obmc_variance16x64,
1944 aom_highbd_10_obmc_sub_pixel_variance16x64)
1945
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001946 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
1947 aom_highbd_10_obmc_variance32x8,
1948 aom_highbd_10_obmc_sub_pixel_variance32x8)
1949
1950 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
1951 aom_highbd_10_obmc_variance8x32,
1952 aom_highbd_10_obmc_sub_pixel_variance8x32)
1953
1954 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
1955 aom_highbd_10_obmc_variance16x4,
1956 aom_highbd_10_obmc_sub_pixel_variance16x4)
1957
1958 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
1959 aom_highbd_10_obmc_variance4x16,
1960 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001961 break;
1962
Yaowu Xuf883b422016-08-30 14:01:10 -07001963 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001964 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
1965 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
1966 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001967 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001968 aom_highbd_sad64x16x4d_bits12,
1969 aom_highbd_jnt_sad64x16_avg_bits12,
1970 aom_highbd_12_jnt_sub_pixel_avg_variance64x16);
1971
1972 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
1973 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
1974 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001975 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001976 aom_highbd_sad16x64x4d_bits12,
1977 aom_highbd_jnt_sad16x64_avg_bits12,
1978 aom_highbd_12_jnt_sub_pixel_avg_variance16x64);
1979
1980 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
1981 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
1982 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001983 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001984 aom_highbd_sad32x8x4d_bits12,
1985 aom_highbd_jnt_sad32x8_avg_bits12,
1986 aom_highbd_12_jnt_sub_pixel_avg_variance32x8);
1987
1988 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
1989 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
1990 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001991 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001992 aom_highbd_sad8x32x4d_bits12,
1993 aom_highbd_jnt_sad8x32_avg_bits12,
1994 aom_highbd_12_jnt_sub_pixel_avg_variance8x32);
1995
1996 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
1997 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
1998 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001999 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002000 aom_highbd_sad16x4x4d_bits12,
2001 aom_highbd_jnt_sad16x4_avg_bits12,
2002 aom_highbd_12_jnt_sub_pixel_avg_variance16x4);
2003
2004 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2005 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2006 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002007 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002008 aom_highbd_sad4x16x4d_bits12,
2009 aom_highbd_jnt_sad4x16_avg_bits12,
2010 aom_highbd_12_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002011
2012 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2013 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2014 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002015 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002016 aom_highbd_sad32x16x4d_bits12,
2017 aom_highbd_jnt_sad32x16_avg_bits12,
2018 aom_highbd_12_jnt_sub_pixel_avg_variance32x16);
2019
2020 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2021 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2022 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002023 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002024 aom_highbd_sad16x32x4d_bits12,
2025 aom_highbd_jnt_sad16x32_avg_bits12,
2026 aom_highbd_12_jnt_sub_pixel_avg_variance16x32);
2027
2028 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2029 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2030 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002031 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002032 aom_highbd_sad64x32x4d_bits12,
2033 aom_highbd_jnt_sad64x32_avg_bits12,
2034 aom_highbd_12_jnt_sub_pixel_avg_variance64x32);
2035
2036 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2037 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2038 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002039 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002040 aom_highbd_sad32x64x4d_bits12,
2041 aom_highbd_jnt_sad32x64_avg_bits12,
2042 aom_highbd_12_jnt_sub_pixel_avg_variance32x64);
2043
2044 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2045 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2046 aom_highbd_12_sub_pixel_variance32x32,
2047 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002048 aom_highbd_sad32x32x4d_bits12,
2049 aom_highbd_jnt_sad32x32_avg_bits12,
2050 aom_highbd_12_jnt_sub_pixel_avg_variance32x32);
2051
2052 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2053 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2054 aom_highbd_12_sub_pixel_variance64x64,
2055 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002056 aom_highbd_sad64x64x4d_bits12,
2057 aom_highbd_jnt_sad64x64_avg_bits12,
2058 aom_highbd_12_jnt_sub_pixel_avg_variance64x64);
2059
2060 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2061 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2062 aom_highbd_12_sub_pixel_variance16x16,
2063 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002064 aom_highbd_sad16x16x4d_bits12,
2065 aom_highbd_jnt_sad16x16_avg_bits12,
2066 aom_highbd_12_jnt_sub_pixel_avg_variance16x16);
2067
2068 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2069 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2070 aom_highbd_12_sub_pixel_variance16x8,
2071 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002072 aom_highbd_sad16x8x4d_bits12,
2073 aom_highbd_jnt_sad16x8_avg_bits12,
2074 aom_highbd_12_jnt_sub_pixel_avg_variance16x8);
2075
2076 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2077 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2078 aom_highbd_12_sub_pixel_variance8x16,
2079 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002080 aom_highbd_sad8x16x4d_bits12,
2081 aom_highbd_jnt_sad8x16_avg_bits12,
2082 aom_highbd_12_jnt_sub_pixel_avg_variance8x16);
2083
2084 HIGHBD_BFP(
2085 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2086 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002087 aom_highbd_12_sub_pixel_avg_variance8x8,
2088 aom_highbd_sad8x8x4d_bits12, aom_highbd_jnt_sad8x8_avg_bits12,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002089 aom_highbd_12_jnt_sub_pixel_avg_variance8x8);
2090
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002091 HIGHBD_BFP(
2092 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2093 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2094 aom_highbd_12_sub_pixel_avg_variance8x4,
2095 aom_highbd_sad8x4x4d_bits12, aom_highbd_jnt_sad8x4_avg_bits12,
2096 aom_highbd_12_jnt_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002097
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002098 HIGHBD_BFP(
2099 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2100 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2101 aom_highbd_12_sub_pixel_avg_variance4x8,
2102 aom_highbd_sad4x8x4d_bits12, aom_highbd_jnt_sad4x8_avg_bits12,
2103 aom_highbd_12_jnt_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002104
2105 HIGHBD_BFP(
2106 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2107 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002108 aom_highbd_12_sub_pixel_avg_variance4x4,
2109 aom_highbd_sad4x4x4d_bits12, aom_highbd_jnt_sad4x4_avg_bits12,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002110 aom_highbd_12_jnt_sub_pixel_avg_variance4x4);
2111
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002112 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2113 aom_highbd_sad128x128_avg_bits12,
2114 aom_highbd_12_variance128x128,
2115 aom_highbd_12_sub_pixel_variance128x128,
2116 aom_highbd_12_sub_pixel_avg_variance128x128,
2117 aom_highbd_sad128x128x4d_bits12,
2118 aom_highbd_jnt_sad128x128_avg_bits12,
2119 aom_highbd_12_jnt_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002120
2121 HIGHBD_BFP(
2122 BLOCK_128X64, aom_highbd_sad128x64_bits12,
2123 aom_highbd_sad128x64_avg_bits12, aom_highbd_12_variance128x64,
2124 aom_highbd_12_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002125 aom_highbd_12_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002126 aom_highbd_sad128x64x4d_bits12, aom_highbd_jnt_sad128x64_avg_bits12,
2127 aom_highbd_12_jnt_sub_pixel_avg_variance128x64);
2128
2129 HIGHBD_BFP(
2130 BLOCK_64X128, aom_highbd_sad64x128_bits12,
2131 aom_highbd_sad64x128_avg_bits12, aom_highbd_12_variance64x128,
2132 aom_highbd_12_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002133 aom_highbd_12_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002134 aom_highbd_sad64x128x4d_bits12, aom_highbd_jnt_sad64x128_avg_bits12,
2135 aom_highbd_12_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002136
David Barkerf19f35f2017-05-22 16:33:22 +01002137 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2138 aom_highbd_12_masked_sub_pixel_variance128x128)
2139 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2140 aom_highbd_12_masked_sub_pixel_variance128x64)
2141 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2142 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002143 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2144 aom_highbd_12_masked_sub_pixel_variance64x64)
2145 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2146 aom_highbd_12_masked_sub_pixel_variance64x32)
2147 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2148 aom_highbd_12_masked_sub_pixel_variance32x64)
2149 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2150 aom_highbd_12_masked_sub_pixel_variance32x32)
2151 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2152 aom_highbd_12_masked_sub_pixel_variance32x16)
2153 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2154 aom_highbd_12_masked_sub_pixel_variance16x32)
2155 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2156 aom_highbd_12_masked_sub_pixel_variance16x16)
2157 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2158 aom_highbd_12_masked_sub_pixel_variance8x16)
2159 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2160 aom_highbd_12_masked_sub_pixel_variance16x8)
2161 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2162 aom_highbd_12_masked_sub_pixel_variance8x8)
2163 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2164 aom_highbd_12_masked_sub_pixel_variance4x8)
2165 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2166 aom_highbd_12_masked_sub_pixel_variance8x4)
2167 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2168 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002169 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2170 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002171 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2172 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002173 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2174 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002175 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2176 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002177 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2178 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002179 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2180 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002181 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2182 aom_highbd_12_obmc_variance128x128,
2183 aom_highbd_12_obmc_sub_pixel_variance128x128)
2184 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2185 aom_highbd_12_obmc_variance128x64,
2186 aom_highbd_12_obmc_sub_pixel_variance128x64)
2187 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2188 aom_highbd_12_obmc_variance64x128,
2189 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002190 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2191 aom_highbd_12_obmc_variance64x64,
2192 aom_highbd_12_obmc_sub_pixel_variance64x64)
2193 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2194 aom_highbd_12_obmc_variance64x32,
2195 aom_highbd_12_obmc_sub_pixel_variance64x32)
2196 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2197 aom_highbd_12_obmc_variance32x64,
2198 aom_highbd_12_obmc_sub_pixel_variance32x64)
2199 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2200 aom_highbd_12_obmc_variance32x32,
2201 aom_highbd_12_obmc_sub_pixel_variance32x32)
2202 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2203 aom_highbd_12_obmc_variance32x16,
2204 aom_highbd_12_obmc_sub_pixel_variance32x16)
2205 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2206 aom_highbd_12_obmc_variance16x32,
2207 aom_highbd_12_obmc_sub_pixel_variance16x32)
2208 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2209 aom_highbd_12_obmc_variance16x16,
2210 aom_highbd_12_obmc_sub_pixel_variance16x16)
2211 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2212 aom_highbd_12_obmc_variance8x16,
2213 aom_highbd_12_obmc_sub_pixel_variance8x16)
2214 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2215 aom_highbd_12_obmc_variance16x8,
2216 aom_highbd_12_obmc_sub_pixel_variance16x8)
2217 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2218 aom_highbd_12_obmc_variance8x8,
2219 aom_highbd_12_obmc_sub_pixel_variance8x8)
2220 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2221 aom_highbd_12_obmc_variance4x8,
2222 aom_highbd_12_obmc_sub_pixel_variance4x8)
2223 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2224 aom_highbd_12_obmc_variance8x4,
2225 aom_highbd_12_obmc_sub_pixel_variance8x4)
2226 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2227 aom_highbd_12_obmc_variance4x4,
2228 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002229 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2230 aom_highbd_12_obmc_variance64x16,
2231 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002232 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2233 aom_highbd_12_obmc_variance16x64,
2234 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002235 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2236 aom_highbd_12_obmc_variance32x8,
2237 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002238 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2239 aom_highbd_12_obmc_variance8x32,
2240 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002241 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2242 aom_highbd_12_obmc_variance16x4,
2243 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002244 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2245 aom_highbd_12_obmc_variance4x16,
2246 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002247 break;
2248
2249 default:
2250 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002251 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002252 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002253 }
2254 }
2255}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256
Yaowu Xuf883b422016-08-30 14:01:10 -07002257static void realloc_segmentation_maps(AV1_COMP *cpi) {
2258 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259
2260 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002261 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002262 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002263 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002264
2265 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002266 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002267 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002268 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002269
2270 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002271 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002272 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002273 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002274}
2275
Yaowu Xuf883b422016-08-30 14:01:10 -07002276void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2277 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002278 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002279 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002280 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002281 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002282
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002283 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2284 seq_params->bit_depth = oxcf->bit_depth;
2285 seq_params->color_primaries = oxcf->color_primaries;
2286 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2287 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2288 seq_params->monochrome = oxcf->monochrome;
2289 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2290 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002291
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002292 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2293 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002294
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002295 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002296 cm->timing_info.num_units_in_display_tick =
2297 oxcf->timing_info.num_units_in_display_tick;
2298 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2299 cm->timing_info.equal_picture_interval =
2300 oxcf->timing_info.equal_picture_interval;
2301 cm->timing_info.num_ticks_per_picture =
2302 oxcf->timing_info.num_ticks_per_picture;
2303
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002304 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002305 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002306 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002307 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002308 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002309 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002310 cm->buffer_model.num_units_in_decoding_tick =
2311 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002312 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08002313 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07002314 set_dec_model_op_parameters(&cm->op_params[0]);
2315 } else if (cm->timing_info_present &&
2316 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002317 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002318 // set the decoder model parameters in resource availability mode
2319 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002320 } else {
2321 cm->op_params[0].initial_display_delay =
2322 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002323 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002324
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002325 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002326
Yaowu Xuc27fc142016-08-22 16:08:15 -07002327 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002328 cpi->common.options = oxcf->cfg;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002329 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002330 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002331
Yaowu Xuf883b422016-08-30 14:01:10 -07002332 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002333 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2334 } else {
2335 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2336 }
2337
2338 cpi->refresh_last_frame = 1;
2339 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002340 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002341 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002342
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002343 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2344 ? REFRESH_FRAME_CONTEXT_DISABLED
2345 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002346 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002347 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002348
Alex Converse74ad0912017-07-18 10:22:58 -07002349 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002350 CHECK_MEM_ERROR(cm, x->palette_buffer,
2351 aom_memalign(16, sizeof(*x->palette_buffer)));
2352 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002353 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002354 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002355
Yaowu Xuc27fc142016-08-22 16:08:15 -07002356 set_rc_buffer_sizes(rc, &cpi->oxcf);
2357
2358 // Under a configuration change, where maximum_buffer_size may change,
2359 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002360 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2361 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002362
2363 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002364 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002365
2366 // Set absolute upper and lower quality limits
2367 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2368 rc->best_quality = cpi->oxcf.best_allowed_q;
2369
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002370 if (!oxcf->large_scale_tile)
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002371 cm->interp_filter = cpi->sf.default_interp_filter;
Yunqing Wangb6e23bc2017-11-15 13:18:55 -08002372 else
2373 cm->interp_filter = EIGHTTAP_REGULAR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002374
Yue Chen5380cb52018-02-23 15:33:21 -08002375 cm->switchable_motion_mode = 1;
2376
Yaowu Xuc27fc142016-08-22 16:08:15 -07002377 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2378 cm->render_width = cpi->oxcf.render_width;
2379 cm->render_height = cpi->oxcf.render_height;
2380 } else {
2381 cm->render_width = cpi->oxcf.width;
2382 cm->render_height = cpi->oxcf.height;
2383 }
2384 cm->width = cpi->oxcf.width;
2385 cm->height = cpi->oxcf.height;
2386
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002387 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002388 // Superblock size should not be updated after the first key frame.
2389 if (!cpi->seq_params_locked) {
2390 set_sb_size(&cm->seq_params, select_sb_size(cpi));
2391 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002392
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002393 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002394 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002395 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002396 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002397 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002398 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002399 realloc_segmentation_maps(cpi);
2400 cpi->initial_width = cpi->initial_height = 0;
2401 }
2402 }
2403 update_frame_size(cpi);
2404
2405 cpi->alt_ref_source = NULL;
2406 rc->is_src_frame_alt_ref = 0;
2407
Yaowu Xuc27fc142016-08-22 16:08:15 -07002408 rc->is_bwd_ref_frame = 0;
2409 rc->is_last_bipred_frame = 0;
2410 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002411
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412 set_tile_info(cpi);
2413
2414 cpi->ext_refresh_frame_flags_pending = 0;
2415 cpi->ext_refresh_frame_context_pending = 0;
2416
Yaowu Xuc27fc142016-08-22 16:08:15 -07002417 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002418
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002419 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002420 // This should not be called after the first key frame.
2421 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002422 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002423 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Andrey Norkin26495512018-06-20 17:13:11 -07002424 init_seq_coding_tools(&cm->seq_params, cm, oxcf);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002425 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002426}
2427
Yaowu Xuf883b422016-08-30 14:01:10 -07002428AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2429 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002430 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002431 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2432 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002433
2434 if (!cm) return NULL;
2435
Yaowu Xuf883b422016-08-30 14:01:10 -07002436 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002437
2438 if (setjmp(cm->error.jmp)) {
2439 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002440 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441 return 0;
2442 }
2443
2444 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002445 cm->alloc_mi = enc_alloc_mi;
2446 cm->free_mi = enc_free_mi;
2447 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002448
Angie Chianga5d96c42016-10-21 16:16:56 -07002449 CHECK_MEM_ERROR(cm, cm->fc,
2450 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2451 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2452 (FRAME_CONTEXT *)aom_memalign(
2453 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2454 memset(cm->fc, 0, sizeof(*cm->fc));
2455 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456
2457 cpi->resize_state = 0;
2458 cpi->resize_avg_qp = 0;
2459 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002460
Yaowu Xuc27fc142016-08-22 16:08:15 -07002461 cpi->common.buffer_pool = pool;
2462
2463 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002464 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002465
2466 cm->current_video_frame = 0;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002467 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002468 cpi->partition_search_skippable_frame = 0;
2469 cpi->tile_data = NULL;
2470 cpi->last_show_frame_buf_idx = INVALID_IDX;
2471
2472 realloc_segmentation_maps(cpi);
2473
Jingning Hanf050fc12018-03-09 14:53:33 -08002474 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2475 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002476
Yaowu Xuc27fc142016-08-22 16:08:15 -07002477 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2478 i++) {
2479 CHECK_MEM_ERROR(
2480 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002481 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002482 }
2483
2484#if CONFIG_FP_MB_STATS
2485 cpi->use_fp_mb_stats = 0;
2486 if (cpi->use_fp_mb_stats) {
2487 // a place holder used to store the first pass mb stats in the first pass
2488 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002489 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002490 } else {
2491 cpi->twopass.frame_mb_stats_buf = NULL;
2492 }
2493#endif
2494
2495 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002496
2497 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2498#if CONFIG_INTERNAL_STATS
2499 cpi->b_calculate_blockiness = 1;
2500 cpi->b_calculate_consistency = 1;
2501 cpi->total_inconsistency = 0;
2502 cpi->psnr.worst = 100.0;
2503 cpi->worst_ssim = 100.0;
2504
2505 cpi->count = 0;
2506 cpi->bytes = 0;
2507
2508 if (cpi->b_calculate_psnr) {
2509 cpi->total_sq_error = 0;
2510 cpi->total_samples = 0;
2511 cpi->tot_recode_hits = 0;
2512 cpi->summed_quality = 0;
2513 cpi->summed_weights = 0;
2514 }
2515
2516 cpi->fastssim.worst = 100.0;
2517 cpi->psnrhvs.worst = 100.0;
2518
2519 if (cpi->b_calculate_blockiness) {
2520 cpi->total_blockiness = 0;
2521 cpi->worst_blockiness = 0.0;
2522 }
2523
2524 if (cpi->b_calculate_consistency) {
2525 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002526 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002527 cpi->common.mi_rows * cpi->common.mi_cols));
2528 cpi->worst_consistency = 100.0;
2529 }
2530#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002531#if CONFIG_ENTROPY_STATS
2532 av1_zero(aggregate_fc);
2533#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002534
2535 cpi->first_time_stamp_ever = INT64_MAX;
2536
Jingning Hanf050fc12018-03-09 14:53:33 -08002537 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2538 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2539 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2540 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002541
Yaowu Xuc27fc142016-08-22 16:08:15 -07002542#ifdef OUTPUT_YUV_SKINMAP
2543 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2544#endif
2545#ifdef OUTPUT_YUV_REC
2546 yuv_rec_file = fopen("rec.yuv", "wb");
2547#endif
2548
Yaowu Xuc27fc142016-08-22 16:08:15 -07002549 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002550 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002551 } else if (oxcf->pass == 2) {
2552 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2553 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2554
2555#if CONFIG_FP_MB_STATS
2556 if (cpi->use_fp_mb_stats) {
2557 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2558 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2559
2560 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2561 oxcf->firstpass_mb_stats_in.buf;
2562 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2563 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2564 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2565 }
2566#endif
2567
2568 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2569 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2570 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2571
Yaowu Xuf883b422016-08-30 14:01:10 -07002572 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002573 }
2574
Jingning Hand064cf02017-06-01 10:00:39 -07002575 CHECK_MEM_ERROR(
2576 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002577 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002578 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002579 CHECK_MEM_ERROR(
2580 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002581 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002582 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002583
2584 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2585 (int32_t *)aom_memalign(
2586 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2587
2588 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2589 (int32_t *)aom_memalign(
2590 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2591
Yaowu Xuf883b422016-08-30 14:01:10 -07002592 av1_set_speed_features_framesize_independent(cpi);
2593 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002594
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002595#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2596 cpi->fn_ptr[BT].sdf = SDF; \
2597 cpi->fn_ptr[BT].sdaf = SDAF; \
2598 cpi->fn_ptr[BT].vf = VF; \
2599 cpi->fn_ptr[BT].svf = SVF; \
2600 cpi->fn_ptr[BT].svaf = SVAF; \
2601 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2602 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002603 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002604
Cheng Chenf78632e2017-10-20 15:30:51 -07002605 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002606 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002607 aom_sad4x16x4d, aom_jnt_sad4x16_avg, aom_jnt_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002608
2609 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002610 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Cheng Chend0179a62017-11-16 17:02:53 -08002611 aom_sad16x4x4d, aom_jnt_sad16x4_avg, aom_jnt_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002612
2613 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002614 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002615 aom_sad8x32x4d, aom_jnt_sad8x32_avg, aom_jnt_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002616
2617 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002618 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Cheng Chend0179a62017-11-16 17:02:53 -08002619 aom_sad32x8x4d, aom_jnt_sad32x8_avg, aom_jnt_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002620
2621 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002622 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002623 aom_sad16x64x4d, aom_jnt_sad16x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002624 aom_jnt_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002625
2626 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002627 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002628 aom_sad64x16x4d, aom_jnt_sad64x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002629 aom_jnt_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002630
Cheng Chenf78632e2017-10-20 15:30:51 -07002631 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2632 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002633 aom_sad128x128x4d, aom_jnt_sad128x128_avg,
2634 aom_jnt_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002635
2636 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002637 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
2638 aom_sad128x64x4d, aom_jnt_sad128x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002639 aom_jnt_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002640
2641 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002642 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
2643 aom_sad64x128x4d, aom_jnt_sad64x128_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002644 aom_jnt_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002645
2646 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002647 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002648 aom_sad32x16x4d, aom_jnt_sad32x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002649 aom_jnt_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002650
2651 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002652 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002653 aom_sad16x32x4d, aom_jnt_sad16x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002654 aom_jnt_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002655
2656 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002657 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002658 aom_sad64x32x4d, aom_jnt_sad64x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002659 aom_jnt_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002660
2661 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002662 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002663 aom_sad32x64x4d, aom_jnt_sad32x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002664 aom_jnt_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002665
2666 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2667 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002668 aom_sad32x32x4d, aom_jnt_sad32x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002669 aom_jnt_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002670
2671 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2672 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002673 aom_sad64x64x4d, aom_jnt_sad64x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002674 aom_jnt_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002675
2676 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2677 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002678 aom_sad16x16x4d, aom_jnt_sad16x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002679 aom_jnt_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002680
2681 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002682 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
2683 aom_sad16x8x4d, aom_jnt_sad16x8_avg, aom_jnt_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002684
2685 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002686 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
2687 aom_sad8x16x4d, aom_jnt_sad8x16_avg, aom_jnt_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002688
2689 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002690 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
2691 aom_jnt_sad8x8_avg, aom_jnt_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002692
2693 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002694 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
2695 aom_jnt_sad8x4_avg, aom_jnt_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002696
2697 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002698 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
2699 aom_jnt_sad4x8_avg, aom_jnt_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002700
2701 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002702 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
2703 aom_jnt_sad4x4_avg, aom_jnt_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002704
Yaowu Xuc27fc142016-08-22 16:08:15 -07002705#define OBFP(BT, OSDF, OVF, OSVF) \
2706 cpi->fn_ptr[BT].osdf = OSDF; \
2707 cpi->fn_ptr[BT].ovf = OVF; \
2708 cpi->fn_ptr[BT].osvf = OSVF;
2709
Yaowu Xuf883b422016-08-30 14:01:10 -07002710 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2711 aom_obmc_sub_pixel_variance128x128)
2712 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2713 aom_obmc_sub_pixel_variance128x64)
2714 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2715 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002716 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2717 aom_obmc_sub_pixel_variance64x64)
2718 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2719 aom_obmc_sub_pixel_variance64x32)
2720 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2721 aom_obmc_sub_pixel_variance32x64)
2722 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2723 aom_obmc_sub_pixel_variance32x32)
2724 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2725 aom_obmc_sub_pixel_variance32x16)
2726 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2727 aom_obmc_sub_pixel_variance16x32)
2728 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2729 aom_obmc_sub_pixel_variance16x16)
2730 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2731 aom_obmc_sub_pixel_variance16x8)
2732 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2733 aom_obmc_sub_pixel_variance8x16)
2734 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2735 aom_obmc_sub_pixel_variance8x8)
2736 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2737 aom_obmc_sub_pixel_variance4x8)
2738 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2739 aom_obmc_sub_pixel_variance8x4)
2740 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2741 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002742 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2743 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002744 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2745 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002746 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2747 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002748 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2749 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002750 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2751 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002752 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2753 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002754
David Barkerf19f35f2017-05-22 16:33:22 +01002755#define MBFP(BT, MCSDF, MCSVF) \
2756 cpi->fn_ptr[BT].msdf = MCSDF; \
2757 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002758
David Barkerf19f35f2017-05-22 16:33:22 +01002759 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2760 aom_masked_sub_pixel_variance128x128)
2761 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2762 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002763 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2764 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2765 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2766 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2767 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2768 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2769 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2770 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2771 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2772 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2773 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2774 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2775 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002776
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002777 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2778
2779 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2780
2781 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2782
2783 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002784
2785 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2786
2787 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002788
Yaowu Xuc27fc142016-08-22 16:08:15 -07002789 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002790
Yaowu Xuf883b422016-08-30 14:01:10 -07002791 /* av1_init_quantizer() is first called here. Add check in
2792 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002793 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002794 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002795 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002796 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08002797 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002798
Yaowu Xuf883b422016-08-30 14:01:10 -07002799 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07002800 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002801 cm->superres_upscaled_width = oxcf->width;
2802 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002803 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002804
2805 cm->error.setjmp = 0;
2806
2807 return cpi;
2808}
2809
Urvang Joshiee2c8112018-05-04 14:53:15 -07002810#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002811#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2812
2813#define SNPRINT2(H, T, V) \
2814 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07002815#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002816
Yaowu Xuf883b422016-08-30 14:01:10 -07002817void av1_remove_compressor(AV1_COMP *cpi) {
2818 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002819 unsigned int i;
2820 int t;
2821
2822 if (!cpi) return;
2823
2824 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002825 const int num_planes = av1_num_planes(cm);
2826
Yaowu Xuc27fc142016-08-22 16:08:15 -07002827 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002828#if CONFIG_ENTROPY_STATS
2829 if (cpi->oxcf.pass != 1) {
2830 fprintf(stderr, "Writing counts.stt\n");
2831 FILE *f = fopen("counts.stt", "wb");
2832 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
2833 fclose(f);
2834 }
2835#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002836#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07002837 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002838
2839 if (cpi->oxcf.pass != 1) {
2840 char headings[512] = { 0 };
2841 char results[512] = { 0 };
2842 FILE *f = fopen("opsnr.stt", "a");
2843 double time_encoded =
2844 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2845 10000000.000;
2846 double total_encode_time =
2847 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2848 const double dr =
2849 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2850 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2851 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2852 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2853
2854 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002855 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002856 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2857 const double total_ssim =
2858 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2859 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08002860 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07002861 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002862 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08002863 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002864 snprintf(results, sizeof(results),
2865 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2866 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002867 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08002868 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07002869 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
2870 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
2871 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
2872 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002873 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07002874 cpi->psnr.stat[STAT_Y] / cpi->count,
2875 cpi->psnr.stat[STAT_U] / cpi->count,
2876 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002877
2878 if (cpi->b_calculate_blockiness) {
2879 SNPRINT(headings, "\t Block\tWstBlck");
2880 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2881 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2882 }
2883
2884 if (cpi->b_calculate_consistency) {
2885 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07002886 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002887 (double)cpi->total_inconsistency);
2888
2889 SNPRINT(headings, "\tConsist\tWstCons");
2890 SNPRINT2(results, "\t%7.3f", consistency);
2891 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2892 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07002893 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2894 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002895 rate_err, fabs(rate_err));
2896 }
2897
2898 fclose(f);
2899 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07002900#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002901 }
2902
2903 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002904 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002905 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2906
2907 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07002908 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909
2910 // Deallocate allocated thread data.
2911 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07002912 aom_free(thread_data->td->palette_buffer);
Jingning Hand064cf02017-06-01 10:00:39 -07002913 aom_free(thread_data->td->above_pred_buf);
2914 aom_free(thread_data->td->left_pred_buf);
2915 aom_free(thread_data->td->wsrc_buf);
2916 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002917 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002918 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07002919 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002920 }
2921 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002922 aom_free(cpi->tile_thr_data);
2923 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924
Deepa K G964e72e2018-05-16 16:56:01 +05302925 if (cpi->num_workers > 1) {
2926 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05302927 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05302928 }
2929
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930 dealloc_compressor_data(cpi);
2931
2932 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
2933 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002934 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002935 }
2936
2937#if CONFIG_FP_MB_STATS
2938 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002939 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002940 cpi->twopass.frame_mb_stats_buf = NULL;
2941 }
2942#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08002943#if CONFIG_INTERNAL_STATS
2944 aom_free(cpi->ssim_vars);
2945 cpi->ssim_vars = NULL;
2946#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002947
Yaowu Xuf883b422016-08-30 14:01:10 -07002948 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08002949 for (i = 0; i < FRAME_BUFFERS; ++i) {
2950 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
2951 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08002952 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xuf883b422016-08-30 14:01:10 -07002953 av1_free_ref_frame_buffers(cm->buffer_pool);
2954 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955
2956#ifdef OUTPUT_YUV_SKINMAP
2957 fclose(yuv_skinmap_file);
2958#endif
2959#ifdef OUTPUT_YUV_REC
2960 fclose(yuv_rec_file);
2961#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002962}
2963
Yaowu Xuf883b422016-08-30 14:01:10 -07002964static void generate_psnr_packet(AV1_COMP *cpi) {
2965 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002966 int i;
2967 PSNR_STATS psnr;
Alex Conversef77fd0b2017-04-20 11:00:24 -07002968 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002970
2971 for (i = 0; i < 4; ++i) {
2972 pkt.data.psnr.samples[i] = psnr.samples[i];
2973 pkt.data.psnr.sse[i] = psnr.sse[i];
2974 pkt.data.psnr.psnr[i] = psnr.psnr[i];
2975 }
Yaowu Xuf883b422016-08-30 14:01:10 -07002976 pkt.kind = AOM_CODEC_PSNR_PKT;
2977 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002978}
2979
Yaowu Xuf883b422016-08-30 14:01:10 -07002980int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
2982
Yunqing Wangf2e7a392017-11-08 00:27:21 -08002983 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002984 return 0;
2985}
2986
Yunqing Wang9a50fec2017-11-02 17:02:00 -07002987void av1_update_reference(AV1_COMP *cpi, int ref_frame_upd_flags) {
2988 cpi->ext_refresh_last_frame = (ref_frame_upd_flags & AOM_LAST_FLAG) != 0;
2989 cpi->ext_refresh_golden_frame = (ref_frame_upd_flags & AOM_GOLD_FLAG) != 0;
2990 cpi->ext_refresh_alt_ref_frame = (ref_frame_upd_flags & AOM_ALT_FLAG) != 0;
2991 cpi->ext_refresh_bwd_ref_frame = (ref_frame_upd_flags & AOM_BWD_FLAG) != 0;
2992 cpi->ext_refresh_alt2_ref_frame = (ref_frame_upd_flags & AOM_ALT2_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002993 cpi->ext_refresh_frame_flags_pending = 1;
2994}
2995
Thomas Daede497d1952017-08-08 17:33:06 -07002996int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
2997 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002998 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07002999 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003000 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003001 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003002 return 0;
3003 } else {
3004 return -1;
3005 }
3006}
3007
Thomas Daede497d1952017-08-08 17:33:06 -07003008int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3009 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003010 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003011 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003012 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003013 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003014 return 0;
3015 } else {
3016 return -1;
3017 }
3018}
3019
3020int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003021 cpi->ext_refresh_frame_context = update;
3022 cpi->ext_refresh_frame_context_pending = 1;
3023 return 0;
3024}
3025
3026#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3027// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3028// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3029// not denoise the UV channels at this time. If ever we implement UV channel
3030// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003031void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003032 uint8_t *src = s->y_buffer;
3033 int h = s->y_height;
3034
3035 do {
3036 fwrite(src, s->y_width, 1, f);
3037 src += s->y_stride;
3038 } while (--h);
3039
3040 src = s->u_buffer;
3041 h = s->uv_height;
3042
3043 do {
3044 fwrite(src, s->uv_width, 1, f);
3045 src += s->uv_stride;
3046 } while (--h);
3047
3048 src = s->v_buffer;
3049 h = s->uv_height;
3050
3051 do {
3052 fwrite(src, s->uv_width, 1, f);
3053 src += s->uv_stride;
3054 } while (--h);
3055}
3056#endif
3057
Zoe Liu8dd1c982017-09-11 10:14:35 -07003058#if USE_GF16_MULTI_LAYER
3059static void check_show_existing_frame_gf16(AV1_COMP *cpi) {
3060 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3061 AV1_COMMON *const cm = &cpi->common;
3062 const FRAME_UPDATE_TYPE next_frame_update_type =
3063 gf_group->update_type[gf_group->index];
3064
3065 if (cm->show_existing_frame == 1) {
3066 cm->show_existing_frame = 0;
3067 } else if (cpi->rc.is_last_bipred_frame) {
3068 cpi->rc.is_last_bipred_frame = 0;
3069 cm->show_existing_frame = 1;
Zoe Liu5989a722018-03-29 13:37:36 -07003070 cpi->existing_fb_idx_to_show = cpi->ref_fb_idx[BWDREF_FRAME - 1];
Zoe Liu8dd1c982017-09-11 10:14:35 -07003071 } else if (next_frame_update_type == OVERLAY_UPDATE ||
3072 next_frame_update_type == INTNL_OVERLAY_UPDATE) {
3073 // Check the temporal filtering status for the next OVERLAY frame
3074 const int num_arfs_in_gf = cpi->num_extra_arfs + 1;
3075 int which_arf = 0, arf_idx;
3076 // Identify the index to the next overlay frame.
3077 for (arf_idx = 0; arf_idx < num_arfs_in_gf; arf_idx++) {
3078 if (gf_group->index == cpi->arf_pos_for_ovrly[arf_idx]) {
3079 which_arf = arf_idx;
3080 break;
3081 }
3082 }
3083 assert(arf_idx < num_arfs_in_gf);
3084 if (cpi->is_arf_filter_off[which_arf]) {
3085 cm->show_existing_frame = 1;
3086 cpi->rc.is_src_frame_alt_ref = 1;
3087 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
Zoe Liu5989a722018-03-29 13:37:36 -07003088 ? cpi->ref_fb_idx[ALTREF_FRAME - 1]
3089 : cpi->ref_fb_idx[BWDREF_FRAME - 1];
Zoe Liu8dd1c982017-09-11 10:14:35 -07003090 cpi->is_arf_filter_off[which_arf] = 0;
3091 }
3092 }
3093 cpi->rc.is_src_frame_ext_arf = 0;
3094}
3095#endif // USE_GF16_MULTI_LAYER
3096
Yaowu Xuf883b422016-08-30 14:01:10 -07003097static void check_show_existing_frame(AV1_COMP *cpi) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003098#if USE_GF16_MULTI_LAYER
3099 if (cpi->rc.baseline_gf_interval == 16) {
3100 check_show_existing_frame_gf16(cpi);
3101 return;
3102 }
3103#endif // USE_GF16_MULTI_LAYER
3104
Yaowu Xuc27fc142016-08-22 16:08:15 -07003105 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003106 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003107 const FRAME_UPDATE_TYPE next_frame_update_type =
3108 gf_group->update_type[gf_group->index];
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003109#if USE_SYMM_MULTI_LAYER
3110 const int which_arf = (cpi->new_bwdref_update_rule == 1)
3111 ? gf_group->arf_update_idx[gf_group->index] > 0
3112 : gf_group->arf_update_idx[gf_group->index];
3113#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003114 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003115#endif
Zoe Liu5fca7242016-10-10 17:18:57 -07003116
3117 if (cm->show_existing_frame == 1) {
3118 cm->show_existing_frame = 0;
3119 } else if (cpi->rc.is_last_bipred_frame) {
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003120#if USE_SYMM_MULTI_LAYER
3121 // NOTE: When new structure is used, every bwdref will have one overlay
3122 // frame. Therefore, there is no need to find out which frame to
3123 // show in advance.
3124 if (cpi->new_bwdref_update_rule == 0) {
3125#endif
3126 // NOTE: If the current frame is a last bi-predictive frame, it is
3127 // needed next to show the BWDREF_FRAME, which is pointed by
3128 // the last_fb_idxes[0] after reference frame buffer update
3129 cpi->rc.is_last_bipred_frame = 0;
3130 cm->show_existing_frame = 1;
3131 cpi->existing_fb_idx_to_show = cpi->ref_fb_idx[0];
3132#if USE_SYMM_MULTI_LAYER
3133 }
3134#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003135 } else if (cpi->is_arf_filter_off[which_arf] &&
3136 (next_frame_update_type == OVERLAY_UPDATE ||
3137 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003138#if USE_SYMM_MULTI_LAYER
3139 const int bwdref_to_show =
3140 (cpi->new_bwdref_update_rule == 1) ? BWDREF_FRAME : ALTREF2_FRAME;
3141#else
3142 const int bwdref_to_show = ALTREF2_FRAME;
3143#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003144 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003145 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003146 cm->show_existing_frame = 1;
3147 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003148 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
Zoe Liu5989a722018-03-29 13:37:36 -07003149 ? cpi->ref_fb_idx[ALTREF_FRAME - 1]
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003150 : cpi->ref_fb_idx[bwdref_to_show - 1];
3151#if USE_SYMM_MULTI_LAYER
3152 if (cpi->new_bwdref_update_rule == 0)
3153#endif
3154 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003155 }
3156 cpi->rc.is_src_frame_ext_arf = 0;
3157}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003158
3159#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003160void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003161 uint8_t *src = s->y_buffer;
3162 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003163 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003164 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3165 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3166
3167 do {
3168 fwrite(src16, s->y_width, 2, yuv_rec_file);
3169 src16 += s->y_stride;
3170 } while (--h);
3171
3172 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3173 h = s->uv_height;
3174
3175 do {
3176 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3177 src16 += s->uv_stride;
3178 } while (--h);
3179
3180 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3181 h = s->uv_height;
3182
3183 do {
3184 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3185 src16 += s->uv_stride;
3186 } while (--h);
3187
3188 fflush(yuv_rec_file);
3189 return;
3190 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003191
3192 do {
3193 fwrite(src, s->y_width, 1, yuv_rec_file);
3194 src += s->y_stride;
3195 } while (--h);
3196
3197 src = s->u_buffer;
3198 h = s->uv_height;
3199
3200 do {
3201 fwrite(src, s->uv_width, 1, yuv_rec_file);
3202 src += s->uv_stride;
3203 } while (--h);
3204
3205 src = s->v_buffer;
3206 h = s->uv_height;
3207
3208 do {
3209 fwrite(src, s->uv_width, 1, yuv_rec_file);
3210 src += s->uv_stride;
3211 } while (--h);
3212
3213 fflush(yuv_rec_file);
3214}
3215#endif // OUTPUT_YUV_REC
3216
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003217#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003218static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3219 int i;
3220 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003221 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003222 AV1_COMMON *const cm = &cpi->common;
3223 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3224 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003225 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003226 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003227 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003228 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003229 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003230 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003231 // TODO(sarahparker): The earlier condition for recoding here was:
3232 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3233 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003234 }
3235 }
3236 return recode;
3237}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003238
Yaowu Xuc27fc142016-08-22 16:08:15 -07003239// Function to test for conditions that indicate we should loop
3240// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003241static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3242 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003243 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003244 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003245 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3246 int force_recode = 0;
3247
3248 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3249 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3250 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003251 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3252 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3253 (rc->projected_frame_size < low_limit && q > minq)) {
3254 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003255 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003256 // Deal with frame undershoot and whether or not we are
3257 // below the automatically set cq level.
3258 if (q > oxcf->cq_level &&
3259 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3260 force_recode = 1;
3261 }
3262 }
3263 }
3264 return force_recode;
3265}
3266
Yaowu Xuc27fc142016-08-22 16:08:15 -07003267#define DUMP_REF_FRAME_IMAGES 0
3268
3269#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003270static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003271 const YV12_BUFFER_CONFIG *const ref_buf,
3272 char *file_name) {
3273 int h;
3274 FILE *f_ref = NULL;
3275
3276 if (ref_buf == NULL) {
3277 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003278 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003279 }
3280
3281 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3282 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003283 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003284 }
3285
3286 // --- Y ---
3287 for (h = 0; h < cm->height; ++h) {
3288 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3289 }
3290 // --- U ---
3291 for (h = 0; h < (cm->height >> 1); ++h) {
3292 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3293 f_ref);
3294 }
3295 // --- V ---
3296 for (h = 0; h < (cm->height >> 1); ++h) {
3297 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3298 f_ref);
3299 }
3300
3301 fclose(f_ref);
3302
Yaowu Xuf883b422016-08-30 14:01:10 -07003303 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003304}
3305
Yaowu Xuf883b422016-08-30 14:01:10 -07003306static void dump_ref_frame_images(AV1_COMP *cpi) {
3307 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003308 MV_REFERENCE_FRAME ref_frame;
3309
3310 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3311 char file_name[256] = "";
3312 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3313 cm->current_video_frame, ref_frame);
3314 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3315 }
3316}
3317#endif // DUMP_REF_FRAME_IMAGES == 1
3318
Yaowu Xuc27fc142016-08-22 16:08:15 -07003319// This function is used to shift the virtual indices of last reference frames
3320// as follows:
3321// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3322// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003323static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003324 // TODO(isbs): shift the scaled indices as well
Yaowu Xuc27fc142016-08-22 16:08:15 -07003325 int ref_frame;
3326 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003327 cpi->ref_fb_idx[ref_frame] = cpi->ref_fb_idx[ref_frame - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003328
3329 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003330 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003331 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003332 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3333 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3334 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003335 }
3336 }
3337}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003338
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003339#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003340// This function is used to shift the virtual indices of bwd reference
3341// frames as follows:
3342// BWD_REF -> ALT2_REF -> EXT_REF
3343// to clear a space to store the closest bwdref
3344static INLINE void rshift_bwd_ref_frames(AV1_COMP *cpi) {
3345 // TODO(isbs): shift the scaled indices as well
3346 static const int ordered_bwd[3] = { BWDREF_FRAME - 1, ALTREF2_FRAME - 1,
3347 EXTREF_FRAME - 1 };
3348
3349 for (int i = 2; i > 0; --i) {
3350 cpi->ref_fb_idx[ordered_bwd[i]] = cpi->ref_fb_idx[ordered_bwd[i - 1]];
3351
3352 // [0] is allocated to the current coded frame, i.e. bwdref
3353 memcpy(
3354 cpi->interp_filter_selected[ordered_bwd[i] + LAST_FRAME],
3355 cpi->interp_filter_selected[ordered_bwd[i - 1] + LAST_FRAME],
3356 sizeof(cpi->interp_filter_selected[ordered_bwd[i - 1] + LAST_FRAME]));
3357 }
3358}
3359
3360// This function is used to shift the virtual indices of bwd reference
3361// frames as follows:
3362// BWD_REF <- ALT2_REF <- EXT_REF
3363// to update the bwd reference frame for coding the next frame.
3364static INLINE void lshift_bwd_ref_frames(AV1_COMP *cpi) {
3365 // TODO(isbs): shift the scaled indices as well
3366 static const int ordered_bwd[3] = { BWDREF_FRAME - 1, ALTREF2_FRAME - 1,
3367 EXTREF_FRAME - 1 };
3368
3369 for (int i = 0; i < 2; ++i) {
3370 cpi->ref_fb_idx[ordered_bwd[i]] = cpi->ref_fb_idx[ordered_bwd[i + 1]];
3371
3372 // [0] is allocated to the current coded frame, i.e. bwdref
3373 memcpy(
3374 cpi->interp_filter_selected[ordered_bwd[i] + LAST_FRAME],
3375 cpi->interp_filter_selected[ordered_bwd[i + 1] + LAST_FRAME],
3376 sizeof(cpi->interp_filter_selected[ordered_bwd[i + 1] + LAST_FRAME]));
3377 }
3378}
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003379#endif // USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003380
Zoe Liu8dd1c982017-09-11 10:14:35 -07003381#if USE_GF16_MULTI_LAYER
3382static void update_reference_frames_gf16(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003383 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003384 BufferPool *const pool = cm->buffer_pool;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003385
Zoe Liu8dd1c982017-09-11 10:14:35 -07003386 if (cm->frame_type == KEY_FRAME) {
Zoe Liu5989a722018-03-29 13:37:36 -07003387 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame) {
Zoe Liu8dd1c982017-09-11 10:14:35 -07003388 ref_cnt_fb(pool->frame_bufs,
Zoe Liu5989a722018-03-29 13:37:36 -07003389 &cm->ref_frame_map[cpi->ref_fb_idx[ref_frame]],
Zoe Liu8dd1c982017-09-11 10:14:35 -07003390 cm->new_fb_idx);
3391 }
Zoe Liu8dd1c982017-09-11 10:14:35 -07003392 } else {
3393 if (cpi->refresh_last_frame || cpi->refresh_golden_frame ||
3394 cpi->refresh_bwd_ref_frame || cpi->refresh_alt2_ref_frame ||
3395 cpi->refresh_alt_ref_frame) {
3396 assert(cpi->refresh_fb_idx >= 0 && cpi->refresh_fb_idx < REF_FRAMES);
3397 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[cpi->refresh_fb_idx],
3398 cm->new_fb_idx);
3399 }
3400
3401 // TODO(zoeliu): To handle cpi->interp_filter_selected[].
3402
3403 // For GF of 16, an additional ref frame index mapping needs to be handled
3404 // if this is the last frame to encode in the current GF group.
3405 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3406 if (gf_group->update_type[gf_group->index + 1] == OVERLAY_UPDATE)
3407 av1_ref_frame_map_idx_updates(cpi, gf_group->index + 1);
3408 }
3409
3410#if DUMP_REF_FRAME_IMAGES == 1
3411 // Dump out all reference frame images.
3412 dump_ref_frame_images(cpi);
3413#endif // DUMP_REF_FRAME_IMAGES
3414}
3415#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003416
3417static void update_reference_frames(AV1_COMP *cpi) {
3418 AV1_COMMON *const cm = &cpi->common;
3419
Yaowu Xuc27fc142016-08-22 16:08:15 -07003420 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3421 // for the purpose to verify no mismatch between encoder and decoder.
3422 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3423
Zoe Liu8dd1c982017-09-11 10:14:35 -07003424#if USE_GF16_MULTI_LAYER
3425 if (cpi->rc.baseline_gf_interval == 16) {
3426 update_reference_frames_gf16(cpi);
3427 return;
3428 }
3429#endif // USE_GF16_MULTI_LAYER
Zoe Liu8dd1c982017-09-11 10:14:35 -07003430
3431 BufferPool *const pool = cm->buffer_pool;
Zoe Liubcef1e62018-04-06 20:56:11 -07003432
Yaowu Xuc27fc142016-08-22 16:08:15 -07003433 // At this point the new frame has been encoded.
3434 // If any buffer copy / swapping is signaled it should be done here.
Zoe Liubcef1e62018-04-06 20:56:11 -07003435
Sarah Parkerb9041612018-05-22 19:06:47 -07003436 // Only update all of the reference buffers if a KEY_FRAME is also a
3437 // show_frame. This ensures a fwd keyframe does not update all of the buffers
3438 if ((cm->frame_type == KEY_FRAME && cm->show_frame) || frame_is_sframe(cm)) {
Zoe Liubcef1e62018-04-06 20:56:11 -07003439 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame) {
3440 ref_cnt_fb(pool->frame_bufs,
3441 &cm->ref_frame_map[cpi->ref_fb_idx[ref_frame]],
3442 cm->new_fb_idx);
3443 }
3444 return;
3445 }
3446
3447 if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003448 // We have decided to preserve the previously existing golden frame as our
3449 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003450 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003451 // we're updating the GF with the current decoded frame, we save it to the
3452 // ARF slot instead.
3453 // We now have to update the ARF with the current frame and swap gld_fb_idx
3454 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3455 // slot and, if we're updating the GF, the current frame becomes the new GF.
3456 int tmp;
3457
Zoe Liu5989a722018-03-29 13:37:36 -07003458 ref_cnt_fb(pool->frame_bufs,
3459 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003460 cm->new_fb_idx);
Zoe Liu5989a722018-03-29 13:37:36 -07003461 tmp = cpi->ref_fb_idx[ALTREF_FRAME - 1];
3462 cpi->ref_fb_idx[ALTREF_FRAME - 1] = cpi->ref_fb_idx[GOLDEN_FRAME - 1];
3463 cpi->ref_fb_idx[GOLDEN_FRAME - 1] = tmp;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003464
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003465 // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3466 // cpi->interp_filter_selected[GOLDEN_FRAME]?
Yaowu Xuc27fc142016-08-22 16:08:15 -07003467 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
Wei-Ting Linb72453f2018-06-26 14:05:38 -07003468#if CONFIG_DEBUG
3469 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3470 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
3471#endif
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003472#if USE_SYMM_MULTI_LAYER
3473 const int bwdref_to_show =
3474 (cpi->new_bwdref_update_rule == 1) ? BWDREF_FRAME : ALTREF2_FRAME;
3475#else
3476 const int bwdref_to_show = ALTREF2_FRAME;
3477#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003478 // Deal with the special case for showing existing internal ALTREF_FRAME
3479 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3480 // by updating the virtual indices.
Zoe Liu5989a722018-03-29 13:37:36 -07003481 const int tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003482 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003483
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003484 cpi->ref_fb_idx[LAST_FRAME - 1] = cpi->ref_fb_idx[bwdref_to_show - 1];
Zoe Liue9b15e22017-07-19 15:53:01 -07003485
3486 memcpy(cpi->interp_filter_selected[LAST_FRAME],
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003487 cpi->interp_filter_selected[bwdref_to_show],
3488 sizeof(cpi->interp_filter_selected[bwdref_to_show]));
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003489#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003490 if (cpi->new_bwdref_update_rule == 1) {
3491 lshift_bwd_ref_frames(cpi);
3492 // pass outdated forward reference frame (previous LAST3) to the
3493 // spared space
3494 cpi->ref_fb_idx[EXTREF_FRAME - 1] = tmp;
3495 } else {
3496#endif
3497 cpi->ref_fb_idx[bwdref_to_show - 1] = tmp;
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003498#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003499 }
3500#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003501 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003502 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003503 if (cpi->refresh_alt_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003504 int arf_idx = cpi->ref_fb_idx[ALTREF_FRAME - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003505 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003506
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07003507 memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003508 cpi->interp_filter_selected[0],
3509 sizeof(cpi->interp_filter_selected[0]));
3510 }
3511
Zoe Liue9b15e22017-07-19 15:53:01 -07003512 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003513 if (cpi->refresh_golden_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003514 ref_cnt_fb(pool->frame_bufs,
3515 &cm->ref_frame_map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003517
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003518 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3519 cpi->interp_filter_selected[0],
3520 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003521 }
3522
Zoe Liue9b15e22017-07-19 15:53:01 -07003523 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003524 if (cpi->refresh_bwd_ref_frame) {
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003525#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003526 if (cpi->new_bwdref_update_rule) {
3527 // We shift the backward reference frame as follows:
3528 // BWDREF -> ALTREF2 -> EXTREF
3529 // and assign the newly coded frame to BWDREF so that it always
3530 // keeps the nearest future frame
3531 int tmp = cpi->ref_fb_idx[EXTREF_FRAME - 1];
3532 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[tmp], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003533
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003534 rshift_bwd_ref_frames(cpi);
3535 cpi->ref_fb_idx[BWDREF_FRAME - 1] = tmp;
3536 } else {
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003537#endif // USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003538 ref_cnt_fb(pool->frame_bufs,
3539 &cm->ref_frame_map[cpi->ref_fb_idx[BWDREF_FRAME - 1]],
3540 cm->new_fb_idx);
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003541#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003542 }
3543#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003544 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3545 cpi->interp_filter_selected[0],
3546 sizeof(cpi->interp_filter_selected[0]));
3547 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003548
Zoe Liue9b15e22017-07-19 15:53:01 -07003549 // === ALTREF2_FRAME ===
3550 if (cpi->refresh_alt2_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003551 ref_cnt_fb(pool->frame_bufs,
3552 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]],
Zoe Liue9b15e22017-07-19 15:53:01 -07003553 cm->new_fb_idx);
3554
3555 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3556 cpi->interp_filter_selected[0],
3557 sizeof(cpi->interp_filter_selected[0]));
3558 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003559 }
3560
3561 if (cpi->refresh_last_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003562 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3563 // reference to the reference frame buffer virtual index; and then (2) from
3564 // the virtual index to the reference frame buffer physical index:
3565 //
3566 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3567 // | | |
3568 // v v v
Zoe Liu5989a722018-03-29 13:37:36 -07003569 // ref_fb_idx[0], ..., ref_fb_idx[2], ..., ref_fb_idx[ALTREF_FRAME-1]
Yaowu Xuc27fc142016-08-22 16:08:15 -07003570 // | | |
3571 // v v v
3572 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3573 //
3574 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3575 // have the other 2 LAST reference frames shifted as follows:
3576 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3577 // , and then have LAST_FRAME refreshed by the newly coded frame.
3578 //
3579 // To fulfill it, the decoder will be notified to execute following 2 steps:
3580 //
3581 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3582 // to point to the newly coded frame, i.e.
3583 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3584 //
3585 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3586 // original virtual index of LAST3_FRAME and have the other mappings
3587 // shifted as follows:
3588 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3589 // | | |
3590 // v v v
Zoe Liu5989a722018-03-29 13:37:36 -07003591 // ref_fb_idx[2], ref_fb_idx[0], ref_fb_idx[1]
Zoe Liubcef1e62018-04-06 20:56:11 -07003592 int tmp;
Zoe Liu5fca7242016-10-10 17:18:57 -07003593
Zoe Liubcef1e62018-04-06 20:56:11 -07003594 ref_cnt_fb(pool->frame_bufs,
3595 &cm->ref_frame_map[cpi->ref_fb_idx[LAST_REF_FRAMES - 1]],
3596 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003597
Zoe Liubcef1e62018-04-06 20:56:11 -07003598 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003599
Zoe Liubcef1e62018-04-06 20:56:11 -07003600 shift_last_ref_frames(cpi);
3601 cpi->ref_fb_idx[0] = tmp;
3602
3603 assert(cm->show_existing_frame == 0);
3604 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3605 cpi->interp_filter_selected[0],
3606 sizeof(cpi->interp_filter_selected[0]));
3607
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003608 // If the new structure is used, we will always have overlay frames coupled
3609 // with bwdref frames. Therefore, we won't have to perform this update
3610 // in advance (we do this update when the overlay frame shows up).
3611#if USE_SYMM_MULTI_LAYER
3612 if (cpi->new_bwdref_update_rule == 0 && cpi->rc.is_last_bipred_frame) {
3613#else
Zoe Liubcef1e62018-04-06 20:56:11 -07003614 if (cpi->rc.is_last_bipred_frame) {
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003615#endif
Zoe Liubcef1e62018-04-06 20:56:11 -07003616 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3617 // LAST3_FRAME by updating the virtual indices.
3618 //
3619 // NOTE: The source frame for BWDREF does not have a holding position as
3620 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3621 // virtual index reshuffling for BWDREF, the encoder always
3622 // specifies a LAST_BIPRED right before BWDREF and completes the
3623 // reshuffling job accordingly.
Zoe Liu5989a722018-03-29 13:37:36 -07003624 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003625
3626 shift_last_ref_frames(cpi);
Zoe Liubcef1e62018-04-06 20:56:11 -07003627 cpi->ref_fb_idx[0] = cpi->ref_fb_idx[BWDREF_FRAME - 1];
3628 cpi->ref_fb_idx[BWDREF_FRAME - 1] = tmp;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003629
Zoe Liuf0e46692016-10-12 12:31:43 -07003630 memcpy(cpi->interp_filter_selected[LAST_FRAME],
Zoe Liubcef1e62018-04-06 20:56:11 -07003631 cpi->interp_filter_selected[BWDREF_FRAME],
3632 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003633 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003634 }
3635
3636#if DUMP_REF_FRAME_IMAGES == 1
3637 // Dump out all reference frame images.
3638 dump_ref_frame_images(cpi);
3639#endif // DUMP_REF_FRAME_IMAGES
3640}
3641
Yaowu Xuf883b422016-08-30 14:01:10 -07003642static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003643 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003644 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003645 ensure_mv_buffer(new_fb_ptr, cm);
3646 new_fb_ptr->width = cm->width;
3647 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003648}
3649
Cheng Chen46f30c72017-09-07 11:13:33 -07003650static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003651 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003652 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003653 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003654 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003655 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3656 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003657 };
3658
3659 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003660 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003661 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3662 BufferPool *const pool = cm->buffer_pool;
3663 const YV12_BUFFER_CONFIG *const ref =
3664 get_ref_frame_buffer(cpi, ref_frame);
3665
3666 if (ref == NULL) {
3667 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3668 continue;
3669 }
3670
Yaowu Xuc27fc142016-08-22 16:08:15 -07003671 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3672 RefCntBuffer *new_fb_ptr = NULL;
3673 int force_scaling = 0;
3674 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3675 if (new_fb == INVALID_IDX) {
3676 new_fb = get_free_fb(cm);
3677 force_scaling = 1;
3678 }
3679 if (new_fb == INVALID_IDX) return;
3680 new_fb_ptr = &pool->frame_bufs[new_fb];
3681 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3682 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003683 if (aom_realloc_frame_buffer(
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003684 &new_fb_ptr->buf, cm->width, cm->height,
3685 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
3686 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003687 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003688 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003689 "Failed to allocate frame buffer");
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003690 av1_resize_and_extend_frame(
3691 ref, &new_fb_ptr->buf, (int)cm->seq_params.bit_depth, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003692 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3693 alloc_frame_mvs(cm, new_fb);
3694 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003695 } else {
3696 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3697 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3698 buf->buf.y_crop_width = ref->y_crop_width;
3699 buf->buf.y_crop_height = ref->y_crop_height;
3700 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3701 ++buf->ref_count;
3702 }
3703 } else {
3704 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3705 }
3706 }
3707}
3708
Yaowu Xuf883b422016-08-30 14:01:10 -07003709static void release_scaled_references(AV1_COMP *cpi) {
3710 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003711 int i;
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003712 // TODO(isbs): only refresh the necessary frames, rather than all of them
Zoe Liu27deb382018-03-27 15:13:56 -07003713 for (i = 0; i < REF_FRAMES; ++i) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003714 const int idx = cpi->scaled_ref_idx[i];
3715 RefCntBuffer *const buf =
3716 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3717 if (buf != NULL) {
3718 --buf->ref_count;
3719 cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003720 }
3721 }
3722}
3723
Yaowu Xuf883b422016-08-30 14:01:10 -07003724static void set_mv_search_params(AV1_COMP *cpi) {
3725 const AV1_COMMON *const cm = &cpi->common;
3726 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003727
3728 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003729 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003730
3731 if (cpi->sf.mv.auto_mv_step_size) {
3732 if (frame_is_intra_only(cm)) {
3733 // Initialize max_mv_magnitude for use in the first INTER frame
3734 // after a key/intra-only frame.
3735 cpi->max_mv_magnitude = max_mv_def;
3736 } else {
3737 if (cm->show_frame) {
3738 // Allow mv_steps to correspond to twice the max mv magnitude found
3739 // in the previous frame, capped by the default max_mv_magnitude based
3740 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003741 cpi->mv_step_param = av1_init_search_range(
3742 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003743 }
3744 cpi->max_mv_magnitude = 0;
3745 }
3746 }
3747}
3748
Yaowu Xuf883b422016-08-30 14:01:10 -07003749static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003750 int i;
3751 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003752 cpi->common.global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003753 }
3754 cpi->global_motion_search_done = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003755 av1_set_speed_features_framesize_independent(cpi);
3756 av1_set_rd_speed_thresholds(cpi);
3757 av1_set_rd_speed_thresholds_sub8x8(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003758 cpi->common.interp_filter = cpi->sf.default_interp_filter;
Yue Chen5380cb52018-02-23 15:33:21 -08003759 cpi->common.switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003760}
3761
Yaowu Xuf883b422016-08-30 14:01:10 -07003762static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003763 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003764 AV1_COMMON *const cm = &cpi->common;
3765 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003766
3767 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003768 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003769
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003770 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003771 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3772 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003773
James Zern01a9d702017-08-25 19:09:33 +00003774 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003775 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003776 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003777 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003778
3779 // Configure experimental use of segmentation for enhanced coding of
3780 // static regions if indicated.
3781 // Only allowed in the second pass of a two pass encode, as it requires
3782 // lagged coding, and if the relevant speed feature flag is set.
3783 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3784 configure_static_seg_features(cpi);
3785}
3786
Yaowu Xuf883b422016-08-30 14:01:10 -07003787static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003788 int y_stride = cpi->scaled_source.y_stride;
3789
3790 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003791 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003792 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003793 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003794 }
3795}
3796
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003797#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003798static void set_restoration_unit_size(int width, int height, int sx, int sy,
3799 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003800 (void)width;
3801 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003802 (void)sx;
3803 (void)sy;
3804#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3805 int s = AOMMIN(sx, sy);
3806#else
3807 int s = 0;
3808#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3809
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003810 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003811 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003812 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003813 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003814 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3815 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003816}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003817
Cheng Chen46f30c72017-09-07 11:13:33 -07003818static void init_ref_frame_bufs(AV1_COMMON *cm) {
3819 int i;
3820 BufferPool *const pool = cm->buffer_pool;
3821 cm->new_fb_idx = INVALID_IDX;
3822 for (i = 0; i < REF_FRAMES; ++i) {
3823 cm->ref_frame_map[i] = INVALID_IDX;
3824 pool->frame_bufs[i].ref_count = 0;
3825 }
RogerZhou86902d02018-02-28 15:29:16 -08003826 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003827 for (i = 0; i < FRAME_BUFFERS; ++i) {
3828 av1_hash_table_init(&pool->frame_bufs[i].hash_table);
3829 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003830 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003831}
3832
Yaowu Xud3e7c682017-12-21 14:08:25 -08003833static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003834 int subsampling_x, int subsampling_y) {
3835 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003836 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07003837
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003838 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3839 seq_params->subsampling_x != subsampling_x ||
3840 seq_params->subsampling_y != subsampling_y) {
3841 seq_params->subsampling_x = subsampling_x;
3842 seq_params->subsampling_y = subsampling_y;
3843 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003844
3845 alloc_raw_frame_buffers(cpi);
3846 init_ref_frame_bufs(cm);
3847 alloc_util_frame_buffers(cpi);
3848
3849 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3850
3851 cpi->initial_width = cm->width;
3852 cpi->initial_height = cm->height;
3853 cpi->initial_mbs = cm->MBs;
3854 }
3855}
3856
3857// Returns 1 if the assigned width or height was <= 0.
3858static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3859 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003860 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003861 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3862 cm->seq_params.subsampling_x,
3863 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003864
3865 if (width <= 0 || height <= 0) return 1;
3866
3867 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003868 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003869
3870 if (cpi->initial_width && cpi->initial_height &&
3871 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3872 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003873 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003874 alloc_compressor_data(cpi);
3875 realloc_segmentation_maps(cpi);
3876 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003877 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003878 update_frame_size(cpi);
3879
3880 return 0;
3881}
3882
Fergus Simpsonbc189932017-05-16 17:02:39 -07003883static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003884 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003885 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003886 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003887 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003888 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003889
Fergus Simpsonbc189932017-05-16 17:02:39 -07003890 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003891 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003892 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003893 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003894 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07003895 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003896 }
3897
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003898 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003899 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003900 }
3901
3902 alloc_frame_mvs(cm, cm->new_fb_idx);
3903
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303904 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05303905 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3906 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303907 cm->num_allocated_above_contexts < cm->tile_rows) {
3908 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3909 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3910 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3911 "Failed to allocate context buffers");
3912 }
3913
Yaowu Xuc27fc142016-08-22 16:08:15 -07003914 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003915 if (aom_realloc_frame_buffer(
3916 get_frame_new_buffer(cm), cm->width, cm->height,
3917 seq_params->subsampling_x, seq_params->subsampling_y,
3918 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3919 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003920 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003921 "Failed to allocate frame buffer");
3922
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003923 const int frame_width = cm->superres_upscaled_width;
3924 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003925 set_restoration_unit_size(frame_width, frame_height,
3926 seq_params->subsampling_x,
3927 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003928 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003929 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003930
3931 av1_alloc_restoration_buffers(cm);
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003932 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003933 init_motion_estimation(cpi);
3934
3935 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3936 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3937 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3938
3939 ref_buf->idx = buf_idx;
3940
3941 if (buf_idx != INVALID_IDX) {
3942 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3943 ref_buf->buf = buf;
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003944 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3945 buf->y_crop_height, cm->width,
3946 cm->height);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003947 if (av1_is_scaled(&ref_buf->sf))
3948 aom_extend_frame_borders(buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003949 } else {
3950 ref_buf->buf = NULL;
3951 }
3952 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003953
Hui Su5ebd8702018-01-08 18:09:20 -08003954 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003955 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003956
3957 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3958}
3959
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003960static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3961 // Choose an arbitrary random number
3962 static unsigned int seed = 56789;
3963 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003964 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3965 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003966
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07003967 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003968 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003969 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003970 case RESIZE_FIXED:
3971 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003972 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003973 else
Urvang Joshide71d142017-10-05 12:12:15 -07003974 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003975 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003976 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003977 default: assert(0);
3978 }
Urvang Joshide71d142017-10-05 12:12:15 -07003979 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003980}
3981
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003982static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
3983 // Choose an arbitrary random number
3984 static unsigned int seed = 34567;
3985 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003986 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3987 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07003988
3989 // Make sure that superres mode of the frame is consistent with the
3990 // sequence-level flag.
3991 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
3992 cpi->common.seq_params.enable_superres));
3993 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
3994 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003995
3996 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003997 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003998 case SUPERRES_FIXED:
3999 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004000 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004001 else
Urvang Joshide71d142017-10-05 12:12:15 -07004002 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004003 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004004 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004005 case SUPERRES_QTHRESH: {
4006 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4007 const RATE_FACTOR_LEVEL rf_level = gf_group->rf_level[gf_group->index];
4008 const double rate_factor_delta = rate_factor_deltas[rf_level];
Urvang Joshi2c92b072018-03-19 17:23:31 -07004009 const int qthresh = (rate_factor_delta <= 1.0)
4010 ? oxcf->superres_qthresh
4011 : oxcf->superres_kf_qthresh;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004012 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi2c92b072018-03-19 17:23:31 -07004013 int bottom_index, top_index;
4014 const int q = av1_rc_pick_q_and_bounds(
4015 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004016 if (q < qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004017 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004018 } else {
Urvang Joshi28983f72017-10-25 14:41:06 -07004019 const uint8_t min_denom = SCALE_NUMERATOR + 1;
4020 const uint8_t denom_step = (MAXQ - qthresh + 1) >> 3;
Imdad Sardharwallaf6154fd2018-03-02 16:53:41 +00004021
4022 if (q == qthresh) {
4023 new_denom = min_denom;
4024 } else if (denom_step == 0) {
4025 new_denom = SCALE_NUMERATOR << 1;
4026 } else {
4027 const uint8_t additional_denom = (q - qthresh) / denom_step;
4028 new_denom =
4029 AOMMIN(min_denom + additional_denom, SCALE_NUMERATOR << 1);
4030 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004031 }
4032 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004033 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004034 default: assert(0);
4035 }
Urvang Joshide71d142017-10-05 12:12:15 -07004036 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004037}
4038
Urvang Joshide71d142017-10-05 12:12:15 -07004039static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4040 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4041}
4042
4043static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004044 // Only need to check the width, as scaling is horizontal only.
4045 (void)oheight;
4046 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004047}
4048
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004049static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004050 SUPERRES_MODE superres_mode, int owidth,
4051 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004052 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004053 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004054 }
4055
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004056 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004057 int resize_denom =
4058 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4059 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4060
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004061 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004062 // Alter superres scale as needed to enforce conformity.
4063 rsz->superres_denom =
4064 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4065 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4066 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004067 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004068 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004069 // Alter resize scale as needed to enforce conformity.
4070 resize_denom =
4071 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004072 rsz->resize_width = owidth;
4073 rsz->resize_height = oheight;
4074 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004075 resize_denom);
4076 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4077 if (resize_denom > SCALE_NUMERATOR) {
4078 --resize_denom;
4079 rsz->resize_width = owidth;
4080 rsz->resize_height = oheight;
4081 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4082 resize_denom);
4083 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004084 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004085 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004086 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004087 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004088 if (resize_denom > rsz->superres_denom)
4089 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004090 else
Urvang Joshide71d142017-10-05 12:12:15 -07004091 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004092 rsz->resize_width = owidth;
4093 rsz->resize_height = oheight;
4094 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004095 resize_denom);
4096 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4097 (resize_denom > SCALE_NUMERATOR ||
4098 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004099 } else { // We are allowed to alter neither resize scale nor superres
4100 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004101 return 0;
4102 }
Urvang Joshide71d142017-10-05 12:12:15 -07004103 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004104}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004105
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004106// Calculates resize and superres params for next frame
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004107size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
4108 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004109 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshide71d142017-10-05 12:12:15 -07004110 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004111 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004112 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4113 rsz.resize_width = cpi->resize_pending_width;
4114 rsz.resize_height = cpi->resize_pending_height;
4115 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4116 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004117 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004118 rsz.resize_width = cpi->oxcf.width;
4119 rsz.resize_height = cpi->oxcf.height;
4120 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004121 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004122 }
Urvang Joshide71d142017-10-05 12:12:15 -07004123 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004124 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4125 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004126 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004127 return rsz;
4128}
4129
4130static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004131 int encode_width = rsz->resize_width;
4132 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004133
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004134 AV1_COMMON *cm = &cpi->common;
4135 cm->superres_upscaled_width = encode_width;
4136 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004137 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004138 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4139 rsz->superres_denom);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004140 set_frame_size(cpi, encode_width, encode_height);
4141}
4142
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004143static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004144 size_params_type rsz = av1_calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004145 setup_frame_size_from_params(cpi, &rsz);
4146}
4147
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004148static void superres_post_encode(AV1_COMP *cpi) {
4149 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004150 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004151
Cheng Chen09c83a52018-06-05 12:27:36 -07004152 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004153
Urvang Joshid6b5d512018-03-20 13:34:38 -07004154 assert(cpi->oxcf.enable_superres);
4155 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004156 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004157
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004158 av1_superres_upscale(cm, NULL);
4159
4160 // If regular resizing is occurring the source will need to be downscaled to
4161 // match the upscaled superres resolution. Otherwise the original source is
4162 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004163 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004164 cpi->source = cpi->unscaled_source;
4165 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4166 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004167 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4168 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004169 // Do downscale. cm->(width|height) has been updated by
4170 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004171 if (aom_realloc_frame_buffer(
4172 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004173 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4174 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4175 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004176 aom_internal_error(
4177 &cm->error, AOM_CODEC_MEM_ERROR,
4178 "Failed to reallocate scaled source buffer for superres");
4179 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4180 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004181 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004182 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004183 cpi->source = &cpi->scaled_source;
4184 }
4185}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004186
4187static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004188 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004189 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004190
Urvang Joshic8b52d52018-03-23 13:16:51 -07004191 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4192 cm->coded_lossless && cm->all_lossless));
4193
4194 const int no_loopfilter = cm->coded_lossless || cm->large_scale_tile;
4195 const int no_cdef =
Debargha Mukherjee98a311c2018-03-25 16:33:11 -07004196 !cm->seq_params.enable_cdef || cm->coded_lossless || cm->large_scale_tile;
4197 const int no_restoration = !cm->seq_params.enable_restoration ||
4198 cm->all_lossless || cm->large_scale_tile;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004199
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004200 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004201
4202 if (no_loopfilter) {
Cheng Chen179479f2017-08-04 10:56:39 -07004203 lf->filter_level[0] = 0;
4204 lf->filter_level[1] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004205 } else {
4206 struct aom_usec_timer timer;
4207
4208 aom_clear_system_state();
4209
4210 aom_usec_timer_start(&timer);
4211
4212 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4213
4214 aom_usec_timer_mark(&timer);
4215 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4216 }
4217
Debargha Mukherjee2382b142018-02-26 14:31:32 -08004218 if (lf->filter_level[0] || lf->filter_level[1]) {
Cheng Chen8ab1f442018-04-27 18:01:52 -07004219#if LOOP_FILTER_BITMASK
4220 av1_loop_filter_frame(cm->frame_to_show, cm, xd, 0, num_planes, 0);
4221#else
Deepa K G964e72e2018-05-16 16:56:01 +05304222 if (cpi->num_workers > 1)
4223 av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd, 0, num_planes, 0,
4224 cpi->workers, cpi->num_workers,
4225 &cpi->lf_row_sync);
4226 else
4227 av1_loop_filter_frame(cm->frame_to_show, cm, xd, 0, num_planes, 0);
Cheng Chen8ab1f442018-04-27 18:01:52 -07004228#endif
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004229 }
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004230
Yaowu Xu35ee2342017-11-08 11:50:46 -08004231 if (!no_restoration)
4232 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004233
Yaowu Xu35ee2342017-11-08 11:50:46 -08004234 if (no_cdef) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004235 cm->cdef_bits = 0;
4236 cm->cdef_strengths[0] = 0;
4237 cm->nb_cdef_strengths = 1;
Yunqing Wangdad63d42017-12-08 12:45:07 -08004238 cm->cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004239 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004240 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004241 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004242 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004243
4244 // Apply the filter
4245 av1_cdef_frame(cm->frame_to_show, cm, xd);
4246 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004247
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004248 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004249
Yaowu Xu35ee2342017-11-08 11:50:46 -08004250 if (no_restoration) {
4251 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4252 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4253 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4254 } else {
4255 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 1);
4256 av1_pick_filter_restoration(cpi->source, cpi);
4257 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4258 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4259 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304260 if (cpi->num_workers > 1)
4261 av1_loop_restoration_filter_frame_mt(cm->frame_to_show, cm, 0,
4262 cpi->workers, cpi->num_workers,
4263 &cpi->lr_row_sync, &cpi->lr_ctxt);
4264 else
4265 av1_loop_restoration_filter_frame(cm->frame_to_show, cm, 0,
4266 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004267 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004268 }
Fergus Simpsonbc189932017-05-16 17:02:39 -07004269}
4270
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004271static int encode_without_recode_loop(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004272 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004273 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004274
Yaowu Xuf883b422016-08-30 14:01:10 -07004275 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004276
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004277 set_size_independent_vars(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004278
Fergus Simpsonbc189932017-05-16 17:02:39 -07004279 setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004280
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004281 assert(cm->width == cpi->scaled_source.y_crop_width);
4282 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004283
Yaowu Xuc27fc142016-08-22 16:08:15 -07004284 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4285
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004286 cpi->source =
4287 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4288 if (cpi->unscaled_last_source != NULL)
4289 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4290 &cpi->scaled_last_source);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004291 cpi->source->buf_8bit_valid = 0;
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004292 if (frame_is_intra_only(cm) == 0) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004293 scale_references(cpi);
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004294 }
4295
Yaowu Xuf883b422016-08-30 14:01:10 -07004296 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004297 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004298 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004299
Yaowu Xuc27fc142016-08-22 16:08:15 -07004300 // Variance adaptive and in frame q adjustment experiments are mutually
4301 // exclusive.
4302 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004303 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004304 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004305 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004306 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004307 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004308 }
4309 apply_active_map(cpi);
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004310 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004311 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004312 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004313 } else {
4314 calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004315 }
David Barkercab37552018-03-21 11:56:24 +00004316 } else {
4317 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004318 }
David Barkercab37552018-03-21 11:56:24 +00004319 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004320
4321 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004322 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004323
4324 // Update some stats from cyclic refresh, and check if we should not update
4325 // golden reference, for 1 pass CBR.
4326 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004327 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4328 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004329
4330 // Update the skip mb flag probabilities based on the distribution
4331 // seen in the last encoder iteration.
4332 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004333 aom_clear_system_state();
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004334 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004335}
4336
Tom Finegane4099e32018-01-23 12:01:51 -08004337static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004338 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004339 RATE_CONTROL *const rc = &cpi->rc;
4340 int bottom_index, top_index;
4341 int loop_count = 0;
4342 int loop_at_this_size = 0;
4343 int loop = 0;
4344 int overshoot_seen = 0;
4345 int undershoot_seen = 0;
4346 int frame_over_shoot_limit;
4347 int frame_under_shoot_limit;
4348 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004349
4350 set_size_independent_vars(cpi);
4351
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004352 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004353
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004354 aom_clear_system_state();
4355 setup_frame_size(cpi);
4356 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4357
Yaowu Xuc27fc142016-08-22 16:08:15 -07004358 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004359 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004360
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004361 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004362 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4363 set_mv_search_params(cpi);
4364
4365 // Reset the loop state for new frame size.
4366 overshoot_seen = 0;
4367 undershoot_seen = 0;
4368
Yaowu Xuc27fc142016-08-22 16:08:15 -07004369 q_low = bottom_index;
4370 q_high = top_index;
4371
4372 loop_at_this_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004373
Urvang Joshi2a74cf22017-12-18 17:21:41 -08004374 // Decide frame size bounds first time through.
Yaowu Xuf883b422016-08-30 14:01:10 -07004375 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4376 &frame_under_shoot_limit,
4377 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004378 }
4379
Urvang Joshif1fa6862018-01-08 16:39:33 -08004380 // if frame was scaled calculate global_motion_search again if already
4381 // done
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004382 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4383 if (cpi->source->y_crop_width != cm->width ||
4384 cpi->source->y_crop_height != cm->height)
4385 cpi->global_motion_search_done = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004386 cpi->source =
4387 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004388 if (cpi->unscaled_last_source != NULL)
4389 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4390 &cpi->scaled_last_source);
4391
Yaowu Xuc27fc142016-08-22 16:08:15 -07004392 if (frame_is_intra_only(cm) == 0) {
4393 if (loop_count > 0) {
4394 release_scaled_references(cpi);
4395 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004396 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004397 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004398 av1_set_quantizer(cm, q);
Yaowu Xu2e587232018-05-10 09:30:08 -07004399 // printf("Frame %d/%d: q = %d, frame_type = %d\n", cm->current_video_frame,
4400 // cm->show_frame, q, cm->frame_type);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004401
4402 if (loop_count == 0) setup_frame(cpi);
4403
Yaowu Xuc27fc142016-08-22 16:08:15 -07004404 // Base q-index may have changed, so we need to assign proper default coef
4405 // probs before every iteration.
David Barkercc615a82018-03-19 14:38:51 +00004406 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
4407 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004408 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004409 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004410 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004411
Yaowu Xuc27fc142016-08-22 16:08:15 -07004412 // Variance adaptive and in frame q adjustment experiments are mutually
4413 // exclusive.
4414 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004415 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004416 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004417 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004418 }
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004419 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004420 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004421 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004422 } else {
4423 calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004424 }
David Barkercab37552018-03-21 11:56:24 +00004425 } else {
4426 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004427 }
David Barkercab37552018-03-21 11:56:24 +00004428 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004429
4430 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004431 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004432 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004433
4434 // Update the skip mb flag probabilities based on the distribution
4435 // seen in the last encoder iteration.
4436 // update_base_skip_probs(cpi);
4437
Yaowu Xuf883b422016-08-30 14:01:10 -07004438 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004439
4440 // Dummy pack of the bitstream using up to date stats to get an
4441 // accurate estimate of output frame size to determine if we need
4442 // to recode.
4443 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004444 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004445
4446 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4447 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004448
4449 rc->projected_frame_size = (int)(*size) << 3;
4450 restore_coding_context(cpi);
4451
4452 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4453 }
4454
Yaowu Xuf883b422016-08-30 14:01:10 -07004455 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004456 loop = 0;
4457 } else {
4458 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4459 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4460 int last_q = q;
4461 int64_t kf_err;
4462
4463 int64_t high_err_target = cpi->ambient_err;
4464 int64_t low_err_target = cpi->ambient_err >> 1;
4465
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004466 if (cm->seq_params.use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004467 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004468 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004469 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004470 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004471 // Prevent possible divide by zero error below for perfect KF
4472 kf_err += !kf_err;
4473
4474 // The key frame is not good enough or we can afford
4475 // to make it better without undue risk of popping.
4476 if ((kf_err > high_err_target &&
4477 rc->projected_frame_size <= frame_over_shoot_limit) ||
4478 (kf_err > low_err_target &&
4479 rc->projected_frame_size <= frame_under_shoot_limit)) {
4480 // Lower q_high
4481 q_high = q > q_low ? q - 1 : q_low;
4482
4483 // Adjust Q
4484 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004485 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004486 } else if (kf_err < low_err_target &&
4487 rc->projected_frame_size >= frame_under_shoot_limit) {
4488 // The key frame is much better than the previous frame
4489 // Raise q_low
4490 q_low = q < q_high ? q + 1 : q_high;
4491
4492 // Adjust Q
4493 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004494 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004495 }
4496
4497 // Clamp Q to upper and lower limits:
4498 q = clamp(q, q_low, q_high);
4499
4500 loop = q != last_q;
4501 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4502 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004503 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004504 // Is the projected frame size out of range and are we allowed
4505 // to attempt to recode.
4506 int last_q = q;
4507 int retries = 0;
4508
Yaowu Xuc27fc142016-08-22 16:08:15 -07004509 // Frame size out of permitted range:
4510 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004511 // Frame is too large
4512 if (rc->projected_frame_size > rc->this_frame_target) {
4513 // Special case if the projected size is > the max allowed.
4514 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4515 q_high = rc->worst_quality;
4516
4517 // Raise Qlow as to at least the current value
4518 q_low = q < q_high ? q + 1 : q_high;
4519
4520 if (undershoot_seen || loop_at_this_size > 1) {
4521 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004522 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004523
4524 q = (q_high + q_low + 1) / 2;
4525 } else {
4526 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004527 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004528
Yaowu Xuf883b422016-08-30 14:01:10 -07004529 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004530 AOMMAX(q_high, top_index), cm->width,
4531 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004532
4533 while (q < q_low && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004534 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004535 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004536 AOMMAX(q_high, top_index), cm->width,
4537 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004538 retries++;
4539 }
4540 }
4541
4542 overshoot_seen = 1;
4543 } else {
4544 // Frame is too small
4545 q_high = q > q_low ? q - 1 : q_low;
4546
4547 if (overshoot_seen || loop_at_this_size > 1) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004548 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004549 q = (q_high + q_low) / 2;
4550 } else {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004551 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004552 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004553 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004554 // Special case reset for qlow for constrained quality.
4555 // This should only trigger where there is very substantial
4556 // undershoot on a frame and the auto cq level is above
4557 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004558 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004559 q_low = q;
4560 }
4561
4562 while (q > q_high && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004563 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004564 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004565 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004566 retries++;
4567 }
4568 }
4569
4570 undershoot_seen = 1;
4571 }
4572
4573 // Clamp Q to upper and lower limits:
4574 q = clamp(q, q_low, q_high);
4575
4576 loop = (q != last_q);
4577 } else {
4578 loop = 0;
4579 }
4580 }
4581
4582 // Special case for overlay frame.
4583 if (rc->is_src_frame_alt_ref &&
4584 rc->projected_frame_size < rc->max_frame_bandwidth)
4585 loop = 0;
4586
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004587 if (recode_loop_test_global_motion(cpi)) {
4588 loop = 1;
4589 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004590
Yaowu Xuc27fc142016-08-22 16:08:15 -07004591 if (loop) {
4592 ++loop_count;
4593 ++loop_at_this_size;
4594
4595#if CONFIG_INTERNAL_STATS
4596 ++cpi->tot_recode_hits;
4597#endif
4598 }
4599 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004600
4601 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004602}
4603
Yaowu Xuf883b422016-08-30 14:01:10 -07004604static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004605 const int *const map = cpi->common.ref_frame_map;
4606
Zoe Liu368bf162017-11-03 20:10:19 -07004607 // No.1 Priority: LAST_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004608 const int last2_is_last = map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[0]];
4609 const int last3_is_last = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[0]];
4610 const int gld_is_last =
4611 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4612 const int bwd_is_last =
4613 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4614 const int alt2_is_last =
4615 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4616 const int alt_is_last =
4617 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004618
Zoe Liu368bf162017-11-03 20:10:19 -07004619 // No.2 Priority: ALTREF_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004620 const int last2_is_alt =
4621 map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4622 const int last3_is_alt =
4623 map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4624 const int gld_is_alt = map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] ==
4625 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4626 const int bwd_is_alt = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4627 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4628 const int alt2_is_alt = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4629 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004630
Zoe Liu368bf162017-11-03 20:10:19 -07004631 // No.3 Priority: LAST2_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004632 const int last3_is_last2 = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[1]];
4633 const int gld_is_last2 =
4634 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4635 const int bwd_is_last2 =
4636 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4637 const int alt2_is_last2 =
4638 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004639
Zoe Liu368bf162017-11-03 20:10:19 -07004640 // No.4 Priority: LAST3_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004641 const int gld_is_last3 =
4642 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4643 const int bwd_is_last3 =
4644 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4645 const int alt2_is_last3 =
4646 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
Zoe Liu368bf162017-11-03 20:10:19 -07004647
4648 // No.5 Priority: GOLDEN_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004649 const int bwd_is_gld = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4650 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
4651 const int alt2_is_gld = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4652 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
Zoe Liu368bf162017-11-03 20:10:19 -07004653
4654 // No.6 Priority: BWDREF_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004655 const int alt2_is_bwd = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4656 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]];
Zoe Liu368bf162017-11-03 20:10:19 -07004657
4658 // No.7 Priority: ALTREF2_FRAME
4659
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004660 // After av1_apply_encoding_flags() is called, cpi->ref_frame_flags might be
4661 // adjusted according to external encoder flags.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08004662 int flags = cpi->ext_ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004663
Yaowu Xuf883b422016-08-30 14:01:10 -07004664 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004665
Yaowu Xuf883b422016-08-30 14:01:10 -07004666 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004667
Yaowu Xuf883b422016-08-30 14:01:10 -07004668 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004669
Zoe Liu368bf162017-11-03 20:10:19 -07004670 if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004671
Zoe Liu368bf162017-11-03 20:10:19 -07004672 if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
4673 flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004674
Zoe Liu368bf162017-11-03 20:10:19 -07004675 if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 ||
4676 bwd_is_gld) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004677 (flags & AOM_BWD_FLAG))
4678 flags &= ~AOM_BWD_FLAG;
Zoe Liue9b15e22017-07-19 15:53:01 -07004679
Zoe Liu368bf162017-11-03 20:10:19 -07004680 if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
4681 alt2_is_gld || alt2_is_bwd) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004682 (flags & AOM_ALT2_FLAG))
4683 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004684
4685 return flags;
4686}
4687
Yaowu Xuf883b422016-08-30 14:01:10 -07004688static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004689 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07004690 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07004691 // Note: The overrides are valid only for the next frame passed
4692 // to encode_frame_to_data_rate() function
sarahparker9806fed2018-03-30 17:43:44 -07004693 if (cpi->ext_use_s_frame) cpi->common.frame_type = S_FRAME;
Sarah Parker50b6d6e2018-04-11 19:21:54 -07004694 cpi->common.force_primary_ref_none = cpi->ext_use_primary_ref_none;
Debargha Mukherjee52fb0472018-03-29 15:48:11 -07004695
Yaowu Xuc27fc142016-08-22 16:08:15 -07004696 if (cpi->ext_refresh_frame_context_pending) {
4697 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4698 cpi->ext_refresh_frame_context_pending = 0;
4699 }
4700 if (cpi->ext_refresh_frame_flags_pending) {
4701 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4702 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4703 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004704 cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
4705 cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004706 cpi->ext_refresh_frame_flags_pending = 0;
4707 }
sarahparker21dbca42018-03-30 17:43:44 -07004708 cpi->common.allow_ref_frame_mvs = cpi->ext_use_ref_frame_mvs;
Sarah Parker14feea42018-07-06 16:41:41 -07004709 // A keyframe is already error resilient and keyframes with
4710 // error_resilient_mode interferes with the use of show_existing_frame
4711 // when forward reference keyframes are enabled.
4712 cpi->common.error_resilient_mode =
4713 cpi->ext_use_error_resilient && cpi->common.frame_type != KEY_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004714}
4715
Yaowu Xuf883b422016-08-30 14:01:10 -07004716static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
James Zern7b9407a2016-05-18 23:48:05 -07004717 InterpFilter ifilter;
Zoe Liu27deb382018-03-27 15:13:56 -07004718 int ref_total[REF_FRAMES] = { 0 };
Yaowu Xuc27fc142016-08-22 16:08:15 -07004719 MV_REFERENCE_FRAME ref;
4720 int mask = 0;
4721 int arf_idx = ALTREF_FRAME;
4722
Zoe Liue9b15e22017-07-19 15:53:01 -07004723 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame ||
4724 cpi->refresh_alt2_ref_frame)
Yaowu Xuc27fc142016-08-22 16:08:15 -07004725 return mask;
4726
Yaowu Xuc27fc142016-08-22 16:08:15 -07004727 for (ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref)
4728 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter)
4729 ref_total[ref] += cpi->interp_filter_selected[ref][ifilter];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004730
4731 for (ifilter = EIGHTTAP_REGULAR; ifilter < SWITCHABLE_FILTERS; ++ifilter) {
4732 if ((ref_total[LAST_FRAME] &&
4733 cpi->interp_filter_selected[LAST_FRAME][ifilter] == 0) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004734 (ref_total[LAST2_FRAME] == 0 ||
4735 cpi->interp_filter_selected[LAST2_FRAME][ifilter] * 50 <
4736 ref_total[LAST2_FRAME]) &&
4737 (ref_total[LAST3_FRAME] == 0 ||
4738 cpi->interp_filter_selected[LAST3_FRAME][ifilter] * 50 <
4739 ref_total[LAST3_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004740 (ref_total[GOLDEN_FRAME] == 0 ||
4741 cpi->interp_filter_selected[GOLDEN_FRAME][ifilter] * 50 <
4742 ref_total[GOLDEN_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004743 (ref_total[BWDREF_FRAME] == 0 ||
4744 cpi->interp_filter_selected[BWDREF_FRAME][ifilter] * 50 <
4745 ref_total[BWDREF_FRAME]) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004746 (ref_total[ALTREF2_FRAME] == 0 ||
4747 cpi->interp_filter_selected[ALTREF2_FRAME][ifilter] * 50 <
4748 ref_total[ALTREF2_FRAME]) &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004749 (ref_total[ALTREF_FRAME] == 0 ||
4750 cpi->interp_filter_selected[arf_idx][ifilter] * 50 <
4751 ref_total[ALTREF_FRAME]))
4752 mask |= 1 << ifilter;
4753 }
4754 return mask;
4755}
4756
4757#define DUMP_RECON_FRAMES 0
4758
4759#if DUMP_RECON_FRAMES == 1
4760// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004761static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4762 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004763 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004764
Zoe Liub4f31032017-11-03 23:48:35 -07004765 if (recon_buf == NULL) {
4766 printf("Frame %d is not ready.\n", cm->current_video_frame);
4767 return;
4768 }
4769
Zoe Liu27deb382018-03-27 15:13:56 -07004770 static const int flag_list[REF_FRAMES] = { 0,
4771 AOM_LAST_FLAG,
4772 AOM_LAST2_FLAG,
4773 AOM_LAST3_FLAG,
4774 AOM_GOLD_FLAG,
4775 AOM_BWD_FLAG,
4776 AOM_ALT2_FLAG,
4777 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004778 printf(
4779 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4780 "show_existing_frame=%d) "
4781 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
4782 cm->current_video_frame, cm->frame_offset, cm->show_frame,
4783 cm->show_existing_frame);
4784 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4785 const int buf_idx = cm->frame_refs[ref_frame - LAST_FRAME].idx;
4786 const int ref_offset =
4787 (buf_idx >= 0)
4788 ? (int)cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset
4789 : -1;
Zoe Liuf452fdf2017-11-02 23:08:12 -07004790 printf(
4791 " %d(%c-%d-%4.2f)", ref_offset,
4792 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N',
4793 (buf_idx >= 0) ? (int)cpi->frame_rf_level[buf_idx] : -1,
4794 (buf_idx >= 0) ? rate_factor_deltas[cpi->frame_rf_level[buf_idx]] : -1);
Zoe Liub4f31032017-11-03 23:48:35 -07004795 }
4796 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004797
4798 if (!cm->show_frame) {
4799 printf("Frame %d is a no show frame, so no image dump.\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004800 cm->current_video_frame);
4801 return;
4802 }
4803
Zoe Liub4f31032017-11-03 23:48:35 -07004804 int h;
4805 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4806 FILE *f_recon = NULL;
4807
Yaowu Xuc27fc142016-08-22 16:08:15 -07004808 if (cm->current_video_frame == 0) {
4809 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4810 printf("Unable to open file %s to write.\n", file_name);
4811 return;
4812 }
4813 } else {
4814 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4815 printf("Unable to open file %s to append.\n", file_name);
4816 return;
4817 }
4818 }
4819 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004820 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4821 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
4822 "refresh_alt_ref_frame=%d, rf_level=%d, "
4823 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004824 cm->current_video_frame, cpi->twopass.gf_group.index,
4825 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liuf40a9572017-10-13 12:37:19 -07004826 cm->frame_offset, cm->show_frame, cm->show_existing_frame,
4827 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
4828 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4829 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004830#if 0
4831 int ref_frame;
4832 printf("get_ref_frame_map_idx: [");
4833 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
4834 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
4835 printf(" ]\n");
4836 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
4837 printf("cm->ref_frame_map = [");
4838 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4839 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
4840 }
4841 printf(" ]\n");
4842#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004843
4844 // --- Y ---
4845 for (h = 0; h < cm->height; ++h) {
4846 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4847 f_recon);
4848 }
4849 // --- U ---
4850 for (h = 0; h < (cm->height >> 1); ++h) {
4851 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4852 f_recon);
4853 }
4854 // --- V ---
4855 for (h = 0; h < (cm->height >> 1); ++h) {
4856 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4857 f_recon);
4858 }
4859
4860 fclose(f_recon);
4861}
4862#endif // DUMP_RECON_FRAMES
4863
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07004864static INLINE int is_frame_droppable(AV1_COMP *cpi) {
4865 return !(cpi->refresh_alt_ref_frame || cpi->refresh_alt2_ref_frame ||
4866 cpi->refresh_bwd_ref_frame || cpi->refresh_golden_frame ||
4867 cpi->refresh_last_frame);
4868}
4869
Tom Finegane4099e32018-01-23 12:01:51 -08004870static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size, uint8_t *dest,
4871 int skip_adapt,
4872 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004873 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004874 SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07004875 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004876 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01004877
Yaowu Xuc27fc142016-08-22 16:08:15 -07004878 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004879 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004880
Fangwen Fu8d164de2016-12-14 13:40:54 -08004881 // frame type has been decided outside of this function call
Zoe Liud78ce2d2018-02-24 05:50:57 -08004882 cm->cur_frame->intra_only = frame_is_intra_only(cm);
Zoe Liu2723a9d2018-02-22 20:17:00 -08004883 cm->cur_frame->frame_type = cm->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07004884
4885 // S_FRAMEs are always error resilient
sarahparker27d686a2018-03-30 17:43:44 -07004886 cm->error_resilient_mode |= frame_is_sframe(cm);
Yunqing Wang9612d552018-05-15 14:58:30 -07004887
4888 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
4889 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004890 if (cm->large_scale_tile) seq_params->frame_id_numbers_present_flag = 0;
Yunqing Wang9612d552018-05-15 14:58:30 -07004891
sarahparker21dbca42018-03-30 17:43:44 -07004892 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07004893 // cm->allow_ref_frame_mvs needs to be written into the frame header while
4894 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
4895 // separated from frame_might_allow_ref_frame_mvs().
4896 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
4897
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07004898 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07004899 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004900
Jingning Hand8a15a62017-10-30 10:53:42 -07004901 // Reset the frame packet stamp index.
Sarah Parkerb9041612018-05-22 19:06:47 -07004902 if (cm->frame_type == KEY_FRAME && cm->show_frame)
4903 cm->current_video_frame = 0;
Jingning Hand8a15a62017-10-30 10:53:42 -07004904
Yaowu Xuc27fc142016-08-22 16:08:15 -07004905 // NOTE:
4906 // (1) Move the setup of the ref_frame_flags upfront as it would be
4907 // determined by the current frame properties;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004908 // (2) The setup of the ref_frame_flags applies to both
4909 // show_existing_frame's
Yaowu Xuc27fc142016-08-22 16:08:15 -07004910 // and the other cases.
4911 if (cm->current_video_frame > 0)
4912 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4913
4914 if (cm->show_existing_frame) {
4915 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
4916 // BWDREF_FRAME in the reference frame buffer.
Sarah Parkerb9041612018-05-22 19:06:47 -07004917 if (cm->frame_type == KEY_FRAME) {
4918 cm->reset_decoder_state = 1;
4919 } else {
4920 cm->frame_type = INTER_FRAME;
4921 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004922 cm->show_frame = 1;
4923 cpi->frame_flags = *frame_flags;
4924
4925 // In the case of show_existing frame, we will not send fresh flag
4926 // to decoder. Any change in the reference frame buffer can be done by
4927 // switching the virtual indices.
4928
4929 cpi->refresh_last_frame = 0;
4930 cpi->refresh_golden_frame = 0;
4931 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07004932 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004933 cpi->refresh_alt_ref_frame = 0;
4934
4935 cpi->rc.is_bwd_ref_frame = 0;
4936 cpi->rc.is_last_bipred_frame = 0;
4937 cpi->rc.is_bipred_frame = 0;
4938
Jingning Han8f661602017-08-19 08:16:50 -07004939 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07004940
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
Yaowu Xuc27fc142016-08-22 16:08:15 -07004947 // Set up frame to show to get ready for stats collection.
4948 cm->frame_to_show = get_frame_new_buffer(cm);
4949
Zoe Liub4f31032017-11-03 23:48:35 -07004950 // Update current frame offset.
4951 cm->frame_offset =
4952 cm->buffer_pool->frame_bufs[cm->new_fb_idx].cur_frame_offset;
Zoe Liub4f31032017-11-03 23:48:35 -07004953
Yaowu Xuc27fc142016-08-22 16:08:15 -07004954#if DUMP_RECON_FRAMES == 1
4955 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4956 dump_filtered_recon_frames(cpi);
4957#endif // DUMP_RECON_FRAMES
4958
4959 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07004960 // NOTE:
4961 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
4962 // update has been done previously when handling the LAST_BIPRED_FRAME
4963 // right before BWDREF_FRAME (in the display order);
4964 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
Urvang Joshif1fa6862018-01-08 16:39:33 -08004965 // update will be done when the following is called, which will
4966 // exchange
Zoe Liue9b15e22017-07-19 15:53:01 -07004967 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
Urvang Joshif1fa6862018-01-08 16:39:33 -08004968 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2,
4969 // and
Zoe Liue9b15e22017-07-19 15:53:01 -07004970 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Cheng Chen46f30c72017-09-07 11:13:33 -07004971 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004972
4973 // Update frame flags
4974 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4975 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4976 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4977
4978 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4979
4980 // Update the frame type
4981 cm->last_frame_type = cm->frame_type;
4982
Yaowu Xuc27fc142016-08-22 16:08:15 -07004983 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4984 // to do post-encoding update accordingly.
4985 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004986 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004987 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004988 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004989
Wei-Ting Lin96ee0eb2018-06-22 15:27:22 -07004990 // Decrement count down till next gf
4991 if (cpi->rc.frames_till_gf_update_due > 0)
4992 cpi->rc.frames_till_gf_update_due--;
4993
Yaowu Xuc27fc142016-08-22 16:08:15 -07004994 ++cm->current_video_frame;
4995
Tom Finegane4099e32018-01-23 12:01:51 -08004996 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004997 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004998
4999 // Set default state for segment based loop filter update flags.
5000 cm->lf.mode_ref_delta_update = 0;
5001
5002 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5003 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
5004
5005 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05005006 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005007 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005008 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005009
5010 // If segmentation is enabled force a map update for key frames.
5011 if (seg->enabled) {
5012 seg->update_map = 1;
5013 seg->update_data = 1;
5014 }
5015
5016 // The alternate reference frame cannot be active for a key frame.
5017 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005018 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005019 if (cpi->oxcf.mtu == 0) {
5020 cm->num_tg = cpi->oxcf.num_tile_groups;
5021 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005022 // Use a default value for the purposes of weighting costs in probability
5023 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005024 cm->num_tg = DEFAULT_MAX_NUM_TG;
5025 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005026
5027 // For 1 pass CBR, check if we are dropping this frame.
5028 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005029 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005030 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005031 if (av1_rc_drop_frame(cpi)) {
5032 av1_rc_postencode_update_drop_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005033 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005034 }
5035 }
5036
Yaowu Xuf883b422016-08-30 14:01:10 -07005037 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005038
5039#if CONFIG_INTERNAL_STATS
5040 memset(cpi->mode_chosen_counts, 0,
5041 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5042#endif
5043
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005044 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005045 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08005046 * wraparound) */
Sebastien Alaiwand418f682017-10-19 15:06:52 +02005047 const int frame_id_length = FRAME_ID_LENGTH;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005048 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005049 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08005050 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07005051 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5052 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5053 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005054 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005055 lsb = cpi->source->y_buffer[0] & 0xff;
5056 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005057 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005058 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005059
5060 // S_frame is meant for stitching different streams of different
5061 // resolutions together, so current_frame_id must be the
5062 // same across different streams of the same content current_frame_id
5063 // should be the same and not random. 0x37 is a chosen number as start
5064 // point
5065 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005066 } else {
5067 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005068 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
5069 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005070 }
5071 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005072
Hui Su483a8452018-02-26 12:28:48 -08005073 switch (cpi->oxcf.cdf_update_mode) {
5074 case 0: // No CDF update for any frames(4~6% compression loss).
5075 cm->disable_cdf_update = 1;
5076 break;
5077 case 1: // Enable CDF update for all frames.
5078 cm->disable_cdf_update = 0;
5079 break;
5080 case 2:
5081 // Strategically determine at which frames to do CDF update.
5082 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5083 // compression loss).
5084 // TODO(huisu@google.com): design schemes for various trade-offs between
5085 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005086 cm->disable_cdf_update =
5087 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005088 break;
5089 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005090 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005091
Yaowu Xuc27fc142016-08-22 16:08:15 -07005092 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005093 if (encode_without_recode_loop(cpi) != AOM_CODEC_OK) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005094 } else {
Tom Finegane4099e32018-01-23 12:01:51 -08005095 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
5096 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005097 }
5098
Yi Luo10e23002017-07-31 11:54:43 -07005099 cm->last_tile_cols = cm->tile_cols;
5100 cm->last_tile_rows = cm->tile_rows;
5101
Yaowu Xuc27fc142016-08-22 16:08:15 -07005102#ifdef OUTPUT_YUV_SKINMAP
5103 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005104 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005105 }
5106#endif // OUTPUT_YUV_SKINMAP
5107
5108 // Special case code to reduce pulsing when key frames are forced at a
5109 // fixed interval. Note the reconstruction error if it is the frame before
5110 // the force key frame
5111 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005112 if (seq_params->use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005113 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07005114 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005115 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005116 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005117 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005118 }
5119
Tarek AMARAc9813852018-03-05 18:40:18 -05005120 // If the encoder forced a KEY_FRAME decision or if frame is an S_FRAME
Sarah Parkerb9041612018-05-22 19:06:47 -07005121 if ((cm->frame_type == KEY_FRAME && cm->show_frame) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005122 cpi->refresh_last_frame = 1;
5123 }
5124
5125 cm->frame_to_show = get_frame_new_buffer(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005126 cm->frame_to_show->color_primaries = seq_params->color_primaries;
5127 cm->frame_to_show->transfer_characteristics =
5128 seq_params->transfer_characteristics;
5129 cm->frame_to_show->matrix_coefficients = seq_params->matrix_coefficients;
5130 cm->frame_to_show->monochrome = seq_params->monochrome;
5131 cm->frame_to_show->chroma_sample_position =
5132 seq_params->chroma_sample_position;
5133 cm->frame_to_show->color_range = seq_params->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005134 cm->frame_to_show->render_width = cm->render_width;
5135 cm->frame_to_show->render_height = cm->render_height;
5136
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005137 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5138 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005139
5140 // Pick the loop filter level for the frame.
Cheng Chen68dc9142018-05-02 17:46:28 -07005141 if (!cm->allow_intrabc) {
David Barker218556e2018-02-14 14:23:12 +00005142 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08005143 } else {
Hui Su06463e42018-02-23 22:17:36 -08005144 cm->lf.filter_level[0] = 0;
5145 cm->lf.filter_level[1] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005146 cm->cdef_bits = 0;
5147 cm->cdef_strengths[0] = 0;
5148 cm->nb_cdef_strengths = 1;
5149 cm->cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005150 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5151 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5152 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08005153 }
David Barker218556e2018-02-14 14:23:12 +00005154
5155 // TODO(debargha): Fix mv search range on encoder side
5156 // aom_extend_frame_inner_borders(cm->frame_to_show, av1_num_planes(cm));
5157 aom_extend_frame_borders(cm->frame_to_show, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005158
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005159#ifdef OUTPUT_YUV_REC
5160 aom_write_one_yuv_frame(cm, cm->frame_to_show);
5161#endif
5162
Yaowu Xuc27fc142016-08-22 16:08:15 -07005163 // Build the bitstream
Tom Finegane4099e32018-01-23 12:01:51 -08005164 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
5165 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005166
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005167 cpi->seq_params_locked = 1;
5168
Hui Sudc54be62018-03-14 19:14:28 -07005169 if (skip_adapt) return AOM_CODEC_OK;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005170
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005171 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005172 int i;
Zoe Liu630a89f2018-04-16 09:36:50 -07005173 // Update reference frame id values based on the value of refresh_frame_mask
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005174 for (i = 0; i < REF_FRAMES; i++) {
Zoe Liu630a89f2018-04-16 09:36:50 -07005175 if ((cpi->refresh_frame_mask >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005176 cm->ref_frame_id[i] = cm->current_frame_id;
5177 }
5178 }
5179 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005180
Yaowu Xuc27fc142016-08-22 16:08:15 -07005181#if DUMP_RECON_FRAMES == 1
5182 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005183 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005184#endif // DUMP_RECON_FRAMES
5185
Soo-Chul Han934af352017-10-15 15:21:51 -04005186 if (cm->seg.enabled) {
5187 if (cm->seg.update_map) {
5188 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005189 } else if (cm->last_frame_seg_map) {
Soo-Chul Han934af352017-10-15 15:21:51 -04005190 memcpy(cm->current_frame_seg_map, cm->last_frame_seg_map,
5191 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5192 }
5193 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005194
5195 if (frame_is_intra_only(cm) == 0) {
5196 release_scaled_references(cpi);
5197 }
5198
Cheng Chen46f30c72017-09-07 11:13:33 -07005199 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005200
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005201#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005202 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005203#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005204
Hui Sudc54be62018-03-14 19:14:28 -07005205 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
5206 *cm->fc = cpi->tile_data[cm->largest_tile_id].tctx;
5207 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005208 }
5209
5210 if (cpi->refresh_golden_frame == 1)
5211 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5212 else
5213 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5214
5215 if (cpi->refresh_alt_ref_frame == 1)
5216 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5217 else
5218 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5219
Yaowu Xuc27fc142016-08-22 16:08:15 -07005220 if (cpi->refresh_bwd_ref_frame == 1)
5221 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5222 else
5223 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005224
Yaowu Xuc27fc142016-08-22 16:08:15 -07005225 cm->last_frame_type = cm->frame_type;
5226
Yaowu Xuf883b422016-08-30 14:01:10 -07005227 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005228
Yaowu Xuc27fc142016-08-22 16:08:15 -07005229 if (cm->frame_type == KEY_FRAME) {
5230 // Tell the caller that the frame was coded as a key frame
5231 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5232 } else {
5233 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5234 }
5235
5236 // Clear the one shot update flags for segmentation map and mode/ref loop
5237 // filter deltas.
5238 cm->seg.update_map = 0;
5239 cm->seg.update_data = 0;
5240 cm->lf.mode_ref_delta_update = 0;
5241
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005242 // A droppable frame might not be shown but it always
5243 // takes a space in the gf group. Therefore, even when
5244 // it is not shown, we still need update the count down.
5245
5246 // TODO(weitinglin): This is a work-around to handle the condition
5247 // when a frame is drop. We should fix the cm->show_frame flag
5248 // instead of checking the other condition to update the counter properly.
5249 if (cm->show_frame || is_frame_droppable(cpi)) {
5250 // Decrement count down till next gf
5251 if (cpi->rc.frames_till_gf_update_due > 0)
5252 cpi->rc.frames_till_gf_update_due--;
5253 }
5254
Yaowu Xuc27fc142016-08-22 16:08:15 -07005255 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08005256 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5257 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005258 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005259 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005260 // Don't increment frame counters if this was an altref buffer
5261 // update not a real frame
Wei-Ting Lin96ee0eb2018-06-22 15:27:22 -07005262
Yaowu Xuc27fc142016-08-22 16:08:15 -07005263 ++cm->current_video_frame;
5264 }
5265
Yaowu Xuc27fc142016-08-22 16:08:15 -07005266 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005267 if (cm->is_reference_frame) {
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005268 // keep track of the last coded dimensions
5269 cm->last_width = cm->width;
5270 cm->last_height = cm->height;
5271
5272 // reset to normal state now that we are done.
5273 cm->last_show_frame = cm->show_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005274 }
Yi Luo10e23002017-07-31 11:54:43 -07005275
Tom Finegane4099e32018-01-23 12:01:51 -08005276 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005277}
5278
Tom Finegane4099e32018-01-23 12:01:51 -08005279static int Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5280 int skip_adapt, unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005281 if (cpi->oxcf.rc_mode == AOM_CBR) {
5282 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005283 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005284 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005285 }
Debargha Mukherjeeff48c092018-04-04 23:53:40 -07005286 if (encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags) !=
5287 AOM_CODEC_OK) {
5288 return AOM_CODEC_ERROR;
5289 }
5290 check_show_existing_frame(cpi);
5291 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005292}
5293
Tom Finegane4099e32018-01-23 12:01:51 -08005294static int Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5295 unsigned int *frame_flags) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -08005296#if CONFIG_MISMATCH_DEBUG
5297 mismatch_move_frame_idx_w();
5298#endif
Angie Chiang4d55d762017-12-13 16:18:37 -08005299#if TXCOEFF_COST_TIMER
5300 AV1_COMMON *cm = &cpi->common;
5301 cm->txcoeff_cost_timer = 0;
5302 cm->txcoeff_cost_count = 0;
5303#endif
Tom Finegane4099e32018-01-23 12:01:51 -08005304
5305 if (encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags) !=
5306 AOM_CODEC_OK) {
5307 return AOM_CODEC_ERROR;
5308 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005309
Angie Chiang4d55d762017-12-13 16:18:37 -08005310#if TXCOEFF_COST_TIMER
5311 cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer;
5312 fprintf(stderr,
5313 "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld "
5314 "in us\n",
5315 cm->txcoeff_cost_count, cm->txcoeff_cost_timer,
5316 cm->cum_txcoeff_cost_timer);
5317#endif
5318
Urvang Joshif1fa6862018-01-08 16:39:33 -08005319 // Do not do post-encoding update for those frames that do not have a spot
5320 // in
5321 // a gf group, but note that an OVERLAY frame always has a spot in a gf
5322 // group,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005323 // even when show_existing_frame is used.
5324 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005325 av1_twopass_postencode_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005326 }
5327 check_show_existing_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005328 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005329}
5330
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005331#if CONFIG_DENOISE
5332static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5333 int block_size, float noise_level,
5334 int64_t time_stamp, int64_t end_time) {
5335 AV1_COMMON *const cm = &cpi->common;
5336 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005337 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5338 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005339 if (!cpi->denoise_and_model) {
5340 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5341 "Error allocating denoise and model");
5342 return -1;
5343 }
5344 }
5345 if (!cpi->film_grain_table) {
5346 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5347 if (!cpi->film_grain_table) {
5348 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5349 "Error allocating grain table");
5350 return -1;
5351 }
5352 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5353 }
5354 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5355 &cm->film_grain_params)) {
5356 if (cm->film_grain_params.apply_grain) {
5357 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5358 &cm->film_grain_params);
5359 }
5360 }
5361 return 0;
5362}
5363#endif
5364
James Zern3e2613b2017-03-30 23:14:40 -07005365int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005366 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5367 int64_t end_time) {
5368 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005369 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07005370 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005371 int res = 0;
5372 const int subsampling_x = sd->subsampling_x;
5373 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005374 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005375
Yaowu Xuc27fc142016-08-22 16:08:15 -07005376 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005377
Yaowu Xuf883b422016-08-30 14:01:10 -07005378 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005379
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005380#if CONFIG_DENOISE
5381 if (cpi->oxcf.noise_level > 0)
5382 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5383 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5384 res = -1;
5385#endif // CONFIG_DENOISE
5386
Yaowu Xuf883b422016-08-30 14:01:10 -07005387 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005388 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005389 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005390 aom_usec_timer_mark(&timer);
5391 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005392
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005393 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005394 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005395 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005396 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005397 res = -1;
5398 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005399 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005400 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005401 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005402 "Profile 1 requires 4:4:4 color format");
5403 res = -1;
5404 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005405 if ((seq_params->profile == PROFILE_2) &&
5406 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005407 !(subsampling_x == 1 && subsampling_y == 0)) {
5408 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5409 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005410 res = -1;
5411 }
5412
5413 return res;
5414}
5415
Yaowu Xuf883b422016-08-30 14:01:10 -07005416static int frame_is_reference(const AV1_COMP *cpi) {
5417 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005418
5419 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005420 cpi->refresh_golden_frame || cpi->refresh_bwd_ref_frame ||
5421 cpi->refresh_alt2_ref_frame || cpi->refresh_alt_ref_frame ||
5422 !cm->error_resilient_mode || cm->lf.mode_ref_delta_update ||
5423 cm->seg.update_map || cm->seg.update_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005424}
5425
Yaowu Xuf883b422016-08-30 14:01:10 -07005426static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005427 const struct lookahead_entry *source) {
5428 int64_t this_duration;
5429 int step = 0;
5430
5431 if (source->ts_start == cpi->first_time_stamp_ever) {
5432 this_duration = source->ts_end - source->ts_start;
5433 step = 1;
5434 } else {
5435 int64_t last_duration =
5436 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5437
5438 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5439
5440 // do a step update if the duration changes by 10%
5441 if (last_duration)
5442 step = (int)((this_duration - last_duration) * 10 / last_duration);
5443 }
5444
5445 if (this_duration) {
5446 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005447 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005448 } else {
5449 // Average this frame's rate into the last second's average
5450 // frame rate. If we haven't seen 1 second yet, then average
5451 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005452 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005453 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5454 double avg_duration = 10000000.0 / cpi->framerate;
5455 avg_duration *= (interval - avg_duration + this_duration);
5456 avg_duration /= interval;
5457
Yaowu Xuf883b422016-08-30 14:01:10 -07005458 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005459 }
5460 }
5461 cpi->last_time_stamp_seen = source->ts_start;
5462 cpi->last_end_time_stamp_seen = source->ts_end;
5463}
5464
5465// Returns 0 if this is not an alt ref else the offset of the source frame
5466// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005467static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005468 RATE_CONTROL *const rc = &cpi->rc;
5469 int arf_src_index = 0;
5470 if (is_altref_enabled(cpi)) {
5471 if (cpi->oxcf.pass == 2) {
5472 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5473 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5474 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5475 }
5476 } else if (rc->source_alt_ref_pending) {
5477 arf_src_index = rc->frames_till_gf_update_due;
5478 }
5479 }
5480 return arf_src_index;
5481}
5482
Yaowu Xuf883b422016-08-30 14:01:10 -07005483static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005484 int brf_src_index = 0;
5485 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5486
5487 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5488 // flag.
5489 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5490 if (cpi->oxcf.pass == 2) {
5491 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5492 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5493 } else {
5494 // TODO(zoeliu): To re-visit the setup for this scenario
5495 brf_src_index = cpi->rc.bipred_group_interval - 1;
5496 }
5497 }
5498
5499 return brf_src_index;
5500}
Zoe Liue9b15e22017-07-19 15:53:01 -07005501
Zoe Liue9b15e22017-07-19 15:53:01 -07005502// Returns 0 if this is not an alt ref else the offset of the source frame
5503// used as the arf midpoint.
5504static int get_arf2_src_index(AV1_COMP *cpi) {
5505 int arf2_src_index = 0;
5506 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5507 if (cpi->oxcf.pass == 2) {
5508 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5509 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5510 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5511 }
5512 }
5513 }
5514 return arf2_src_index;
5515}
Yaowu Xuc27fc142016-08-22 16:08:15 -07005516
Yaowu Xuf883b422016-08-30 14:01:10 -07005517static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005518 const struct lookahead_entry *source) {
5519 RATE_CONTROL *const rc = &cpi->rc;
5520
5521 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005522 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005523
5524 if (cpi->oxcf.pass == 2) {
5525 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5526 rc->is_src_frame_alt_ref =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005527 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07005528 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07005529 rc->is_src_frame_ext_arf =
5530 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005531 } else {
5532 rc->is_src_frame_alt_ref =
5533 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5534 }
5535
5536 if (rc->is_src_frame_alt_ref) {
5537 // Current frame is an ARF overlay frame.
5538 cpi->alt_ref_source = NULL;
5539
Zoe Liue9b15e22017-07-19 15:53:01 -07005540 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
5541 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
5542 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
5543 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
5544 cpi->refresh_last_frame = 1;
5545 } else {
Zoe Liue9b15e22017-07-19 15:53:01 -07005546 // Don't refresh the last buffer for an ARF overlay frame. It will
5547 // become the GF so preserve last as an alternative prediction option.
5548 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005549 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005550 }
5551}
5552
5553#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005554extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5555 const unsigned char *img2, int img2_pitch,
5556 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005557
5558static void adjust_image_stat(double y, double u, double v, double all,
5559 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07005560 s->stat[STAT_Y] += y;
5561 s->stat[STAT_U] += u;
5562 s->stat[STAT_V] += v;
5563 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005564 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005565}
5566
Angie Chiang08a22a62017-07-17 17:29:17 -07005567static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005568 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005569 double samples = 0.0;
5570 uint32_t in_bit_depth = 8;
5571 uint32_t bit_depth = 8;
5572
Angie Chiang08a22a62017-07-17 17:29:17 -07005573#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07005574 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005575#endif
5576 cpi->bytes += frame_bytes;
5577
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005578 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005579 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005580 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005581 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005582 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005583 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005584 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5585 double y, u, v, frame_all;
5586
5587 cpi->count++;
5588 if (cpi->b_calculate_psnr) {
5589 PSNR_STATS psnr;
5590 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005591 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005592 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005593 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005594
5595 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5596 &cpi->psnr);
5597 cpi->total_sq_error += psnr.sse[0];
5598 cpi->total_samples += psnr.samples[0];
5599 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005600 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005601 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005602 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005603 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005604 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005605 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005606
Yaowu Xuf883b422016-08-30 14:01:10 -07005607 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005608 cpi->summed_quality += frame_ssim2 * weight;
5609 cpi->summed_weights += weight;
5610
5611#if 0
5612 {
5613 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005614 double y2 = psnr.psnr[1];
5615 double u2 = psnr.psnr[2];
5616 double v2 = psnr.psnr[3];
5617 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005618 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 -08005619 cm->current_video_frame, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005620 frame_psnr2, frame_ssim2);
5621 fclose(f);
5622 }
5623#endif
5624 }
5625 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005626 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005627 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005628 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5629 recon->y_stride, orig->y_width, orig->y_height);
5630 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005631 cpi->total_blockiness += frame_blockiness;
5632 }
5633
5634 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005635 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005636 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005637 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5638 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5639
5640 const double peak = (double)((1 << in_bit_depth) - 1);
5641 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005642 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005643 if (consistency > 0.0)
5644 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005645 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005646 cpi->total_inconsistency += this_inconsistency;
5647 }
5648 }
5649 }
5650
5651 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005652 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005653 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005654 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005655 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5656 }
5657}
5658#endif // CONFIG_INTERNAL_STATS
5659
RogerZhou3b635242017-09-19 10:06:46 -07005660static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5661 const YV12_BUFFER_CONFIG *last_picture,
5662 hash_table *last_hash_table) {
5663 aom_clear_system_state();
5664 // check use hash ME
5665 int k;
5666 uint32_t hash_value_1;
5667 uint32_t hash_value_2;
5668
5669 const int block_size = 8;
5670 const double threshold_current = 0.8;
5671 const double threshold_average = 0.95;
5672 const int max_history_size = 32;
5673 int T = 0; // total block
5674 int C = 0; // match with collocated block
5675 int S = 0; // smooth region but not match with collocated block
5676 int M = 0; // match with other block
5677
5678 const int pic_width = cur_picture->y_width;
5679 const int pic_height = cur_picture->y_height;
5680 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5681 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5682 const int x_pos = j;
5683 const int y_pos = i;
5684 int match = 1;
5685 T++;
5686
5687 // check whether collocated block match with current
5688 uint8_t *p_cur = cur_picture->y_buffer;
5689 uint8_t *p_ref = last_picture->y_buffer;
5690 int stride_cur = cur_picture->y_stride;
5691 int stride_ref = last_picture->y_stride;
5692 p_cur += (y_pos * stride_cur + x_pos);
5693 p_ref += (y_pos * stride_ref + x_pos);
5694
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005695 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5696 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5697 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5698 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5699 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5700 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5701 match = 0;
5702 }
RogerZhou3b635242017-09-19 10:06:46 -07005703 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005704 p16_cur += stride_cur;
5705 p16_ref += stride_ref;
RogerZhou3b635242017-09-19 10:06:46 -07005706 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005707 } else {
5708 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5709 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5710 if (p_cur[tmpX] != p_ref[tmpX]) {
5711 match = 0;
5712 }
5713 }
5714 p_cur += stride_cur;
5715 p_ref += stride_ref;
5716 }
RogerZhou3b635242017-09-19 10:06:46 -07005717 }
5718
5719 if (match) {
5720 C++;
5721 continue;
5722 }
5723
5724 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5725 y_pos) ||
5726 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5727 S++;
5728 continue;
5729 }
5730
5731 av1_get_block_hash_value(
5732 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005733 block_size, &hash_value_1, &hash_value_2,
5734 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH));
5735 // Hashing does not work for highbitdepth currently.
5736 // TODO(Roger): Make it work for highbitdepth.
5737 if (av1_use_hash_me(&cpi->common)) {
5738 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5739 M++;
5740 }
RogerZhou3b635242017-09-19 10:06:46 -07005741 }
5742 }
5743 }
5744
5745 assert(T > 0);
5746 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5747 double m_rate = ((double)(M)) / ((double)(T));
5748
5749 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5750 cpi->m_rate_array[cpi->rate_index] = m_rate;
5751
5752 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5753 cpi->rate_size++;
5754 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5755
5756 if (csm_rate < threshold_current) {
5757 return 0;
5758 }
5759
5760 if (C == T) {
5761 return 1;
5762 }
5763
5764 double csm_average = 0.0;
5765 double m_average = 0.0;
5766
5767 for (k = 0; k < cpi->rate_size; k++) {
5768 csm_average += cpi->csm_rate_array[k];
5769 m_average += cpi->m_rate_array[k];
5770 }
5771 csm_average /= cpi->rate_size;
5772 m_average /= cpi->rate_size;
5773
5774 if (csm_average < threshold_average) {
5775 return 0;
5776 }
5777
5778 if (M > (T - C - S) / 3) {
5779 return 1;
5780 }
5781
5782 if (csm_rate > 0.99 && m_rate > 0.01) {
5783 return 1;
5784 }
5785
5786 if (csm_average + m_average > 1.01) {
5787 return 1;
5788 }
5789
5790 return 0;
5791}
RogerZhou3b635242017-09-19 10:06:46 -07005792
Andrey Norkin795ba872018-03-06 13:24:14 -08005793int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5794 size_t *size, uint8_t *dest, int64_t *time_stamp,
5795 int64_t *time_end, int flush,
5796 const aom_rational_t *timebase) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005797 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5798 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005799 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005800 BufferPool *const pool = cm->buffer_pool;
5801 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07005802 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005803 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5804 struct lookahead_entry *last_source = NULL;
5805 struct lookahead_entry *source = NULL;
5806 int arf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005807 int brf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005808 int i;
5809
5810#if CONFIG_BITSTREAM_DEBUG
5811 assert(cpi->oxcf.max_threads == 0 &&
5812 "bitstream debug tool does not support multithreading");
5813 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07005814 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005815#endif
5816
Dominic Symesd4929012018-01-31 17:32:01 +01005817 cm->showable_frame = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005818 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005819
RogerZhou3b635242017-09-19 10:06:46 -07005820 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005821
5822 // Is multi-arf enabled.
5823 // Note that at the moment multi_arf is only configured for 2 pass VBR
5824 if ((oxcf->pass == 2) && (cpi->oxcf.enable_auto_arf > 1))
5825 cpi->multi_arf_allowed = 1;
5826 else
5827 cpi->multi_arf_allowed = 0;
5828
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07005829 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07005830 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
5831 ? REFRESH_FRAME_CONTEXT_DISABLED
5832 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005833 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08005834 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005835
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07005836 // default reference buffers update config
5837 av1_configure_buffer_updates_firstpass(cpi, LF_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005838
Sarah Parkerb9041612018-05-22 19:06:47 -07005839 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005840 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005841 cm->reset_decoder_state = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005842
Sarah Parker7e13da82018-04-30 17:47:24 -07005843 // Don't allow a show_existing_frame to coincide with an error resilient or
Sarah Parker14feea42018-07-06 16:41:41 -07005844 // S-Frame. An exception can be made in the case of a keyframe, since it
5845 // does not depend on any previous frames. We must make this exception here
5846 // because of the use of show_existing_frame with forward coded keyframes.
Sarah Parker7e13da82018-04-30 17:47:24 -07005847 struct lookahead_entry *lookahead_src = NULL;
5848 if (cm->current_video_frame > 0)
5849 lookahead_src = av1_lookahead_peek(cpi->lookahead, 0);
5850 if (lookahead_src != NULL &&
5851 ((cpi->oxcf.error_resilient_mode |
5852 ((lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT) != 0)) ||
5853 (cpi->oxcf.s_frame_mode |
Sarah Parker14feea42018-07-06 16:41:41 -07005854 ((lookahead_src->flags & AOM_EFLAG_SET_S_FRAME) != 0))) &&
5855 !(rc->frames_to_key == 0 || (cpi->frame_flags & FRAMEFLAGS_KEY))) {
Sarah Parker7e13da82018-04-30 17:47:24 -07005856 cm->show_existing_frame = 0;
5857 }
5858
Yaowu Xuc27fc142016-08-22 16:08:15 -07005859 if (oxcf->pass == 2 && cm->show_existing_frame) {
5860 // Manage the source buffer and flush out the source frame that has been
5861 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07005862 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005863 *size = 0;
5864 return -1;
5865 }
sarahparker21dbca42018-03-30 17:43:44 -07005866 av1_apply_encoding_flags(cpi, source->flags);
Alex Conversef77fd0b2017-04-20 11:00:24 -07005867 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005868 // TODO(zoeliu): To track down to determine whether it's needed to adjust
5869 // the frame rate.
5870 *time_stamp = source->ts_start;
5871 *time_end = source->ts_end;
5872
5873 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005874 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005875
Urvang Joshif1fa6862018-01-08 16:39:33 -08005876 // Find a free buffer for the new frame, releasing the reference
5877 // previously
Yaowu Xuc27fc142016-08-22 16:08:15 -07005878 // held.
5879 if (cm->new_fb_idx != INVALID_IDX) {
5880 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5881 }
5882 cm->new_fb_idx = get_free_fb(cm);
5883
5884 if (cm->new_fb_idx == INVALID_IDX) return -1;
5885
5886 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005887 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005888
5889 // Start with a 0 size frame.
5890 *size = 0;
5891
5892 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005893 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005894
Tom Finegane4099e32018-01-23 12:01:51 -08005895 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
5896 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005897
5898 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
5899
5900#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07005901 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005902#endif // CONFIG_INTERNAL_STATS
5903
5904 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005905 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005906
5907 cm->show_existing_frame = 0;
5908 return 0;
5909 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005910
5911 // Should we encode an arf frame.
5912 arf_src_index = get_arf_src_index(cpi);
5913 if (arf_src_index) {
5914 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005915 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005916 // Avoid creating an alt-ref if there's a forced keyframe pending.
5917 if (e == NULL) {
5918 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07005919 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005920 arf_src_index = 0;
5921 flush = 1;
5922 break;
5923 }
5924 }
5925 }
5926
5927 if (arf_src_index) {
5928 assert(arf_src_index <= rc->frames_to_key);
5929
Yaowu Xuf883b422016-08-30 14:01:10 -07005930 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005931 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005932 cpi->alt_ref_source = source;
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005933 // When arf_src_index == rc->frames_to_key, it indicates a fwd_kf
Sarah Parkerb9041612018-05-22 19:06:47 -07005934 if (arf_src_index == rc->frames_to_key) {
5935 // Skip temporal filtering and mark as intra_only if we have a fwd_kf
5936 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5937 int which_arf = gf_group->arf_update_idx[gf_group->index];
5938 cpi->is_arf_filter_off[which_arf] = 1;
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005939 cpi->no_show_kf = 1;
Sarah Parkerb9041612018-05-22 19:06:47 -07005940 } else {
5941 if (oxcf->arnr_max_frames > 0) {
5942 // Produce the filtered ARF frame.
5943 av1_temporal_filter(cpi, arf_src_index);
5944 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
5945 force_src_buffer = &cpi->alt_ref_buffer;
5946 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005947 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005948 cm->show_frame = 0;
5949 cm->intra_only = 0;
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07005950
5951 if (oxcf->pass < 2) {
5952 // In second pass, the buffer updates configure will be set
5953 // in the function av1_rc_get_second_pass_params
5954 av1_configure_buffer_updates_firstpass(cpi, ARF_UPDATE);
5955 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005956 }
5957 rc->source_alt_ref_pending = 0;
5958 }
5959
Zoe Liue9b15e22017-07-19 15:53:01 -07005960 // Should we encode an arf2 frame.
5961 arf_src_index = get_arf2_src_index(cpi);
5962 if (arf_src_index) {
5963 for (i = 0; i <= arf_src_index; ++i) {
5964 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
5965 // Avoid creating an alt-ref if there's a forced keyframe pending.
5966 if (e == NULL) {
5967 break;
5968 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
5969 arf_src_index = 0;
5970 flush = 1;
5971 break;
5972 }
5973 }
5974 }
5975
5976 if (arf_src_index) {
5977 assert(arf_src_index <= rc->frames_to_key);
5978
5979 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005980 cm->showable_frame = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07005981 cpi->alt_ref_source = source;
5982
5983 if (oxcf->arnr_max_frames > 0) {
5984 // Produce the filtered ARF frame.
Sebastien Alaiwan6697acf2018-02-21 16:59:17 +01005985 av1_temporal_filter(cpi, arf_src_index);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005986 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
Zoe Liue9b15e22017-07-19 15:53:01 -07005987 force_src_buffer = &cpi->alt_ref_buffer;
5988 }
5989
5990 cm->show_frame = 0;
5991 cm->intra_only = 0;
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07005992
5993 if (oxcf->pass < 2) {
5994 // In second pass, the buffer updates configure will be set
5995 // in the function av1_rc_get_second_pass_params
5996 av1_configure_buffer_updates_firstpass(cpi, INTNL_ARF_UPDATE);
5997 }
Zoe Liue9b15e22017-07-19 15:53:01 -07005998 }
5999 rc->source_alt_ref_pending = 0;
6000 }
Zoe Liue9b15e22017-07-19 15:53:01 -07006001
Yaowu Xuc27fc142016-08-22 16:08:15 -07006002 rc->is_bwd_ref_frame = 0;
6003 brf_src_index = get_brf_src_index(cpi);
6004 if (brf_src_index) {
6005 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07006006 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01006007 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006008 cm->show_frame = 0;
6009 cm->intra_only = 0;
6010
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07006011 if (oxcf->pass < 2) {
6012 // In second pass, the buffer updates configure will be set
6013 // in the function av1_rc_get_second_pass_params
6014 av1_configure_buffer_updates_firstpass(cpi, BIPRED_UPDATE);
6015 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006016 }
6017 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006018
6019 if (!source) {
6020 // Get last frame source.
6021 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006022 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006023 return -1;
6024 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006025 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006026 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07006027 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006028
6029 if (source != NULL) {
6030 cm->show_frame = 1;
6031 cm->intra_only = 0;
6032
6033 // Check to see if the frame should be encoded as an arf overlay.
6034 check_src_altref(cpi, source);
6035 }
6036 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006037 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006038 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07006039 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006040 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
6041
6042 *time_stamp = source->ts_start;
6043 *time_end = source->ts_end;
Sarah Parker73556772018-03-28 18:28:05 -07006044 av1_apply_encoding_flags(cpi, source->flags);
Yaowu Xuf883b422016-08-30 14:01:10 -07006045 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006046
6047 } else {
6048 *size = 0;
6049 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006050 av1_end_first_pass(cpi); /* get last stats packet */
Yaowu Xuc27fc142016-08-22 16:08:15 -07006051 cpi->twopass.first_pass_done = 1;
6052 }
6053 return -1;
6054 }
6055
6056 if (source->ts_start < cpi->first_time_stamp_ever) {
6057 cpi->first_time_stamp_ever = source->ts_start;
6058 cpi->last_end_time_stamp_seen = source->ts_start;
6059 }
6060
6061 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006062 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006063
6064 // adjust frame rates based on timestamps given
6065 if (cm->show_frame) adjust_frame_rate(cpi, source);
6066
6067 // Find a free buffer for the new frame, releasing the reference previously
6068 // held.
6069 if (cm->new_fb_idx != INVALID_IDX) {
6070 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6071 }
6072 cm->new_fb_idx = get_free_fb(cm);
6073
6074 if (cm->new_fb_idx == INVALID_IDX) return -1;
6075
Zoe Liuf452fdf2017-11-02 23:08:12 -07006076 // Retain the RF_LEVEL for the current newly coded frame.
6077 cpi->frame_rf_level[cm->new_fb_idx] =
6078 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
Zoe Liuf452fdf2017-11-02 23:08:12 -07006079
Yaowu Xuc27fc142016-08-22 16:08:15 -07006080 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Yaowu Xu9b0f7032017-07-31 11:01:19 -07006081 cm->cur_frame->buf.buf_8bit_valid = 0;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07006082
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006083 if (cpi->film_grain_table) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -07006084 cm->seq_params.film_grain_params_present = aom_film_grain_table_lookup(
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006085 cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07006086 &cm->film_grain_params);
6087 }
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -07006088 cm->cur_frame->film_grain_params_present =
6089 cm->seq_params.film_grain_params_present;
Zoe Liu6cfaff92016-10-18 17:12:11 -07006090
Andrey Norkin795ba872018-03-06 13:24:14 -08006091 // only one operating point supported now
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07006092 const int64_t pts64 = ticks_to_timebase_units(timebase, *time_stamp);
6093 if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR;
6094 cpi->common.frame_presentation_time = (uint32_t)pts64;
Andrey Norkin795ba872018-03-06 13:24:14 -08006095
Yaowu Xuc27fc142016-08-22 16:08:15 -07006096 // Start with a 0 size frame.
6097 *size = 0;
6098
6099 cpi->frame_flags = *frame_flags;
6100
6101 if (oxcf->pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006102 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006103 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07006104 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006105 }
6106
6107 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
Zoe Liu27deb382018-03-27 15:13:56 -07006108 for (i = 0; i < REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006109 }
6110
Yaowu Xuc27fc142016-08-22 16:08:15 -07006111 cm->using_qmatrix = cpi->oxcf.using_qm;
6112 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
6113 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006114
David Barker5e70a112017-10-03 14:28:17 +01006115 if (cm->seq_params.frame_id_numbers_present_flag) {
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006116 if (*time_stamp == 0) {
6117 cpi->common.current_frame_id = -1;
6118 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006119 }
Zoe Liuca0cd3f2018-02-26 15:07:50 -08006120
RogerZhou3b635242017-09-19 10:06:46 -07006121 cpi->cur_poc++;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08006122 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
6123 !frame_is_intra_only(cm)) {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00006124 if (cpi->common.seq_params.force_integer_mv == 2) {
RogerZhou3b635242017-09-19 10:06:46 -07006125 struct lookahead_entry *previous_entry =
Debargha Mukherjeea71e3db2018-02-28 07:47:17 -08006126 av1_lookahead_peek(cpi->lookahead, cpi->previous_index);
6127 if (!previous_entry)
6128 cpi->common.cur_frame_force_integer_mv = 0;
6129 else
6130 cpi->common.cur_frame_force_integer_mv = is_integer_mv(
6131 cpi, cpi->source, &previous_entry->img, cpi->previous_hash_table);
RogerZhou3b635242017-09-19 10:06:46 -07006132 } else {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00006133 cpi->common.cur_frame_force_integer_mv =
6134 cpi->common.seq_params.force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07006135 }
6136 } else {
RogerZhou10a03802017-10-26 11:49:48 -07006137 cpi->common.cur_frame_force_integer_mv = 0;
RogerZhou3b635242017-09-19 10:06:46 -07006138 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006139
Yaowu Xuc27fc142016-08-22 16:08:15 -07006140 if (oxcf->pass == 1) {
6141 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07006142 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006143 } else if (oxcf->pass == 2) {
Tom Finegane4099e32018-01-23 12:01:51 -08006144 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
6145 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006146 } else {
6147 // One pass encode
Tom Finegane4099e32018-01-23 12:01:51 -08006148 if (Pass0Encode(cpi, size, dest, 0, frame_flags) != AOM_CODEC_OK)
6149 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006150 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07006151 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08006152 cpi->previous_hash_table = &cm->cur_frame->hash_table;
RogerZhou3b635242017-09-19 10:06:46 -07006153 {
6154 int l;
6155 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
6156 if ((cpi->lookahead->buf + l) == source) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08006157 cpi->previous_index = l;
RogerZhou3b635242017-09-19 10:06:46 -07006158 break;
6159 }
6160 }
6161
6162 if (l == cpi->lookahead->max_sz) {
6163 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6164 "Failed to find last frame original buffer");
6165 }
6166 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07006167 }
6168
Yunqing Wang267e3272017-11-09 14:23:22 -08006169 if (!cm->large_scale_tile) {
Yunqing Wang267e3272017-11-09 14:23:22 -08006170 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Yunqing Wang267e3272017-11-09 14:23:22 -08006171 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006172
Yunqing Wangb041d8a2017-11-15 12:31:18 -08006173#define EXT_TILE_DEBUG 0
6174#if EXT_TILE_DEBUG
6175 if (cm->large_scale_tile && oxcf->pass == 2) {
6176 char fn[20] = "./fc";
6177 fn[4] = cm->current_video_frame / 100 + '0';
6178 fn[5] = (cm->current_video_frame % 100) / 10 + '0';
6179 fn[6] = (cm->current_video_frame % 10) + '0';
6180 fn[7] = '\0';
6181 av1_print_frame_contexts(cm->fc, fn);
6182 }
6183#endif // EXT_TILE_DEBUG
6184#undef EXT_TILE_DEBUG
Yaowu Xuc7119a72018-03-29 09:59:37 -07006185
Dominic Symesd4929012018-01-31 17:32:01 +01006186 cm->showable_frame = !cm->show_frame && cm->showable_frame;
Yunqing Wangb041d8a2017-11-15 12:31:18 -08006187
Yaowu Xuc27fc142016-08-22 16:08:15 -07006188 // No frame encoded, or frame was dropped, release scaled references.
6189 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6190 release_scaled_references(cpi);
6191 }
6192
6193 if (*size > 0) {
6194 cpi->droppable = !frame_is_reference(cpi);
6195 }
6196
Yaowu Xuf883b422016-08-30 14:01:10 -07006197 aom_usec_timer_mark(&cmptimer);
6198 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006199
6200 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6201 generate_psnr_packet(cpi);
6202
6203#if CONFIG_INTERNAL_STATS
6204 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006205 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006206 }
6207#endif // CONFIG_INTERNAL_STATS
6208
Yaowu Xuf883b422016-08-30 14:01:10 -07006209 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006210
6211 return 0;
6212}
6213
Yaowu Xuf883b422016-08-30 14:01:10 -07006214int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6215 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006216 if (!cm->show_frame) {
6217 return -1;
6218 } else {
6219 int ret;
6220 if (cm->frame_to_show) {
6221 *dest = *cm->frame_to_show;
6222 dest->y_width = cm->width;
6223 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006224 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6225 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006226 ret = 0;
6227 } else {
6228 ret = -1;
6229 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006230 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006231 return ret;
6232 }
6233}
6234
Yaowu Xuf883b422016-08-30 14:01:10 -07006235int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006236 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6237
6238 *frame =
6239 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6240 return 0;
6241}
6242
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006243static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6244 const YV12_BUFFER_CONFIG *b) {
6245 return a->y_height == b->y_height && a->y_width == b->y_width &&
6246 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6247 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6248 a->border == b->border &&
6249 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6250 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6251}
6252
Yunqing Wang93b18f32018-06-08 21:08:29 -07006253aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6254 YV12_BUFFER_CONFIG *new_frame,
6255 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006256 const int num_planes = av1_num_planes(cm);
6257 if (!equal_dimensions_and_border(new_frame, sd))
6258 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6259 "Incorrect buffer dimensions");
6260 else
6261 aom_yv12_copy_frame(new_frame, sd, num_planes);
6262
6263 return cm->error.error_code;
6264}
6265
Yaowu Xuf883b422016-08-30 14:01:10 -07006266int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6267 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006268 int hr = 0, hs = 0, vr = 0, vs = 0;
6269
6270 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6271
6272 Scale2Ratio(horiz_mode, &hr, &hs);
6273 Scale2Ratio(vert_mode, &vr, &vs);
6274
6275 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006276 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6277 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006278
6279 return 0;
6280}
6281
Yaowu Xuf883b422016-08-30 14:01:10 -07006282int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006283
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006284int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6285 size_t output_size = 0;
6286 size_t total_bytes_read = 0;
6287 size_t remaining_size = *frame_size;
6288 uint8_t *buff_ptr = buffer;
6289
6290 // go through each OBUs
6291 while (total_bytes_read < *frame_size) {
6292 uint8_t saved_obu_header[2];
6293 uint64_t obu_payload_size;
6294 size_t length_of_payload_size;
6295 size_t length_of_obu_size;
6296 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6297 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6298
6299 // save the obu header (1 or 2 bytes)
6300 memmove(saved_obu_header, buff_ptr, obu_header_size);
6301 // clear the obu_has_size_field
6302 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6303
6304 // get the payload_size and length of payload_size
6305 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6306 &obu_payload_size, &length_of_payload_size) != 0) {
6307 return AOM_CODEC_ERROR;
6308 }
6309 obu_bytes_read += length_of_payload_size;
6310
6311 // calculate the length of size of the obu header plus payload
6312 length_of_obu_size =
6313 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
6314
6315 // move the rest of data to new location
6316 memmove(buff_ptr + length_of_obu_size + obu_header_size,
6317 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07006318 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006319
6320 // write the new obu size
6321 const uint64_t obu_size = obu_header_size + obu_payload_size;
6322 size_t coded_obu_size;
6323 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
6324 &coded_obu_size) != 0) {
6325 return AOM_CODEC_ERROR;
6326 }
6327
6328 // write the saved (modified) obu_header following obu size
6329 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
6330
6331 total_bytes_read += obu_bytes_read;
6332 remaining_size -= obu_bytes_read;
6333 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07006334 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006335 }
6336
6337 *frame_size = output_size;
6338 return AOM_CODEC_OK;
6339}
6340
Yaowu Xuf883b422016-08-30 14:01:10 -07006341void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006342 // TODO(yunqingwang): For what references to use, external encoding flags
6343 // should be consistent with internal reference frame selection. Need to
6344 // ensure that there is not conflict between the two. In AV1 encoder, the
6345 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
6346 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
6347 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006348 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006349 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006350 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6351 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6352 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006353 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006354 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006355 } else {
6356 int ref = AOM_REFFRAME_ALL;
6357
6358 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6359 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6360
6361 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6362
6363 if (flags & AOM_EFLAG_NO_REF_ARF) {
6364 ref ^= AOM_ALT_FLAG;
6365 ref ^= AOM_BWD_FLAG;
6366 ref ^= AOM_ALT2_FLAG;
6367 } else {
6368 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6369 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
6370 }
6371
6372 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006373 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006374 }
6375
6376 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006377 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006378 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006379
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006380 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6381 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006382
Yaowu Xuf883b422016-08-30 14:01:10 -07006383 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006384
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006385 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6386 upd ^= AOM_ALT_FLAG;
6387 upd ^= AOM_BWD_FLAG;
6388 upd ^= AOM_ALT2_FLAG;
6389 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006390
Yaowu Xuf883b422016-08-30 14:01:10 -07006391 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006392 }
6393
sarahparker21dbca42018-03-30 17:43:44 -07006394 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
6395 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07006396 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
6397 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07006398 cpi->ext_use_s_frame =
6399 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07006400 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07006401
Yaowu Xuf883b422016-08-30 14:01:10 -07006402 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6403 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006404 }
6405}
Andrey Norkin795ba872018-03-06 13:24:14 -08006406
Andrey Norkin795ba872018-03-06 13:24:14 -08006407int64_t timebase_units_to_ticks(const aom_rational_t *timebase, int64_t n) {
6408 return n * TICKS_PER_SEC * timebase->num / timebase->den;
6409}
6410
6411int64_t ticks_to_timebase_units(const aom_rational_t *timebase, int64_t n) {
6412 const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
6413 return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
6414}