blob: 1f97ea1b0ba7855c39816a1a4996033555203898 [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/aom_dsp_rtcd.h"
18#include "config/aom_scale_rtcd.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070019#include "config/av1_rtcd.h"
20
21#include "aom_dsp/aom_dsp_common.h"
22#include "aom_dsp/aom_filter.h"
23#if CONFIG_DENOISE
24#include "aom_dsp/grain_table.h"
25#include "aom_dsp/noise_util.h"
26#include "aom_dsp/noise_model.h"
27#endif
28#include "aom_dsp/psnr.h"
29#if CONFIG_INTERNAL_STATS
30#include "aom_dsp/ssim.h"
31#endif
32#include "aom_ports/aom_timer.h"
33#include "aom_ports/mem.h"
34#include "aom_ports/system_state.h"
35#include "aom_scale/aom_scale.h"
36#if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
37#include "aom_util/debug_util.h"
38#endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -070039
40#include "av1/common/alloccommon.h"
Steinar Midtskogena9d41e82017-03-17 12:48:15 +010041#include "av1/common/cdef.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070042#include "av1/common/filter.h"
43#include "av1/common/idct.h"
44#include "av1/common/reconinter.h"
45#include "av1/common/reconintra.h"
Fergus Simpsond0565002017-03-27 16:51:52 -070046#include "av1/common/resize.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070047#include "av1/common/tile_common.h"
48
49#include "av1/encoder/aq_complexity.h"
50#include "av1/encoder/aq_cyclicrefresh.h"
51#include "av1/encoder/aq_variance.h"
52#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070053#include "av1/encoder/context_tree.h"
54#include "av1/encoder/encodeframe.h"
55#include "av1/encoder/encodemv.h"
56#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070057#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070058#include "av1/encoder/ethread.h"
59#include "av1/encoder/firstpass.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070060#include "av1/encoder/grain_test_vectors.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070061#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070062#include "av1/encoder/mbgraph.h"
63#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070064#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070065#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070066#include "av1/encoder/ratectrl.h"
67#include "av1/encoder/rd.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070068#include "av1/encoder/segmentation.h"
69#include "av1/encoder/speed_features.h"
70#include "av1/encoder/temporal_filter.h"
71
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) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700416 av1_rtcd();
417 aom_dsp_rtcd();
418 aom_scale_rtcd();
419 av1_init_intra_predictors();
420 av1_init_me_luts();
421 av1_rc_init_minq_luts();
422 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700423}
424
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700425static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
426 if (cpi->mbmi_ext_base) {
427 aom_free(cpi->mbmi_ext_base);
428 cpi->mbmi_ext_base = NULL;
429 }
430}
431
432static void alloc_context_buffers_ext(AV1_COMP *cpi) {
433 AV1_COMMON *cm = &cpi->common;
434 int mi_size = cm->mi_cols * cm->mi_rows;
435
436 dealloc_context_buffers_ext(cpi);
437 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
438 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
439}
440
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800441static void update_film_grain_parameters(struct AV1_COMP *cpi,
442 const AV1EncoderConfig *oxcf) {
443 AV1_COMMON *const cm = &cpi->common;
444 cpi->oxcf = *oxcf;
445
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700446 if (cpi->film_grain_table) {
447 aom_film_grain_table_free(cpi->film_grain_table);
448 aom_free(cpi->film_grain_table);
449 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700450 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700451
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800452 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700453 cm->seq_params.film_grain_params_present = 1;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800454 if (cm->frame_type == KEY_FRAME) {
455 memcpy(&cm->film_grain_params,
456 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
457 sizeof(cm->film_grain_params));
458
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700459 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
460 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800461 cm->film_grain_params.clip_to_restricted_range = 0;
462 }
463 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700464 } else if (oxcf->film_grain_table_filename) {
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700465 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
466 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700467
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700468 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700469 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800470 } else {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700471 cm->seq_params.film_grain_params_present = 0;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800472 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
473 }
474}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800475
Yaowu Xuf883b422016-08-30 14:01:10 -0700476static void dealloc_compressor_data(AV1_COMP *cpi) {
477 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000478 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700479
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700480 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700481
Yaowu Xuf883b422016-08-30 14:01:10 -0700482 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700483 cpi->tile_data = NULL;
484
485 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700486 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700487 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700488
Yaowu Xuf883b422016-08-30 14:01:10 -0700489 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700490 cpi->cyclic_refresh = NULL;
491
Yaowu Xuf883b422016-08-30 14:01:10 -0700492 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700493 cpi->active_map.map = NULL;
494
Jingning Hand064cf02017-06-01 10:00:39 -0700495 aom_free(cpi->td.mb.above_pred_buf);
496 cpi->td.mb.above_pred_buf = NULL;
497
498 aom_free(cpi->td.mb.left_pred_buf);
499 cpi->td.mb.left_pred_buf = NULL;
500
501 aom_free(cpi->td.mb.wsrc_buf);
502 cpi->td.mb.wsrc_buf = NULL;
503
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530504 for (int i = 0; i < 2; i++)
505 for (int j = 0; j < 2; j++) {
506 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
507 cpi->td.mb.hash_value_buffer[i][j] = NULL;
508 }
Jingning Hand064cf02017-06-01 10:00:39 -0700509 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
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530530 aom_free(cpi->tplist[0][0]);
531 cpi->tplist[0][0] = NULL;
532
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000533 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534
hui sud9a812b2017-07-06 14:34:37 -0700535 aom_free(cpi->td.mb.palette_buffer);
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700536
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700537 aom_free(cpi->td.mb.tmp_conv_dst);
538 for (int j = 0; j < 2; ++j) {
539 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
540 }
541
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700542#if CONFIG_DENOISE
543 if (cpi->denoise_and_model) {
544 aom_denoise_and_model_free(cpi->denoise_and_model);
545 cpi->denoise_and_model = NULL;
546 }
547#endif
548 if (cpi->film_grain_table) {
549 aom_film_grain_table_free(cpi->film_grain_table);
550 cpi->film_grain_table = NULL;
551 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700552}
553
Yaowu Xuf883b422016-08-30 14:01:10 -0700554static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700555 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700556 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700557
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200558 // Stores a snapshot of key state variables which can subsequently be
559 // restored with a call to av1_restore_coding_context. These functions are
560 // intended for use in a re-code loop in av1_compress_frame where the
561 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800562 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
563 av1_copy(cc->nmv_costs, cpi->nmv_costs);
564 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000565
Yaowu Xuc27fc142016-08-22 16:08:15 -0700566 cc->fc = *cm->fc;
567}
568
Yaowu Xuf883b422016-08-30 14:01:10 -0700569static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700570 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700571 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700572
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200573 // Restore key state variables to the snapshot state stored in the
574 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800575 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
576 av1_copy(cpi->nmv_costs, cc->nmv_costs);
577 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000578
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579 *cm->fc = cc->fc;
580}
581
Yaowu Xuf883b422016-08-30 14:01:10 -0700582static void configure_static_seg_features(AV1_COMP *cpi) {
583 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700584 const RATE_CONTROL *const rc = &cpi->rc;
585 struct segmentation *const seg = &cm->seg;
586
587 int high_q = (int)(rc->avg_q > 48.0);
588 int qi_delta;
589
590 // Disable and clear down for KF
591 if (cm->frame_type == KEY_FRAME) {
592 // Clear down the global segmentation map
593 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
594 seg->update_map = 0;
595 seg->update_data = 0;
596 cpi->static_mb_pct = 0;
597
598 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700599 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700600
601 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700602 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700603 } else if (cpi->refresh_alt_ref_frame) {
604 // If this is an alt ref frame
605 // Clear down the global segmentation map
606 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
607 seg->update_map = 0;
608 seg->update_data = 0;
609 cpi->static_mb_pct = 0;
610
611 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700612 av1_disable_segmentation(seg);
613 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700614
615 // Scan frames from current to arf frame.
616 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700617 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700618
619 // If segmentation was enabled set those features needed for the
620 // arf itself.
621 if (seg->enabled) {
622 seg->update_map = 1;
623 seg->update_data = 1;
624
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700625 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
626 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700627 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700628 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
629 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
630 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
631 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
632
633 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
634 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
635 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
636 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637
Yaowu Xuf883b422016-08-30 14:01:10 -0700638 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700639 }
640 } else if (seg->enabled) {
641 // All other frames if segmentation has been enabled
642
643 // First normal frame in a valid gf or alt ref group
644 if (rc->frames_since_golden == 0) {
645 // Set up segment features for normal frames in an arf group
646 if (rc->source_alt_ref_active) {
647 seg->update_map = 0;
648 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700649
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700650 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
651 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700652 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
653 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700654
Cheng Chend8184da2017-09-26 18:15:22 -0700655 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
656 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
657 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
658 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
659
660 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
661 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
662 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
663 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664
665 // Segment coding disabled for compred testing
666 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700667 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
668 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
669 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700670 }
671 } else {
672 // Disable segmentation and clear down features if alt ref
673 // is not active for this group
674
Yaowu Xuf883b422016-08-30 14:01:10 -0700675 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700676
677 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
678
679 seg->update_map = 0;
680 seg->update_data = 0;
681
Yaowu Xuf883b422016-08-30 14:01:10 -0700682 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 }
684 } else if (rc->is_src_frame_alt_ref) {
685 // Special case where we are coding over the top of a previous
686 // alt ref frame.
687 // Segment coding disabled for compred testing
688
689 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700690 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
691 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700692
693 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700694 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
695 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
696 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
697 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700698
699 // Skip all MBs if high Q (0,0 mv and skip coeffs)
700 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700701 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
702 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703 }
704 // Enable data update
705 seg->update_data = 1;
706 } else {
707 // All other frames.
708
709 // No updates.. leave things as they are.
710 seg->update_map = 0;
711 seg->update_data = 0;
712 }
713 }
714}
715
Yaowu Xuf883b422016-08-30 14:01:10 -0700716static void update_reference_segmentation_map(AV1_COMP *cpi) {
717 AV1_COMMON *const cm = &cpi->common;
Yushin Choa7f65922018-04-04 16:06:11 -0700718 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
Soo-Chul Han934af352017-10-15 15:21:51 -0400719 uint8_t *cache_ptr = cm->current_frame_seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700720 int row, col;
721
722 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700723 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700724 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700725 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
726 cache[0] = mi_4x4[0]->segment_id;
727 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728 cache_ptr += cm->mi_cols;
729 }
730}
731
Yaowu Xuf883b422016-08-30 14:01:10 -0700732static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
733 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700734 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736
737 if (!cpi->lookahead)
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700738 cpi->lookahead =
739 av1_lookahead_init(oxcf->width, oxcf->height, seq_params->subsampling_x,
740 seq_params->subsampling_y,
741 seq_params->use_highbitdepth, oxcf->lag_in_frames);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700742 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700743 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744 "Failed to allocate lag buffers");
745
746 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700747 if (aom_realloc_frame_buffer(
748 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
749 seq_params->subsampling_x, seq_params->subsampling_y,
750 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
751 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700752 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700753 "Failed to allocate altref buffer");
754}
755
Yaowu Xuf883b422016-08-30 14:01:10 -0700756static void alloc_util_frame_buffers(AV1_COMP *cpi) {
757 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700758 const SequenceHeader *const seq_params = &cm->seq_params;
759 if (aom_realloc_frame_buffer(
760 &cpi->last_frame_uf, cm->width, cm->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))
Yaowu Xuf883b422016-08-30 14:01:10 -0700763 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700764 "Failed to allocate last frame buffer");
765
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700766 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800767 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700768 cm->superres_upscaled_height, seq_params->subsampling_x,
769 seq_params->subsampling_y, seq_params->use_highbitdepth,
770 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800771 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800772 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700773
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700774 if (aom_realloc_frame_buffer(
775 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
776 seq_params->subsampling_y, seq_params->use_highbitdepth,
777 AOM_BORDER_IN_PIXELS, 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 source buffer");
780
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700781 if (aom_realloc_frame_buffer(
782 &cpi->scaled_last_source, cm->width, cm->height,
783 seq_params->subsampling_x, seq_params->subsampling_y,
784 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
785 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700786 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700787 "Failed to allocate scaled last source buffer");
788}
789
Cheng Chen46f30c72017-09-07 11:13:33 -0700790static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700791 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000792 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793
Yaowu Xuf883b422016-08-30 14:01:10 -0700794 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530796 int mi_rows_aligned_to_sb =
797 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
798 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
799
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700800 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700801
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802 alloc_context_buffers_ext(cpi);
803
Yaowu Xuf883b422016-08-30 14:01:10 -0700804 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700805
806 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000807 unsigned int tokens =
808 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700809 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700810 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700811 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530812 aom_free(cpi->tplist[0][0]);
813
814 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
815 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
816 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817
Yaowu Xuf883b422016-08-30 14:01:10 -0700818 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700819}
820
Yaowu Xuf883b422016-08-30 14:01:10 -0700821void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700822 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700823 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700824}
825
Yunqing Wang75e20e82018-06-16 12:10:48 -0700826static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200827 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200828 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200829
830 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200831
832 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200833 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200834 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200835 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
836 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200837 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000838 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
839 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200840 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200841 cm->uniform_tile_spacing_flag = 0;
842 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
843 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200844 size_sb = cpi->oxcf.tile_widths[j++];
845 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000846 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200847 }
848 cm->tile_cols = i;
849 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200850 }
851 av1_calculate_tile_cols(cm);
852
853 // configure tile rows
854 if (cm->uniform_tile_spacing_flag) {
855 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
856 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200857 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000858 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
859 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200860 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200861 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
862 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200863 size_sb = cpi->oxcf.tile_heights[j++];
864 if (j >= cpi->oxcf.tile_height_count) j = 0;
865 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200866 }
867 cm->tile_rows = i;
868 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200869 }
870 av1_calculate_tile_rows(cm);
871}
872
Yaowu Xuf883b422016-08-30 14:01:10 -0700873static void update_frame_size(AV1_COMP *cpi) {
874 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
876
Yaowu Xuf883b422016-08-30 14:01:10 -0700877 av1_set_mb_mi(cm, cm->width, cm->height);
878 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500879 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700880 memset(cpi->mbmi_ext_base, 0,
881 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700882 set_tile_info(cpi);
883}
884
Yaowu Xuf883b422016-08-30 14:01:10 -0700885static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700886 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700887 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
888 cpi->ref_fb_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700889 cpi->rate_index = 0;
890 cpi->rate_size = 0;
891 cpi->cur_poc = -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700892}
893
Debargha Mukherjee57498692018-05-11 13:29:31 -0700894static INLINE int does_level_match(int width, int height, double fps,
895 int lvl_width, int lvl_height,
896 double lvl_fps, int lvl_dim_mult) {
897 const int64_t lvl_luma_pels = lvl_width * lvl_height;
898 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
899 const int64_t luma_pels = width * height;
900 const double display_sample_rate = luma_pels * fps;
901 return luma_pels <= lvl_luma_pels &&
902 display_sample_rate <= lvl_display_sample_rate &&
903 width <= lvl_width * lvl_dim_mult &&
904 height <= lvl_height * lvl_dim_mult;
905}
906
Andrey Norkin26495512018-06-20 17:13:11 -0700907static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -0700908 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700909 // TODO(any): This is a placeholder function that only addresses dimensions
910 // and max display sample rates.
911 // Need to add checks for max bit rate, max decoded luma sample rate, header
912 // rate, etc. that are not covered by this function.
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700913 (void)oxcf;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700914 BitstreamLevel bl = { 9, 3 };
915 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
916 288, 30.0, 4)) {
917 bl.major = 2;
918 bl.minor = 0;
919 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
920 704, 396, 30.0, 4)) {
921 bl.major = 2;
922 bl.minor = 1;
923 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
924 1088, 612, 30.0, 4)) {
925 bl.major = 3;
926 bl.minor = 0;
927 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
928 1376, 774, 30.0, 4)) {
929 bl.major = 3;
930 bl.minor = 1;
931 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
932 2048, 1152, 30.0, 3)) {
933 bl.major = 4;
934 bl.minor = 0;
935 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
936 2048, 1152, 60.0, 3)) {
937 bl.major = 4;
938 bl.minor = 1;
939 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
940 4096, 2176, 30.0, 2)) {
941 bl.major = 5;
942 bl.minor = 0;
943 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
944 4096, 2176, 60.0, 2)) {
945 bl.major = 5;
946 bl.minor = 1;
947 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
948 4096, 2176, 120.0, 2)) {
949 bl.major = 5;
950 bl.minor = 2;
951 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
952 8192, 4352, 30.0, 2)) {
953 bl.major = 6;
954 bl.minor = 0;
955 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
956 8192, 4352, 60.0, 2)) {
957 bl.major = 6;
958 bl.minor = 1;
959 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
960 8192, 4352, 120.0, 2)) {
961 bl.major = 6;
962 bl.minor = 2;
963 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
964 16384, 8704, 30.0, 2)) {
965 bl.major = 7;
966 bl.minor = 0;
967 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
968 16384, 8704, 60.0, 2)) {
969 bl.major = 7;
970 bl.minor = 1;
971 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
972 16384, 8704, 120.0, 2)) {
973 bl.major = 7;
974 bl.minor = 2;
975 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700976 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700977 seq->level[i] = bl;
Andrey Norkinf481d982018-05-15 12:05:31 -0700978 seq->tier[i] = 0; // setting main tier by default
Andrey Norkin26495512018-06-20 17:13:11 -0700979 // Set the maximum parameters for bitrate and buffer size for this profile,
980 // level, and tier
981 cm->op_params[i].bitrate = max_level_bitrate(
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700982 cm->seq_params.profile, major_minor_to_seq_level_idx(seq->level[i]),
983 seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -0700984 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
985 // check
Andrey Norkin26495512018-06-20 17:13:11 -0700986 if (cm->op_params[i].bitrate == 0)
987 aom_internal_error(
988 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
989 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -0700990 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -0700991 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700992 }
993}
994
Andrey Norkin26495512018-06-20 17:13:11 -0700995static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
996 const AV1EncoderConfig *oxcf) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -0700997 seq->still_picture = (oxcf->limit == 1);
998 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -0700999 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001000 seq->force_screen_content_tools = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001001 seq->force_integer_mv = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001002 seq->enable_order_hint = oxcf->enable_order_hint;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001003 seq->frame_id_numbers_present_flag = oxcf->large_scale_tile;
1004 if (seq->still_picture && seq->reduced_still_picture_hdr) {
1005 seq->enable_order_hint = 0;
1006 seq->frame_id_numbers_present_flag = 0;
1007 seq->force_screen_content_tools = 2;
1008 seq->force_integer_mv = 2;
1009 }
Wan-Teh Chang69582972018-05-15 13:18:52 -07001010 seq->order_hint_bits_minus_1 =
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001011 seq->enable_order_hint ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1 : -1;
1012
1013 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001014 seq->enable_jnt_comp = oxcf->enable_jnt_comp;
1015 seq->enable_jnt_comp &= seq->enable_order_hint;
1016 seq->enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1017 seq->enable_ref_frame_mvs &= seq->enable_order_hint;
1018 seq->enable_superres = oxcf->enable_superres;
1019 seq->enable_cdef = oxcf->enable_cdef;
1020 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001021 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee97095fb2018-03-26 07:51:48 -07001022 seq->enable_interintra_compound = 1;
1023 seq->enable_masked_compound = 1;
Debargha Mukherjee365eae82018-03-26 19:19:55 -07001024 seq->enable_intra_edge_filter = 1;
1025 seq->enable_filter_intra = 1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001026
Andrey Norkin26495512018-06-20 17:13:11 -07001027 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001028
1029 if (seq->operating_points_cnt_minus_1 == 0) {
1030 seq->operating_point_idc[0] = 0;
1031 } else {
1032 // Set operating_point_idc[] such that for the i-th operating point the
1033 // first (operating_points_cnt-i) spatial layers and the first temporal
1034 // layer are decoded Note that highest quality operating point should come
1035 // first
1036 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1037 seq->operating_point_idc[i] =
1038 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1039 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001040}
1041
Yaowu Xuf883b422016-08-30 14:01:10 -07001042static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1043 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001044
1045 cpi->oxcf = *oxcf;
1046 cpi->framerate = oxcf->init_framerate;
1047
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001048 cm->seq_params.profile = oxcf->profile;
1049 cm->seq_params.bit_depth = oxcf->bit_depth;
1050 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1051 cm->seq_params.color_primaries = oxcf->color_primaries;
1052 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1053 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001054 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001055 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1056 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001057 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001058 cm->timing_info.num_units_in_display_tick =
1059 oxcf->timing_info.num_units_in_display_tick;
1060 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1061 cm->timing_info.equal_picture_interval =
1062 oxcf->timing_info.equal_picture_interval;
1063 cm->timing_info.num_ticks_per_picture =
1064 oxcf->timing_info.num_ticks_per_picture;
1065
Andrey Norkin26495512018-06-20 17:13:11 -07001066 cm->seq_params.display_model_info_present_flag =
1067 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001068 cm->seq_params.decoder_model_info_present_flag =
1069 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001070 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001071 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001072 cm->buffer_model.num_units_in_decoding_tick =
1073 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001074 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08001075 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07001076 set_dec_model_op_parameters(&cm->op_params[0]);
1077 } else if (cm->timing_info_present &&
1078 cm->timing_info.equal_picture_interval &&
1079 !cm->seq_params.decoder_model_info_present_flag) {
1080 // set the decoder model parameters in resource availability mode
1081 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001082 } else {
1083 cm->op_params[0].initial_display_delay =
1084 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001085 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001086
Tom Fineganf8d6a162018-08-21 10:47:55 -07001087 if (cm->seq_params.monochrome) {
1088 cm->seq_params.subsampling_x = 1;
1089 cm->seq_params.subsampling_y = 1;
1090 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1091 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1092 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1093 cm->seq_params.subsampling_x = 0;
1094 cm->seq_params.subsampling_y = 0;
1095 } else {
1096 if (cm->seq_params.profile == 0) {
1097 cm->seq_params.subsampling_x = 1;
1098 cm->seq_params.subsampling_y = 1;
1099 } else if (cm->seq_params.profile == 1) {
1100 cm->seq_params.subsampling_x = 0;
1101 cm->seq_params.subsampling_y = 0;
1102 } else {
1103 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1104 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1105 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1106 } else {
1107 cm->seq_params.subsampling_x = 1;
1108 cm->seq_params.subsampling_y = 0;
1109 }
1110 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001111 }
1112
Yaowu Xuc27fc142016-08-22 16:08:15 -07001113 cm->width = oxcf->width;
1114 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001115 set_sb_size(&cm->seq_params,
1116 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001117 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001118
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001119 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001120
1121 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001122 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001123
1124 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001125 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001126
1127 cpi->static_mb_pct = 0;
1128 cpi->ref_frame_flags = 0;
1129
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001130 // Reset resize pending flags
1131 cpi->resize_pending_width = 0;
1132 cpi->resize_pending_height = 0;
1133
Yaowu Xuc27fc142016-08-22 16:08:15 -07001134 init_buffer_indices(cpi);
1135}
1136
1137static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001138 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001139 const int64_t bandwidth = oxcf->target_bandwidth;
1140 const int64_t starting = oxcf->starting_buffer_level_ms;
1141 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1142 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1143
1144 rc->starting_buffer_level = starting * bandwidth / 1000;
1145 rc->optimal_buffer_level =
1146 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1147 rc->maximum_buffer_size =
1148 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1149}
1150
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001151#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1152 cpi->fn_ptr[BT].sdf = SDF; \
1153 cpi->fn_ptr[BT].sdaf = SDAF; \
1154 cpi->fn_ptr[BT].vf = VF; \
1155 cpi->fn_ptr[BT].svf = SVF; \
1156 cpi->fn_ptr[BT].svaf = SVAF; \
1157 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1158 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001159 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001160
1161#define MAKE_BFP_SAD_WRAPPER(fnname) \
1162 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1163 int source_stride, \
1164 const uint8_t *ref_ptr, int ref_stride) { \
1165 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1166 } \
1167 static unsigned int fnname##_bits10( \
1168 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1169 int ref_stride) { \
1170 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1171 } \
1172 static unsigned int fnname##_bits12( \
1173 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1174 int ref_stride) { \
1175 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1176 }
1177
1178#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1179 static unsigned int fnname##_bits8( \
1180 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1181 int ref_stride, const uint8_t *second_pred) { \
1182 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1183 } \
1184 static unsigned int fnname##_bits10( \
1185 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1186 int ref_stride, const uint8_t *second_pred) { \
1187 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1188 2; \
1189 } \
1190 static unsigned int fnname##_bits12( \
1191 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1192 int ref_stride, const uint8_t *second_pred) { \
1193 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1194 4; \
1195 }
1196
Yaowu Xuc27fc142016-08-22 16:08:15 -07001197#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1198 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1199 const uint8_t *const ref_ptr[], int ref_stride, \
1200 unsigned int *sad_array) { \
1201 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1202 } \
1203 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1204 const uint8_t *const ref_ptr[], int ref_stride, \
1205 unsigned int *sad_array) { \
1206 int i; \
1207 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1208 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1209 } \
1210 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1211 const uint8_t *const ref_ptr[], int ref_stride, \
1212 unsigned int *sad_array) { \
1213 int i; \
1214 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1215 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1216 }
1217
Cheng Chenbf3d4962017-11-01 14:48:52 -07001218#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1219 static unsigned int fnname##_bits8( \
1220 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1221 int ref_stride, const uint8_t *second_pred, \
1222 const JNT_COMP_PARAMS *jcp_param) { \
1223 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1224 jcp_param); \
1225 } \
1226 static unsigned int fnname##_bits10( \
1227 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1228 int ref_stride, const uint8_t *second_pred, \
1229 const JNT_COMP_PARAMS *jcp_param) { \
1230 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1231 jcp_param) >> \
1232 2; \
1233 } \
1234 static unsigned int fnname##_bits12( \
1235 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1236 int ref_stride, const uint8_t *second_pred, \
1237 const JNT_COMP_PARAMS *jcp_param) { \
1238 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1239 jcp_param) >> \
1240 4; \
1241 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001242
Yaowu Xuf883b422016-08-30 14:01:10 -07001243MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1244MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001245MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1246MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1247MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1248MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1249MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1250MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1251MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001252MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1253MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1254MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1255MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1256MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1257MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1258MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1259MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1260MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1261MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1262MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1263MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1264MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1265MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001266MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1267MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1268MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001269MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1270MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1271MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001272MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1273MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1274MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001275MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1276MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1277MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001278MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1279MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1280MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001281MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1282MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1283MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001284MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1285MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1286MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001287MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1288MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1289MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001290MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001291
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001292MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1293MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1294MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1295MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1296MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1297MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1298MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1299MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1300MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1301MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1302MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1303MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001304MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1305MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1306MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1307MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1308MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1309MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001310
Cheng Chenbf3d4962017-11-01 14:48:52 -07001311MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x128_avg)
1312MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad128x64_avg)
1313MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x128_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001314MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x16_avg)
1315MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x32_avg)
1316MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x32_avg)
1317MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x64_avg)
1318MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x32_avg)
1319MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x64_avg)
1320MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x16_avg)
1321MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x8_avg)
1322MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x16_avg)
1323MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x8_avg)
1324MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x4_avg)
1325MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x8_avg)
1326MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x4_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001327MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad4x16_avg)
1328MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x4_avg)
1329MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad8x32_avg)
1330MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad32x8_avg)
1331MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad16x64_avg)
1332MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_jnt_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001333
David Barker0f3c94e2017-05-16 15:21:50 +01001334#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001335 cpi->fn_ptr[BT].msdf = MCSDF; \
1336 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001337
David Barkerc155e012017-05-11 13:54:54 +01001338#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1339 static unsigned int fnname##_bits8( \
1340 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1341 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1342 int m_stride, int invert_mask) { \
1343 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1344 second_pred_ptr, m, m_stride, invert_mask); \
1345 } \
1346 static unsigned int fnname##_bits10( \
1347 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1348 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1349 int m_stride, int invert_mask) { \
1350 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1351 second_pred_ptr, m, m_stride, invert_mask) >> \
1352 2; \
1353 } \
1354 static unsigned int fnname##_bits12( \
1355 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1356 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1357 int m_stride, int invert_mask) { \
1358 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1359 second_pred_ptr, m, m_stride, invert_mask) >> \
1360 4; \
1361 }
1362
David Barkerf19f35f2017-05-22 16:33:22 +01001363MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1364MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1365MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001366MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1367MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1368MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1369MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1370MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1371MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1372MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1373MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1374MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1375MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1376MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1377MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1378MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001379MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1380MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1381MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1382MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001383MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1384MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001385
Yaowu Xuc27fc142016-08-22 16:08:15 -07001386#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1387 cpi->fn_ptr[BT].osdf = OSDF; \
1388 cpi->fn_ptr[BT].ovf = OVF; \
1389 cpi->fn_ptr[BT].osvf = OSVF;
1390
1391#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1392 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1393 const int32_t *wsrc, \
1394 const int32_t *msk) { \
1395 return fnname(ref, ref_stride, wsrc, msk); \
1396 } \
1397 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1398 const int32_t *wsrc, \
1399 const int32_t *msk) { \
1400 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1401 } \
1402 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1403 const int32_t *wsrc, \
1404 const int32_t *msk) { \
1405 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1406 }
1407
Yaowu Xuf883b422016-08-30 14:01:10 -07001408MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1409MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1410MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001411MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1412MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1413MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1414MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1415MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1416MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1417MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1418MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1419MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1420MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1421MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1422MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1423MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001424MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1425MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1426MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1427MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001428MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1429MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001430
Yaowu Xuf883b422016-08-30 14:01:10 -07001431static void highbd_set_var_fns(AV1_COMP *const cpi) {
1432 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001433 if (cm->seq_params.use_highbitdepth) {
1434 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001435 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001436 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1437 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1438 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001439 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001440 aom_highbd_sad64x16x4d_bits8,
1441 aom_highbd_jnt_sad64x16_avg_bits8,
1442 aom_highbd_8_jnt_sub_pixel_avg_variance64x16)
1443
1444 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1445 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1446 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001447 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001448 aom_highbd_sad16x64x4d_bits8,
1449 aom_highbd_jnt_sad16x64_avg_bits8,
1450 aom_highbd_8_jnt_sub_pixel_avg_variance16x64)
1451
1452 HIGHBD_BFP(
1453 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1454 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001455 aom_highbd_8_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001456 aom_highbd_sad32x8x4d_bits8, aom_highbd_jnt_sad32x8_avg_bits8,
1457 aom_highbd_8_jnt_sub_pixel_avg_variance32x8)
1458
1459 HIGHBD_BFP(
1460 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1461 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001462 aom_highbd_8_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001463 aom_highbd_sad8x32x4d_bits8, aom_highbd_jnt_sad8x32_avg_bits8,
1464 aom_highbd_8_jnt_sub_pixel_avg_variance8x32)
1465
1466 HIGHBD_BFP(
1467 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1468 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001469 aom_highbd_8_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001470 aom_highbd_sad16x4x4d_bits8, aom_highbd_jnt_sad16x4_avg_bits8,
1471 aom_highbd_8_jnt_sub_pixel_avg_variance16x4)
1472
1473 HIGHBD_BFP(
1474 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1475 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001476 aom_highbd_8_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001477 aom_highbd_sad4x16x4d_bits8, aom_highbd_jnt_sad4x16_avg_bits8,
1478 aom_highbd_8_jnt_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001479
1480 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1481 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1482 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001483 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001484 aom_highbd_sad32x16x4d_bits8,
1485 aom_highbd_jnt_sad32x16_avg_bits8,
1486 aom_highbd_8_jnt_sub_pixel_avg_variance32x16)
1487
1488 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1489 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1490 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001491 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001492 aom_highbd_sad16x32x4d_bits8,
1493 aom_highbd_jnt_sad16x32_avg_bits8,
1494 aom_highbd_8_jnt_sub_pixel_avg_variance16x32)
1495
1496 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1497 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1498 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001499 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001500 aom_highbd_sad64x32x4d_bits8,
1501 aom_highbd_jnt_sad64x32_avg_bits8,
1502 aom_highbd_8_jnt_sub_pixel_avg_variance64x32)
1503
1504 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1505 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1506 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001507 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001508 aom_highbd_sad32x64x4d_bits8,
1509 aom_highbd_jnt_sad32x64_avg_bits8,
1510 aom_highbd_8_jnt_sub_pixel_avg_variance32x64)
1511
1512 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1513 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1514 aom_highbd_8_sub_pixel_variance32x32,
1515 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001516 aom_highbd_sad32x32x4d_bits8,
1517 aom_highbd_jnt_sad32x32_avg_bits8,
1518 aom_highbd_8_jnt_sub_pixel_avg_variance32x32)
1519
1520 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1521 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1522 aom_highbd_8_sub_pixel_variance64x64,
1523 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001524 aom_highbd_sad64x64x4d_bits8,
1525 aom_highbd_jnt_sad64x64_avg_bits8,
1526 aom_highbd_8_jnt_sub_pixel_avg_variance64x64)
1527
1528 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1529 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1530 aom_highbd_8_sub_pixel_variance16x16,
1531 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001532 aom_highbd_sad16x16x4d_bits8,
1533 aom_highbd_jnt_sad16x16_avg_bits8,
1534 aom_highbd_8_jnt_sub_pixel_avg_variance16x16)
1535
1536 HIGHBD_BFP(
1537 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1538 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001539 aom_highbd_8_sub_pixel_avg_variance16x8,
1540 aom_highbd_sad16x8x4d_bits8, aom_highbd_jnt_sad16x8_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001541 aom_highbd_8_jnt_sub_pixel_avg_variance16x8)
1542
1543 HIGHBD_BFP(
1544 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1545 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001546 aom_highbd_8_sub_pixel_avg_variance8x16,
1547 aom_highbd_sad8x16x4d_bits8, aom_highbd_jnt_sad8x16_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001548 aom_highbd_8_jnt_sub_pixel_avg_variance8x16)
1549
1550 HIGHBD_BFP(BLOCK_8X8, aom_highbd_sad8x8_bits8,
1551 aom_highbd_sad8x8_avg_bits8, aom_highbd_8_variance8x8,
1552 aom_highbd_8_sub_pixel_variance8x8,
1553 aom_highbd_8_sub_pixel_avg_variance8x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001554 aom_highbd_sad8x8x4d_bits8, aom_highbd_jnt_sad8x8_avg_bits8,
1555 aom_highbd_8_jnt_sub_pixel_avg_variance8x8)
1556
1557 HIGHBD_BFP(BLOCK_8X4, aom_highbd_sad8x4_bits8,
1558 aom_highbd_sad8x4_avg_bits8, aom_highbd_8_variance8x4,
1559 aom_highbd_8_sub_pixel_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001560 aom_highbd_8_sub_pixel_avg_variance8x4,
1561 aom_highbd_sad8x4x4d_bits8, aom_highbd_jnt_sad8x4_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001562 aom_highbd_8_jnt_sub_pixel_avg_variance8x4)
1563
1564 HIGHBD_BFP(BLOCK_4X8, aom_highbd_sad4x8_bits8,
1565 aom_highbd_sad4x8_avg_bits8, aom_highbd_8_variance4x8,
1566 aom_highbd_8_sub_pixel_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001567 aom_highbd_8_sub_pixel_avg_variance4x8,
1568 aom_highbd_sad4x8x4d_bits8, aom_highbd_jnt_sad4x8_avg_bits8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001569 aom_highbd_8_jnt_sub_pixel_avg_variance4x8)
1570
1571 HIGHBD_BFP(BLOCK_4X4, aom_highbd_sad4x4_bits8,
1572 aom_highbd_sad4x4_avg_bits8, aom_highbd_8_variance4x4,
1573 aom_highbd_8_sub_pixel_variance4x4,
1574 aom_highbd_8_sub_pixel_avg_variance4x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001575 aom_highbd_sad4x4x4d_bits8, aom_highbd_jnt_sad4x4_avg_bits8,
1576 aom_highbd_8_jnt_sub_pixel_avg_variance4x4)
1577
Cheng Chenbf3d4962017-11-01 14:48:52 -07001578 HIGHBD_BFP(
1579 BLOCK_128X128, aom_highbd_sad128x128_bits8,
1580 aom_highbd_sad128x128_avg_bits8, aom_highbd_8_variance128x128,
1581 aom_highbd_8_sub_pixel_variance128x128,
1582 aom_highbd_8_sub_pixel_avg_variance128x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001583 aom_highbd_sad128x128x4d_bits8, aom_highbd_jnt_sad128x128_avg_bits8,
1584 aom_highbd_8_jnt_sub_pixel_avg_variance128x128)
1585
1586 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1587 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1588 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001589 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001590 aom_highbd_sad128x64x4d_bits8,
1591 aom_highbd_jnt_sad128x64_avg_bits8,
1592 aom_highbd_8_jnt_sub_pixel_avg_variance128x64)
1593
1594 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1595 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1596 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001597 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001598 aom_highbd_sad64x128x4d_bits8,
1599 aom_highbd_jnt_sad64x128_avg_bits8,
1600 aom_highbd_8_jnt_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001601
David Barkerf19f35f2017-05-22 16:33:22 +01001602 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1603 aom_highbd_8_masked_sub_pixel_variance128x128)
1604 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1605 aom_highbd_8_masked_sub_pixel_variance128x64)
1606 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1607 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001608 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1609 aom_highbd_8_masked_sub_pixel_variance64x64)
1610 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1611 aom_highbd_8_masked_sub_pixel_variance64x32)
1612 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1613 aom_highbd_8_masked_sub_pixel_variance32x64)
1614 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1615 aom_highbd_8_masked_sub_pixel_variance32x32)
1616 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1617 aom_highbd_8_masked_sub_pixel_variance32x16)
1618 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1619 aom_highbd_8_masked_sub_pixel_variance16x32)
1620 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1621 aom_highbd_8_masked_sub_pixel_variance16x16)
1622 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1623 aom_highbd_8_masked_sub_pixel_variance8x16)
1624 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1625 aom_highbd_8_masked_sub_pixel_variance16x8)
1626 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1627 aom_highbd_8_masked_sub_pixel_variance8x8)
1628 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1629 aom_highbd_8_masked_sub_pixel_variance4x8)
1630 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1631 aom_highbd_8_masked_sub_pixel_variance8x4)
1632 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1633 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001634 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1635 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001636 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1637 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001638 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1639 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001640 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1641 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001642 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1643 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001644 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1645 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001646 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1647 aom_highbd_obmc_variance128x128,
1648 aom_highbd_obmc_sub_pixel_variance128x128)
1649 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1650 aom_highbd_obmc_variance128x64,
1651 aom_highbd_obmc_sub_pixel_variance128x64)
1652 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1653 aom_highbd_obmc_variance64x128,
1654 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001655 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1656 aom_highbd_obmc_variance64x64,
1657 aom_highbd_obmc_sub_pixel_variance64x64)
1658 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1659 aom_highbd_obmc_variance64x32,
1660 aom_highbd_obmc_sub_pixel_variance64x32)
1661 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1662 aom_highbd_obmc_variance32x64,
1663 aom_highbd_obmc_sub_pixel_variance32x64)
1664 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1665 aom_highbd_obmc_variance32x32,
1666 aom_highbd_obmc_sub_pixel_variance32x32)
1667 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1668 aom_highbd_obmc_variance32x16,
1669 aom_highbd_obmc_sub_pixel_variance32x16)
1670 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1671 aom_highbd_obmc_variance16x32,
1672 aom_highbd_obmc_sub_pixel_variance16x32)
1673 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1674 aom_highbd_obmc_variance16x16,
1675 aom_highbd_obmc_sub_pixel_variance16x16)
1676 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1677 aom_highbd_obmc_variance8x16,
1678 aom_highbd_obmc_sub_pixel_variance8x16)
1679 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1680 aom_highbd_obmc_variance16x8,
1681 aom_highbd_obmc_sub_pixel_variance16x8)
1682 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1683 aom_highbd_obmc_variance8x8,
1684 aom_highbd_obmc_sub_pixel_variance8x8)
1685 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1686 aom_highbd_obmc_variance4x8,
1687 aom_highbd_obmc_sub_pixel_variance4x8)
1688 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1689 aom_highbd_obmc_variance8x4,
1690 aom_highbd_obmc_sub_pixel_variance8x4)
1691 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1692 aom_highbd_obmc_variance4x4,
1693 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001694 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1695 aom_highbd_obmc_variance64x16,
1696 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001697 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1698 aom_highbd_obmc_variance16x64,
1699 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001700 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1701 aom_highbd_obmc_variance32x8,
1702 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001703 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1704 aom_highbd_obmc_variance8x32,
1705 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001706 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1707 aom_highbd_obmc_variance16x4,
1708 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001709 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1710 aom_highbd_obmc_variance4x16,
1711 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001712 break;
1713
Yaowu Xuf883b422016-08-30 14:01:10 -07001714 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001715 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1716 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1717 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001718 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001719 aom_highbd_sad64x16x4d_bits10,
1720 aom_highbd_jnt_sad64x16_avg_bits10,
1721 aom_highbd_10_jnt_sub_pixel_avg_variance64x16);
1722
1723 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1724 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1725 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001726 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001727 aom_highbd_sad16x64x4d_bits10,
1728 aom_highbd_jnt_sad16x64_avg_bits10,
1729 aom_highbd_10_jnt_sub_pixel_avg_variance16x64);
1730
1731 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1732 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1733 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001734 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001735 aom_highbd_sad32x8x4d_bits10,
1736 aom_highbd_jnt_sad32x8_avg_bits10,
1737 aom_highbd_10_jnt_sub_pixel_avg_variance32x8);
1738
1739 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1740 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1741 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001742 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001743 aom_highbd_sad8x32x4d_bits10,
1744 aom_highbd_jnt_sad8x32_avg_bits10,
1745 aom_highbd_10_jnt_sub_pixel_avg_variance8x32);
1746
1747 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1748 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1749 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001750 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001751 aom_highbd_sad16x4x4d_bits10,
1752 aom_highbd_jnt_sad16x4_avg_bits10,
1753 aom_highbd_10_jnt_sub_pixel_avg_variance16x4);
1754
1755 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1756 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1757 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001758 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001759 aom_highbd_sad4x16x4d_bits10,
1760 aom_highbd_jnt_sad4x16_avg_bits10,
1761 aom_highbd_10_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001762
1763 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1764 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1765 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001766 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001767 aom_highbd_sad32x16x4d_bits10,
1768 aom_highbd_jnt_sad32x16_avg_bits10,
1769 aom_highbd_10_jnt_sub_pixel_avg_variance32x16);
1770
1771 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1772 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1773 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001774 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001775 aom_highbd_sad16x32x4d_bits10,
1776 aom_highbd_jnt_sad16x32_avg_bits10,
1777 aom_highbd_10_jnt_sub_pixel_avg_variance16x32);
1778
1779 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1780 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1781 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001782 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001783 aom_highbd_sad64x32x4d_bits10,
1784 aom_highbd_jnt_sad64x32_avg_bits10,
1785 aom_highbd_10_jnt_sub_pixel_avg_variance64x32);
1786
1787 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1788 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1789 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001790 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001791 aom_highbd_sad32x64x4d_bits10,
1792 aom_highbd_jnt_sad32x64_avg_bits10,
1793 aom_highbd_10_jnt_sub_pixel_avg_variance32x64);
1794
1795 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1796 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1797 aom_highbd_10_sub_pixel_variance32x32,
1798 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001799 aom_highbd_sad32x32x4d_bits10,
1800 aom_highbd_jnt_sad32x32_avg_bits10,
1801 aom_highbd_10_jnt_sub_pixel_avg_variance32x32);
1802
1803 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1804 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1805 aom_highbd_10_sub_pixel_variance64x64,
1806 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001807 aom_highbd_sad64x64x4d_bits10,
1808 aom_highbd_jnt_sad64x64_avg_bits10,
1809 aom_highbd_10_jnt_sub_pixel_avg_variance64x64);
1810
1811 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1812 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1813 aom_highbd_10_sub_pixel_variance16x16,
1814 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001815 aom_highbd_sad16x16x4d_bits10,
1816 aom_highbd_jnt_sad16x16_avg_bits10,
1817 aom_highbd_10_jnt_sub_pixel_avg_variance16x16);
1818
1819 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1820 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1821 aom_highbd_10_sub_pixel_variance16x8,
1822 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001823 aom_highbd_sad16x8x4d_bits10,
1824 aom_highbd_jnt_sad16x8_avg_bits10,
1825 aom_highbd_10_jnt_sub_pixel_avg_variance16x8);
1826
1827 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1828 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1829 aom_highbd_10_sub_pixel_variance8x16,
1830 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001831 aom_highbd_sad8x16x4d_bits10,
1832 aom_highbd_jnt_sad8x16_avg_bits10,
1833 aom_highbd_10_jnt_sub_pixel_avg_variance8x16);
1834
1835 HIGHBD_BFP(
1836 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1837 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001838 aom_highbd_10_sub_pixel_avg_variance8x8,
1839 aom_highbd_sad8x8x4d_bits10, aom_highbd_jnt_sad8x8_avg_bits10,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001840 aom_highbd_10_jnt_sub_pixel_avg_variance8x8);
1841
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001842 HIGHBD_BFP(
1843 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1844 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1845 aom_highbd_10_sub_pixel_avg_variance8x4,
1846 aom_highbd_sad8x4x4d_bits10, aom_highbd_jnt_sad8x4_avg_bits10,
1847 aom_highbd_10_jnt_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001848
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001849 HIGHBD_BFP(
1850 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1851 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1852 aom_highbd_10_sub_pixel_avg_variance4x8,
1853 aom_highbd_sad4x8x4d_bits10, aom_highbd_jnt_sad4x8_avg_bits10,
1854 aom_highbd_10_jnt_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001855
1856 HIGHBD_BFP(
1857 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1858 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001859 aom_highbd_10_sub_pixel_avg_variance4x4,
1860 aom_highbd_sad4x4x4d_bits10, aom_highbd_jnt_sad4x4_avg_bits10,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001861 aom_highbd_10_jnt_sub_pixel_avg_variance4x4);
1862
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001863 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1864 aom_highbd_sad128x128_avg_bits10,
1865 aom_highbd_10_variance128x128,
1866 aom_highbd_10_sub_pixel_variance128x128,
1867 aom_highbd_10_sub_pixel_avg_variance128x128,
1868 aom_highbd_sad128x128x4d_bits10,
1869 aom_highbd_jnt_sad128x128_avg_bits10,
1870 aom_highbd_10_jnt_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001871
1872 HIGHBD_BFP(
1873 BLOCK_128X64, aom_highbd_sad128x64_bits10,
1874 aom_highbd_sad128x64_avg_bits10, aom_highbd_10_variance128x64,
1875 aom_highbd_10_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001876 aom_highbd_10_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001877 aom_highbd_sad128x64x4d_bits10, aom_highbd_jnt_sad128x64_avg_bits10,
1878 aom_highbd_10_jnt_sub_pixel_avg_variance128x64);
1879
1880 HIGHBD_BFP(
1881 BLOCK_64X128, aom_highbd_sad64x128_bits10,
1882 aom_highbd_sad64x128_avg_bits10, aom_highbd_10_variance64x128,
1883 aom_highbd_10_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001884 aom_highbd_10_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001885 aom_highbd_sad64x128x4d_bits10, aom_highbd_jnt_sad64x128_avg_bits10,
1886 aom_highbd_10_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001887
David Barkerf19f35f2017-05-22 16:33:22 +01001888 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1889 aom_highbd_10_masked_sub_pixel_variance128x128)
1890 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1891 aom_highbd_10_masked_sub_pixel_variance128x64)
1892 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1893 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001894 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1895 aom_highbd_10_masked_sub_pixel_variance64x64)
1896 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1897 aom_highbd_10_masked_sub_pixel_variance64x32)
1898 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1899 aom_highbd_10_masked_sub_pixel_variance32x64)
1900 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1901 aom_highbd_10_masked_sub_pixel_variance32x32)
1902 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1903 aom_highbd_10_masked_sub_pixel_variance32x16)
1904 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1905 aom_highbd_10_masked_sub_pixel_variance16x32)
1906 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1907 aom_highbd_10_masked_sub_pixel_variance16x16)
1908 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1909 aom_highbd_10_masked_sub_pixel_variance8x16)
1910 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1911 aom_highbd_10_masked_sub_pixel_variance16x8)
1912 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1913 aom_highbd_10_masked_sub_pixel_variance8x8)
1914 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1915 aom_highbd_10_masked_sub_pixel_variance4x8)
1916 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1917 aom_highbd_10_masked_sub_pixel_variance8x4)
1918 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1919 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001920 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1921 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001922 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1923 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001924 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1925 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001926 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1927 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001928 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1929 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001930 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
1931 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001932 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
1933 aom_highbd_10_obmc_variance128x128,
1934 aom_highbd_10_obmc_sub_pixel_variance128x128)
1935 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
1936 aom_highbd_10_obmc_variance128x64,
1937 aom_highbd_10_obmc_sub_pixel_variance128x64)
1938 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
1939 aom_highbd_10_obmc_variance64x128,
1940 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001941 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
1942 aom_highbd_10_obmc_variance64x64,
1943 aom_highbd_10_obmc_sub_pixel_variance64x64)
1944 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
1945 aom_highbd_10_obmc_variance64x32,
1946 aom_highbd_10_obmc_sub_pixel_variance64x32)
1947 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
1948 aom_highbd_10_obmc_variance32x64,
1949 aom_highbd_10_obmc_sub_pixel_variance32x64)
1950 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
1951 aom_highbd_10_obmc_variance32x32,
1952 aom_highbd_10_obmc_sub_pixel_variance32x32)
1953 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
1954 aom_highbd_10_obmc_variance32x16,
1955 aom_highbd_10_obmc_sub_pixel_variance32x16)
1956 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
1957 aom_highbd_10_obmc_variance16x32,
1958 aom_highbd_10_obmc_sub_pixel_variance16x32)
1959 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
1960 aom_highbd_10_obmc_variance16x16,
1961 aom_highbd_10_obmc_sub_pixel_variance16x16)
1962 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
1963 aom_highbd_10_obmc_variance8x16,
1964 aom_highbd_10_obmc_sub_pixel_variance8x16)
1965 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
1966 aom_highbd_10_obmc_variance16x8,
1967 aom_highbd_10_obmc_sub_pixel_variance16x8)
1968 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
1969 aom_highbd_10_obmc_variance8x8,
1970 aom_highbd_10_obmc_sub_pixel_variance8x8)
1971 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
1972 aom_highbd_10_obmc_variance4x8,
1973 aom_highbd_10_obmc_sub_pixel_variance4x8)
1974 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
1975 aom_highbd_10_obmc_variance8x4,
1976 aom_highbd_10_obmc_sub_pixel_variance8x4)
1977 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
1978 aom_highbd_10_obmc_variance4x4,
1979 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01001980
Rupert Swarbrick72678572017-08-02 12:05:26 +01001981 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
1982 aom_highbd_10_obmc_variance64x16,
1983 aom_highbd_10_obmc_sub_pixel_variance64x16)
1984
1985 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
1986 aom_highbd_10_obmc_variance16x64,
1987 aom_highbd_10_obmc_sub_pixel_variance16x64)
1988
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001989 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
1990 aom_highbd_10_obmc_variance32x8,
1991 aom_highbd_10_obmc_sub_pixel_variance32x8)
1992
1993 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
1994 aom_highbd_10_obmc_variance8x32,
1995 aom_highbd_10_obmc_sub_pixel_variance8x32)
1996
1997 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
1998 aom_highbd_10_obmc_variance16x4,
1999 aom_highbd_10_obmc_sub_pixel_variance16x4)
2000
2001 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2002 aom_highbd_10_obmc_variance4x16,
2003 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002004 break;
2005
Yaowu Xuf883b422016-08-30 14:01:10 -07002006 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002007 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2008 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2009 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002010 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002011 aom_highbd_sad64x16x4d_bits12,
2012 aom_highbd_jnt_sad64x16_avg_bits12,
2013 aom_highbd_12_jnt_sub_pixel_avg_variance64x16);
2014
2015 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2016 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2017 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002018 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002019 aom_highbd_sad16x64x4d_bits12,
2020 aom_highbd_jnt_sad16x64_avg_bits12,
2021 aom_highbd_12_jnt_sub_pixel_avg_variance16x64);
2022
2023 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2024 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2025 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002026 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002027 aom_highbd_sad32x8x4d_bits12,
2028 aom_highbd_jnt_sad32x8_avg_bits12,
2029 aom_highbd_12_jnt_sub_pixel_avg_variance32x8);
2030
2031 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2032 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2033 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002034 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002035 aom_highbd_sad8x32x4d_bits12,
2036 aom_highbd_jnt_sad8x32_avg_bits12,
2037 aom_highbd_12_jnt_sub_pixel_avg_variance8x32);
2038
2039 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2040 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2041 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002042 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002043 aom_highbd_sad16x4x4d_bits12,
2044 aom_highbd_jnt_sad16x4_avg_bits12,
2045 aom_highbd_12_jnt_sub_pixel_avg_variance16x4);
2046
2047 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2048 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2049 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002050 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002051 aom_highbd_sad4x16x4d_bits12,
2052 aom_highbd_jnt_sad4x16_avg_bits12,
2053 aom_highbd_12_jnt_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002054
2055 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2056 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2057 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002058 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002059 aom_highbd_sad32x16x4d_bits12,
2060 aom_highbd_jnt_sad32x16_avg_bits12,
2061 aom_highbd_12_jnt_sub_pixel_avg_variance32x16);
2062
2063 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2064 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2065 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002066 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002067 aom_highbd_sad16x32x4d_bits12,
2068 aom_highbd_jnt_sad16x32_avg_bits12,
2069 aom_highbd_12_jnt_sub_pixel_avg_variance16x32);
2070
2071 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2072 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2073 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002074 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002075 aom_highbd_sad64x32x4d_bits12,
2076 aom_highbd_jnt_sad64x32_avg_bits12,
2077 aom_highbd_12_jnt_sub_pixel_avg_variance64x32);
2078
2079 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2080 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2081 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002082 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002083 aom_highbd_sad32x64x4d_bits12,
2084 aom_highbd_jnt_sad32x64_avg_bits12,
2085 aom_highbd_12_jnt_sub_pixel_avg_variance32x64);
2086
2087 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2088 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2089 aom_highbd_12_sub_pixel_variance32x32,
2090 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002091 aom_highbd_sad32x32x4d_bits12,
2092 aom_highbd_jnt_sad32x32_avg_bits12,
2093 aom_highbd_12_jnt_sub_pixel_avg_variance32x32);
2094
2095 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2096 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2097 aom_highbd_12_sub_pixel_variance64x64,
2098 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002099 aom_highbd_sad64x64x4d_bits12,
2100 aom_highbd_jnt_sad64x64_avg_bits12,
2101 aom_highbd_12_jnt_sub_pixel_avg_variance64x64);
2102
2103 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2104 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2105 aom_highbd_12_sub_pixel_variance16x16,
2106 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002107 aom_highbd_sad16x16x4d_bits12,
2108 aom_highbd_jnt_sad16x16_avg_bits12,
2109 aom_highbd_12_jnt_sub_pixel_avg_variance16x16);
2110
2111 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2112 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2113 aom_highbd_12_sub_pixel_variance16x8,
2114 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002115 aom_highbd_sad16x8x4d_bits12,
2116 aom_highbd_jnt_sad16x8_avg_bits12,
2117 aom_highbd_12_jnt_sub_pixel_avg_variance16x8);
2118
2119 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2120 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2121 aom_highbd_12_sub_pixel_variance8x16,
2122 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002123 aom_highbd_sad8x16x4d_bits12,
2124 aom_highbd_jnt_sad8x16_avg_bits12,
2125 aom_highbd_12_jnt_sub_pixel_avg_variance8x16);
2126
2127 HIGHBD_BFP(
2128 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2129 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002130 aom_highbd_12_sub_pixel_avg_variance8x8,
2131 aom_highbd_sad8x8x4d_bits12, aom_highbd_jnt_sad8x8_avg_bits12,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002132 aom_highbd_12_jnt_sub_pixel_avg_variance8x8);
2133
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002134 HIGHBD_BFP(
2135 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2136 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2137 aom_highbd_12_sub_pixel_avg_variance8x4,
2138 aom_highbd_sad8x4x4d_bits12, aom_highbd_jnt_sad8x4_avg_bits12,
2139 aom_highbd_12_jnt_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002140
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002141 HIGHBD_BFP(
2142 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2143 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2144 aom_highbd_12_sub_pixel_avg_variance4x8,
2145 aom_highbd_sad4x8x4d_bits12, aom_highbd_jnt_sad4x8_avg_bits12,
2146 aom_highbd_12_jnt_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002147
2148 HIGHBD_BFP(
2149 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2150 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002151 aom_highbd_12_sub_pixel_avg_variance4x4,
2152 aom_highbd_sad4x4x4d_bits12, aom_highbd_jnt_sad4x4_avg_bits12,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002153 aom_highbd_12_jnt_sub_pixel_avg_variance4x4);
2154
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002155 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2156 aom_highbd_sad128x128_avg_bits12,
2157 aom_highbd_12_variance128x128,
2158 aom_highbd_12_sub_pixel_variance128x128,
2159 aom_highbd_12_sub_pixel_avg_variance128x128,
2160 aom_highbd_sad128x128x4d_bits12,
2161 aom_highbd_jnt_sad128x128_avg_bits12,
2162 aom_highbd_12_jnt_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002163
2164 HIGHBD_BFP(
2165 BLOCK_128X64, aom_highbd_sad128x64_bits12,
2166 aom_highbd_sad128x64_avg_bits12, aom_highbd_12_variance128x64,
2167 aom_highbd_12_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002168 aom_highbd_12_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002169 aom_highbd_sad128x64x4d_bits12, aom_highbd_jnt_sad128x64_avg_bits12,
2170 aom_highbd_12_jnt_sub_pixel_avg_variance128x64);
2171
2172 HIGHBD_BFP(
2173 BLOCK_64X128, aom_highbd_sad64x128_bits12,
2174 aom_highbd_sad64x128_avg_bits12, aom_highbd_12_variance64x128,
2175 aom_highbd_12_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002176 aom_highbd_12_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002177 aom_highbd_sad64x128x4d_bits12, aom_highbd_jnt_sad64x128_avg_bits12,
2178 aom_highbd_12_jnt_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002179
David Barkerf19f35f2017-05-22 16:33:22 +01002180 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2181 aom_highbd_12_masked_sub_pixel_variance128x128)
2182 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2183 aom_highbd_12_masked_sub_pixel_variance128x64)
2184 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2185 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002186 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2187 aom_highbd_12_masked_sub_pixel_variance64x64)
2188 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2189 aom_highbd_12_masked_sub_pixel_variance64x32)
2190 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2191 aom_highbd_12_masked_sub_pixel_variance32x64)
2192 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2193 aom_highbd_12_masked_sub_pixel_variance32x32)
2194 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2195 aom_highbd_12_masked_sub_pixel_variance32x16)
2196 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2197 aom_highbd_12_masked_sub_pixel_variance16x32)
2198 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2199 aom_highbd_12_masked_sub_pixel_variance16x16)
2200 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2201 aom_highbd_12_masked_sub_pixel_variance8x16)
2202 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2203 aom_highbd_12_masked_sub_pixel_variance16x8)
2204 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2205 aom_highbd_12_masked_sub_pixel_variance8x8)
2206 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2207 aom_highbd_12_masked_sub_pixel_variance4x8)
2208 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2209 aom_highbd_12_masked_sub_pixel_variance8x4)
2210 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2211 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002212 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2213 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002214 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2215 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002216 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2217 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002218 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2219 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002220 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2221 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002222 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2223 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002224 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2225 aom_highbd_12_obmc_variance128x128,
2226 aom_highbd_12_obmc_sub_pixel_variance128x128)
2227 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2228 aom_highbd_12_obmc_variance128x64,
2229 aom_highbd_12_obmc_sub_pixel_variance128x64)
2230 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2231 aom_highbd_12_obmc_variance64x128,
2232 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002233 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2234 aom_highbd_12_obmc_variance64x64,
2235 aom_highbd_12_obmc_sub_pixel_variance64x64)
2236 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2237 aom_highbd_12_obmc_variance64x32,
2238 aom_highbd_12_obmc_sub_pixel_variance64x32)
2239 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2240 aom_highbd_12_obmc_variance32x64,
2241 aom_highbd_12_obmc_sub_pixel_variance32x64)
2242 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2243 aom_highbd_12_obmc_variance32x32,
2244 aom_highbd_12_obmc_sub_pixel_variance32x32)
2245 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2246 aom_highbd_12_obmc_variance32x16,
2247 aom_highbd_12_obmc_sub_pixel_variance32x16)
2248 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2249 aom_highbd_12_obmc_variance16x32,
2250 aom_highbd_12_obmc_sub_pixel_variance16x32)
2251 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2252 aom_highbd_12_obmc_variance16x16,
2253 aom_highbd_12_obmc_sub_pixel_variance16x16)
2254 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2255 aom_highbd_12_obmc_variance8x16,
2256 aom_highbd_12_obmc_sub_pixel_variance8x16)
2257 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2258 aom_highbd_12_obmc_variance16x8,
2259 aom_highbd_12_obmc_sub_pixel_variance16x8)
2260 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2261 aom_highbd_12_obmc_variance8x8,
2262 aom_highbd_12_obmc_sub_pixel_variance8x8)
2263 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2264 aom_highbd_12_obmc_variance4x8,
2265 aom_highbd_12_obmc_sub_pixel_variance4x8)
2266 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2267 aom_highbd_12_obmc_variance8x4,
2268 aom_highbd_12_obmc_sub_pixel_variance8x4)
2269 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2270 aom_highbd_12_obmc_variance4x4,
2271 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002272 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2273 aom_highbd_12_obmc_variance64x16,
2274 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002275 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2276 aom_highbd_12_obmc_variance16x64,
2277 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002278 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2279 aom_highbd_12_obmc_variance32x8,
2280 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002281 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2282 aom_highbd_12_obmc_variance8x32,
2283 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002284 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2285 aom_highbd_12_obmc_variance16x4,
2286 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002287 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2288 aom_highbd_12_obmc_variance4x16,
2289 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002290 break;
2291
2292 default:
2293 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002294 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002295 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002296 }
2297 }
2298}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002299
Yaowu Xuf883b422016-08-30 14:01:10 -07002300static void realloc_segmentation_maps(AV1_COMP *cpi) {
2301 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002302
2303 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002304 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002305 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002306 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002307
2308 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002309 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002310 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002311 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002312
2313 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002314 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002315 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002316 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002317}
2318
Yaowu Xuf883b422016-08-30 14:01:10 -07002319void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2320 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002321 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002322 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002323 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002324 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002325
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002326 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2327 seq_params->bit_depth = oxcf->bit_depth;
2328 seq_params->color_primaries = oxcf->color_primaries;
2329 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2330 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2331 seq_params->monochrome = oxcf->monochrome;
2332 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2333 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002334
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002335 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2336 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002337
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002338 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002339 cm->timing_info.num_units_in_display_tick =
2340 oxcf->timing_info.num_units_in_display_tick;
2341 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2342 cm->timing_info.equal_picture_interval =
2343 oxcf->timing_info.equal_picture_interval;
2344 cm->timing_info.num_ticks_per_picture =
2345 oxcf->timing_info.num_ticks_per_picture;
2346
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002347 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002348 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002349 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002350 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002351 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002352 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002353 cm->buffer_model.num_units_in_decoding_tick =
2354 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002355 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08002356 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07002357 set_dec_model_op_parameters(&cm->op_params[0]);
2358 } else if (cm->timing_info_present &&
2359 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002360 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002361 // set the decoder model parameters in resource availability mode
2362 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002363 } else {
2364 cm->op_params[0].initial_display_delay =
2365 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002366 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002367
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002368 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002369
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002371 cpi->common.options = oxcf->cfg;
Ravi Chaudhary0ec03a42018-08-17 18:53:28 +05302372 cpi->row_mt = oxcf->row_mt;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002373 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002374 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002375
Yaowu Xuf883b422016-08-30 14:01:10 -07002376 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002377 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2378 } else {
2379 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2380 }
2381
2382 cpi->refresh_last_frame = 1;
2383 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002384 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002385 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002387 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2388 ? REFRESH_FRAME_CONTEXT_DISABLED
2389 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002390 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002391 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002392
Alex Converse74ad0912017-07-18 10:22:58 -07002393 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002394 CHECK_MEM_ERROR(cm, x->palette_buffer,
2395 aom_memalign(16, sizeof(*x->palette_buffer)));
2396 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002397
2398 if (x->tmp_conv_dst == NULL) {
2399 CHECK_MEM_ERROR(
2400 cm, x->tmp_conv_dst,
2401 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002402 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002403 }
2404 for (int i = 0; i < 2; ++i) {
2405 if (x->tmp_obmc_bufs[i] == NULL) {
2406 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
2407 aom_memalign(16, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
2408 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002409 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002410 }
2411 }
2412
Yaowu Xuf883b422016-08-30 14:01:10 -07002413 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002414 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002415
Yaowu Xuc27fc142016-08-22 16:08:15 -07002416 set_rc_buffer_sizes(rc, &cpi->oxcf);
2417
2418 // Under a configuration change, where maximum_buffer_size may change,
2419 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002420 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2421 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002422
2423 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002424 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002425
2426 // Set absolute upper and lower quality limits
2427 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2428 rc->best_quality = cpi->oxcf.best_allowed_q;
2429
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002430 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002431 cm->switchable_motion_mode = 1;
2432
Yaowu Xuc27fc142016-08-22 16:08:15 -07002433 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2434 cm->render_width = cpi->oxcf.render_width;
2435 cm->render_height = cpi->oxcf.render_height;
2436 } else {
2437 cm->render_width = cpi->oxcf.width;
2438 cm->render_height = cpi->oxcf.height;
2439 }
2440 cm->width = cpi->oxcf.width;
2441 cm->height = cpi->oxcf.height;
2442
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002443 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002444 // Superblock size should not be updated after the first key frame.
2445 if (!cpi->seq_params_locked) {
2446 set_sb_size(&cm->seq_params, select_sb_size(cpi));
2447 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002448
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002449 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002450 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002451 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002452 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002453 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002454 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002455 realloc_segmentation_maps(cpi);
2456 cpi->initial_width = cpi->initial_height = 0;
2457 }
2458 }
2459 update_frame_size(cpi);
2460
2461 cpi->alt_ref_source = NULL;
2462 rc->is_src_frame_alt_ref = 0;
2463
Yaowu Xuc27fc142016-08-22 16:08:15 -07002464 rc->is_bwd_ref_frame = 0;
2465 rc->is_last_bipred_frame = 0;
2466 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002467
Yaowu Xuc27fc142016-08-22 16:08:15 -07002468 set_tile_info(cpi);
2469
2470 cpi->ext_refresh_frame_flags_pending = 0;
2471 cpi->ext_refresh_frame_context_pending = 0;
2472
Yaowu Xuc27fc142016-08-22 16:08:15 -07002473 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002474
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002475 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002476 // This should not be called after the first key frame.
2477 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002478 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002479 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Andrey Norkin26495512018-06-20 17:13:11 -07002480 init_seq_coding_tools(&cm->seq_params, cm, oxcf);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002481 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002482}
2483
Yaowu Xuf883b422016-08-30 14:01:10 -07002484AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2485 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002486 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002487 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2488 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002489
2490 if (!cm) return NULL;
2491
Yaowu Xuf883b422016-08-30 14:01:10 -07002492 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002493
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002494 // The jmp_buf is valid only for the duration of the function that calls
2495 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2496 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002497 if (setjmp(cm->error.jmp)) {
2498 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002499 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002500 return 0;
2501 }
2502
2503 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002504 cm->alloc_mi = enc_alloc_mi;
2505 cm->free_mi = enc_free_mi;
2506 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002507
Angie Chianga5d96c42016-10-21 16:16:56 -07002508 CHECK_MEM_ERROR(cm, cm->fc,
2509 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
2510 CHECK_MEM_ERROR(cm, cm->frame_contexts,
2511 (FRAME_CONTEXT *)aom_memalign(
2512 32, FRAME_CONTEXTS * sizeof(*cm->frame_contexts)));
2513 memset(cm->fc, 0, sizeof(*cm->fc));
2514 memset(cm->frame_contexts, 0, FRAME_CONTEXTS * sizeof(*cm->frame_contexts));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002515
2516 cpi->resize_state = 0;
2517 cpi->resize_avg_qp = 0;
2518 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002519
Yaowu Xuc27fc142016-08-22 16:08:15 -07002520 cpi->common.buffer_pool = pool;
2521
2522 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002523 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002524
2525 cm->current_video_frame = 0;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002526 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002527 cpi->partition_search_skippable_frame = 0;
2528 cpi->tile_data = NULL;
2529 cpi->last_show_frame_buf_idx = INVALID_IDX;
2530
2531 realloc_segmentation_maps(cpi);
2532
Jingning Hanf050fc12018-03-09 14:53:33 -08002533 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2534 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002535
Yaowu Xuc27fc142016-08-22 16:08:15 -07002536 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2537 i++) {
2538 CHECK_MEM_ERROR(
2539 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002540 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002541 }
2542
2543#if CONFIG_FP_MB_STATS
2544 cpi->use_fp_mb_stats = 0;
2545 if (cpi->use_fp_mb_stats) {
2546 // a place holder used to store the first pass mb stats in the first pass
2547 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002548 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002549 } else {
2550 cpi->twopass.frame_mb_stats_buf = NULL;
2551 }
2552#endif
2553
2554 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002555
2556 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2557#if CONFIG_INTERNAL_STATS
2558 cpi->b_calculate_blockiness = 1;
2559 cpi->b_calculate_consistency = 1;
2560 cpi->total_inconsistency = 0;
2561 cpi->psnr.worst = 100.0;
2562 cpi->worst_ssim = 100.0;
2563
2564 cpi->count = 0;
2565 cpi->bytes = 0;
2566
2567 if (cpi->b_calculate_psnr) {
2568 cpi->total_sq_error = 0;
2569 cpi->total_samples = 0;
2570 cpi->tot_recode_hits = 0;
2571 cpi->summed_quality = 0;
2572 cpi->summed_weights = 0;
2573 }
2574
2575 cpi->fastssim.worst = 100.0;
2576 cpi->psnrhvs.worst = 100.0;
2577
2578 if (cpi->b_calculate_blockiness) {
2579 cpi->total_blockiness = 0;
2580 cpi->worst_blockiness = 0.0;
2581 }
2582
2583 if (cpi->b_calculate_consistency) {
2584 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002585 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002586 cpi->common.mi_rows * cpi->common.mi_cols));
2587 cpi->worst_consistency = 100.0;
2588 }
2589#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002590#if CONFIG_ENTROPY_STATS
2591 av1_zero(aggregate_fc);
2592#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002593
2594 cpi->first_time_stamp_ever = INT64_MAX;
2595
Jingning Hanf050fc12018-03-09 14:53:33 -08002596 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2597 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2598 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2599 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002600
Yaowu Xuc27fc142016-08-22 16:08:15 -07002601#ifdef OUTPUT_YUV_SKINMAP
2602 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2603#endif
2604#ifdef OUTPUT_YUV_REC
2605 yuv_rec_file = fopen("rec.yuv", "wb");
2606#endif
2607
Yaowu Xuc27fc142016-08-22 16:08:15 -07002608 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002609 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002610 } else if (oxcf->pass == 2) {
2611 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2612 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2613
2614#if CONFIG_FP_MB_STATS
2615 if (cpi->use_fp_mb_stats) {
2616 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2617 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2618
2619 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2620 oxcf->firstpass_mb_stats_in.buf;
2621 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2622 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2623 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2624 }
2625#endif
2626
2627 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2628 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2629 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2630
Yaowu Xuf883b422016-08-30 14:01:10 -07002631 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002632 }
2633
Jingning Hand064cf02017-06-01 10:00:39 -07002634 CHECK_MEM_ERROR(
2635 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002636 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002637 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002638 CHECK_MEM_ERROR(
2639 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002640 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002641 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002642
2643 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2644 (int32_t *)aom_memalign(
2645 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2646
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302647 for (int x = 0; x < 2; x++)
2648 for (int y = 0; y < 2; y++)
2649 CHECK_MEM_ERROR(
2650 cm, cpi->td.mb.hash_value_buffer[x][y],
2651 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2652 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2653
2654 cpi->td.mb.g_crc_initialized = 0;
2655
Jingning Hand064cf02017-06-01 10:00:39 -07002656 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2657 (int32_t *)aom_memalign(
2658 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2659
Yaowu Xuf883b422016-08-30 14:01:10 -07002660 av1_set_speed_features_framesize_independent(cpi);
2661 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002662
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002663#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2664 cpi->fn_ptr[BT].sdf = SDF; \
2665 cpi->fn_ptr[BT].sdaf = SDAF; \
2666 cpi->fn_ptr[BT].vf = VF; \
2667 cpi->fn_ptr[BT].svf = SVF; \
2668 cpi->fn_ptr[BT].svaf = SVAF; \
2669 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2670 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002671 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672
Cheng Chenf78632e2017-10-20 15:30:51 -07002673 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002674 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002675 aom_sad4x16x4d, aom_jnt_sad4x16_avg, aom_jnt_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002676
2677 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002678 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Cheng Chend0179a62017-11-16 17:02:53 -08002679 aom_sad16x4x4d, aom_jnt_sad16x4_avg, aom_jnt_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002680
2681 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002682 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002683 aom_sad8x32x4d, aom_jnt_sad8x32_avg, aom_jnt_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002684
2685 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002686 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Cheng Chend0179a62017-11-16 17:02:53 -08002687 aom_sad32x8x4d, aom_jnt_sad32x8_avg, aom_jnt_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002688
2689 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002690 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002691 aom_sad16x64x4d, aom_jnt_sad16x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002692 aom_jnt_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002693
2694 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002695 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002696 aom_sad64x16x4d, aom_jnt_sad64x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002697 aom_jnt_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002698
Cheng Chenf78632e2017-10-20 15:30:51 -07002699 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2700 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002701 aom_sad128x128x4d, aom_jnt_sad128x128_avg,
2702 aom_jnt_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002703
2704 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002705 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
2706 aom_sad128x64x4d, aom_jnt_sad128x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002707 aom_jnt_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002708
2709 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002710 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
2711 aom_sad64x128x4d, aom_jnt_sad64x128_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002712 aom_jnt_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002713
2714 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002715 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Cheng Chend0179a62017-11-16 17:02:53 -08002716 aom_sad32x16x4d, aom_jnt_sad32x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002717 aom_jnt_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002718
2719 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002720 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002721 aom_sad16x32x4d, aom_jnt_sad16x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002722 aom_jnt_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002723
2724 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002725 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Cheng Chend0179a62017-11-16 17:02:53 -08002726 aom_sad64x32x4d, aom_jnt_sad64x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002727 aom_jnt_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002728
2729 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002730 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Cheng Chend0179a62017-11-16 17:02:53 -08002731 aom_sad32x64x4d, aom_jnt_sad32x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002732 aom_jnt_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002733
2734 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2735 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002736 aom_sad32x32x4d, aom_jnt_sad32x32_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002737 aom_jnt_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002738
2739 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2740 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002741 aom_sad64x64x4d, aom_jnt_sad64x64_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002742 aom_jnt_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002743
2744 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2745 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002746 aom_sad16x16x4d, aom_jnt_sad16x16_avg,
Cheng Chend2864432017-11-17 17:59:24 -08002747 aom_jnt_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002748
2749 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002750 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
2751 aom_sad16x8x4d, aom_jnt_sad16x8_avg, aom_jnt_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002752
2753 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002754 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
2755 aom_sad8x16x4d, aom_jnt_sad8x16_avg, aom_jnt_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002756
2757 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002758 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
2759 aom_jnt_sad8x8_avg, aom_jnt_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002760
2761 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002762 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
2763 aom_jnt_sad8x4_avg, aom_jnt_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002764
2765 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002766 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
2767 aom_jnt_sad4x8_avg, aom_jnt_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002768
2769 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002770 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
2771 aom_jnt_sad4x4_avg, aom_jnt_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002772
Yaowu Xuc27fc142016-08-22 16:08:15 -07002773#define OBFP(BT, OSDF, OVF, OSVF) \
2774 cpi->fn_ptr[BT].osdf = OSDF; \
2775 cpi->fn_ptr[BT].ovf = OVF; \
2776 cpi->fn_ptr[BT].osvf = OSVF;
2777
Yaowu Xuf883b422016-08-30 14:01:10 -07002778 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2779 aom_obmc_sub_pixel_variance128x128)
2780 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2781 aom_obmc_sub_pixel_variance128x64)
2782 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2783 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002784 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2785 aom_obmc_sub_pixel_variance64x64)
2786 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2787 aom_obmc_sub_pixel_variance64x32)
2788 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2789 aom_obmc_sub_pixel_variance32x64)
2790 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2791 aom_obmc_sub_pixel_variance32x32)
2792 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2793 aom_obmc_sub_pixel_variance32x16)
2794 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2795 aom_obmc_sub_pixel_variance16x32)
2796 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2797 aom_obmc_sub_pixel_variance16x16)
2798 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2799 aom_obmc_sub_pixel_variance16x8)
2800 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2801 aom_obmc_sub_pixel_variance8x16)
2802 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2803 aom_obmc_sub_pixel_variance8x8)
2804 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2805 aom_obmc_sub_pixel_variance4x8)
2806 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2807 aom_obmc_sub_pixel_variance8x4)
2808 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2809 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002810 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2811 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002812 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2813 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002814 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2815 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002816 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2817 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002818 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2819 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002820 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2821 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002822
David Barkerf19f35f2017-05-22 16:33:22 +01002823#define MBFP(BT, MCSDF, MCSVF) \
2824 cpi->fn_ptr[BT].msdf = MCSDF; \
2825 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002826
David Barkerf19f35f2017-05-22 16:33:22 +01002827 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2828 aom_masked_sub_pixel_variance128x128)
2829 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2830 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002831 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2832 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2833 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2834 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2835 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2836 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2837 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2838 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2839 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2840 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2841 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2842 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2843 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002844
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002845 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2846
2847 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2848
2849 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2850
2851 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002852
2853 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2854
2855 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002856
Yaowu Xuc27fc142016-08-22 16:08:15 -07002857 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002858
Yaowu Xuf883b422016-08-30 14:01:10 -07002859 /* av1_init_quantizer() is first called here. Add check in
2860 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002861 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002862 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002863 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002864 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08002865 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002866
Yaowu Xuf883b422016-08-30 14:01:10 -07002867 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07002868 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002869 cm->superres_upscaled_width = oxcf->width;
2870 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002871 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002872
2873 cm->error.setjmp = 0;
2874
2875 return cpi;
2876}
2877
Urvang Joshiee2c8112018-05-04 14:53:15 -07002878#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002879#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2880
2881#define SNPRINT2(H, T, V) \
2882 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07002883#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002884
Yaowu Xuf883b422016-08-30 14:01:10 -07002885void av1_remove_compressor(AV1_COMP *cpi) {
2886 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002887 unsigned int i;
2888 int t;
2889
2890 if (!cpi) return;
2891
2892 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002893 const int num_planes = av1_num_planes(cm);
2894
Yaowu Xuc27fc142016-08-22 16:08:15 -07002895 if (cm->current_video_frame > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002896#if CONFIG_ENTROPY_STATS
2897 if (cpi->oxcf.pass != 1) {
2898 fprintf(stderr, "Writing counts.stt\n");
2899 FILE *f = fopen("counts.stt", "wb");
2900 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
2901 fclose(f);
2902 }
2903#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002904#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07002905 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002906
2907 if (cpi->oxcf.pass != 1) {
2908 char headings[512] = { 0 };
2909 char results[512] = { 0 };
2910 FILE *f = fopen("opsnr.stt", "a");
2911 double time_encoded =
2912 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
2913 10000000.000;
2914 double total_encode_time =
2915 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
2916 const double dr =
2917 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
2918 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
2919 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
2920 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
2921
2922 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002923 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
2925 const double total_ssim =
2926 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
2927 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08002928 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07002929 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002930 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08002931 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002932 snprintf(results, sizeof(results),
2933 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
2934 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002935 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08002936 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07002937 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
2938 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
2939 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
2940 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08002941 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07002942 cpi->psnr.stat[STAT_Y] / cpi->count,
2943 cpi->psnr.stat[STAT_U] / cpi->count,
2944 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002945
2946 if (cpi->b_calculate_blockiness) {
2947 SNPRINT(headings, "\t Block\tWstBlck");
2948 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
2949 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
2950 }
2951
2952 if (cpi->b_calculate_consistency) {
2953 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07002954 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955 (double)cpi->total_inconsistency);
2956
2957 SNPRINT(headings, "\tConsist\tWstCons");
2958 SNPRINT2(results, "\t%7.3f", consistency);
2959 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
2960 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07002961 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
2962 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963 rate_err, fabs(rate_err));
2964 }
2965
2966 fclose(f);
2967 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07002968#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969 }
2970
2971 for (t = 0; t < cpi->num_workers; ++t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002972 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07002973 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
2974
2975 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07002976 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002977
2978 // Deallocate allocated thread data.
2979 if (t < cpi->num_workers - 1) {
hui sud9a812b2017-07-06 14:34:37 -07002980 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002981 aom_free(thread_data->td->tmp_conv_dst);
2982 for (int j = 0; j < 2; ++j) {
2983 aom_free(thread_data->td->tmp_obmc_bufs[j]);
2984 }
Jingning Hand064cf02017-06-01 10:00:39 -07002985 aom_free(thread_data->td->above_pred_buf);
2986 aom_free(thread_data->td->left_pred_buf);
2987 aom_free(thread_data->td->wsrc_buf);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002988 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302989 for (int y = 0; y < 2; y++) {
2990 aom_free(thread_data->td->hash_value_buffer[x][y]);
2991 thread_data->td->hash_value_buffer[x][y] = NULL;
2992 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002993 }
Jingning Hand064cf02017-06-01 10:00:39 -07002994 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002995 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002996 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07002997 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002998 }
2999 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003000 aom_free(cpi->tile_thr_data);
3001 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003002
Deepa K G964e72e2018-05-16 16:56:01 +05303003 if (cpi->num_workers > 1) {
3004 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303005 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303006 }
3007
Yaowu Xuc27fc142016-08-22 16:08:15 -07003008 dealloc_compressor_data(cpi);
3009
3010 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3011 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003012 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003013 }
3014
3015#if CONFIG_FP_MB_STATS
3016 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003017 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003018 cpi->twopass.frame_mb_stats_buf = NULL;
3019 }
3020#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003021#if CONFIG_INTERNAL_STATS
3022 aom_free(cpi->ssim_vars);
3023 cpi->ssim_vars = NULL;
3024#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003025
Yaowu Xuf883b422016-08-30 14:01:10 -07003026 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003027 for (i = 0; i < FRAME_BUFFERS; ++i) {
3028 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3029 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003030 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xuf883b422016-08-30 14:01:10 -07003031 av1_free_ref_frame_buffers(cm->buffer_pool);
3032 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003033
3034#ifdef OUTPUT_YUV_SKINMAP
3035 fclose(yuv_skinmap_file);
3036#endif
3037#ifdef OUTPUT_YUV_REC
3038 fclose(yuv_rec_file);
3039#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003040}
3041
Yaowu Xuf883b422016-08-30 14:01:10 -07003042static void generate_psnr_packet(AV1_COMP *cpi) {
3043 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003044 int i;
3045 PSNR_STATS psnr;
Alex Conversef77fd0b2017-04-20 11:00:24 -07003046 aom_calc_highbd_psnr(cpi->source, cpi->common.frame_to_show, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003047 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003048
3049 for (i = 0; i < 4; ++i) {
3050 pkt.data.psnr.samples[i] = psnr.samples[i];
3051 pkt.data.psnr.sse[i] = psnr.sse[i];
3052 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3053 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003054 pkt.kind = AOM_CODEC_PSNR_PKT;
3055 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003056}
3057
Yaowu Xuf883b422016-08-30 14:01:10 -07003058int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003059 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3060
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003061 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003062 return 0;
3063}
3064
Yunqing Wang9a50fec2017-11-02 17:02:00 -07003065void av1_update_reference(AV1_COMP *cpi, int ref_frame_upd_flags) {
3066 cpi->ext_refresh_last_frame = (ref_frame_upd_flags & AOM_LAST_FLAG) != 0;
3067 cpi->ext_refresh_golden_frame = (ref_frame_upd_flags & AOM_GOLD_FLAG) != 0;
3068 cpi->ext_refresh_alt_ref_frame = (ref_frame_upd_flags & AOM_ALT_FLAG) != 0;
3069 cpi->ext_refresh_bwd_ref_frame = (ref_frame_upd_flags & AOM_BWD_FLAG) != 0;
3070 cpi->ext_refresh_alt2_ref_frame = (ref_frame_upd_flags & AOM_ALT2_FLAG) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003071 cpi->ext_refresh_frame_flags_pending = 1;
3072}
3073
Thomas Daede497d1952017-08-08 17:33:06 -07003074int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3075 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003076 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003077 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003078 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003079 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003080 return 0;
3081 } else {
3082 return -1;
3083 }
3084}
3085
Thomas Daede497d1952017-08-08 17:33:06 -07003086int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3087 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003088 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003089 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003090 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003091 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003092 return 0;
3093 } else {
3094 return -1;
3095 }
3096}
3097
3098int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003099 cpi->ext_refresh_frame_context = update;
3100 cpi->ext_refresh_frame_context_pending = 1;
3101 return 0;
3102}
3103
3104#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3105// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3106// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3107// not denoise the UV channels at this time. If ever we implement UV channel
3108// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003109void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003110 uint8_t *src = s->y_buffer;
3111 int h = s->y_height;
3112
3113 do {
3114 fwrite(src, s->y_width, 1, f);
3115 src += s->y_stride;
3116 } while (--h);
3117
3118 src = s->u_buffer;
3119 h = s->uv_height;
3120
3121 do {
3122 fwrite(src, s->uv_width, 1, f);
3123 src += s->uv_stride;
3124 } while (--h);
3125
3126 src = s->v_buffer;
3127 h = s->uv_height;
3128
3129 do {
3130 fwrite(src, s->uv_width, 1, f);
3131 src += s->uv_stride;
3132 } while (--h);
3133}
3134#endif
3135
Yaowu Xuf883b422016-08-30 14:01:10 -07003136static void check_show_existing_frame(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003137 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
Yaowu Xuf883b422016-08-30 14:01:10 -07003138 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003139 const FRAME_UPDATE_TYPE next_frame_update_type =
3140 gf_group->update_type[gf_group->index];
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003141#if USE_SYMM_MULTI_LAYER
3142 const int which_arf = (cpi->new_bwdref_update_rule == 1)
3143 ? gf_group->arf_update_idx[gf_group->index] > 0
3144 : gf_group->arf_update_idx[gf_group->index];
3145#else
Yaowu Xuc27fc142016-08-22 16:08:15 -07003146 const int which_arf = gf_group->arf_update_idx[gf_group->index];
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003147#endif
Zoe Liu5fca7242016-10-10 17:18:57 -07003148
3149 if (cm->show_existing_frame == 1) {
3150 cm->show_existing_frame = 0;
3151 } else if (cpi->rc.is_last_bipred_frame) {
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003152#if USE_SYMM_MULTI_LAYER
3153 // NOTE: When new structure is used, every bwdref will have one overlay
3154 // frame. Therefore, there is no need to find out which frame to
3155 // show in advance.
3156 if (cpi->new_bwdref_update_rule == 0) {
3157#endif
3158 // NOTE: If the current frame is a last bi-predictive frame, it is
3159 // needed next to show the BWDREF_FRAME, which is pointed by
3160 // the last_fb_idxes[0] after reference frame buffer update
3161 cpi->rc.is_last_bipred_frame = 0;
3162 cm->show_existing_frame = 1;
3163 cpi->existing_fb_idx_to_show = cpi->ref_fb_idx[0];
3164#if USE_SYMM_MULTI_LAYER
3165 }
3166#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003167 } else if (cpi->is_arf_filter_off[which_arf] &&
3168 (next_frame_update_type == OVERLAY_UPDATE ||
3169 next_frame_update_type == INTNL_OVERLAY_UPDATE)) {
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003170#if USE_SYMM_MULTI_LAYER
3171 const int bwdref_to_show =
3172 (cpi->new_bwdref_update_rule == 1) ? BWDREF_FRAME : ALTREF2_FRAME;
3173#else
3174 const int bwdref_to_show = ALTREF2_FRAME;
3175#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003176 // Other parameters related to OVERLAY_UPDATE will be taken care of
Yaowu Xuf883b422016-08-30 14:01:10 -07003177 // in av1_rc_get_second_pass_params(cpi)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003178 cm->show_existing_frame = 1;
3179 cpi->rc.is_src_frame_alt_ref = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07003180 cpi->existing_fb_idx_to_show = (next_frame_update_type == OVERLAY_UPDATE)
Zoe Liu5989a722018-03-29 13:37:36 -07003181 ? cpi->ref_fb_idx[ALTREF_FRAME - 1]
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003182 : cpi->ref_fb_idx[bwdref_to_show - 1];
3183#if USE_SYMM_MULTI_LAYER
3184 if (cpi->new_bwdref_update_rule == 0)
3185#endif
3186 cpi->is_arf_filter_off[which_arf] = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003187 }
3188 cpi->rc.is_src_frame_ext_arf = 0;
3189}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003190
3191#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003192void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003193 uint8_t *src = s->y_buffer;
3194 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003195 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003196 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3197 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3198
3199 do {
3200 fwrite(src16, s->y_width, 2, yuv_rec_file);
3201 src16 += s->y_stride;
3202 } while (--h);
3203
3204 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3205 h = s->uv_height;
3206
3207 do {
3208 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3209 src16 += s->uv_stride;
3210 } while (--h);
3211
3212 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3213 h = s->uv_height;
3214
3215 do {
3216 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3217 src16 += s->uv_stride;
3218 } while (--h);
3219
3220 fflush(yuv_rec_file);
3221 return;
3222 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003223
3224 do {
3225 fwrite(src, s->y_width, 1, yuv_rec_file);
3226 src += s->y_stride;
3227 } while (--h);
3228
3229 src = s->u_buffer;
3230 h = s->uv_height;
3231
3232 do {
3233 fwrite(src, s->uv_width, 1, yuv_rec_file);
3234 src += s->uv_stride;
3235 } while (--h);
3236
3237 src = s->v_buffer;
3238 h = s->uv_height;
3239
3240 do {
3241 fwrite(src, s->uv_width, 1, yuv_rec_file);
3242 src += s->uv_stride;
3243 } while (--h);
3244
3245 fflush(yuv_rec_file);
3246}
3247#endif // OUTPUT_YUV_REC
3248
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003249#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003250static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3251 int i;
3252 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003253 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003254 AV1_COMMON *const cm = &cpi->common;
3255 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3256 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003257 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003258 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003259 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003260 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003261 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003262 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003263 // TODO(sarahparker): The earlier condition for recoding here was:
3264 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3265 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003266 }
3267 }
3268 return recode;
3269}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003270
Yaowu Xuc27fc142016-08-22 16:08:15 -07003271// Function to test for conditions that indicate we should loop
3272// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003273static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3274 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003275 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003276 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003277 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3278 int force_recode = 0;
3279
3280 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3281 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3282 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003283 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3284 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3285 (rc->projected_frame_size < low_limit && q > minq)) {
3286 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003287 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003288 // Deal with frame undershoot and whether or not we are
3289 // below the automatically set cq level.
3290 if (q > oxcf->cq_level &&
3291 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3292 force_recode = 1;
3293 }
3294 }
3295 }
3296 return force_recode;
3297}
3298
Yaowu Xuc27fc142016-08-22 16:08:15 -07003299#define DUMP_REF_FRAME_IMAGES 0
3300
3301#if DUMP_REF_FRAME_IMAGES == 1
Yaowu Xuf883b422016-08-30 14:01:10 -07003302static int dump_one_image(AV1_COMMON *cm,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003303 const YV12_BUFFER_CONFIG *const ref_buf,
3304 char *file_name) {
3305 int h;
3306 FILE *f_ref = NULL;
3307
3308 if (ref_buf == NULL) {
3309 printf("Frame data buffer is NULL.\n");
Yaowu Xuf883b422016-08-30 14:01:10 -07003310 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003311 }
3312
3313 if ((f_ref = fopen(file_name, "wb")) == NULL) {
3314 printf("Unable to open file %s to write.\n", file_name);
Yaowu Xuf883b422016-08-30 14:01:10 -07003315 return AOM_CODEC_MEM_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003316 }
3317
3318 // --- Y ---
3319 for (h = 0; h < cm->height; ++h) {
3320 fwrite(&ref_buf->y_buffer[h * ref_buf->y_stride], 1, cm->width, f_ref);
3321 }
3322 // --- U ---
3323 for (h = 0; h < (cm->height >> 1); ++h) {
3324 fwrite(&ref_buf->u_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3325 f_ref);
3326 }
3327 // --- V ---
3328 for (h = 0; h < (cm->height >> 1); ++h) {
3329 fwrite(&ref_buf->v_buffer[h * ref_buf->uv_stride], 1, (cm->width >> 1),
3330 f_ref);
3331 }
3332
3333 fclose(f_ref);
3334
Yaowu Xuf883b422016-08-30 14:01:10 -07003335 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003336}
3337
Yaowu Xuf883b422016-08-30 14:01:10 -07003338static void dump_ref_frame_images(AV1_COMP *cpi) {
3339 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003340 MV_REFERENCE_FRAME ref_frame;
3341
3342 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3343 char file_name[256] = "";
3344 snprintf(file_name, sizeof(file_name), "/tmp/enc_F%d_ref_%d.yuv",
3345 cm->current_video_frame, ref_frame);
3346 dump_one_image(cm, get_ref_frame_buffer(cpi, ref_frame), file_name);
3347 }
3348}
3349#endif // DUMP_REF_FRAME_IMAGES == 1
3350
Yaowu Xuc27fc142016-08-22 16:08:15 -07003351// This function is used to shift the virtual indices of last reference frames
3352// as follows:
3353// LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3354// when the LAST_FRAME is updated.
Yaowu Xuf883b422016-08-30 14:01:10 -07003355static INLINE void shift_last_ref_frames(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003356 // TODO(isbs): shift the scaled indices as well
Yaowu Xuc27fc142016-08-22 16:08:15 -07003357 int ref_frame;
3358 for (ref_frame = LAST_REF_FRAMES - 1; ref_frame > 0; --ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003359 cpi->ref_fb_idx[ref_frame] = cpi->ref_fb_idx[ref_frame - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003360
3361 // [0] is allocated to the current coded frame. The statistics for the
Zoe Liuf0e46692016-10-12 12:31:43 -07003362 // reference frames start at [LAST_FRAME], i.e. [1].
Yaowu Xuc27fc142016-08-22 16:08:15 -07003363 if (!cpi->rc.is_src_frame_alt_ref) {
Zoe Liuf0e46692016-10-12 12:31:43 -07003364 memcpy(cpi->interp_filter_selected[ref_frame + LAST_FRAME],
3365 cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME],
3366 sizeof(cpi->interp_filter_selected[ref_frame - 1 + LAST_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003367 }
3368 }
3369}
Yaowu Xuc27fc142016-08-22 16:08:15 -07003370
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003371#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003372// This function is used to shift the virtual indices of bwd reference
3373// frames as follows:
3374// BWD_REF -> ALT2_REF -> EXT_REF
3375// to clear a space to store the closest bwdref
3376static INLINE void rshift_bwd_ref_frames(AV1_COMP *cpi) {
3377 // TODO(isbs): shift the scaled indices as well
3378 static const int ordered_bwd[3] = { BWDREF_FRAME - 1, ALTREF2_FRAME - 1,
3379 EXTREF_FRAME - 1 };
3380
3381 for (int i = 2; i > 0; --i) {
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003382 // [0] is allocated to the current coded frame, i.e. bwdref
3383 memcpy(
3384 cpi->interp_filter_selected[ordered_bwd[i] + LAST_FRAME],
3385 cpi->interp_filter_selected[ordered_bwd[i - 1] + LAST_FRAME],
3386 sizeof(cpi->interp_filter_selected[ordered_bwd[i - 1] + LAST_FRAME]));
SmilingWolfdad22822018-09-12 15:43:43 -07003387
3388 cpi->ref_fb_idx[ordered_bwd[i]] = cpi->ref_fb_idx[ordered_bwd[i - 1]];
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003389 }
3390}
3391
3392// This function is used to shift the virtual indices of bwd reference
3393// frames as follows:
3394// BWD_REF <- ALT2_REF <- EXT_REF
3395// to update the bwd reference frame for coding the next frame.
3396static INLINE void lshift_bwd_ref_frames(AV1_COMP *cpi) {
3397 // TODO(isbs): shift the scaled indices as well
3398 static const int ordered_bwd[3] = { BWDREF_FRAME - 1, ALTREF2_FRAME - 1,
3399 EXTREF_FRAME - 1 };
3400
3401 for (int i = 0; i < 2; ++i) {
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003402 // [0] is allocated to the current coded frame, i.e. bwdref
3403 memcpy(
3404 cpi->interp_filter_selected[ordered_bwd[i] + LAST_FRAME],
3405 cpi->interp_filter_selected[ordered_bwd[i + 1] + LAST_FRAME],
3406 sizeof(cpi->interp_filter_selected[ordered_bwd[i + 1] + LAST_FRAME]));
SmilingWolfdad22822018-09-12 15:43:43 -07003407
3408 cpi->ref_fb_idx[ordered_bwd[i]] = cpi->ref_fb_idx[ordered_bwd[i + 1]];
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003409 }
3410}
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003411#endif // USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003412
Zoe Liu8dd1c982017-09-11 10:14:35 -07003413static void update_reference_frames(AV1_COMP *cpi) {
3414 AV1_COMMON *const cm = &cpi->common;
3415
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
3417 // for the purpose to verify no mismatch between encoder and decoder.
3418 if (cm->show_frame) cpi->last_show_frame_buf_idx = cm->new_fb_idx;
3419
Sarah Parker33005522018-07-27 14:46:25 -07003420 // In the case of show_existing frame, we will not send fresh flag
3421 // to decoder. Any change in the reference frame buffer can be done by
3422 // switching the virtual indices.
3423 if (cm->show_existing_frame) {
3424 cpi->refresh_last_frame = 0;
3425 cpi->refresh_golden_frame = 0;
3426 cpi->refresh_bwd_ref_frame = 0;
3427 cpi->refresh_alt2_ref_frame = 0;
3428 cpi->refresh_alt_ref_frame = 0;
3429
3430 cpi->rc.is_bwd_ref_frame = 0;
3431 cpi->rc.is_last_bipred_frame = 0;
3432 cpi->rc.is_bipred_frame = 0;
3433 }
3434
Zoe Liu8dd1c982017-09-11 10:14:35 -07003435 BufferPool *const pool = cm->buffer_pool;
Zoe Liubcef1e62018-04-06 20:56:11 -07003436
Yaowu Xuc27fc142016-08-22 16:08:15 -07003437 // At this point the new frame has been encoded.
3438 // If any buffer copy / swapping is signaled it should be done here.
Zoe Liubcef1e62018-04-06 20:56:11 -07003439
Sarah Parkerb9041612018-05-22 19:06:47 -07003440 // Only update all of the reference buffers if a KEY_FRAME is also a
3441 // show_frame. This ensures a fwd keyframe does not update all of the buffers
3442 if ((cm->frame_type == KEY_FRAME && cm->show_frame) || frame_is_sframe(cm)) {
Zoe Liubcef1e62018-04-06 20:56:11 -07003443 for (int ref_frame = 0; ref_frame < REF_FRAMES; ++ref_frame) {
3444 ref_cnt_fb(pool->frame_bufs,
3445 &cm->ref_frame_map[cpi->ref_fb_idx[ref_frame]],
3446 cm->new_fb_idx);
3447 }
3448 return;
3449 }
3450
3451 if (av1_preserve_existing_gf(cpi)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003452 // We have decided to preserve the previously existing golden frame as our
3453 // new ARF frame. However, in the short term in function
Yaowu Xuf883b422016-08-30 14:01:10 -07003454 // av1_bitstream.c::get_refresh_mask() we left it in the GF slot and, if
Yaowu Xuc27fc142016-08-22 16:08:15 -07003455 // we're updating the GF with the current decoded frame, we save it to the
3456 // ARF slot instead.
3457 // We now have to update the ARF with the current frame and swap gld_fb_idx
3458 // and alt_fb_idx so that, overall, we've stored the old GF in the new ARF
3459 // slot and, if we're updating the GF, the current frame becomes the new GF.
3460 int tmp;
3461
Wei-Ting Lina8c02452018-08-13 11:04:06 -07003462 // ARF in general is a better reference than overlay. We shouldkeep ARF as
3463 // reference instead of replacing it with overlay.
3464
3465 if (!cpi->preserve_arf_as_gld) {
3466 ref_cnt_fb(pool->frame_bufs,
3467 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF_FRAME - 1]],
3468 cm->new_fb_idx);
3469 }
3470
Zoe Liu5989a722018-03-29 13:37:36 -07003471 tmp = cpi->ref_fb_idx[ALTREF_FRAME - 1];
3472 cpi->ref_fb_idx[ALTREF_FRAME - 1] = cpi->ref_fb_idx[GOLDEN_FRAME - 1];
3473 cpi->ref_fb_idx[GOLDEN_FRAME - 1] = tmp;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003474
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003475 // TODO(zoeliu): Do we need to copy cpi->interp_filter_selected[0] over to
3476 // cpi->interp_filter_selected[GOLDEN_FRAME]?
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477 } else if (cpi->rc.is_src_frame_ext_arf && cm->show_existing_frame) {
Wei-Ting Linb72453f2018-06-26 14:05:38 -07003478#if CONFIG_DEBUG
3479 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
3480 assert(gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE);
3481#endif
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003482#if USE_SYMM_MULTI_LAYER
3483 const int bwdref_to_show =
3484 (cpi->new_bwdref_update_rule == 1) ? BWDREF_FRAME : ALTREF2_FRAME;
3485#else
3486 const int bwdref_to_show = ALTREF2_FRAME;
3487#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488 // Deal with the special case for showing existing internal ALTREF_FRAME
3489 // Refresh the LAST_FRAME with the ALTREF_FRAME and retire the LAST3_FRAME
3490 // by updating the virtual indices.
Zoe Liu5989a722018-03-29 13:37:36 -07003491 const int tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003492 shift_last_ref_frames(cpi);
Zoe Liue9b15e22017-07-19 15:53:01 -07003493
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003494 cpi->ref_fb_idx[LAST_FRAME - 1] = cpi->ref_fb_idx[bwdref_to_show - 1];
Zoe Liue9b15e22017-07-19 15:53:01 -07003495
3496 memcpy(cpi->interp_filter_selected[LAST_FRAME],
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003497 cpi->interp_filter_selected[bwdref_to_show],
3498 sizeof(cpi->interp_filter_selected[bwdref_to_show]));
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003499#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003500 if (cpi->new_bwdref_update_rule == 1) {
3501 lshift_bwd_ref_frames(cpi);
3502 // pass outdated forward reference frame (previous LAST3) to the
3503 // spared space
3504 cpi->ref_fb_idx[EXTREF_FRAME - 1] = tmp;
3505 } else {
3506#endif
3507 cpi->ref_fb_idx[bwdref_to_show - 1] = tmp;
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003508#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003509 }
3510#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003511 } else { /* For non key/golden frames */
Zoe Liue9b15e22017-07-19 15:53:01 -07003512 // === ALTREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003513 if (cpi->refresh_alt_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003514 int arf_idx = cpi->ref_fb_idx[ALTREF_FRAME - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003515 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[arf_idx], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003516
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07003517 memcpy(cpi->interp_filter_selected[ALTREF_FRAME],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003518 cpi->interp_filter_selected[0],
3519 sizeof(cpi->interp_filter_selected[0]));
3520 }
3521
Zoe Liue9b15e22017-07-19 15:53:01 -07003522 // === GOLDEN_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003523 if (cpi->refresh_golden_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003524 ref_cnt_fb(pool->frame_bufs,
3525 &cm->ref_frame_map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]],
Yaowu Xuc27fc142016-08-22 16:08:15 -07003526 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003527
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003528 memcpy(cpi->interp_filter_selected[GOLDEN_FRAME],
3529 cpi->interp_filter_selected[0],
3530 sizeof(cpi->interp_filter_selected[0]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003531 }
3532
Zoe Liue9b15e22017-07-19 15:53:01 -07003533 // === BWDREF_FRAME ===
Yaowu Xuc27fc142016-08-22 16:08:15 -07003534 if (cpi->refresh_bwd_ref_frame) {
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003535#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003536 if (cpi->new_bwdref_update_rule) {
3537 // We shift the backward reference frame as follows:
3538 // BWDREF -> ALTREF2 -> EXTREF
3539 // and assign the newly coded frame to BWDREF so that it always
3540 // keeps the nearest future frame
3541 int tmp = cpi->ref_fb_idx[EXTREF_FRAME - 1];
3542 ref_cnt_fb(pool->frame_bufs, &cm->ref_frame_map[tmp], cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003543
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003544 rshift_bwd_ref_frames(cpi);
3545 cpi->ref_fb_idx[BWDREF_FRAME - 1] = tmp;
3546 } else {
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003547#endif // USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003548 ref_cnt_fb(pool->frame_bufs,
3549 &cm->ref_frame_map[cpi->ref_fb_idx[BWDREF_FRAME - 1]],
3550 cm->new_fb_idx);
Wei-Ting Lin240d9b42018-07-12 11:48:02 -07003551#if USE_SYMM_MULTI_LAYER
Wei-Ting Lincc75ca72018-07-10 15:36:32 -07003552 }
3553#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003554 memcpy(cpi->interp_filter_selected[BWDREF_FRAME],
3555 cpi->interp_filter_selected[0],
3556 sizeof(cpi->interp_filter_selected[0]));
3557 }
Zoe Liue9b15e22017-07-19 15:53:01 -07003558
Zoe Liue9b15e22017-07-19 15:53:01 -07003559 // === ALTREF2_FRAME ===
3560 if (cpi->refresh_alt2_ref_frame) {
Zoe Liu5989a722018-03-29 13:37:36 -07003561 ref_cnt_fb(pool->frame_bufs,
3562 &cm->ref_frame_map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]],
Zoe Liue9b15e22017-07-19 15:53:01 -07003563 cm->new_fb_idx);
3564
3565 memcpy(cpi->interp_filter_selected[ALTREF2_FRAME],
3566 cpi->interp_filter_selected[0],
3567 sizeof(cpi->interp_filter_selected[0]));
3568 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003569 }
3570
3571 if (cpi->refresh_last_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003572 // NOTE(zoeliu): We have two layers of mapping (1) from the per-frame
3573 // reference to the reference frame buffer virtual index; and then (2) from
3574 // the virtual index to the reference frame buffer physical index:
3575 //
3576 // LAST_FRAME, ..., LAST3_FRAME, ..., ALTREF_FRAME
3577 // | | |
3578 // v v v
Zoe Liu5989a722018-03-29 13:37:36 -07003579 // ref_fb_idx[0], ..., ref_fb_idx[2], ..., ref_fb_idx[ALTREF_FRAME-1]
Yaowu Xuc27fc142016-08-22 16:08:15 -07003580 // | | |
3581 // v v v
3582 // ref_frame_map[], ..., ref_frame_map[], ..., ref_frame_map[]
3583 //
3584 // When refresh_last_frame is set, it is intended to retire LAST3_FRAME,
3585 // have the other 2 LAST reference frames shifted as follows:
3586 // LAST_FRAME -> LAST2_FRAME -> LAST3_FRAME
3587 // , and then have LAST_FRAME refreshed by the newly coded frame.
3588 //
3589 // To fulfill it, the decoder will be notified to execute following 2 steps:
3590 //
3591 // (a) To change ref_frame_map[] and have the virtual index of LAST3_FRAME
3592 // to point to the newly coded frame, i.e.
3593 // ref_frame_map[lst_fb_idexes[2]] => new_fb_idx;
3594 //
3595 // (b) To change the 1st layer mapping to have LAST_FRAME mapped to the
3596 // original virtual index of LAST3_FRAME and have the other mappings
3597 // shifted as follows:
3598 // LAST_FRAME, LAST2_FRAME, LAST3_FRAME
3599 // | | |
3600 // v v v
Zoe Liu5989a722018-03-29 13:37:36 -07003601 // ref_fb_idx[2], ref_fb_idx[0], ref_fb_idx[1]
Zoe Liubcef1e62018-04-06 20:56:11 -07003602 int tmp;
Zoe Liu5fca7242016-10-10 17:18:57 -07003603
Zoe Liubcef1e62018-04-06 20:56:11 -07003604 ref_cnt_fb(pool->frame_bufs,
3605 &cm->ref_frame_map[cpi->ref_fb_idx[LAST_REF_FRAMES - 1]],
3606 cm->new_fb_idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003607
Zoe Liubcef1e62018-04-06 20:56:11 -07003608 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003609
Zoe Liubcef1e62018-04-06 20:56:11 -07003610 shift_last_ref_frames(cpi);
3611 cpi->ref_fb_idx[0] = tmp;
3612
3613 assert(cm->show_existing_frame == 0);
3614 memcpy(cpi->interp_filter_selected[LAST_FRAME],
3615 cpi->interp_filter_selected[0],
3616 sizeof(cpi->interp_filter_selected[0]));
3617
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003618 // If the new structure is used, we will always have overlay frames coupled
3619 // with bwdref frames. Therefore, we won't have to perform this update
3620 // in advance (we do this update when the overlay frame shows up).
3621#if USE_SYMM_MULTI_LAYER
3622 if (cpi->new_bwdref_update_rule == 0 && cpi->rc.is_last_bipred_frame) {
3623#else
Zoe Liubcef1e62018-04-06 20:56:11 -07003624 if (cpi->rc.is_last_bipred_frame) {
Wei-Ting Linbafa11c2018-07-10 13:20:59 -07003625#endif
Zoe Liubcef1e62018-04-06 20:56:11 -07003626 // Refresh the LAST_FRAME with the BWDREF_FRAME and retire the
3627 // LAST3_FRAME by updating the virtual indices.
3628 //
3629 // NOTE: The source frame for BWDREF does not have a holding position as
3630 // the OVERLAY frame for ALTREF's. Hence, to resolve the reference
3631 // virtual index reshuffling for BWDREF, the encoder always
3632 // specifies a LAST_BIPRED right before BWDREF and completes the
3633 // reshuffling job accordingly.
Zoe Liu5989a722018-03-29 13:37:36 -07003634 tmp = cpi->ref_fb_idx[LAST_REF_FRAMES - 1];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003635
3636 shift_last_ref_frames(cpi);
Zoe Liubcef1e62018-04-06 20:56:11 -07003637 cpi->ref_fb_idx[0] = cpi->ref_fb_idx[BWDREF_FRAME - 1];
3638 cpi->ref_fb_idx[BWDREF_FRAME - 1] = tmp;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003639
Zoe Liuf0e46692016-10-12 12:31:43 -07003640 memcpy(cpi->interp_filter_selected[LAST_FRAME],
Zoe Liubcef1e62018-04-06 20:56:11 -07003641 cpi->interp_filter_selected[BWDREF_FRAME],
3642 sizeof(cpi->interp_filter_selected[BWDREF_FRAME]));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003643 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003644 }
3645
3646#if DUMP_REF_FRAME_IMAGES == 1
3647 // Dump out all reference frame images.
3648 dump_ref_frame_images(cpi);
3649#endif // DUMP_REF_FRAME_IMAGES
3650}
3651
Yaowu Xuf883b422016-08-30 14:01:10 -07003652static INLINE void alloc_frame_mvs(AV1_COMMON *const cm, int buffer_idx) {
Debargha Mukherjee887069f2017-06-16 18:54:36 -07003653 assert(buffer_idx != INVALID_IDX);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003654 RefCntBuffer *const new_fb_ptr = &cm->buffer_pool->frame_bufs[buffer_idx];
Rupert Swarbrick1f990a62017-07-11 11:09:33 +01003655 ensure_mv_buffer(new_fb_ptr, cm);
3656 new_fb_ptr->width = cm->width;
3657 new_fb_ptr->height = cm->height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003658}
3659
Cheng Chen46f30c72017-09-07 11:13:33 -07003660static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003661 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003662 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003663 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003664 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003665 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3666 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003667 };
3668
3669 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003670 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003671 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3672 BufferPool *const pool = cm->buffer_pool;
3673 const YV12_BUFFER_CONFIG *const ref =
3674 get_ref_frame_buffer(cpi, ref_frame);
3675
3676 if (ref == NULL) {
3677 cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3678 continue;
3679 }
3680
Yaowu Xuc27fc142016-08-22 16:08:15 -07003681 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
3682 RefCntBuffer *new_fb_ptr = NULL;
3683 int force_scaling = 0;
3684 int new_fb = cpi->scaled_ref_idx[ref_frame - 1];
3685 if (new_fb == INVALID_IDX) {
3686 new_fb = get_free_fb(cm);
3687 force_scaling = 1;
3688 }
3689 if (new_fb == INVALID_IDX) return;
3690 new_fb_ptr = &pool->frame_bufs[new_fb];
3691 if (force_scaling || new_fb_ptr->buf.y_crop_width != cm->width ||
3692 new_fb_ptr->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003693 if (aom_realloc_frame_buffer(
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003694 &new_fb_ptr->buf, cm->width, cm->height,
3695 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
3696 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003697 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003698 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003699 "Failed to allocate frame buffer");
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003700 av1_resize_and_extend_frame(
3701 ref, &new_fb_ptr->buf, (int)cm->seq_params.bit_depth, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003702 cpi->scaled_ref_idx[ref_frame - 1] = new_fb;
3703 alloc_frame_mvs(cm, new_fb);
3704 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003705 } else {
3706 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3707 RefCntBuffer *const buf = &pool->frame_bufs[buf_idx];
3708 buf->buf.y_crop_width = ref->y_crop_width;
3709 buf->buf.y_crop_height = ref->y_crop_height;
3710 cpi->scaled_ref_idx[ref_frame - 1] = buf_idx;
3711 ++buf->ref_count;
3712 }
3713 } else {
3714 if (cpi->oxcf.pass != 0) cpi->scaled_ref_idx[ref_frame - 1] = INVALID_IDX;
3715 }
3716 }
3717}
3718
Yaowu Xuf883b422016-08-30 14:01:10 -07003719static void release_scaled_references(AV1_COMP *cpi) {
3720 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003721 int i;
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003722 // TODO(isbs): only refresh the necessary frames, rather than all of them
Zoe Liu27deb382018-03-27 15:13:56 -07003723 for (i = 0; i < REF_FRAMES; ++i) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003724 const int idx = cpi->scaled_ref_idx[i];
3725 RefCntBuffer *const buf =
3726 idx != INVALID_IDX ? &cm->buffer_pool->frame_bufs[idx] : NULL;
3727 if (buf != NULL) {
3728 --buf->ref_count;
3729 cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003730 }
3731 }
3732}
3733
Yaowu Xuf883b422016-08-30 14:01:10 -07003734static void set_mv_search_params(AV1_COMP *cpi) {
3735 const AV1_COMMON *const cm = &cpi->common;
3736 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003737
3738 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003739 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003740
3741 if (cpi->sf.mv.auto_mv_step_size) {
3742 if (frame_is_intra_only(cm)) {
3743 // Initialize max_mv_magnitude for use in the first INTER frame
3744 // after a key/intra-only frame.
3745 cpi->max_mv_magnitude = max_mv_def;
3746 } else {
3747 if (cm->show_frame) {
3748 // Allow mv_steps to correspond to twice the max mv magnitude found
3749 // in the previous frame, capped by the default max_mv_magnitude based
3750 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003751 cpi->mv_step_param = av1_init_search_range(
3752 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003753 }
3754 cpi->max_mv_magnitude = 0;
3755 }
3756 }
3757}
3758
Yaowu Xuf883b422016-08-30 14:01:10 -07003759static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003760 int i;
3761 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003762 cpi->common.global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003763 }
3764 cpi->global_motion_search_done = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07003765 av1_set_speed_features_framesize_independent(cpi);
3766 av1_set_rd_speed_thresholds(cpi);
3767 av1_set_rd_speed_thresholds_sub8x8(cpi);
Urvang Joshib55cb5e2018-09-12 14:50:21 -07003768 cpi->common.interp_filter = SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08003769 cpi->common.switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003770}
3771
Yaowu Xuf883b422016-08-30 14:01:10 -07003772static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003773 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003774 AV1_COMMON *const cm = &cpi->common;
3775 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003776
3777 // Setup variables that depend on the dimensions of the frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003778 av1_set_speed_features_framesize_dependent(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003779
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003780 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003781 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3782 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003783
James Zern01a9d702017-08-25 19:09:33 +00003784 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003785 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003786 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003787 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003788
3789 // Configure experimental use of segmentation for enhanced coding of
3790 // static regions if indicated.
3791 // Only allowed in the second pass of a two pass encode, as it requires
3792 // lagged coding, and if the relevant speed feature flag is set.
3793 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3794 configure_static_seg_features(cpi);
3795}
3796
Yaowu Xuf883b422016-08-30 14:01:10 -07003797static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003798 int y_stride = cpi->scaled_source.y_stride;
3799
3800 if (cpi->sf.mv.search_method == NSTEP) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003801 av1_init3smotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003802 } else if (cpi->sf.mv.search_method == DIAMOND) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003803 av1_init_dsmotion_compensation(&cpi->ss_cfg, y_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003804 }
3805}
3806
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003807#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003808static void set_restoration_unit_size(int width, int height, int sx, int sy,
3809 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003810 (void)width;
3811 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003812 (void)sx;
3813 (void)sy;
3814#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3815 int s = AOMMIN(sx, sy);
3816#else
3817 int s = 0;
3818#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3819
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003820 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003821 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003822 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003823 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003824 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3825 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003826}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003827
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303828static void init_ref_frame_bufs(AV1_COMP *cpi) {
3829 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07003830 int i;
3831 BufferPool *const pool = cm->buffer_pool;
3832 cm->new_fb_idx = INVALID_IDX;
3833 for (i = 0; i < REF_FRAMES; ++i) {
3834 cm->ref_frame_map[i] = INVALID_IDX;
3835 pool->frame_bufs[i].ref_count = 0;
3836 }
RogerZhou86902d02018-02-28 15:29:16 -08003837 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003838 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303839 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08003840 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003841 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003842}
3843
Yaowu Xud3e7c682017-12-21 14:08:25 -08003844static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003845 int subsampling_x, int subsampling_y) {
3846 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003847 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07003848
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003849 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3850 seq_params->subsampling_x != subsampling_x ||
3851 seq_params->subsampling_y != subsampling_y) {
3852 seq_params->subsampling_x = subsampling_x;
3853 seq_params->subsampling_y = subsampling_y;
3854 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003855
3856 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303857 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07003858 alloc_util_frame_buffers(cpi);
3859
3860 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3861
3862 cpi->initial_width = cm->width;
3863 cpi->initial_height = cm->height;
3864 cpi->initial_mbs = cm->MBs;
3865 }
3866}
3867
3868// Returns 1 if the assigned width or height was <= 0.
3869static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3870 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003871 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003872 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3873 cm->seq_params.subsampling_x,
3874 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003875
3876 if (width <= 0 || height <= 0) return 1;
3877
3878 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003879 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003880
3881 if (cpi->initial_width && cpi->initial_height &&
3882 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3883 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003884 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003885 alloc_compressor_data(cpi);
3886 realloc_segmentation_maps(cpi);
3887 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003888 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003889 update_frame_size(cpi);
3890
3891 return 0;
3892}
3893
Fergus Simpsonbc189932017-05-16 17:02:39 -07003894static void set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003895 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003896 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003897 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003898 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003899 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003900
Fergus Simpsonbc189932017-05-16 17:02:39 -07003901 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003902 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003903 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003904 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003905 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07003906 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003907 }
3908
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003909 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003910 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003911 }
3912
3913 alloc_frame_mvs(cm, cm->new_fb_idx);
3914
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303915 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05303916 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3917 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303918 cm->num_allocated_above_contexts < cm->tile_rows) {
3919 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3920 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3921 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3922 "Failed to allocate context buffers");
3923 }
3924
Yaowu Xuc27fc142016-08-22 16:08:15 -07003925 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003926 if (aom_realloc_frame_buffer(
3927 get_frame_new_buffer(cm), cm->width, cm->height,
3928 seq_params->subsampling_x, seq_params->subsampling_y,
3929 seq_params->use_highbitdepth, AOM_BORDER_IN_PIXELS,
3930 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003931 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003932 "Failed to allocate frame buffer");
3933
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003934 const int frame_width = cm->superres_upscaled_width;
3935 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003936 set_restoration_unit_size(frame_width, frame_height,
3937 seq_params->subsampling_x,
3938 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003939 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003940 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003941
3942 av1_alloc_restoration_buffers(cm);
Fergus Simpson9cd57cf2017-06-12 17:02:03 -07003943 alloc_util_frame_buffers(cpi); // TODO(afergs): Remove? Gets called anyways.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003944 init_motion_estimation(cpi);
3945
3946 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
3947 RefBuffer *const ref_buf = &cm->frame_refs[ref_frame - LAST_FRAME];
3948 const int buf_idx = get_ref_frame_buf_idx(cpi, ref_frame);
3949
3950 ref_buf->idx = buf_idx;
3951
3952 if (buf_idx != INVALID_IDX) {
3953 YV12_BUFFER_CONFIG *const buf = &cm->buffer_pool->frame_bufs[buf_idx].buf;
3954 ref_buf->buf = buf;
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003955 av1_setup_scale_factors_for_frame(&ref_buf->sf, buf->y_crop_width,
3956 buf->y_crop_height, cm->width,
3957 cm->height);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003958 if (av1_is_scaled(&ref_buf->sf))
3959 aom_extend_frame_borders(buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003960 } else {
3961 ref_buf->buf = NULL;
3962 }
3963 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003964
Hui Su5ebd8702018-01-08 18:09:20 -08003965 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003966 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003967
3968 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3969}
3970
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003971static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3972 // Choose an arbitrary random number
3973 static unsigned int seed = 56789;
3974 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003975 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3976 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003977
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07003978 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003979 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003980 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003981 case RESIZE_FIXED:
3982 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003983 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003984 else
Urvang Joshide71d142017-10-05 12:12:15 -07003985 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003986 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003987 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003988 default: assert(0);
3989 }
Urvang Joshide71d142017-10-05 12:12:15 -07003990 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003991}
3992
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003993static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
3994 // Choose an arbitrary random number
3995 static unsigned int seed = 34567;
3996 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003997 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3998 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07003999
4000 // Make sure that superres mode of the frame is consistent with the
4001 // sequence-level flag.
4002 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4003 cpi->common.seq_params.enable_superres));
4004 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4005 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004006
4007 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004008 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004009 case SUPERRES_FIXED:
4010 if (cpi->common.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004011 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004012 else
Urvang Joshide71d142017-10-05 12:12:15 -07004013 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004014 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004015 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004016 case SUPERRES_QTHRESH: {
4017 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
4018 const RATE_FACTOR_LEVEL rf_level = gf_group->rf_level[gf_group->index];
4019 const double rate_factor_delta = rate_factor_deltas[rf_level];
Urvang Joshi2c92b072018-03-19 17:23:31 -07004020 const int qthresh = (rate_factor_delta <= 1.0)
4021 ? oxcf->superres_qthresh
4022 : oxcf->superres_kf_qthresh;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004023 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi2c92b072018-03-19 17:23:31 -07004024 int bottom_index, top_index;
4025 const int q = av1_rc_pick_q_and_bounds(
4026 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004027 if (q < qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004028 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004029 } else {
Urvang Joshi28983f72017-10-25 14:41:06 -07004030 const uint8_t min_denom = SCALE_NUMERATOR + 1;
4031 const uint8_t denom_step = (MAXQ - qthresh + 1) >> 3;
Imdad Sardharwallaf6154fd2018-03-02 16:53:41 +00004032
4033 if (q == qthresh) {
4034 new_denom = min_denom;
4035 } else if (denom_step == 0) {
4036 new_denom = SCALE_NUMERATOR << 1;
4037 } else {
4038 const uint8_t additional_denom = (q - qthresh) / denom_step;
4039 new_denom =
4040 AOMMIN(min_denom + additional_denom, SCALE_NUMERATOR << 1);
4041 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004042 }
4043 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004044 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004045 default: assert(0);
4046 }
Urvang Joshide71d142017-10-05 12:12:15 -07004047 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004048}
4049
Urvang Joshide71d142017-10-05 12:12:15 -07004050static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4051 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4052}
4053
4054static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004055 // Only need to check the width, as scaling is horizontal only.
4056 (void)oheight;
4057 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004058}
4059
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004060static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004061 SUPERRES_MODE superres_mode, int owidth,
4062 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004063 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004064 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004065 }
4066
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004067 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004068 int resize_denom =
4069 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4070 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4071
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004072 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004073 // Alter superres scale as needed to enforce conformity.
4074 rsz->superres_denom =
4075 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4076 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4077 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004078 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004079 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004080 // Alter resize scale as needed to enforce conformity.
4081 resize_denom =
4082 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004083 rsz->resize_width = owidth;
4084 rsz->resize_height = oheight;
4085 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004086 resize_denom);
4087 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4088 if (resize_denom > SCALE_NUMERATOR) {
4089 --resize_denom;
4090 rsz->resize_width = owidth;
4091 rsz->resize_height = oheight;
4092 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4093 resize_denom);
4094 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004095 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004096 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004097 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004098 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004099 if (resize_denom > rsz->superres_denom)
4100 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004101 else
Urvang Joshide71d142017-10-05 12:12:15 -07004102 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004103 rsz->resize_width = owidth;
4104 rsz->resize_height = oheight;
4105 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004106 resize_denom);
4107 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4108 (resize_denom > SCALE_NUMERATOR ||
4109 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004110 } else { // We are allowed to alter neither resize scale nor superres
4111 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004112 return 0;
4113 }
Urvang Joshide71d142017-10-05 12:12:15 -07004114 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004115}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004116
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004117// Calculates resize and superres params for next frame
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004118size_params_type av1_calculate_next_size_params(AV1_COMP *cpi) {
4119 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004120 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshide71d142017-10-05 12:12:15 -07004121 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004122 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004123 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4124 rsz.resize_width = cpi->resize_pending_width;
4125 rsz.resize_height = cpi->resize_pending_height;
4126 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4127 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004128 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004129 rsz.resize_width = cpi->oxcf.width;
4130 rsz.resize_height = cpi->oxcf.height;
4131 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004132 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004133 }
Urvang Joshide71d142017-10-05 12:12:15 -07004134 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004135 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4136 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004137 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004138 return rsz;
4139}
4140
4141static void setup_frame_size_from_params(AV1_COMP *cpi, size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004142 int encode_width = rsz->resize_width;
4143 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004144
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004145 AV1_COMMON *cm = &cpi->common;
4146 cm->superres_upscaled_width = encode_width;
4147 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004148 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004149 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4150 rsz->superres_denom);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004151 set_frame_size(cpi, encode_width, encode_height);
4152}
4153
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004154static void setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004155 size_params_type rsz = av1_calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004156 setup_frame_size_from_params(cpi, &rsz);
4157}
4158
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004159static void superres_post_encode(AV1_COMP *cpi) {
4160 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004161 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004162
Cheng Chen09c83a52018-06-05 12:27:36 -07004163 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004164
Urvang Joshid6b5d512018-03-20 13:34:38 -07004165 assert(cpi->oxcf.enable_superres);
4166 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004167 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004168
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004169 av1_superres_upscale(cm, NULL);
4170
4171 // If regular resizing is occurring the source will need to be downscaled to
4172 // match the upscaled superres resolution. Otherwise the original source is
4173 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004174 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004175 cpi->source = cpi->unscaled_source;
4176 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4177 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004178 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4179 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004180 // Do downscale. cm->(width|height) has been updated by
4181 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004182 if (aom_realloc_frame_buffer(
4183 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004184 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4185 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4186 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004187 aom_internal_error(
4188 &cm->error, AOM_CODEC_MEM_ERROR,
4189 "Failed to reallocate scaled source buffer for superres");
4190 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4191 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004192 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004193 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004194 cpi->source = &cpi->scaled_source;
4195 }
4196}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004197
4198static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004199 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004200 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004201
Urvang Joshic8b52d52018-03-23 13:16:51 -07004202 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4203 cm->coded_lossless && cm->all_lossless));
4204
4205 const int no_loopfilter = cm->coded_lossless || cm->large_scale_tile;
4206 const int no_cdef =
Debargha Mukherjee98a311c2018-03-25 16:33:11 -07004207 !cm->seq_params.enable_cdef || cm->coded_lossless || cm->large_scale_tile;
4208 const int no_restoration = !cm->seq_params.enable_restoration ||
4209 cm->all_lossless || cm->large_scale_tile;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004210
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004211 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004212
4213 if (no_loopfilter) {
Cheng Chen179479f2017-08-04 10:56:39 -07004214 lf->filter_level[0] = 0;
4215 lf->filter_level[1] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004216 } else {
4217 struct aom_usec_timer timer;
4218
4219 aom_clear_system_state();
4220
4221 aom_usec_timer_start(&timer);
4222
4223 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4224
4225 aom_usec_timer_mark(&timer);
4226 cpi->time_pick_lpf += aom_usec_timer_elapsed(&timer);
4227 }
4228
Debargha Mukherjee2382b142018-02-26 14:31:32 -08004229 if (lf->filter_level[0] || lf->filter_level[1]) {
Cheng Chen8ab1f442018-04-27 18:01:52 -07004230#if LOOP_FILTER_BITMASK
4231 av1_loop_filter_frame(cm->frame_to_show, cm, xd, 0, num_planes, 0);
4232#else
Deepa K G964e72e2018-05-16 16:56:01 +05304233 if (cpi->num_workers > 1)
4234 av1_loop_filter_frame_mt(cm->frame_to_show, cm, xd, 0, num_planes, 0,
4235 cpi->workers, cpi->num_workers,
4236 &cpi->lf_row_sync);
4237 else
4238 av1_loop_filter_frame(cm->frame_to_show, cm, xd, 0, num_planes, 0);
Cheng Chen8ab1f442018-04-27 18:01:52 -07004239#endif
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004240 }
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004241
Yaowu Xu35ee2342017-11-08 11:50:46 -08004242 if (!no_restoration)
4243 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004244
Yaowu Xu35ee2342017-11-08 11:50:46 -08004245 if (no_cdef) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004246 cm->cdef_bits = 0;
4247 cm->cdef_strengths[0] = 0;
4248 cm->nb_cdef_strengths = 1;
Yunqing Wangdad63d42017-12-08 12:45:07 -08004249 cm->cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004250 } else {
Steinar Midtskogen59782122017-07-20 08:49:43 +02004251 // Find CDEF parameters
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004252 av1_cdef_search(cm->frame_to_show, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004253 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004254
4255 // Apply the filter
4256 av1_cdef_frame(cm->frame_to_show, cm, xd);
4257 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004258
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004259 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004260
Yaowu Xu35ee2342017-11-08 11:50:46 -08004261 if (no_restoration) {
4262 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4263 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4264 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
4265 } else {
4266 av1_loop_restoration_save_boundary_lines(cm->frame_to_show, cm, 1);
4267 av1_pick_filter_restoration(cpi->source, cpi);
4268 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4269 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4270 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304271 if (cpi->num_workers > 1)
4272 av1_loop_restoration_filter_frame_mt(cm->frame_to_show, cm, 0,
4273 cpi->workers, cpi->num_workers,
4274 &cpi->lr_row_sync, &cpi->lr_ctxt);
4275 else
4276 av1_loop_restoration_filter_frame(cm->frame_to_show, cm, 0,
4277 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004278 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004279 }
Fergus Simpsonbc189932017-05-16 17:02:39 -07004280}
4281
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004282static int encode_without_recode_loop(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004283 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004284 int q = 0, bottom_index = 0, top_index = 0; // Dummy variables.
Yaowu Xuc27fc142016-08-22 16:08:15 -07004285
Yaowu Xuf883b422016-08-30 14:01:10 -07004286 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004287
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004288 set_size_independent_vars(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004289
Fergus Simpsonbc189932017-05-16 17:02:39 -07004290 setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004291
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004292 assert(cm->width == cpi->scaled_source.y_crop_width);
4293 assert(cm->height == cpi->scaled_source.y_crop_height);
Fergus Simpsonfecb2ab2017-04-30 15:49:57 -07004294
Yaowu Xuc27fc142016-08-22 16:08:15 -07004295 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4296
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004297 cpi->source =
4298 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
4299 if (cpi->unscaled_last_source != NULL)
4300 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4301 &cpi->scaled_last_source);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004302 cpi->source->buf_8bit_valid = 0;
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004303 if (frame_is_intra_only(cm) == 0) {
Cheng Chen46f30c72017-09-07 11:13:33 -07004304 scale_references(cpi);
Debargha Mukherjee887069f2017-06-16 18:54:36 -07004305 }
4306
Yaowu Xuf883b422016-08-30 14:01:10 -07004307 av1_set_quantizer(cm, q);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004308 setup_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004309 suppress_active_map(cpi);
hui sued5a30f2017-04-27 16:02:49 -07004310
Yaowu Xuc27fc142016-08-22 16:08:15 -07004311 // Variance adaptive and in frame q adjustment experiments are mutually
4312 // exclusive.
4313 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004314 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004315 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004316 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004317 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004318 av1_cyclic_refresh_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004319 }
4320 apply_active_map(cpi);
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004321 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004322 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004323 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004324 } else {
4325 calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004326 }
David Barkercab37552018-03-21 11:56:24 +00004327 } else {
4328 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004329 }
David Barkercab37552018-03-21 11:56:24 +00004330 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004331
4332 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004333 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004334
4335 // Update some stats from cyclic refresh, and check if we should not update
4336 // golden reference, for 1 pass CBR.
4337 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && cm->frame_type != KEY_FRAME &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004338 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR))
4339 av1_cyclic_refresh_check_golden_update(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004340
4341 // Update the skip mb flag probabilities based on the distribution
4342 // seen in the last encoder iteration.
4343 // update_base_skip_probs(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004344 aom_clear_system_state();
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004345 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004346}
4347
Tom Finegane4099e32018-01-23 12:01:51 -08004348static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004349 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004350 RATE_CONTROL *const rc = &cpi->rc;
4351 int bottom_index, top_index;
4352 int loop_count = 0;
4353 int loop_at_this_size = 0;
4354 int loop = 0;
4355 int overshoot_seen = 0;
4356 int undershoot_seen = 0;
4357 int frame_over_shoot_limit;
4358 int frame_under_shoot_limit;
4359 int q = 0, q_low = 0, q_high = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004360
4361 set_size_independent_vars(cpi);
4362
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004363 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004364
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004365 aom_clear_system_state();
4366 setup_frame_size(cpi);
4367 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4368
Yaowu Xuc27fc142016-08-22 16:08:15 -07004369 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004370 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004371
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004372 if (loop_count == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004373 // TODO(agrange) Scale cpi->max_mv_magnitude if frame-size has changed.
4374 set_mv_search_params(cpi);
4375
4376 // Reset the loop state for new frame size.
4377 overshoot_seen = 0;
4378 undershoot_seen = 0;
4379
Yaowu Xuc27fc142016-08-22 16:08:15 -07004380 q_low = bottom_index;
4381 q_high = top_index;
4382
4383 loop_at_this_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004384
Urvang Joshi2a74cf22017-12-18 17:21:41 -08004385 // Decide frame size bounds first time through.
Yaowu Xuf883b422016-08-30 14:01:10 -07004386 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4387 &frame_under_shoot_limit,
4388 &frame_over_shoot_limit);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004389 }
4390
Urvang Joshif1fa6862018-01-08 16:39:33 -08004391 // if frame was scaled calculate global_motion_search again if already
4392 // done
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004393 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done)
4394 if (cpi->source->y_crop_width != cm->width ||
4395 cpi->source->y_crop_height != cm->height)
4396 cpi->global_motion_search_done = 0;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004397 cpi->source =
4398 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004399 if (cpi->unscaled_last_source != NULL)
4400 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4401 &cpi->scaled_last_source);
4402
Yaowu Xuc27fc142016-08-22 16:08:15 -07004403 if (frame_is_intra_only(cm) == 0) {
4404 if (loop_count > 0) {
4405 release_scaled_references(cpi);
4406 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004407 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004408 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004409 av1_set_quantizer(cm, q);
Yaowu Xu2e587232018-05-10 09:30:08 -07004410 // printf("Frame %d/%d: q = %d, frame_type = %d\n", cm->current_video_frame,
4411 // cm->show_frame, q, cm->frame_type);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004412
4413 if (loop_count == 0) setup_frame(cpi);
4414
Yaowu Xuc27fc142016-08-22 16:08:15 -07004415 // Base q-index may have changed, so we need to assign proper default coef
4416 // probs before every iteration.
David Barkercc615a82018-03-19 14:38:51 +00004417 if (cm->primary_ref_frame == PRIMARY_REF_NONE ||
4418 cm->frame_refs[cm->primary_ref_frame].idx < 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004419 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004420 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004421 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004422
Yaowu Xuc27fc142016-08-22 16:08:15 -07004423 // Variance adaptive and in frame q adjustment experiments are mutually
4424 // exclusive.
4425 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004426 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004427 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004428 av1_setup_in_frame_q_adj(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004429 }
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004430 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004431 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004432 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004433 } else {
4434 calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004435 }
David Barkercab37552018-03-21 11:56:24 +00004436 } else {
4437 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004438 }
David Barkercab37552018-03-21 11:56:24 +00004439 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004440
4441 // transform / motion compensation build reconstruction frame
Jingning Han8f661602017-08-19 08:16:50 -07004442 save_coding_context(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004443 av1_encode_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004444
4445 // Update the skip mb flag probabilities based on the distribution
4446 // seen in the last encoder iteration.
4447 // update_base_skip_probs(cpi);
4448
Yaowu Xuf883b422016-08-30 14:01:10 -07004449 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004450
4451 // Dummy pack of the bitstream using up to date stats to get an
4452 // accurate estimate of output frame size to determine if we need
4453 // to recode.
4454 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004455 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004456
4457 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4458 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004459
4460 rc->projected_frame_size = (int)(*size) << 3;
4461 restore_coding_context(cpi);
4462
4463 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4464 }
4465
Yaowu Xuf883b422016-08-30 14:01:10 -07004466 if (cpi->oxcf.rc_mode == AOM_Q) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004467 loop = 0;
4468 } else {
4469 if ((cm->frame_type == KEY_FRAME) && rc->this_key_frame_forced &&
4470 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4471 int last_q = q;
4472 int64_t kf_err;
4473
4474 int64_t high_err_target = cpi->ambient_err;
4475 int64_t low_err_target = cpi->ambient_err >> 1;
4476
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004477 if (cm->seq_params.use_highbitdepth) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004478 kf_err = aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004479 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004480 kf_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004481 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004482 // Prevent possible divide by zero error below for perfect KF
4483 kf_err += !kf_err;
4484
4485 // The key frame is not good enough or we can afford
4486 // to make it better without undue risk of popping.
4487 if ((kf_err > high_err_target &&
4488 rc->projected_frame_size <= frame_over_shoot_limit) ||
4489 (kf_err > low_err_target &&
4490 rc->projected_frame_size <= frame_under_shoot_limit)) {
4491 // Lower q_high
4492 q_high = q > q_low ? q - 1 : q_low;
4493
4494 // Adjust Q
4495 q = (int)((q * high_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004496 q = AOMMIN(q, (q_high + q_low) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004497 } else if (kf_err < low_err_target &&
4498 rc->projected_frame_size >= frame_under_shoot_limit) {
4499 // The key frame is much better than the previous frame
4500 // Raise q_low
4501 q_low = q < q_high ? q + 1 : q_high;
4502
4503 // Adjust Q
4504 q = (int)((q * low_err_target) / kf_err);
Yaowu Xuf883b422016-08-30 14:01:10 -07004505 q = AOMMIN(q, (q_high + q_low + 1) >> 1);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004506 }
4507
4508 // Clamp Q to upper and lower limits:
4509 q = clamp(q, q_low, q_high);
4510
4511 loop = q != last_q;
4512 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4513 frame_under_shoot_limit, q,
Yaowu Xuf883b422016-08-30 14:01:10 -07004514 AOMMAX(q_high, top_index), bottom_index)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004515 // Is the projected frame size out of range and are we allowed
4516 // to attempt to recode.
4517 int last_q = q;
4518 int retries = 0;
4519
Yaowu Xuc27fc142016-08-22 16:08:15 -07004520 // Frame size out of permitted range:
4521 // Update correction factor & compute new Q to try...
Yaowu Xuc27fc142016-08-22 16:08:15 -07004522 // Frame is too large
4523 if (rc->projected_frame_size > rc->this_frame_target) {
4524 // Special case if the projected size is > the max allowed.
4525 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4526 q_high = rc->worst_quality;
4527
4528 // Raise Qlow as to at least the current value
4529 q_low = q < q_high ? q + 1 : q_high;
4530
4531 if (undershoot_seen || loop_at_this_size > 1) {
4532 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004533 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004534
4535 q = (q_high + q_low + 1) / 2;
4536 } else {
4537 // Update rate_correction_factor unless
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004538 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004539
Yaowu Xuf883b422016-08-30 14:01:10 -07004540 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004541 AOMMAX(q_high, top_index), cm->width,
4542 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004543
4544 while (q < q_low && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004545 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004546 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004547 AOMMAX(q_high, top_index), cm->width,
4548 cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004549 retries++;
4550 }
4551 }
4552
4553 overshoot_seen = 1;
4554 } else {
4555 // Frame is too small
4556 q_high = q > q_low ? q - 1 : q_low;
4557
4558 if (overshoot_seen || loop_at_this_size > 1) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004559 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004560 q = (q_high + q_low) / 2;
4561 } else {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004562 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004563 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004564 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004565 // Special case reset for qlow for constrained quality.
4566 // This should only trigger where there is very substantial
4567 // undershoot on a frame and the auto cq level is above
4568 // the user passsed in value.
Yaowu Xuf883b422016-08-30 14:01:10 -07004569 if (cpi->oxcf.rc_mode == AOM_CQ && q < q_low) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004570 q_low = q;
4571 }
4572
4573 while (q > q_high && retries < 10) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004574 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004575 q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004576 top_index, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004577 retries++;
4578 }
4579 }
4580
4581 undershoot_seen = 1;
4582 }
4583
4584 // Clamp Q to upper and lower limits:
4585 q = clamp(q, q_low, q_high);
4586
4587 loop = (q != last_q);
4588 } else {
4589 loop = 0;
4590 }
4591 }
4592
4593 // Special case for overlay frame.
4594 if (rc->is_src_frame_alt_ref &&
4595 rc->projected_frame_size < rc->max_frame_bandwidth)
4596 loop = 0;
4597
Debargha Mukherjee79853eb2018-09-13 17:38:02 -07004598 if (!cpi->sf.gm_disable_recode) {
4599 if (recode_loop_test_global_motion(cpi)) loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004600 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004601
Yaowu Xuc27fc142016-08-22 16:08:15 -07004602 if (loop) {
4603 ++loop_count;
4604 ++loop_at_this_size;
4605
4606#if CONFIG_INTERNAL_STATS
4607 ++cpi->tot_recode_hits;
4608#endif
4609 }
4610 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004611
4612 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004613}
4614
Yaowu Xuf883b422016-08-30 14:01:10 -07004615static int get_ref_frame_flags(const AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004616 const int *const map = cpi->common.ref_frame_map;
4617
Zoe Liu368bf162017-11-03 20:10:19 -07004618 // No.1 Priority: LAST_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004619 const int last2_is_last = map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[0]];
4620 const int last3_is_last = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[0]];
4621 const int gld_is_last =
4622 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4623 const int bwd_is_last =
4624 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4625 const int alt2_is_last =
4626 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
4627 const int alt_is_last =
4628 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]] == map[cpi->ref_fb_idx[0]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004629
Zoe Liu368bf162017-11-03 20:10:19 -07004630 // No.2 Priority: ALTREF_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004631 const int last2_is_alt =
4632 map[cpi->ref_fb_idx[1]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4633 const int last3_is_alt =
4634 map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4635 const int gld_is_alt = map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] ==
4636 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4637 const int bwd_is_alt = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4638 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
4639 const int alt2_is_alt = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4640 map[cpi->ref_fb_idx[ALTREF_FRAME - 1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004641
Zoe Liu368bf162017-11-03 20:10:19 -07004642 // No.3 Priority: LAST2_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004643 const int last3_is_last2 = map[cpi->ref_fb_idx[2]] == map[cpi->ref_fb_idx[1]];
4644 const int gld_is_last2 =
4645 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4646 const int bwd_is_last2 =
4647 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
4648 const int alt2_is_last2 =
4649 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[1]];
Yaowu Xuc27fc142016-08-22 16:08:15 -07004650
Zoe Liu368bf162017-11-03 20:10:19 -07004651 // No.4 Priority: LAST3_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004652 const int gld_is_last3 =
4653 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4654 const int bwd_is_last3 =
4655 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
4656 const int alt2_is_last3 =
4657 map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] == map[cpi->ref_fb_idx[2]];
Zoe Liu368bf162017-11-03 20:10:19 -07004658
4659 // No.5 Priority: GOLDEN_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004660 const int bwd_is_gld = map[cpi->ref_fb_idx[BWDREF_FRAME - 1]] ==
4661 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
4662 const int alt2_is_gld = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4663 map[cpi->ref_fb_idx[GOLDEN_FRAME - 1]];
Zoe Liu368bf162017-11-03 20:10:19 -07004664
4665 // No.6 Priority: BWDREF_FRAME
Zoe Liu5989a722018-03-29 13:37:36 -07004666 const int alt2_is_bwd = map[cpi->ref_fb_idx[ALTREF2_FRAME - 1]] ==
4667 map[cpi->ref_fb_idx[BWDREF_FRAME - 1]];
Zoe Liu368bf162017-11-03 20:10:19 -07004668
4669 // No.7 Priority: ALTREF2_FRAME
4670
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004671 // After av1_apply_encoding_flags() is called, cpi->ref_frame_flags might be
4672 // adjusted according to external encoder flags.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08004673 int flags = cpi->ext_ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004674
Yaowu Xuf883b422016-08-30 14:01:10 -07004675 if (cpi->rc.frames_till_gf_update_due == INT_MAX) flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004676
Yaowu Xuf883b422016-08-30 14:01:10 -07004677 if (alt_is_last) flags &= ~AOM_ALT_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004678
Yaowu Xuf883b422016-08-30 14:01:10 -07004679 if (last2_is_last || last2_is_alt) flags &= ~AOM_LAST2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004680
Zoe Liu368bf162017-11-03 20:10:19 -07004681 if (last3_is_last || last3_is_alt || last3_is_last2) flags &= ~AOM_LAST3_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004682
Zoe Liu368bf162017-11-03 20:10:19 -07004683 if (gld_is_last || gld_is_alt || gld_is_last2 || gld_is_last3)
4684 flags &= ~AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004685
Zoe Liu368bf162017-11-03 20:10:19 -07004686 if ((bwd_is_last || bwd_is_alt || bwd_is_last2 || bwd_is_last3 ||
4687 bwd_is_gld) &&
Yaowu Xuf883b422016-08-30 14:01:10 -07004688 (flags & AOM_BWD_FLAG))
4689 flags &= ~AOM_BWD_FLAG;
Zoe Liue9b15e22017-07-19 15:53:01 -07004690
Zoe Liu368bf162017-11-03 20:10:19 -07004691 if ((alt2_is_last || alt2_is_alt || alt2_is_last2 || alt2_is_last3 ||
4692 alt2_is_gld || alt2_is_bwd) &&
Zoe Liue9b15e22017-07-19 15:53:01 -07004693 (flags & AOM_ALT2_FLAG))
4694 flags &= ~AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004695
4696 return flags;
4697}
4698
Yaowu Xuf883b422016-08-30 14:01:10 -07004699static void set_ext_overrides(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004700 // Overrides the defaults with the externally supplied values with
Yaowu Xuf883b422016-08-30 14:01:10 -07004701 // av1_update_reference() and av1_update_entropy() calls
Yaowu Xuc27fc142016-08-22 16:08:15 -07004702 // Note: The overrides are valid only for the next frame passed
4703 // to encode_frame_to_data_rate() function
sarahparker9806fed2018-03-30 17:43:44 -07004704 if (cpi->ext_use_s_frame) cpi->common.frame_type = S_FRAME;
Sarah Parker50b6d6e2018-04-11 19:21:54 -07004705 cpi->common.force_primary_ref_none = cpi->ext_use_primary_ref_none;
Debargha Mukherjee52fb0472018-03-29 15:48:11 -07004706
Yaowu Xuc27fc142016-08-22 16:08:15 -07004707 if (cpi->ext_refresh_frame_context_pending) {
4708 cpi->common.refresh_frame_context = cpi->ext_refresh_frame_context;
4709 cpi->ext_refresh_frame_context_pending = 0;
4710 }
4711 if (cpi->ext_refresh_frame_flags_pending) {
4712 cpi->refresh_last_frame = cpi->ext_refresh_last_frame;
4713 cpi->refresh_golden_frame = cpi->ext_refresh_golden_frame;
4714 cpi->refresh_alt_ref_frame = cpi->ext_refresh_alt_ref_frame;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07004715 cpi->refresh_bwd_ref_frame = cpi->ext_refresh_bwd_ref_frame;
4716 cpi->refresh_alt2_ref_frame = cpi->ext_refresh_alt2_ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004717 cpi->ext_refresh_frame_flags_pending = 0;
4718 }
sarahparker21dbca42018-03-30 17:43:44 -07004719 cpi->common.allow_ref_frame_mvs = cpi->ext_use_ref_frame_mvs;
Sarah Parker14feea42018-07-06 16:41:41 -07004720 // A keyframe is already error resilient and keyframes with
4721 // error_resilient_mode interferes with the use of show_existing_frame
4722 // when forward reference keyframes are enabled.
4723 cpi->common.error_resilient_mode =
4724 cpi->ext_use_error_resilient && cpi->common.frame_type != KEY_FRAME;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004725}
4726
Yaowu Xuc27fc142016-08-22 16:08:15 -07004727#define DUMP_RECON_FRAMES 0
4728
4729#if DUMP_RECON_FRAMES == 1
4730// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004731static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4732 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004733 const YV12_BUFFER_CONFIG *recon_buf = cm->frame_to_show;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004734
Zoe Liub4f31032017-11-03 23:48:35 -07004735 if (recon_buf == NULL) {
4736 printf("Frame %d is not ready.\n", cm->current_video_frame);
4737 return;
4738 }
4739
Zoe Liu27deb382018-03-27 15:13:56 -07004740 static const int flag_list[REF_FRAMES] = { 0,
4741 AOM_LAST_FLAG,
4742 AOM_LAST2_FLAG,
4743 AOM_LAST3_FLAG,
4744 AOM_GOLD_FLAG,
4745 AOM_BWD_FLAG,
4746 AOM_ALT2_FLAG,
4747 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004748 printf(
4749 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4750 "show_existing_frame=%d) "
4751 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
4752 cm->current_video_frame, cm->frame_offset, cm->show_frame,
4753 cm->show_existing_frame);
4754 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4755 const int buf_idx = cm->frame_refs[ref_frame - LAST_FRAME].idx;
4756 const int ref_offset =
4757 (buf_idx >= 0)
4758 ? (int)cm->buffer_pool->frame_bufs[buf_idx].cur_frame_offset
4759 : -1;
Zoe Liuf452fdf2017-11-02 23:08:12 -07004760 printf(
4761 " %d(%c-%d-%4.2f)", ref_offset,
4762 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N',
4763 (buf_idx >= 0) ? (int)cpi->frame_rf_level[buf_idx] : -1,
4764 (buf_idx >= 0) ? rate_factor_deltas[cpi->frame_rf_level[buf_idx]] : -1);
Zoe Liub4f31032017-11-03 23:48:35 -07004765 }
4766 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004767
4768 if (!cm->show_frame) {
4769 printf("Frame %d is a no show frame, so no image dump.\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004770 cm->current_video_frame);
4771 return;
4772 }
4773
Zoe Liub4f31032017-11-03 23:48:35 -07004774 int h;
4775 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4776 FILE *f_recon = NULL;
4777
Yaowu Xuc27fc142016-08-22 16:08:15 -07004778 if (cm->current_video_frame == 0) {
4779 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4780 printf("Unable to open file %s to write.\n", file_name);
4781 return;
4782 }
4783 } else {
4784 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4785 printf("Unable to open file %s to append.\n", file_name);
4786 return;
4787 }
4788 }
4789 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004790 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4791 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
4792 "refresh_alt_ref_frame=%d, rf_level=%d, "
4793 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Yaowu Xuc27fc142016-08-22 16:08:15 -07004794 cm->current_video_frame, cpi->twopass.gf_group.index,
4795 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
Zoe Liuf40a9572017-10-13 12:37:19 -07004796 cm->frame_offset, cm->show_frame, cm->show_existing_frame,
4797 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
4798 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4799 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004800#if 0
4801 int ref_frame;
4802 printf("get_ref_frame_map_idx: [");
4803 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
4804 printf(" %d", get_ref_frame_map_idx(cpi, ref_frame));
4805 printf(" ]\n");
4806 printf("cm->new_fb_idx = %d\n", cm->new_fb_idx);
4807 printf("cm->ref_frame_map = [");
4808 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
4809 printf(" %d", cm->ref_frame_map[ref_frame - LAST_FRAME]);
4810 }
4811 printf(" ]\n");
4812#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004813
4814 // --- Y ---
4815 for (h = 0; h < cm->height; ++h) {
4816 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4817 f_recon);
4818 }
4819 // --- U ---
4820 for (h = 0; h < (cm->height >> 1); ++h) {
4821 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4822 f_recon);
4823 }
4824 // --- V ---
4825 for (h = 0; h < (cm->height >> 1); ++h) {
4826 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4827 f_recon);
4828 }
4829
4830 fclose(f_recon);
4831}
4832#endif // DUMP_RECON_FRAMES
4833
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07004834static INLINE int is_frame_droppable(AV1_COMP *cpi) {
4835 return !(cpi->refresh_alt_ref_frame || cpi->refresh_alt2_ref_frame ||
4836 cpi->refresh_bwd_ref_frame || cpi->refresh_golden_frame ||
4837 cpi->refresh_last_frame);
4838}
4839
Tom Finegane4099e32018-01-23 12:01:51 -08004840static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size, uint8_t *dest,
4841 int skip_adapt,
4842 unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004843 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004844 SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07004845 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004846 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01004847
Yaowu Xuc27fc142016-08-22 16:08:15 -07004848 set_ext_overrides(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07004849 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004850
Fangwen Fu8d164de2016-12-14 13:40:54 -08004851 // frame type has been decided outside of this function call
Zoe Liud78ce2d2018-02-24 05:50:57 -08004852 cm->cur_frame->intra_only = frame_is_intra_only(cm);
Zoe Liu2723a9d2018-02-22 20:17:00 -08004853 cm->cur_frame->frame_type = cm->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07004854
4855 // S_FRAMEs are always error resilient
sarahparker27d686a2018-03-30 17:43:44 -07004856 cm->error_resilient_mode |= frame_is_sframe(cm);
Yunqing Wang9612d552018-05-15 14:58:30 -07004857
4858 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
4859 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004860 if (cm->large_scale_tile) seq_params->frame_id_numbers_present_flag = 0;
Yunqing Wang9612d552018-05-15 14:58:30 -07004861
sarahparker21dbca42018-03-30 17:43:44 -07004862 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07004863 // cm->allow_ref_frame_mvs needs to be written into the frame header while
4864 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
4865 // separated from frame_might_allow_ref_frame_mvs().
4866 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
4867
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07004868 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07004869 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004870
Jingning Hand8a15a62017-10-30 10:53:42 -07004871 // Reset the frame packet stamp index.
Sarah Parkerb9041612018-05-22 19:06:47 -07004872 if (cm->frame_type == KEY_FRAME && cm->show_frame)
4873 cm->current_video_frame = 0;
Jingning Hand8a15a62017-10-30 10:53:42 -07004874
Yaowu Xuc27fc142016-08-22 16:08:15 -07004875 // NOTE:
4876 // (1) Move the setup of the ref_frame_flags upfront as it would be
4877 // determined by the current frame properties;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004878 // (2) The setup of the ref_frame_flags applies to both
4879 // show_existing_frame's
Yaowu Xuc27fc142016-08-22 16:08:15 -07004880 // and the other cases.
4881 if (cm->current_video_frame > 0)
4882 cpi->ref_frame_flags = get_ref_frame_flags(cpi);
4883
Sarah Parker33005522018-07-27 14:46:25 -07004884 if (encode_show_existing_frame(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004885 // NOTE(zoeliu): In BIDIR_PRED, the existing frame to show is the current
4886 // BWDREF_FRAME in the reference frame buffer.
Sarah Parkerb9041612018-05-22 19:06:47 -07004887 if (cm->frame_type == KEY_FRAME) {
4888 cm->reset_decoder_state = 1;
4889 } else {
4890 cm->frame_type = INTER_FRAME;
4891 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004892 cm->show_frame = 1;
4893 cpi->frame_flags = *frame_flags;
4894
Jingning Han8f661602017-08-19 08:16:50 -07004895 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07004896
Yaowu Xuc27fc142016-08-22 16:08:15 -07004897 // Build the bitstream
Tom Finegane4099e32018-01-23 12:01:51 -08004898 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
4899 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004900
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004901 cpi->seq_params_locked = 1;
4902
Yaowu Xuc27fc142016-08-22 16:08:15 -07004903 // Set up frame to show to get ready for stats collection.
4904 cm->frame_to_show = get_frame_new_buffer(cm);
4905
Zoe Liub4f31032017-11-03 23:48:35 -07004906 // Update current frame offset.
4907 cm->frame_offset =
4908 cm->buffer_pool->frame_bufs[cm->new_fb_idx].cur_frame_offset;
Zoe Liub4f31032017-11-03 23:48:35 -07004909
Yaowu Xuc27fc142016-08-22 16:08:15 -07004910#if DUMP_RECON_FRAMES == 1
4911 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4912 dump_filtered_recon_frames(cpi);
4913#endif // DUMP_RECON_FRAMES
4914
4915 // Update the LAST_FRAME in the reference frame buffer.
Zoe Liue9b15e22017-07-19 15:53:01 -07004916 // NOTE:
4917 // (1) For BWDREF_FRAME as the show_existing_frame, the reference frame
4918 // update has been done previously when handling the LAST_BIPRED_FRAME
4919 // right before BWDREF_FRAME (in the display order);
4920 // (2) For INTNL_OVERLAY as the show_existing_frame, the reference frame
Urvang Joshif1fa6862018-01-08 16:39:33 -08004921 // update will be done when the following is called, which will
4922 // exchange
Zoe Liue9b15e22017-07-19 15:53:01 -07004923 // the virtual indexes between LAST_FRAME and ALTREF2_FRAME, so that
Urvang Joshif1fa6862018-01-08 16:39:33 -08004924 // LAST3 will get retired, LAST2 becomes LAST3, LAST becomes LAST2,
4925 // and
Zoe Liue9b15e22017-07-19 15:53:01 -07004926 // ALTREF2_FRAME will serve as the new LAST_FRAME.
Cheng Chen46f30c72017-09-07 11:13:33 -07004927 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004928
4929 // Update frame flags
4930 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
4931 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
4932 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
4933
4934 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
4935
4936 // Update the frame type
4937 cm->last_frame_type = cm->frame_type;
4938
Yaowu Xuc27fc142016-08-22 16:08:15 -07004939 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4940 // to do post-encoding update accordingly.
4941 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004942 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004943 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004944 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004945
Yaowu Xuc27fc142016-08-22 16:08:15 -07004946 ++cm->current_video_frame;
4947
Tom Finegane4099e32018-01-23 12:01:51 -08004948 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004949 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004950
4951 // Set default state for segment based loop filter update flags.
4952 cm->lf.mode_ref_delta_update = 0;
4953
Yaowu Xuc27fc142016-08-22 16:08:15 -07004954 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05004955 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004956 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004957 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004958
4959 // If segmentation is enabled force a map update for key frames.
4960 if (seg->enabled) {
4961 seg->update_map = 1;
4962 seg->update_data = 1;
4963 }
4964
4965 // The alternate reference frame cannot be active for a key frame.
4966 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004967 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00004968 if (cpi->oxcf.mtu == 0) {
4969 cm->num_tg = cpi->oxcf.num_tile_groups;
4970 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08004971 // Use a default value for the purposes of weighting costs in probability
4972 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00004973 cm->num_tg = DEFAULT_MAX_NUM_TG;
4974 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004975
4976 // For 1 pass CBR, check if we are dropping this frame.
4977 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004978 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07004979 cm->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004980 if (av1_rc_drop_frame(cpi)) {
4981 av1_rc_postencode_update_drop_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004982 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004983 }
4984 }
4985
Yaowu Xuf883b422016-08-30 14:01:10 -07004986 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004987
4988#if CONFIG_INTERNAL_STATS
4989 memset(cpi->mode_chosen_counts, 0,
4990 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4991#endif
4992
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004993 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004994 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08004995 * wraparound) */
Sebastien Alaiwand418f682017-10-19 15:06:52 +02004996 const int frame_id_length = FRAME_ID_LENGTH;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004997 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00004998 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08004999 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07005000 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5001 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5002 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005003 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005004 lsb = cpi->source->y_buffer[0] & 0xff;
5005 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005006 }
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005007 cm->current_frame_id = ((msb << 8) + lsb) % (1 << frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005008
5009 // S_frame is meant for stitching different streams of different
5010 // resolutions together, so current_frame_id must be the
5011 // same across different streams of the same content current_frame_id
5012 // should be the same and not random. 0x37 is a chosen number as start
5013 // point
5014 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005015 } else {
5016 cm->current_frame_id =
Arild Fuldseth (arilfuld)788dc232016-12-20 17:55:52 +01005017 (cm->current_frame_id + 1 + (1 << frame_id_length)) %
5018 (1 << frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005019 }
5020 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005021
Hui Su483a8452018-02-26 12:28:48 -08005022 switch (cpi->oxcf.cdf_update_mode) {
5023 case 0: // No CDF update for any frames(4~6% compression loss).
5024 cm->disable_cdf_update = 1;
5025 break;
5026 case 1: // Enable CDF update for all frames.
5027 cm->disable_cdf_update = 0;
5028 break;
5029 case 2:
5030 // Strategically determine at which frames to do CDF update.
5031 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5032 // compression loss).
5033 // TODO(huisu@google.com): design schemes for various trade-offs between
5034 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005035 cm->disable_cdf_update =
5036 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005037 break;
5038 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005039 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005040
Yaowu Xuc27fc142016-08-22 16:08:15 -07005041 if (cpi->sf.recode_loop == DISALLOW_RECODE) {
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005042 if (encode_without_recode_loop(cpi) != AOM_CODEC_OK) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005043 } else {
Tom Finegane4099e32018-01-23 12:01:51 -08005044 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
5045 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005046 }
5047
Yi Luo10e23002017-07-31 11:54:43 -07005048 cm->last_tile_cols = cm->tile_cols;
5049 cm->last_tile_rows = cm->tile_rows;
5050
Yaowu Xuc27fc142016-08-22 16:08:15 -07005051#ifdef OUTPUT_YUV_SKINMAP
5052 if (cpi->common.current_video_frame > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005053 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005054 }
5055#endif // OUTPUT_YUV_SKINMAP
5056
5057 // Special case code to reduce pulsing when key frames are forced at a
5058 // fixed interval. Note the reconstruction error if it is the frame before
5059 // the force key frame
5060 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005061 if (seq_params->use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005062 cpi->ambient_err =
Alex Conversef77fd0b2017-04-20 11:00:24 -07005063 aom_highbd_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005064 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005065 cpi->ambient_err = aom_get_y_sse(cpi->source, get_frame_new_buffer(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005066 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005067 }
5068
Tarek AMARAc9813852018-03-05 18:40:18 -05005069 // If the encoder forced a KEY_FRAME decision or if frame is an S_FRAME
Sarah Parkerb9041612018-05-22 19:06:47 -07005070 if ((cm->frame_type == KEY_FRAME && cm->show_frame) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005071 cpi->refresh_last_frame = 1;
5072 }
5073
5074 cm->frame_to_show = get_frame_new_buffer(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005075 cm->frame_to_show->color_primaries = seq_params->color_primaries;
5076 cm->frame_to_show->transfer_characteristics =
5077 seq_params->transfer_characteristics;
5078 cm->frame_to_show->matrix_coefficients = seq_params->matrix_coefficients;
5079 cm->frame_to_show->monochrome = seq_params->monochrome;
5080 cm->frame_to_show->chroma_sample_position =
5081 seq_params->chroma_sample_position;
5082 cm->frame_to_show->color_range = seq_params->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005083 cm->frame_to_show->render_width = cm->render_width;
5084 cm->frame_to_show->render_height = cm->render_height;
5085
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005086 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5087 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005088
5089 // Pick the loop filter level for the frame.
Cheng Chen68dc9142018-05-02 17:46:28 -07005090 if (!cm->allow_intrabc) {
David Barker218556e2018-02-14 14:23:12 +00005091 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08005092 } else {
Hui Su06463e42018-02-23 22:17:36 -08005093 cm->lf.filter_level[0] = 0;
5094 cm->lf.filter_level[1] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005095 cm->cdef_bits = 0;
5096 cm->cdef_strengths[0] = 0;
5097 cm->nb_cdef_strengths = 1;
5098 cm->cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005099 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5100 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5101 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08005102 }
David Barker218556e2018-02-14 14:23:12 +00005103
5104 // TODO(debargha): Fix mv search range on encoder side
5105 // aom_extend_frame_inner_borders(cm->frame_to_show, av1_num_planes(cm));
5106 aom_extend_frame_borders(cm->frame_to_show, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005107
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005108#ifdef OUTPUT_YUV_REC
5109 aom_write_one_yuv_frame(cm, cm->frame_to_show);
5110#endif
5111
Yaowu Xuc27fc142016-08-22 16:08:15 -07005112 // Build the bitstream
Tom Finegane4099e32018-01-23 12:01:51 -08005113 if (av1_pack_bitstream(cpi, dest, size) != AOM_CODEC_OK)
5114 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005115
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005116 cpi->seq_params_locked = 1;
5117
Hui Sudc54be62018-03-14 19:14:28 -07005118 if (skip_adapt) return AOM_CODEC_OK;
Rostislav Pehlivanov74021a52017-03-09 09:05:29 +00005119
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005120 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005121 int i;
Zoe Liu630a89f2018-04-16 09:36:50 -07005122 // Update reference frame id values based on the value of refresh_frame_mask
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005123 for (i = 0; i < REF_FRAMES; i++) {
Zoe Liu630a89f2018-04-16 09:36:50 -07005124 if ((cpi->refresh_frame_mask >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005125 cm->ref_frame_id[i] = cm->current_frame_id;
5126 }
5127 }
5128 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005129
Yaowu Xuc27fc142016-08-22 16:08:15 -07005130#if DUMP_RECON_FRAMES == 1
5131 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005132 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005133#endif // DUMP_RECON_FRAMES
5134
Soo-Chul Han934af352017-10-15 15:21:51 -04005135 if (cm->seg.enabled) {
5136 if (cm->seg.update_map) {
5137 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005138 } else if (cm->last_frame_seg_map) {
Soo-Chul Han934af352017-10-15 15:21:51 -04005139 memcpy(cm->current_frame_seg_map, cm->last_frame_seg_map,
5140 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5141 }
5142 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005143
5144 if (frame_is_intra_only(cm) == 0) {
5145 release_scaled_references(cpi);
5146 }
5147
Cheng Chen46f30c72017-09-07 11:13:33 -07005148 update_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005149
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005150#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005151 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005152#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005153
Hui Sudc54be62018-03-14 19:14:28 -07005154 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
5155 *cm->fc = cpi->tile_data[cm->largest_tile_id].tctx;
5156 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005157 }
5158
5159 if (cpi->refresh_golden_frame == 1)
5160 cpi->frame_flags |= FRAMEFLAGS_GOLDEN;
5161 else
5162 cpi->frame_flags &= ~FRAMEFLAGS_GOLDEN;
5163
5164 if (cpi->refresh_alt_ref_frame == 1)
5165 cpi->frame_flags |= FRAMEFLAGS_ALTREF;
5166 else
5167 cpi->frame_flags &= ~FRAMEFLAGS_ALTREF;
5168
Yaowu Xuc27fc142016-08-22 16:08:15 -07005169 if (cpi->refresh_bwd_ref_frame == 1)
5170 cpi->frame_flags |= FRAMEFLAGS_BWDREF;
5171 else
5172 cpi->frame_flags &= ~FRAMEFLAGS_BWDREF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005173
Yaowu Xuc27fc142016-08-22 16:08:15 -07005174 cm->last_frame_type = cm->frame_type;
5175
Yaowu Xuf883b422016-08-30 14:01:10 -07005176 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005177
Yaowu Xuc27fc142016-08-22 16:08:15 -07005178 if (cm->frame_type == KEY_FRAME) {
5179 // Tell the caller that the frame was coded as a key frame
5180 *frame_flags = cpi->frame_flags | FRAMEFLAGS_KEY;
5181 } else {
5182 *frame_flags = cpi->frame_flags & ~FRAMEFLAGS_KEY;
5183 }
5184
5185 // Clear the one shot update flags for segmentation map and mode/ref loop
5186 // filter deltas.
5187 cm->seg.update_map = 0;
5188 cm->seg.update_data = 0;
5189 cm->lf.mode_ref_delta_update = 0;
5190
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005191 // A droppable frame might not be shown but it always
5192 // takes a space in the gf group. Therefore, even when
5193 // it is not shown, we still need update the count down.
5194
Yaowu Xuc27fc142016-08-22 16:08:15 -07005195 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08005196 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5197 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005198 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005199 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005200 // Don't increment frame counters if this was an altref buffer
5201 // update not a real frame
Wei-Ting Lin96ee0eb2018-06-22 15:27:22 -07005202
Yaowu Xuc27fc142016-08-22 16:08:15 -07005203 ++cm->current_video_frame;
5204 }
5205
Yaowu Xuc27fc142016-08-22 16:08:15 -07005206 // NOTE: Shall not refer to any frame not used as reference.
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005207 if (cm->is_reference_frame) {
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005208 // keep track of the last coded dimensions
5209 cm->last_width = cm->width;
5210 cm->last_height = cm->height;
5211
5212 // reset to normal state now that we are done.
5213 cm->last_show_frame = cm->show_frame;
Fergus Simpson2b4ea112017-06-19 11:33:59 -07005214 }
Yi Luo10e23002017-07-31 11:54:43 -07005215
Tom Finegane4099e32018-01-23 12:01:51 -08005216 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005217}
5218
Sarah Parker3491dd22018-08-08 18:38:31 -07005219static INLINE void update_keyframe_counters(AV1_COMP *cpi) {
5220 // TODO(zoeliu): To investigate whether we should treat BWDREF_FRAME
5221 // differently here for rc->avg_frame_bandwidth.
5222 if (cpi->common.show_frame || cpi->rc.is_bwd_ref_frame) {
5223 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref ||
5224 cpi->common.frame_type == KEY_FRAME) {
5225 // If this is a show_existing_frame with a source other than altref,
5226 // or if it is not a displayed forward keyframe, the keyframe update
5227 // counters were incremented when it was originally encoded.
5228 cpi->rc.frames_since_key++;
5229 cpi->rc.frames_to_key--;
5230 }
5231 }
5232}
5233
5234static INLINE void update_frames_till_gf_update(AV1_COMP *cpi) {
5235 // TODO(weitinglin): Updating this counter for is_frame_droppable
5236 // is a work-around to handle the condition when a frame is drop.
5237 // We should fix the cpi->common.show_frame flag
5238 // instead of checking the other condition to update the counter properly.
5239 if (cpi->common.show_frame || is_frame_droppable(cpi)) {
5240 // Decrement count down till next gf
5241 if (cpi->rc.frames_till_gf_update_due > 0)
5242 cpi->rc.frames_till_gf_update_due--;
5243 }
5244}
5245
5246static INLINE void update_twopass_gf_group_index(AV1_COMP *cpi) {
5247 // Increment the gf group index ready for the next frame. If this is
5248 // a show_existing_frame with a source other than altref, or if it is not
5249 // a displayed forward keyframe, the index was incremented when it was
5250 // originally encoded.
5251 if (!cpi->common.show_existing_frame || cpi->rc.is_src_frame_alt_ref ||
5252 cpi->common.frame_type == KEY_FRAME) {
5253 ++cpi->twopass.gf_group.index;
5254 }
5255}
5256
5257static void update_rc_counts(AV1_COMP *cpi) {
5258 update_keyframe_counters(cpi);
5259 update_frames_till_gf_update(cpi);
5260 if (cpi->oxcf.pass == 2) update_twopass_gf_group_index(cpi);
5261}
5262
Tom Finegane4099e32018-01-23 12:01:51 -08005263static int Pass0Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5264 int skip_adapt, unsigned int *frame_flags) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005265 if (cpi->oxcf.rc_mode == AOM_CBR) {
5266 av1_rc_get_one_pass_cbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005267 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -07005268 av1_rc_get_one_pass_vbr_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005269 }
Debargha Mukherjeeff48c092018-04-04 23:53:40 -07005270 if (encode_frame_to_data_rate(cpi, size, dest, skip_adapt, frame_flags) !=
5271 AOM_CODEC_OK) {
5272 return AOM_CODEC_ERROR;
5273 }
Sarah Parker3491dd22018-08-08 18:38:31 -07005274 update_rc_counts(cpi);
Debargha Mukherjeeff48c092018-04-04 23:53:40 -07005275 check_show_existing_frame(cpi);
5276 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005277}
5278
Tom Finegane4099e32018-01-23 12:01:51 -08005279static int Pass2Encode(AV1_COMP *cpi, size_t *size, uint8_t *dest,
5280 unsigned int *frame_flags) {
Angie Chiang5b5f4df2017-12-06 10:41:12 -08005281#if CONFIG_MISMATCH_DEBUG
5282 mismatch_move_frame_idx_w();
5283#endif
Angie Chiang4d55d762017-12-13 16:18:37 -08005284#if TXCOEFF_COST_TIMER
5285 AV1_COMMON *cm = &cpi->common;
5286 cm->txcoeff_cost_timer = 0;
5287 cm->txcoeff_cost_count = 0;
5288#endif
Tom Finegane4099e32018-01-23 12:01:51 -08005289
5290 if (encode_frame_to_data_rate(cpi, size, dest, 0, frame_flags) !=
5291 AOM_CODEC_OK) {
5292 return AOM_CODEC_ERROR;
5293 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005294
Angie Chiang4d55d762017-12-13 16:18:37 -08005295#if TXCOEFF_COST_TIMER
5296 cm->cum_txcoeff_cost_timer += cm->txcoeff_cost_timer;
5297 fprintf(stderr,
5298 "\ntxb coeff cost block number: %ld, frame time: %ld, cum time %ld "
5299 "in us\n",
5300 cm->txcoeff_cost_count, cm->txcoeff_cost_timer,
5301 cm->cum_txcoeff_cost_timer);
5302#endif
5303
Sarah Parker3491dd22018-08-08 18:38:31 -07005304 av1_twopass_postencode_update(cpi);
5305 update_rc_counts(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005306 check_show_existing_frame(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005307 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005308}
5309
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005310#if CONFIG_DENOISE
5311static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5312 int block_size, float noise_level,
5313 int64_t time_stamp, int64_t end_time) {
5314 AV1_COMMON *const cm = &cpi->common;
5315 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005316 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5317 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005318 if (!cpi->denoise_and_model) {
5319 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5320 "Error allocating denoise and model");
5321 return -1;
5322 }
5323 }
5324 if (!cpi->film_grain_table) {
5325 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5326 if (!cpi->film_grain_table) {
5327 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5328 "Error allocating grain table");
5329 return -1;
5330 }
5331 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5332 }
5333 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5334 &cm->film_grain_params)) {
5335 if (cm->film_grain_params.apply_grain) {
5336 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5337 &cm->film_grain_params);
5338 }
5339 }
5340 return 0;
5341}
5342#endif
5343
James Zern3e2613b2017-03-30 23:14:40 -07005344int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005345 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5346 int64_t end_time) {
5347 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005348 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -07005349 struct aom_usec_timer timer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005350 int res = 0;
5351 const int subsampling_x = sd->subsampling_x;
5352 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005353 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005354
Yaowu Xuc27fc142016-08-22 16:08:15 -07005355 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005356
Yaowu Xuf883b422016-08-30 14:01:10 -07005357 aom_usec_timer_start(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005358
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005359#if CONFIG_DENOISE
5360 if (cpi->oxcf.noise_level > 0)
5361 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5362 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5363 res = -1;
5364#endif // CONFIG_DENOISE
5365
Yaowu Xuf883b422016-08-30 14:01:10 -07005366 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005367 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005368 res = -1;
Yaowu Xuf883b422016-08-30 14:01:10 -07005369 aom_usec_timer_mark(&timer);
5370 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005371
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005372 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005373 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005374 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005375 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005376 res = -1;
5377 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005378 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005379 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005380 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005381 "Profile 1 requires 4:4:4 color format");
5382 res = -1;
5383 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005384 if ((seq_params->profile == PROFILE_2) &&
5385 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005386 !(subsampling_x == 1 && subsampling_y == 0)) {
5387 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5388 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005389 res = -1;
5390 }
5391
5392 return res;
5393}
5394
Yaowu Xuf883b422016-08-30 14:01:10 -07005395static int frame_is_reference(const AV1_COMP *cpi) {
5396 const AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005397
5398 return cm->frame_type == KEY_FRAME || cpi->refresh_last_frame ||
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005399 cpi->refresh_golden_frame || cpi->refresh_bwd_ref_frame ||
5400 cpi->refresh_alt2_ref_frame || cpi->refresh_alt_ref_frame ||
5401 !cm->error_resilient_mode || cm->lf.mode_ref_delta_update ||
5402 cm->seg.update_map || cm->seg.update_data;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005403}
5404
Yaowu Xuf883b422016-08-30 14:01:10 -07005405static void adjust_frame_rate(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005406 const struct lookahead_entry *source) {
5407 int64_t this_duration;
5408 int step = 0;
5409
5410 if (source->ts_start == cpi->first_time_stamp_ever) {
5411 this_duration = source->ts_end - source->ts_start;
5412 step = 1;
5413 } else {
5414 int64_t last_duration =
5415 cpi->last_end_time_stamp_seen - cpi->last_time_stamp_seen;
5416
5417 this_duration = source->ts_end - cpi->last_end_time_stamp_seen;
5418
5419 // do a step update if the duration changes by 10%
5420 if (last_duration)
5421 step = (int)((this_duration - last_duration) * 10 / last_duration);
5422 }
5423
5424 if (this_duration) {
5425 if (step) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005426 av1_new_framerate(cpi, 10000000.0 / this_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005427 } else {
5428 // Average this frame's rate into the last second's average
5429 // frame rate. If we haven't seen 1 second yet, then average
5430 // over the whole interval seen.
Yaowu Xuf883b422016-08-30 14:01:10 -07005431 const double interval = AOMMIN(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005432 (double)(source->ts_end - cpi->first_time_stamp_ever), 10000000.0);
5433 double avg_duration = 10000000.0 / cpi->framerate;
5434 avg_duration *= (interval - avg_duration + this_duration);
5435 avg_duration /= interval;
5436
Yaowu Xuf883b422016-08-30 14:01:10 -07005437 av1_new_framerate(cpi, 10000000.0 / avg_duration);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005438 }
5439 }
5440 cpi->last_time_stamp_seen = source->ts_start;
5441 cpi->last_end_time_stamp_seen = source->ts_end;
5442}
5443
5444// Returns 0 if this is not an alt ref else the offset of the source frame
5445// used as the arf midpoint.
Yaowu Xuf883b422016-08-30 14:01:10 -07005446static int get_arf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005447 RATE_CONTROL *const rc = &cpi->rc;
5448 int arf_src_index = 0;
5449 if (is_altref_enabled(cpi)) {
5450 if (cpi->oxcf.pass == 2) {
5451 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5452 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
5453 arf_src_index = gf_group->arf_src_offset[gf_group->index];
5454 }
5455 } else if (rc->source_alt_ref_pending) {
5456 arf_src_index = rc->frames_till_gf_update_due;
5457 }
5458 }
5459 return arf_src_index;
5460}
5461
Yaowu Xuf883b422016-08-30 14:01:10 -07005462static int get_brf_src_index(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005463 int brf_src_index = 0;
5464 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5465
5466 // TODO(zoeliu): We need to add the check on the -bwd_ref command line setup
5467 // flag.
5468 if (gf_group->bidir_pred_enabled[gf_group->index]) {
5469 if (cpi->oxcf.pass == 2) {
5470 if (gf_group->update_type[gf_group->index] == BRF_UPDATE)
5471 brf_src_index = gf_group->brf_src_offset[gf_group->index];
5472 } else {
5473 // TODO(zoeliu): To re-visit the setup for this scenario
5474 brf_src_index = cpi->rc.bipred_group_interval - 1;
5475 }
5476 }
5477
5478 return brf_src_index;
5479}
Zoe Liue9b15e22017-07-19 15:53:01 -07005480
Zoe Liue9b15e22017-07-19 15:53:01 -07005481// Returns 0 if this is not an alt ref else the offset of the source frame
5482// used as the arf midpoint.
5483static int get_arf2_src_index(AV1_COMP *cpi) {
5484 int arf2_src_index = 0;
5485 if (is_altref_enabled(cpi) && cpi->num_extra_arfs) {
5486 if (cpi->oxcf.pass == 2) {
5487 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5488 if (gf_group->update_type[gf_group->index] == INTNL_ARF_UPDATE) {
5489 arf2_src_index = gf_group->arf_src_offset[gf_group->index];
5490 }
5491 }
5492 }
5493 return arf2_src_index;
5494}
Yaowu Xuc27fc142016-08-22 16:08:15 -07005495
Yaowu Xuf883b422016-08-30 14:01:10 -07005496static void check_src_altref(AV1_COMP *cpi,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005497 const struct lookahead_entry *source) {
5498 RATE_CONTROL *const rc = &cpi->rc;
5499
5500 // If pass == 2, the parameters set here will be reset in
Yaowu Xuf883b422016-08-30 14:01:10 -07005501 // av1_rc_get_second_pass_params()
Yaowu Xuc27fc142016-08-22 16:08:15 -07005502
5503 if (cpi->oxcf.pass == 2) {
5504 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5505 rc->is_src_frame_alt_ref =
Yaowu Xuc27fc142016-08-22 16:08:15 -07005506 (gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE) ||
Yaowu Xuc27fc142016-08-22 16:08:15 -07005507 (gf_group->update_type[gf_group->index] == OVERLAY_UPDATE);
Zoe Liue9b15e22017-07-19 15:53:01 -07005508 rc->is_src_frame_ext_arf =
5509 gf_group->update_type[gf_group->index] == INTNL_OVERLAY_UPDATE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005510 } else {
5511 rc->is_src_frame_alt_ref =
5512 cpi->alt_ref_source && (source == cpi->alt_ref_source);
5513 }
5514
5515 if (rc->is_src_frame_alt_ref) {
5516 // Current frame is an ARF overlay frame.
5517 cpi->alt_ref_source = NULL;
5518
Zoe Liue9b15e22017-07-19 15:53:01 -07005519 if (rc->is_src_frame_ext_arf && !cpi->common.show_existing_frame) {
5520 // For INTNL_OVERLAY, when show_existing_frame == 0, they do need to
5521 // refresh the LAST_FRAME, i.e. LAST3 gets retired, LAST2 becomes LAST3,
5522 // LAST becomes LAST2, and INTNL_OVERLAY becomes LAST.
5523 cpi->refresh_last_frame = 1;
5524 } else {
Zoe Liue9b15e22017-07-19 15:53:01 -07005525 // Don't refresh the last buffer for an ARF overlay frame. It will
5526 // become the GF so preserve last as an alternative prediction option.
5527 cpi->refresh_last_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07005528 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005529 }
5530}
5531
5532#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005533extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5534 const unsigned char *img2, int img2_pitch,
5535 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005536
5537static void adjust_image_stat(double y, double u, double v, double all,
5538 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07005539 s->stat[STAT_Y] += y;
5540 s->stat[STAT_U] += u;
5541 s->stat[STAT_V] += v;
5542 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005543 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005544}
5545
Angie Chiang08a22a62017-07-17 17:29:17 -07005546static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005547 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005548 double samples = 0.0;
5549 uint32_t in_bit_depth = 8;
5550 uint32_t bit_depth = 8;
5551
Angie Chiang08a22a62017-07-17 17:29:17 -07005552#if CONFIG_INTER_STATS_ONLY
Yaowu Xu1b4ffc42017-07-26 09:54:07 -07005553 if (cm->frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005554#endif
5555 cpi->bytes += frame_bytes;
5556
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005557 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005558 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005559 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005560 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005561 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005562 const YV12_BUFFER_CONFIG *orig = cpi->source;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005563 const YV12_BUFFER_CONFIG *recon = cpi->common.frame_to_show;
5564 double y, u, v, frame_all;
5565
5566 cpi->count++;
5567 if (cpi->b_calculate_psnr) {
5568 PSNR_STATS psnr;
5569 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005570 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005571 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005572 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005573
5574 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5575 &cpi->psnr);
5576 cpi->total_sq_error += psnr.sse[0];
5577 cpi->total_samples += psnr.samples[0];
5578 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005579 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005580 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005581 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005582 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005583 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005584 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005585
Yaowu Xuf883b422016-08-30 14:01:10 -07005586 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005587 cpi->summed_quality += frame_ssim2 * weight;
5588 cpi->summed_weights += weight;
5589
5590#if 0
5591 {
5592 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005593 double y2 = psnr.psnr[1];
5594 double u2 = psnr.psnr[2];
5595 double v2 = psnr.psnr[3];
5596 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005597 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 -08005598 cm->current_video_frame, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005599 frame_psnr2, frame_ssim2);
5600 fclose(f);
5601 }
5602#endif
5603 }
5604 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005605 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005606 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005607 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5608 recon->y_stride, orig->y_width, orig->y_height);
5609 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005610 cpi->total_blockiness += frame_blockiness;
5611 }
5612
5613 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005614 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005615 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005616 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5617 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5618
5619 const double peak = (double)((1 << in_bit_depth) - 1);
5620 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005621 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005622 if (consistency > 0.0)
5623 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005624 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005625 cpi->total_inconsistency += this_inconsistency;
5626 }
5627 }
5628 }
5629
5630 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005631 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005632 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005633 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005634 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5635 }
5636}
5637#endif // CONFIG_INTERNAL_STATS
5638
RogerZhou3b635242017-09-19 10:06:46 -07005639static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5640 const YV12_BUFFER_CONFIG *last_picture,
5641 hash_table *last_hash_table) {
5642 aom_clear_system_state();
5643 // check use hash ME
5644 int k;
5645 uint32_t hash_value_1;
5646 uint32_t hash_value_2;
5647
5648 const int block_size = 8;
5649 const double threshold_current = 0.8;
5650 const double threshold_average = 0.95;
5651 const int max_history_size = 32;
5652 int T = 0; // total block
5653 int C = 0; // match with collocated block
5654 int S = 0; // smooth region but not match with collocated block
5655 int M = 0; // match with other block
5656
5657 const int pic_width = cur_picture->y_width;
5658 const int pic_height = cur_picture->y_height;
5659 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5660 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5661 const int x_pos = j;
5662 const int y_pos = i;
5663 int match = 1;
5664 T++;
5665
5666 // check whether collocated block match with current
5667 uint8_t *p_cur = cur_picture->y_buffer;
5668 uint8_t *p_ref = last_picture->y_buffer;
5669 int stride_cur = cur_picture->y_stride;
5670 int stride_ref = last_picture->y_stride;
5671 p_cur += (y_pos * stride_cur + x_pos);
5672 p_ref += (y_pos * stride_ref + x_pos);
5673
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005674 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5675 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5676 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5677 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5678 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5679 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5680 match = 0;
5681 }
RogerZhou3b635242017-09-19 10:06:46 -07005682 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005683 p16_cur += stride_cur;
5684 p16_ref += stride_ref;
RogerZhou3b635242017-09-19 10:06:46 -07005685 }
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005686 } else {
5687 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5688 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5689 if (p_cur[tmpX] != p_ref[tmpX]) {
5690 match = 0;
5691 }
5692 }
5693 p_cur += stride_cur;
5694 p_ref += stride_ref;
5695 }
RogerZhou3b635242017-09-19 10:06:46 -07005696 }
5697
5698 if (match) {
5699 C++;
5700 continue;
5701 }
5702
5703 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5704 y_pos) ||
5705 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5706 S++;
5707 continue;
5708 }
5709
5710 av1_get_block_hash_value(
5711 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005712 block_size, &hash_value_1, &hash_value_2,
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05305713 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
Debargha Mukherjee1c583012018-02-28 22:16:16 -08005714 // Hashing does not work for highbitdepth currently.
5715 // TODO(Roger): Make it work for highbitdepth.
5716 if (av1_use_hash_me(&cpi->common)) {
5717 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5718 M++;
5719 }
RogerZhou3b635242017-09-19 10:06:46 -07005720 }
5721 }
5722 }
5723
5724 assert(T > 0);
5725 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5726 double m_rate = ((double)(M)) / ((double)(T));
5727
5728 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5729 cpi->m_rate_array[cpi->rate_index] = m_rate;
5730
5731 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5732 cpi->rate_size++;
5733 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5734
5735 if (csm_rate < threshold_current) {
5736 return 0;
5737 }
5738
5739 if (C == T) {
5740 return 1;
5741 }
5742
5743 double csm_average = 0.0;
5744 double m_average = 0.0;
5745
5746 for (k = 0; k < cpi->rate_size; k++) {
5747 csm_average += cpi->csm_rate_array[k];
5748 m_average += cpi->m_rate_array[k];
5749 }
5750 csm_average /= cpi->rate_size;
5751 m_average /= cpi->rate_size;
5752
5753 if (csm_average < threshold_average) {
5754 return 0;
5755 }
5756
5757 if (M > (T - C - S) / 3) {
5758 return 1;
5759 }
5760
5761 if (csm_rate > 0.99 && m_rate > 0.01) {
5762 return 1;
5763 }
5764
5765 if (csm_average + m_average > 1.01) {
5766 return 1;
5767 }
5768
5769 return 0;
5770}
RogerZhou3b635242017-09-19 10:06:46 -07005771
Andrey Norkin795ba872018-03-06 13:24:14 -08005772int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5773 size_t *size, uint8_t *dest, int64_t *time_stamp,
5774 int64_t *time_end, int flush,
5775 const aom_rational_t *timebase) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005776 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5777 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005778 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005779 BufferPool *const pool = cm->buffer_pool;
5780 RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07005781 struct aom_usec_timer cmptimer;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005782 YV12_BUFFER_CONFIG *force_src_buffer = NULL;
5783 struct lookahead_entry *last_source = NULL;
5784 struct lookahead_entry *source = NULL;
5785 int arf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005786 int brf_src_index;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005787 int i;
5788
5789#if CONFIG_BITSTREAM_DEBUG
5790 assert(cpi->oxcf.max_threads == 0 &&
5791 "bitstream debug tool does not support multithreading");
5792 bitstream_queue_record_write();
Angie Chiangcb9a9eb2016-09-01 16:10:50 -07005793 bitstream_queue_set_frame_write(cm->current_video_frame * 2 + cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005794#endif
5795
Dominic Symesd4929012018-01-31 17:32:01 +01005796 cm->showable_frame = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005797 aom_usec_timer_start(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005798
RogerZhou3b635242017-09-19 10:06:46 -07005799 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005800
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07005801 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07005802 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
5803 ? REFRESH_FRAME_CONTEXT_DISABLED
5804 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005805 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08005806 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005807
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07005808 // default reference buffers update config
5809 av1_configure_buffer_updates_firstpass(cpi, LF_UPDATE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005810
Sarah Parkerb9041612018-05-22 19:06:47 -07005811 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005812 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005813 cm->reset_decoder_state = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005814
Sarah Parker7e13da82018-04-30 17:47:24 -07005815 // Don't allow a show_existing_frame to coincide with an error resilient or
Sarah Parker14feea42018-07-06 16:41:41 -07005816 // S-Frame. An exception can be made in the case of a keyframe, since it
5817 // does not depend on any previous frames. We must make this exception here
5818 // because of the use of show_existing_frame with forward coded keyframes.
Sarah Parker7e13da82018-04-30 17:47:24 -07005819 struct lookahead_entry *lookahead_src = NULL;
5820 if (cm->current_video_frame > 0)
5821 lookahead_src = av1_lookahead_peek(cpi->lookahead, 0);
Sarah Parker349e0bd2018-08-03 14:18:05 -07005822
5823 int use_show_existing = 1;
5824 if (lookahead_src != NULL) {
5825 const int is_error_resilient =
5826 cpi->oxcf.error_resilient_mode ||
5827 (lookahead_src->flags & AOM_EFLAG_ERROR_RESILIENT);
5828 const int is_s_frame = cpi->oxcf.s_frame_mode ||
5829 (lookahead_src->flags & AOM_EFLAG_SET_S_FRAME);
5830 const int is_key_frame =
5831 (rc->frames_to_key == 0) || (cpi->frame_flags & FRAMEFLAGS_KEY);
5832 use_show_existing = !(is_error_resilient || is_s_frame) || is_key_frame;
5833 }
Sarah Parker7e13da82018-04-30 17:47:24 -07005834
Sarah Parker33005522018-07-27 14:46:25 -07005835 if (oxcf->pass == 2 && cm->show_existing_frame && use_show_existing) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005836 // Manage the source buffer and flush out the source frame that has been
5837 // coded already; Also get prepared for PSNR calculation if needed.
Yaowu Xuf883b422016-08-30 14:01:10 -07005838 if ((source = av1_lookahead_pop(cpi->lookahead, flush)) == NULL) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005839 *size = 0;
5840 return -1;
5841 }
sarahparker21dbca42018-03-30 17:43:44 -07005842 av1_apply_encoding_flags(cpi, source->flags);
Alex Conversef77fd0b2017-04-20 11:00:24 -07005843 cpi->source = &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005844 // TODO(zoeliu): To track down to determine whether it's needed to adjust
5845 // the frame rate.
5846 *time_stamp = source->ts_start;
5847 *time_end = source->ts_end;
5848
5849 // We need to adjust frame rate for an overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005850 if (cpi->rc.is_src_frame_alt_ref) adjust_frame_rate(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005851
Urvang Joshif1fa6862018-01-08 16:39:33 -08005852 // Find a free buffer for the new frame, releasing the reference
5853 // previously
Yaowu Xuc27fc142016-08-22 16:08:15 -07005854 // held.
5855 if (cm->new_fb_idx != INVALID_IDX) {
5856 --pool->frame_bufs[cm->new_fb_idx].ref_count;
5857 }
5858 cm->new_fb_idx = get_free_fb(cm);
5859
5860 if (cm->new_fb_idx == INVALID_IDX) return -1;
5861
5862 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005863 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005864
5865 // Start with a 0 size frame.
5866 *size = 0;
5867
5868 // We need to update the gf_group for show_existing overlay frame
Zoe Liue04abf72017-04-19 15:37:11 -07005869 if (cpi->rc.is_src_frame_alt_ref) av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005870
Tom Finegane4099e32018-01-23 12:01:51 -08005871 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
5872 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005873
5874 if (cpi->b_calculate_psnr) generate_psnr_packet(cpi);
5875
5876#if CONFIG_INTERNAL_STATS
Angie Chiang08a22a62017-07-17 17:29:17 -07005877 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005878#endif // CONFIG_INTERNAL_STATS
5879
5880 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07005881 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005882
5883 cm->show_existing_frame = 0;
5884 return 0;
5885 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005886
5887 // Should we encode an arf frame.
5888 arf_src_index = get_arf_src_index(cpi);
5889 if (arf_src_index) {
5890 for (i = 0; i <= arf_src_index; ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005891 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005892 // Avoid creating an alt-ref if there's a forced keyframe pending.
5893 if (e == NULL) {
5894 break;
Yaowu Xuf883b422016-08-30 14:01:10 -07005895 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005896 arf_src_index = 0;
5897 flush = 1;
5898 break;
5899 }
5900 }
5901 }
5902
5903 if (arf_src_index) {
5904 assert(arf_src_index <= rc->frames_to_key);
5905
Yaowu Xuf883b422016-08-30 14:01:10 -07005906 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005907 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005908 cpi->alt_ref_source = source;
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005909 // When arf_src_index == rc->frames_to_key, it indicates a fwd_kf
Sarah Parkerb9041612018-05-22 19:06:47 -07005910 if (arf_src_index == rc->frames_to_key) {
5911 // Skip temporal filtering and mark as intra_only if we have a fwd_kf
5912 const GF_GROUP *const gf_group = &cpi->twopass.gf_group;
5913 int which_arf = gf_group->arf_update_idx[gf_group->index];
5914 cpi->is_arf_filter_off[which_arf] = 1;
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005915 cpi->no_show_kf = 1;
Sarah Parkerb9041612018-05-22 19:06:47 -07005916 } else {
5917 if (oxcf->arnr_max_frames > 0) {
5918 // Produce the filtered ARF frame.
5919 av1_temporal_filter(cpi, arf_src_index);
5920 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
5921 force_src_buffer = &cpi->alt_ref_buffer;
5922 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005923 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005924 cm->show_frame = 0;
5925 cm->intra_only = 0;
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07005926
5927 if (oxcf->pass < 2) {
5928 // In second pass, the buffer updates configure will be set
5929 // in the function av1_rc_get_second_pass_params
5930 av1_configure_buffer_updates_firstpass(cpi, ARF_UPDATE);
5931 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005932 }
5933 rc->source_alt_ref_pending = 0;
5934 }
5935
Zoe Liue9b15e22017-07-19 15:53:01 -07005936 // Should we encode an arf2 frame.
5937 arf_src_index = get_arf2_src_index(cpi);
5938 if (arf_src_index) {
5939 for (i = 0; i <= arf_src_index; ++i) {
5940 struct lookahead_entry *e = av1_lookahead_peek(cpi->lookahead, i);
5941 // Avoid creating an alt-ref if there's a forced keyframe pending.
5942 if (e == NULL) {
5943 break;
5944 } else if (e->flags == AOM_EFLAG_FORCE_KF) {
5945 arf_src_index = 0;
5946 flush = 1;
5947 break;
5948 }
5949 }
5950 }
5951
5952 if (arf_src_index) {
5953 assert(arf_src_index <= rc->frames_to_key);
5954
5955 if ((source = av1_lookahead_peek(cpi->lookahead, arf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005956 cm->showable_frame = 1;
Zoe Liue9b15e22017-07-19 15:53:01 -07005957 cpi->alt_ref_source = source;
5958
5959 if (oxcf->arnr_max_frames > 0) {
5960 // Produce the filtered ARF frame.
Sebastien Alaiwan6697acf2018-02-21 16:59:17 +01005961 av1_temporal_filter(cpi, arf_src_index);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00005962 aom_extend_frame_borders(&cpi->alt_ref_buffer, num_planes);
Zoe Liue9b15e22017-07-19 15:53:01 -07005963 force_src_buffer = &cpi->alt_ref_buffer;
5964 }
5965
5966 cm->show_frame = 0;
5967 cm->intra_only = 0;
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07005968
5969 if (oxcf->pass < 2) {
5970 // In second pass, the buffer updates configure will be set
5971 // in the function av1_rc_get_second_pass_params
5972 av1_configure_buffer_updates_firstpass(cpi, INTNL_ARF_UPDATE);
5973 }
Zoe Liue9b15e22017-07-19 15:53:01 -07005974 }
5975 rc->source_alt_ref_pending = 0;
5976 }
Zoe Liue9b15e22017-07-19 15:53:01 -07005977
Yaowu Xuc27fc142016-08-22 16:08:15 -07005978 rc->is_bwd_ref_frame = 0;
5979 brf_src_index = get_brf_src_index(cpi);
5980 if (brf_src_index) {
5981 assert(brf_src_index <= rc->frames_to_key);
Yaowu Xuf883b422016-08-30 14:01:10 -07005982 if ((source = av1_lookahead_peek(cpi->lookahead, brf_src_index)) != NULL) {
Dominic Symesd4929012018-01-31 17:32:01 +01005983 cm->showable_frame = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005984 cm->show_frame = 0;
5985 cm->intra_only = 0;
5986
Wei-Ting Lin2e8d0452018-06-27 09:32:39 -07005987 if (oxcf->pass < 2) {
5988 // In second pass, the buffer updates configure will be set
5989 // in the function av1_rc_get_second_pass_params
5990 av1_configure_buffer_updates_firstpass(cpi, BIPRED_UPDATE);
5991 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005992 }
5993 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005994
5995 if (!source) {
5996 // Get last frame source.
5997 if (cm->current_video_frame > 0) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005998 if ((last_source = av1_lookahead_peek(cpi->lookahead, -1)) == NULL)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005999 return -1;
6000 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006001 if (cm->current_video_frame > 0) assert(last_source != NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006002 // Read in the source frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07006003 source = av1_lookahead_pop(cpi->lookahead, flush);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006004
6005 if (source != NULL) {
6006 cm->show_frame = 1;
6007 cm->intra_only = 0;
6008
6009 // Check to see if the frame should be encoded as an arf overlay.
6010 check_src_altref(cpi, source);
6011 }
6012 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006013 if (source) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07006014 cpi->unscaled_source = cpi->source =
Yaowu Xuc27fc142016-08-22 16:08:15 -07006015 force_src_buffer ? force_src_buffer : &source->img;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006016 cpi->unscaled_last_source = last_source != NULL ? &last_source->img : NULL;
6017
6018 *time_stamp = source->ts_start;
6019 *time_end = source->ts_end;
Sarah Parker73556772018-03-28 18:28:05 -07006020 av1_apply_encoding_flags(cpi, source->flags);
Yaowu Xuf883b422016-08-30 14:01:10 -07006021 *frame_flags = (source->flags & AOM_EFLAG_FORCE_KF) ? FRAMEFLAGS_KEY : 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006022
6023 } else {
6024 *size = 0;
6025 if (flush && oxcf->pass == 1 && !cpi->twopass.first_pass_done) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006026 av1_end_first_pass(cpi); /* get last stats packet */
Yaowu Xuc27fc142016-08-22 16:08:15 -07006027 cpi->twopass.first_pass_done = 1;
6028 }
6029 return -1;
6030 }
6031
6032 if (source->ts_start < cpi->first_time_stamp_ever) {
6033 cpi->first_time_stamp_ever = source->ts_start;
6034 cpi->last_end_time_stamp_seen = source->ts_start;
6035 }
6036
6037 // Clear down mmx registers
Yaowu Xuf883b422016-08-30 14:01:10 -07006038 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006039
6040 // adjust frame rates based on timestamps given
6041 if (cm->show_frame) adjust_frame_rate(cpi, source);
6042
6043 // Find a free buffer for the new frame, releasing the reference previously
6044 // held.
6045 if (cm->new_fb_idx != INVALID_IDX) {
6046 --pool->frame_bufs[cm->new_fb_idx].ref_count;
6047 }
6048 cm->new_fb_idx = get_free_fb(cm);
6049
6050 if (cm->new_fb_idx == INVALID_IDX) return -1;
6051
Zoe Liuf452fdf2017-11-02 23:08:12 -07006052 // Retain the RF_LEVEL for the current newly coded frame.
6053 cpi->frame_rf_level[cm->new_fb_idx] =
6054 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index];
Zoe Liuf452fdf2017-11-02 23:08:12 -07006055
Yaowu Xuc27fc142016-08-22 16:08:15 -07006056 cm->cur_frame = &pool->frame_bufs[cm->new_fb_idx];
Yaowu Xu9b0f7032017-07-31 11:01:19 -07006057 cm->cur_frame->buf.buf_8bit_valid = 0;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07006058
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006059 if (cpi->film_grain_table) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -07006060 cm->seq_params.film_grain_params_present = aom_film_grain_table_lookup(
Neil Birkbecka2893ab2018-06-08 14:45:13 -07006061 cpi->film_grain_table, *time_stamp, *time_end, 0 /* =erase */,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -07006062 &cm->film_grain_params);
6063 }
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -07006064 cm->cur_frame->film_grain_params_present =
6065 cm->seq_params.film_grain_params_present;
Zoe Liu6cfaff92016-10-18 17:12:11 -07006066
Andrey Norkin795ba872018-03-06 13:24:14 -08006067 // only one operating point supported now
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07006068 const int64_t pts64 = ticks_to_timebase_units(timebase, *time_stamp);
6069 if (pts64 < 0 || pts64 > UINT32_MAX) return AOM_CODEC_ERROR;
6070 cpi->common.frame_presentation_time = (uint32_t)pts64;
Andrey Norkin795ba872018-03-06 13:24:14 -08006071
Yaowu Xuc27fc142016-08-22 16:08:15 -07006072 // Start with a 0 size frame.
6073 *size = 0;
6074
6075 cpi->frame_flags = *frame_flags;
6076
6077 if (oxcf->pass == 2) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006078 av1_rc_get_second_pass_params(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006079 } else if (oxcf->pass == 1) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07006080 setup_frame_size(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006081 }
6082
6083 if (cpi->oxcf.pass != 0 || frame_is_intra_only(cm) == 1) {
Zoe Liu27deb382018-03-27 15:13:56 -07006084 for (i = 0; i < REF_FRAMES; ++i) cpi->scaled_ref_idx[i] = INVALID_IDX;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006085 }
6086
Yaowu Xuc27fc142016-08-22 16:08:15 -07006087 cm->using_qmatrix = cpi->oxcf.using_qm;
6088 cm->min_qmlevel = cpi->oxcf.qm_minlevel;
6089 cm->max_qmlevel = cpi->oxcf.qm_maxlevel;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006090
David Barker5e70a112017-10-03 14:28:17 +01006091 if (cm->seq_params.frame_id_numbers_present_flag) {
Debargha Mukherjee778023d2017-09-26 17:50:27 -07006092 if (*time_stamp == 0) {
6093 cpi->common.current_frame_id = -1;
6094 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006095 }
Zoe Liuca0cd3f2018-02-26 15:07:50 -08006096
RogerZhou3b635242017-09-19 10:06:46 -07006097 cpi->cur_poc++;
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08006098 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
6099 !frame_is_intra_only(cm)) {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00006100 if (cpi->common.seq_params.force_integer_mv == 2) {
RogerZhou3b635242017-09-19 10:06:46 -07006101 struct lookahead_entry *previous_entry =
Debargha Mukherjeea71e3db2018-02-28 07:47:17 -08006102 av1_lookahead_peek(cpi->lookahead, cpi->previous_index);
6103 if (!previous_entry)
6104 cpi->common.cur_frame_force_integer_mv = 0;
6105 else
6106 cpi->common.cur_frame_force_integer_mv = is_integer_mv(
6107 cpi, cpi->source, &previous_entry->img, cpi->previous_hash_table);
RogerZhou3b635242017-09-19 10:06:46 -07006108 } else {
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00006109 cpi->common.cur_frame_force_integer_mv =
6110 cpi->common.seq_params.force_integer_mv;
RogerZhou3b635242017-09-19 10:06:46 -07006111 }
6112 } else {
RogerZhou10a03802017-10-26 11:49:48 -07006113 cpi->common.cur_frame_force_integer_mv = 0;
RogerZhou3b635242017-09-19 10:06:46 -07006114 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01006115
Yaowu Xuc27fc142016-08-22 16:08:15 -07006116 if (oxcf->pass == 1) {
6117 cpi->td.mb.e_mbd.lossless[0] = is_lossless_requested(oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07006118 av1_first_pass(cpi, source);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006119 } else if (oxcf->pass == 2) {
Tom Finegane4099e32018-01-23 12:01:51 -08006120 if (Pass2Encode(cpi, size, dest, frame_flags) != AOM_CODEC_OK)
6121 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006122 } else {
6123 // One pass encode
Tom Finegane4099e32018-01-23 12:01:51 -08006124 if (Pass0Encode(cpi, size, dest, 0, frame_flags) != AOM_CODEC_OK)
6125 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006126 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07006127 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08006128 cpi->previous_hash_table = &cm->cur_frame->hash_table;
RogerZhou3b635242017-09-19 10:06:46 -07006129 {
6130 int l;
6131 for (l = -MAX_PRE_FRAMES; l < cpi->lookahead->max_sz; l++) {
6132 if ((cpi->lookahead->buf + l) == source) {
Debargha Mukherjeee41a6672018-02-27 11:56:31 -08006133 cpi->previous_index = l;
RogerZhou3b635242017-09-19 10:06:46 -07006134 break;
6135 }
6136 }
6137
6138 if (l == cpi->lookahead->max_sz) {
6139 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
6140 "Failed to find last frame original buffer");
6141 }
6142 }
RogerZhoucc5d35d2017-08-07 22:20:15 -07006143 }
6144
Yunqing Wang267e3272017-11-09 14:23:22 -08006145 if (!cm->large_scale_tile) {
Yunqing Wang267e3272017-11-09 14:23:22 -08006146 cm->frame_contexts[cm->new_fb_idx] = *cm->fc;
Yunqing Wang267e3272017-11-09 14:23:22 -08006147 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006148
Yunqing Wangb041d8a2017-11-15 12:31:18 -08006149#define EXT_TILE_DEBUG 0
6150#if EXT_TILE_DEBUG
6151 if (cm->large_scale_tile && oxcf->pass == 2) {
6152 char fn[20] = "./fc";
6153 fn[4] = cm->current_video_frame / 100 + '0';
6154 fn[5] = (cm->current_video_frame % 100) / 10 + '0';
6155 fn[6] = (cm->current_video_frame % 10) + '0';
6156 fn[7] = '\0';
6157 av1_print_frame_contexts(cm->fc, fn);
6158 }
6159#endif // EXT_TILE_DEBUG
6160#undef EXT_TILE_DEBUG
Yaowu Xuc7119a72018-03-29 09:59:37 -07006161
Dominic Symesd4929012018-01-31 17:32:01 +01006162 cm->showable_frame = !cm->show_frame && cm->showable_frame;
Yunqing Wangb041d8a2017-11-15 12:31:18 -08006163
Yaowu Xuc27fc142016-08-22 16:08:15 -07006164 // No frame encoded, or frame was dropped, release scaled references.
6165 if ((*size == 0) && (frame_is_intra_only(cm) == 0)) {
6166 release_scaled_references(cpi);
6167 }
6168
6169 if (*size > 0) {
6170 cpi->droppable = !frame_is_reference(cpi);
6171 }
6172
Yaowu Xuf883b422016-08-30 14:01:10 -07006173 aom_usec_timer_mark(&cmptimer);
6174 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006175
6176 if (cpi->b_calculate_psnr && oxcf->pass != 1 && cm->show_frame)
6177 generate_psnr_packet(cpi);
6178
6179#if CONFIG_INTERNAL_STATS
6180 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006181 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006182 }
6183#endif // CONFIG_INTERNAL_STATS
6184
Yaowu Xuf883b422016-08-30 14:01:10 -07006185 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006186
6187 return 0;
6188}
6189
Yaowu Xuf883b422016-08-30 14:01:10 -07006190int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6191 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006192 if (!cm->show_frame) {
6193 return -1;
6194 } else {
6195 int ret;
6196 if (cm->frame_to_show) {
6197 *dest = *cm->frame_to_show;
6198 dest->y_width = cm->width;
6199 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006200 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6201 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006202 ret = 0;
6203 } else {
6204 ret = -1;
6205 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006206 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006207 return ret;
6208 }
6209}
6210
Yaowu Xuf883b422016-08-30 14:01:10 -07006211int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006212 if (cpi->last_show_frame_buf_idx == INVALID_IDX) return -1;
6213
6214 *frame =
6215 cpi->common.buffer_pool->frame_bufs[cpi->last_show_frame_buf_idx].buf;
6216 return 0;
6217}
6218
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006219static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6220 const YV12_BUFFER_CONFIG *b) {
6221 return a->y_height == b->y_height && a->y_width == b->y_width &&
6222 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6223 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6224 a->border == b->border &&
6225 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6226 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6227}
6228
Yunqing Wang93b18f32018-06-08 21:08:29 -07006229aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6230 YV12_BUFFER_CONFIG *new_frame,
6231 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006232 const int num_planes = av1_num_planes(cm);
6233 if (!equal_dimensions_and_border(new_frame, sd))
6234 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6235 "Incorrect buffer dimensions");
6236 else
6237 aom_yv12_copy_frame(new_frame, sd, num_planes);
6238
6239 return cm->error.error_code;
6240}
6241
Yaowu Xuf883b422016-08-30 14:01:10 -07006242int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6243 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006244 int hr = 0, hs = 0, vr = 0, vs = 0;
6245
6246 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6247
6248 Scale2Ratio(horiz_mode, &hr, &hs);
6249 Scale2Ratio(vert_mode, &vr, &vs);
6250
6251 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006252 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6253 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006254
6255 return 0;
6256}
6257
Yaowu Xuf883b422016-08-30 14:01:10 -07006258int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006259
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006260int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6261 size_t output_size = 0;
6262 size_t total_bytes_read = 0;
6263 size_t remaining_size = *frame_size;
6264 uint8_t *buff_ptr = buffer;
6265
6266 // go through each OBUs
6267 while (total_bytes_read < *frame_size) {
6268 uint8_t saved_obu_header[2];
6269 uint64_t obu_payload_size;
6270 size_t length_of_payload_size;
6271 size_t length_of_obu_size;
6272 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6273 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6274
6275 // save the obu header (1 or 2 bytes)
6276 memmove(saved_obu_header, buff_ptr, obu_header_size);
6277 // clear the obu_has_size_field
6278 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6279
6280 // get the payload_size and length of payload_size
6281 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6282 &obu_payload_size, &length_of_payload_size) != 0) {
6283 return AOM_CODEC_ERROR;
6284 }
6285 obu_bytes_read += length_of_payload_size;
6286
6287 // calculate the length of size of the obu header plus payload
6288 length_of_obu_size =
6289 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
6290
6291 // move the rest of data to new location
6292 memmove(buff_ptr + length_of_obu_size + obu_header_size,
6293 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07006294 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006295
6296 // write the new obu size
6297 const uint64_t obu_size = obu_header_size + obu_payload_size;
6298 size_t coded_obu_size;
6299 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
6300 &coded_obu_size) != 0) {
6301 return AOM_CODEC_ERROR;
6302 }
6303
6304 // write the saved (modified) obu_header following obu size
6305 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
6306
6307 total_bytes_read += obu_bytes_read;
6308 remaining_size -= obu_bytes_read;
6309 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07006310 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006311 }
6312
6313 *frame_size = output_size;
6314 return AOM_CODEC_OK;
6315}
6316
Yaowu Xuf883b422016-08-30 14:01:10 -07006317void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006318 // TODO(yunqingwang): For what references to use, external encoding flags
6319 // should be consistent with internal reference frame selection. Need to
6320 // ensure that there is not conflict between the two. In AV1 encoder, the
6321 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
6322 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
6323 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006324 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006325 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006326 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6327 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6328 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006329 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006330 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006331 } else {
6332 int ref = AOM_REFFRAME_ALL;
6333
6334 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6335 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6336
6337 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6338
6339 if (flags & AOM_EFLAG_NO_REF_ARF) {
6340 ref ^= AOM_ALT_FLAG;
6341 ref ^= AOM_BWD_FLAG;
6342 ref ^= AOM_ALT2_FLAG;
6343 } else {
6344 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6345 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
6346 }
6347
6348 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006349 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006350 }
6351
6352 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006353 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006354 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006355
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006356 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6357 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006358
Yaowu Xuf883b422016-08-30 14:01:10 -07006359 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006360
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006361 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6362 upd ^= AOM_ALT_FLAG;
6363 upd ^= AOM_BWD_FLAG;
6364 upd ^= AOM_ALT2_FLAG;
6365 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006366
Yaowu Xuf883b422016-08-30 14:01:10 -07006367 av1_update_reference(cpi, upd);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006368 }
6369
sarahparker21dbca42018-03-30 17:43:44 -07006370 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
6371 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07006372 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
6373 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07006374 cpi->ext_use_s_frame =
6375 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07006376 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07006377
Yaowu Xuf883b422016-08-30 14:01:10 -07006378 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6379 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006380 }
6381}
Andrey Norkin795ba872018-03-06 13:24:14 -08006382
Andrey Norkin795ba872018-03-06 13:24:14 -08006383int64_t timebase_units_to_ticks(const aom_rational_t *timebase, int64_t n) {
6384 return n * TICKS_PER_SEC * timebase->num / timebase->den;
6385}
6386
6387int64_t ticks_to_timebase_units(const aom_rational_t *timebase, int64_t n) {
6388 const int64_t round = TICKS_PER_SEC * timebase->num / 2 - 1;
6389 return (n * timebase->den + round) / timebase->num / TICKS_PER_SEC;
6390}
Tom Fineganf8d6a162018-08-21 10:47:55 -07006391
6392aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
6393 if (!cpi) return NULL;
6394
6395 uint8_t header_buf[512] = { 0 };
6396 const uint32_t sequence_header_size =
6397 write_sequence_header_obu(cpi, &header_buf[0]);
6398 assert(sequence_header_size <= sizeof(header_buf));
6399 if (sequence_header_size == 0) return NULL;
6400
6401 const size_t obu_header_size = 1;
6402 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
6403 const size_t payload_offset = obu_header_size + size_field_size;
6404
6405 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
6406 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
6407
6408 if (write_obu_header(OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
6409 obu_header_size) {
6410 return NULL;
6411 }
6412
6413 size_t coded_size_field_size = 0;
6414 if (aom_uleb_encode(sequence_header_size, size_field_size,
6415 &header_buf[obu_header_size],
6416 &coded_size_field_size) != 0) {
6417 return NULL;
6418 }
6419 assert(coded_size_field_size == size_field_size);
6420
6421 aom_fixed_buf_t *global_headers =
6422 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
6423 if (!global_headers) return NULL;
6424
6425 const size_t global_header_buf_size =
6426 obu_header_size + size_field_size + sequence_header_size;
6427
6428 global_headers->buf = malloc(global_header_buf_size);
6429 if (!global_headers->buf) {
6430 free(global_headers);
6431 return NULL;
6432 }
6433
6434 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
6435 global_headers->sz = global_header_buf_size;
6436 return global_headers;
6437}