blob: 385cc2bed645625239c92c3fc0b03325dce8f44d [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"
David Turner1539bb02019-01-24 15:28:13 +000036#if CONFIG_BITSTREAM_DEBUG
Yaowu Xufa3721d2018-07-30 14:38:49 -070037#include "aom_util/debug_util.h"
David Turner1539bb02019-01-24 15:28:13 +000038#endif // CONFIG_BITSTREAM_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
Ravi Chaudharyc5e74692018-10-08 16:05:38 +053049#include "av1/encoder/av1_multi_thread.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070050#include "av1/encoder/aq_complexity.h"
51#include "av1/encoder/aq_cyclicrefresh.h"
52#include "av1/encoder/aq_variance.h"
53#include "av1/encoder/bitstream.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070054#include "av1/encoder/context_tree.h"
55#include "av1/encoder/encodeframe.h"
56#include "av1/encoder/encodemv.h"
David Turner056f7cd2019-01-07 17:48:13 +000057#include "av1/encoder/encode_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070058#include "av1/encoder/encoder.h"
Angie Chiangf0fbf9d2017-03-15 15:01:22 -070059#include "av1/encoder/encodetxb.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070060#include "av1/encoder/ethread.h"
61#include "av1/encoder/firstpass.h"
Yaowu Xufa3721d2018-07-30 14:38:49 -070062#include "av1/encoder/grain_test_vectors.h"
RogerZhoucc5d35d2017-08-07 22:20:15 -070063#include "av1/encoder/hash_motion.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070064#include "av1/encoder/mbgraph.h"
David Turner0fa8c492019-02-06 16:38:13 +000065#include "av1/encoder/pass2_strategy.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070066#include "av1/encoder/picklpf.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070067#include "av1/encoder/pickrst.h"
Debargha Mukherjee7166f222017-09-05 21:32:42 -070068#include "av1/encoder/random.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070069#include "av1/encoder/ratectrl.h"
70#include "av1/encoder/rd.h"
Debargha Mukherjeedf713102018-10-02 12:33:32 -070071#include "av1/encoder/rdopt.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070072#include "av1/encoder/segmentation.h"
73#include "av1/encoder/speed_features.h"
Debargha Mukherjee347c64d2019-05-08 13:53:46 -070074#include "av1/encoder/tpl_model.h"
Yue Chen7cae98f2018-08-24 10:43:16 -070075#include "av1/encoder/reconinter_enc.h"
kyslov7b9d0d62018-12-21 11:12:26 -080076#include "av1/encoder/var_based_part.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070077
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000078#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000079
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080080#if CONFIG_ENTROPY_STATS
81FRAME_COUNTS aggregate_fc;
82#endif // CONFIG_ENTROPY_STATS
83
Yaowu Xuc27fc142016-08-22 16:08:15 -070084#define AM_SEGMENT_ID_INACTIVE 7
85#define AM_SEGMENT_ID_ACTIVE 0
86
Johannb0ef6ff2018-02-08 14:32:21 -080087// Whether to use high precision mv for altref computation.
88#define ALTREF_HIGH_PRECISION_MV 1
89
90// Q threshold for high precision mv. Choose a very high value for now so that
91// HIGH_PRECISION is always chosen.
92#define HIGH_PRECISION_MV_QTHRESH 200
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070093
Yaowu Xuc27fc142016-08-22 16:08:15 -070094// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070095#ifdef OUTPUT_YUV_SKINMAP
96FILE *yuv_skinmap_file = NULL;
97#endif
98#ifdef OUTPUT_YUV_REC
99FILE *yuv_rec_file;
100#define FILE_NAME_LEN 100
101#endif
102
Yaowu Xuf883b422016-08-30 14:01:10 -0700103static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700104 switch (mode) {
105 case NORMAL:
106 *hr = 1;
107 *hs = 1;
108 break;
109 case FOURFIVE:
110 *hr = 4;
111 *hs = 5;
112 break;
113 case THREEFIVE:
114 *hr = 3;
115 *hs = 5;
116 break;
117 case ONETWO:
118 *hr = 1;
119 *hs = 2;
120 break;
121 default:
122 *hr = 1;
123 *hs = 1;
124 assert(0);
125 break;
126 }
127}
128
129// Mark all inactive blocks as active. Other segmentation features may be set
130// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700131static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700132 unsigned char *const seg_map = cpi->segmentation_map;
133 int i;
134 if (cpi->active_map.enabled || cpi->active_map.update)
135 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
136 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
137 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
138}
139
Yaowu Xuf883b422016-08-30 14:01:10 -0700140static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700141 struct segmentation *const seg = &cpi->common.seg;
142 unsigned char *const seg_map = cpi->segmentation_map;
143 const unsigned char *const active_map = cpi->active_map.map;
144 int i;
145
146 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
147
148 if (frame_is_intra_only(&cpi->common)) {
149 cpi->active_map.enabled = 0;
150 cpi->active_map.update = 1;
151 }
152
153 if (cpi->active_map.update) {
154 if (cpi->active_map.enabled) {
155 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
156 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700157 av1_enable_segmentation(seg);
158 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700159 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
160 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
161 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
162 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
163
164 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
165 -MAX_LOOP_FILTER);
166 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
167 -MAX_LOOP_FILTER);
168 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
169 -MAX_LOOP_FILTER);
170 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
171 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700172 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700173 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700174 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
175 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
176 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
177 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700178 if (seg->enabled) {
179 seg->update_data = 1;
180 seg->update_map = 1;
181 }
182 }
183 cpi->active_map.update = 0;
184 }
185}
186
Yaowu Xuf883b422016-08-30 14:01:10 -0700187int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
188 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700189 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
190 unsigned char *const active_map_8x8 = cpi->active_map.map;
191 const int mi_rows = cpi->common.mi_rows;
192 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700193 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
194 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700195 cpi->active_map.update = 1;
196 if (new_map_16x16) {
197 int r, c;
198 for (r = 0; r < mi_rows; ++r) {
199 for (c = 0; c < mi_cols; ++c) {
200 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700201 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700202 ? AM_SEGMENT_ID_ACTIVE
203 : AM_SEGMENT_ID_INACTIVE;
204 }
205 }
206 cpi->active_map.enabled = 1;
207 } else {
208 cpi->active_map.enabled = 0;
209 }
210 return 0;
211 } else {
212 return -1;
213 }
214}
215
Yaowu Xuf883b422016-08-30 14:01:10 -0700216int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
217 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700218 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
219 new_map_16x16) {
220 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
221 const int mi_rows = cpi->common.mi_rows;
222 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700223 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
224 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
225
Yaowu Xuc27fc142016-08-22 16:08:15 -0700226 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
227 if (cpi->active_map.enabled) {
228 int r, c;
229 for (r = 0; r < mi_rows; ++r) {
230 for (c = 0; c < mi_cols; ++c) {
231 // Cyclic refresh segments are considered active despite not having
232 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700233 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700234 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
235 }
236 }
237 }
238 return 0;
239 } else {
240 return -1;
241 }
242}
243
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800244// Compute the horizontal frequency components' energy in a frame
245// by calculuating the 16x4 Horizontal DCT. This is to be used to
246// decide the superresolution parameters.
Yaowu Xubedbf4f2019-05-01 17:54:36 -0700247static void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800248 uint64_t freq_energy[16] = { 0 };
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800249 const YV12_BUFFER_CONFIG *buf = cpi->source;
250 const int bd = cpi->td.mb.e_mbd.bd;
251 const int width = buf->y_crop_width;
252 const int height = buf->y_crop_height;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800253 DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800254 int n = 0;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800255 memset(freq_energy, 0, sizeof(freq_energy));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800256 if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
257 const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
258 for (int i = 0; i < height - 4; i += 4) {
259 for (int j = 0; j < width - 16; j += 16) {
260 av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
261 H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800262 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800263 const uint64_t this_energy =
264 ((int64_t)coeff[k] * coeff[k]) +
265 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
266 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
267 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800268 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800269 }
270 n++;
271 }
272 }
273 } else {
Debargha Mukherjeeac28c722018-11-14 22:09:46 -0800274 assert(bd == 8);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800275 DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800276 for (int i = 0; i < height - 4; i += 4) {
277 for (int j = 0; j < width - 16; j += 16) {
278 for (int ii = 0; ii < 4; ++ii)
279 for (int jj = 0; jj < 16; ++jj)
280 src16[ii * 16 + jj] =
281 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800282 av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800283 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800284 const uint64_t this_energy =
285 ((int64_t)coeff[k] * coeff[k]) +
286 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
287 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
288 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800289 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800290 }
291 n++;
292 }
293 }
294 }
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800295 if (n) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800296 for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800297 // Convert to cumulative energy
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800298 for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800299 } else {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800300 for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800301 }
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800302}
303
Yaowu Xu45295c32018-03-29 12:06:10 -0700304static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
305 int cur_frame_force_integer_mv) {
James Zern01a9d702017-08-25 19:09:33 +0000306 MACROBLOCK *const mb = &cpi->td.mb;
Hui Su50361152018-03-02 11:01:42 -0800307 cpi->common.allow_high_precision_mv =
308 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Rupert Swarbricka84faf22017-12-11 13:56:40 +0000309 const int copy_hp =
310 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Jingning Hanf050fc12018-03-09 14:53:33 -0800311 int *(*src)[2] = copy_hp ? &mb->nmvcost_hp : &mb->nmvcost;
312 mb->mv_cost_stack = *src;
James Zern01a9d702017-08-25 19:09:33 +0000313}
314
Yaowu Xuf883b422016-08-30 14:01:10 -0700315static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800316 const AV1_COMMON *const cm = &cpi->common;
317
Yaowu Xuf883b422016-08-30 14:01:10 -0700318 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700319 return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100320#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800321 if (cm->options && cm->options->ext_partition)
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100322#endif
323 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
324 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700325
Yaowu Xuf883b422016-08-30 14:01:10 -0700326 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700327
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100328// TODO(any): Possibly could improve this with a heuristic.
329#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800330 if (cm->options && !cm->options->ext_partition) return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100331#endif
Urvang Joshie4530f82018-01-09 11:43:37 -0800332
Urvang Joshiaab74432018-06-01 12:06:22 -0700333 // When superres / resize is on, 'cm->width / height' can change between
Hui Su3e3b9342019-04-12 18:27:28 +0000334 // calls, so we don't apply this heuristic there.
335 // Things break if superblock size changes between the first pass and second
336 // pass encoding, which is why this heuristic is not configured as a
337 // speed-feature.
Urvang Joshiaab74432018-06-01 12:06:22 -0700338 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
Hui Su3e3b9342019-04-12 18:27:28 +0000339 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 1) {
340 return AOMMIN(cm->width, cm->height) > 480 ? BLOCK_128X128 : BLOCK_64X64;
Urvang Joshie4530f82018-01-09 11:43:37 -0800341 }
342
Yaowu Xuc27fc142016-08-22 16:08:15 -0700343 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344}
345
Yaowu Xuf883b422016-08-30 14:01:10 -0700346static void setup_frame(AV1_COMP *cpi) {
347 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800348 // Set up entropy context depending on frame type. The decoder mandates
349 // the use of the default context, index 0, for keyframes and inter
350 // frames where the error_resilient_mode or intra_only flag is set. For
351 // other inter-frames the encoder currently uses only two contexts;
352 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500353
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700354 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
David Turnera7f133c2019-01-22 14:47:16 +0000355 cpi->ext_use_primary_ref_none) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700356 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700357 }
358
Hui Sueb4b7de2019-04-03 11:00:18 -0700359 if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
360 frame_is_sframe(cm)) {
361 if (!cpi->seq_params_locked) {
362 set_sb_size(&cm->seq_params, select_sb_size(cpi));
363 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700364 } else {
David Turnera21966b2018-12-05 14:48:49 +0000365 const RefCntBuffer *const primary_ref_buf = get_primary_ref_frame_buf(cm);
366 if (primary_ref_buf == NULL) {
David Barkercc615a82018-03-19 14:38:51 +0000367 av1_setup_past_independence(cm);
368 cm->seg.update_map = 1;
369 cm->seg.update_data = 1;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700370 } else {
David Turnera21966b2018-12-05 14:48:49 +0000371 *cm->fc = primary_ref_buf->frame_context;
Thomas Daededa4d8b92017-06-05 15:44:14 -0700372 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700373 }
374
David Turnerbc0993e2019-02-15 14:42:23 +0000375 av1_zero(cm->cur_frame->interp_filter_selected);
David Turnera21966b2018-12-05 14:48:49 +0000376 cm->prev_frame = get_primary_ref_frame_buf(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700377 cpi->vaq_refresh = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700378}
379
chiyotsaia7091f12019-08-09 16:48:27 -0700380static void enc_set_mb_mi(AV1_COMMON *cm, int width, int height) {
381 // Ensure that the decoded width and height are both multiples of
382 // 8 luma pixels (note: this may only be a multiple of 4 chroma pixels if
383 // subsampling is used).
384 // This simplifies the implementation of various experiments,
385 // eg. cdef, which operates on units of 8x8 luma pixels.
386 const int aligned_width = ALIGN_POWER_OF_TWO(width, 3);
387 const int aligned_height = ALIGN_POWER_OF_TWO(height, 3);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700388
chiyotsaia7091f12019-08-09 16:48:27 -0700389 cm->mi_cols = aligned_width >> MI_SIZE_LOG2;
390 cm->mi_rows = aligned_height >> MI_SIZE_LOG2;
391 cm->mi_stride = calc_mi_size(cm->mi_cols);
392
393 cm->mb_cols = (cm->mi_cols + 2) >> 2;
394 cm->mb_rows = (cm->mi_rows + 2) >> 2;
395 cm->MBs = cm->mb_rows * cm->mb_cols;
396
397 const int is_4k_or_larger = AOMMIN(width, height) >= 2160;
398
399 cm->mi_alloc_bsize = is_4k_or_larger ? BLOCK_8X8 : BLOCK_4X4;
400 const int mi_alloc_size_1d = mi_size_wide[cm->mi_alloc_bsize];
401 cm->mi_alloc_rows = (cm->mi_rows + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
402 cm->mi_alloc_cols = (cm->mi_cols + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
403 cm->mi_alloc_stride =
404 (cm->mi_stride + mi_alloc_size_1d - 1) / mi_alloc_size_1d;
405
406 assert(mi_size_wide[cm->mi_alloc_bsize] == mi_size_high[cm->mi_alloc_bsize]);
407
408#if CONFIG_LPF_MASK
chiyotsaia13c19f2019-08-15 10:12:57 -0700409 av1_alloc_loop_filter_mask(cm);
chiyotsaia7091f12019-08-09 16:48:27 -0700410#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700411}
412
chiyotsaia7091f12019-08-09 16:48:27 -0700413static void enc_setup_mi(AV1_COMMON *cm) {
414 const int mi_grid_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
415 memset(cm->mi, 0, cm->mi_alloc_size * sizeof(*cm->mi));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700416
chiyotsaia7091f12019-08-09 16:48:27 -0700417 memset(cm->mi_grid_base, 0, mi_grid_size * sizeof(*cm->mi_grid_base));
418}
419
420static int enc_alloc_mi(AV1_COMMON *cm) {
421 const int mi_grid_size = cm->mi_stride * calc_mi_size(cm->mi_rows);
422 const int alloc_size_1d = mi_size_wide[cm->mi_alloc_bsize];
423 const int alloc_mi_size =
424 cm->mi_alloc_stride * (calc_mi_size(cm->mi_rows) / alloc_size_1d);
425
426 if (cm->mi_alloc_size < alloc_mi_size || cm->mi_grid_size < mi_grid_size) {
427 cm->free_mi(cm);
428
429 cm->mi = aom_calloc(alloc_mi_size, sizeof(*cm->mi));
430 if (!cm->mi) return 1;
431 cm->mi_alloc_size = alloc_mi_size;
432
433 cm->mi_grid_base =
434 (MB_MODE_INFO **)aom_calloc(mi_grid_size, sizeof(MB_MODE_INFO *));
435 if (!cm->mi_grid_base) return 1;
436 cm->mi_grid_size = mi_grid_size;
437 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700438
439 return 0;
440}
441
Cheng Chen46f30c72017-09-07 11:13:33 -0700442static void enc_free_mi(AV1_COMMON *cm) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700443 aom_free(cm->mi);
444 cm->mi = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700445 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700446 cm->mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700447 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700448}
449
Yaowu Xuf883b422016-08-30 14:01:10 -0700450void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700451 av1_rtcd();
452 aom_dsp_rtcd();
453 aom_scale_rtcd();
454 av1_init_intra_predictors();
455 av1_init_me_luts();
456 av1_rc_init_minq_luts();
457 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700458}
459
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700460static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
Remya0cce44c2019-08-16 11:57:24 +0530461 if (cpi->mbmi_ext_frame_base) {
462 aom_free(cpi->mbmi_ext_frame_base);
463 cpi->mbmi_ext_frame_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700464 }
465}
466
467static void alloc_context_buffers_ext(AV1_COMP *cpi) {
468 AV1_COMMON *cm = &cpi->common;
chiyotsaia7091f12019-08-09 16:48:27 -0700469 const int new_ext_mi_size = cm->mi_alloc_rows * cm->mi_alloc_cols;
chiyotsai426c0662019-08-05 16:15:11 -0700470
chiyotsaia7091f12019-08-09 16:48:27 -0700471 if (new_ext_mi_size > cpi->mi_ext_alloc_size) {
472 dealloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +0530473 CHECK_MEM_ERROR(
474 cm, cpi->mbmi_ext_frame_base,
475 aom_calloc(new_ext_mi_size, sizeof(*cpi->mbmi_ext_frame_base)));
chiyotsaia7091f12019-08-09 16:48:27 -0700476 cpi->mi_ext_alloc_size = new_ext_mi_size;
477 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700478}
479
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800480static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
481 pars->num_cr_points = 0;
482 pars->cr_mult = 0;
483 pars->cr_luma_mult = 0;
484 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
485 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
486 pars->num_cb_points = 0;
487 pars->cb_mult = 0;
488 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800489 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800490 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
491 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
492}
493
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800494static void update_film_grain_parameters(struct AV1_COMP *cpi,
495 const AV1EncoderConfig *oxcf) {
496 AV1_COMMON *const cm = &cpi->common;
497 cpi->oxcf = *oxcf;
498
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700499 if (cpi->film_grain_table) {
500 aom_film_grain_table_free(cpi->film_grain_table);
501 aom_free(cpi->film_grain_table);
502 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700503 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700504
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800505 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700506 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000507 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800508 memcpy(&cm->film_grain_params,
509 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
510 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800511 if (oxcf->monochrome)
512 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700513 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
514 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800515 cm->film_grain_params.clip_to_restricted_range = 0;
516 }
517 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700518 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800519 cm->seq_params.film_grain_params_present = 1;
520
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700521 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
522 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700523
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700524 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700525 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800526 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800527#if CONFIG_DENOISE
528 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
529#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700530 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800531#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800532 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
533 }
534}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800535
Yaowu Xuf883b422016-08-30 14:01:10 -0700536static void dealloc_compressor_data(AV1_COMP *cpi) {
537 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000538 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700539
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700540 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700541
Yaowu Xuf883b422016-08-30 14:01:10 -0700542 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543 cpi->tile_data = NULL;
544
545 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700546 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700547 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700548
Yaowu Xuf883b422016-08-30 14:01:10 -0700549 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700550 cpi->cyclic_refresh = NULL;
551
Yaowu Xuf883b422016-08-30 14:01:10 -0700552 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700553 cpi->active_map.map = NULL;
554
sdengc23c7f12019-06-11 16:56:50 -0700555 aom_free(cpi->ssim_rdmult_scaling_factors);
556 cpi->ssim_rdmult_scaling_factors = NULL;
557
sdengf46a1062019-08-04 18:43:50 -0700558 aom_free(cpi->tpl_rdmult_scaling_factors);
559 cpi->tpl_rdmult_scaling_factors = NULL;
560
561 aom_free(cpi->tpl_sb_rdmult_scaling_factors);
562 cpi->tpl_sb_rdmult_scaling_factors = NULL;
563
Jingning Hand064cf02017-06-01 10:00:39 -0700564 aom_free(cpi->td.mb.above_pred_buf);
565 cpi->td.mb.above_pred_buf = NULL;
566
567 aom_free(cpi->td.mb.left_pred_buf);
568 cpi->td.mb.left_pred_buf = NULL;
569
570 aom_free(cpi->td.mb.wsrc_buf);
571 cpi->td.mb.wsrc_buf = NULL;
572
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530573 aom_free(cpi->td.mb.inter_modes_info);
574 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530575
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530576 for (int i = 0; i < 2; i++)
577 for (int j = 0; j < 2; j++) {
578 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
579 cpi->td.mb.hash_value_buffer[i][j] = NULL;
580 }
Jingning Hand064cf02017-06-01 10:00:39 -0700581 aom_free(cpi->td.mb.mask_buf);
582 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700583
Jingning Han6cc1fd32017-10-13 09:05:36 -0700584 aom_free(cm->tpl_mvs);
585 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700586
Remya0cce44c2019-08-16 11:57:24 +0530587 aom_free(cpi->td.mb.mbmi_ext);
588 cpi->td.mb.mbmi_ext = NULL;
589
Yaowu Xuf883b422016-08-30 14:01:10 -0700590 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700591 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700592 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700593
Yaowu Xuf883b422016-08-30 14:01:10 -0700594 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700595 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800596 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700597 aom_free_frame_buffer(&cpi->scaled_source);
598 aom_free_frame_buffer(&cpi->scaled_last_source);
599 aom_free_frame_buffer(&cpi->alt_ref_buffer);
Cheng Chen7abe3132019-06-19 11:55:28 -0700600 aom_free_frame_buffer(&cpi->source_kf_buffer);
Yaowu Xuf883b422016-08-30 14:01:10 -0700601 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700602
Yaowu Xuf883b422016-08-30 14:01:10 -0700603 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604 cpi->tile_tok[0][0] = 0;
605
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530606 aom_free(cpi->tplist[0][0]);
607 cpi->tplist[0][0] = NULL;
608
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000609 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610
hui sud9a812b2017-07-06 14:34:37 -0700611 aom_free(cpi->td.mb.palette_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700612 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700613 aom_free(cpi->td.mb.tmp_conv_dst);
614 for (int j = 0; j < 2; ++j) {
615 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
616 }
617
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700618#if CONFIG_DENOISE
619 if (cpi->denoise_and_model) {
620 aom_denoise_and_model_free(cpi->denoise_and_model);
621 cpi->denoise_and_model = NULL;
622 }
623#endif
624 if (cpi->film_grain_table) {
625 aom_film_grain_table_free(cpi->film_grain_table);
626 cpi->film_grain_table = NULL;
627 }
Hui Suc3a8d372019-05-28 15:52:45 -0700628
629 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
630 aom_free(cpi->level_info[i]);
631 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632}
633
Yaowu Xuf883b422016-08-30 14:01:10 -0700634static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700635 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700636 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200638 // Stores a snapshot of key state variables which can subsequently be
639 // restored with a call to av1_restore_coding_context. These functions are
640 // intended for use in a re-code loop in av1_compress_frame where the
641 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800642 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
643 av1_copy(cc->nmv_costs, cpi->nmv_costs);
644 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000645
Yaowu Xuc27fc142016-08-22 16:08:15 -0700646 cc->fc = *cm->fc;
647}
648
Yaowu Xuf883b422016-08-30 14:01:10 -0700649static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700650 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700651 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700652
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200653 // Restore key state variables to the snapshot state stored in the
654 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800655 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
656 av1_copy(cpi->nmv_costs, cc->nmv_costs);
657 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000658
Yaowu Xuc27fc142016-08-22 16:08:15 -0700659 *cm->fc = cc->fc;
660}
661
Yaowu Xuf883b422016-08-30 14:01:10 -0700662static void configure_static_seg_features(AV1_COMP *cpi) {
663 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700664 const RATE_CONTROL *const rc = &cpi->rc;
665 struct segmentation *const seg = &cm->seg;
666
667 int high_q = (int)(rc->avg_q > 48.0);
668 int qi_delta;
669
670 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000671 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700672 // Clear down the global segmentation map
673 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
674 seg->update_map = 0;
675 seg->update_data = 0;
676 cpi->static_mb_pct = 0;
677
678 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700679 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700680
681 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700682 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700683 } else if (cpi->refresh_alt_ref_frame) {
684 // If this is an alt ref frame
685 // Clear down the global segmentation map
686 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
687 seg->update_map = 0;
688 seg->update_data = 0;
689 cpi->static_mb_pct = 0;
690
691 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 av1_disable_segmentation(seg);
693 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700695#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696 // Scan frames from current to arf frame.
697 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700698 av1_update_mbgraph_stats(cpi);
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700699#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700700
701 // If segmentation was enabled set those features needed for the
702 // arf itself.
703 if (seg->enabled) {
704 seg->update_map = 1;
705 seg->update_data = 1;
706
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700707 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
708 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700709 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700710 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
711 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
712 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
713 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
714
715 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
716 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
717 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
718 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700719
Yaowu Xuf883b422016-08-30 14:01:10 -0700720 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700721 }
722 } else if (seg->enabled) {
723 // All other frames if segmentation has been enabled
724
725 // First normal frame in a valid gf or alt ref group
726 if (rc->frames_since_golden == 0) {
727 // Set up segment features for normal frames in an arf group
728 if (rc->source_alt_ref_active) {
729 seg->update_map = 0;
730 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700731
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700732 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
733 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
735 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736
Cheng Chend8184da2017-09-26 18:15:22 -0700737 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
738 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
739 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
740 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
741
742 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
743 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
744 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
745 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700746
747 // Segment coding disabled for compred testing
748 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700749 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
750 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
751 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752 }
753 } else {
754 // Disable segmentation and clear down features if alt ref
755 // is not active for this group
756
Yaowu Xuf883b422016-08-30 14:01:10 -0700757 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700758
759 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
760
761 seg->update_map = 0;
762 seg->update_data = 0;
763
Yaowu Xuf883b422016-08-30 14:01:10 -0700764 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700765 }
766 } else if (rc->is_src_frame_alt_ref) {
767 // Special case where we are coding over the top of a previous
768 // alt ref frame.
769 // Segment coding disabled for compred testing
770
771 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700772 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
773 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774
775 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700776 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
777 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
778 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
779 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780
781 // Skip all MBs if high Q (0,0 mv and skip coeffs)
782 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700783 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
784 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700785 }
786 // Enable data update
787 seg->update_data = 1;
788 } else {
789 // All other frames.
790
791 // No updates.. leave things as they are.
792 seg->update_map = 0;
793 seg->update_data = 0;
794 }
795 }
796}
797
Yaowu Xuf883b422016-08-30 14:01:10 -0700798static void update_reference_segmentation_map(AV1_COMP *cpi) {
799 AV1_COMMON *const cm = &cpi->common;
chiyotsai2202ba02019-07-12 16:09:21 -0700800 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +0000801 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700802 int row, col;
803
804 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700805 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700807 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
808 cache[0] = mi_4x4[0]->segment_id;
809 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700810 cache_ptr += cm->mi_cols;
811 }
812}
813
Yaowu Xuf883b422016-08-30 14:01:10 -0700814static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
815 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700816 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700817 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530819 if (!cpi->lookahead) {
820 int is_scale = (oxcf->resize_mode || oxcf->superres_mode);
Satish Kumar Suman29909962019-01-09 10:31:21 +0530821 cpi->lookahead = av1_lookahead_init(
822 oxcf->width, oxcf->height, seq_params->subsampling_x,
823 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530824 oxcf->lag_in_frames, oxcf->border_in_pixels, is_scale);
825 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700827 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700828 "Failed to allocate lag buffers");
829
830 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700831 if (aom_realloc_frame_buffer(
832 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
833 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530834 seq_params->use_highbitdepth, oxcf->border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700835 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700836 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700837 "Failed to allocate altref buffer");
Cheng Chen7abe3132019-06-19 11:55:28 -0700838
839 // Allocate frame buffer to hold source frame whey key frame filtering
840 // is applied.
841 if (aom_realloc_frame_buffer(
842 &cpi->source_kf_buffer, oxcf->width, oxcf->height,
843 seq_params->subsampling_x, seq_params->subsampling_y,
844 seq_params->use_highbitdepth, oxcf->border_in_pixels,
845 cm->byte_alignment, NULL, NULL, NULL))
846 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
847 "Failed to allocate altref buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700848}
849
Yaowu Xuf883b422016-08-30 14:01:10 -0700850static void alloc_util_frame_buffers(AV1_COMP *cpi) {
851 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700852 const SequenceHeader *const seq_params = &cm->seq_params;
853 if (aom_realloc_frame_buffer(
854 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
855 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530856 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700857 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700858 "Failed to allocate last frame buffer");
859
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700860 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800861 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700862 cm->superres_upscaled_height, seq_params->subsampling_x,
863 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +0530864 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800865 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800866 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700867
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700868 if (aom_realloc_frame_buffer(
869 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
870 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530871 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700872 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873 "Failed to allocate scaled source buffer");
874
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700875 if (aom_realloc_frame_buffer(
876 &cpi->scaled_last_source, cm->width, cm->height,
877 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530878 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700879 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700880 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700881 "Failed to allocate scaled last source buffer");
882}
883
Cheng Chen46f30c72017-09-07 11:13:33 -0700884static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700885 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000886 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700887
chiyotsaia7091f12019-08-09 16:48:27 -0700888 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
889 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
890 "Failed to allocate context buffers");
891 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700892
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530893 int mi_rows_aligned_to_sb =
894 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
895 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
896
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700897 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700898
Yaowu Xuc27fc142016-08-22 16:08:15 -0700899 alloc_context_buffers_ext(cpi);
900
Yaowu Xuf883b422016-08-30 14:01:10 -0700901 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700902
903 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000904 unsigned int tokens =
905 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700906 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700907 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700908 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530909 aom_free(cpi->tplist[0][0]);
910
911 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
912 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
913 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700914
Yaowu Xuf883b422016-08-30 14:01:10 -0700915 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700916}
917
Yaowu Xuf883b422016-08-30 14:01:10 -0700918void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700919 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700920 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700921}
922
Hui Suef139e12019-05-20 15:51:22 -0700923double av1_get_compression_ratio(const AV1_COMMON *const cm,
924 size_t encoded_frame_size) {
925 const int upscaled_width = cm->superres_upscaled_width;
926 const int height = cm->height;
927 const int luma_pic_size = upscaled_width * height;
928 const SequenceHeader *const seq_params = &cm->seq_params;
929 const BITSTREAM_PROFILE profile = seq_params->profile;
930 const int pic_size_profile_factor =
931 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
932 encoded_frame_size =
933 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
934 const size_t uncompressed_frame_size =
935 (luma_pic_size * pic_size_profile_factor) >> 3;
936 return uncompressed_frame_size / (double)encoded_frame_size;
937}
938
Yunqing Wang75e20e82018-06-16 12:10:48 -0700939static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200940 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200941 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200942
943 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200944
945 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200946 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200947 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200948 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
949 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200950 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000951 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
952 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200953 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200954 cm->uniform_tile_spacing_flag = 0;
955 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
956 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200957 size_sb = cpi->oxcf.tile_widths[j++];
958 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000959 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200960 }
961 cm->tile_cols = i;
962 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200963 }
964 av1_calculate_tile_cols(cm);
965
966 // configure tile rows
967 if (cm->uniform_tile_spacing_flag) {
968 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
969 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200970 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000971 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
972 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200973 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200974 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
975 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200976 size_sb = cpi->oxcf.tile_heights[j++];
977 if (j >= cpi->oxcf.tile_height_count) j = 0;
978 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200979 }
980 cm->tile_rows = i;
981 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200982 }
983 av1_calculate_tile_rows(cm);
984}
985
Yaowu Xuf883b422016-08-30 14:01:10 -0700986static void update_frame_size(AV1_COMP *cpi) {
987 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700988 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
989
chiyotsaia7091f12019-08-09 16:48:27 -0700990 // We need to reallocate the context buffers here in case we need more mis.
991 if (av1_alloc_context_buffers(cm, cm->width, cm->height)) {
992 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
993 "Failed to allocate context buffers");
994 }
Yaowu Xuf883b422016-08-30 14:01:10 -0700995 av1_init_context_buffers(cm);
chiyotsaia7091f12019-08-09 16:48:27 -0700996
Luc Trudeau1e84af52017-11-25 15:00:28 -0500997 av1_init_macroblockd(cm, xd, NULL);
chiyotsai426c0662019-08-05 16:15:11 -0700998
chiyotsaia7091f12019-08-09 16:48:27 -0700999 const int ext_mi_size = cm->mi_alloc_rows * cm->mi_alloc_cols;
1000 alloc_context_buffers_ext(cpi);
Remya0cce44c2019-08-16 11:57:24 +05301001 memset(cpi->mbmi_ext_frame_base, 0,
1002 ext_mi_size * sizeof(*cpi->mbmi_ext_frame_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -07001003 set_tile_info(cpi);
1004}
1005
Yaowu Xuf883b422016-08-30 14:01:10 -07001006static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001007 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -07001008 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +00001009 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -07001010 cpi->rate_index = 0;
1011 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001012}
1013
Debargha Mukherjee57498692018-05-11 13:29:31 -07001014static INLINE int does_level_match(int width, int height, double fps,
1015 int lvl_width, int lvl_height,
1016 double lvl_fps, int lvl_dim_mult) {
1017 const int64_t lvl_luma_pels = lvl_width * lvl_height;
1018 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
1019 const int64_t luma_pels = width * height;
1020 const double display_sample_rate = luma_pels * fps;
1021 return luma_pels <= lvl_luma_pels &&
1022 display_sample_rate <= lvl_display_sample_rate &&
1023 width <= lvl_width * lvl_dim_mult &&
1024 height <= lvl_height * lvl_dim_mult;
1025}
1026
Andrey Norkin26495512018-06-20 17:13:11 -07001027static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -07001028 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001029 // TODO(any): This is a placeholder function that only addresses dimensions
1030 // and max display sample rates.
1031 // Need to add checks for max bit rate, max decoded luma sample rate, header
1032 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -07001033 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001034 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
1035 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001036 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001037 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1038 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001039 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001040 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1041 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001042 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001043 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1044 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001045 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001046 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1047 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001048 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001049 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1050 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001051 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001052 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1053 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001054 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001055 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1056 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001057 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001058 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1059 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001060 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001061 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1062 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001063 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001064 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1065 8192, 4352, 60.0, 2)) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001066 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1067 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001068 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001069 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1070 16384, 8704, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001071 level = SEQ_LEVEL_7_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001072 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1073 16384, 8704, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001074 level = SEQ_LEVEL_7_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001075 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1076 16384, 8704, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001077 level = SEQ_LEVEL_7_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001078 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001079 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001080 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001081 // Set the maximum parameters for bitrate and buffer size for this profile,
1082 // level, and tier
Yaowu Xu7e450882019-04-30 15:09:18 -07001083 cm->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001084 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001085 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1086 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001087 if (cm->op_params[i].bitrate == 0)
1088 aom_internal_error(
1089 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1090 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001091 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001092 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001093 }
1094}
1095
Andrey Norkin26495512018-06-20 17:13:11 -07001096static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
Marco Paniconi67142112019-07-24 15:00:31 -07001097 const AV1EncoderConfig *oxcf, int use_svc) {
Yaowu Xu2a9ac432019-08-06 14:21:17 -07001098 seq->still_picture = (oxcf->force_video_mode == 0) && (oxcf->limit == 1);
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001099 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001100 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001101 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001102 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001103 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001104 seq->frame_id_numbers_present_flag =
1105 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
Marco Paniconi67142112019-07-24 15:00:31 -07001106 !oxcf->large_scale_tile && oxcf->error_resilient_mode && !use_svc;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001107 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001108 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001109 seq->force_screen_content_tools = 2;
1110 seq->force_integer_mv = 2;
1111 }
David Turnerebf96f42018-11-14 16:57:57 +00001112 seq->order_hint_info.order_hint_bits_minus_1 =
1113 seq->order_hint_info.enable_order_hint
1114 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1115 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001116
David Turner760a2f42018-12-07 15:25:36 +00001117 seq->max_frame_width =
1118 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1119 seq->max_frame_height = oxcf->forced_max_frame_height
1120 ? oxcf->forced_max_frame_height
1121 : oxcf->height;
1122 seq->num_bits_width =
1123 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1124 seq->num_bits_height =
1125 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1126 assert(seq->num_bits_width <= 16);
1127 assert(seq->num_bits_height <= 16);
1128
1129 seq->frame_id_length = FRAME_ID_LENGTH;
1130 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1131
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001132 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001133 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1134 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001135 seq->order_hint_info.enable_order_hint;
1136 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1137 seq->order_hint_info.enable_ref_frame_mvs &=
1138 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001139 seq->enable_superres = oxcf->enable_superres;
1140 seq->enable_cdef = oxcf->enable_cdef;
1141 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001142 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001143 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1144 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001145 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001146 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001147
Andrey Norkin26495512018-06-20 17:13:11 -07001148 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001149
1150 if (seq->operating_points_cnt_minus_1 == 0) {
1151 seq->operating_point_idc[0] = 0;
1152 } else {
1153 // Set operating_point_idc[] such that for the i-th operating point the
1154 // first (operating_points_cnt-i) spatial layers and the first temporal
1155 // layer are decoded Note that highest quality operating point should come
1156 // first
1157 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1158 seq->operating_point_idc[i] =
1159 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1160 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001161}
1162
Yaowu Xuf883b422016-08-30 14:01:10 -07001163static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1164 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001165
1166 cpi->oxcf = *oxcf;
1167 cpi->framerate = oxcf->init_framerate;
1168
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001169 cm->seq_params.profile = oxcf->profile;
1170 cm->seq_params.bit_depth = oxcf->bit_depth;
1171 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1172 cm->seq_params.color_primaries = oxcf->color_primaries;
1173 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1174 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001175 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001176 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1177 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001178 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001179 cm->timing_info.num_units_in_display_tick =
1180 oxcf->timing_info.num_units_in_display_tick;
1181 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1182 cm->timing_info.equal_picture_interval =
1183 oxcf->timing_info.equal_picture_interval;
1184 cm->timing_info.num_ticks_per_picture =
1185 oxcf->timing_info.num_ticks_per_picture;
1186
Andrey Norkin26495512018-06-20 17:13:11 -07001187 cm->seq_params.display_model_info_present_flag =
1188 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001189 cm->seq_params.decoder_model_info_present_flag =
1190 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001191 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001192 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001193 cm->buffer_model.num_units_in_decoding_tick =
1194 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001195 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07001196 av1_set_aom_dec_model_info(&cm->buffer_model);
1197 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07001198 } else if (cm->timing_info_present &&
1199 cm->timing_info.equal_picture_interval &&
1200 !cm->seq_params.decoder_model_info_present_flag) {
1201 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07001202 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001203 } else {
1204 cm->op_params[0].initial_display_delay =
1205 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001206 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001207
Tom Fineganf8d6a162018-08-21 10:47:55 -07001208 if (cm->seq_params.monochrome) {
1209 cm->seq_params.subsampling_x = 1;
1210 cm->seq_params.subsampling_y = 1;
1211 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1212 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1213 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1214 cm->seq_params.subsampling_x = 0;
1215 cm->seq_params.subsampling_y = 0;
1216 } else {
1217 if (cm->seq_params.profile == 0) {
1218 cm->seq_params.subsampling_x = 1;
1219 cm->seq_params.subsampling_y = 1;
1220 } else if (cm->seq_params.profile == 1) {
1221 cm->seq_params.subsampling_x = 0;
1222 cm->seq_params.subsampling_y = 0;
1223 } else {
1224 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1225 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1226 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1227 } else {
1228 cm->seq_params.subsampling_x = 1;
1229 cm->seq_params.subsampling_y = 0;
1230 }
1231 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001232 }
1233
Yaowu Xuc27fc142016-08-22 16:08:15 -07001234 cm->width = oxcf->width;
1235 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001236 set_sb_size(&cm->seq_params,
1237 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001238 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001239
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001240 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001241
1242 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001243 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001244
Marco Paniconi67142112019-07-24 15:00:31 -07001245 cpi->use_svc = 0;
Marco Paniconid8574e32019-08-04 21:30:12 -07001246 cpi->svc.external_ref_frame_config = 0;
1247 cpi->svc.non_reference_frame = 0;
Marco Paniconi67142112019-07-24 15:00:31 -07001248 cm->number_spatial_layers = 1;
1249 cm->number_temporal_layers = 1;
1250
Yaowu Xuc27fc142016-08-22 16:08:15 -07001251 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001252 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001253
1254 cpi->static_mb_pct = 0;
1255 cpi->ref_frame_flags = 0;
1256
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001257 // Reset resize pending flags
1258 cpi->resize_pending_width = 0;
1259 cpi->resize_pending_height = 0;
1260
Yaowu Xuc27fc142016-08-22 16:08:15 -07001261 init_buffer_indices(cpi);
1262}
1263
1264static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001265 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001266 const int64_t bandwidth = oxcf->target_bandwidth;
1267 const int64_t starting = oxcf->starting_buffer_level_ms;
1268 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1269 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1270
1271 rc->starting_buffer_level = starting * bandwidth / 1000;
1272 rc->optimal_buffer_level =
1273 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1274 rc->maximum_buffer_size =
1275 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1276}
1277
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001278#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1279 cpi->fn_ptr[BT].sdf = SDF; \
1280 cpi->fn_ptr[BT].sdaf = SDAF; \
1281 cpi->fn_ptr[BT].vf = VF; \
1282 cpi->fn_ptr[BT].svf = SVF; \
1283 cpi->fn_ptr[BT].svaf = SVAF; \
1284 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1285 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001286 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001287
1288#define MAKE_BFP_SAD_WRAPPER(fnname) \
1289 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1290 int source_stride, \
1291 const uint8_t *ref_ptr, int ref_stride) { \
1292 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1293 } \
1294 static unsigned int fnname##_bits10( \
1295 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1296 int ref_stride) { \
1297 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1298 } \
1299 static unsigned int fnname##_bits12( \
1300 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1301 int ref_stride) { \
1302 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1303 }
1304
1305#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1306 static unsigned int fnname##_bits8( \
1307 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1308 int ref_stride, const uint8_t *second_pred) { \
1309 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1310 } \
1311 static unsigned int fnname##_bits10( \
1312 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1313 int ref_stride, const uint8_t *second_pred) { \
1314 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1315 2; \
1316 } \
1317 static unsigned int fnname##_bits12( \
1318 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1319 int ref_stride, const uint8_t *second_pred) { \
1320 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1321 4; \
1322 }
1323
Yaowu Xuc27fc142016-08-22 16:08:15 -07001324#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1325 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1326 const uint8_t *const ref_ptr[], int ref_stride, \
1327 unsigned int *sad_array) { \
1328 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1329 } \
1330 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1331 const uint8_t *const ref_ptr[], int ref_stride, \
1332 unsigned int *sad_array) { \
1333 int i; \
1334 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1335 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1336 } \
1337 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1338 const uint8_t *const ref_ptr[], int ref_stride, \
1339 unsigned int *sad_array) { \
1340 int i; \
1341 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1342 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1343 }
1344
Cheng Chenbf3d4962017-11-01 14:48:52 -07001345#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1346 static unsigned int fnname##_bits8( \
1347 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1348 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001349 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001350 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1351 jcp_param); \
1352 } \
1353 static unsigned int fnname##_bits10( \
1354 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1355 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001356 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001357 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1358 jcp_param) >> \
1359 2; \
1360 } \
1361 static unsigned int fnname##_bits12( \
1362 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1363 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001364 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001365 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1366 jcp_param) >> \
1367 4; \
1368 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001369
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001370#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001371MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1372MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001373MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1374MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1375MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1376MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1377MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1378MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1379MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001380MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1381MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1382MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1383MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1384MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1385MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1386MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1387MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1388MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1389MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1390MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1391MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1392MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1393MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001394MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1395MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1396MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001397MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1398MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1399MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001400MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1401MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1402MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001403MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1404MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1405MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001406MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1407MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1408MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001409MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1410MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1411MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001412MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1413MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1414MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001415MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1416MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1417MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001418MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001419
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001420MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1421MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1422MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1423MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1424MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1425MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1426MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1427MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1428MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1429MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1430MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1431MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001432MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1433MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1434MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1435MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1436MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1437MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001438
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001439MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1440MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1441MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1442MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1443MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1444MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1445MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1446MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1447MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1448MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1449MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1450MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1451MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1452MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1453MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1454MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1455MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1456MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1457MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1458MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1459MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1460MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001461#endif // CONFIG_AV1_HIGHBITDEPTH
Cheng Chenbf3d4962017-11-01 14:48:52 -07001462
David Barker0f3c94e2017-05-16 15:21:50 +01001463#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001464 cpi->fn_ptr[BT].msdf = MCSDF; \
1465 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001466
David Barkerc155e012017-05-11 13:54:54 +01001467#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1468 static unsigned int fnname##_bits8( \
1469 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1470 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1471 int m_stride, int invert_mask) { \
1472 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1473 second_pred_ptr, m, m_stride, invert_mask); \
1474 } \
1475 static unsigned int fnname##_bits10( \
1476 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1477 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1478 int m_stride, int invert_mask) { \
1479 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1480 second_pred_ptr, m, m_stride, invert_mask) >> \
1481 2; \
1482 } \
1483 static unsigned int fnname##_bits12( \
1484 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1485 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1486 int m_stride, int invert_mask) { \
1487 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1488 second_pred_ptr, m, m_stride, invert_mask) >> \
1489 4; \
1490 }
1491
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001492#if CONFIG_AV1_HIGHBITDEPTH
David Barkerf19f35f2017-05-22 16:33:22 +01001493MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1494MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1495MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001496MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1497MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1498MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1499MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1500MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1501MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1502MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1503MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1504MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1505MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1506MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1507MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1508MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001509MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1510MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1511MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1512MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001513MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1514MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001515#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07001516
Yaowu Xuc27fc142016-08-22 16:08:15 -07001517#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1518 cpi->fn_ptr[BT].osdf = OSDF; \
1519 cpi->fn_ptr[BT].ovf = OVF; \
1520 cpi->fn_ptr[BT].osvf = OSVF;
1521
1522#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1523 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1524 const int32_t *wsrc, \
1525 const int32_t *msk) { \
1526 return fnname(ref, ref_stride, wsrc, msk); \
1527 } \
1528 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1529 const int32_t *wsrc, \
1530 const int32_t *msk) { \
1531 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1532 } \
1533 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1534 const int32_t *wsrc, \
1535 const int32_t *msk) { \
1536 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1537 }
1538
Jerome Jiangfa1d1732019-08-06 10:31:20 -07001539#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuf883b422016-08-30 14:01:10 -07001540MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1541MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1542MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001543MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1544MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1545MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1546MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1547MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1548MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1549MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1550MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1551MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1552MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1553MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1554MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1555MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001556MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1557MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1558MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1559MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001560MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1561MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001562
Yaowu Xuf883b422016-08-30 14:01:10 -07001563static void highbd_set_var_fns(AV1_COMP *const cpi) {
1564 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001565 if (cm->seq_params.use_highbitdepth) {
1566 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001567 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001568 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1569 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1570 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001571 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001572 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001573 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1574 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001575
1576 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1577 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1578 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001579 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001580 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001581 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1582 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001583
1584 HIGHBD_BFP(
1585 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1586 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001587 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001588 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1589 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001590
1591 HIGHBD_BFP(
1592 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1593 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001594 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001595 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1596 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001597
1598 HIGHBD_BFP(
1599 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1600 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001601 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001602 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1603 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001604
1605 HIGHBD_BFP(
1606 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1607 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001608 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001609 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1610 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001611
1612 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1613 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1614 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001615 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001616 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001617 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1618 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001619
1620 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1621 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1622 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001623 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001624 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001625 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1626 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001627
1628 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1629 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1630 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001631 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001632 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001633 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1634 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001635
1636 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1637 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1638 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001639 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001640 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001641 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1642 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001643
1644 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1645 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1646 aom_highbd_8_sub_pixel_variance32x32,
1647 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001648 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001649 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1650 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001651
1652 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1653 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1654 aom_highbd_8_sub_pixel_variance64x64,
1655 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001656 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001657 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1658 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001659
1660 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1661 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1662 aom_highbd_8_sub_pixel_variance16x16,
1663 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001664 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001665 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1666 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001667
1668 HIGHBD_BFP(
1669 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1670 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001671 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001672 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1673 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001674
1675 HIGHBD_BFP(
1676 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1677 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001678 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001679 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1680 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001681
Cheng Chenbf3d4962017-11-01 14:48:52 -07001682 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001683 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1684 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1685 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1686 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1687 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1688
1689 HIGHBD_BFP(
1690 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1691 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1692 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1693 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1694 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1695
1696 HIGHBD_BFP(
1697 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1698 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1699 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1700 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1701 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1702
1703 HIGHBD_BFP(
1704 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1705 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1706 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1707 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1708 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1709
1710 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1711 aom_highbd_sad128x128_avg_bits8,
1712 aom_highbd_8_variance128x128,
1713 aom_highbd_8_sub_pixel_variance128x128,
1714 aom_highbd_8_sub_pixel_avg_variance128x128,
1715 aom_highbd_sad128x128x4d_bits8,
1716 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1717 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001718
1719 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1720 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1721 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001722 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001723 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001724 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1725 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001726
1727 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1728 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1729 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001730 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001731 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001732 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1733 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001734
David Barkerf19f35f2017-05-22 16:33:22 +01001735 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1736 aom_highbd_8_masked_sub_pixel_variance128x128)
1737 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1738 aom_highbd_8_masked_sub_pixel_variance128x64)
1739 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1740 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001741 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1742 aom_highbd_8_masked_sub_pixel_variance64x64)
1743 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1744 aom_highbd_8_masked_sub_pixel_variance64x32)
1745 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1746 aom_highbd_8_masked_sub_pixel_variance32x64)
1747 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1748 aom_highbd_8_masked_sub_pixel_variance32x32)
1749 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1750 aom_highbd_8_masked_sub_pixel_variance32x16)
1751 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1752 aom_highbd_8_masked_sub_pixel_variance16x32)
1753 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1754 aom_highbd_8_masked_sub_pixel_variance16x16)
1755 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1756 aom_highbd_8_masked_sub_pixel_variance8x16)
1757 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1758 aom_highbd_8_masked_sub_pixel_variance16x8)
1759 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1760 aom_highbd_8_masked_sub_pixel_variance8x8)
1761 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1762 aom_highbd_8_masked_sub_pixel_variance4x8)
1763 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1764 aom_highbd_8_masked_sub_pixel_variance8x4)
1765 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1766 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001767 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1768 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001769 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1770 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001771 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1772 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001773 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1774 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001775 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1776 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001777 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1778 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001779 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1780 aom_highbd_obmc_variance128x128,
1781 aom_highbd_obmc_sub_pixel_variance128x128)
1782 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1783 aom_highbd_obmc_variance128x64,
1784 aom_highbd_obmc_sub_pixel_variance128x64)
1785 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1786 aom_highbd_obmc_variance64x128,
1787 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001788 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1789 aom_highbd_obmc_variance64x64,
1790 aom_highbd_obmc_sub_pixel_variance64x64)
1791 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1792 aom_highbd_obmc_variance64x32,
1793 aom_highbd_obmc_sub_pixel_variance64x32)
1794 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1795 aom_highbd_obmc_variance32x64,
1796 aom_highbd_obmc_sub_pixel_variance32x64)
1797 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1798 aom_highbd_obmc_variance32x32,
1799 aom_highbd_obmc_sub_pixel_variance32x32)
1800 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1801 aom_highbd_obmc_variance32x16,
1802 aom_highbd_obmc_sub_pixel_variance32x16)
1803 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1804 aom_highbd_obmc_variance16x32,
1805 aom_highbd_obmc_sub_pixel_variance16x32)
1806 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1807 aom_highbd_obmc_variance16x16,
1808 aom_highbd_obmc_sub_pixel_variance16x16)
1809 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1810 aom_highbd_obmc_variance8x16,
1811 aom_highbd_obmc_sub_pixel_variance8x16)
1812 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1813 aom_highbd_obmc_variance16x8,
1814 aom_highbd_obmc_sub_pixel_variance16x8)
1815 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1816 aom_highbd_obmc_variance8x8,
1817 aom_highbd_obmc_sub_pixel_variance8x8)
1818 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1819 aom_highbd_obmc_variance4x8,
1820 aom_highbd_obmc_sub_pixel_variance4x8)
1821 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1822 aom_highbd_obmc_variance8x4,
1823 aom_highbd_obmc_sub_pixel_variance8x4)
1824 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1825 aom_highbd_obmc_variance4x4,
1826 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001827 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1828 aom_highbd_obmc_variance64x16,
1829 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001830 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1831 aom_highbd_obmc_variance16x64,
1832 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001833 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1834 aom_highbd_obmc_variance32x8,
1835 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001836 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1837 aom_highbd_obmc_variance8x32,
1838 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001839 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1840 aom_highbd_obmc_variance16x4,
1841 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001842 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1843 aom_highbd_obmc_variance4x16,
1844 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001845 break;
1846
Yaowu Xuf883b422016-08-30 14:01:10 -07001847 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001848 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1849 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1850 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001851 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001852 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001853 aom_highbd_dist_wtd_sad64x16_avg_bits10,
1854 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001855
1856 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1857 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1858 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001859 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001860 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001861 aom_highbd_dist_wtd_sad16x64_avg_bits10,
1862 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001863
1864 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1865 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1866 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001867 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001868 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001869 aom_highbd_dist_wtd_sad32x8_avg_bits10,
1870 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001871
1872 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1873 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1874 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001875 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001876 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001877 aom_highbd_dist_wtd_sad8x32_avg_bits10,
1878 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001879
1880 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1881 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1882 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001883 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001884 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001885 aom_highbd_dist_wtd_sad16x4_avg_bits10,
1886 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001887
1888 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1889 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1890 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001891 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001892 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001893 aom_highbd_dist_wtd_sad4x16_avg_bits10,
1894 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001895
1896 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1897 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1898 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001899 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001900 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001901 aom_highbd_dist_wtd_sad32x16_avg_bits10,
1902 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001903
1904 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1905 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1906 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001907 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001908 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001909 aom_highbd_dist_wtd_sad16x32_avg_bits10,
1910 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001911
1912 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1913 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1914 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001915 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001916 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001917 aom_highbd_dist_wtd_sad64x32_avg_bits10,
1918 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001919
1920 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1921 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1922 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001923 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001924 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001925 aom_highbd_dist_wtd_sad32x64_avg_bits10,
1926 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001927
1928 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1929 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1930 aom_highbd_10_sub_pixel_variance32x32,
1931 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001932 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001933 aom_highbd_dist_wtd_sad32x32_avg_bits10,
1934 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001935
1936 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1937 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1938 aom_highbd_10_sub_pixel_variance64x64,
1939 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001940 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001941 aom_highbd_dist_wtd_sad64x64_avg_bits10,
1942 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001943
1944 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1945 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1946 aom_highbd_10_sub_pixel_variance16x16,
1947 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001948 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001949 aom_highbd_dist_wtd_sad16x16_avg_bits10,
1950 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001951
1952 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1953 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1954 aom_highbd_10_sub_pixel_variance16x8,
1955 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001956 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001957 aom_highbd_dist_wtd_sad16x8_avg_bits10,
1958 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001959
1960 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1961 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1962 aom_highbd_10_sub_pixel_variance8x16,
1963 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001964 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001965 aom_highbd_dist_wtd_sad8x16_avg_bits10,
1966 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001967
1968 HIGHBD_BFP(
1969 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1970 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001971 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001972 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
1973 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001974
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001975 HIGHBD_BFP(
1976 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1977 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1978 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001979 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
1980 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001981
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001982 HIGHBD_BFP(
1983 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1984 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1985 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001986 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
1987 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001988
1989 HIGHBD_BFP(
1990 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1991 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001992 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001993 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
1994 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001995
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001996 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1997 aom_highbd_sad128x128_avg_bits10,
1998 aom_highbd_10_variance128x128,
1999 aom_highbd_10_sub_pixel_variance128x128,
2000 aom_highbd_10_sub_pixel_avg_variance128x128,
2001 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002002 aom_highbd_dist_wtd_sad128x128_avg_bits10,
2003 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002004
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002005 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
2006 aom_highbd_sad128x64_avg_bits10,
2007 aom_highbd_10_variance128x64,
2008 aom_highbd_10_sub_pixel_variance128x64,
2009 aom_highbd_10_sub_pixel_avg_variance128x64,
2010 aom_highbd_sad128x64x4d_bits10,
2011 aom_highbd_dist_wtd_sad128x64_avg_bits10,
2012 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002013
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002014 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
2015 aom_highbd_sad64x128_avg_bits10,
2016 aom_highbd_10_variance64x128,
2017 aom_highbd_10_sub_pixel_variance64x128,
2018 aom_highbd_10_sub_pixel_avg_variance64x128,
2019 aom_highbd_sad64x128x4d_bits10,
2020 aom_highbd_dist_wtd_sad64x128_avg_bits10,
2021 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002022
David Barkerf19f35f2017-05-22 16:33:22 +01002023 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
2024 aom_highbd_10_masked_sub_pixel_variance128x128)
2025 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
2026 aom_highbd_10_masked_sub_pixel_variance128x64)
2027 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
2028 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002029 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
2030 aom_highbd_10_masked_sub_pixel_variance64x64)
2031 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
2032 aom_highbd_10_masked_sub_pixel_variance64x32)
2033 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
2034 aom_highbd_10_masked_sub_pixel_variance32x64)
2035 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
2036 aom_highbd_10_masked_sub_pixel_variance32x32)
2037 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
2038 aom_highbd_10_masked_sub_pixel_variance32x16)
2039 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
2040 aom_highbd_10_masked_sub_pixel_variance16x32)
2041 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
2042 aom_highbd_10_masked_sub_pixel_variance16x16)
2043 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
2044 aom_highbd_10_masked_sub_pixel_variance8x16)
2045 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
2046 aom_highbd_10_masked_sub_pixel_variance16x8)
2047 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
2048 aom_highbd_10_masked_sub_pixel_variance8x8)
2049 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2050 aom_highbd_10_masked_sub_pixel_variance4x8)
2051 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2052 aom_highbd_10_masked_sub_pixel_variance8x4)
2053 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2054 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002055 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2056 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002057 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2058 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002059 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2060 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002061 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2062 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002063 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2064 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002065 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2066 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002067 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2068 aom_highbd_10_obmc_variance128x128,
2069 aom_highbd_10_obmc_sub_pixel_variance128x128)
2070 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2071 aom_highbd_10_obmc_variance128x64,
2072 aom_highbd_10_obmc_sub_pixel_variance128x64)
2073 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2074 aom_highbd_10_obmc_variance64x128,
2075 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002076 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2077 aom_highbd_10_obmc_variance64x64,
2078 aom_highbd_10_obmc_sub_pixel_variance64x64)
2079 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2080 aom_highbd_10_obmc_variance64x32,
2081 aom_highbd_10_obmc_sub_pixel_variance64x32)
2082 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2083 aom_highbd_10_obmc_variance32x64,
2084 aom_highbd_10_obmc_sub_pixel_variance32x64)
2085 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2086 aom_highbd_10_obmc_variance32x32,
2087 aom_highbd_10_obmc_sub_pixel_variance32x32)
2088 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2089 aom_highbd_10_obmc_variance32x16,
2090 aom_highbd_10_obmc_sub_pixel_variance32x16)
2091 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2092 aom_highbd_10_obmc_variance16x32,
2093 aom_highbd_10_obmc_sub_pixel_variance16x32)
2094 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2095 aom_highbd_10_obmc_variance16x16,
2096 aom_highbd_10_obmc_sub_pixel_variance16x16)
2097 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2098 aom_highbd_10_obmc_variance8x16,
2099 aom_highbd_10_obmc_sub_pixel_variance8x16)
2100 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2101 aom_highbd_10_obmc_variance16x8,
2102 aom_highbd_10_obmc_sub_pixel_variance16x8)
2103 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2104 aom_highbd_10_obmc_variance8x8,
2105 aom_highbd_10_obmc_sub_pixel_variance8x8)
2106 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2107 aom_highbd_10_obmc_variance4x8,
2108 aom_highbd_10_obmc_sub_pixel_variance4x8)
2109 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2110 aom_highbd_10_obmc_variance8x4,
2111 aom_highbd_10_obmc_sub_pixel_variance8x4)
2112 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2113 aom_highbd_10_obmc_variance4x4,
2114 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002115
Rupert Swarbrick72678572017-08-02 12:05:26 +01002116 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2117 aom_highbd_10_obmc_variance64x16,
2118 aom_highbd_10_obmc_sub_pixel_variance64x16)
2119
2120 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2121 aom_highbd_10_obmc_variance16x64,
2122 aom_highbd_10_obmc_sub_pixel_variance16x64)
2123
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002124 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2125 aom_highbd_10_obmc_variance32x8,
2126 aom_highbd_10_obmc_sub_pixel_variance32x8)
2127
2128 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2129 aom_highbd_10_obmc_variance8x32,
2130 aom_highbd_10_obmc_sub_pixel_variance8x32)
2131
2132 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2133 aom_highbd_10_obmc_variance16x4,
2134 aom_highbd_10_obmc_sub_pixel_variance16x4)
2135
2136 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2137 aom_highbd_10_obmc_variance4x16,
2138 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002139 break;
2140
Yaowu Xuf883b422016-08-30 14:01:10 -07002141 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002142 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2143 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2144 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002145 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002146 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002147 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2148 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002149
2150 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2151 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2152 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002153 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002154 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002155 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2156 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002157
2158 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2159 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2160 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002161 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002162 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002163 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2164 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002165
2166 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2167 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2168 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002169 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002170 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002171 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2172 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002173
2174 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2175 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2176 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002177 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002178 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002179 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2180 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002181
2182 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2183 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2184 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002185 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002186 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002187 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2188 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002189
2190 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2191 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2192 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002193 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002194 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002195 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2196 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002197
2198 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2199 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2200 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002201 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002202 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002203 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2204 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002205
2206 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2207 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2208 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002209 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002210 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002211 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2212 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002213
2214 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2215 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2216 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002217 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002218 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002219 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2220 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002221
2222 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2223 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2224 aom_highbd_12_sub_pixel_variance32x32,
2225 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002226 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002227 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2228 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002229
2230 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2231 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2232 aom_highbd_12_sub_pixel_variance64x64,
2233 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002234 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002235 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2236 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002237
2238 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2239 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2240 aom_highbd_12_sub_pixel_variance16x16,
2241 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002242 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002243 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2244 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002245
2246 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2247 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2248 aom_highbd_12_sub_pixel_variance16x8,
2249 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002250 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002251 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2252 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002253
2254 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2255 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2256 aom_highbd_12_sub_pixel_variance8x16,
2257 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002258 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002259 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2260 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002261
2262 HIGHBD_BFP(
2263 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2264 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002265 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002266 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2267 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002268
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002269 HIGHBD_BFP(
2270 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2271 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2272 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002273 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2274 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002275
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002276 HIGHBD_BFP(
2277 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2278 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2279 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002280 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2281 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002282
2283 HIGHBD_BFP(
2284 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2285 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002286 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002287 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2288 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002289
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002290 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2291 aom_highbd_sad128x128_avg_bits12,
2292 aom_highbd_12_variance128x128,
2293 aom_highbd_12_sub_pixel_variance128x128,
2294 aom_highbd_12_sub_pixel_avg_variance128x128,
2295 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002296 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2297 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002298
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002299 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2300 aom_highbd_sad128x64_avg_bits12,
2301 aom_highbd_12_variance128x64,
2302 aom_highbd_12_sub_pixel_variance128x64,
2303 aom_highbd_12_sub_pixel_avg_variance128x64,
2304 aom_highbd_sad128x64x4d_bits12,
2305 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2306 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002307
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002308 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2309 aom_highbd_sad64x128_avg_bits12,
2310 aom_highbd_12_variance64x128,
2311 aom_highbd_12_sub_pixel_variance64x128,
2312 aom_highbd_12_sub_pixel_avg_variance64x128,
2313 aom_highbd_sad64x128x4d_bits12,
2314 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2315 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002316
David Barkerf19f35f2017-05-22 16:33:22 +01002317 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2318 aom_highbd_12_masked_sub_pixel_variance128x128)
2319 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2320 aom_highbd_12_masked_sub_pixel_variance128x64)
2321 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2322 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002323 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2324 aom_highbd_12_masked_sub_pixel_variance64x64)
2325 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2326 aom_highbd_12_masked_sub_pixel_variance64x32)
2327 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2328 aom_highbd_12_masked_sub_pixel_variance32x64)
2329 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2330 aom_highbd_12_masked_sub_pixel_variance32x32)
2331 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2332 aom_highbd_12_masked_sub_pixel_variance32x16)
2333 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2334 aom_highbd_12_masked_sub_pixel_variance16x32)
2335 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2336 aom_highbd_12_masked_sub_pixel_variance16x16)
2337 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2338 aom_highbd_12_masked_sub_pixel_variance8x16)
2339 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2340 aom_highbd_12_masked_sub_pixel_variance16x8)
2341 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2342 aom_highbd_12_masked_sub_pixel_variance8x8)
2343 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2344 aom_highbd_12_masked_sub_pixel_variance4x8)
2345 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2346 aom_highbd_12_masked_sub_pixel_variance8x4)
2347 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2348 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002349 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2350 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002351 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2352 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002353 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2354 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002355 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2356 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002357 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2358 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002359 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2360 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002361 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2362 aom_highbd_12_obmc_variance128x128,
2363 aom_highbd_12_obmc_sub_pixel_variance128x128)
2364 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2365 aom_highbd_12_obmc_variance128x64,
2366 aom_highbd_12_obmc_sub_pixel_variance128x64)
2367 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2368 aom_highbd_12_obmc_variance64x128,
2369 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002370 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2371 aom_highbd_12_obmc_variance64x64,
2372 aom_highbd_12_obmc_sub_pixel_variance64x64)
2373 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2374 aom_highbd_12_obmc_variance64x32,
2375 aom_highbd_12_obmc_sub_pixel_variance64x32)
2376 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2377 aom_highbd_12_obmc_variance32x64,
2378 aom_highbd_12_obmc_sub_pixel_variance32x64)
2379 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2380 aom_highbd_12_obmc_variance32x32,
2381 aom_highbd_12_obmc_sub_pixel_variance32x32)
2382 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2383 aom_highbd_12_obmc_variance32x16,
2384 aom_highbd_12_obmc_sub_pixel_variance32x16)
2385 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2386 aom_highbd_12_obmc_variance16x32,
2387 aom_highbd_12_obmc_sub_pixel_variance16x32)
2388 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2389 aom_highbd_12_obmc_variance16x16,
2390 aom_highbd_12_obmc_sub_pixel_variance16x16)
2391 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2392 aom_highbd_12_obmc_variance8x16,
2393 aom_highbd_12_obmc_sub_pixel_variance8x16)
2394 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2395 aom_highbd_12_obmc_variance16x8,
2396 aom_highbd_12_obmc_sub_pixel_variance16x8)
2397 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2398 aom_highbd_12_obmc_variance8x8,
2399 aom_highbd_12_obmc_sub_pixel_variance8x8)
2400 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2401 aom_highbd_12_obmc_variance4x8,
2402 aom_highbd_12_obmc_sub_pixel_variance4x8)
2403 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2404 aom_highbd_12_obmc_variance8x4,
2405 aom_highbd_12_obmc_sub_pixel_variance8x4)
2406 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2407 aom_highbd_12_obmc_variance4x4,
2408 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002409 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2410 aom_highbd_12_obmc_variance64x16,
2411 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002412 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2413 aom_highbd_12_obmc_variance16x64,
2414 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002415 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2416 aom_highbd_12_obmc_variance32x8,
2417 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002418 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2419 aom_highbd_12_obmc_variance8x32,
2420 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002421 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2422 aom_highbd_12_obmc_variance16x4,
2423 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002424 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2425 aom_highbd_12_obmc_variance4x16,
2426 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002427 break;
2428
2429 default:
2430 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002431 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002432 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002433 }
2434 }
2435}
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002436#endif // CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002437
Yaowu Xuf883b422016-08-30 14:01:10 -07002438static void realloc_segmentation_maps(AV1_COMP *cpi) {
2439 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002440
2441 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002442 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002443 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002444 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002445
2446 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002447 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002448 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002449 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002450
2451 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002452 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002454 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002455}
2456
Yue Chenb4c93f02019-08-05 13:47:31 -07002457static void set_tpl_stats_block_size(AV1_COMP *cpi) {
2458 AV1_COMMON *const cm = &cpi->common;
2459 const int is_720p_or_larger = AOMMIN(cm->width, cm->height) >= 720;
2460
2461 // 0: 4x4, 1: 8x8, 2: 16x16
2462 cpi->tpl_stats_block_mis_log2 = is_720p_or_larger ? 2 : 1;
2463}
2464
Hui Su38711e72019-06-11 10:49:47 -07002465void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2466 CompoundTypeRdBuffers *const bufs) {
2467 CHECK_MEM_ERROR(
2468 cm, bufs->pred0,
2469 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2470 CHECK_MEM_ERROR(
2471 cm, bufs->pred1,
2472 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2473 CHECK_MEM_ERROR(
2474 cm, bufs->residual1,
2475 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2476 CHECK_MEM_ERROR(
2477 cm, bufs->diff10,
2478 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2479 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2480 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2481 sizeof(*bufs->tmp_best_mask_buf)));
2482}
2483
2484void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2485 aom_free(bufs->pred0);
2486 aom_free(bufs->pred1);
2487 aom_free(bufs->residual1);
2488 aom_free(bufs->diff10);
2489 aom_free(bufs->tmp_best_mask_buf);
2490 av1_zero(*bufs); // Set all pointers to NULL for safety.
2491}
2492
Yaowu Xuf883b422016-08-30 14:01:10 -07002493void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2494 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002495 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002496 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002497 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002498 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002499
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002500 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2501 seq_params->bit_depth = oxcf->bit_depth;
2502 seq_params->color_primaries = oxcf->color_primaries;
2503 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2504 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2505 seq_params->monochrome = oxcf->monochrome;
2506 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2507 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002508
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002509 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2510 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002511
Hui Su2aa492c2019-03-12 15:18:18 -07002512 memcpy(cpi->target_seq_level_idx, oxcf->target_seq_level_idx,
2513 sizeof(cpi->target_seq_level_idx));
2514 cpi->keep_level_stats = 0;
2515 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su6d666502019-06-13 15:16:50 -07002516 if (cpi->target_seq_level_idx[i] <= SEQ_LEVELS) {
Hui Su72ff0482019-05-28 14:07:37 -07002517 cpi->keep_level_stats |= 1u << i;
Hui Suc3a8d372019-05-28 15:52:45 -07002518 if (!cpi->level_info[i]) {
2519 CHECK_MEM_ERROR(cm, cpi->level_info[i],
2520 aom_calloc(1, sizeof(*cpi->level_info[i])));
2521 }
Hui Su2aa492c2019-03-12 15:18:18 -07002522 }
2523 }
kyslovabeeb7c2019-03-06 18:35:04 -08002524
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002525 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002526 cm->timing_info.num_units_in_display_tick =
2527 oxcf->timing_info.num_units_in_display_tick;
2528 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2529 cm->timing_info.equal_picture_interval =
2530 oxcf->timing_info.equal_picture_interval;
2531 cm->timing_info.num_ticks_per_picture =
2532 oxcf->timing_info.num_ticks_per_picture;
2533
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002534 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002535 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002536 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002537 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002538 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002539 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002540 cm->buffer_model.num_units_in_decoding_tick =
2541 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002542 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07002543 av1_set_aom_dec_model_info(&cm->buffer_model);
2544 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07002545 } else if (cm->timing_info_present &&
2546 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002547 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002548 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07002549 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002550 } else {
2551 cm->op_params[0].initial_display_delay =
2552 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002553 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002554
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002555 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002556
Yaowu Xuc27fc142016-08-22 16:08:15 -07002557 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002558 cpi->common.options = oxcf->cfg;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002559 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002560 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002561
Yaowu Xuf883b422016-08-30 14:01:10 -07002562 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002563 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2564 } else {
2565 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2566 }
2567
2568 cpi->refresh_last_frame = 1;
2569 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570 cpi->refresh_bwd_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002571
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002572 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2573 ? REFRESH_FRAME_CONTEXT_DISABLED
2574 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002575 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002576 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002577
Alex Converse74ad0912017-07-18 10:22:58 -07002578 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002579 CHECK_MEM_ERROR(cm, x->palette_buffer,
2580 aom_memalign(16, sizeof(*x->palette_buffer)));
2581 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002582
Hui Su38711e72019-06-11 10:49:47 -07002583 if (x->comp_rd_buffer.pred0 == NULL) {
2584 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2585 }
2586
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002587 if (x->tmp_conv_dst == NULL) {
2588 CHECK_MEM_ERROR(
2589 cm, x->tmp_conv_dst,
2590 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002591 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002592 }
2593 for (int i = 0; i < 2; ++i) {
2594 if (x->tmp_obmc_bufs[i] == NULL) {
2595 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002596 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002597 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002598 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002599 }
2600 }
2601
Yaowu Xuf883b422016-08-30 14:01:10 -07002602 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002603 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002604
Yaowu Xuc27fc142016-08-22 16:08:15 -07002605 set_rc_buffer_sizes(rc, &cpi->oxcf);
2606
2607 // Under a configuration change, where maximum_buffer_size may change,
2608 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002609 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2610 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002611
2612 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002613 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002614
2615 // Set absolute upper and lower quality limits
2616 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2617 rc->best_quality = cpi->oxcf.best_allowed_q;
2618
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002619 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002620 cm->switchable_motion_mode = 1;
2621
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2623 cm->render_width = cpi->oxcf.render_width;
2624 cm->render_height = cpi->oxcf.render_height;
2625 } else {
2626 cm->render_width = cpi->oxcf.width;
2627 cm->render_height = cpi->oxcf.height;
2628 }
2629 cm->width = cpi->oxcf.width;
2630 cm->height = cpi->oxcf.height;
2631
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002632 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002633 // Superblock size should not be updated after the first key frame.
2634 if (!cpi->seq_params_locked) {
2635 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002636 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2637 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002638 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002639
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002640 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002641 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002642 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002643 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002644 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002645 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002646 realloc_segmentation_maps(cpi);
2647 cpi->initial_width = cpi->initial_height = 0;
2648 }
2649 }
2650 update_frame_size(cpi);
2651
2652 cpi->alt_ref_source = NULL;
2653 rc->is_src_frame_alt_ref = 0;
2654
Yaowu Xuc27fc142016-08-22 16:08:15 -07002655 set_tile_info(cpi);
2656
Marco Paniconid8574e32019-08-04 21:30:12 -07002657 if (!cpi->svc.external_ref_frame_config)
2658 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659 cpi->ext_refresh_frame_context_pending = 0;
2660
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002661#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07002662 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07002663#endif
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002664
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002665 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002666 // This should not be called after the first key frame.
2667 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002668 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002669 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Marco Paniconi67142112019-07-24 15:00:31 -07002670 init_seq_coding_tools(&cm->seq_params, cm, oxcf, cpi->use_svc);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002671 }
Marco Paniconi5b1e4732019-08-08 18:57:53 -07002672
2673 if (cpi->use_svc)
2674 av1_update_layer_context_change_config(cpi, oxcf->target_bandwidth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002675}
2676
Yaowu Xuf883b422016-08-30 14:01:10 -07002677AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2678 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002679 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002680 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2681 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002682
2683 if (!cm) return NULL;
2684
Yaowu Xuf883b422016-08-30 14:01:10 -07002685 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002686
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002687 // The jmp_buf is valid only for the duration of the function that calls
2688 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2689 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002690 if (setjmp(cm->error.jmp)) {
2691 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002692 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002693 return 0;
2694 }
2695
2696 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002697 cm->alloc_mi = enc_alloc_mi;
2698 cm->free_mi = enc_free_mi;
2699 cm->setup_mi = enc_setup_mi;
chiyotsaia7091f12019-08-09 16:48:27 -07002700 cm->set_mb_mi = enc_set_mb_mi;
2701
2702 cm->mi_alloc_bsize = BLOCK_4X4;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002703
Angie Chianga5d96c42016-10-21 16:16:56 -07002704 CHECK_MEM_ERROR(cm, cm->fc,
2705 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002706 CHECK_MEM_ERROR(
2707 cm, cm->default_frame_context,
2708 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002709 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002710 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002711
2712 cpi->resize_state = 0;
2713 cpi->resize_avg_qp = 0;
2714 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002715
Yaowu Xuc27fc142016-08-22 16:08:15 -07002716 cpi->common.buffer_pool = pool;
2717
2718 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002719 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002720
David Turnerd2a592e2018-11-16 14:59:31 +00002721 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002722 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002723 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002724 cpi->partition_search_skippable_frame = 0;
2725 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00002726 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002727 realloc_segmentation_maps(cpi);
2728
Jingning Hanf050fc12018-03-09 14:53:33 -08002729 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2730 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002731
Yaowu Xuc27fc142016-08-22 16:08:15 -07002732 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2733 i++) {
2734 CHECK_MEM_ERROR(
2735 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002736 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002737 }
2738
Yaowu Xuc27fc142016-08-22 16:08:15 -07002739 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002740
2741 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2742#if CONFIG_INTERNAL_STATS
2743 cpi->b_calculate_blockiness = 1;
2744 cpi->b_calculate_consistency = 1;
2745 cpi->total_inconsistency = 0;
2746 cpi->psnr.worst = 100.0;
2747 cpi->worst_ssim = 100.0;
2748
2749 cpi->count = 0;
2750 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08002751#if CONFIG_SPEED_STATS
2752 cpi->tx_search_count = 0;
2753#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002754
2755 if (cpi->b_calculate_psnr) {
2756 cpi->total_sq_error = 0;
2757 cpi->total_samples = 0;
2758 cpi->tot_recode_hits = 0;
2759 cpi->summed_quality = 0;
2760 cpi->summed_weights = 0;
2761 }
2762
2763 cpi->fastssim.worst = 100.0;
2764 cpi->psnrhvs.worst = 100.0;
2765
2766 if (cpi->b_calculate_blockiness) {
2767 cpi->total_blockiness = 0;
2768 cpi->worst_blockiness = 0.0;
2769 }
2770
2771 if (cpi->b_calculate_consistency) {
2772 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002773 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002774 cpi->common.mi_rows * cpi->common.mi_cols));
2775 cpi->worst_consistency = 100.0;
2776 }
2777#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002778#if CONFIG_ENTROPY_STATS
2779 av1_zero(aggregate_fc);
2780#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002781
2782 cpi->first_time_stamp_ever = INT64_MAX;
2783
Jingning Hanf050fc12018-03-09 14:53:33 -08002784 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2785 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2786 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2787 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002788
Yaowu Xuc27fc142016-08-22 16:08:15 -07002789#ifdef OUTPUT_YUV_SKINMAP
2790 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2791#endif
2792#ifdef OUTPUT_YUV_REC
2793 yuv_rec_file = fopen("rec.yuv", "wb");
2794#endif
2795
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002796#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -07002797 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002798 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002799 } else if (oxcf->pass == 2) {
2800 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2801 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2802
Yaowu Xuc27fc142016-08-22 16:08:15 -07002803 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2804 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2805 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2806
Yaowu Xuf883b422016-08-30 14:01:10 -07002807 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002808 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002809#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002810
Remya0cce44c2019-08-16 11:57:24 +05302811 int sb_mi_size = av1_get_sb_mi_size(cm);
2812
Jingning Hand064cf02017-06-01 10:00:39 -07002813 CHECK_MEM_ERROR(
2814 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002815 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002816 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002817 CHECK_MEM_ERROR(
2818 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002819 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002820 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002821
2822 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2823 (int32_t *)aom_memalign(
2824 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2825
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302826 CHECK_MEM_ERROR(
2827 cm, cpi->td.mb.inter_modes_info,
2828 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302829
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302830 for (int x = 0; x < 2; x++)
2831 for (int y = 0; y < 2; y++)
2832 CHECK_MEM_ERROR(
2833 cm, cpi->td.mb.hash_value_buffer[x][y],
2834 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2835 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2836
2837 cpi->td.mb.g_crc_initialized = 0;
2838
Jingning Hand064cf02017-06-01 10:00:39 -07002839 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2840 (int32_t *)aom_memalign(
2841 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2842
Remya0cce44c2019-08-16 11:57:24 +05302843 CHECK_MEM_ERROR(cm, cpi->td.mb.mbmi_ext,
2844 aom_calloc(sb_mi_size, sizeof(*cpi->td.mb.mbmi_ext)));
2845
David Turner04b70d82019-01-24 15:39:19 +00002846 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
2847 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002848
sdengc23c7f12019-06-11 16:56:50 -07002849 {
2850 const int bsize = BLOCK_16X16;
2851 const int w = mi_size_wide[bsize];
2852 const int h = mi_size_high[bsize];
2853 const int num_cols = (cm->mi_cols + w - 1) / w;
2854 const int num_rows = (cm->mi_rows + h - 1) / h;
sdengf46a1062019-08-04 18:43:50 -07002855 CHECK_MEM_ERROR(cm, cpi->tpl_rdmult_scaling_factors,
2856 aom_calloc(num_rows * num_cols,
2857 sizeof(*cpi->tpl_rdmult_scaling_factors)));
2858 CHECK_MEM_ERROR(cm, cpi->tpl_sb_rdmult_scaling_factors,
2859 aom_calloc(num_rows * num_cols,
2860 sizeof(*cpi->tpl_sb_rdmult_scaling_factors)));
2861 }
2862
2863 {
2864 const int bsize = BLOCK_16X16;
2865 const int w = mi_size_wide[bsize];
2866 const int h = mi_size_high[bsize];
2867 const int num_cols = (cm->mi_cols + w - 1) / w;
2868 const int num_rows = (cm->mi_rows + h - 1) / h;
sdengc23c7f12019-06-11 16:56:50 -07002869 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
2870 aom_calloc(num_rows * num_cols,
2871 sizeof(*cpi->ssim_rdmult_scaling_factors)));
2872 }
2873
Yue Chenb4c93f02019-08-05 13:47:31 -07002874 set_tpl_stats_block_size(cpi);
Yue Chenc9b23e02019-04-10 16:54:03 -07002875 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chenb4c93f02019-08-05 13:47:31 -07002876 const int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2877 const int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2878
2879 cpi->tpl_stats_buffer[frame].is_valid = 0;
2880 cpi->tpl_stats_buffer[frame].width =
2881 mi_cols >> cpi->tpl_stats_block_mis_log2;
2882 cpi->tpl_stats_buffer[frame].height =
2883 mi_rows >> cpi->tpl_stats_block_mis_log2;
2884 cpi->tpl_stats_buffer[frame].stride = cpi->tpl_stats_buffer[frame].width;
2885 cpi->tpl_stats_buffer[frame].mi_rows = cm->mi_rows;
2886 cpi->tpl_stats_buffer[frame].mi_cols = cm->mi_cols;
Yue Chen7cae98f2018-08-24 10:43:16 -07002887
Jingning Han81d6fbb2019-07-15 10:14:13 -07002888 CHECK_MEM_ERROR(
2889 cm, cpi->tpl_stats_buffer[frame].tpl_stats_ptr,
Yue Chenb4c93f02019-08-05 13:47:31 -07002890 aom_calloc(cpi->tpl_stats_buffer[frame].width *
2891 cpi->tpl_stats_buffer[frame].height,
Jingning Han81d6fbb2019-07-15 10:14:13 -07002892 sizeof(*cpi->tpl_stats_buffer[frame].tpl_stats_ptr)));
Yue Chen7cae98f2018-08-24 10:43:16 -07002893 }
Jingning Han31a0ee92019-07-15 13:56:55 -07002894 cpi->tpl_frame = &cpi->tpl_stats_buffer[REF_FRAMES + 1];
Yue Chen7cae98f2018-08-24 10:43:16 -07002895
chiyotsai9c484b32019-03-07 16:01:50 -08002896#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08002897 av1_zero(cpi->partition_stats);
2898#endif
2899
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002900#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2901 cpi->fn_ptr[BT].sdf = SDF; \
2902 cpi->fn_ptr[BT].sdaf = SDAF; \
2903 cpi->fn_ptr[BT].vf = VF; \
2904 cpi->fn_ptr[BT].svf = SVF; \
2905 cpi->fn_ptr[BT].svaf = SVAF; \
2906 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2907 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002908 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002909
Cheng Chenf78632e2017-10-20 15:30:51 -07002910 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002911 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002912 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
2913 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002914
2915 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002916 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002917 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
2918 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002919
2920 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002921 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002922 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
2923 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002924
2925 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002926 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002927 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
2928 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002929
2930 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002931 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002932 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
2933 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002934
2935 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002936 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002937 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
2938 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002939
Cheng Chenf78632e2017-10-20 15:30:51 -07002940 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2941 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002942 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
2943 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002944
2945 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002946 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002947 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
2948 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002949
2950 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002951 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002952 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
2953 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002954
2955 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002956 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002957 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
2958 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002959
2960 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002961 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002962 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
2963 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002964
2965 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002966 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002967 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
2968 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002969
2970 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002971 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002972 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
2973 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002974
2975 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2976 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002977 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
2978 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002979
2980 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2981 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002982 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
2983 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002984
2985 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2986 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002987 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
2988 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002989
2990 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002991 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002992 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
2993 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002994
2995 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002996 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002997 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
2998 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002999
3000 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003001 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003002 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003003
3004 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003005 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003006 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003007
3008 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003009 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003010 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07003011
3012 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04003013 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08003014 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07003015
Yaowu Xuc27fc142016-08-22 16:08:15 -07003016#define OBFP(BT, OSDF, OVF, OSVF) \
3017 cpi->fn_ptr[BT].osdf = OSDF; \
3018 cpi->fn_ptr[BT].ovf = OVF; \
3019 cpi->fn_ptr[BT].osvf = OSVF;
3020
Yaowu Xuf883b422016-08-30 14:01:10 -07003021 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
3022 aom_obmc_sub_pixel_variance128x128)
3023 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
3024 aom_obmc_sub_pixel_variance128x64)
3025 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
3026 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07003027 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
3028 aom_obmc_sub_pixel_variance64x64)
3029 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
3030 aom_obmc_sub_pixel_variance64x32)
3031 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
3032 aom_obmc_sub_pixel_variance32x64)
3033 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
3034 aom_obmc_sub_pixel_variance32x32)
3035 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
3036 aom_obmc_sub_pixel_variance32x16)
3037 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
3038 aom_obmc_sub_pixel_variance16x32)
3039 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
3040 aom_obmc_sub_pixel_variance16x16)
3041 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
3042 aom_obmc_sub_pixel_variance16x8)
3043 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
3044 aom_obmc_sub_pixel_variance8x16)
3045 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
3046 aom_obmc_sub_pixel_variance8x8)
3047 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
3048 aom_obmc_sub_pixel_variance4x8)
3049 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
3050 aom_obmc_sub_pixel_variance8x4)
3051 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
3052 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003053 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
3054 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003055 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
3056 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003057 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
3058 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003059 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
3060 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003061 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
3062 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003063 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
3064 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07003065
David Barkerf19f35f2017-05-22 16:33:22 +01003066#define MBFP(BT, MCSDF, MCSVF) \
3067 cpi->fn_ptr[BT].msdf = MCSDF; \
3068 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003069
David Barkerf19f35f2017-05-22 16:33:22 +01003070 MBFP(BLOCK_128X128, aom_masked_sad128x128,
3071 aom_masked_sub_pixel_variance128x128)
3072 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
3073 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01003074 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
3075 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
3076 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
3077 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
3078 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
3079 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
3080 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
3081 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
3082 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
3083 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
3084 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
3085 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
3086 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003087
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01003088 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
3089
3090 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
3091
3092 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3093
3094 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003095
3096 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3097
3098 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003099
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003100#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xuc27fc142016-08-22 16:08:15 -07003101 highbd_set_var_fns(cpi);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003102#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003103
Yaowu Xuf883b422016-08-30 14:01:10 -07003104 /* av1_init_quantizer() is first called here. Add check in
3105 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003106 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003107 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003108 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003109 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08003110 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003111
Yaowu Xuf883b422016-08-30 14:01:10 -07003112 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003113 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003114 cm->superres_upscaled_width = oxcf->width;
3115 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003116 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003117
3118 cm->error.setjmp = 0;
3119
3120 return cpi;
3121}
3122
Urvang Joshiee2c8112018-05-04 14:53:15 -07003123#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003124#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3125
3126#define SNPRINT2(H, T, V) \
3127 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003128#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003129
Yaowu Xuf883b422016-08-30 14:01:10 -07003130void av1_remove_compressor(AV1_COMP *cpi) {
3131 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003132 unsigned int i;
3133 int t;
3134
3135 if (!cpi) return;
3136
3137 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003138 const int num_planes = av1_num_planes(cm);
3139
David Turnerd2a592e2018-11-16 14:59:31 +00003140 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003141#if CONFIG_ENTROPY_STATS
3142 if (cpi->oxcf.pass != 1) {
3143 fprintf(stderr, "Writing counts.stt\n");
3144 FILE *f = fopen("counts.stt", "wb");
3145 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3146 fclose(f);
3147 }
3148#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003149#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003150 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003151
3152 if (cpi->oxcf.pass != 1) {
3153 char headings[512] = { 0 };
3154 char results[512] = { 0 };
3155 FILE *f = fopen("opsnr.stt", "a");
3156 double time_encoded =
3157 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3158 10000000.000;
3159 double total_encode_time =
3160 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3161 const double dr =
3162 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3163 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3164 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3165 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3166
3167 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003168 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003169 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3170 const double total_ssim =
3171 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3172 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003173 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003174 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003175 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003176 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003177 snprintf(results, sizeof(results),
3178 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3179 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003180 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Yue Chenf0652ed2019-08-13 16:09:25 -07003181 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003182 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3183 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3184 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3185 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003186 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003187 cpi->psnr.stat[STAT_Y] / cpi->count,
3188 cpi->psnr.stat[STAT_U] / cpi->count,
Yue Chenf0652ed2019-08-13 16:09:25 -07003189 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003190
3191 if (cpi->b_calculate_blockiness) {
3192 SNPRINT(headings, "\t Block\tWstBlck");
3193 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3194 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3195 }
3196
3197 if (cpi->b_calculate_consistency) {
3198 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003199 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003200 (double)cpi->total_inconsistency);
3201
3202 SNPRINT(headings, "\tConsist\tWstCons");
3203 SNPRINT2(results, "\t%7.3f", consistency);
3204 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3205 }
Yue Chenf0652ed2019-08-13 16:09:25 -07003206
3207 SNPRINT(headings, "\t Time\tRcErr\tAbsErr");
3208 SNPRINT2(results, "\t%8.0f", total_encode_time);
3209 SNPRINT2(results, "\t%7.2f", rate_err);
3210 SNPRINT2(results, "\t%7.2f", fabs(rate_err));
3211
3212 fprintf(f, "%s\tAPsnr611\n", headings);
3213 fprintf(f, "%s\t%7.3f\n", results,
3214 (6 * cpi->psnr.stat[STAT_Y] + cpi->psnr.stat[STAT_U] +
3215 cpi->psnr.stat[STAT_V]) /
3216 (cpi->count * 8));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003217 }
3218
3219 fclose(f);
3220 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003221#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003222#if CONFIG_SPEED_STATS
3223 if (cpi->oxcf.pass != 1) {
3224 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3225 }
3226#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003227
chiyotsai9c484b32019-03-07 16:01:50 -08003228#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003229 if (cpi->oxcf.pass != 1) {
3230 av1_print_partition_stats(&cpi->partition_stats);
3231 }
3232#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003233 }
3234
Yue Chenc9b23e02019-04-10 16:54:03 -07003235 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Jingning Han81d6fbb2019-07-15 10:14:13 -07003236 aom_free(cpi->tpl_stats_buffer[frame].tpl_stats_ptr);
3237 cpi->tpl_stats_buffer[frame].is_valid = 0;
Yue Chen7cae98f2018-08-24 10:43:16 -07003238 }
3239
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303240 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003241 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003242 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3243
3244 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003245 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003246
3247 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303248 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303249 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003250 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003251 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003252 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003253 for (int j = 0; j < 2; ++j) {
3254 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3255 }
Jingning Hand064cf02017-06-01 10:00:39 -07003256 aom_free(thread_data->td->above_pred_buf);
3257 aom_free(thread_data->td->left_pred_buf);
3258 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003259
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303260 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003261 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303262 for (int y = 0; y < 2; y++) {
3263 aom_free(thread_data->td->hash_value_buffer[x][y]);
3264 thread_data->td->hash_value_buffer[x][y] = NULL;
3265 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003266 }
Jingning Hand064cf02017-06-01 10:00:39 -07003267 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003268 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003269 av1_free_pc_tree(thread_data->td, num_planes);
Remya0cce44c2019-08-16 11:57:24 +05303270 aom_free(thread_data->td->mbmi_ext);
Yaowu Xuf883b422016-08-30 14:01:10 -07003271 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003272 }
3273 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303274#if CONFIG_MULTITHREAD
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303275 if (cpi->row_mt_mutex_ != NULL) {
3276 pthread_mutex_destroy(cpi->row_mt_mutex_);
3277 aom_free(cpi->row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303278 }
3279#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303280 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003281 aom_free(cpi->tile_thr_data);
3282 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003283
Deepa K G964e72e2018-05-16 16:56:01 +05303284 if (cpi->num_workers > 1) {
3285 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303286 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303287 }
3288
Yaowu Xuc27fc142016-08-22 16:08:15 -07003289 dealloc_compressor_data(cpi);
3290
3291 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3292 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003293 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003294 }
3295
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003296#if CONFIG_INTERNAL_STATS
3297 aom_free(cpi->ssim_vars);
3298 cpi->ssim_vars = NULL;
3299#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003300
Yaowu Xuf883b422016-08-30 14:01:10 -07003301 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003302 for (i = 0; i < FRAME_BUFFERS; ++i) {
3303 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3304 }
Yaowu Xu74e63352019-05-06 09:21:33 -07003305#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003306 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003307#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003308 av1_free_ref_frame_buffers(cm->buffer_pool);
3309 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003310
3311#ifdef OUTPUT_YUV_SKINMAP
3312 fclose(yuv_skinmap_file);
3313#endif
3314#ifdef OUTPUT_YUV_REC
3315 fclose(yuv_rec_file);
3316#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003317}
3318
Yaowu Xuf883b422016-08-30 14:01:10 -07003319static void generate_psnr_packet(AV1_COMP *cpi) {
3320 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003321 int i;
3322 PSNR_STATS psnr;
Jerome Jiangfa1d1732019-08-06 10:31:20 -07003323#if CONFIG_AV1_HIGHBITDEPTH
3324 // TODO(yaowu): unify these two versions into one.
3325 if (cpi->common.seq_params.use_highbitdepth)
3326 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
3327 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
3328 else
3329 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3330#else
3331 aom_calc_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr);
3332#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003333
3334 for (i = 0; i < 4; ++i) {
3335 pkt.data.psnr.samples[i] = psnr.samples[i];
3336 pkt.data.psnr.sse[i] = psnr.sse[i];
3337 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3338 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003339 pkt.kind = AOM_CODEC_PSNR_PKT;
3340 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003341}
3342
Yaowu Xuf883b422016-08-30 14:01:10 -07003343int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003344 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3345
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003346 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003347 return 0;
3348}
3349
Thomas Daede497d1952017-08-08 17:33:06 -07003350int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3351 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003352 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003353 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003354 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003355 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003356 return 0;
3357 } else {
3358 return -1;
3359 }
3360}
3361
Thomas Daede497d1952017-08-08 17:33:06 -07003362int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3363 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003364 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003365 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003366 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003367 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003368 return 0;
3369 } else {
3370 return -1;
3371 }
3372}
3373
3374int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003375 cpi->ext_refresh_frame_context = update;
3376 cpi->ext_refresh_frame_context_pending = 1;
3377 return 0;
3378}
3379
3380#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3381// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3382// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3383// not denoise the UV channels at this time. If ever we implement UV channel
3384// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003385void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386 uint8_t *src = s->y_buffer;
3387 int h = s->y_height;
3388
3389 do {
3390 fwrite(src, s->y_width, 1, f);
3391 src += s->y_stride;
3392 } while (--h);
3393
3394 src = s->u_buffer;
3395 h = s->uv_height;
3396
3397 do {
3398 fwrite(src, s->uv_width, 1, f);
3399 src += s->uv_stride;
3400 } while (--h);
3401
3402 src = s->v_buffer;
3403 h = s->uv_height;
3404
3405 do {
3406 fwrite(src, s->uv_width, 1, f);
3407 src += s->uv_stride;
3408 } while (--h);
3409}
3410#endif
3411
Yaowu Xuc27fc142016-08-22 16:08:15 -07003412#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003413void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003414 uint8_t *src = s->y_buffer;
3415 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003416 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003417 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3418 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3419
3420 do {
3421 fwrite(src16, s->y_width, 2, yuv_rec_file);
3422 src16 += s->y_stride;
3423 } while (--h);
3424
3425 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3426 h = s->uv_height;
3427
3428 do {
3429 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3430 src16 += s->uv_stride;
3431 } while (--h);
3432
3433 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3434 h = s->uv_height;
3435
3436 do {
3437 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3438 src16 += s->uv_stride;
3439 } while (--h);
3440
3441 fflush(yuv_rec_file);
3442 return;
3443 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003444
3445 do {
3446 fwrite(src, s->y_width, 1, yuv_rec_file);
3447 src += s->y_stride;
3448 } while (--h);
3449
3450 src = s->u_buffer;
3451 h = s->uv_height;
3452
3453 do {
3454 fwrite(src, s->uv_width, 1, yuv_rec_file);
3455 src += s->uv_stride;
3456 } while (--h);
3457
3458 src = s->v_buffer;
3459 h = s->uv_height;
3460
3461 do {
3462 fwrite(src, s->uv_width, 1, yuv_rec_file);
3463 src += s->uv_stride;
3464 } while (--h);
3465
3466 fflush(yuv_rec_file);
3467}
3468#endif // OUTPUT_YUV_REC
3469
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003470#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003471static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3472 int i;
3473 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003474 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003475 AV1_COMMON *const cm = &cpi->common;
3476 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3477 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003478 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003479 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003480 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003481 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003482 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003483 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003484 // TODO(sarahparker): The earlier condition for recoding here was:
3485 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3486 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003487 }
3488 }
3489 return recode;
3490}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003491
Yaowu Xuc27fc142016-08-22 16:08:15 -07003492// Function to test for conditions that indicate we should loop
3493// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003494static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3495 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003496 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003497 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003498 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3499 int force_recode = 0;
3500
3501 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3502 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3503 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003504 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3505 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3506 (rc->projected_frame_size < low_limit && q > minq)) {
3507 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003508 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003509 // Deal with frame undershoot and whether or not we are
3510 // below the automatically set cq level.
3511 if (q > oxcf->cq_level &&
3512 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3513 force_recode = 1;
3514 }
3515 }
3516 }
3517 return force_recode;
3518}
3519
Cheng Chen46f30c72017-09-07 11:13:33 -07003520static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003521 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003522 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003523 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003524
3525 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003526 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Jingning Han667561a2019-07-22 15:48:01 -07003527 if (cpi->ref_frame_flags & av1_ref_frame_flag_list[ref_frame]) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003528 BufferPool *const pool = cm->buffer_pool;
3529 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003530 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003531
3532 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003533 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003534 continue;
3535 }
3536
Yaowu Xuc27fc142016-08-22 16:08:15 -07003537 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003538 // Replace the reference buffer with a copy having a thicker border,
3539 // if the reference buffer is higher resolution than the current
3540 // frame, and the border is thin.
3541 if ((ref->y_crop_width > cm->width ||
3542 ref->y_crop_height > cm->height) &&
3543 ref->border < AOM_BORDER_IN_PIXELS) {
3544 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3545 if (aom_yv12_realloc_with_new_border(
3546 &ref_fb->buf, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3547 num_planes) != 0) {
3548 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3549 "Failed to allocate frame buffer");
3550 }
3551 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003553 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3554 if (new_fb == NULL) {
3555 const int new_fb_idx = get_free_fb(cm);
3556 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003557 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3558 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003559 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003560 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003561 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003562 }
David Turnere7ebf902018-12-04 14:04:55 +00003563
3564 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3565 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003566 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003567 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003568 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003569 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003570 cm->byte_alignment, NULL, NULL, NULL)) {
3571 if (force_scaling) {
3572 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003573 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003574 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003575 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003576 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003577 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003578 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003579 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3580 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003581 alloc_frame_mvs(cm, new_fb);
3582 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003583 } else {
David Turnera21966b2018-12-05 14:48:49 +00003584 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003585 buf->buf.y_crop_width = ref->y_crop_width;
3586 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003587 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003588 ++buf->ref_count;
3589 }
3590 } else {
David Turnere7ebf902018-12-04 14:04:55 +00003591 if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003592 }
3593 }
3594}
3595
Yaowu Xuf883b422016-08-30 14:01:10 -07003596static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003597 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003598 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3599 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3600 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003601 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003602 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003603 }
3604 }
3605}
3606
Yaowu Xuf883b422016-08-30 14:01:10 -07003607static void set_mv_search_params(AV1_COMP *cpi) {
3608 const AV1_COMMON *const cm = &cpi->common;
3609 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003610
3611 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003612 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003613
3614 if (cpi->sf.mv.auto_mv_step_size) {
3615 if (frame_is_intra_only(cm)) {
3616 // Initialize max_mv_magnitude for use in the first INTER frame
3617 // after a key/intra-only frame.
3618 cpi->max_mv_magnitude = max_mv_def;
3619 } else {
3620 if (cm->show_frame) {
3621 // Allow mv_steps to correspond to twice the max mv magnitude found
3622 // in the previous frame, capped by the default max_mv_magnitude based
3623 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003624 cpi->mv_step_param = av1_init_search_range(
3625 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003626 }
3627 cpi->max_mv_magnitude = 0;
3628 }
3629 }
3630}
3631
Hui Subdf0c992019-02-14 14:52:41 -08003632static void set_screen_content_options(AV1_COMP *cpi) {
3633 AV1_COMMON *cm = &cpi->common;
3634
3635 if (cm->seq_params.force_screen_content_tools != 2) {
3636 cm->allow_screen_content_tools = cm->allow_intrabc =
3637 cm->seq_params.force_screen_content_tools;
3638 return;
3639 }
3640
3641 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3642 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3643 return;
3644 }
3645
3646 // Estimate if the source frame is screen content, based on the portion of
3647 // blocks that have few luma colors.
3648 const uint8_t *src = cpi->source->y_buffer;
3649 assert(src != NULL);
3650 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
3651 const int stride = cpi->source->y_stride;
3652 const int width = cpi->source->y_width;
3653 const int height = cpi->source->y_height;
3654 const int bd = cm->seq_params.bit_depth;
3655 const int blk_w = 16;
3656 const int blk_h = 16;
3657 // These threshold values are selected experimentally.
3658 const int color_thresh = 4;
3659 const unsigned int var_thresh = 0;
3660 // Counts of blocks with no more than color_thresh colors.
3661 int counts_1 = 0;
3662 // Counts of blocks with no more than color_thresh colors and variance larger
3663 // than var_thresh.
3664 int counts_2 = 0;
3665
3666 for (int r = 0; r + blk_h <= height; r += blk_h) {
3667 for (int c = 0; c + blk_w <= width; c += blk_w) {
3668 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3669 const uint8_t *const this_src = src + r * stride + c;
3670 const int n_colors =
3671 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3672 count_buf)
3673 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3674 if (n_colors > 1 && n_colors <= color_thresh) {
3675 ++counts_1;
3676 struct buf_2d buf;
3677 buf.stride = stride;
3678 buf.buf = (uint8_t *)this_src;
3679 const unsigned int var =
3680 use_hbd
3681 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3682 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3683 if (var > var_thresh) ++counts_2;
3684 }
3685 }
3686 }
3687
3688 // The threshold values are selected experimentally.
3689 cm->allow_screen_content_tools =
3690 counts_1 * blk_h * blk_w * 10 > width * height;
3691 // IntraBC would force loop filters off, so we use more strict rules that also
3692 // requires that the block has high variance.
3693 cm->allow_intrabc = cm->allow_screen_content_tools &&
Hui Su64a2ffb2019-04-11 16:47:13 -07003694 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08003695}
3696
Yaowu Xuf883b422016-08-30 14:01:10 -07003697static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003698 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003699 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003700 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003701 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003702 }
3703 cpi->global_motion_search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07003704
3705 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
3706 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
3707
David Turner04b70d82019-01-24 15:39:19 +00003708 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07003709 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003710 cm->interp_filter = SWITCHABLE;
3711 cm->switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003712}
3713
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003714static int get_gfu_boost_from_r0(double r0, int frames_to_key) {
3715 double factor = sqrt((double)frames_to_key);
3716 factor = AOMMIN(factor, 10.0);
3717 factor = AOMMAX(factor, 4.0);
3718 const int boost = (int)rint((200.0 + 10.0 * factor) / r0);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003719 return boost;
3720}
3721
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003722static int get_kf_boost_from_r0(double r0, int frames_to_key) {
3723 double factor = sqrt((double)frames_to_key);
3724 factor = AOMMIN(factor, 10.0);
3725 factor = AOMMAX(factor, 4.0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003726 const int boost = (int)rint((75.0 + 14.0 * factor) / r0);
3727 return boost;
3728}
3729
3730int combine_prior_with_tpl_boost(int prior_boost, int tpl_boost,
3731 int frames_to_key) {
3732 double factor = sqrt((double)frames_to_key);
3733 factor = AOMMIN(factor, 12.0);
3734 factor = AOMMAX(factor, 4.0);
3735 factor -= 4.0;
3736 int boost = (int)((factor * prior_boost + (8.0 - factor) * tpl_boost) / 8.0);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003737 return boost;
3738}
3739
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003740static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07003741 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003742 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003743
Sarah Parkere1b22012019-06-06 16:35:25 -07003744 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
Jingning Han31a0ee92019-07-15 13:56:55 -07003745
3746 const int tpl_idx = gf_group->index;
Jingning Han81d6fbb2019-07-15 10:14:13 -07003747 TplDepFrame *tpl_frame = &cpi->tpl_frame[tpl_idx];
Yue Chen4e585cc2019-06-03 14:47:16 -07003748 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003749
Yue Chen4e585cc2019-06-03 14:47:16 -07003750 if (tpl_frame->is_valid) {
3751 int tpl_stride = tpl_frame->stride;
3752 int64_t intra_cost_base = 0;
3753 int64_t mc_dep_cost_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07003754#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003755 int64_t mc_saved_base = 0;
3756 int64_t mc_count_base = 0;
Yue Chenbd934232019-08-05 14:23:39 -07003757#endif // !USE_TPL_CLASSIC_MODEL
Yue Chenb4c93f02019-08-05 13:47:31 -07003758 const int step = 1 << cpi->tpl_stats_block_mis_log2;
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07003759 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
Yue Chenb4c93f02019-08-05 13:47:31 -07003760
3761 for (int row = 0; row < cm->mi_rows; row += step) {
3762 for (int col = 0; col < mi_cols_sr; col += step) {
3763 TplDepStats *this_stats =
3764 &tpl_stats[av1_tpl_ptr_pos(cpi, row, col, tpl_stride)];
Yue Chen4e585cc2019-06-03 14:47:16 -07003765 intra_cost_base += this_stats->intra_cost;
3766 mc_dep_cost_base += this_stats->intra_cost + this_stats->mc_flow;
Yue Chenbd934232019-08-05 14:23:39 -07003767#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003768 mc_count_base += this_stats->mc_count;
3769 mc_saved_base += this_stats->mc_saved;
Yue Chenbd934232019-08-05 14:23:39 -07003770#endif // !USE_TPL_CLASSIC_MODEL
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003771 }
Yue Chen4e585cc2019-06-03 14:47:16 -07003772 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003773
Yue Chen4e585cc2019-06-03 14:47:16 -07003774 if (mc_dep_cost_base == 0) {
3775 tpl_frame->is_valid = 0;
3776 } else {
3777 aom_clear_system_state();
3778 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
3779 if (is_frame_arf_and_tpl_eligible(cpi)) {
3780 cpi->rd.arf_r0 = cpi->rd.r0;
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003781 const int gfu_boost =
3782 get_gfu_boost_from_r0(cpi->rd.arf_r0, cpi->rc.frames_to_key);
Yue Chen4e585cc2019-06-03 14:47:16 -07003783 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003784 // gfu_boost);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003785 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
3786 cpi->rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003787 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003788 // TODO(debargha): Turn off q adjustment for kf temporarily to
3789 // reduce impact on speed of encoding. Need to investigate how
3790 // to mitigate the issue.
3791 if (cpi->oxcf.rc_mode == AOM_Q) {
3792 const int kf_boost =
3793 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
3794 // printf("old kf boost %d new kf boost %d [%d]\n", cpi->rc.kf_boost,
3795 // kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003796 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
3797 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003798 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003799 }
Yue Chenbd934232019-08-05 14:23:39 -07003800#if !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003801 cpi->rd.mc_count_base =
3802 (double)mc_count_base / (cm->mi_rows * cm->mi_cols);
3803 cpi->rd.mc_saved_base =
3804 (double)mc_saved_base / (cm->mi_rows * cm->mi_cols);
Yue Chenbd934232019-08-05 14:23:39 -07003805#endif // !USE_TPL_CLASSIC_MODEL
Yue Chen4e585cc2019-06-03 14:47:16 -07003806 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003807 }
3808 }
3809}
3810
Yaowu Xuf883b422016-08-30 14:01:10 -07003811static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003812 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003813 AV1_COMMON *const cm = &cpi->common;
3814 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003815
3816 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00003817 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003818
Yue Chen4e585cc2019-06-03 14:47:16 -07003819 if (cpi->oxcf.enable_tpl_model && cpi->tpl_model_pass == 0 &&
sdengf46a1062019-08-04 18:43:50 -07003820 is_frame_tpl_eligible(cpi)) {
Yue Chen4e585cc2019-06-03 14:47:16 -07003821 process_tpl_stats_frame(cpi);
sdengf46a1062019-08-04 18:43:50 -07003822 av1_tpl_rdmult_setup(cpi);
3823 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003824
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003825 // Decide q and q bounds.
Jingning Han2e029872019-08-02 10:54:19 -07003826 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, cpi->gf_group.index,
3827 bottom_index, top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003828
James Zern01a9d702017-08-25 19:09:33 +00003829 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003830 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003831 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003832 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003833
3834 // Configure experimental use of segmentation for enhanced coding of
3835 // static regions if indicated.
3836 // Only allowed in the second pass of a two pass encode, as it requires
3837 // lagged coding, and if the relevant speed feature flag is set.
3838 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3839 configure_static_seg_features(cpi);
3840}
3841
Yaowu Xuf883b422016-08-30 14:01:10 -07003842static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08003843 AV1_COMMON *const cm = &cpi->common;
chiyotsai836b69b2019-04-09 13:41:24 -07003844 const int y_stride = cpi->scaled_source.y_stride;
Urvang Joshi510d8f62019-01-10 12:11:50 -08003845 const int y_stride_src =
3846 ((cpi->oxcf.width != cm->width || cpi->oxcf.height != cm->height) ||
3847 av1_superres_scaled(cm))
3848 ? y_stride
3849 : cpi->lookahead->buf->img.y_stride;
chiyotsai836b69b2019-04-09 13:41:24 -07003850 // Update if ss_cfg is uninitialized or the current frame has a new stride
3851 const int should_update = !cpi->ss_cfg[SS_CFG_SRC].stride ||
3852 !cpi->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
3853 (y_stride != cpi->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003854
chiyotsai836b69b2019-04-09 13:41:24 -07003855 if (!should_update) {
3856 return;
3857 }
3858
3859 if (cpi->sf.mv.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303860 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3861 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
3862 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07003863 } else {
3864 // Update the offsets in search_sites as y_stride can change due to scaled
3865 // references. This update allows NSTEP to be used on scaled references as
3866 // long as sf.mv.search_method is not DIAMOND. Currently in the codebae,
3867 // sf.mv.search_method is never set to DIAMOND.
3868 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3869 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003870 }
3871}
3872
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003873#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003874static void set_restoration_unit_size(int width, int height, int sx, int sy,
3875 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003876 (void)width;
3877 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003878 (void)sx;
3879 (void)sy;
3880#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3881 int s = AOMMIN(sx, sy);
3882#else
3883 int s = 0;
3884#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3885
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003886 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003887 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003888 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003889 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003890 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3891 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003892}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003893
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303894static void init_ref_frame_bufs(AV1_COMP *cpi) {
3895 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07003896 int i;
3897 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00003898 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07003899 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00003900 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07003901 }
3902 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07003903 pool->frame_bufs[i].ref_count = 0;
3904 }
RogerZhou86902d02018-02-28 15:29:16 -08003905 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003906 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303907 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08003908 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003909 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003910}
3911
Yaowu Xud3e7c682017-12-21 14:08:25 -08003912static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003913 int subsampling_x, int subsampling_y) {
3914 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003915 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07003916
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003917 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3918 seq_params->subsampling_x != subsampling_x ||
3919 seq_params->subsampling_y != subsampling_y) {
3920 seq_params->subsampling_x = subsampling_x;
3921 seq_params->subsampling_y = subsampling_y;
3922 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003923
3924 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303925 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07003926 alloc_util_frame_buffers(cpi);
3927
3928 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3929
3930 cpi->initial_width = cm->width;
3931 cpi->initial_height = cm->height;
3932 cpi->initial_mbs = cm->MBs;
3933 }
3934}
3935
3936// Returns 1 if the assigned width or height was <= 0.
3937static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3938 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003939 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003940 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3941 cm->seq_params.subsampling_x,
3942 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003943
3944 if (width <= 0 || height <= 0) return 1;
3945
3946 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003947 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003948
3949 if (cpi->initial_width && cpi->initial_height &&
3950 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3951 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003952 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003953 alloc_compressor_data(cpi);
3954 realloc_segmentation_maps(cpi);
3955 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003956 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003957 update_frame_size(cpi);
3958
3959 return 0;
3960}
3961
David Turner475a3132019-01-18 15:17:17 +00003962void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003963 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003964 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003965 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003966 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003967 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003968
Fergus Simpsonbc189932017-05-16 17:02:39 -07003969 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003970 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003971 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003972 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003973 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07003974 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003975 }
3976
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003977 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003978 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003979 }
3980
David Turnere7ebf902018-12-04 14:04:55 +00003981 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003982
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303983 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05303984 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3985 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303986 cm->num_allocated_above_contexts < cm->tile_rows) {
3987 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3988 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3989 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3990 "Failed to allocate context buffers");
3991 }
3992
Yaowu Xuc27fc142016-08-22 16:08:15 -07003993 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003994 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00003995 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
3996 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05303997 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003998 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003999 "Failed to allocate frame buffer");
4000
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004001 const int frame_width = cm->superres_upscaled_width;
4002 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004003 set_restoration_unit_size(frame_width, frame_height,
4004 seq_params->subsampling_x,
4005 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004006 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01004007 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01004008
4009 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004010 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004011 init_motion_estimation(cpi);
4012
4013 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004014 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00004015 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00004016 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
4017 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00004018 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004019 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00004020 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004021 }
4022 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07004023
Hui Su5ebd8702018-01-08 18:09:20 -08004024 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08004025 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004026
4027 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
4028}
4029
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004030static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
4031 // Choose an arbitrary random number
4032 static unsigned int seed = 56789;
4033 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004034 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4035 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004036
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07004037 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004038 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004039 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004040 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004041 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004042 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004043 else
Urvang Joshide71d142017-10-05 12:12:15 -07004044 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004045 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004046 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004047 default: assert(0);
4048 }
Urvang Joshide71d142017-10-05 12:12:15 -07004049 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004050}
4051
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004052#if CONFIG_SUPERRES_IN_RECODE
4053static int superres_in_recode_allowed(const AV1_COMP *const cpi) {
4054 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
4055 // Empirically found to not be beneficial for AOM_Q mode and images coding.
4056 return oxcf->superres_mode == SUPERRES_AUTO &&
4057 (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ) &&
4058 cpi->rc.frames_to_key > 1;
4059}
4060#endif // CONFIG_SUPERRES_IN_RECODE
4061
Urvang Joshi510d8f62019-01-10 12:11:50 -08004062#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004063#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
4064#define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
Urvang Joshi510d8f62019-01-10 12:11:50 -08004065#define SUPERRES_ENERGY_BY_AC_THRESH 0.2
4066
4067static double get_energy_by_q2_thresh(const GF_GROUP *gf_group,
4068 const RATE_CONTROL *rc) {
4069 // TODO(now): Return keyframe thresh * factor based on frame type / pyramid
4070 // level.
4071 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
4072 return SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME;
4073 } else if (gf_group->update_type[gf_group->index] == KF_UPDATE) {
4074 if (rc->frames_to_key <= 1)
4075 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO;
4076 else
4077 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME;
4078 } else {
4079 assert(0);
4080 }
4081 return 0;
4082}
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004083
4084static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004085 double threshq,
4086 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004087 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004088 const double tq = threshq * q * q;
4089 const double tp = threshp * energy[1];
4090 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004091 int k;
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004092 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004093 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004094 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004095 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004096}
4097
Urvang Joshi510d8f62019-01-10 12:11:50 -08004098static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex,
4099 int sr_kf, int sr_arf) {
4100 // Use superres for Key-frames and Alt-ref frames only.
4101 const GF_GROUP *gf_group = &cpi->gf_group;
4102 if (gf_group->update_type[gf_group->index] != KF_UPDATE &&
4103 gf_group->update_type[gf_group->index] != ARF_UPDATE) {
4104 return SCALE_NUMERATOR;
4105 }
4106 if (gf_group->update_type[gf_group->index] == KF_UPDATE && !sr_kf) {
4107 return SCALE_NUMERATOR;
4108 }
4109 if (gf_group->update_type[gf_group->index] == ARF_UPDATE && !sr_arf) {
4110 return SCALE_NUMERATOR;
4111 }
4112
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004113 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004114 analyze_hor_freq(cpi, energy);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004115
4116 const double energy_by_q2_thresh =
4117 get_energy_by_q2_thresh(gf_group, &cpi->rc);
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004118 int denom = get_superres_denom_from_qindex_energy(
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004119 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004120 /*
4121 printf("\nenergy = [");
4122 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
4123 printf("]\n");
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07004124 printf("boost = %d\n",
4125 (gf_group->update_type[gf_group->index] == KF_UPDATE)
4126 ? cpi->rc.kf_boost
4127 : cpi->rc.gfu_boost);
4128 printf("denom = %d\n", denom);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004129 */
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004130#if CONFIG_SUPERRES_IN_RECODE
4131 if (superres_in_recode_allowed(cpi)) {
4132 // Force superres to be tried in the recode loop, as full-res is also going
4133 // to be tried anyway.
4134 denom = AOMMAX(denom, SCALE_NUMERATOR + 1);
4135 }
4136#endif // CONFIG_SUPERRES_IN_RECODE
Urvang Joshi510d8f62019-01-10 12:11:50 -08004137 return denom;
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004138}
4139
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004140static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4141 // Choose an arbitrary random number
4142 static unsigned int seed = 34567;
4143 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004144 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4145 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004146
4147 // Make sure that superres mode of the frame is consistent with the
4148 // sequence-level flag.
4149 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4150 cpi->common.seq_params.enable_superres));
4151 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4152 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004153
4154 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004155 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004156 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004157 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004158 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004159 else
Urvang Joshide71d142017-10-05 12:12:15 -07004160 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004161 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004162 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004163 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004164 // Do not use superres when screen content tools are used.
4165 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004166 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4167 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004168
4169 // Now decide the use of superres based on 'q'.
Urvang Joshi2c92b072018-03-19 17:23:31 -07004170 int bottom_index, top_index;
4171 const int q = av1_rc_pick_q_and_bounds(
Jingning Han2e029872019-08-02 10:54:19 -07004172 cpi, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
4173 &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004174
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004175 const int qthresh = (frame_is_intra_only(&cpi->common))
4176 ? oxcf->superres_kf_qthresh
4177 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08004178 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004179 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004180 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004181 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004182 }
4183 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004184 }
Urvang Joshi36a83732019-01-31 15:31:57 -08004185 case SUPERRES_AUTO: {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004186 // Do not use superres when screen content tools are used.
Urvang Joshi36a83732019-01-31 15:31:57 -08004187 if (cpi->common.allow_screen_content_tools) break;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004188 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4189 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi36a83732019-01-31 15:31:57 -08004190
4191 // Now decide the use of superres based on 'q'.
4192 int bottom_index, top_index;
4193 const int q = av1_rc_pick_q_and_bounds(
Jingning Han2e029872019-08-02 10:54:19 -07004194 cpi, cpi->oxcf.width, cpi->oxcf.height, cpi->gf_group.index,
4195 &bottom_index, &top_index);
Urvang Joshi36a83732019-01-31 15:31:57 -08004196
4197 const int qthresh = 128;
4198 if (q <= qthresh) {
4199 new_denom = SCALE_NUMERATOR;
4200 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004201 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Urvang Joshi36a83732019-01-31 15:31:57 -08004202 }
4203 break;
4204 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004205 default: assert(0);
4206 }
Urvang Joshide71d142017-10-05 12:12:15 -07004207 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004208}
4209
Urvang Joshide71d142017-10-05 12:12:15 -07004210static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4211 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4212}
4213
4214static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004215 // Only need to check the width, as scaling is horizontal only.
4216 (void)oheight;
4217 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004218}
4219
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004220static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004221 SUPERRES_MODE superres_mode, int owidth,
4222 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004223 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004224 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004225 }
4226
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004227 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004228 int resize_denom =
4229 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4230 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4231
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004232 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004233 // Alter superres scale as needed to enforce conformity.
4234 rsz->superres_denom =
4235 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4236 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4237 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004238 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004239 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004240 // Alter resize scale as needed to enforce conformity.
4241 resize_denom =
4242 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004243 rsz->resize_width = owidth;
4244 rsz->resize_height = oheight;
4245 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004246 resize_denom);
4247 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4248 if (resize_denom > SCALE_NUMERATOR) {
4249 --resize_denom;
4250 rsz->resize_width = owidth;
4251 rsz->resize_height = oheight;
4252 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4253 resize_denom);
4254 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004255 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004256 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004257 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004258 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004259 if (resize_denom > rsz->superres_denom)
4260 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004261 else
Urvang Joshide71d142017-10-05 12:12:15 -07004262 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004263 rsz->resize_width = owidth;
4264 rsz->resize_height = oheight;
4265 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004266 resize_denom);
4267 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4268 (resize_denom > SCALE_NUMERATOR ||
4269 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004270 } else { // We are allowed to alter neither resize scale nor superres
4271 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004272 return 0;
4273 }
Urvang Joshide71d142017-10-05 12:12:15 -07004274 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004275}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004276
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004277// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004278static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004279 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004280 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshi510d8f62019-01-10 12:11:50 -08004281 int resize_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004282 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004283 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4284 rsz.resize_width = cpi->resize_pending_width;
4285 rsz.resize_height = cpi->resize_pending_height;
4286 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4287 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004288 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004289 rsz.resize_width = cpi->oxcf.width;
4290 rsz.resize_height = cpi->oxcf.height;
4291 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004292 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004293 }
Urvang Joshide71d142017-10-05 12:12:15 -07004294 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004295 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4296 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004297 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004298 return rsz;
4299}
4300
Urvang Joshi22b150b2019-01-10 14:32:32 -08004301static void setup_frame_size_from_params(AV1_COMP *cpi,
4302 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004303 int encode_width = rsz->resize_width;
4304 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004305
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004306 AV1_COMMON *cm = &cpi->common;
4307 cm->superres_upscaled_width = encode_width;
4308 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004309 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004310 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4311 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004312 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004313}
4314
David Turnerdedd8ff2019-01-23 13:59:46 +00004315void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004316 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004317 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004318 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004319 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004320 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004321
Yaowu Xu7e450882019-04-30 15:09:18 -07004322 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004323}
4324
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004325static void superres_post_encode(AV1_COMP *cpi) {
4326 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004327 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004328
Cheng Chen09c83a52018-06-05 12:27:36 -07004329 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004330
Urvang Joshid6b5d512018-03-20 13:34:38 -07004331 assert(cpi->oxcf.enable_superres);
4332 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004333 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004334
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004335 av1_superres_upscale(cm, NULL);
4336
4337 // If regular resizing is occurring the source will need to be downscaled to
4338 // match the upscaled superres resolution. Otherwise the original source is
4339 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004340 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004341 cpi->source = cpi->unscaled_source;
4342 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4343 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004344 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4345 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004346 // Do downscale. cm->(width|height) has been updated by
4347 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004348 if (aom_realloc_frame_buffer(
4349 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004350 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4351 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4352 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004353 aom_internal_error(
4354 &cm->error, AOM_CODEC_MEM_ERROR,
4355 "Failed to reallocate scaled source buffer for superres");
4356 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4357 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004358 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004359 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004360 cpi->source = &cpi->scaled_source;
4361 }
4362}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004363
Yue Chen92271e12019-07-09 16:28:02 -07004364static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4365 MACROBLOCKD *xd, int use_restoration,
4366 int use_cdef) {
logangwf95c9162019-02-20 12:02:32 -08004367 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004368 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004369
logangwf95c9162019-02-20 12:02:32 -08004370 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004371#if CONFIG_COLLECT_COMPONENT_TIMING
4372 start_timing(cpi, cdef_time);
4373#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004374 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004375 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
Hui Sub93880a2019-06-27 18:29:30 -07004376 cpi->sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004377
4378 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004379 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004380#if CONFIG_COLLECT_COMPONENT_TIMING
4381 end_timing(cpi, cdef_time);
4382#endif
logangwf95c9162019-02-20 12:02:32 -08004383 } else {
4384 cm->cdef_info.cdef_bits = 0;
4385 cm->cdef_info.cdef_strengths[0] = 0;
4386 cm->cdef_info.nb_cdef_strengths = 1;
4387 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004388 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004389
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004390 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004391
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004392#if CONFIG_COLLECT_COMPONENT_TIMING
4393 start_timing(cpi, loop_restoration_time);
4394#endif
logangwf95c9162019-02-20 12:02:32 -08004395 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004396 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004397 av1_pick_filter_restoration(cpi->source, cpi);
4398 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4399 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4400 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304401 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004402 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304403 cpi->workers, cpi->num_workers,
4404 &cpi->lr_row_sync, &cpi->lr_ctxt);
4405 else
David Turnerc29e1a92018-12-06 14:10:14 +00004406 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304407 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004408 }
logangwf95c9162019-02-20 12:02:32 -08004409 } else {
4410 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4411 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4412 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004413 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004414#if CONFIG_COLLECT_COMPONENT_TIMING
4415 end_timing(cpi, loop_restoration_time);
4416#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004417}
4418
Yue Chen92271e12019-07-09 16:28:02 -07004419static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4420 const int num_planes = av1_num_planes(cm);
4421 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4422
4423 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4424 cm->coded_lossless && cm->all_lossless));
4425
4426 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4427 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4428 !cm->large_scale_tile;
4429 const int use_restoration = cm->seq_params.enable_restoration &&
4430 !cm->all_lossless && !cm->large_scale_tile;
4431
4432 struct loopfilter *lf = &cm->lf;
4433
4434#if CONFIG_COLLECT_COMPONENT_TIMING
4435 start_timing(cpi, loop_filter_time);
4436#endif
4437 if (use_loopfilter) {
4438 aom_clear_system_state();
4439 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4440 } else {
4441 lf->filter_level[0] = 0;
4442 lf->filter_level[1] = 0;
4443 }
4444
4445 if (lf->filter_level[0] || lf->filter_level[1]) {
4446 if (cpi->num_workers > 1)
4447 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4448#if CONFIG_LPF_MASK
4449 0,
4450#endif
4451 cpi->workers, cpi->num_workers,
4452 &cpi->lf_row_sync);
4453 else
4454 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4455#if CONFIG_LPF_MASK
4456 0,
4457#endif
4458 0, num_planes, 0);
4459 }
4460#if CONFIG_COLLECT_COMPONENT_TIMING
4461 end_timing(cpi, loop_filter_time);
4462#endif
4463
4464 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4465}
4466
David Turnerf2b334c2018-12-13 13:00:55 +00004467static void fix_interp_filter(InterpFilter *const interp_filter,
4468 const FRAME_COUNTS *const counts) {
4469 if (*interp_filter == SWITCHABLE) {
4470 // Check to see if only one of the filters is actually used
4471 int count[SWITCHABLE_FILTERS] = { 0 };
4472 int num_filters_used = 0;
4473 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4474 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4475 count[i] += counts->switchable_interp[j][i];
4476 num_filters_used += (count[i] > 0);
4477 }
4478 if (num_filters_used == 1) {
4479 // Only one filter is used. So set the filter at frame level
4480 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4481 if (count[i]) {
4482 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4483 break;
4484 }
4485 }
4486 }
4487 }
4488}
4489
David Turner996b2c12018-12-07 15:52:30 +00004490static void finalize_encoded_frame(AV1_COMP *const cpi) {
4491 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004492 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004493
David Turner99e990e2018-12-10 12:54:26 +00004494 if (!cm->seq_params.reduced_still_picture_hdr &&
4495 encode_show_existing_frame(cm)) {
4496 RefCntBuffer *const frame_to_show =
4497 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4498
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004499 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004500 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4501 "Buffer does not contain a reconstructed frame");
4502 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004503 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004504 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004505 }
David Turner08f909c2018-12-18 13:29:14 +00004506
4507 if (!encode_show_existing_frame(cm) &&
4508 cm->seq_params.film_grain_params_present &&
4509 (cm->show_frame || cm->showable_frame)) {
4510 // Copy the current frame's film grain params to the its corresponding
4511 // RefCntBuffer slot.
4512 cm->cur_frame->film_grain_params = cm->film_grain_params;
4513
4514 // We must update the parameters if this is not an INTER_FRAME
4515 if (current_frame->frame_type != INTER_FRAME)
4516 cm->cur_frame->film_grain_params.update_parameters = 1;
4517
4518 // Iterate the random seed for the next frame.
4519 cm->film_grain_params.random_seed += 3381;
4520 if (cm->film_grain_params.random_seed == 0)
4521 cm->film_grain_params.random_seed = 7391;
4522 }
David Turnerf2b334c2018-12-13 13:00:55 +00004523
4524 // Initialise all tiles' contexts from the global frame context
4525 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4526 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4527 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4528 cpi->tile_data[tile_idx].tctx = *cm->fc;
4529 }
4530 }
4531
4532 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004533}
4534
Urvang Joshi57643372019-02-21 11:10:57 -08004535static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4536 int top_index, int bottom_index) {
4537 const AV1_COMMON *const cm = &cpi->common;
4538 const RATE_CONTROL *const rc = &cpi->rc;
4539
4540 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4541
4542 int q_regulated =
4543 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4544 AOMMAX(q_high, top_index), cm->width, cm->height);
4545
4546 int retries = 0;
4547 while (q_regulated < q_low && retries < 10) {
4548 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4549 q_regulated =
4550 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4551 AOMMAX(q_high, top_index), cm->width, cm->height);
4552 retries++;
4553 }
4554 return q_regulated;
4555}
4556
4557static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4558 int top_index, int bottom_index) {
4559 const AV1_COMMON *const cm = &cpi->common;
4560 const RATE_CONTROL *const rc = &cpi->rc;
4561
4562 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4563 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4564 top_index, cm->width, cm->height);
4565
4566 int retries = 0;
4567 while (q_regulated > q_high && retries < 10) {
4568 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4569 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4570 top_index, cm->width, cm->height);
4571 retries++;
4572 }
4573 return q_regulated;
4574}
4575
David Turner2f3b5df2019-01-02 14:30:50 +00004576// Called after encode_with_recode_loop() has just encoded a frame and packed
4577// its bitstream. This function works out whether we under- or over-shot
4578// our bitrate target and adjusts q as appropriate. Also decides whether
4579// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07004580static void recode_loop_update_q(
4581 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
4582 int *const q_high, const int top_index, const int bottom_index,
4583 int *const undershoot_seen, int *const overshoot_seen,
4584 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00004585 AV1_COMMON *const cm = &cpi->common;
4586 RATE_CONTROL *const rc = &cpi->rc;
4587
Hui Suef139e12019-05-20 15:51:22 -07004588 const int min_cr = cpi->oxcf.min_cr;
4589 if (min_cr > 0) {
4590 aom_clear_system_state();
4591 const double compression_ratio =
4592 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
4593 const double target_cr = min_cr / 100.0;
4594 if (compression_ratio < target_cr) {
4595 *low_cr_seen = 1;
4596 if (*q < rc->worst_quality) {
4597 const double cr_ratio = target_cr / compression_ratio;
4598 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
4599 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
4600 *q_low = AOMMAX(*q, *q_low);
4601 *q_high = AOMMAX(*q, *q_high);
4602 *loop = 1;
4603 }
4604 }
4605 if (*low_cr_seen) return;
4606 }
4607
4608 if (cpi->oxcf.rc_mode == AOM_Q) return;
4609
David Turner2f3b5df2019-01-02 14:30:50 +00004610 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4611 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4612 &frame_under_shoot_limit,
4613 &frame_over_shoot_limit);
4614 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4615
4616 if ((cm->current_frame.frame_type == KEY_FRAME) &&
4617 rc->this_key_frame_forced &&
4618 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4619 int last_q = *q;
4620 int64_t kf_err;
4621
4622 int64_t high_err_target = cpi->ambient_err;
4623 int64_t low_err_target = cpi->ambient_err >> 1;
4624
Jerome Jiangfa1d1732019-08-06 10:31:20 -07004625#if CONFIG_AV1_HIGHBITDEPTH
David Turner2f3b5df2019-01-02 14:30:50 +00004626 if (cm->seq_params.use_highbitdepth) {
4627 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4628 } else {
4629 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4630 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07004631#else
4632 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4633#endif
David Turner2f3b5df2019-01-02 14:30:50 +00004634 // Prevent possible divide by zero error below for perfect KF
4635 kf_err += !kf_err;
4636
4637 // The key frame is not good enough or we can afford
4638 // to make it better without undue risk of popping.
4639 if ((kf_err > high_err_target &&
4640 rc->projected_frame_size <= frame_over_shoot_limit) ||
4641 (kf_err > low_err_target &&
4642 rc->projected_frame_size <= frame_under_shoot_limit)) {
4643 // Lower q_high
4644 *q_high = *q > *q_low ? *q - 1 : *q_low;
4645
4646 // Adjust Q
4647 *q = (int)((*q * high_err_target) / kf_err);
4648 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4649 } else if (kf_err < low_err_target &&
4650 rc->projected_frame_size >= frame_under_shoot_limit) {
4651 // The key frame is much better than the previous frame
4652 // Raise q_low
4653 *q_low = *q < *q_high ? *q + 1 : *q_high;
4654
4655 // Adjust Q
4656 *q = (int)((*q * low_err_target) / kf_err);
4657 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4658 }
4659
4660 // Clamp Q to upper and lower limits:
4661 *q = clamp(*q, *q_low, *q_high);
4662
4663 *loop = *q != last_q;
4664 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4665 frame_under_shoot_limit, *q,
4666 AOMMAX(*q_high, top_index), bottom_index)) {
4667 // Is the projected frame size out of range and are we allowed
4668 // to attempt to recode.
4669 int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004670
4671 // Frame size out of permitted range:
4672 // Update correction factor & compute new Q to try...
4673 // Frame is too large
4674 if (rc->projected_frame_size > rc->this_frame_target) {
4675 // Special case if the projected size is > the max allowed.
Urvang Joshi7344d3e2019-07-25 17:24:57 -07004676 if (*q == *q_high &&
4677 rc->projected_frame_size >= rc->max_frame_bandwidth) {
4678 const double q_val_high_current =
4679 av1_convert_qindex_to_q(*q_high, cm->seq_params.bit_depth);
4680 const double q_val_high_new =
4681 q_val_high_current *
4682 ((double)rc->projected_frame_size / rc->max_frame_bandwidth);
4683 *q_high = av1_find_qindex(q_val_high_new, cm->seq_params.bit_depth,
4684 rc->best_quality, rc->worst_quality);
4685 }
David Turner2f3b5df2019-01-02 14:30:50 +00004686
4687 // Raise Qlow as to at least the current value
4688 *q_low = *q < *q_high ? *q + 1 : *q_high;
4689
Urvang Joshi57643372019-02-21 11:10:57 -08004690 if (*undershoot_seen || loop_at_this_size > 2 ||
4691 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004692 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4693
4694 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004695 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4696 const int q_mid = (*q_high + *q_low + 1) / 2;
4697 const int q_regulated = get_regulated_q_overshoot(
4698 cpi, *q_low, *q_high, top_index, bottom_index);
4699 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4700 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4701 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00004702 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004703 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
4704 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00004705 }
4706
4707 *overshoot_seen = 1;
4708 } else {
4709 // Frame is too small
4710 *q_high = *q > *q_low ? *q - 1 : *q_low;
4711
Urvang Joshi57643372019-02-21 11:10:57 -08004712 if (*overshoot_seen || loop_at_this_size > 2 ||
4713 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004714 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4715 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004716 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4717 const int q_mid = (*q_high + *q_low) / 2;
4718 const int q_regulated =
4719 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4720 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4721 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4722 *q = (q_mid + q_regulated) / 2;
4723
4724 // Special case reset for qlow for constrained quality.
4725 // This should only trigger where there is very substantial
4726 // undershoot on a frame and the auto cq level is above
4727 // the user passsed in value.
4728 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
4729 *q_low = *q;
4730 }
David Turner2f3b5df2019-01-02 14:30:50 +00004731 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004732 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4733
David Turner2f3b5df2019-01-02 14:30:50 +00004734 // Special case reset for qlow for constrained quality.
4735 // This should only trigger where there is very substantial
4736 // undershoot on a frame and the auto cq level is above
4737 // the user passsed in value.
4738 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
4739 *q_low = *q;
4740 }
David Turner2f3b5df2019-01-02 14:30:50 +00004741 }
4742
4743 *undershoot_seen = 1;
4744 }
4745
4746 // Clamp Q to upper and lower limits:
4747 *q = clamp(*q, *q_low, *q_high);
4748
4749 *loop = (*q != last_q);
4750 } else {
4751 *loop = 0;
4752 }
4753}
4754
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304755static int get_interp_filter_selected(const AV1_COMMON *const cm,
4756 MV_REFERENCE_FRAME ref,
4757 InterpFilter ifilter) {
4758 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
4759 if (buf == NULL) return 0;
4760 return buf->interp_filter_selected[ifilter];
4761}
4762
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304763static uint16_t setup_interp_filter_search_mask(AV1_COMP *cpi) {
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304764 const AV1_COMMON *const cm = &cpi->common;
4765 int ref_total[REF_FRAMES] = { 0 };
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304766 uint16_t mask = ALLOW_ALL_INTERP_FILT_MASK;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304767
4768 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304769 return mask;
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304770
4771 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
4772 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4773 ++ifilter) {
4774 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
4775 }
4776 }
4777 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
4778 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
4779 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
4780
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304781 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4782 ++ifilter) {
4783 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
4784 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
4785 int filter_score =
4786 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
4787 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
4788 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
4789 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
4790 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
4791 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
Satish Kumar Suman0389a492019-07-31 13:35:58 +05304792 if (filter_score < ref_total_total) {
4793 DUAL_FILTER_TYPE filt_type = ifilter + SWITCHABLE_FILTERS * ifilter;
4794 reset_interp_filter_allowed_mask(&mask, filt_type);
4795 }
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304796 }
4797 }
4798 return mask;
4799}
4800
Tom Finegane4099e32018-01-23 12:01:51 -08004801static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004802 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004803 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004804 const int allow_recode = (cpi->sf.recode_loop != DISALLOW_RECODE);
Hui Sua1d71842019-07-31 12:02:24 -07004805 // Must allow recode if minimum compression ratio is set.
4806 assert(IMPLIES(cpi->oxcf.min_cr > 0, allow_recode));
Yaowu Xuc27fc142016-08-22 16:08:15 -07004807
4808 set_size_independent_vars(cpi);
Satish Kumar Suman829c1682019-07-31 11:30:07 +05304809 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4810 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004811 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004812
David Turnerdedd8ff2019-01-23 13:59:46 +00004813 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004814
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004815#if CONFIG_SUPERRES_IN_RECODE
4816 if (superres_in_recode_allowed(cpi) &&
4817 cm->superres_scale_denominator == SCALE_NUMERATOR) {
4818 // Superres won't be picked, so no need to try, as we will go through
4819 // another recode loop for full-resolution after this anyway.
4820 return -1;
4821 }
4822#endif // CONFIG_SUPERRES_IN_RECODE
4823
David Turner2f3b5df2019-01-02 14:30:50 +00004824 int top_index = 0, bottom_index = 0;
4825 int q = 0, q_low = 0, q_high = 0;
4826 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4827 q_low = bottom_index;
4828 q_high = top_index;
4829
4830 // Loop variables
4831 int loop_count = 0;
4832 int loop_at_this_size = 0;
4833 int loop = 0;
4834 int overshoot_seen = 0;
4835 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07004836 int low_cr_seen = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004837
4838#if CONFIG_COLLECT_COMPONENT_TIMING
4839 printf("\n Encoding a frame:");
4840#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004841 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004842 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004843 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004844
Urvang Joshif1fa6862018-01-08 16:39:33 -08004845 // if frame was scaled calculate global_motion_search again if already
4846 // done
David Turner2f3b5df2019-01-02 14:30:50 +00004847 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004848 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00004849 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004850 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00004851 }
4852 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004853 cpi->source =
4854 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004855 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004856 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4857 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004858 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004859
David Turner2f3b5df2019-01-02 14:30:50 +00004860 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004861 if (loop_count > 0) {
4862 release_scaled_references(cpi);
4863 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004864 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004865 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004866 av1_set_quantizer(cm, q);
Yue Chena7d80482019-01-31 09:48:09 -08004867 av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08004868
4869 av1_set_variance_partition_thresholds(cpi, q, 0);
4870
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004871 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
4872 // cm->current_frame.frame_number, cm->show_frame, q,
4873 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004874
David Turner2f3b5df2019-01-02 14:30:50 +00004875 if (loop_count == 0) {
4876 setup_frame(cpi);
4877 } else if (get_primary_ref_frame_buf(cm) == NULL) {
4878 // Base q-index may have changed, so we need to assign proper default coef
4879 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07004880 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004881 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004882 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004883
Yaowu Xuc27fc142016-08-22 16:08:15 -07004884 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004885 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004886 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004887 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00004888 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
4889 suppress_active_map(cpi);
4890 av1_cyclic_refresh_setup(cpi);
4891 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004892 }
David Turner2f3b5df2019-01-02 14:30:50 +00004893
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004894 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004895 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004896 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004897 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07004898 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004899 }
David Barkercab37552018-03-21 11:56:24 +00004900 } else {
4901 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004902 }
David Barkercab37552018-03-21 11:56:24 +00004903 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004904
David Turner2f3b5df2019-01-02 14:30:50 +00004905 if (allow_recode) save_coding_context(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004906#if CONFIG_COLLECT_COMPONENT_TIMING
4907 start_timing(cpi, av1_encode_frame_time);
4908#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004909 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004910 av1_encode_frame(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004911#if CONFIG_COLLECT_COMPONENT_TIMING
4912 end_timing(cpi, av1_encode_frame_time);
4913#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004914
Yaowu Xuf883b422016-08-30 14:01:10 -07004915 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004916
4917 // Dummy pack of the bitstream using up to date stats to get an
4918 // accurate estimate of output frame size to determine if we need
4919 // to recode.
Hui Sua1d71842019-07-31 12:02:24 -07004920 const int do_dummy_pack =
4921 cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF || cpi->oxcf.min_cr > 0;
4922 if (do_dummy_pack) {
Jingning Han8f661602017-08-19 08:16:50 -07004923 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004924
David Turner996b2c12018-12-07 15:52:30 +00004925 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00004926 int largest_tile_id = 0; // Output from bitstream: unused here
4927 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004928 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004929
4930 rc->projected_frame_size = (int)(*size) << 3;
4931 restore_coding_context(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004932 }
4933
Hui Suef139e12019-05-20 15:51:22 -07004934 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00004935 // Update q and decide whether to do a recode loop
4936 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
4937 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07004938 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004939 }
4940
4941 // Special case for overlay frame.
4942 if (rc->is_src_frame_alt_ref &&
4943 rc->projected_frame_size < rc->max_frame_bandwidth)
4944 loop = 0;
4945
David Turner2f3b5df2019-01-02 14:30:50 +00004946 if (allow_recode && !cpi->sf.gm_disable_recode &&
4947 recode_loop_test_global_motion(cpi)) {
4948 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004949 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004950
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004951 if (cpi->tpl_model_pass == 1) {
4952 assert(cpi->oxcf.enable_tpl_model == 2);
Debargha Mukherjee347c64d2019-05-08 13:53:46 -07004953 av1_tpl_setup_forward_stats(cpi);
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004954 cpi->tpl_model_pass = 0;
4955 loop = 1;
4956 }
4957
Yaowu Xuc27fc142016-08-22 16:08:15 -07004958 if (loop) {
4959 ++loop_count;
4960 ++loop_at_this_size;
4961
4962#if CONFIG_INTERNAL_STATS
4963 ++cpi->tot_recode_hits;
4964#endif
4965 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004966#if CONFIG_COLLECT_COMPONENT_TIMING
4967 if (loop) printf("\n Recoding:");
4968#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004969 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004970
4971 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004972}
4973
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07004974static int encode_with_recode_loop_and_filter(AV1_COMP *cpi, size_t *size,
4975 uint8_t *dest, int64_t *sse,
4976 int64_t *rate,
4977 int *largest_tile_id) {
4978#if CONFIG_COLLECT_COMPONENT_TIMING
4979 start_timing(cpi, encode_with_recode_loop_time);
4980#endif
4981 int err = encode_with_recode_loop(cpi, size, dest);
4982#if CONFIG_COLLECT_COMPONENT_TIMING
4983 end_timing(cpi, encode_with_recode_loop_time);
4984#endif
4985 if (err != AOM_CODEC_OK) {
4986 if (err == -1) {
4987 // special case as described in encode_with_recode_loop().
4988 // Encoding was skipped.
4989 err = AOM_CODEC_OK;
4990 if (sse != NULL) *sse = INT64_MAX;
4991 if (rate != NULL) *rate = INT64_MAX;
4992 *largest_tile_id = 0;
4993 }
4994 return err;
4995 }
4996
4997#ifdef OUTPUT_YUV_SKINMAP
4998 if (cpi->common.current_frame.frame_number > 1) {
4999 av1_compute_skin_map(cpi, yuv_skinmap_file);
5000 }
5001#endif // OUTPUT_YUV_SKINMAP
5002
5003 AV1_COMMON *const cm = &cpi->common;
5004 SequenceHeader *const seq_params = &cm->seq_params;
5005
5006 // Special case code to reduce pulsing when key frames are forced at a
5007 // fixed interval. Note the reconstruction error if it is the frame before
5008 // the force key frame
5009 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005010#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005011 if (seq_params->use_highbitdepth) {
5012 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
5013 } else {
5014 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5015 }
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005016#else
5017 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5018#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005019 }
5020
5021 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5022 cm->cur_frame->buf.transfer_characteristics =
5023 seq_params->transfer_characteristics;
5024 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5025 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5026 cm->cur_frame->buf.chroma_sample_position =
5027 seq_params->chroma_sample_position;
5028 cm->cur_frame->buf.color_range = seq_params->color_range;
5029 cm->cur_frame->buf.render_width = cm->render_width;
5030 cm->cur_frame->buf.render_height = cm->render_height;
5031
5032 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5033 // off.
5034
5035 // Pick the loop filter level for the frame.
5036 if (!cm->allow_intrabc) {
5037 loopfilter_frame(cpi, cm);
5038 } else {
5039 cm->lf.filter_level[0] = 0;
5040 cm->lf.filter_level[1] = 0;
5041 cm->cdef_info.cdef_bits = 0;
5042 cm->cdef_info.cdef_strengths[0] = 0;
5043 cm->cdef_info.nb_cdef_strengths = 1;
5044 cm->cdef_info.cdef_uv_strengths[0] = 0;
5045 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5046 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5047 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
5048 }
5049
5050 // TODO(debargha): Fix mv search range on encoder side
5051 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5052 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5053
5054#ifdef OUTPUT_YUV_REC
5055 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
5056#endif
5057
5058 finalize_encoded_frame(cpi);
5059 // Build the bitstream
5060#if CONFIG_COLLECT_COMPONENT_TIMING
5061 start_timing(cpi, av1_pack_bitstream_final_time);
5062#endif
5063 if (av1_pack_bitstream(cpi, dest, size, largest_tile_id) != AOM_CODEC_OK)
5064 return AOM_CODEC_ERROR;
5065#if CONFIG_COLLECT_COMPONENT_TIMING
5066 end_timing(cpi, av1_pack_bitstream_final_time);
5067#endif
5068
5069 // Compute sse and rate.
5070 if (sse != NULL) {
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005071#if CONFIG_AV1_HIGHBITDEPTH
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005072 *sse = (seq_params->use_highbitdepth)
5073 ? aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf)
5074 : aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Jerome Jiangfa1d1732019-08-06 10:31:20 -07005075#else
5076 *sse = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
5077#endif
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005078 }
5079 if (rate != NULL) {
5080 const int64_t bits = (*size << 3);
5081 *rate = (bits << 5); // To match scale.
5082 }
5083 return AOM_CODEC_OK;
5084}
5085
5086#if CONFIG_SUPERRES_IN_RECODE
5087
5088static void save_cur_buf(AV1_COMP *cpi) {
5089 CODING_CONTEXT *const cc = &cpi->coding_context;
5090 AV1_COMMON *cm = &cpi->common;
5091 const YV12_BUFFER_CONFIG *ybf = &cm->cur_frame->buf;
5092 memset(&cc->copy_buffer, 0, sizeof(cc->copy_buffer));
5093 if (aom_alloc_frame_buffer(&cc->copy_buffer, ybf->y_crop_width,
5094 ybf->y_crop_height, ybf->subsampling_x,
5095 ybf->subsampling_y,
5096 ybf->flags & YV12_FLAG_HIGHBITDEPTH, ybf->border,
5097 cm->byte_alignment) != AOM_CODEC_OK) {
5098 aom_internal_error(
5099 &cm->error, AOM_CODEC_MEM_ERROR,
5100 "Failed to allocate copy buffer for saving coding context");
5101 }
5102 aom_yv12_copy_frame(ybf, &cc->copy_buffer, av1_num_planes(cm));
5103}
5104
5105// Coding context that only needs to be saved when recode loop includes
5106// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5107// restoraton).
5108static void save_extra_coding_context(AV1_COMP *cpi) {
5109 CODING_CONTEXT *const cc = &cpi->coding_context;
5110 AV1_COMMON *cm = &cpi->common;
5111
5112 cc->lf = cm->lf;
5113 cc->cdef_info = cm->cdef_info;
5114 cc->rc = cpi->rc;
5115}
5116
5117static void save_all_coding_context(AV1_COMP *cpi) {
5118 save_coding_context(cpi);
5119 save_cur_buf(cpi);
5120 save_extra_coding_context(cpi);
5121 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5122}
5123
5124static void restore_cur_buf(AV1_COMP *cpi) {
5125 CODING_CONTEXT *const cc = &cpi->coding_context;
5126 AV1_COMMON *cm = &cpi->common;
5127 aom_yv12_copy_frame(&cc->copy_buffer, &cm->cur_frame->buf,
5128 av1_num_planes(cm));
5129}
5130
5131// Coding context that only needs to be restored when recode loop includes
5132// filtering (deblocking, CDEF, superres post-encode upscale and/or loop
5133// restoraton).
5134static void restore_extra_coding_context(AV1_COMP *cpi) {
5135 CODING_CONTEXT *const cc = &cpi->coding_context;
5136 AV1_COMMON *cm = &cpi->common;
5137 cm->lf = cc->lf;
5138 cm->cdef_info = cc->cdef_info;
5139 cpi->rc = cc->rc;
5140}
5141
5142static void restore_all_coding_context(AV1_COMP *cpi) {
5143 restore_coding_context(cpi);
5144 restore_cur_buf(cpi);
5145 restore_extra_coding_context(cpi);
5146 if (!frame_is_intra_only(&cpi->common)) release_scaled_references(cpi);
5147}
5148
5149static int encode_with_and_without_superres(AV1_COMP *cpi, size_t *size,
5150 uint8_t *dest,
5151 int *largest_tile_id) {
5152 const AV1_COMMON *const cm = &cpi->common;
5153 AV1EncoderConfig *const oxcf = &cpi->oxcf;
5154 assert(cm->seq_params.enable_superres);
5155 assert(superres_in_recode_allowed(cpi));
5156 aom_codec_err_t err = AOM_CODEC_OK;
5157 save_all_coding_context(cpi);
5158
5159 // Encode with superres.
5160 int64_t sse1 = INT64_MAX;
5161 int64_t rate1 = INT64_MAX;
5162 int largest_tile_id1;
5163 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse1, &rate1,
5164 &largest_tile_id1);
5165 if (err != AOM_CODEC_OK) return err;
5166
5167 // Encode without superres.
5168 restore_all_coding_context(cpi);
5169 int64_t sse2 = INT64_MAX;
5170 int64_t rate2 = INT64_MAX;
5171 int largest_tile_id2;
5172 oxcf->superres_mode = SUPERRES_NONE; // To force full-res.
5173 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse2, &rate2,
5174 &largest_tile_id2);
5175 oxcf->superres_mode = SUPERRES_AUTO; // Reset.
5176 if (err != AOM_CODEC_OK) return err;
5177
5178 // Note: Both use common rdmult based on base qindex of fullres.
5179 const int64_t rdmult =
5180 av1_compute_rd_mult_based_on_qindex(cpi, cm->base_qindex);
5181 const double proj_rdcost1 = RDCOST_DBL(rdmult, rate1, sse1);
5182 const double proj_rdcost2 = RDCOST_DBL(rdmult, rate2, sse2);
5183
5184 // Re-encode with superres if it's better.
5185 if (proj_rdcost1 < proj_rdcost2) {
5186 restore_all_coding_context(cpi);
5187 // TODO(urvang): We should avoid rerunning the recode loop by saving
5188 // previous output+state, or running encode only for the selected 'q' in
5189 // previous step.
5190 int64_t sse3 = INT64_MAX;
5191 int64_t rate3 = INT64_MAX;
5192 err = encode_with_recode_loop_and_filter(cpi, size, dest, &sse3, &rate3,
5193 largest_tile_id);
5194 assert(sse1 == sse3);
5195 assert(rate1 == rate3);
5196 assert(largest_tile_id1 == *largest_tile_id);
5197 } else {
5198 *largest_tile_id = largest_tile_id2;
5199 }
5200
5201 return err;
5202}
5203#endif // CONFIG_SUPERRES_IN_RECODE
5204
Yaowu Xuc27fc142016-08-22 16:08:15 -07005205#define DUMP_RECON_FRAMES 0
5206
5207#if DUMP_RECON_FRAMES == 1
5208// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07005209static void dump_filtered_recon_frames(AV1_COMP *cpi) {
5210 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00005211 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00005212 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005213
Zoe Liub4f31032017-11-03 23:48:35 -07005214 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00005215 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07005216 return;
5217 }
5218
Zoe Liu27deb382018-03-27 15:13:56 -07005219 static const int flag_list[REF_FRAMES] = { 0,
5220 AOM_LAST_FLAG,
5221 AOM_LAST2_FLAG,
5222 AOM_LAST3_FLAG,
5223 AOM_GOLD_FLAG,
5224 AOM_BWD_FLAG,
5225 AOM_ALT2_FLAG,
5226 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07005227 printf(
5228 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
5229 "show_existing_frame=%d) "
5230 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00005231 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07005232 cm->show_existing_frame);
5233 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00005234 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
5235 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08005236 printf(" %d(%c)", ref_offset,
5237 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07005238 }
5239 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07005240
5241 if (!cm->show_frame) {
5242 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00005243 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005244 return;
5245 }
5246
Zoe Liub4f31032017-11-03 23:48:35 -07005247 int h;
5248 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
5249 FILE *f_recon = NULL;
5250
David Turnerd2a592e2018-11-16 14:59:31 +00005251 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005252 if ((f_recon = fopen(file_name, "wb")) == NULL) {
5253 printf("Unable to open file %s to write.\n", file_name);
5254 return;
5255 }
5256 } else {
5257 if ((f_recon = fopen(file_name, "ab")) == NULL) {
5258 printf("Unable to open file %s to append.\n", file_name);
5259 return;
5260 }
5261 }
5262 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07005263 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
5264 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07005265 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07005266 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07005267 current_frame->frame_number, cpi->gf_group.index,
5268 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
5269 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
5270 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
5271 cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07005272#if 0
5273 int ref_frame;
5274 printf("get_ref_frame_map_idx: [");
5275 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00005276 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07005277 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07005278#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07005279
5280 // --- Y ---
5281 for (h = 0; h < cm->height; ++h) {
5282 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
5283 f_recon);
5284 }
5285 // --- U ---
5286 for (h = 0; h < (cm->height >> 1); ++h) {
5287 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5288 f_recon);
5289 }
5290 // --- V ---
5291 for (h = 0; h < (cm->height >> 1); ++h) {
5292 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
5293 f_recon);
5294 }
5295
5296 fclose(f_recon);
5297}
5298#endif // DUMP_RECON_FRAMES
5299
David Turnerefed6372019-01-11 15:14:11 +00005300static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
5301 const YV12_BUFFER_CONFIG *last_picture,
5302 hash_table *last_hash_table) {
5303 aom_clear_system_state();
5304 // check use hash ME
5305 int k;
5306 uint32_t hash_value_1;
5307 uint32_t hash_value_2;
5308
5309 const int block_size = 8;
5310 const double threshold_current = 0.8;
5311 const double threshold_average = 0.95;
5312 const int max_history_size = 32;
5313 int T = 0; // total block
5314 int C = 0; // match with collocated block
5315 int S = 0; // smooth region but not match with collocated block
5316 int M = 0; // match with other block
5317
5318 const int pic_width = cur_picture->y_width;
5319 const int pic_height = cur_picture->y_height;
5320 for (int i = 0; i + block_size <= pic_height; i += block_size) {
5321 for (int j = 0; j + block_size <= pic_width; j += block_size) {
5322 const int x_pos = j;
5323 const int y_pos = i;
5324 int match = 1;
5325 T++;
5326
5327 // check whether collocated block match with current
5328 uint8_t *p_cur = cur_picture->y_buffer;
5329 uint8_t *p_ref = last_picture->y_buffer;
5330 int stride_cur = cur_picture->y_stride;
5331 int stride_ref = last_picture->y_stride;
5332 p_cur += (y_pos * stride_cur + x_pos);
5333 p_ref += (y_pos * stride_ref + x_pos);
5334
5335 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
5336 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
5337 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
5338 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5339 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5340 if (p16_cur[tmpX] != p16_ref[tmpX]) {
5341 match = 0;
5342 }
5343 }
5344 p16_cur += stride_cur;
5345 p16_ref += stride_ref;
5346 }
5347 } else {
5348 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
5349 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
5350 if (p_cur[tmpX] != p_ref[tmpX]) {
5351 match = 0;
5352 }
5353 }
5354 p_cur += stride_cur;
5355 p_ref += stride_ref;
5356 }
5357 }
5358
5359 if (match) {
5360 C++;
5361 continue;
5362 }
5363
5364 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
5365 y_pos) ||
5366 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
5367 S++;
5368 continue;
5369 }
5370
5371 av1_get_block_hash_value(
5372 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
5373 block_size, &hash_value_1, &hash_value_2,
5374 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
5375 // Hashing does not work for highbitdepth currently.
5376 // TODO(Roger): Make it work for highbitdepth.
5377 if (av1_use_hash_me(&cpi->common)) {
5378 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
5379 M++;
5380 }
5381 }
5382 }
5383 }
5384
5385 assert(T > 0);
5386 double csm_rate = ((double)(C + S + M)) / ((double)(T));
5387 double m_rate = ((double)(M)) / ((double)(T));
5388
5389 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
5390 cpi->m_rate_array[cpi->rate_index] = m_rate;
5391
5392 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
5393 cpi->rate_size++;
5394 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
5395
5396 if (csm_rate < threshold_current) {
5397 return 0;
5398 }
5399
5400 if (C == T) {
5401 return 1;
5402 }
5403
5404 double csm_average = 0.0;
5405 double m_average = 0.0;
5406
5407 for (k = 0; k < cpi->rate_size; k++) {
5408 csm_average += cpi->csm_rate_array[k];
5409 m_average += cpi->m_rate_array[k];
5410 }
5411 csm_average /= cpi->rate_size;
5412 m_average /= cpi->rate_size;
5413
5414 if (csm_average < threshold_average) {
5415 return 0;
5416 }
5417
5418 if (M > (T - C - S) / 3) {
5419 return 1;
5420 }
5421
5422 if (csm_rate > 0.99 && m_rate > 0.01) {
5423 return 1;
5424 }
5425
5426 if (csm_average + m_average > 1.01) {
5427 return 1;
5428 }
5429
5430 return 0;
5431}
5432
David Turner73245762019-02-11 16:42:34 +00005433// Refresh reference frame buffers according to refresh_frame_flags.
5434static void refresh_reference_frames(AV1_COMP *cpi) {
5435 AV1_COMMON *const cm = &cpi->common;
5436 // All buffers are refreshed for shown keyframes and S-frames.
5437
5438 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
5439 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
5440 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
5441 }
5442 }
5443}
5444
sdengc23c7f12019-06-11 16:56:50 -07005445// Implementation and modifications of C. Yeo, H. L. Tan, and Y. H. Tan, "On
5446// rate distortion optimization using SSIM," Circuits and Systems for Video
5447// Technology, IEEE Transactions on, vol. 23, no. 7, pp. 1170-1181, 2013.
sdeng32185d12019-06-19 14:47:09 -07005448// SSIM_VAR_SCALE defines the strength of the bias towards SSIM in RDO:
5449// Test data set: mid_res (33 frames)
5450// SSIM_VAR_SCALE avg_psnr ssim ms-ssim
5451// 8 8.2 -6.0 -6.4
5452// 16 4.0 -5.7 -5.9
5453// 32 1.6 -4.4 -4.5
sdengc23c7f12019-06-11 16:56:50 -07005454#define SSIM_VAR_SCALE 16.0
5455static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
5456 AV1_COMMON *cm = &cpi->common;
5457 ThreadData *td = &cpi->td;
5458 MACROBLOCK *x = &td->mb;
5459 MACROBLOCKD *xd = &x->e_mbd;
5460 uint8_t *y_buffer = cpi->source->y_buffer;
5461 const int y_stride = cpi->source->y_stride;
5462 const int block_size = BLOCK_16X16;
5463
5464 const int num_mi_w = mi_size_wide[block_size];
5465 const int num_mi_h = mi_size_high[block_size];
5466 const int num_cols = (cm->mi_cols + num_mi_w - 1) / num_mi_w;
5467 const int num_rows = (cm->mi_rows + num_mi_h - 1) / num_mi_h;
5468 double log_sum = 0.0;
5469 int row, col;
5470 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
5471
sdeng32185d12019-06-19 14:47:09 -07005472 // TODO(sdeng): tune this param for 12bit videos.
5473 double c2 = 58.5225; // (.03*255)^2
5474 c2 *= SSIM_VAR_SCALE;
sdengc23c7f12019-06-11 16:56:50 -07005475
5476 // Loop through each 16x16 block.
5477 for (row = 0; row < num_rows; ++row) {
5478 for (col = 0; col < num_cols; ++col) {
5479 int mi_row, mi_col;
5480 double var = 0.0, num_of_var = 0.0;
5481 const int index = row * num_cols + col;
5482
5483 // Loop through each 8x8 block.
5484 for (mi_row = row * num_mi_h;
5485 mi_row < cm->mi_rows && mi_row < (row + 1) * num_mi_h; mi_row += 2) {
5486 for (mi_col = col * num_mi_w;
5487 mi_col < cm->mi_cols && mi_col < (col + 1) * num_mi_w;
5488 mi_col += 2) {
5489 struct buf_2d buf;
5490 const int row_offset_y = mi_row << 2;
5491 const int col_offset_y = mi_col << 2;
5492
5493 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
5494 buf.stride = y_stride;
5495
5496 if (use_hbd) {
5497 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
5498 xd->bd);
5499 } else {
5500 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
5501 }
5502
5503 num_of_var += 1.0;
5504 }
5505 }
sdeng32185d12019-06-19 14:47:09 -07005506 var = var / num_of_var;
sdengc23c7f12019-06-11 16:56:50 -07005507 var = 2.0 * var + c2;
5508 cpi->ssim_rdmult_scaling_factors[index] = var;
5509 log_sum += log(var);
5510 }
5511 }
5512 log_sum = exp(log_sum / (double)(num_rows * num_cols));
5513
5514 for (row = 0; row < num_rows; ++row) {
5515 for (col = 0; col < num_cols; ++col) {
5516 const int index = row * num_cols + col;
5517 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
5518 }
5519 }
5520
5521 (void)xd;
5522}
5523
chiyotsaifc1404d2019-08-08 12:09:12 -07005524#if CONFIG_DEBUG
5525static int hash_me_has_at_most_two_refs(RefCntBuffer *frame_bufs) {
5526 int total_count = 0;
5527 for (int frame_idx = 0; frame_idx < FRAME_BUFFERS; ++frame_idx) {
5528 if (frame_bufs[frame_idx].hash_table.has_content > 1) {
5529 return 0;
5530 }
5531 total_count += frame_bufs[frame_idx].hash_table.has_content;
5532 }
5533
5534 return total_count <= 2;
5535}
5536#endif
5537
David Turner73245762019-02-11 16:42:34 +00005538static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
5539 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005540 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005541 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00005542 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07005543 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005544 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005545
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005546#if CONFIG_COLLECT_COMPONENT_TIMING
5547 start_timing(cpi, encode_frame_to_data_rate_time);
5548#endif
5549
Fangwen Fu8d164de2016-12-14 13:40:54 -08005550 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00005551 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07005552
Yunqing Wang9612d552018-05-15 14:58:30 -07005553 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5554 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07005555
sarahparker21dbca42018-03-30 17:43:44 -07005556 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07005557 // cm->allow_ref_frame_mvs needs to be written into the frame header while
5558 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
5559 // separated from frame_might_allow_ref_frame_mvs().
5560 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
5561
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07005562 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07005563 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005564
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305565 cm->last_frame_type = current_frame->frame_type;
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305566
Sarah Parker33005522018-07-27 14:46:25 -07005567 if (encode_show_existing_frame(cm)) {
Jingning Han8f661602017-08-19 08:16:50 -07005568 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07005569
David Turner996b2c12018-12-07 15:52:30 +00005570 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005571 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005572 int largest_tile_id = 0; // Output from bitstream: unused here
5573 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005574 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005575
David Turner90311862018-11-29 13:34:36 +00005576 if (seq_params->frame_id_numbers_present_flag &&
5577 current_frame->frame_type == KEY_FRAME) {
5578 // Displaying a forward key-frame, so reset the ref buffer IDs
5579 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
5580 for (int i = 0; i < REF_FRAMES; i++)
5581 cm->ref_frame_id[i] = display_frame_id;
5582 }
5583
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005584 cpi->seq_params_locked = 1;
5585
Yaowu Xuc27fc142016-08-22 16:08:15 -07005586#if DUMP_RECON_FRAMES == 1
5587 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5588 dump_filtered_recon_frames(cpi);
5589#endif // DUMP_RECON_FRAMES
5590
David Turner73245762019-02-11 16:42:34 +00005591 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5592 // for the purpose to verify no mismatch between encoder and decoder.
5593 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005594
David Turner73245762019-02-11 16:42:34 +00005595 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005596
Yaowu Xuc27fc142016-08-22 16:08:15 -07005597 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5598 // to do post-encoding update accordingly.
5599 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005600 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005601 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005602 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005603
David Turnerd2a592e2018-11-16 14:59:31 +00005604 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005605
Tom Finegane4099e32018-01-23 12:01:51 -08005606 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005607 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005608
David Turnerefed6372019-01-11 15:14:11 +00005609 // Work out whether to force_integer_mv this frame
5610 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
5611 !frame_is_intra_only(cm)) {
5612 if (cpi->common.seq_params.force_integer_mv == 2) {
5613 // Adaptive mode: see what previous frame encoded did
5614 if (cpi->unscaled_last_source != NULL) {
5615 cm->cur_frame_force_integer_mv =
5616 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
5617 cpi->previous_hash_table);
5618 } else {
5619 cpi->common.cur_frame_force_integer_mv = 0;
5620 }
5621 } else {
5622 cpi->common.cur_frame_force_integer_mv =
5623 cpi->common.seq_params.force_integer_mv;
5624 }
5625 } else {
5626 cpi->common.cur_frame_force_integer_mv = 0;
5627 }
5628
chiyotsaifc1404d2019-08-08 12:09:12 -07005629#if CONFIG_DEBUG
5630 assert(hash_me_has_at_most_two_refs(cm->buffer_pool->frame_bufs) &&
5631 "Hash-me is leaking memory!");
5632#endif
5633
5634 if (cpi->oxcf.pass != 1 && cpi->need_to_clear_prev_hash_table) {
5635 av1_hash_table_clear_all(cpi->previous_hash_table);
5636 cpi->need_to_clear_prev_hash_table = 0;
5637 }
5638
Yaowu Xuc27fc142016-08-22 16:08:15 -07005639 // Set default state for segment based loop filter update flags.
5640 cm->lf.mode_ref_delta_update = 0;
5641
Yaowu Xuc27fc142016-08-22 16:08:15 -07005642 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05005643 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005644 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005645 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005646
5647 // If segmentation is enabled force a map update for key frames.
5648 if (seg->enabled) {
5649 seg->update_map = 1;
5650 seg->update_data = 1;
5651 }
5652
5653 // The alternate reference frame cannot be active for a key frame.
5654 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005655 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005656 if (cpi->oxcf.mtu == 0) {
5657 cm->num_tg = cpi->oxcf.num_tile_groups;
5658 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005659 // Use a default value for the purposes of weighting costs in probability
5660 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005661 cm->num_tg = DEFAULT_MAX_NUM_TG;
5662 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005663
5664 // For 1 pass CBR, check if we are dropping this frame.
5665 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005666 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00005667 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005668 if (av1_rc_drop_frame(cpi)) {
5669 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00005670 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005671 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005672 }
5673 }
5674
sdengc23c7f12019-06-11 16:56:50 -07005675 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
5676
Yaowu Xuf883b422016-08-30 14:01:10 -07005677 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005678
5679#if CONFIG_INTERNAL_STATS
5680 memset(cpi->mode_chosen_counts, 0,
5681 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5682#endif
5683
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005684 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005685 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08005686 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005687 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005688 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08005689 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07005690 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5691 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5692 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005693 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005694 lsb = cpi->source->y_buffer[0] & 0xff;
5695 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005696 }
David Turner760a2f42018-12-07 15:25:36 +00005697 cm->current_frame_id =
5698 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005699
5700 // S_frame is meant for stitching different streams of different
5701 // resolutions together, so current_frame_id must be the
5702 // same across different streams of the same content current_frame_id
5703 // should be the same and not random. 0x37 is a chosen number as start
5704 // point
5705 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005706 } else {
5707 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00005708 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
5709 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005710 }
5711 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005712
Hui Su483a8452018-02-26 12:28:48 -08005713 switch (cpi->oxcf.cdf_update_mode) {
5714 case 0: // No CDF update for any frames(4~6% compression loss).
5715 cm->disable_cdf_update = 1;
5716 break;
5717 case 1: // Enable CDF update for all frames.
5718 cm->disable_cdf_update = 0;
5719 break;
5720 case 2:
5721 // Strategically determine at which frames to do CDF update.
5722 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5723 // compression loss).
5724 // TODO(huisu@google.com): design schemes for various trade-offs between
5725 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005726 cm->disable_cdf_update =
5727 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005728 break;
5729 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005730 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005731
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005732 if (cpi->oxcf.pass == 2 && cpi->oxcf.enable_tpl_model == 2 &&
5733 current_frame->frame_type == INTER_FRAME) {
5734 if (!cm->show_frame) {
5735 assert(cpi->tpl_model_pass == 0);
5736 cpi->tpl_model_pass = 1;
5737 }
5738 }
5739
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005740 int largest_tile_id = 0;
5741#if CONFIG_SUPERRES_IN_RECODE
5742 if (superres_in_recode_allowed(cpi)) {
5743 if (encode_with_and_without_superres(cpi, size, dest, &largest_tile_id) !=
5744 AOM_CODEC_OK) {
5745 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005746 }
Hui Su06463e42018-02-23 22:17:36 -08005747 } else {
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005748#endif // CONFIG_SUPERRES_IN_RECODE
5749 if (encode_with_recode_loop_and_filter(cpi, size, dest, NULL, NULL,
5750 &largest_tile_id) != AOM_CODEC_OK) {
5751 return AOM_CODEC_ERROR;
5752 }
5753#if CONFIG_SUPERRES_IN_RECODE
Hui Su06463e42018-02-23 22:17:36 -08005754 }
Urvang Joshi0e8b6ed2019-06-20 15:36:21 -07005755#endif // CONFIG_SUPERRES_IN_RECODE
Yaowu Xuc27fc142016-08-22 16:08:15 -07005756
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005757 cpi->seq_params_locked = 1;
5758
David Turner996b2c12018-12-07 15:52:30 +00005759 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005760 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00005761 for (int i = 0; i < REF_FRAMES; i++) {
5762 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005763 cm->ref_frame_id[i] = cm->current_frame_id;
5764 }
5765 }
5766 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005767
Yaowu Xuc27fc142016-08-22 16:08:15 -07005768#if DUMP_RECON_FRAMES == 1
5769 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005770 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005771#endif // DUMP_RECON_FRAMES
5772
Soo-Chul Han934af352017-10-15 15:21:51 -04005773 if (cm->seg.enabled) {
5774 if (cm->seg.update_map) {
5775 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005776 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00005777 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04005778 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5779 }
5780 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005781
5782 if (frame_is_intra_only(cm) == 0) {
5783 release_scaled_references(cpi);
5784 }
5785
David Turner73245762019-02-11 16:42:34 +00005786 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5787 // for the purpose to verify no mismatch between encoder and decoder.
5788 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
5789
5790 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005791
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005792#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005793 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005794#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005795
Hui Sudc54be62018-03-14 19:14:28 -07005796 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00005797 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07005798 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005799 }
David Turnera4c96252019-01-11 16:36:39 +00005800 if (!cm->large_scale_tile) {
5801 cm->cur_frame->frame_context = *cm->fc;
5802 }
5803#define EXT_TILE_DEBUG 0
5804#if EXT_TILE_DEBUG
5805 if (cm->large_scale_tile && oxcf->pass == 2) {
5806 char fn[20] = "./fc";
5807 fn[4] = current_frame->frame_number / 100 + '0';
5808 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
5809 fn[6] = (current_frame->frame_number % 10) + '0';
5810 fn[7] = '\0';
5811 av1_print_frame_contexts(cm->fc, fn);
5812 }
5813#endif // EXT_TILE_DEBUG
5814#undef EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07005815
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005816#if CONFIG_COLLECT_COMPONENT_TIMING
5817 end_timing(cpi, encode_frame_to_data_rate_time);
5818
5819 // Print out timing information.
5820 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08005821 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005822 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08005823 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005824 for (i = 0; i < kTimingComponents; i++) {
5825 cpi->component_time[i] += cpi->frame_component_time[i];
5826 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
5827 get_component_name(i), cpi->frame_component_time[i],
5828 cpi->component_time[i]);
5829 cpi->frame_component_time[i] = 0;
5830 }
5831#endif
5832
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305833 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005834
Yaowu Xuf883b422016-08-30 14:01:10 -07005835 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005836
chiyotsaifc1404d2019-08-08 12:09:12 -07005837 // Store encoded frame's hash table for in_integer_mv() next time.
5838 // Beware! If we don't update previous_hash_table here we will leak the
5839 // items stored in cur_frame's hash_table!
5840 if (oxcf->pass != 1 && av1_use_hash_me(cm)) {
David Turnerefed6372019-01-11 15:14:11 +00005841 cpi->previous_hash_table = &cm->cur_frame->hash_table;
chiyotsaifc1404d2019-08-08 12:09:12 -07005842 cpi->need_to_clear_prev_hash_table = 1;
David Turnerefed6372019-01-11 15:14:11 +00005843 }
5844
Yaowu Xuc27fc142016-08-22 16:08:15 -07005845 // Clear the one shot update flags for segmentation map and mode/ref loop
5846 // filter deltas.
5847 cm->seg.update_map = 0;
5848 cm->seg.update_data = 0;
5849 cm->lf.mode_ref_delta_update = 0;
5850
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005851 // A droppable frame might not be shown but it always
5852 // takes a space in the gf group. Therefore, even when
5853 // it is not shown, we still need update the count down.
5854
Yaowu Xuc27fc142016-08-22 16:08:15 -07005855 if (cm->show_frame) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005856 // Don't increment frame counters if this was an altref buffer
5857 // update not a real frame
David Turnerd2a592e2018-11-16 14:59:31 +00005858 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005859 }
5860
Tom Finegane4099e32018-01-23 12:01:51 -08005861 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005862}
5863
David Turner056f7cd2019-01-07 17:48:13 +00005864int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00005865 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00005866 const EncodeFrameParams *const frame_params,
5867 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00005868 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00005869 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005870
David Turnercb5e36f2019-01-17 17:15:25 +00005871 cpi->unscaled_source = frame_input->source;
5872 cpi->source = frame_input->source;
5873 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00005874
David Turner6e8b4d92019-02-18 15:01:33 +00005875 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00005876 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00005877 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00005878 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00005879 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005880 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00005881 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00005882 cm->show_existing_frame = frame_params->show_existing_frame;
5883 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00005884
David Turner73245762019-02-11 16:42:34 +00005885 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
5886 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
5887
David Turnerfe3aecb2019-02-06 14:42:42 +00005888 cpi->refresh_last_frame = frame_params->refresh_last_frame;
5889 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
5890 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
David Turnerfe3aecb2019-02-06 14:42:42 +00005891 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
5892
David Turnera7f133c2019-01-22 14:47:16 +00005893 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
5894 current_frame->frame_number = 0;
5895
5896 if (cm->show_existing_frame) {
5897 current_frame->order_hint = cm->cur_frame->order_hint;
Ravi Chaudhary9701cd62019-07-18 17:32:26 +05305898 current_frame->display_order_hint = cm->cur_frame->display_order_hint;
David Turnera7f133c2019-01-22 14:47:16 +00005899 } else {
5900 current_frame->order_hint =
5901 current_frame->frame_number + frame_params->order_offset;
Ravi Chaudhary9701cd62019-07-18 17:32:26 +05305902 current_frame->display_order_hint = current_frame->order_hint;
David Turnera7f133c2019-01-22 14:47:16 +00005903 current_frame->order_hint %=
5904 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
5905 }
5906
David Turnercb5e36f2019-01-17 17:15:25 +00005907 if (cpi->oxcf.pass == 1) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005908#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00005909 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005910#endif
David Turnercb5e36f2019-01-17 17:15:25 +00005911 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00005912 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
5913 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00005914 return AOM_CODEC_ERROR;
5915 }
5916 } else {
David Turner056f7cd2019-01-07 17:48:13 +00005917 return AOM_CODEC_ERROR;
5918 }
5919
5920 return AOM_CODEC_OK;
5921}
5922
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005923#if CONFIG_DENOISE
5924static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5925 int block_size, float noise_level,
5926 int64_t time_stamp, int64_t end_time) {
5927 AV1_COMMON *const cm = &cpi->common;
5928 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005929 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5930 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005931 if (!cpi->denoise_and_model) {
5932 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5933 "Error allocating denoise and model");
5934 return -1;
5935 }
5936 }
5937 if (!cpi->film_grain_table) {
5938 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5939 if (!cpi->film_grain_table) {
5940 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5941 "Error allocating grain table");
5942 return -1;
5943 }
5944 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5945 }
5946 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5947 &cm->film_grain_params)) {
5948 if (cm->film_grain_params.apply_grain) {
5949 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5950 &cm->film_grain_params);
5951 }
5952 }
5953 return 0;
5954}
5955#endif
5956
James Zern3e2613b2017-03-30 23:14:40 -07005957int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005958 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5959 int64_t end_time) {
5960 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005961 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005962 int res = 0;
5963 const int subsampling_x = sd->subsampling_x;
5964 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005965 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005966
Yaowu Xuc27fc142016-08-22 16:08:15 -07005967 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005968
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005969#if CONFIG_INTERNAL_STATS
5970 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005971 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005972#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005973#if CONFIG_DENOISE
5974 if (cpi->oxcf.noise_level > 0)
5975 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5976 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5977 res = -1;
5978#endif // CONFIG_DENOISE
5979
Yaowu Xuf883b422016-08-30 14:01:10 -07005980 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005981 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005982 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005983#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005984 aom_usec_timer_mark(&timer);
5985 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005986#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005987 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005988 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005989 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005990 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005991 res = -1;
5992 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005993 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005994 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005995 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005996 "Profile 1 requires 4:4:4 color format");
5997 res = -1;
5998 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005999 if ((seq_params->profile == PROFILE_2) &&
6000 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08006001 !(subsampling_x == 1 && subsampling_y == 0)) {
6002 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
6003 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07006004 res = -1;
6005 }
6006
6007 return res;
6008}
6009
Yaowu Xuc27fc142016-08-22 16:08:15 -07006010#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006011extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
6012 const unsigned char *img2, int img2_pitch,
6013 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006014
6015static void adjust_image_stat(double y, double u, double v, double all,
6016 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07006017 s->stat[STAT_Y] += y;
6018 s->stat[STAT_U] += u;
6019 s->stat[STAT_V] += v;
6020 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07006021 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006022}
6023
Angie Chiang08a22a62017-07-17 17:29:17 -07006024static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006025 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006026 double samples = 0.0;
6027 uint32_t in_bit_depth = 8;
6028 uint32_t bit_depth = 8;
6029
Angie Chiang08a22a62017-07-17 17:29:17 -07006030#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00006031 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07006032#endif
6033 cpi->bytes += frame_bytes;
6034
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006035 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006036 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006037 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006038 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006039 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07006040 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00006041 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006042 double y, u, v, frame_all;
6043
6044 cpi->count++;
6045 if (cpi->b_calculate_psnr) {
6046 PSNR_STATS psnr;
6047 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07006048 aom_clear_system_state();
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006049#if CONFIG_AV1_HIGHBITDEPTH
Yaowu Xud3e7c682017-12-21 14:08:25 -08006050 // TODO(yaowu): unify these two versions into one.
Jerome Jiangfa1d1732019-08-06 10:31:20 -07006051 if (cm->seq_params.use_highbitdepth)
6052 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
6053 else
6054 aom_calc_psnr(orig, recon, &psnr);
6055#else
6056 aom_calc_psnr(orig, recon, &psnr);
6057#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07006058 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
6059 &cpi->psnr);
6060 cpi->total_sq_error += psnr.sse[0];
6061 cpi->total_samples += psnr.samples[0];
6062 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08006063 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006064 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07006065 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07006066 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006067 else
Yaowu Xuf883b422016-08-30 14:01:10 -07006068 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006069
Yaowu Xuf883b422016-08-30 14:01:10 -07006070 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006071 cpi->summed_quality += frame_ssim2 * weight;
6072 cpi->summed_weights += weight;
6073
6074#if 0
6075 {
6076 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08006077 double y2 = psnr.psnr[1];
6078 double u2 = psnr.psnr[2];
6079 double v2 = psnr.psnr[3];
6080 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07006081 fprintf(f, "%5d : Y%f7.3:U%f7.3:V%f7.3:F%f7.3:S%7.3f\n",
David Turnerd2a592e2018-11-16 14:59:31 +00006082 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07006083 frame_psnr2, frame_ssim2);
6084 fclose(f);
6085 }
6086#endif
6087 }
6088 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006089 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006090 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07006091 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
6092 recon->y_stride, orig->y_width, orig->y_height);
6093 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006094 cpi->total_blockiness += frame_blockiness;
6095 }
6096
6097 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006098 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006099 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07006100 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
6101 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
6102
6103 const double peak = (double)((1 << in_bit_depth) - 1);
6104 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006105 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006106 if (consistency > 0.0)
6107 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07006108 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006109 cpi->total_inconsistency += this_inconsistency;
6110 }
6111 }
6112 }
6113
6114 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07006115 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006116 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07006117 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006118 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
6119 }
6120}
6121#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08006122int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
6123 size_t *size, uint8_t *dest, int64_t *time_stamp,
6124 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07006125 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006126 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
6127 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006128
6129#if CONFIG_BITSTREAM_DEBUG
6130 assert(cpi->oxcf.max_threads == 0 &&
6131 "bitstream debug tool does not support multithreading");
6132 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07006133 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
6134 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006135#endif
6136
Sarah Parker740e8392019-01-23 15:47:53 -08006137 // Indicates whether or not to use an adaptive quantize b rather than
6138 // the traditional version
6139 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
6140
Dominic Symesd4929012018-01-31 17:32:01 +01006141 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00006142 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006143#if CONFIG_INTERNAL_STATS
6144 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07006145 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006146#endif
RogerZhou3b635242017-09-19 10:06:46 -07006147 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006148
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07006149 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07006150 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
6151 ? REFRESH_FRAME_CONTEXT_DISABLED
6152 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01006153 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08006154 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006155
Sarah Parkerb9041612018-05-22 19:06:47 -07006156 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07006157 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08006158
David Turnerdedd8ff2019-01-23 13:59:46 +00006159 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006160
Yue Chen1bc5be62018-08-24 13:57:32 -07006161 const int result =
6162 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
6163 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00006164 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00006165 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00006166 } else if (result == -1) {
6167 // Returning -1 indicates no frame encoded; more input is required
6168 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006169 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08006170#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07006171 aom_usec_timer_mark(&cmptimer);
6172 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07006173#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006174 if (cpi->b_calculate_psnr) {
6175 if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
6176 generate_psnr_packet(cpi);
6177 }
6178 }
Hui Su8ea87322019-03-29 14:44:32 -07006179
6180 if (cpi->keep_level_stats && oxcf->pass != 1) {
6181 // Initialize level info. at the beginning of each sequence.
6182 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07006183 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07006184 }
kyslovabeeb7c2019-03-06 18:35:04 -08006185 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07006186 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006187
6188#if CONFIG_INTERNAL_STATS
6189 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07006190 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07006191 }
6192#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006193#if CONFIG_SPEED_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00006194 if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08006195 cpi->tx_search_count += cpi->td.mb.tx_search_count;
6196 cpi->td.mb.tx_search_count = 0;
6197 }
6198#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07006199
Yaowu Xuf883b422016-08-30 14:01:10 -07006200 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006201
Hui Su0d0ee662019-07-29 14:38:36 -07006202 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006203}
6204
Yaowu Xuf883b422016-08-30 14:01:10 -07006205int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
6206 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006207 if (!cm->show_frame) {
6208 return -1;
6209 } else {
6210 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00006211 if (cm->cur_frame != NULL) {
6212 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006213 dest->y_width = cm->width;
6214 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07006215 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
6216 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006217 ret = 0;
6218 } else {
6219 ret = -1;
6220 }
Yaowu Xuf883b422016-08-30 14:01:10 -07006221 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07006222 return ret;
6223 }
6224}
6225
Yaowu Xuf883b422016-08-30 14:01:10 -07006226int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00006227 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006228
David Turnere7ebf902018-12-04 14:04:55 +00006229 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006230 return 0;
6231}
6232
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006233static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
6234 const YV12_BUFFER_CONFIG *b) {
6235 return a->y_height == b->y_height && a->y_width == b->y_width &&
6236 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
6237 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
6238 a->border == b->border &&
6239 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
6240 (b->flags & YV12_FLAG_HIGHBITDEPTH);
6241}
6242
Yunqing Wang93b18f32018-06-08 21:08:29 -07006243aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
6244 YV12_BUFFER_CONFIG *new_frame,
6245 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07006246 const int num_planes = av1_num_planes(cm);
6247 if (!equal_dimensions_and_border(new_frame, sd))
6248 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
6249 "Incorrect buffer dimensions");
6250 else
6251 aom_yv12_copy_frame(new_frame, sd, num_planes);
6252
6253 return cm->error.error_code;
6254}
6255
Yaowu Xuf883b422016-08-30 14:01:10 -07006256int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
6257 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07006258 int hr = 0, hs = 0, vr = 0, vs = 0;
6259
6260 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
6261
6262 Scale2Ratio(horiz_mode, &hr, &hs);
6263 Scale2Ratio(vert_mode, &vr, &vs);
6264
6265 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07006266 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
6267 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006268
6269 return 0;
6270}
6271
Yaowu Xuf883b422016-08-30 14:01:10 -07006272int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006273
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006274int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
6275 size_t output_size = 0;
6276 size_t total_bytes_read = 0;
6277 size_t remaining_size = *frame_size;
6278 uint8_t *buff_ptr = buffer;
6279
6280 // go through each OBUs
6281 while (total_bytes_read < *frame_size) {
6282 uint8_t saved_obu_header[2];
6283 uint64_t obu_payload_size;
6284 size_t length_of_payload_size;
6285 size_t length_of_obu_size;
6286 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
6287 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
6288
6289 // save the obu header (1 or 2 bytes)
6290 memmove(saved_obu_header, buff_ptr, obu_header_size);
6291 // clear the obu_has_size_field
6292 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
6293
6294 // get the payload_size and length of payload_size
6295 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
6296 &obu_payload_size, &length_of_payload_size) != 0) {
6297 return AOM_CODEC_ERROR;
6298 }
6299 obu_bytes_read += length_of_payload_size;
6300
6301 // calculate the length of size of the obu header plus payload
6302 length_of_obu_size =
6303 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
6304
6305 // move the rest of data to new location
6306 memmove(buff_ptr + length_of_obu_size + obu_header_size,
6307 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07006308 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006309
6310 // write the new obu size
6311 const uint64_t obu_size = obu_header_size + obu_payload_size;
6312 size_t coded_obu_size;
6313 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
6314 &coded_obu_size) != 0) {
6315 return AOM_CODEC_ERROR;
6316 }
6317
6318 // write the saved (modified) obu_header following obu size
6319 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
6320
6321 total_bytes_read += obu_bytes_read;
6322 remaining_size -= obu_bytes_read;
6323 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07006324 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04006325 }
6326
6327 *frame_size = output_size;
6328 return AOM_CODEC_OK;
6329}
6330
Marco Paniconid8574e32019-08-04 21:30:12 -07006331static void svc_set_updates_external_ref_frame_config(AV1_COMP *cpi) {
6332 cpi->ext_refresh_frame_flags_pending = 1;
6333 cpi->ext_refresh_last_frame = cpi->svc.refresh[cpi->svc.ref_idx[0]];
6334 cpi->ext_refresh_golden_frame = cpi->svc.refresh[cpi->svc.ref_idx[3]];
6335 cpi->ext_refresh_bwd_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[4]];
6336 cpi->ext_refresh_alt2_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[5]];
6337 cpi->ext_refresh_alt_ref_frame = cpi->svc.refresh[cpi->svc.ref_idx[6]];
6338 cpi->svc.non_reference_frame = 1;
6339 for (int i = 0; i < REF_FRAMES; i++) {
6340 if (cpi->svc.refresh[i] == 1) {
6341 cpi->svc.non_reference_frame = 0;
6342 break;
6343 }
6344 }
6345}
6346
Yaowu Xuf883b422016-08-30 14:01:10 -07006347void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006348 // TODO(yunqingwang): For what references to use, external encoding flags
6349 // should be consistent with internal reference frame selection. Need to
6350 // ensure that there is not conflict between the two. In AV1 encoder, the
6351 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006352 // GOLDEN, BWDREF, ALTREF2.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08006353 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006354 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006355 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
6356 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
6357 AOM_EFLAG_NO_REF_ARF2)) {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006358 int ref = AOM_REFFRAME_ALL;
6359
6360 if (flags & AOM_EFLAG_NO_REF_LAST) ref ^= AOM_LAST_FLAG;
6361 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
6362 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
6363
6364 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
6365
6366 if (flags & AOM_EFLAG_NO_REF_ARF) {
6367 ref ^= AOM_ALT_FLAG;
6368 ref ^= AOM_BWD_FLAG;
6369 ref ^= AOM_ALT2_FLAG;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006370 } else {
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006371 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
6372 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006373 }
Marco Paniconi60a4d7f2019-08-02 14:43:27 -07006374
6375 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006376 }
6377
6378 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006379 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006380 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006381
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006382 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6383 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006384
Yaowu Xuf883b422016-08-30 14:01:10 -07006385 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006386
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006387 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6388 upd ^= AOM_ALT_FLAG;
6389 upd ^= AOM_BWD_FLAG;
6390 upd ^= AOM_ALT2_FLAG;
6391 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006392
David Turnerce9b5902019-01-23 17:25:47 +00006393 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
6394 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
6395 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
6396 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
6397 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
6398 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00006399 } else {
Marco Paniconid8574e32019-08-04 21:30:12 -07006400 if (cpi->svc.external_ref_frame_config)
6401 svc_set_updates_external_ref_frame_config(cpi);
6402 else
6403 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006404 }
6405
sarahparker21dbca42018-03-30 17:43:44 -07006406 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
6407 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07006408 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
6409 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07006410 cpi->ext_use_s_frame =
6411 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07006412 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07006413
Yaowu Xuf883b422016-08-30 14:01:10 -07006414 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6415 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006416 }
6417}
Andrey Norkin795ba872018-03-06 13:24:14 -08006418
Tom Fineganf8d6a162018-08-21 10:47:55 -07006419aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
6420 if (!cpi) return NULL;
6421
6422 uint8_t header_buf[512] = { 0 };
6423 const uint32_t sequence_header_size =
Yaowu Xu797674b2019-05-01 17:38:11 -07006424 av1_write_sequence_header_obu(cpi, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07006425 assert(sequence_header_size <= sizeof(header_buf));
6426 if (sequence_header_size == 0) return NULL;
6427
6428 const size_t obu_header_size = 1;
6429 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
6430 const size_t payload_offset = obu_header_size + size_field_size;
6431
6432 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
6433 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
6434
Hui Su4fd11762019-03-26 16:05:07 -07006435 if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
Tom Fineganf8d6a162018-08-21 10:47:55 -07006436 obu_header_size) {
6437 return NULL;
6438 }
6439
6440 size_t coded_size_field_size = 0;
6441 if (aom_uleb_encode(sequence_header_size, size_field_size,
6442 &header_buf[obu_header_size],
6443 &coded_size_field_size) != 0) {
6444 return NULL;
6445 }
6446 assert(coded_size_field_size == size_field_size);
6447
6448 aom_fixed_buf_t *global_headers =
6449 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
6450 if (!global_headers) return NULL;
6451
6452 const size_t global_header_buf_size =
6453 obu_header_size + size_field_size + sequence_header_size;
6454
6455 global_headers->buf = malloc(global_header_buf_size);
6456 if (!global_headers->buf) {
6457 free(global_headers);
6458 return NULL;
6459 }
6460
6461 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
6462 global_headers->sz = global_header_buf_size;
6463 return global_headers;
6464}