blob: 035bc4f067a5c9063f190190d44ec32d90d67a78 [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
Cheng Chen46f30c72017-09-07 11:13:33 -0700380static void enc_setup_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700381 int i;
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530382 int mi_rows_sb_aligned = calc_mi_size(cm->mi_rows);
chiyotsai04ca87d2019-05-24 15:06:19 -0700383 memset(cm->mi, 0, cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700384 // Clear top border row
chiyotsai04ca87d2019-05-24 15:06:19 -0700385 memset(cm->prev_mi, 0, sizeof(*cm->prev_mi) * cm->mi_stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700386 // Clear left border column
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530387 for (i = 0; i < mi_rows_sb_aligned; ++i)
chiyotsai04ca87d2019-05-24 15:06:19 -0700388 memset(&cm->prev_mi[i * cm->mi_stride], 0, sizeof(*cm->prev_mi));
Yunqing Wang19b9f722018-02-20 16:22:01 -0800389 cm->mi_grid_visible = cm->mi_grid_base;
390 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700391
392 memset(cm->mi_grid_base, 0,
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530393 cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi_grid_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700394}
395
Cheng Chen46f30c72017-09-07 11:13:33 -0700396static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700397 cm->mi = aom_calloc(mi_size, sizeof(*cm->mi));
398 if (!cm->mi) return 1;
399 cm->prev_mi = aom_calloc(mi_size, sizeof(*cm->prev_mi));
400 if (!cm->prev_mi) return 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700401 cm->mi_alloc_size = mi_size;
402
Yue Chen53b53f02018-03-29 14:31:23 -0700403 cm->mi_grid_base =
404 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700405 if (!cm->mi_grid_base) return 1;
406 cm->prev_mi_grid_base =
Yue Chen53b53f02018-03-29 14:31:23 -0700407 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700408 if (!cm->prev_mi_grid_base) return 1;
409
410 return 0;
411}
412
Cheng Chen46f30c72017-09-07 11:13:33 -0700413static void enc_free_mi(AV1_COMMON *cm) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700414 aom_free(cm->mi);
415 cm->mi = NULL;
416 aom_free(cm->prev_mi);
417 cm->prev_mi = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700418 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700419 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700420 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700422 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700423}
424
Cheng Chen46f30c72017-09-07 11:13:33 -0700425static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700426 // Current mi will be the prev_mi for the next frame.
Yue Chen53b53f02018-03-29 14:31:23 -0700427 MB_MODE_INFO **temp_base = cm->prev_mi_grid_base;
chiyotsai04ca87d2019-05-24 15:06:19 -0700428 MB_MODE_INFO *temp = cm->prev_mi;
429 cm->prev_mi = cm->mi;
430 cm->mi = temp;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700431
432 // Update the upper left visible macroblock ptrs.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700433 cm->prev_mi_grid_base = cm->mi_grid_base;
434 cm->mi_grid_base = temp_base;
Yunqing Wang19b9f722018-02-20 16:22:01 -0800435 cm->mi_grid_visible = cm->mi_grid_base;
436 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437}
438
Yaowu Xuf883b422016-08-30 14:01:10 -0700439void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700440 av1_rtcd();
441 aom_dsp_rtcd();
442 aom_scale_rtcd();
443 av1_init_intra_predictors();
444 av1_init_me_luts();
445 av1_rc_init_minq_luts();
446 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700447}
448
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700449static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
450 if (cpi->mbmi_ext_base) {
451 aom_free(cpi->mbmi_ext_base);
452 cpi->mbmi_ext_base = NULL;
453 }
454}
455
456static void alloc_context_buffers_ext(AV1_COMP *cpi) {
457 AV1_COMMON *cm = &cpi->common;
458 int mi_size = cm->mi_cols * cm->mi_rows;
459
460 dealloc_context_buffers_ext(cpi);
461 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
462 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
463}
464
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800465static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
466 pars->num_cr_points = 0;
467 pars->cr_mult = 0;
468 pars->cr_luma_mult = 0;
469 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
470 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
471 pars->num_cb_points = 0;
472 pars->cb_mult = 0;
473 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800474 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800475 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
476 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
477}
478
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800479static void update_film_grain_parameters(struct AV1_COMP *cpi,
480 const AV1EncoderConfig *oxcf) {
481 AV1_COMMON *const cm = &cpi->common;
482 cpi->oxcf = *oxcf;
483
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700484 if (cpi->film_grain_table) {
485 aom_film_grain_table_free(cpi->film_grain_table);
486 aom_free(cpi->film_grain_table);
487 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700488 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700489
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800490 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700491 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000492 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800493 memcpy(&cm->film_grain_params,
494 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
495 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800496 if (oxcf->monochrome)
497 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700498 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
499 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800500 cm->film_grain_params.clip_to_restricted_range = 0;
501 }
502 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700503 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800504 cm->seq_params.film_grain_params_present = 1;
505
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700506 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
507 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700508
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700509 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700510 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800511 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800512#if CONFIG_DENOISE
513 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
514#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700515 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800516#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800517 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
518 }
519}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800520
Yaowu Xuf883b422016-08-30 14:01:10 -0700521static void dealloc_compressor_data(AV1_COMP *cpi) {
522 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000523 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700524
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700525 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700526
Yaowu Xuf883b422016-08-30 14:01:10 -0700527 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700528 cpi->tile_data = NULL;
529
530 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700531 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700532 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533
Yaowu Xuf883b422016-08-30 14:01:10 -0700534 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700535 cpi->cyclic_refresh = NULL;
536
Yaowu Xuf883b422016-08-30 14:01:10 -0700537 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538 cpi->active_map.map = NULL;
539
sdengc23c7f12019-06-11 16:56:50 -0700540 aom_free(cpi->ssim_rdmult_scaling_factors);
541 cpi->ssim_rdmult_scaling_factors = NULL;
542
Jingning Hand064cf02017-06-01 10:00:39 -0700543 aom_free(cpi->td.mb.above_pred_buf);
544 cpi->td.mb.above_pred_buf = NULL;
545
546 aom_free(cpi->td.mb.left_pred_buf);
547 cpi->td.mb.left_pred_buf = NULL;
548
549 aom_free(cpi->td.mb.wsrc_buf);
550 cpi->td.mb.wsrc_buf = NULL;
551
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530552 aom_free(cpi->td.mb.inter_modes_info);
553 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530554
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530555 for (int i = 0; i < 2; i++)
556 for (int j = 0; j < 2; j++) {
557 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
558 cpi->td.mb.hash_value_buffer[i][j] = NULL;
559 }
Jingning Hand064cf02017-06-01 10:00:39 -0700560 aom_free(cpi->td.mb.mask_buf);
561 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700562
Jingning Han6cc1fd32017-10-13 09:05:36 -0700563 aom_free(cm->tpl_mvs);
564 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700565
Yaowu Xuf883b422016-08-30 14:01:10 -0700566 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700567 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700568 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700569
Yaowu Xuf883b422016-08-30 14:01:10 -0700570 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700571 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800572 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700573 aom_free_frame_buffer(&cpi->scaled_source);
574 aom_free_frame_buffer(&cpi->scaled_last_source);
575 aom_free_frame_buffer(&cpi->alt_ref_buffer);
576 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700577
Yaowu Xuf883b422016-08-30 14:01:10 -0700578 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579 cpi->tile_tok[0][0] = 0;
580
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530581 aom_free(cpi->tplist[0][0]);
582 cpi->tplist[0][0] = NULL;
583
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000584 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700585
hui sud9a812b2017-07-06 14:34:37 -0700586 aom_free(cpi->td.mb.palette_buffer);
Hui Su38711e72019-06-11 10:49:47 -0700587 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700588 aom_free(cpi->td.mb.tmp_conv_dst);
589 for (int j = 0; j < 2; ++j) {
590 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
591 }
592
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700593#if CONFIG_DENOISE
594 if (cpi->denoise_and_model) {
595 aom_denoise_and_model_free(cpi->denoise_and_model);
596 cpi->denoise_and_model = NULL;
597 }
598#endif
599 if (cpi->film_grain_table) {
600 aom_film_grain_table_free(cpi->film_grain_table);
601 cpi->film_grain_table = NULL;
602 }
Hui Suc3a8d372019-05-28 15:52:45 -0700603
604 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
605 aom_free(cpi->level_info[i]);
606 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607}
608
Yaowu Xuf883b422016-08-30 14:01:10 -0700609static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700611 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700612
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200613 // Stores a snapshot of key state variables which can subsequently be
614 // restored with a call to av1_restore_coding_context. These functions are
615 // intended for use in a re-code loop in av1_compress_frame where the
616 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800617 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
618 av1_copy(cc->nmv_costs, cpi->nmv_costs);
619 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000620
Yaowu Xuc27fc142016-08-22 16:08:15 -0700621 cc->fc = *cm->fc;
622}
623
Yaowu Xuf883b422016-08-30 14:01:10 -0700624static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700626 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700627
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200628 // Restore key state variables to the snapshot state stored in the
629 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800630 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
631 av1_copy(cpi->nmv_costs, cc->nmv_costs);
632 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000633
Yaowu Xuc27fc142016-08-22 16:08:15 -0700634 *cm->fc = cc->fc;
635}
636
Yaowu Xuf883b422016-08-30 14:01:10 -0700637static void configure_static_seg_features(AV1_COMP *cpi) {
638 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700639 const RATE_CONTROL *const rc = &cpi->rc;
640 struct segmentation *const seg = &cm->seg;
641
642 int high_q = (int)(rc->avg_q > 48.0);
643 int qi_delta;
644
645 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000646 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647 // Clear down the global segmentation map
648 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
649 seg->update_map = 0;
650 seg->update_data = 0;
651 cpi->static_mb_pct = 0;
652
653 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655
656 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700657 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700658 } else if (cpi->refresh_alt_ref_frame) {
659 // If this is an alt ref frame
660 // Clear down the global segmentation map
661 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
662 seg->update_map = 0;
663 seg->update_data = 0;
664 cpi->static_mb_pct = 0;
665
666 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700667 av1_disable_segmentation(seg);
668 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700669
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700670#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -0700671 // Scan frames from current to arf frame.
672 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700673 av1_update_mbgraph_stats(cpi);
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700674#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700675
676 // If segmentation was enabled set those features needed for the
677 // arf itself.
678 if (seg->enabled) {
679 seg->update_map = 1;
680 seg->update_data = 1;
681
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700682 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
683 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700684 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700685 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
686 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
687 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
688 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
689
690 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
691 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
692 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
693 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700694
Yaowu Xuf883b422016-08-30 14:01:10 -0700695 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696 }
697 } else if (seg->enabled) {
698 // All other frames if segmentation has been enabled
699
700 // First normal frame in a valid gf or alt ref group
701 if (rc->frames_since_golden == 0) {
702 // Set up segment features for normal frames in an arf group
703 if (rc->source_alt_ref_active) {
704 seg->update_map = 0;
705 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700707 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
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);
710 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700711
Cheng Chend8184da2017-09-26 18:15:22 -0700712 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
713 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
714 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
715 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
716
717 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
718 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
719 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
720 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700721
722 // Segment coding disabled for compred testing
723 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700724 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
725 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
726 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700727 }
728 } else {
729 // Disable segmentation and clear down features if alt ref
730 // is not active for this group
731
Yaowu Xuf883b422016-08-30 14:01:10 -0700732 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700733
734 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
735
736 seg->update_map = 0;
737 seg->update_data = 0;
738
Yaowu Xuf883b422016-08-30 14:01:10 -0700739 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700740 }
741 } else if (rc->is_src_frame_alt_ref) {
742 // Special case where we are coding over the top of a previous
743 // alt ref frame.
744 // Segment coding disabled for compred testing
745
746 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700747 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
748 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700749
750 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700751 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
752 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
753 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
754 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700755
756 // Skip all MBs if high Q (0,0 mv and skip coeffs)
757 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700758 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
759 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700760 }
761 // Enable data update
762 seg->update_data = 1;
763 } else {
764 // All other frames.
765
766 // No updates.. leave things as they are.
767 seg->update_map = 0;
768 seg->update_data = 0;
769 }
770 }
771}
772
Yaowu Xuf883b422016-08-30 14:01:10 -0700773static void update_reference_segmentation_map(AV1_COMP *cpi) {
774 AV1_COMMON *const cm = &cpi->common;
Yushin Choa7f65922018-04-04 16:06:11 -0700775 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
David Turnerb757ce02018-11-12 15:01:28 +0000776 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700777 int row, col;
778
779 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700780 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700781 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700782 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
783 cache[0] = mi_4x4[0]->segment_id;
784 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700785 cache_ptr += cm->mi_cols;
786 }
787}
788
Yaowu Xuf883b422016-08-30 14:01:10 -0700789static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
790 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700791 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700792 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700793
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530794 if (!cpi->lookahead) {
795 int is_scale = (oxcf->resize_mode || oxcf->superres_mode);
Satish Kumar Suman29909962019-01-09 10:31:21 +0530796 cpi->lookahead = av1_lookahead_init(
797 oxcf->width, oxcf->height, seq_params->subsampling_x,
798 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530799 oxcf->lag_in_frames, oxcf->border_in_pixels, is_scale);
800 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700801 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700802 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700803 "Failed to allocate lag buffers");
804
805 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700806 if (aom_realloc_frame_buffer(
807 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
808 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530809 seq_params->use_highbitdepth, oxcf->border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700810 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700811 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700812 "Failed to allocate altref buffer");
813}
814
Yaowu Xuf883b422016-08-30 14:01:10 -0700815static void alloc_util_frame_buffers(AV1_COMP *cpi) {
816 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700817 const SequenceHeader *const seq_params = &cm->seq_params;
818 if (aom_realloc_frame_buffer(
819 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
820 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530821 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700822 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700823 "Failed to allocate last frame buffer");
824
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700825 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800826 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700827 cm->superres_upscaled_height, seq_params->subsampling_x,
828 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +0530829 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800830 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800831 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700833 if (aom_realloc_frame_buffer(
834 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
835 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530836 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700837 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700838 "Failed to allocate scaled source buffer");
839
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700840 if (aom_realloc_frame_buffer(
841 &cpi->scaled_last_source, cm->width, cm->height,
842 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530843 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700844 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700845 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846 "Failed to allocate scaled last source buffer");
847}
848
Cheng Chen46f30c72017-09-07 11:13:33 -0700849static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700850 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000851 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852
Yaowu Xuf883b422016-08-30 14:01:10 -0700853 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700854
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530855 int mi_rows_aligned_to_sb =
856 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
857 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
858
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700859 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700860
Yaowu Xuc27fc142016-08-22 16:08:15 -0700861 alloc_context_buffers_ext(cpi);
862
Yaowu Xuf883b422016-08-30 14:01:10 -0700863 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864
865 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000866 unsigned int tokens =
867 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700869 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530871 aom_free(cpi->tplist[0][0]);
872
873 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
874 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
875 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700876
Yaowu Xuf883b422016-08-30 14:01:10 -0700877 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878}
879
Yaowu Xuf883b422016-08-30 14:01:10 -0700880void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700881 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700882 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883}
884
Hui Suef139e12019-05-20 15:51:22 -0700885double av1_get_compression_ratio(const AV1_COMMON *const cm,
886 size_t encoded_frame_size) {
887 const int upscaled_width = cm->superres_upscaled_width;
888 const int height = cm->height;
889 const int luma_pic_size = upscaled_width * height;
890 const SequenceHeader *const seq_params = &cm->seq_params;
891 const BITSTREAM_PROFILE profile = seq_params->profile;
892 const int pic_size_profile_factor =
893 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
894 encoded_frame_size =
895 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
896 const size_t uncompressed_frame_size =
897 (luma_pic_size * pic_size_profile_factor) >> 3;
898 return uncompressed_frame_size / (double)encoded_frame_size;
899}
900
Yunqing Wang75e20e82018-06-16 12:10:48 -0700901static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200902 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200903 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200904
905 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200906
907 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200908 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200909 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200910 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
911 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200912 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000913 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
914 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200915 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200916 cm->uniform_tile_spacing_flag = 0;
917 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
918 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200919 size_sb = cpi->oxcf.tile_widths[j++];
920 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000921 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200922 }
923 cm->tile_cols = i;
924 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200925 }
926 av1_calculate_tile_cols(cm);
927
928 // configure tile rows
929 if (cm->uniform_tile_spacing_flag) {
930 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
931 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200932 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000933 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
934 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200935 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200936 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
937 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200938 size_sb = cpi->oxcf.tile_heights[j++];
939 if (j >= cpi->oxcf.tile_height_count) j = 0;
940 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200941 }
942 cm->tile_rows = i;
943 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200944 }
945 av1_calculate_tile_rows(cm);
946}
947
Yaowu Xuf883b422016-08-30 14:01:10 -0700948static void update_frame_size(AV1_COMP *cpi) {
949 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700950 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
951
Yaowu Xuf883b422016-08-30 14:01:10 -0700952 av1_set_mb_mi(cm, cm->width, cm->height);
953 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500954 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700955 memset(cpi->mbmi_ext_base, 0,
956 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957 set_tile_info(cpi);
958}
959
Yaowu Xuf883b422016-08-30 14:01:10 -0700960static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700961 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700962 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +0000963 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700964 cpi->rate_index = 0;
965 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700966}
967
Debargha Mukherjee57498692018-05-11 13:29:31 -0700968static INLINE int does_level_match(int width, int height, double fps,
969 int lvl_width, int lvl_height,
970 double lvl_fps, int lvl_dim_mult) {
971 const int64_t lvl_luma_pels = lvl_width * lvl_height;
972 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
973 const int64_t luma_pels = width * height;
974 const double display_sample_rate = luma_pels * fps;
975 return luma_pels <= lvl_luma_pels &&
976 display_sample_rate <= lvl_display_sample_rate &&
977 width <= lvl_width * lvl_dim_mult &&
978 height <= lvl_height * lvl_dim_mult;
979}
980
Andrey Norkin26495512018-06-20 17:13:11 -0700981static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -0700982 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700983 // TODO(any): This is a placeholder function that only addresses dimensions
984 // and max display sample rates.
985 // Need to add checks for max bit rate, max decoded luma sample rate, header
986 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -0700987 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700988 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
989 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700990 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700991 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
992 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700993 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700994 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
995 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700996 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700997 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
998 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700999 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001000 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1001 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001002 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001003 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1004 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001005 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001006 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1007 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001008 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001009 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1010 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001011 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001012 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1013 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001014 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001015 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1016 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001017 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001018 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1019 8192, 4352, 60.0, 2)) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001020 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1021 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001022 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001023 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1024 16384, 8704, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001025 level = SEQ_LEVEL_7_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001026 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1027 16384, 8704, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001028 level = SEQ_LEVEL_7_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001029 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1030 16384, 8704, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001031 level = SEQ_LEVEL_7_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001032 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001033 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001034 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001035 // Set the maximum parameters for bitrate and buffer size for this profile,
1036 // level, and tier
Yaowu Xu7e450882019-04-30 15:09:18 -07001037 cm->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001038 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001039 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1040 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001041 if (cm->op_params[i].bitrate == 0)
1042 aom_internal_error(
1043 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1044 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001045 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001046 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001047 }
1048}
1049
Andrey Norkin26495512018-06-20 17:13:11 -07001050static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
1051 const AV1EncoderConfig *oxcf) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001052 seq->still_picture = (oxcf->limit == 1);
1053 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001054 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001055 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001056 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001057 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001058 seq->frame_id_numbers_present_flag =
1059 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
1060 !oxcf->large_scale_tile && oxcf->error_resilient_mode;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001061 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001062 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001063 seq->force_screen_content_tools = 2;
1064 seq->force_integer_mv = 2;
1065 }
David Turnerebf96f42018-11-14 16:57:57 +00001066 seq->order_hint_info.order_hint_bits_minus_1 =
1067 seq->order_hint_info.enable_order_hint
1068 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1069 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001070
David Turner760a2f42018-12-07 15:25:36 +00001071 seq->max_frame_width =
1072 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1073 seq->max_frame_height = oxcf->forced_max_frame_height
1074 ? oxcf->forced_max_frame_height
1075 : oxcf->height;
1076 seq->num_bits_width =
1077 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1078 seq->num_bits_height =
1079 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1080 assert(seq->num_bits_width <= 16);
1081 assert(seq->num_bits_height <= 16);
1082
1083 seq->frame_id_length = FRAME_ID_LENGTH;
1084 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1085
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001086 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001087 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1088 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001089 seq->order_hint_info.enable_order_hint;
1090 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1091 seq->order_hint_info.enable_ref_frame_mvs &=
1092 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001093 seq->enable_superres = oxcf->enable_superres;
1094 seq->enable_cdef = oxcf->enable_cdef;
1095 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001096 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001097 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1098 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001099 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001100 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001101
Andrey Norkin26495512018-06-20 17:13:11 -07001102 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001103
1104 if (seq->operating_points_cnt_minus_1 == 0) {
1105 seq->operating_point_idc[0] = 0;
1106 } else {
1107 // Set operating_point_idc[] such that for the i-th operating point the
1108 // first (operating_points_cnt-i) spatial layers and the first temporal
1109 // layer are decoded Note that highest quality operating point should come
1110 // first
1111 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1112 seq->operating_point_idc[i] =
1113 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1114 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001115}
1116
Yaowu Xuf883b422016-08-30 14:01:10 -07001117static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1118 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001119
1120 cpi->oxcf = *oxcf;
1121 cpi->framerate = oxcf->init_framerate;
1122
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001123 cm->seq_params.profile = oxcf->profile;
1124 cm->seq_params.bit_depth = oxcf->bit_depth;
1125 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1126 cm->seq_params.color_primaries = oxcf->color_primaries;
1127 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1128 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001129 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001130 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1131 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001132 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001133 cm->timing_info.num_units_in_display_tick =
1134 oxcf->timing_info.num_units_in_display_tick;
1135 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1136 cm->timing_info.equal_picture_interval =
1137 oxcf->timing_info.equal_picture_interval;
1138 cm->timing_info.num_ticks_per_picture =
1139 oxcf->timing_info.num_ticks_per_picture;
1140
Andrey Norkin26495512018-06-20 17:13:11 -07001141 cm->seq_params.display_model_info_present_flag =
1142 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001143 cm->seq_params.decoder_model_info_present_flag =
1144 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001145 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001146 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001147 cm->buffer_model.num_units_in_decoding_tick =
1148 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001149 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07001150 av1_set_aom_dec_model_info(&cm->buffer_model);
1151 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07001152 } else if (cm->timing_info_present &&
1153 cm->timing_info.equal_picture_interval &&
1154 !cm->seq_params.decoder_model_info_present_flag) {
1155 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07001156 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001157 } else {
1158 cm->op_params[0].initial_display_delay =
1159 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001160 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001161
Tom Fineganf8d6a162018-08-21 10:47:55 -07001162 if (cm->seq_params.monochrome) {
1163 cm->seq_params.subsampling_x = 1;
1164 cm->seq_params.subsampling_y = 1;
1165 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1166 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1167 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1168 cm->seq_params.subsampling_x = 0;
1169 cm->seq_params.subsampling_y = 0;
1170 } else {
1171 if (cm->seq_params.profile == 0) {
1172 cm->seq_params.subsampling_x = 1;
1173 cm->seq_params.subsampling_y = 1;
1174 } else if (cm->seq_params.profile == 1) {
1175 cm->seq_params.subsampling_x = 0;
1176 cm->seq_params.subsampling_y = 0;
1177 } else {
1178 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1179 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1180 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1181 } else {
1182 cm->seq_params.subsampling_x = 1;
1183 cm->seq_params.subsampling_y = 0;
1184 }
1185 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001186 }
1187
Yaowu Xuc27fc142016-08-22 16:08:15 -07001188 cm->width = oxcf->width;
1189 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001190 set_sb_size(&cm->seq_params,
1191 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001192 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001193
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001194 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001195
1196 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001197 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001198
1199 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001200 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001201
1202 cpi->static_mb_pct = 0;
1203 cpi->ref_frame_flags = 0;
1204
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001205 // Reset resize pending flags
1206 cpi->resize_pending_width = 0;
1207 cpi->resize_pending_height = 0;
1208
Yaowu Xuc27fc142016-08-22 16:08:15 -07001209 init_buffer_indices(cpi);
1210}
1211
1212static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001213 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001214 const int64_t bandwidth = oxcf->target_bandwidth;
1215 const int64_t starting = oxcf->starting_buffer_level_ms;
1216 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1217 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1218
1219 rc->starting_buffer_level = starting * bandwidth / 1000;
1220 rc->optimal_buffer_level =
1221 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1222 rc->maximum_buffer_size =
1223 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1224}
1225
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001226#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1227 cpi->fn_ptr[BT].sdf = SDF; \
1228 cpi->fn_ptr[BT].sdaf = SDAF; \
1229 cpi->fn_ptr[BT].vf = VF; \
1230 cpi->fn_ptr[BT].svf = SVF; \
1231 cpi->fn_ptr[BT].svaf = SVAF; \
1232 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1233 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001234 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001235
1236#define MAKE_BFP_SAD_WRAPPER(fnname) \
1237 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1238 int source_stride, \
1239 const uint8_t *ref_ptr, int ref_stride) { \
1240 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1241 } \
1242 static unsigned int fnname##_bits10( \
1243 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1244 int ref_stride) { \
1245 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1246 } \
1247 static unsigned int fnname##_bits12( \
1248 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1249 int ref_stride) { \
1250 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1251 }
1252
1253#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1254 static unsigned int fnname##_bits8( \
1255 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1256 int ref_stride, const uint8_t *second_pred) { \
1257 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1258 } \
1259 static unsigned int fnname##_bits10( \
1260 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1261 int ref_stride, const uint8_t *second_pred) { \
1262 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1263 2; \
1264 } \
1265 static unsigned int fnname##_bits12( \
1266 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1267 int ref_stride, const uint8_t *second_pred) { \
1268 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1269 4; \
1270 }
1271
Yaowu Xuc27fc142016-08-22 16:08:15 -07001272#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1273 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1274 const uint8_t *const ref_ptr[], int ref_stride, \
1275 unsigned int *sad_array) { \
1276 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1277 } \
1278 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1279 const uint8_t *const ref_ptr[], int ref_stride, \
1280 unsigned int *sad_array) { \
1281 int i; \
1282 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1283 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1284 } \
1285 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1286 const uint8_t *const ref_ptr[], int ref_stride, \
1287 unsigned int *sad_array) { \
1288 int i; \
1289 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1290 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1291 }
1292
Cheng Chenbf3d4962017-11-01 14:48:52 -07001293#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1294 static unsigned int fnname##_bits8( \
1295 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1296 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001297 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001298 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1299 jcp_param); \
1300 } \
1301 static unsigned int fnname##_bits10( \
1302 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1303 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001304 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001305 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1306 jcp_param) >> \
1307 2; \
1308 } \
1309 static unsigned int fnname##_bits12( \
1310 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1311 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001312 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001313 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1314 jcp_param) >> \
1315 4; \
1316 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001317
Yaowu Xuf883b422016-08-30 14:01:10 -07001318MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1319MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001320MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1321MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1322MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1323MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1324MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1325MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1326MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001327MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1328MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1329MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1330MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1331MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1332MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1333MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1334MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1335MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1336MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1337MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1338MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1339MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1340MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001341MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1342MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1343MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001344MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1345MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1346MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001347MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1348MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1349MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001350MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1351MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1352MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001353MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1354MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1355MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001356MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1357MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1358MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001359MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1360MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1361MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001362MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1363MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1364MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001365MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001366
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001367MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1368MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1369MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1370MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1371MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1372MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1373MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1374MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1375MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1376MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1377MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1378MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001379MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1380MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1381MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1382MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1383MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1384MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001385
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001386MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1387MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1388MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1389MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1390MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1391MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1392MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1393MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1394MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1395MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1396MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1397MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1398MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1399MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1400MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1401MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1402MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1403MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1404MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1405MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1406MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1407MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001408
David Barker0f3c94e2017-05-16 15:21:50 +01001409#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001410 cpi->fn_ptr[BT].msdf = MCSDF; \
1411 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001412
David Barkerc155e012017-05-11 13:54:54 +01001413#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1414 static unsigned int fnname##_bits8( \
1415 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1416 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1417 int m_stride, int invert_mask) { \
1418 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1419 second_pred_ptr, m, m_stride, invert_mask); \
1420 } \
1421 static unsigned int fnname##_bits10( \
1422 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1423 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1424 int m_stride, int invert_mask) { \
1425 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1426 second_pred_ptr, m, m_stride, invert_mask) >> \
1427 2; \
1428 } \
1429 static unsigned int fnname##_bits12( \
1430 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1431 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1432 int m_stride, int invert_mask) { \
1433 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1434 second_pred_ptr, m, m_stride, invert_mask) >> \
1435 4; \
1436 }
1437
David Barkerf19f35f2017-05-22 16:33:22 +01001438MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1439MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1440MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001441MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1442MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1443MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1444MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1445MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1446MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1447MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1448MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1449MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1450MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1451MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1452MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1453MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001454MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1455MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1456MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1457MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001458MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1459MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001460
Yaowu Xuc27fc142016-08-22 16:08:15 -07001461#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1462 cpi->fn_ptr[BT].osdf = OSDF; \
1463 cpi->fn_ptr[BT].ovf = OVF; \
1464 cpi->fn_ptr[BT].osvf = OSVF;
1465
1466#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1467 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1468 const int32_t *wsrc, \
1469 const int32_t *msk) { \
1470 return fnname(ref, ref_stride, wsrc, msk); \
1471 } \
1472 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1473 const int32_t *wsrc, \
1474 const int32_t *msk) { \
1475 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1476 } \
1477 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1478 const int32_t *wsrc, \
1479 const int32_t *msk) { \
1480 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1481 }
1482
Yaowu Xuf883b422016-08-30 14:01:10 -07001483MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1484MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1485MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001486MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1487MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1488MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1489MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1490MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1491MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1492MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1493MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1494MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1495MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1496MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1497MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1498MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001499MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1500MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1501MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1502MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001503MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1504MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001505
Yaowu Xuf883b422016-08-30 14:01:10 -07001506static void highbd_set_var_fns(AV1_COMP *const cpi) {
1507 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001508 if (cm->seq_params.use_highbitdepth) {
1509 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001510 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001511 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1512 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1513 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001514 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001515 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001516 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1517 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001518
1519 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1520 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1521 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001522 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001523 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001524 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1525 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001526
1527 HIGHBD_BFP(
1528 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1529 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001530 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001531 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1532 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001533
1534 HIGHBD_BFP(
1535 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1536 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001537 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001538 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1539 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001540
1541 HIGHBD_BFP(
1542 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1543 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001544 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001545 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1546 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001547
1548 HIGHBD_BFP(
1549 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1550 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001551 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001552 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1553 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001554
1555 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1556 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1557 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001558 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001559 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001560 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1561 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001562
1563 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1564 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1565 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001566 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001567 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001568 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1569 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001570
1571 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1572 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1573 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001574 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001575 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001576 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1577 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001578
1579 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1580 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1581 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001582 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001583 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001584 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1585 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001586
1587 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1588 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1589 aom_highbd_8_sub_pixel_variance32x32,
1590 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001591 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001592 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1593 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001594
1595 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1596 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1597 aom_highbd_8_sub_pixel_variance64x64,
1598 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001599 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001600 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1601 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001602
1603 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1604 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1605 aom_highbd_8_sub_pixel_variance16x16,
1606 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001607 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001608 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1609 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001610
1611 HIGHBD_BFP(
1612 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1613 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001614 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001615 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1616 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001617
1618 HIGHBD_BFP(
1619 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1620 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001621 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001622 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1623 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001624
Cheng Chenbf3d4962017-11-01 14:48:52 -07001625 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001626 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1627 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1628 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1629 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1630 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1631
1632 HIGHBD_BFP(
1633 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1634 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1635 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1636 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1637 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1638
1639 HIGHBD_BFP(
1640 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1641 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1642 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1643 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1644 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1645
1646 HIGHBD_BFP(
1647 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1648 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1649 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1650 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1651 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1652
1653 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1654 aom_highbd_sad128x128_avg_bits8,
1655 aom_highbd_8_variance128x128,
1656 aom_highbd_8_sub_pixel_variance128x128,
1657 aom_highbd_8_sub_pixel_avg_variance128x128,
1658 aom_highbd_sad128x128x4d_bits8,
1659 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1660 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001661
1662 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1663 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1664 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001665 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001666 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001667 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1668 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001669
1670 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1671 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1672 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001673 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001674 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001675 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1676 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001677
David Barkerf19f35f2017-05-22 16:33:22 +01001678 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1679 aom_highbd_8_masked_sub_pixel_variance128x128)
1680 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1681 aom_highbd_8_masked_sub_pixel_variance128x64)
1682 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1683 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001684 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1685 aom_highbd_8_masked_sub_pixel_variance64x64)
1686 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1687 aom_highbd_8_masked_sub_pixel_variance64x32)
1688 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1689 aom_highbd_8_masked_sub_pixel_variance32x64)
1690 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1691 aom_highbd_8_masked_sub_pixel_variance32x32)
1692 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1693 aom_highbd_8_masked_sub_pixel_variance32x16)
1694 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1695 aom_highbd_8_masked_sub_pixel_variance16x32)
1696 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1697 aom_highbd_8_masked_sub_pixel_variance16x16)
1698 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1699 aom_highbd_8_masked_sub_pixel_variance8x16)
1700 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1701 aom_highbd_8_masked_sub_pixel_variance16x8)
1702 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1703 aom_highbd_8_masked_sub_pixel_variance8x8)
1704 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1705 aom_highbd_8_masked_sub_pixel_variance4x8)
1706 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1707 aom_highbd_8_masked_sub_pixel_variance8x4)
1708 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1709 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001710 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1711 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001712 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1713 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001714 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1715 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001716 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1717 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001718 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1719 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001720 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1721 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001722 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1723 aom_highbd_obmc_variance128x128,
1724 aom_highbd_obmc_sub_pixel_variance128x128)
1725 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1726 aom_highbd_obmc_variance128x64,
1727 aom_highbd_obmc_sub_pixel_variance128x64)
1728 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1729 aom_highbd_obmc_variance64x128,
1730 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001731 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1732 aom_highbd_obmc_variance64x64,
1733 aom_highbd_obmc_sub_pixel_variance64x64)
1734 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1735 aom_highbd_obmc_variance64x32,
1736 aom_highbd_obmc_sub_pixel_variance64x32)
1737 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1738 aom_highbd_obmc_variance32x64,
1739 aom_highbd_obmc_sub_pixel_variance32x64)
1740 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1741 aom_highbd_obmc_variance32x32,
1742 aom_highbd_obmc_sub_pixel_variance32x32)
1743 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1744 aom_highbd_obmc_variance32x16,
1745 aom_highbd_obmc_sub_pixel_variance32x16)
1746 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1747 aom_highbd_obmc_variance16x32,
1748 aom_highbd_obmc_sub_pixel_variance16x32)
1749 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1750 aom_highbd_obmc_variance16x16,
1751 aom_highbd_obmc_sub_pixel_variance16x16)
1752 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1753 aom_highbd_obmc_variance8x16,
1754 aom_highbd_obmc_sub_pixel_variance8x16)
1755 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1756 aom_highbd_obmc_variance16x8,
1757 aom_highbd_obmc_sub_pixel_variance16x8)
1758 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1759 aom_highbd_obmc_variance8x8,
1760 aom_highbd_obmc_sub_pixel_variance8x8)
1761 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1762 aom_highbd_obmc_variance4x8,
1763 aom_highbd_obmc_sub_pixel_variance4x8)
1764 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1765 aom_highbd_obmc_variance8x4,
1766 aom_highbd_obmc_sub_pixel_variance8x4)
1767 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1768 aom_highbd_obmc_variance4x4,
1769 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001770 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1771 aom_highbd_obmc_variance64x16,
1772 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001773 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1774 aom_highbd_obmc_variance16x64,
1775 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001776 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1777 aom_highbd_obmc_variance32x8,
1778 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001779 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1780 aom_highbd_obmc_variance8x32,
1781 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001782 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1783 aom_highbd_obmc_variance16x4,
1784 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001785 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1786 aom_highbd_obmc_variance4x16,
1787 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001788 break;
1789
Yaowu Xuf883b422016-08-30 14:01:10 -07001790 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001791 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1792 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1793 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001794 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001795 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001796 aom_highbd_dist_wtd_sad64x16_avg_bits10,
1797 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001798
1799 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1800 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1801 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001802 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001803 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001804 aom_highbd_dist_wtd_sad16x64_avg_bits10,
1805 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001806
1807 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1808 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1809 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001810 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001811 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001812 aom_highbd_dist_wtd_sad32x8_avg_bits10,
1813 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001814
1815 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1816 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1817 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001818 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001819 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001820 aom_highbd_dist_wtd_sad8x32_avg_bits10,
1821 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001822
1823 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1824 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1825 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001826 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001827 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001828 aom_highbd_dist_wtd_sad16x4_avg_bits10,
1829 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001830
1831 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1832 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1833 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001834 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001835 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001836 aom_highbd_dist_wtd_sad4x16_avg_bits10,
1837 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001838
1839 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1840 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1841 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001842 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001843 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001844 aom_highbd_dist_wtd_sad32x16_avg_bits10,
1845 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001846
1847 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1848 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1849 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001850 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001851 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001852 aom_highbd_dist_wtd_sad16x32_avg_bits10,
1853 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001854
1855 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1856 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1857 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001858 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001859 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001860 aom_highbd_dist_wtd_sad64x32_avg_bits10,
1861 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001862
1863 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1864 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1865 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001866 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001867 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001868 aom_highbd_dist_wtd_sad32x64_avg_bits10,
1869 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001870
1871 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1872 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1873 aom_highbd_10_sub_pixel_variance32x32,
1874 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001875 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001876 aom_highbd_dist_wtd_sad32x32_avg_bits10,
1877 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001878
1879 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1880 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1881 aom_highbd_10_sub_pixel_variance64x64,
1882 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001883 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001884 aom_highbd_dist_wtd_sad64x64_avg_bits10,
1885 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001886
1887 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1888 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1889 aom_highbd_10_sub_pixel_variance16x16,
1890 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001891 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001892 aom_highbd_dist_wtd_sad16x16_avg_bits10,
1893 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001894
1895 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1896 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1897 aom_highbd_10_sub_pixel_variance16x8,
1898 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001899 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001900 aom_highbd_dist_wtd_sad16x8_avg_bits10,
1901 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001902
1903 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1904 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1905 aom_highbd_10_sub_pixel_variance8x16,
1906 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001907 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001908 aom_highbd_dist_wtd_sad8x16_avg_bits10,
1909 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001910
1911 HIGHBD_BFP(
1912 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1913 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001914 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001915 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
1916 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001917
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001918 HIGHBD_BFP(
1919 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1920 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1921 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001922 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
1923 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001924
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001925 HIGHBD_BFP(
1926 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1927 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1928 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001929 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
1930 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001931
1932 HIGHBD_BFP(
1933 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1934 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001935 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001936 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
1937 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001938
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001939 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1940 aom_highbd_sad128x128_avg_bits10,
1941 aom_highbd_10_variance128x128,
1942 aom_highbd_10_sub_pixel_variance128x128,
1943 aom_highbd_10_sub_pixel_avg_variance128x128,
1944 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001945 aom_highbd_dist_wtd_sad128x128_avg_bits10,
1946 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001947
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001948 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1949 aom_highbd_sad128x64_avg_bits10,
1950 aom_highbd_10_variance128x64,
1951 aom_highbd_10_sub_pixel_variance128x64,
1952 aom_highbd_10_sub_pixel_avg_variance128x64,
1953 aom_highbd_sad128x64x4d_bits10,
1954 aom_highbd_dist_wtd_sad128x64_avg_bits10,
1955 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001956
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001957 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1958 aom_highbd_sad64x128_avg_bits10,
1959 aom_highbd_10_variance64x128,
1960 aom_highbd_10_sub_pixel_variance64x128,
1961 aom_highbd_10_sub_pixel_avg_variance64x128,
1962 aom_highbd_sad64x128x4d_bits10,
1963 aom_highbd_dist_wtd_sad64x128_avg_bits10,
1964 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001965
David Barkerf19f35f2017-05-22 16:33:22 +01001966 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1967 aom_highbd_10_masked_sub_pixel_variance128x128)
1968 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1969 aom_highbd_10_masked_sub_pixel_variance128x64)
1970 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1971 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001972 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1973 aom_highbd_10_masked_sub_pixel_variance64x64)
1974 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1975 aom_highbd_10_masked_sub_pixel_variance64x32)
1976 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1977 aom_highbd_10_masked_sub_pixel_variance32x64)
1978 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1979 aom_highbd_10_masked_sub_pixel_variance32x32)
1980 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1981 aom_highbd_10_masked_sub_pixel_variance32x16)
1982 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1983 aom_highbd_10_masked_sub_pixel_variance16x32)
1984 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1985 aom_highbd_10_masked_sub_pixel_variance16x16)
1986 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1987 aom_highbd_10_masked_sub_pixel_variance8x16)
1988 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1989 aom_highbd_10_masked_sub_pixel_variance16x8)
1990 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1991 aom_highbd_10_masked_sub_pixel_variance8x8)
1992 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1993 aom_highbd_10_masked_sub_pixel_variance4x8)
1994 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1995 aom_highbd_10_masked_sub_pixel_variance8x4)
1996 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1997 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001998 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1999 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002000 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2001 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002002 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2003 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002004 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2005 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002006 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2007 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002008 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2009 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002010 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2011 aom_highbd_10_obmc_variance128x128,
2012 aom_highbd_10_obmc_sub_pixel_variance128x128)
2013 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2014 aom_highbd_10_obmc_variance128x64,
2015 aom_highbd_10_obmc_sub_pixel_variance128x64)
2016 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2017 aom_highbd_10_obmc_variance64x128,
2018 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002019 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2020 aom_highbd_10_obmc_variance64x64,
2021 aom_highbd_10_obmc_sub_pixel_variance64x64)
2022 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2023 aom_highbd_10_obmc_variance64x32,
2024 aom_highbd_10_obmc_sub_pixel_variance64x32)
2025 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2026 aom_highbd_10_obmc_variance32x64,
2027 aom_highbd_10_obmc_sub_pixel_variance32x64)
2028 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2029 aom_highbd_10_obmc_variance32x32,
2030 aom_highbd_10_obmc_sub_pixel_variance32x32)
2031 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2032 aom_highbd_10_obmc_variance32x16,
2033 aom_highbd_10_obmc_sub_pixel_variance32x16)
2034 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2035 aom_highbd_10_obmc_variance16x32,
2036 aom_highbd_10_obmc_sub_pixel_variance16x32)
2037 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2038 aom_highbd_10_obmc_variance16x16,
2039 aom_highbd_10_obmc_sub_pixel_variance16x16)
2040 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2041 aom_highbd_10_obmc_variance8x16,
2042 aom_highbd_10_obmc_sub_pixel_variance8x16)
2043 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2044 aom_highbd_10_obmc_variance16x8,
2045 aom_highbd_10_obmc_sub_pixel_variance16x8)
2046 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2047 aom_highbd_10_obmc_variance8x8,
2048 aom_highbd_10_obmc_sub_pixel_variance8x8)
2049 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2050 aom_highbd_10_obmc_variance4x8,
2051 aom_highbd_10_obmc_sub_pixel_variance4x8)
2052 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2053 aom_highbd_10_obmc_variance8x4,
2054 aom_highbd_10_obmc_sub_pixel_variance8x4)
2055 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2056 aom_highbd_10_obmc_variance4x4,
2057 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002058
Rupert Swarbrick72678572017-08-02 12:05:26 +01002059 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2060 aom_highbd_10_obmc_variance64x16,
2061 aom_highbd_10_obmc_sub_pixel_variance64x16)
2062
2063 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2064 aom_highbd_10_obmc_variance16x64,
2065 aom_highbd_10_obmc_sub_pixel_variance16x64)
2066
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002067 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2068 aom_highbd_10_obmc_variance32x8,
2069 aom_highbd_10_obmc_sub_pixel_variance32x8)
2070
2071 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2072 aom_highbd_10_obmc_variance8x32,
2073 aom_highbd_10_obmc_sub_pixel_variance8x32)
2074
2075 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2076 aom_highbd_10_obmc_variance16x4,
2077 aom_highbd_10_obmc_sub_pixel_variance16x4)
2078
2079 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2080 aom_highbd_10_obmc_variance4x16,
2081 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002082 break;
2083
Yaowu Xuf883b422016-08-30 14:01:10 -07002084 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002085 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2086 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2087 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002088 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002089 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002090 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2091 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002092
2093 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2094 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2095 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002096 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002097 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002098 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2099 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002100
2101 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2102 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2103 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002104 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002105 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002106 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2107 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002108
2109 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2110 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2111 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002112 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002113 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002114 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2115 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002116
2117 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2118 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2119 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002120 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002121 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002122 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2123 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002124
2125 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2126 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2127 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002128 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002129 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002130 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2131 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002132
2133 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2134 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2135 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002136 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002137 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002138 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2139 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002140
2141 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2142 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2143 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002144 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002145 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002146 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2147 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002148
2149 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2150 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2151 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002152 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002153 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002154 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2155 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002156
2157 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2158 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2159 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002160 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002161 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002162 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2163 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002164
2165 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2166 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2167 aom_highbd_12_sub_pixel_variance32x32,
2168 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002169 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002170 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2171 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002172
2173 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2174 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2175 aom_highbd_12_sub_pixel_variance64x64,
2176 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002177 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002178 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2179 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002180
2181 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2182 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2183 aom_highbd_12_sub_pixel_variance16x16,
2184 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002185 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002186 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2187 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002188
2189 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2190 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2191 aom_highbd_12_sub_pixel_variance16x8,
2192 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002193 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002194 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2195 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002196
2197 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2198 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2199 aom_highbd_12_sub_pixel_variance8x16,
2200 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002201 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002202 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2203 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002204
2205 HIGHBD_BFP(
2206 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2207 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002208 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002209 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2210 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002211
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002212 HIGHBD_BFP(
2213 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2214 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2215 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002216 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2217 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002218
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002219 HIGHBD_BFP(
2220 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2221 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2222 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002223 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2224 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002225
2226 HIGHBD_BFP(
2227 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2228 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002229 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002230 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2231 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002232
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002233 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2234 aom_highbd_sad128x128_avg_bits12,
2235 aom_highbd_12_variance128x128,
2236 aom_highbd_12_sub_pixel_variance128x128,
2237 aom_highbd_12_sub_pixel_avg_variance128x128,
2238 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002239 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2240 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002241
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002242 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2243 aom_highbd_sad128x64_avg_bits12,
2244 aom_highbd_12_variance128x64,
2245 aom_highbd_12_sub_pixel_variance128x64,
2246 aom_highbd_12_sub_pixel_avg_variance128x64,
2247 aom_highbd_sad128x64x4d_bits12,
2248 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2249 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002250
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002251 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2252 aom_highbd_sad64x128_avg_bits12,
2253 aom_highbd_12_variance64x128,
2254 aom_highbd_12_sub_pixel_variance64x128,
2255 aom_highbd_12_sub_pixel_avg_variance64x128,
2256 aom_highbd_sad64x128x4d_bits12,
2257 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2258 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002259
David Barkerf19f35f2017-05-22 16:33:22 +01002260 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2261 aom_highbd_12_masked_sub_pixel_variance128x128)
2262 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2263 aom_highbd_12_masked_sub_pixel_variance128x64)
2264 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2265 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002266 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2267 aom_highbd_12_masked_sub_pixel_variance64x64)
2268 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2269 aom_highbd_12_masked_sub_pixel_variance64x32)
2270 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2271 aom_highbd_12_masked_sub_pixel_variance32x64)
2272 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2273 aom_highbd_12_masked_sub_pixel_variance32x32)
2274 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2275 aom_highbd_12_masked_sub_pixel_variance32x16)
2276 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2277 aom_highbd_12_masked_sub_pixel_variance16x32)
2278 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2279 aom_highbd_12_masked_sub_pixel_variance16x16)
2280 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2281 aom_highbd_12_masked_sub_pixel_variance8x16)
2282 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2283 aom_highbd_12_masked_sub_pixel_variance16x8)
2284 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2285 aom_highbd_12_masked_sub_pixel_variance8x8)
2286 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2287 aom_highbd_12_masked_sub_pixel_variance4x8)
2288 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2289 aom_highbd_12_masked_sub_pixel_variance8x4)
2290 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2291 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002292 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2293 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002294 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2295 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002296 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2297 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002298 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2299 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002300 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2301 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002302 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2303 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002304 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2305 aom_highbd_12_obmc_variance128x128,
2306 aom_highbd_12_obmc_sub_pixel_variance128x128)
2307 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2308 aom_highbd_12_obmc_variance128x64,
2309 aom_highbd_12_obmc_sub_pixel_variance128x64)
2310 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2311 aom_highbd_12_obmc_variance64x128,
2312 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002313 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2314 aom_highbd_12_obmc_variance64x64,
2315 aom_highbd_12_obmc_sub_pixel_variance64x64)
2316 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2317 aom_highbd_12_obmc_variance64x32,
2318 aom_highbd_12_obmc_sub_pixel_variance64x32)
2319 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2320 aom_highbd_12_obmc_variance32x64,
2321 aom_highbd_12_obmc_sub_pixel_variance32x64)
2322 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2323 aom_highbd_12_obmc_variance32x32,
2324 aom_highbd_12_obmc_sub_pixel_variance32x32)
2325 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2326 aom_highbd_12_obmc_variance32x16,
2327 aom_highbd_12_obmc_sub_pixel_variance32x16)
2328 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2329 aom_highbd_12_obmc_variance16x32,
2330 aom_highbd_12_obmc_sub_pixel_variance16x32)
2331 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2332 aom_highbd_12_obmc_variance16x16,
2333 aom_highbd_12_obmc_sub_pixel_variance16x16)
2334 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2335 aom_highbd_12_obmc_variance8x16,
2336 aom_highbd_12_obmc_sub_pixel_variance8x16)
2337 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2338 aom_highbd_12_obmc_variance16x8,
2339 aom_highbd_12_obmc_sub_pixel_variance16x8)
2340 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2341 aom_highbd_12_obmc_variance8x8,
2342 aom_highbd_12_obmc_sub_pixel_variance8x8)
2343 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2344 aom_highbd_12_obmc_variance4x8,
2345 aom_highbd_12_obmc_sub_pixel_variance4x8)
2346 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2347 aom_highbd_12_obmc_variance8x4,
2348 aom_highbd_12_obmc_sub_pixel_variance8x4)
2349 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2350 aom_highbd_12_obmc_variance4x4,
2351 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002352 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2353 aom_highbd_12_obmc_variance64x16,
2354 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002355 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2356 aom_highbd_12_obmc_variance16x64,
2357 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002358 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2359 aom_highbd_12_obmc_variance32x8,
2360 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002361 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2362 aom_highbd_12_obmc_variance8x32,
2363 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002364 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2365 aom_highbd_12_obmc_variance16x4,
2366 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002367 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2368 aom_highbd_12_obmc_variance4x16,
2369 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370 break;
2371
2372 default:
2373 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002374 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002375 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002376 }
2377 }
2378}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002379
Yaowu Xuf883b422016-08-30 14:01:10 -07002380static void realloc_segmentation_maps(AV1_COMP *cpi) {
2381 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002382
2383 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002384 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002385 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002386 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002387
2388 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002389 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002391 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392
2393 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002394 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002395 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002396 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397}
2398
Hui Su38711e72019-06-11 10:49:47 -07002399void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2400 CompoundTypeRdBuffers *const bufs) {
2401 CHECK_MEM_ERROR(
2402 cm, bufs->pred0,
2403 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2404 CHECK_MEM_ERROR(
2405 cm, bufs->pred1,
2406 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2407 CHECK_MEM_ERROR(
2408 cm, bufs->residual1,
2409 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2410 CHECK_MEM_ERROR(
2411 cm, bufs->diff10,
2412 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2413 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2414 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2415 sizeof(*bufs->tmp_best_mask_buf)));
2416}
2417
2418void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2419 aom_free(bufs->pred0);
2420 aom_free(bufs->pred1);
2421 aom_free(bufs->residual1);
2422 aom_free(bufs->diff10);
2423 aom_free(bufs->tmp_best_mask_buf);
2424 av1_zero(*bufs); // Set all pointers to NULL for safety.
2425}
2426
Yaowu Xuf883b422016-08-30 14:01:10 -07002427void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2428 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002429 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002430 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002431 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002432 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002433
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002434 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2435 seq_params->bit_depth = oxcf->bit_depth;
2436 seq_params->color_primaries = oxcf->color_primaries;
2437 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2438 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2439 seq_params->monochrome = oxcf->monochrome;
2440 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2441 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002442
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002443 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2444 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002445
Hui Su2aa492c2019-03-12 15:18:18 -07002446 memcpy(cpi->target_seq_level_idx, oxcf->target_seq_level_idx,
2447 sizeof(cpi->target_seq_level_idx));
2448 cpi->keep_level_stats = 0;
2449 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su6d666502019-06-13 15:16:50 -07002450 if (cpi->target_seq_level_idx[i] <= SEQ_LEVELS) {
Hui Su72ff0482019-05-28 14:07:37 -07002451 cpi->keep_level_stats |= 1u << i;
Hui Suc3a8d372019-05-28 15:52:45 -07002452 if (!cpi->level_info[i]) {
2453 CHECK_MEM_ERROR(cm, cpi->level_info[i],
2454 aom_calloc(1, sizeof(*cpi->level_info[i])));
2455 }
Hui Su2aa492c2019-03-12 15:18:18 -07002456 }
2457 }
kyslovabeeb7c2019-03-06 18:35:04 -08002458
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002459 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002460 cm->timing_info.num_units_in_display_tick =
2461 oxcf->timing_info.num_units_in_display_tick;
2462 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2463 cm->timing_info.equal_picture_interval =
2464 oxcf->timing_info.equal_picture_interval;
2465 cm->timing_info.num_ticks_per_picture =
2466 oxcf->timing_info.num_ticks_per_picture;
2467
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002468 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002469 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002470 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002471 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002472 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002473 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002474 cm->buffer_model.num_units_in_decoding_tick =
2475 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002476 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07002477 av1_set_aom_dec_model_info(&cm->buffer_model);
2478 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07002479 } else if (cm->timing_info_present &&
2480 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002481 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002482 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07002483 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002484 } else {
2485 cm->op_params[0].initial_display_delay =
2486 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002487 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002488
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002489 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002490
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002492 cpi->common.options = oxcf->cfg;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002493 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002494 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002495
Yaowu Xuf883b422016-08-30 14:01:10 -07002496 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002497 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2498 } else {
2499 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2500 }
2501
2502 cpi->refresh_last_frame = 1;
2503 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002504 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002505 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002506
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002507 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2508 ? REFRESH_FRAME_CONTEXT_DISABLED
2509 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002510 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002511 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002512
Alex Converse74ad0912017-07-18 10:22:58 -07002513 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002514 CHECK_MEM_ERROR(cm, x->palette_buffer,
2515 aom_memalign(16, sizeof(*x->palette_buffer)));
2516 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002517
Hui Su38711e72019-06-11 10:49:47 -07002518 if (x->comp_rd_buffer.pred0 == NULL) {
2519 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2520 }
2521
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002522 if (x->tmp_conv_dst == NULL) {
2523 CHECK_MEM_ERROR(
2524 cm, x->tmp_conv_dst,
2525 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002526 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002527 }
2528 for (int i = 0; i < 2; ++i) {
2529 if (x->tmp_obmc_bufs[i] == NULL) {
2530 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002531 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002532 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002533 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002534 }
2535 }
2536
Yaowu Xuf883b422016-08-30 14:01:10 -07002537 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002538 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002539
Yaowu Xuc27fc142016-08-22 16:08:15 -07002540 set_rc_buffer_sizes(rc, &cpi->oxcf);
2541
2542 // Under a configuration change, where maximum_buffer_size may change,
2543 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002544 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2545 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002546
2547 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002548 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002549
2550 // Set absolute upper and lower quality limits
2551 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2552 rc->best_quality = cpi->oxcf.best_allowed_q;
2553
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002554 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002555 cm->switchable_motion_mode = 1;
2556
Yaowu Xuc27fc142016-08-22 16:08:15 -07002557 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2558 cm->render_width = cpi->oxcf.render_width;
2559 cm->render_height = cpi->oxcf.render_height;
2560 } else {
2561 cm->render_width = cpi->oxcf.width;
2562 cm->render_height = cpi->oxcf.height;
2563 }
2564 cm->width = cpi->oxcf.width;
2565 cm->height = cpi->oxcf.height;
2566
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002567 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002568 // Superblock size should not be updated after the first key frame.
2569 if (!cpi->seq_params_locked) {
2570 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002571 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2572 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002573 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002574
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002575 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002576 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002577 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002578 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002579 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002580 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002581 realloc_segmentation_maps(cpi);
2582 cpi->initial_width = cpi->initial_height = 0;
2583 }
2584 }
2585 update_frame_size(cpi);
2586
2587 cpi->alt_ref_source = NULL;
2588 rc->is_src_frame_alt_ref = 0;
2589
Yaowu Xuc27fc142016-08-22 16:08:15 -07002590 set_tile_info(cpi);
2591
2592 cpi->ext_refresh_frame_flags_pending = 0;
2593 cpi->ext_refresh_frame_context_pending = 0;
2594
Yaowu Xuc27fc142016-08-22 16:08:15 -07002595 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002596
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002597 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002598 // This should not be called after the first key frame.
2599 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002600 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002601 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Andrey Norkin26495512018-06-20 17:13:11 -07002602 init_seq_coding_tools(&cm->seq_params, cm, oxcf);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002603 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002604}
2605
Yaowu Xuf883b422016-08-30 14:01:10 -07002606AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2607 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002608 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002609 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2610 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002611
2612 if (!cm) return NULL;
2613
Yaowu Xuf883b422016-08-30 14:01:10 -07002614 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002615
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002616 // The jmp_buf is valid only for the duration of the function that calls
2617 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2618 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002619 if (setjmp(cm->error.jmp)) {
2620 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002621 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622 return 0;
2623 }
2624
2625 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002626 cm->alloc_mi = enc_alloc_mi;
2627 cm->free_mi = enc_free_mi;
2628 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002629
Angie Chianga5d96c42016-10-21 16:16:56 -07002630 CHECK_MEM_ERROR(cm, cm->fc,
2631 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002632 CHECK_MEM_ERROR(
2633 cm, cm->default_frame_context,
2634 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002635 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002636 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002637
2638 cpi->resize_state = 0;
2639 cpi->resize_avg_qp = 0;
2640 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002641
Yaowu Xuc27fc142016-08-22 16:08:15 -07002642 cpi->common.buffer_pool = pool;
2643
2644 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002645 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002646
David Turnerd2a592e2018-11-16 14:59:31 +00002647 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002648 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002649 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002650 cpi->partition_search_skippable_frame = 0;
2651 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00002652 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653 realloc_segmentation_maps(cpi);
2654
Jingning Hanf050fc12018-03-09 14:53:33 -08002655 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2656 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002657
Yaowu Xuc27fc142016-08-22 16:08:15 -07002658 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2659 i++) {
2660 CHECK_MEM_ERROR(
2661 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002662 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002663 }
2664
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666
2667 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2668#if CONFIG_INTERNAL_STATS
2669 cpi->b_calculate_blockiness = 1;
2670 cpi->b_calculate_consistency = 1;
2671 cpi->total_inconsistency = 0;
2672 cpi->psnr.worst = 100.0;
2673 cpi->worst_ssim = 100.0;
2674
2675 cpi->count = 0;
2676 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08002677#if CONFIG_SPEED_STATS
2678 cpi->tx_search_count = 0;
2679#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002680
2681 if (cpi->b_calculate_psnr) {
2682 cpi->total_sq_error = 0;
2683 cpi->total_samples = 0;
2684 cpi->tot_recode_hits = 0;
2685 cpi->summed_quality = 0;
2686 cpi->summed_weights = 0;
2687 }
2688
2689 cpi->fastssim.worst = 100.0;
2690 cpi->psnrhvs.worst = 100.0;
2691
2692 if (cpi->b_calculate_blockiness) {
2693 cpi->total_blockiness = 0;
2694 cpi->worst_blockiness = 0.0;
2695 }
2696
2697 if (cpi->b_calculate_consistency) {
2698 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002699 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002700 cpi->common.mi_rows * cpi->common.mi_cols));
2701 cpi->worst_consistency = 100.0;
2702 }
2703#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002704#if CONFIG_ENTROPY_STATS
2705 av1_zero(aggregate_fc);
2706#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002707
2708 cpi->first_time_stamp_ever = INT64_MAX;
2709
Jingning Hanf050fc12018-03-09 14:53:33 -08002710 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2711 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2712 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2713 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002714
Yaowu Xuc27fc142016-08-22 16:08:15 -07002715#ifdef OUTPUT_YUV_SKINMAP
2716 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2717#endif
2718#ifdef OUTPUT_YUV_REC
2719 yuv_rec_file = fopen("rec.yuv", "wb");
2720#endif
2721
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002722#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -07002723 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002724 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002725 } else if (oxcf->pass == 2) {
2726 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2727 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2728
Yaowu Xuc27fc142016-08-22 16:08:15 -07002729 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2730 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2731 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2732
Yaowu Xuf883b422016-08-30 14:01:10 -07002733 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002734 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002735#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002736
Jingning Hand064cf02017-06-01 10:00:39 -07002737 CHECK_MEM_ERROR(
2738 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002739 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002740 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002741 CHECK_MEM_ERROR(
2742 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002743 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002744 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002745
2746 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2747 (int32_t *)aom_memalign(
2748 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2749
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302750 CHECK_MEM_ERROR(
2751 cm, cpi->td.mb.inter_modes_info,
2752 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302753
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302754 for (int x = 0; x < 2; x++)
2755 for (int y = 0; y < 2; y++)
2756 CHECK_MEM_ERROR(
2757 cm, cpi->td.mb.hash_value_buffer[x][y],
2758 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2759 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2760
2761 cpi->td.mb.g_crc_initialized = 0;
2762
Jingning Hand064cf02017-06-01 10:00:39 -07002763 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2764 (int32_t *)aom_memalign(
2765 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2766
David Turner04b70d82019-01-24 15:39:19 +00002767 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
2768 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002769
sdengc23c7f12019-06-11 16:56:50 -07002770 {
2771 const int bsize = BLOCK_16X16;
2772 const int w = mi_size_wide[bsize];
2773 const int h = mi_size_high[bsize];
2774 const int num_cols = (cm->mi_cols + w - 1) / w;
2775 const int num_rows = (cm->mi_rows + h - 1) / h;
2776 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
2777 aom_calloc(num_rows * num_cols,
2778 sizeof(*cpi->ssim_rdmult_scaling_factors)));
2779 }
2780
Yue Chenc9b23e02019-04-10 16:54:03 -07002781 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chen7cae98f2018-08-24 10:43:16 -07002782 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2783 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2784
2785 CHECK_MEM_ERROR(cm, cpi->tpl_stats[frame].tpl_stats_ptr,
2786 aom_calloc(mi_rows * mi_cols,
2787 sizeof(*cpi->tpl_stats[frame].tpl_stats_ptr)));
2788 cpi->tpl_stats[frame].is_valid = 0;
2789 cpi->tpl_stats[frame].width = mi_cols;
2790 cpi->tpl_stats[frame].height = mi_rows;
2791 cpi->tpl_stats[frame].stride = mi_cols;
2792 cpi->tpl_stats[frame].mi_rows = cm->mi_rows;
2793 cpi->tpl_stats[frame].mi_cols = cm->mi_cols;
2794 }
2795
chiyotsai9c484b32019-03-07 16:01:50 -08002796#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08002797 av1_zero(cpi->partition_stats);
2798#endif
2799
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002800#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2801 cpi->fn_ptr[BT].sdf = SDF; \
2802 cpi->fn_ptr[BT].sdaf = SDAF; \
2803 cpi->fn_ptr[BT].vf = VF; \
2804 cpi->fn_ptr[BT].svf = SVF; \
2805 cpi->fn_ptr[BT].svaf = SVAF; \
2806 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2807 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002808 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002809
Cheng Chenf78632e2017-10-20 15:30:51 -07002810 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002811 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002812 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
2813 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002814
2815 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002816 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002817 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
2818 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002819
2820 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002821 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002822 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
2823 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002824
2825 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002826 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002827 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
2828 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002829
2830 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002831 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002832 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
2833 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002834
2835 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002836 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002837 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
2838 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002839
Cheng Chenf78632e2017-10-20 15:30:51 -07002840 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2841 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002842 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
2843 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002844
2845 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002846 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002847 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
2848 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002849
2850 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002851 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002852 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
2853 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002854
2855 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002856 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002857 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
2858 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002859
2860 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002861 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002862 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
2863 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002864
2865 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002866 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002867 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
2868 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002869
2870 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002871 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002872 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
2873 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002874
2875 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2876 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002877 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
2878 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002879
2880 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2881 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002882 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
2883 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002884
2885 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2886 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002887 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
2888 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002889
2890 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002891 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002892 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
2893 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002894
2895 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002896 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002897 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
2898 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002899
2900 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002901 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002902 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002903
2904 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002905 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002906 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002907
2908 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002909 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002910 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002911
2912 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002913 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002914 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002915
Yaowu Xuc27fc142016-08-22 16:08:15 -07002916#define OBFP(BT, OSDF, OVF, OSVF) \
2917 cpi->fn_ptr[BT].osdf = OSDF; \
2918 cpi->fn_ptr[BT].ovf = OVF; \
2919 cpi->fn_ptr[BT].osvf = OSVF;
2920
Yaowu Xuf883b422016-08-30 14:01:10 -07002921 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2922 aom_obmc_sub_pixel_variance128x128)
2923 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2924 aom_obmc_sub_pixel_variance128x64)
2925 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2926 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002927 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2928 aom_obmc_sub_pixel_variance64x64)
2929 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2930 aom_obmc_sub_pixel_variance64x32)
2931 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2932 aom_obmc_sub_pixel_variance32x64)
2933 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2934 aom_obmc_sub_pixel_variance32x32)
2935 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2936 aom_obmc_sub_pixel_variance32x16)
2937 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2938 aom_obmc_sub_pixel_variance16x32)
2939 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2940 aom_obmc_sub_pixel_variance16x16)
2941 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2942 aom_obmc_sub_pixel_variance16x8)
2943 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2944 aom_obmc_sub_pixel_variance8x16)
2945 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2946 aom_obmc_sub_pixel_variance8x8)
2947 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2948 aom_obmc_sub_pixel_variance4x8)
2949 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2950 aom_obmc_sub_pixel_variance8x4)
2951 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2952 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002953 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2954 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002955 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2956 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002957 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2958 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002959 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2960 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002961 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2962 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002963 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2964 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002965
David Barkerf19f35f2017-05-22 16:33:22 +01002966#define MBFP(BT, MCSDF, MCSVF) \
2967 cpi->fn_ptr[BT].msdf = MCSDF; \
2968 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969
David Barkerf19f35f2017-05-22 16:33:22 +01002970 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2971 aom_masked_sub_pixel_variance128x128)
2972 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2973 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002974 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2975 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2976 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2977 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2978 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2979 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2980 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2981 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2982 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2983 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2984 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2985 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2986 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002987
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002988 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2989
2990 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2991
2992 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2993
2994 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002995
2996 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2997
2998 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002999
Yaowu Xuc27fc142016-08-22 16:08:15 -07003000 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003001
Yaowu Xuf883b422016-08-30 14:01:10 -07003002 /* av1_init_quantizer() is first called here. Add check in
3003 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003004 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003005 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003006 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003007 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08003008 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003009
Yaowu Xuf883b422016-08-30 14:01:10 -07003010 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003011 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003012 cm->superres_upscaled_width = oxcf->width;
3013 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003014 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003015
3016 cm->error.setjmp = 0;
3017
3018 return cpi;
3019}
3020
Urvang Joshiee2c8112018-05-04 14:53:15 -07003021#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003022#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3023
3024#define SNPRINT2(H, T, V) \
3025 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003026#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003027
Yaowu Xuf883b422016-08-30 14:01:10 -07003028void av1_remove_compressor(AV1_COMP *cpi) {
3029 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003030 unsigned int i;
3031 int t;
3032
3033 if (!cpi) return;
3034
3035 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003036 const int num_planes = av1_num_planes(cm);
3037
David Turnerd2a592e2018-11-16 14:59:31 +00003038 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003039#if CONFIG_ENTROPY_STATS
3040 if (cpi->oxcf.pass != 1) {
3041 fprintf(stderr, "Writing counts.stt\n");
3042 FILE *f = fopen("counts.stt", "wb");
3043 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3044 fclose(f);
3045 }
3046#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003047#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003048 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003049
3050 if (cpi->oxcf.pass != 1) {
3051 char headings[512] = { 0 };
3052 char results[512] = { 0 };
3053 FILE *f = fopen("opsnr.stt", "a");
3054 double time_encoded =
3055 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3056 10000000.000;
3057 double total_encode_time =
3058 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3059 const double dr =
3060 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3061 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3062 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3063 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3064
3065 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003066 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003067 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3068 const double total_ssim =
3069 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3070 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003071 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003072 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003073 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08003074 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003075 snprintf(results, sizeof(results),
3076 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3077 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003078 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08003079 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003080 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3081 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3082 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3083 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003084 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003085 cpi->psnr.stat[STAT_Y] / cpi->count,
3086 cpi->psnr.stat[STAT_U] / cpi->count,
3087 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003088
3089 if (cpi->b_calculate_blockiness) {
3090 SNPRINT(headings, "\t Block\tWstBlck");
3091 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3092 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3093 }
3094
3095 if (cpi->b_calculate_consistency) {
3096 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003097 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003098 (double)cpi->total_inconsistency);
3099
3100 SNPRINT(headings, "\tConsist\tWstCons");
3101 SNPRINT2(results, "\t%7.3f", consistency);
3102 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3103 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003104 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3105 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003106 rate_err, fabs(rate_err));
3107 }
3108
3109 fclose(f);
3110 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003111#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003112#if CONFIG_SPEED_STATS
3113 if (cpi->oxcf.pass != 1) {
3114 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3115 }
3116#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003117
chiyotsai9c484b32019-03-07 16:01:50 -08003118#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003119 if (cpi->oxcf.pass != 1) {
3120 av1_print_partition_stats(&cpi->partition_stats);
3121 }
3122#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003123 }
3124
Yue Chenc9b23e02019-04-10 16:54:03 -07003125 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chen7cae98f2018-08-24 10:43:16 -07003126 aom_free(cpi->tpl_stats[frame].tpl_stats_ptr);
3127 cpi->tpl_stats[frame].is_valid = 0;
3128 }
3129
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303130 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003131 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003132 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3133
3134 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003135 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003136
3137 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303138 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303139 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003140 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003141 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003142 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003143 for (int j = 0; j < 2; ++j) {
3144 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3145 }
Jingning Hand064cf02017-06-01 10:00:39 -07003146 aom_free(thread_data->td->above_pred_buf);
3147 aom_free(thread_data->td->left_pred_buf);
3148 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003149
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303150 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003151 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303152 for (int y = 0; y < 2; y++) {
3153 aom_free(thread_data->td->hash_value_buffer[x][y]);
3154 thread_data->td->hash_value_buffer[x][y] = NULL;
3155 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003156 }
Jingning Hand064cf02017-06-01 10:00:39 -07003157 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003158 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003159 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003160 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003161 }
3162 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303163#if CONFIG_MULTITHREAD
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303164 if (cpi->row_mt_mutex_ != NULL) {
3165 pthread_mutex_destroy(cpi->row_mt_mutex_);
3166 aom_free(cpi->row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303167 }
3168#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303169 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003170 aom_free(cpi->tile_thr_data);
3171 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003172
Deepa K G964e72e2018-05-16 16:56:01 +05303173 if (cpi->num_workers > 1) {
3174 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303175 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303176 }
3177
Yaowu Xuc27fc142016-08-22 16:08:15 -07003178 dealloc_compressor_data(cpi);
3179
3180 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3181 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003182 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003183 }
3184
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003185#if CONFIG_INTERNAL_STATS
3186 aom_free(cpi->ssim_vars);
3187 cpi->ssim_vars = NULL;
3188#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003189
Yaowu Xuf883b422016-08-30 14:01:10 -07003190 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003191 for (i = 0; i < FRAME_BUFFERS; ++i) {
3192 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3193 }
Yaowu Xu74e63352019-05-06 09:21:33 -07003194#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003195 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003196#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003197 av1_free_ref_frame_buffers(cm->buffer_pool);
3198 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003199
3200#ifdef OUTPUT_YUV_SKINMAP
3201 fclose(yuv_skinmap_file);
3202#endif
3203#ifdef OUTPUT_YUV_REC
3204 fclose(yuv_rec_file);
3205#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003206}
3207
Yaowu Xuf883b422016-08-30 14:01:10 -07003208static void generate_psnr_packet(AV1_COMP *cpi) {
3209 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003210 int i;
3211 PSNR_STATS psnr;
David Turnerc29e1a92018-12-06 14:10:14 +00003212 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003213 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003214
3215 for (i = 0; i < 4; ++i) {
3216 pkt.data.psnr.samples[i] = psnr.samples[i];
3217 pkt.data.psnr.sse[i] = psnr.sse[i];
3218 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3219 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003220 pkt.kind = AOM_CODEC_PSNR_PKT;
3221 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003222}
3223
Yaowu Xuf883b422016-08-30 14:01:10 -07003224int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003225 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3226
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003227 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003228 return 0;
3229}
3230
Thomas Daede497d1952017-08-08 17:33:06 -07003231int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3232 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003233 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003234 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003235 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003236 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003237 return 0;
3238 } else {
3239 return -1;
3240 }
3241}
3242
Thomas Daede497d1952017-08-08 17:33:06 -07003243int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3244 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003245 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003246 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003247 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003248 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003249 return 0;
3250 } else {
3251 return -1;
3252 }
3253}
3254
3255int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003256 cpi->ext_refresh_frame_context = update;
3257 cpi->ext_refresh_frame_context_pending = 1;
3258 return 0;
3259}
3260
3261#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3262// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3263// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3264// not denoise the UV channels at this time. If ever we implement UV channel
3265// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003266void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003267 uint8_t *src = s->y_buffer;
3268 int h = s->y_height;
3269
3270 do {
3271 fwrite(src, s->y_width, 1, f);
3272 src += s->y_stride;
3273 } while (--h);
3274
3275 src = s->u_buffer;
3276 h = s->uv_height;
3277
3278 do {
3279 fwrite(src, s->uv_width, 1, f);
3280 src += s->uv_stride;
3281 } while (--h);
3282
3283 src = s->v_buffer;
3284 h = s->uv_height;
3285
3286 do {
3287 fwrite(src, s->uv_width, 1, f);
3288 src += s->uv_stride;
3289 } while (--h);
3290}
3291#endif
3292
Yaowu Xuc27fc142016-08-22 16:08:15 -07003293#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003294void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003295 uint8_t *src = s->y_buffer;
3296 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003297 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003298 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3299 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3300
3301 do {
3302 fwrite(src16, s->y_width, 2, yuv_rec_file);
3303 src16 += s->y_stride;
3304 } while (--h);
3305
3306 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3307 h = s->uv_height;
3308
3309 do {
3310 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3311 src16 += s->uv_stride;
3312 } while (--h);
3313
3314 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3315 h = s->uv_height;
3316
3317 do {
3318 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3319 src16 += s->uv_stride;
3320 } while (--h);
3321
3322 fflush(yuv_rec_file);
3323 return;
3324 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003325
3326 do {
3327 fwrite(src, s->y_width, 1, yuv_rec_file);
3328 src += s->y_stride;
3329 } while (--h);
3330
3331 src = s->u_buffer;
3332 h = s->uv_height;
3333
3334 do {
3335 fwrite(src, s->uv_width, 1, yuv_rec_file);
3336 src += s->uv_stride;
3337 } while (--h);
3338
3339 src = s->v_buffer;
3340 h = s->uv_height;
3341
3342 do {
3343 fwrite(src, s->uv_width, 1, yuv_rec_file);
3344 src += s->uv_stride;
3345 } while (--h);
3346
3347 fflush(yuv_rec_file);
3348}
3349#endif // OUTPUT_YUV_REC
3350
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003351#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003352static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3353 int i;
3354 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003355 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003356 AV1_COMMON *const cm = &cpi->common;
3357 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3358 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003359 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003360 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003361 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003362 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003363 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003364 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003365 // TODO(sarahparker): The earlier condition for recoding here was:
3366 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3367 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003368 }
3369 }
3370 return recode;
3371}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003372
Yaowu Xuc27fc142016-08-22 16:08:15 -07003373// Function to test for conditions that indicate we should loop
3374// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003375static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3376 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003377 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003378 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003379 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3380 int force_recode = 0;
3381
3382 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3383 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3384 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003385 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3386 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3387 (rc->projected_frame_size < low_limit && q > minq)) {
3388 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003389 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003390 // Deal with frame undershoot and whether or not we are
3391 // below the automatically set cq level.
3392 if (q > oxcf->cq_level &&
3393 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3394 force_recode = 1;
3395 }
3396 }
3397 }
3398 return force_recode;
3399}
3400
Cheng Chen46f30c72017-09-07 11:13:33 -07003401static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003402 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003403 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003404 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003405 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003406 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3407 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003408 };
3409
3410 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003411 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003412 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3413 BufferPool *const pool = cm->buffer_pool;
3414 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003415 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416
3417 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003418 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003419 continue;
3420 }
3421
Yaowu Xuc27fc142016-08-22 16:08:15 -07003422 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003423 // Replace the reference buffer with a copy having a thicker border,
3424 // if the reference buffer is higher resolution than the current
3425 // frame, and the border is thin.
3426 if ((ref->y_crop_width > cm->width ||
3427 ref->y_crop_height > cm->height) &&
3428 ref->border < AOM_BORDER_IN_PIXELS) {
3429 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3430 if (aom_yv12_realloc_with_new_border(
3431 &ref_fb->buf, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3432 num_planes) != 0) {
3433 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3434 "Failed to allocate frame buffer");
3435 }
3436 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003437 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003438 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3439 if (new_fb == NULL) {
3440 const int new_fb_idx = get_free_fb(cm);
3441 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003442 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3443 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003444 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003445 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003446 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003447 }
David Turnere7ebf902018-12-04 14:04:55 +00003448
3449 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3450 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003451 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003452 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003453 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003454 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003455 cm->byte_alignment, NULL, NULL, NULL)) {
3456 if (force_scaling) {
3457 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003458 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003459 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003460 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003461 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003462 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003463 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003464 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3465 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003466 alloc_frame_mvs(cm, new_fb);
3467 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003468 } else {
David Turnera21966b2018-12-05 14:48:49 +00003469 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003470 buf->buf.y_crop_width = ref->y_crop_width;
3471 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003472 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003473 ++buf->ref_count;
3474 }
3475 } else {
David Turnere7ebf902018-12-04 14:04:55 +00003476 if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477 }
3478 }
3479}
3480
Yaowu Xuf883b422016-08-30 14:01:10 -07003481static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003482 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003483 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3484 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3485 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003486 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003487 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003488 }
3489 }
3490}
3491
Yaowu Xuf883b422016-08-30 14:01:10 -07003492static void set_mv_search_params(AV1_COMP *cpi) {
3493 const AV1_COMMON *const cm = &cpi->common;
3494 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003495
3496 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003497 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003498
3499 if (cpi->sf.mv.auto_mv_step_size) {
3500 if (frame_is_intra_only(cm)) {
3501 // Initialize max_mv_magnitude for use in the first INTER frame
3502 // after a key/intra-only frame.
3503 cpi->max_mv_magnitude = max_mv_def;
3504 } else {
3505 if (cm->show_frame) {
3506 // Allow mv_steps to correspond to twice the max mv magnitude found
3507 // in the previous frame, capped by the default max_mv_magnitude based
3508 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003509 cpi->mv_step_param = av1_init_search_range(
3510 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003511 }
3512 cpi->max_mv_magnitude = 0;
3513 }
3514 }
3515}
3516
Hui Subdf0c992019-02-14 14:52:41 -08003517static void set_screen_content_options(AV1_COMP *cpi) {
3518 AV1_COMMON *cm = &cpi->common;
3519
3520 if (cm->seq_params.force_screen_content_tools != 2) {
3521 cm->allow_screen_content_tools = cm->allow_intrabc =
3522 cm->seq_params.force_screen_content_tools;
3523 return;
3524 }
3525
3526 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3527 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3528 return;
3529 }
3530
3531 // Estimate if the source frame is screen content, based on the portion of
3532 // blocks that have few luma colors.
3533 const uint8_t *src = cpi->source->y_buffer;
3534 assert(src != NULL);
3535 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
3536 const int stride = cpi->source->y_stride;
3537 const int width = cpi->source->y_width;
3538 const int height = cpi->source->y_height;
3539 const int bd = cm->seq_params.bit_depth;
3540 const int blk_w = 16;
3541 const int blk_h = 16;
3542 // These threshold values are selected experimentally.
3543 const int color_thresh = 4;
3544 const unsigned int var_thresh = 0;
3545 // Counts of blocks with no more than color_thresh colors.
3546 int counts_1 = 0;
3547 // Counts of blocks with no more than color_thresh colors and variance larger
3548 // than var_thresh.
3549 int counts_2 = 0;
3550
3551 for (int r = 0; r + blk_h <= height; r += blk_h) {
3552 for (int c = 0; c + blk_w <= width; c += blk_w) {
3553 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3554 const uint8_t *const this_src = src + r * stride + c;
3555 const int n_colors =
3556 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3557 count_buf)
3558 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3559 if (n_colors > 1 && n_colors <= color_thresh) {
3560 ++counts_1;
3561 struct buf_2d buf;
3562 buf.stride = stride;
3563 buf.buf = (uint8_t *)this_src;
3564 const unsigned int var =
3565 use_hbd
3566 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3567 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3568 if (var > var_thresh) ++counts_2;
3569 }
3570 }
3571 }
3572
3573 // The threshold values are selected experimentally.
3574 cm->allow_screen_content_tools =
3575 counts_1 * blk_h * blk_w * 10 > width * height;
3576 // IntraBC would force loop filters off, so we use more strict rules that also
3577 // requires that the block has high variance.
3578 cm->allow_intrabc = cm->allow_screen_content_tools &&
Hui Su64a2ffb2019-04-11 16:47:13 -07003579 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08003580}
3581
Yaowu Xuf883b422016-08-30 14:01:10 -07003582static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003583 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003584 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003585 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003586 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003587 }
3588 cpi->global_motion_search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07003589
3590 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
3591 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
3592
David Turner04b70d82019-01-24 15:39:19 +00003593 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07003594 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003595 cm->interp_filter = SWITCHABLE;
3596 cm->switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003597}
3598
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003599static int get_gfu_boost_from_r0(double r0) {
Debargha Mukherjee41d03ee2019-06-06 12:23:08 -07003600 int boost = (int)rint(260.0 / r0);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003601 return boost;
3602}
3603
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003604static int get_kf_boost_from_r0(double r0, int frames_to_key) {
3605 double factor = sqrt((double)frames_to_key);
3606 factor = AOMMIN(factor, 10.0);
3607 factor = AOMMAX(factor, 4.0);
3608 int boost = (int)rint((75.0 + 14.0 * factor) / r0);
3609 return boost;
3610}
3611
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003612static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07003613 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003614 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003615
Sarah Parkere1b22012019-06-06 16:35:25 -07003616 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
3617 const int tpl_idx = gf_group->frame_disp_idx[gf_group->index];
Yue Chen4e585cc2019-06-03 14:47:16 -07003618 TplDepFrame *tpl_frame = &cpi->tpl_stats[tpl_idx];
3619 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003620
Yue Chen4e585cc2019-06-03 14:47:16 -07003621 if (tpl_frame->is_valid) {
3622 int tpl_stride = tpl_frame->stride;
3623 int64_t intra_cost_base = 0;
3624 int64_t mc_dep_cost_base = 0;
3625 int64_t mc_saved_base = 0;
3626 int64_t mc_count_base = 0;
3627 int row, col;
3628
3629 for (row = 0; row < cm->mi_rows; ++row) {
3630 for (col = 0; col < cm->mi_cols; ++col) {
3631 TplDepStats *this_stats = &tpl_stats[row * tpl_stride + col];
3632 intra_cost_base += this_stats->intra_cost;
3633 mc_dep_cost_base += this_stats->intra_cost + this_stats->mc_flow;
3634 mc_count_base += this_stats->mc_count;
3635 mc_saved_base += this_stats->mc_saved;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003636 }
Yue Chen4e585cc2019-06-03 14:47:16 -07003637 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003638
Yue Chen4e585cc2019-06-03 14:47:16 -07003639 if (mc_dep_cost_base == 0) {
3640 tpl_frame->is_valid = 0;
3641 } else {
3642 aom_clear_system_state();
3643 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
3644 if (is_frame_arf_and_tpl_eligible(cpi)) {
3645 cpi->rd.arf_r0 = cpi->rd.r0;
3646 const int gfu_boost = get_gfu_boost_from_r0(cpi->rd.arf_r0);
3647 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003648 // gfu_boost);
Yue Chen4e585cc2019-06-03 14:47:16 -07003649 cpi->rc.gfu_boost = (cpi->rc.gfu_boost + gfu_boost) / 2;
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003650 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003651 // TODO(debargha): Turn off q adjustment for kf temporarily to
3652 // reduce impact on speed of encoding. Need to investigate how
3653 // to mitigate the issue.
3654 if (cpi->oxcf.rc_mode == AOM_Q) {
3655 const int kf_boost =
3656 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
3657 // printf("old kf boost %d new kf boost %d [%d]\n", cpi->rc.kf_boost,
3658 // kf_boost, cpi->rc.frames_to_key);
3659 cpi->rc.kf_boost = (cpi->rc.kf_boost + kf_boost) / 2;
3660 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003661 }
Yue Chen4e585cc2019-06-03 14:47:16 -07003662 cpi->rd.mc_count_base =
3663 (double)mc_count_base / (cm->mi_rows * cm->mi_cols);
3664 cpi->rd.mc_saved_base =
3665 (double)mc_saved_base / (cm->mi_rows * cm->mi_cols);
3666 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003667 }
3668 }
3669}
3670
Yaowu Xuf883b422016-08-30 14:01:10 -07003671static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003672 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003673 AV1_COMMON *const cm = &cpi->common;
3674 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003675
3676 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00003677 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003678
Yue Chen4e585cc2019-06-03 14:47:16 -07003679 if (cpi->oxcf.enable_tpl_model && cpi->tpl_model_pass == 0 &&
3680 is_frame_tpl_eligible(cpi))
3681 process_tpl_stats_frame(cpi);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003682
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003683 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003684 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3685 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003686
James Zern01a9d702017-08-25 19:09:33 +00003687 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003688 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003689 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003690 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003691
3692 // Configure experimental use of segmentation for enhanced coding of
3693 // static regions if indicated.
3694 // Only allowed in the second pass of a two pass encode, as it requires
3695 // lagged coding, and if the relevant speed feature flag is set.
3696 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3697 configure_static_seg_features(cpi);
3698}
3699
Yaowu Xuf883b422016-08-30 14:01:10 -07003700static void init_motion_estimation(AV1_COMP *cpi) {
chiyotsai836b69b2019-04-09 13:41:24 -07003701 const int y_stride = cpi->scaled_source.y_stride;
3702 const int y_stride_src = (cpi->oxcf.resize_mode || cpi->oxcf.superres_mode)
3703 ? y_stride
3704 : cpi->lookahead->buf->img.y_stride;
3705 // Update if ss_cfg is uninitialized or the current frame has a new stride
3706 const int should_update = !cpi->ss_cfg[SS_CFG_SRC].stride ||
3707 !cpi->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
3708 (y_stride != cpi->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003709
chiyotsai836b69b2019-04-09 13:41:24 -07003710 if (!should_update) {
3711 return;
3712 }
3713
3714 if (cpi->sf.mv.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303715 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3716 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
3717 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07003718 } else {
3719 // Update the offsets in search_sites as y_stride can change due to scaled
3720 // references. This update allows NSTEP to be used on scaled references as
3721 // long as sf.mv.search_method is not DIAMOND. Currently in the codebae,
3722 // sf.mv.search_method is never set to DIAMOND.
3723 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3724 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003725 }
3726}
3727
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003728#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003729static void set_restoration_unit_size(int width, int height, int sx, int sy,
3730 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003731 (void)width;
3732 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003733 (void)sx;
3734 (void)sy;
3735#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3736 int s = AOMMIN(sx, sy);
3737#else
3738 int s = 0;
3739#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3740
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003741 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003742 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003743 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003744 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003745 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3746 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003747}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003748
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303749static void init_ref_frame_bufs(AV1_COMP *cpi) {
3750 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07003751 int i;
3752 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00003753 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07003754 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00003755 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07003756 }
3757 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07003758 pool->frame_bufs[i].ref_count = 0;
3759 }
RogerZhou86902d02018-02-28 15:29:16 -08003760 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003761 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303762 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08003763 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003764 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003765}
3766
Yaowu Xud3e7c682017-12-21 14:08:25 -08003767static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003768 int subsampling_x, int subsampling_y) {
3769 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003770 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07003771
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003772 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3773 seq_params->subsampling_x != subsampling_x ||
3774 seq_params->subsampling_y != subsampling_y) {
3775 seq_params->subsampling_x = subsampling_x;
3776 seq_params->subsampling_y = subsampling_y;
3777 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003778
3779 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303780 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07003781 alloc_util_frame_buffers(cpi);
3782
3783 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3784
3785 cpi->initial_width = cm->width;
3786 cpi->initial_height = cm->height;
3787 cpi->initial_mbs = cm->MBs;
3788 }
3789}
3790
3791// Returns 1 if the assigned width or height was <= 0.
3792static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3793 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003794 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003795 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3796 cm->seq_params.subsampling_x,
3797 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003798
3799 if (width <= 0 || height <= 0) return 1;
3800
3801 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003802 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003803
3804 if (cpi->initial_width && cpi->initial_height &&
3805 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3806 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003807 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003808 alloc_compressor_data(cpi);
3809 realloc_segmentation_maps(cpi);
3810 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003811 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003812 update_frame_size(cpi);
3813
3814 return 0;
3815}
3816
David Turner475a3132019-01-18 15:17:17 +00003817void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003818 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003819 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003820 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003821 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003822 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003823
Fergus Simpsonbc189932017-05-16 17:02:39 -07003824 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003825 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003826 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003827 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003828 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07003829 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003830 }
3831
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003832 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003833 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003834 }
3835
David Turnere7ebf902018-12-04 14:04:55 +00003836 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003837
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303838 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05303839 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3840 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303841 cm->num_allocated_above_contexts < cm->tile_rows) {
3842 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3843 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3844 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3845 "Failed to allocate context buffers");
3846 }
3847
Yaowu Xuc27fc142016-08-22 16:08:15 -07003848 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003849 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00003850 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
3851 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05303852 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003853 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003854 "Failed to allocate frame buffer");
3855
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003856 const int frame_width = cm->superres_upscaled_width;
3857 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003858 set_restoration_unit_size(frame_width, frame_height,
3859 seq_params->subsampling_x,
3860 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003861 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003862 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003863
3864 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003865 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003866 init_motion_estimation(cpi);
3867
3868 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00003869 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00003870 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00003871 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
3872 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00003873 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003874 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00003875 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003876 }
3877 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003878
Hui Su5ebd8702018-01-08 18:09:20 -08003879 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003880 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003881
3882 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3883}
3884
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003885static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3886 // Choose an arbitrary random number
3887 static unsigned int seed = 56789;
3888 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003889 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3890 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003891
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07003892 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003893 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003894 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003895 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003896 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003897 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003898 else
Urvang Joshide71d142017-10-05 12:12:15 -07003899 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003900 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003901 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003902 default: assert(0);
3903 }
Urvang Joshide71d142017-10-05 12:12:15 -07003904 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003905}
3906
Debargha Mukherjeee3cd5a52018-11-29 11:05:22 -08003907#define ENERGY_BY_Q2_THRESH 0.01
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003908#define ENERGY_BY_AC_THRESH 0.2
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003909
3910static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003911 double threshq,
3912 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003913 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003914 const double tq = threshq * q * q;
3915 const double tp = threshp * energy[1];
3916 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003917 int k;
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003918 for (k = 16; k > 8; --k) {
3919 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003920 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003921 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003922}
3923
3924static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003925 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003926 analyze_hor_freq(cpi, energy);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003927 /*
3928 printf("\nenergy = [");
3929 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
3930 printf("]\n");
3931 */
3932 return get_superres_denom_from_qindex_energy(
3933 qindex, energy, ENERGY_BY_Q2_THRESH, ENERGY_BY_AC_THRESH);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08003934}
3935
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003936static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
3937 // Choose an arbitrary random number
3938 static unsigned int seed = 34567;
3939 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003940 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3941 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07003942
3943 // Make sure that superres mode of the frame is consistent with the
3944 // sequence-level flag.
3945 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
3946 cpi->common.seq_params.enable_superres));
3947 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
3948 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003949
3950 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003951 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003952 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003953 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003954 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003955 else
Urvang Joshide71d142017-10-05 12:12:15 -07003956 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003957 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003958 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003959 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003960 // Do not use superres when screen content tools are used.
3961 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08003962 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
3963 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi2c92b072018-03-19 17:23:31 -07003964 int bottom_index, top_index;
3965 const int q = av1_rc_pick_q_and_bounds(
3966 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08003967
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003968 const int qthresh = (frame_is_intra_only(&cpi->common))
3969 ? oxcf->superres_kf_qthresh
3970 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08003971 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07003972 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003973 } else {
Debargha Mukherjeee3cd5a52018-11-29 11:05:22 -08003974 new_denom = get_superres_denom_for_qindex(cpi, q);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003975 }
3976 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003977 }
Urvang Joshi36a83732019-01-31 15:31:57 -08003978 case SUPERRES_AUTO: {
3979 // Don't use when screen content tools are used.
3980 if (cpi->common.allow_screen_content_tools) break;
3981 // Don't use for inter frames.
3982 if (!frame_is_intra_only(&cpi->common)) break;
Urvang Joshibf5e7702019-04-01 11:55:09 -07003983 // Don't use for keyframes that can be used as references, except when
3984 // using AOM_Q mode.
3985 if (cpi->rc.frames_to_key != 1 && cpi->oxcf.rc_mode != AOM_Q) break;
Urvang Joshi36a83732019-01-31 15:31:57 -08003986
3987 // Now decide the use of superres based on 'q'.
3988 int bottom_index, top_index;
3989 const int q = av1_rc_pick_q_and_bounds(
3990 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
3991
3992 const int qthresh = 128;
3993 if (q <= qthresh) {
3994 new_denom = SCALE_NUMERATOR;
3995 } else {
3996 new_denom = get_superres_denom_for_qindex(cpi, q);
3997 }
3998 break;
3999 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004000 default: assert(0);
4001 }
Urvang Joshide71d142017-10-05 12:12:15 -07004002 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004003}
4004
Urvang Joshide71d142017-10-05 12:12:15 -07004005static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4006 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4007}
4008
4009static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004010 // Only need to check the width, as scaling is horizontal only.
4011 (void)oheight;
4012 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004013}
4014
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004015static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004016 SUPERRES_MODE superres_mode, int owidth,
4017 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004018 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004019 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004020 }
4021
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004022 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004023 int resize_denom =
4024 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4025 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4026
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004027 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004028 // Alter superres scale as needed to enforce conformity.
4029 rsz->superres_denom =
4030 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4031 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4032 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004033 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004034 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004035 // Alter resize scale as needed to enforce conformity.
4036 resize_denom =
4037 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004038 rsz->resize_width = owidth;
4039 rsz->resize_height = oheight;
4040 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004041 resize_denom);
4042 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4043 if (resize_denom > SCALE_NUMERATOR) {
4044 --resize_denom;
4045 rsz->resize_width = owidth;
4046 rsz->resize_height = oheight;
4047 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4048 resize_denom);
4049 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004050 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004051 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004052 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004053 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004054 if (resize_denom > rsz->superres_denom)
4055 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004056 else
Urvang Joshide71d142017-10-05 12:12:15 -07004057 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004058 rsz->resize_width = owidth;
4059 rsz->resize_height = oheight;
4060 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004061 resize_denom);
4062 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4063 (resize_denom > SCALE_NUMERATOR ||
4064 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004065 } else { // We are allowed to alter neither resize scale nor superres
4066 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004067 return 0;
4068 }
Urvang Joshide71d142017-10-05 12:12:15 -07004069 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004070}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004071
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004072// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004073static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004074 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004075 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshide71d142017-10-05 12:12:15 -07004076 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004077 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004078 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4079 rsz.resize_width = cpi->resize_pending_width;
4080 rsz.resize_height = cpi->resize_pending_height;
4081 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4082 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004083 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004084 rsz.resize_width = cpi->oxcf.width;
4085 rsz.resize_height = cpi->oxcf.height;
4086 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004087 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004088 }
Urvang Joshide71d142017-10-05 12:12:15 -07004089 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004090 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4091 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004092 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004093 return rsz;
4094}
4095
Urvang Joshi22b150b2019-01-10 14:32:32 -08004096static void setup_frame_size_from_params(AV1_COMP *cpi,
4097 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004098 int encode_width = rsz->resize_width;
4099 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004100
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004101 AV1_COMMON *cm = &cpi->common;
4102 cm->superres_upscaled_width = encode_width;
4103 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004104 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004105 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4106 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004107 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004108}
4109
David Turnerdedd8ff2019-01-23 13:59:46 +00004110void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004111 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004112 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004113 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004114 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004115 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004116
Yaowu Xu7e450882019-04-30 15:09:18 -07004117 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004118}
4119
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004120static void superres_post_encode(AV1_COMP *cpi) {
4121 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004122 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004123
Cheng Chen09c83a52018-06-05 12:27:36 -07004124 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004125
Urvang Joshid6b5d512018-03-20 13:34:38 -07004126 assert(cpi->oxcf.enable_superres);
4127 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004128 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004129
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004130 av1_superres_upscale(cm, NULL);
4131
4132 // If regular resizing is occurring the source will need to be downscaled to
4133 // match the upscaled superres resolution. Otherwise the original source is
4134 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004135 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004136 cpi->source = cpi->unscaled_source;
4137 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4138 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004139 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4140 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004141 // Do downscale. cm->(width|height) has been updated by
4142 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004143 if (aom_realloc_frame_buffer(
4144 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004145 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4146 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4147 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004148 aom_internal_error(
4149 &cm->error, AOM_CODEC_MEM_ERROR,
4150 "Failed to reallocate scaled source buffer for superres");
4151 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4152 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004153 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004154 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004155 cpi->source = &cpi->scaled_source;
4156 }
4157}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004158
4159static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004160 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004161 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004162
Urvang Joshic8b52d52018-03-23 13:16:51 -07004163 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4164 cm->coded_lossless && cm->all_lossless));
4165
logangwf95c9162019-02-20 12:02:32 -08004166 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4167 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4168 !cm->large_scale_tile;
4169 const int use_restoration = cm->seq_params.enable_restoration &&
4170 !cm->all_lossless && !cm->large_scale_tile;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004171
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004172 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004173
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004174#if CONFIG_COLLECT_COMPONENT_TIMING
4175 start_timing(cpi, loop_filter_time);
4176#endif
logangwf95c9162019-02-20 12:02:32 -08004177 if (use_loopfilter) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004178 aom_clear_system_state();
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004179 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
logangwf95c9162019-02-20 12:02:32 -08004180 } else {
4181 lf->filter_level[0] = 0;
4182 lf->filter_level[1] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004183 }
4184
Debargha Mukherjee2382b142018-02-26 14:31:32 -08004185 if (lf->filter_level[0] || lf->filter_level[1]) {
Deepa K G964e72e2018-05-16 16:56:01 +05304186 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004187 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
Yaowu Xu8e718cf2019-05-02 09:06:57 -07004188#if CONFIG_LPF_MASK
Cheng Chene3600cd2018-09-21 18:45:42 -07004189 0,
4190#endif
Deepa K G964e72e2018-05-16 16:56:01 +05304191 cpi->workers, cpi->num_workers,
4192 &cpi->lf_row_sync);
4193 else
David Turnerc29e1a92018-12-06 14:10:14 +00004194 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
Yaowu Xu8e718cf2019-05-02 09:06:57 -07004195#if CONFIG_LPF_MASK
Cheng Chen84b09932018-08-12 17:35:13 -07004196 0,
Cheng Chen8ab1f442018-04-27 18:01:52 -07004197#endif
Cheng Chen84b09932018-08-12 17:35:13 -07004198 0, num_planes, 0);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004199 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004200#if CONFIG_COLLECT_COMPONENT_TIMING
4201 end_timing(cpi, loop_filter_time);
4202#endif
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004203
logangwf95c9162019-02-20 12:02:32 -08004204 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004205 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004206
logangwf95c9162019-02-20 12:02:32 -08004207 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004208#if CONFIG_COLLECT_COMPONENT_TIMING
4209 start_timing(cpi, cdef_time);
4210#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004211 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004212 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
Hui Su805c7d22019-05-07 21:57:50 -07004213 cpi->sf.cdef_pick_method);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004214
4215 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004216 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004217#if CONFIG_COLLECT_COMPONENT_TIMING
4218 end_timing(cpi, cdef_time);
4219#endif
logangwf95c9162019-02-20 12:02:32 -08004220 } else {
4221 cm->cdef_info.cdef_bits = 0;
4222 cm->cdef_info.cdef_strengths[0] = 0;
4223 cm->cdef_info.nb_cdef_strengths = 1;
4224 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004225 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004226
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004227 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004228
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004229#if CONFIG_COLLECT_COMPONENT_TIMING
4230 start_timing(cpi, loop_restoration_time);
4231#endif
logangwf95c9162019-02-20 12:02:32 -08004232 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004233 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004234 av1_pick_filter_restoration(cpi->source, cpi);
4235 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4236 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4237 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304238 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004239 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304240 cpi->workers, cpi->num_workers,
4241 &cpi->lr_row_sync, &cpi->lr_ctxt);
4242 else
David Turnerc29e1a92018-12-06 14:10:14 +00004243 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304244 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004245 }
logangwf95c9162019-02-20 12:02:32 -08004246 } else {
4247 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4248 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4249 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004250 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004251#if CONFIG_COLLECT_COMPONENT_TIMING
4252 end_timing(cpi, loop_restoration_time);
4253#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004254}
4255
David Turnerf2b334c2018-12-13 13:00:55 +00004256static void fix_interp_filter(InterpFilter *const interp_filter,
4257 const FRAME_COUNTS *const counts) {
4258 if (*interp_filter == SWITCHABLE) {
4259 // Check to see if only one of the filters is actually used
4260 int count[SWITCHABLE_FILTERS] = { 0 };
4261 int num_filters_used = 0;
4262 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4263 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4264 count[i] += counts->switchable_interp[j][i];
4265 num_filters_used += (count[i] > 0);
4266 }
4267 if (num_filters_used == 1) {
4268 // Only one filter is used. So set the filter at frame level
4269 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4270 if (count[i]) {
4271 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4272 break;
4273 }
4274 }
4275 }
4276 }
4277}
4278
David Turner996b2c12018-12-07 15:52:30 +00004279static void finalize_encoded_frame(AV1_COMP *const cpi) {
4280 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004281 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004282
David Turner99e990e2018-12-10 12:54:26 +00004283 if (!cm->seq_params.reduced_still_picture_hdr &&
4284 encode_show_existing_frame(cm)) {
4285 RefCntBuffer *const frame_to_show =
4286 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4287
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004288 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004289 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4290 "Buffer does not contain a reconstructed frame");
4291 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004292 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004293 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004294 }
David Turner08f909c2018-12-18 13:29:14 +00004295
4296 if (!encode_show_existing_frame(cm) &&
4297 cm->seq_params.film_grain_params_present &&
4298 (cm->show_frame || cm->showable_frame)) {
4299 // Copy the current frame's film grain params to the its corresponding
4300 // RefCntBuffer slot.
4301 cm->cur_frame->film_grain_params = cm->film_grain_params;
4302
4303 // We must update the parameters if this is not an INTER_FRAME
4304 if (current_frame->frame_type != INTER_FRAME)
4305 cm->cur_frame->film_grain_params.update_parameters = 1;
4306
4307 // Iterate the random seed for the next frame.
4308 cm->film_grain_params.random_seed += 3381;
4309 if (cm->film_grain_params.random_seed == 0)
4310 cm->film_grain_params.random_seed = 7391;
4311 }
David Turnerf2b334c2018-12-13 13:00:55 +00004312
4313 // Initialise all tiles' contexts from the global frame context
4314 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4315 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4316 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4317 cpi->tile_data[tile_idx].tctx = *cm->fc;
4318 }
4319 }
4320
4321 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004322}
4323
Urvang Joshi57643372019-02-21 11:10:57 -08004324static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4325 int top_index, int bottom_index) {
4326 const AV1_COMMON *const cm = &cpi->common;
4327 const RATE_CONTROL *const rc = &cpi->rc;
4328
4329 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4330
4331 int q_regulated =
4332 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4333 AOMMAX(q_high, top_index), cm->width, cm->height);
4334
4335 int retries = 0;
4336 while (q_regulated < q_low && retries < 10) {
4337 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4338 q_regulated =
4339 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4340 AOMMAX(q_high, top_index), cm->width, cm->height);
4341 retries++;
4342 }
4343 return q_regulated;
4344}
4345
4346static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4347 int top_index, int bottom_index) {
4348 const AV1_COMMON *const cm = &cpi->common;
4349 const RATE_CONTROL *const rc = &cpi->rc;
4350
4351 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4352 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4353 top_index, cm->width, cm->height);
4354
4355 int retries = 0;
4356 while (q_regulated > q_high && retries < 10) {
4357 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4358 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4359 top_index, cm->width, cm->height);
4360 retries++;
4361 }
4362 return q_regulated;
4363}
4364
David Turner2f3b5df2019-01-02 14:30:50 +00004365// Called after encode_with_recode_loop() has just encoded a frame and packed
4366// its bitstream. This function works out whether we under- or over-shot
4367// our bitrate target and adjusts q as appropriate. Also decides whether
4368// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07004369static void recode_loop_update_q(
4370 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
4371 int *const q_high, const int top_index, const int bottom_index,
4372 int *const undershoot_seen, int *const overshoot_seen,
4373 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00004374 AV1_COMMON *const cm = &cpi->common;
4375 RATE_CONTROL *const rc = &cpi->rc;
4376
Hui Suef139e12019-05-20 15:51:22 -07004377 const int min_cr = cpi->oxcf.min_cr;
4378 if (min_cr > 0) {
4379 aom_clear_system_state();
4380 const double compression_ratio =
4381 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
4382 const double target_cr = min_cr / 100.0;
4383 if (compression_ratio < target_cr) {
4384 *low_cr_seen = 1;
4385 if (*q < rc->worst_quality) {
4386 const double cr_ratio = target_cr / compression_ratio;
4387 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
4388 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
4389 *q_low = AOMMAX(*q, *q_low);
4390 *q_high = AOMMAX(*q, *q_high);
4391 *loop = 1;
4392 }
4393 }
4394 if (*low_cr_seen) return;
4395 }
4396
4397 if (cpi->oxcf.rc_mode == AOM_Q) return;
4398
David Turner2f3b5df2019-01-02 14:30:50 +00004399 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4400 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4401 &frame_under_shoot_limit,
4402 &frame_over_shoot_limit);
4403 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4404
4405 if ((cm->current_frame.frame_type == KEY_FRAME) &&
4406 rc->this_key_frame_forced &&
4407 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4408 int last_q = *q;
4409 int64_t kf_err;
4410
4411 int64_t high_err_target = cpi->ambient_err;
4412 int64_t low_err_target = cpi->ambient_err >> 1;
4413
4414 if (cm->seq_params.use_highbitdepth) {
4415 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4416 } else {
4417 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4418 }
4419 // Prevent possible divide by zero error below for perfect KF
4420 kf_err += !kf_err;
4421
4422 // The key frame is not good enough or we can afford
4423 // to make it better without undue risk of popping.
4424 if ((kf_err > high_err_target &&
4425 rc->projected_frame_size <= frame_over_shoot_limit) ||
4426 (kf_err > low_err_target &&
4427 rc->projected_frame_size <= frame_under_shoot_limit)) {
4428 // Lower q_high
4429 *q_high = *q > *q_low ? *q - 1 : *q_low;
4430
4431 // Adjust Q
4432 *q = (int)((*q * high_err_target) / kf_err);
4433 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4434 } else if (kf_err < low_err_target &&
4435 rc->projected_frame_size >= frame_under_shoot_limit) {
4436 // The key frame is much better than the previous frame
4437 // Raise q_low
4438 *q_low = *q < *q_high ? *q + 1 : *q_high;
4439
4440 // Adjust Q
4441 *q = (int)((*q * low_err_target) / kf_err);
4442 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4443 }
4444
4445 // Clamp Q to upper and lower limits:
4446 *q = clamp(*q, *q_low, *q_high);
4447
4448 *loop = *q != last_q;
4449 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4450 frame_under_shoot_limit, *q,
4451 AOMMAX(*q_high, top_index), bottom_index)) {
4452 // Is the projected frame size out of range and are we allowed
4453 // to attempt to recode.
4454 int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004455
4456 // Frame size out of permitted range:
4457 // Update correction factor & compute new Q to try...
4458 // Frame is too large
4459 if (rc->projected_frame_size > rc->this_frame_target) {
4460 // Special case if the projected size is > the max allowed.
4461 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4462 *q_high = rc->worst_quality;
4463
4464 // Raise Qlow as to at least the current value
4465 *q_low = *q < *q_high ? *q + 1 : *q_high;
4466
Urvang Joshi57643372019-02-21 11:10:57 -08004467 if (*undershoot_seen || loop_at_this_size > 2 ||
4468 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004469 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4470
4471 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004472 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4473 const int q_mid = (*q_high + *q_low + 1) / 2;
4474 const int q_regulated = get_regulated_q_overshoot(
4475 cpi, *q_low, *q_high, top_index, bottom_index);
4476 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4477 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4478 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00004479 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004480 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
4481 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00004482 }
4483
4484 *overshoot_seen = 1;
4485 } else {
4486 // Frame is too small
4487 *q_high = *q > *q_low ? *q - 1 : *q_low;
4488
Urvang Joshi57643372019-02-21 11:10:57 -08004489 if (*overshoot_seen || loop_at_this_size > 2 ||
4490 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004491 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4492 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004493 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4494 const int q_mid = (*q_high + *q_low) / 2;
4495 const int q_regulated =
4496 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4497 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4498 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4499 *q = (q_mid + q_regulated) / 2;
4500
4501 // Special case reset for qlow for constrained quality.
4502 // This should only trigger where there is very substantial
4503 // undershoot on a frame and the auto cq level is above
4504 // the user passsed in value.
4505 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
4506 *q_low = *q;
4507 }
David Turner2f3b5df2019-01-02 14:30:50 +00004508 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004509 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4510
David Turner2f3b5df2019-01-02 14:30:50 +00004511 // Special case reset for qlow for constrained quality.
4512 // This should only trigger where there is very substantial
4513 // undershoot on a frame and the auto cq level is above
4514 // the user passsed in value.
4515 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
4516 *q_low = *q;
4517 }
David Turner2f3b5df2019-01-02 14:30:50 +00004518 }
4519
4520 *undershoot_seen = 1;
4521 }
4522
4523 // Clamp Q to upper and lower limits:
4524 *q = clamp(*q, *q_low, *q_high);
4525
4526 *loop = (*q != last_q);
4527 } else {
4528 *loop = 0;
4529 }
4530}
4531
Tom Finegane4099e32018-01-23 12:01:51 -08004532static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004533 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004534 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004535 const int allow_recode = (cpi->sf.recode_loop != DISALLOW_RECODE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004536
4537 set_size_independent_vars(cpi);
4538
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004539 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004540
David Turnerdedd8ff2019-01-23 13:59:46 +00004541 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004542
David Turner2f3b5df2019-01-02 14:30:50 +00004543 int top_index = 0, bottom_index = 0;
4544 int q = 0, q_low = 0, q_high = 0;
4545 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4546 q_low = bottom_index;
4547 q_high = top_index;
4548
4549 // Loop variables
4550 int loop_count = 0;
4551 int loop_at_this_size = 0;
4552 int loop = 0;
4553 int overshoot_seen = 0;
4554 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07004555 int low_cr_seen = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004556
4557#if CONFIG_COLLECT_COMPONENT_TIMING
4558 printf("\n Encoding a frame:");
4559#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004560 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004561 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004562 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004563
Urvang Joshif1fa6862018-01-08 16:39:33 -08004564 // if frame was scaled calculate global_motion_search again if already
4565 // done
David Turner2f3b5df2019-01-02 14:30:50 +00004566 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004567 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00004568 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004569 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00004570 }
4571 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004572 cpi->source =
4573 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004574 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004575 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4576 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004577 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004578
David Turner2f3b5df2019-01-02 14:30:50 +00004579 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004580 if (loop_count > 0) {
4581 release_scaled_references(cpi);
4582 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004583 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004584 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004585 av1_set_quantizer(cm, q);
Yue Chena7d80482019-01-31 09:48:09 -08004586 av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08004587
4588 av1_set_variance_partition_thresholds(cpi, q, 0);
4589
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004590 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
4591 // cm->current_frame.frame_number, cm->show_frame, q,
4592 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004593
David Turner2f3b5df2019-01-02 14:30:50 +00004594 if (loop_count == 0) {
4595 setup_frame(cpi);
4596 } else if (get_primary_ref_frame_buf(cm) == NULL) {
4597 // Base q-index may have changed, so we need to assign proper default coef
4598 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07004599 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004600 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004601 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004602
Yaowu Xuc27fc142016-08-22 16:08:15 -07004603 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004604 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004605 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004606 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00004607 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
4608 suppress_active_map(cpi);
4609 av1_cyclic_refresh_setup(cpi);
4610 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004611 }
David Turner2f3b5df2019-01-02 14:30:50 +00004612
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004613 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004614 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004615 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004616 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07004617 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004618 }
David Barkercab37552018-03-21 11:56:24 +00004619 } else {
4620 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004621 }
David Barkercab37552018-03-21 11:56:24 +00004622 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004623
David Turner2f3b5df2019-01-02 14:30:50 +00004624 if (allow_recode) save_coding_context(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004625#if CONFIG_COLLECT_COMPONENT_TIMING
4626 start_timing(cpi, av1_encode_frame_time);
4627#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004628 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004629 av1_encode_frame(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004630#if CONFIG_COLLECT_COMPONENT_TIMING
4631 end_timing(cpi, av1_encode_frame_time);
4632#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004633
Yaowu Xuf883b422016-08-30 14:01:10 -07004634 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004635
4636 // Dummy pack of the bitstream using up to date stats to get an
4637 // accurate estimate of output frame size to determine if we need
4638 // to recode.
4639 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004640 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004641
David Turner996b2c12018-12-07 15:52:30 +00004642 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00004643 int largest_tile_id = 0; // Output from bitstream: unused here
4644 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004645 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004646
4647 rc->projected_frame_size = (int)(*size) << 3;
4648 restore_coding_context(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004649 }
4650
Hui Suef139e12019-05-20 15:51:22 -07004651 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00004652 // Update q and decide whether to do a recode loop
4653 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
4654 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07004655 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004656 }
4657
4658 // Special case for overlay frame.
4659 if (rc->is_src_frame_alt_ref &&
4660 rc->projected_frame_size < rc->max_frame_bandwidth)
4661 loop = 0;
4662
David Turner2f3b5df2019-01-02 14:30:50 +00004663 if (allow_recode && !cpi->sf.gm_disable_recode &&
4664 recode_loop_test_global_motion(cpi)) {
4665 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004666 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004667
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004668 if (cpi->tpl_model_pass == 1) {
4669 assert(cpi->oxcf.enable_tpl_model == 2);
Debargha Mukherjee347c64d2019-05-08 13:53:46 -07004670 av1_tpl_setup_forward_stats(cpi);
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004671 cpi->tpl_model_pass = 0;
4672 loop = 1;
4673 }
4674
Yaowu Xuc27fc142016-08-22 16:08:15 -07004675 if (loop) {
4676 ++loop_count;
4677 ++loop_at_this_size;
4678
4679#if CONFIG_INTERNAL_STATS
4680 ++cpi->tot_recode_hits;
4681#endif
4682 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004683#if CONFIG_COLLECT_COMPONENT_TIMING
4684 if (loop) printf("\n Recoding:");
4685#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004686 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004687
4688 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004689}
4690
Yaowu Xuc27fc142016-08-22 16:08:15 -07004691#define DUMP_RECON_FRAMES 0
4692
4693#if DUMP_RECON_FRAMES == 1
4694// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004695static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4696 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00004697 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00004698 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004699
Zoe Liub4f31032017-11-03 23:48:35 -07004700 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00004701 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07004702 return;
4703 }
4704
Zoe Liu27deb382018-03-27 15:13:56 -07004705 static const int flag_list[REF_FRAMES] = { 0,
4706 AOM_LAST_FLAG,
4707 AOM_LAST2_FLAG,
4708 AOM_LAST3_FLAG,
4709 AOM_GOLD_FLAG,
4710 AOM_BWD_FLAG,
4711 AOM_ALT2_FLAG,
4712 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004713 printf(
4714 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4715 "show_existing_frame=%d) "
4716 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00004717 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07004718 cm->show_existing_frame);
4719 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004720 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
4721 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08004722 printf(" %d(%c)", ref_offset,
4723 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07004724 }
4725 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004726
4727 if (!cm->show_frame) {
4728 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00004729 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004730 return;
4731 }
4732
Zoe Liub4f31032017-11-03 23:48:35 -07004733 int h;
4734 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4735 FILE *f_recon = NULL;
4736
David Turnerd2a592e2018-11-16 14:59:31 +00004737 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004738 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4739 printf("Unable to open file %s to write.\n", file_name);
4740 return;
4741 }
4742 } else {
4743 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4744 printf("Unable to open file %s to append.\n", file_name);
4745 return;
4746 }
4747 }
4748 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004749 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4750 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07004751 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07004752 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07004753 current_frame->frame_number, cpi->gf_group.index,
4754 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
4755 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
4756 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
4757 cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004758#if 0
4759 int ref_frame;
4760 printf("get_ref_frame_map_idx: [");
4761 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00004762 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07004763 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07004764#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004765
4766 // --- Y ---
4767 for (h = 0; h < cm->height; ++h) {
4768 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4769 f_recon);
4770 }
4771 // --- U ---
4772 for (h = 0; h < (cm->height >> 1); ++h) {
4773 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4774 f_recon);
4775 }
4776 // --- V ---
4777 for (h = 0; h < (cm->height >> 1); ++h) {
4778 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4779 f_recon);
4780 }
4781
4782 fclose(f_recon);
4783}
4784#endif // DUMP_RECON_FRAMES
4785
David Turnerbc0993e2019-02-15 14:42:23 +00004786static int get_interp_filter_selected(const AV1_COMMON *const cm,
4787 MV_REFERENCE_FRAME ref,
Sachin Kumar Garg952bb702019-06-18 20:15:07 +05304788 InterpFilter ifilter) {
David Turnerbc0993e2019-02-15 14:42:23 +00004789 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
4790 if (buf == NULL) return 0;
4791 return buf->interp_filter_selected[ifilter];
4792}
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304793
David Turnerbc0993e2019-02-15 14:42:23 +00004794static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
4795 const AV1_COMMON *const cm = &cpi->common;
4796 int ref_total[REF_FRAMES] = { 0 };
4797
4798 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4799 return 0;
4800
4801 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
Sachin Kumar Garg952bb702019-06-18 20:15:07 +05304802 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
David Turnerbc0993e2019-02-15 14:42:23 +00004803 ++ifilter) {
4804 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
4805 }
4806 }
4807 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
4808 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
4809 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
4810
4811 int mask = 0;
Sachin Kumar Garg952bb702019-06-18 20:15:07 +05304812 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
David Turnerbc0993e2019-02-15 14:42:23 +00004813 ++ifilter) {
4814 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
4815 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
4816 int filter_score =
4817 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
4818 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
4819 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
4820 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
4821 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
4822 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
4823 if (filter_score < ref_total_total) mask |= 1 << ifilter;
4824 }
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304825 }
4826 return mask;
4827}
4828
David Turnerefed6372019-01-11 15:14:11 +00004829static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
4830 const YV12_BUFFER_CONFIG *last_picture,
4831 hash_table *last_hash_table) {
4832 aom_clear_system_state();
4833 // check use hash ME
4834 int k;
4835 uint32_t hash_value_1;
4836 uint32_t hash_value_2;
4837
4838 const int block_size = 8;
4839 const double threshold_current = 0.8;
4840 const double threshold_average = 0.95;
4841 const int max_history_size = 32;
4842 int T = 0; // total block
4843 int C = 0; // match with collocated block
4844 int S = 0; // smooth region but not match with collocated block
4845 int M = 0; // match with other block
4846
4847 const int pic_width = cur_picture->y_width;
4848 const int pic_height = cur_picture->y_height;
4849 for (int i = 0; i + block_size <= pic_height; i += block_size) {
4850 for (int j = 0; j + block_size <= pic_width; j += block_size) {
4851 const int x_pos = j;
4852 const int y_pos = i;
4853 int match = 1;
4854 T++;
4855
4856 // check whether collocated block match with current
4857 uint8_t *p_cur = cur_picture->y_buffer;
4858 uint8_t *p_ref = last_picture->y_buffer;
4859 int stride_cur = cur_picture->y_stride;
4860 int stride_ref = last_picture->y_stride;
4861 p_cur += (y_pos * stride_cur + x_pos);
4862 p_ref += (y_pos * stride_ref + x_pos);
4863
4864 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
4865 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
4866 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
4867 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4868 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4869 if (p16_cur[tmpX] != p16_ref[tmpX]) {
4870 match = 0;
4871 }
4872 }
4873 p16_cur += stride_cur;
4874 p16_ref += stride_ref;
4875 }
4876 } else {
4877 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4878 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4879 if (p_cur[tmpX] != p_ref[tmpX]) {
4880 match = 0;
4881 }
4882 }
4883 p_cur += stride_cur;
4884 p_ref += stride_ref;
4885 }
4886 }
4887
4888 if (match) {
4889 C++;
4890 continue;
4891 }
4892
4893 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
4894 y_pos) ||
4895 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
4896 S++;
4897 continue;
4898 }
4899
4900 av1_get_block_hash_value(
4901 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
4902 block_size, &hash_value_1, &hash_value_2,
4903 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
4904 // Hashing does not work for highbitdepth currently.
4905 // TODO(Roger): Make it work for highbitdepth.
4906 if (av1_use_hash_me(&cpi->common)) {
4907 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
4908 M++;
4909 }
4910 }
4911 }
4912 }
4913
4914 assert(T > 0);
4915 double csm_rate = ((double)(C + S + M)) / ((double)(T));
4916 double m_rate = ((double)(M)) / ((double)(T));
4917
4918 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
4919 cpi->m_rate_array[cpi->rate_index] = m_rate;
4920
4921 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
4922 cpi->rate_size++;
4923 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
4924
4925 if (csm_rate < threshold_current) {
4926 return 0;
4927 }
4928
4929 if (C == T) {
4930 return 1;
4931 }
4932
4933 double csm_average = 0.0;
4934 double m_average = 0.0;
4935
4936 for (k = 0; k < cpi->rate_size; k++) {
4937 csm_average += cpi->csm_rate_array[k];
4938 m_average += cpi->m_rate_array[k];
4939 }
4940 csm_average /= cpi->rate_size;
4941 m_average /= cpi->rate_size;
4942
4943 if (csm_average < threshold_average) {
4944 return 0;
4945 }
4946
4947 if (M > (T - C - S) / 3) {
4948 return 1;
4949 }
4950
4951 if (csm_rate > 0.99 && m_rate > 0.01) {
4952 return 1;
4953 }
4954
4955 if (csm_average + m_average > 1.01) {
4956 return 1;
4957 }
4958
4959 return 0;
4960}
4961
David Turner73245762019-02-11 16:42:34 +00004962// Refresh reference frame buffers according to refresh_frame_flags.
4963static void refresh_reference_frames(AV1_COMP *cpi) {
4964 AV1_COMMON *const cm = &cpi->common;
4965 // All buffers are refreshed for shown keyframes and S-frames.
4966
4967 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
4968 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
4969 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
4970 }
4971 }
4972}
4973
sdengc23c7f12019-06-11 16:56:50 -07004974// Implementation and modifications of C. Yeo, H. L. Tan, and Y. H. Tan, "On
4975// rate distortion optimization using SSIM," Circuits and Systems for Video
4976// Technology, IEEE Transactions on, vol. 23, no. 7, pp. 1170-1181, 2013.
4977// SSIM_VAR_SCALE defines the strength of the bias towards SSIM in RDO.
4978#define SSIM_VAR_SCALE 16.0
4979static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
4980 AV1_COMMON *cm = &cpi->common;
4981 ThreadData *td = &cpi->td;
4982 MACROBLOCK *x = &td->mb;
4983 MACROBLOCKD *xd = &x->e_mbd;
4984 uint8_t *y_buffer = cpi->source->y_buffer;
4985 const int y_stride = cpi->source->y_stride;
4986 const int block_size = BLOCK_16X16;
4987
4988 const int num_mi_w = mi_size_wide[block_size];
4989 const int num_mi_h = mi_size_high[block_size];
4990 const int num_cols = (cm->mi_cols + num_mi_w - 1) / num_mi_w;
4991 const int num_rows = (cm->mi_rows + num_mi_h - 1) / num_mi_h;
4992 double log_sum = 0.0;
4993 int row, col;
4994 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
4995
4996 double c2;
4997 if (xd->bd == 10) {
4998 c2 = 941.8761; // (.03*1023)^2
4999 } else if (xd->bd == 12) {
5000 c2 = 15092.1225; // (.03*4095)^2
5001 } else {
5002 c2 = 58.5225; // (.03*255)^2
5003 }
5004
5005 // Loop through each 16x16 block.
5006 for (row = 0; row < num_rows; ++row) {
5007 for (col = 0; col < num_cols; ++col) {
5008 int mi_row, mi_col;
5009 double var = 0.0, num_of_var = 0.0;
5010 const int index = row * num_cols + col;
5011
5012 // Loop through each 8x8 block.
5013 for (mi_row = row * num_mi_h;
5014 mi_row < cm->mi_rows && mi_row < (row + 1) * num_mi_h; mi_row += 2) {
5015 for (mi_col = col * num_mi_w;
5016 mi_col < cm->mi_cols && mi_col < (col + 1) * num_mi_w;
5017 mi_col += 2) {
5018 struct buf_2d buf;
5019 const int row_offset_y = mi_row << 2;
5020 const int col_offset_y = mi_col << 2;
5021
5022 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
5023 buf.stride = y_stride;
5024
5025 if (use_hbd) {
5026 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
5027 xd->bd);
5028 } else {
5029 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
5030 }
5031
5032 num_of_var += 1.0;
5033 }
5034 }
5035 var = var / num_of_var / SSIM_VAR_SCALE;
5036 var = 2.0 * var + c2;
5037 cpi->ssim_rdmult_scaling_factors[index] = var;
5038 log_sum += log(var);
5039 }
5040 }
5041 log_sum = exp(log_sum / (double)(num_rows * num_cols));
5042
5043 for (row = 0; row < num_rows; ++row) {
5044 for (col = 0; col < num_cols; ++col) {
5045 const int index = row * num_cols + col;
5046 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
5047 }
5048 }
5049
5050 (void)xd;
5051}
5052
David Turner73245762019-02-11 16:42:34 +00005053static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
5054 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005055 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005056 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00005057 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07005058 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005059 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005060
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005061#if CONFIG_COLLECT_COMPONENT_TIMING
5062 start_timing(cpi, encode_frame_to_data_rate_time);
5063#endif
5064
Fangwen Fu8d164de2016-12-14 13:40:54 -08005065 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00005066 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07005067
Yunqing Wang9612d552018-05-15 14:58:30 -07005068 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5069 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07005070
sarahparker21dbca42018-03-30 17:43:44 -07005071 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07005072 // cm->allow_ref_frame_mvs needs to be written into the frame header while
5073 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
5074 // separated from frame_might_allow_ref_frame_mvs().
5075 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
5076
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07005077 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07005078 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005079
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305080 cm->last_frame_type = current_frame->frame_type;
5081 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5082 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
5083
Sarah Parker33005522018-07-27 14:46:25 -07005084 if (encode_show_existing_frame(cm)) {
Jingning Han8f661602017-08-19 08:16:50 -07005085 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07005086
David Turner996b2c12018-12-07 15:52:30 +00005087 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005088 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005089 int largest_tile_id = 0; // Output from bitstream: unused here
5090 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005091 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005092
David Turner90311862018-11-29 13:34:36 +00005093 if (seq_params->frame_id_numbers_present_flag &&
5094 current_frame->frame_type == KEY_FRAME) {
5095 // Displaying a forward key-frame, so reset the ref buffer IDs
5096 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
5097 for (int i = 0; i < REF_FRAMES; i++)
5098 cm->ref_frame_id[i] = display_frame_id;
5099 }
5100
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005101 cpi->seq_params_locked = 1;
5102
Yaowu Xuc27fc142016-08-22 16:08:15 -07005103#if DUMP_RECON_FRAMES == 1
5104 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5105 dump_filtered_recon_frames(cpi);
5106#endif // DUMP_RECON_FRAMES
5107
David Turner73245762019-02-11 16:42:34 +00005108 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5109 // for the purpose to verify no mismatch between encoder and decoder.
5110 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005111
David Turner73245762019-02-11 16:42:34 +00005112 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005113
Yaowu Xuc27fc142016-08-22 16:08:15 -07005114 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5115 // to do post-encoding update accordingly.
5116 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005117 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005118 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005119 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005120
David Turnerd2a592e2018-11-16 14:59:31 +00005121 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005122
Tom Finegane4099e32018-01-23 12:01:51 -08005123 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005124 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005125
David Turnerefed6372019-01-11 15:14:11 +00005126 // Work out whether to force_integer_mv this frame
5127 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
5128 !frame_is_intra_only(cm)) {
5129 if (cpi->common.seq_params.force_integer_mv == 2) {
5130 // Adaptive mode: see what previous frame encoded did
5131 if (cpi->unscaled_last_source != NULL) {
5132 cm->cur_frame_force_integer_mv =
5133 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
5134 cpi->previous_hash_table);
5135 } else {
5136 cpi->common.cur_frame_force_integer_mv = 0;
5137 }
5138 } else {
5139 cpi->common.cur_frame_force_integer_mv =
5140 cpi->common.seq_params.force_integer_mv;
5141 }
5142 } else {
5143 cpi->common.cur_frame_force_integer_mv = 0;
5144 }
5145
Yaowu Xuc27fc142016-08-22 16:08:15 -07005146 // Set default state for segment based loop filter update flags.
5147 cm->lf.mode_ref_delta_update = 0;
5148
Yaowu Xuc27fc142016-08-22 16:08:15 -07005149 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05005150 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005151 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005152 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005153
5154 // If segmentation is enabled force a map update for key frames.
5155 if (seg->enabled) {
5156 seg->update_map = 1;
5157 seg->update_data = 1;
5158 }
5159
5160 // The alternate reference frame cannot be active for a key frame.
5161 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005162 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005163 if (cpi->oxcf.mtu == 0) {
5164 cm->num_tg = cpi->oxcf.num_tile_groups;
5165 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005166 // Use a default value for the purposes of weighting costs in probability
5167 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005168 cm->num_tg = DEFAULT_MAX_NUM_TG;
5169 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005170
5171 // For 1 pass CBR, check if we are dropping this frame.
5172 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005173 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00005174 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005175 if (av1_rc_drop_frame(cpi)) {
5176 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00005177 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005178 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005179 }
5180 }
5181
sdengc23c7f12019-06-11 16:56:50 -07005182 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
5183
Yaowu Xuf883b422016-08-30 14:01:10 -07005184 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005185
5186#if CONFIG_INTERNAL_STATS
5187 memset(cpi->mode_chosen_counts, 0,
5188 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5189#endif
5190
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005191 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005192 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08005193 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005194 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005195 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08005196 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07005197 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5198 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5199 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005200 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005201 lsb = cpi->source->y_buffer[0] & 0xff;
5202 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005203 }
David Turner760a2f42018-12-07 15:25:36 +00005204 cm->current_frame_id =
5205 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005206
5207 // S_frame is meant for stitching different streams of different
5208 // resolutions together, so current_frame_id must be the
5209 // same across different streams of the same content current_frame_id
5210 // should be the same and not random. 0x37 is a chosen number as start
5211 // point
5212 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005213 } else {
5214 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00005215 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
5216 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005217 }
5218 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005219
Hui Su483a8452018-02-26 12:28:48 -08005220 switch (cpi->oxcf.cdf_update_mode) {
5221 case 0: // No CDF update for any frames(4~6% compression loss).
5222 cm->disable_cdf_update = 1;
5223 break;
5224 case 1: // Enable CDF update for all frames.
5225 cm->disable_cdf_update = 0;
5226 break;
5227 case 2:
5228 // Strategically determine at which frames to do CDF update.
5229 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5230 // compression loss).
5231 // TODO(huisu@google.com): design schemes for various trade-offs between
5232 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005233 cm->disable_cdf_update =
5234 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005235 break;
5236 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005237 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005238
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005239#if CONFIG_COLLECT_COMPONENT_TIMING
5240 start_timing(cpi, encode_with_recode_loop_time);
5241#endif
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005242
5243 if (cpi->oxcf.pass == 2 && cpi->oxcf.enable_tpl_model == 2 &&
5244 current_frame->frame_type == INTER_FRAME) {
5245 if (!cm->show_frame) {
5246 assert(cpi->tpl_model_pass == 0);
5247 cpi->tpl_model_pass = 1;
5248 }
5249 }
5250
David Turner2f3b5df2019-01-02 14:30:50 +00005251 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
5252 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005253#if CONFIG_COLLECT_COMPONENT_TIMING
5254 end_timing(cpi, encode_with_recode_loop_time);
5255#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005256
5257#ifdef OUTPUT_YUV_SKINMAP
David Turnerd2a592e2018-11-16 14:59:31 +00005258 if (cpi->common.current_frame.frame_number > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005259 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005260 }
5261#endif // OUTPUT_YUV_SKINMAP
5262
5263 // Special case code to reduce pulsing when key frames are forced at a
5264 // fixed interval. Note the reconstruction error if it is the frame before
5265 // the force key frame
5266 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005267 if (seq_params->use_highbitdepth) {
Jack Haughtonddb80602018-11-21 16:41:49 +00005268 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005269 } else {
Jack Haughtonddb80602018-11-21 16:41:49 +00005270 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005271 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005272 }
5273
David Turnerc29e1a92018-12-06 14:10:14 +00005274 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5275 cm->cur_frame->buf.transfer_characteristics =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005276 seq_params->transfer_characteristics;
David Turnerc29e1a92018-12-06 14:10:14 +00005277 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5278 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5279 cm->cur_frame->buf.chroma_sample_position =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005280 seq_params->chroma_sample_position;
David Turnerc29e1a92018-12-06 14:10:14 +00005281 cm->cur_frame->buf.color_range = seq_params->color_range;
5282 cm->cur_frame->buf.render_width = cm->render_width;
5283 cm->cur_frame->buf.render_height = cm->render_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005284
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005285 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5286 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005287
5288 // Pick the loop filter level for the frame.
Cheng Chen68dc9142018-05-02 17:46:28 -07005289 if (!cm->allow_intrabc) {
David Barker218556e2018-02-14 14:23:12 +00005290 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08005291 } else {
Hui Su06463e42018-02-23 22:17:36 -08005292 cm->lf.filter_level[0] = 0;
5293 cm->lf.filter_level[1] = 0;
David Turnerebf96f42018-11-14 16:57:57 +00005294 cm->cdef_info.cdef_bits = 0;
5295 cm->cdef_info.cdef_strengths[0] = 0;
5296 cm->cdef_info.nb_cdef_strengths = 1;
5297 cm->cdef_info.cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005298 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5299 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5300 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08005301 }
David Barker218556e2018-02-14 14:23:12 +00005302
5303 // TODO(debargha): Fix mv search range on encoder side
David Turnerc29e1a92018-12-06 14:10:14 +00005304 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5305 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005306
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005307#ifdef OUTPUT_YUV_REC
David Turnerc29e1a92018-12-06 14:10:14 +00005308 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005309#endif
5310
David Turner996b2c12018-12-07 15:52:30 +00005311 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005312 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005313 int largest_tile_id = 0; // Output from pack_bitstream
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005314#if CONFIG_COLLECT_COMPONENT_TIMING
5315 start_timing(cpi, av1_pack_bitstream_final_time);
5316#endif
David Turner35cba132018-12-10 15:48:15 +00005317 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005318 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005319#if CONFIG_COLLECT_COMPONENT_TIMING
5320 end_timing(cpi, av1_pack_bitstream_final_time);
5321#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005322
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005323 cpi->seq_params_locked = 1;
5324
David Turner996b2c12018-12-07 15:52:30 +00005325 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005326 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00005327 for (int i = 0; i < REF_FRAMES; i++) {
5328 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005329 cm->ref_frame_id[i] = cm->current_frame_id;
5330 }
5331 }
5332 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005333
Yaowu Xuc27fc142016-08-22 16:08:15 -07005334#if DUMP_RECON_FRAMES == 1
5335 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005336 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005337#endif // DUMP_RECON_FRAMES
5338
Soo-Chul Han934af352017-10-15 15:21:51 -04005339 if (cm->seg.enabled) {
5340 if (cm->seg.update_map) {
5341 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005342 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00005343 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04005344 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5345 }
5346 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005347
5348 if (frame_is_intra_only(cm) == 0) {
5349 release_scaled_references(cpi);
5350 }
5351
David Turner73245762019-02-11 16:42:34 +00005352 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5353 // for the purpose to verify no mismatch between encoder and decoder.
5354 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
5355
5356 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005357
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005358#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005359 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005360#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005361
Hui Sudc54be62018-03-14 19:14:28 -07005362 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00005363 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07005364 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005365 }
David Turnera4c96252019-01-11 16:36:39 +00005366 if (!cm->large_scale_tile) {
5367 cm->cur_frame->frame_context = *cm->fc;
5368 }
5369#define EXT_TILE_DEBUG 0
5370#if EXT_TILE_DEBUG
5371 if (cm->large_scale_tile && oxcf->pass == 2) {
5372 char fn[20] = "./fc";
5373 fn[4] = current_frame->frame_number / 100 + '0';
5374 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
5375 fn[6] = (current_frame->frame_number % 10) + '0';
5376 fn[7] = '\0';
5377 av1_print_frame_contexts(cm->fc, fn);
5378 }
5379#endif // EXT_TILE_DEBUG
5380#undef EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07005381
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005382#if CONFIG_COLLECT_COMPONENT_TIMING
5383 end_timing(cpi, encode_frame_to_data_rate_time);
5384
5385 // Print out timing information.
5386 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08005387 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005388 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08005389 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005390 for (i = 0; i < kTimingComponents; i++) {
5391 cpi->component_time[i] += cpi->frame_component_time[i];
5392 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
5393 get_component_name(i), cpi->frame_component_time[i],
5394 cpi->component_time[i]);
5395 cpi->frame_component_time[i] = 0;
5396 }
5397#endif
5398
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305399 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005400
Yaowu Xuf883b422016-08-30 14:01:10 -07005401 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005402
David Turnerefed6372019-01-11 15:14:11 +00005403 // Store encoded frame's hash table for is_integer_mv() next time
5404 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
5405 cpi->previous_hash_table = &cm->cur_frame->hash_table;
5406 }
5407
Yaowu Xuc27fc142016-08-22 16:08:15 -07005408 // Clear the one shot update flags for segmentation map and mode/ref loop
5409 // filter deltas.
5410 cm->seg.update_map = 0;
5411 cm->seg.update_data = 0;
5412 cm->lf.mode_ref_delta_update = 0;
5413
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005414 // A droppable frame might not be shown but it always
5415 // takes a space in the gf group. Therefore, even when
5416 // it is not shown, we still need update the count down.
5417
Yaowu Xuc27fc142016-08-22 16:08:15 -07005418 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08005419 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5420 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005421 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005422 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005423 // Don't increment frame counters if this was an altref buffer
5424 // update not a real frame
Wei-Ting Lin96ee0eb2018-06-22 15:27:22 -07005425
David Turnerd2a592e2018-11-16 14:59:31 +00005426 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005427 }
5428
Tom Finegane4099e32018-01-23 12:01:51 -08005429 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005430}
5431
David Turner056f7cd2019-01-07 17:48:13 +00005432int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00005433 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00005434 const EncodeFrameParams *const frame_params,
5435 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00005436 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00005437 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005438
David Turnercb5e36f2019-01-17 17:15:25 +00005439 cpi->unscaled_source = frame_input->source;
5440 cpi->source = frame_input->source;
5441 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00005442
David Turner6e8b4d92019-02-18 15:01:33 +00005443 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00005444 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00005445 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00005446 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00005447 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005448 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00005449 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00005450 cm->show_existing_frame = frame_params->show_existing_frame;
5451 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00005452
David Turner73245762019-02-11 16:42:34 +00005453 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
5454 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
5455
David Turnerfe3aecb2019-02-06 14:42:42 +00005456 cpi->refresh_last_frame = frame_params->refresh_last_frame;
5457 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
5458 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
5459 cpi->refresh_alt2_ref_frame = frame_params->refresh_alt2_ref_frame;
5460 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
5461
David Turnera7f133c2019-01-22 14:47:16 +00005462 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
5463 current_frame->frame_number = 0;
5464
5465 if (cm->show_existing_frame) {
5466 current_frame->order_hint = cm->cur_frame->order_hint;
5467 } else {
5468 current_frame->order_hint =
5469 current_frame->frame_number + frame_params->order_offset;
5470 current_frame->order_hint %=
5471 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
5472 }
5473
David Turnercb5e36f2019-01-17 17:15:25 +00005474 if (cpi->oxcf.pass == 1) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005475#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00005476 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005477#endif
David Turnercb5e36f2019-01-17 17:15:25 +00005478 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00005479 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
5480 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00005481 return AOM_CODEC_ERROR;
5482 }
5483 } else {
David Turner056f7cd2019-01-07 17:48:13 +00005484 return AOM_CODEC_ERROR;
5485 }
5486
5487 return AOM_CODEC_OK;
5488}
5489
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005490#if CONFIG_DENOISE
5491static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5492 int block_size, float noise_level,
5493 int64_t time_stamp, int64_t end_time) {
5494 AV1_COMMON *const cm = &cpi->common;
5495 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005496 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5497 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005498 if (!cpi->denoise_and_model) {
5499 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5500 "Error allocating denoise and model");
5501 return -1;
5502 }
5503 }
5504 if (!cpi->film_grain_table) {
5505 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5506 if (!cpi->film_grain_table) {
5507 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5508 "Error allocating grain table");
5509 return -1;
5510 }
5511 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5512 }
5513 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5514 &cm->film_grain_params)) {
5515 if (cm->film_grain_params.apply_grain) {
5516 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5517 &cm->film_grain_params);
5518 }
5519 }
5520 return 0;
5521}
5522#endif
5523
James Zern3e2613b2017-03-30 23:14:40 -07005524int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005525 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5526 int64_t end_time) {
5527 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005528 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005529 int res = 0;
5530 const int subsampling_x = sd->subsampling_x;
5531 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005532 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005533
Yaowu Xuc27fc142016-08-22 16:08:15 -07005534 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005535
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005536#if CONFIG_INTERNAL_STATS
5537 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005538 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005539#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005540#if CONFIG_DENOISE
5541 if (cpi->oxcf.noise_level > 0)
5542 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5543 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5544 res = -1;
5545#endif // CONFIG_DENOISE
5546
Yaowu Xuf883b422016-08-30 14:01:10 -07005547 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005548 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005549 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005550#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005551 aom_usec_timer_mark(&timer);
5552 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005553#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005554 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005555 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005556 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005557 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005558 res = -1;
5559 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005560 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005561 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005562 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005563 "Profile 1 requires 4:4:4 color format");
5564 res = -1;
5565 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005566 if ((seq_params->profile == PROFILE_2) &&
5567 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005568 !(subsampling_x == 1 && subsampling_y == 0)) {
5569 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5570 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005571 res = -1;
5572 }
5573
5574 return res;
5575}
5576
Yaowu Xuc27fc142016-08-22 16:08:15 -07005577#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005578extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5579 const unsigned char *img2, int img2_pitch,
5580 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005581
5582static void adjust_image_stat(double y, double u, double v, double all,
5583 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07005584 s->stat[STAT_Y] += y;
5585 s->stat[STAT_U] += u;
5586 s->stat[STAT_V] += v;
5587 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005588 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005589}
5590
Angie Chiang08a22a62017-07-17 17:29:17 -07005591static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005592 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005593 double samples = 0.0;
5594 uint32_t in_bit_depth = 8;
5595 uint32_t bit_depth = 8;
5596
Angie Chiang08a22a62017-07-17 17:29:17 -07005597#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00005598 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005599#endif
5600 cpi->bytes += frame_bytes;
5601
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005602 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005603 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005604 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005605 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005606 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005607 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00005608 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005609 double y, u, v, frame_all;
5610
5611 cpi->count++;
5612 if (cpi->b_calculate_psnr) {
5613 PSNR_STATS psnr;
5614 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005615 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005616 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005617 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005618
5619 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5620 &cpi->psnr);
5621 cpi->total_sq_error += psnr.sse[0];
5622 cpi->total_samples += psnr.samples[0];
5623 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005624 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005625 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005626 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005627 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005628 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005629 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005630
Yaowu Xuf883b422016-08-30 14:01:10 -07005631 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005632 cpi->summed_quality += frame_ssim2 * weight;
5633 cpi->summed_weights += weight;
5634
5635#if 0
5636 {
5637 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005638 double y2 = psnr.psnr[1];
5639 double u2 = psnr.psnr[2];
5640 double v2 = psnr.psnr[3];
5641 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005642 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 +00005643 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005644 frame_psnr2, frame_ssim2);
5645 fclose(f);
5646 }
5647#endif
5648 }
5649 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005650 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005651 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005652 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5653 recon->y_stride, orig->y_width, orig->y_height);
5654 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005655 cpi->total_blockiness += frame_blockiness;
5656 }
5657
5658 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005659 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005660 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005661 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5662 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5663
5664 const double peak = (double)((1 << in_bit_depth) - 1);
5665 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005666 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005667 if (consistency > 0.0)
5668 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005669 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005670 cpi->total_inconsistency += this_inconsistency;
5671 }
5672 }
5673 }
5674
5675 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005676 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005677 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005678 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005679 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5680 }
5681}
5682#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08005683int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5684 size_t *size, uint8_t *dest, int64_t *time_stamp,
5685 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07005686 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005687 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5688 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005689
5690#if CONFIG_BITSTREAM_DEBUG
5691 assert(cpi->oxcf.max_threads == 0 &&
5692 "bitstream debug tool does not support multithreading");
5693 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07005694 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
5695 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005696#endif
5697
Sarah Parker740e8392019-01-23 15:47:53 -08005698 // Indicates whether or not to use an adaptive quantize b rather than
5699 // the traditional version
5700 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
5701
Dominic Symesd4929012018-01-31 17:32:01 +01005702 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00005703 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005704#if CONFIG_INTERNAL_STATS
5705 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005706 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005707#endif
RogerZhou3b635242017-09-19 10:06:46 -07005708 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005709
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07005710 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07005711 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
5712 ? REFRESH_FRAME_CONTEXT_DISABLED
5713 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005714 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08005715 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005716
Sarah Parkerb9041612018-05-22 19:06:47 -07005717 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005718 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005719
David Turnerdedd8ff2019-01-23 13:59:46 +00005720 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005721
Yue Chen1bc5be62018-08-24 13:57:32 -07005722 const int result =
5723 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
5724 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00005725 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00005726 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00005727 } else if (result == -1) {
5728 // Returning -1 indicates no frame encoded; more input is required
5729 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005730 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005731#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005732 aom_usec_timer_mark(&cmptimer);
5733 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07005734#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00005735 if (cpi->b_calculate_psnr) {
5736 if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
5737 generate_psnr_packet(cpi);
5738 }
5739 }
Hui Su8ea87322019-03-29 14:44:32 -07005740
5741 if (cpi->keep_level_stats && oxcf->pass != 1) {
5742 // Initialize level info. at the beginning of each sequence.
5743 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07005744 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07005745 }
kyslovabeeb7c2019-03-06 18:35:04 -08005746 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07005747 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005748
5749#if CONFIG_INTERNAL_STATS
5750 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07005751 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005752 }
5753#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005754#if CONFIG_SPEED_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00005755 if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005756 cpi->tx_search_count += cpi->td.mb.tx_search_count;
5757 cpi->td.mb.tx_search_count = 0;
5758 }
5759#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005760
Yaowu Xuf883b422016-08-30 14:01:10 -07005761 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005762
5763 return 0;
5764}
5765
Yaowu Xuf883b422016-08-30 14:01:10 -07005766int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5767 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005768 if (!cm->show_frame) {
5769 return -1;
5770 } else {
5771 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00005772 if (cm->cur_frame != NULL) {
5773 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005774 dest->y_width = cm->width;
5775 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005776 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
5777 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005778 ret = 0;
5779 } else {
5780 ret = -1;
5781 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005782 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005783 return ret;
5784 }
5785}
5786
Yaowu Xuf883b422016-08-30 14:01:10 -07005787int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00005788 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005789
David Turnere7ebf902018-12-04 14:04:55 +00005790 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005791 return 0;
5792}
5793
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005794static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
5795 const YV12_BUFFER_CONFIG *b) {
5796 return a->y_height == b->y_height && a->y_width == b->y_width &&
5797 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
5798 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
5799 a->border == b->border &&
5800 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
5801 (b->flags & YV12_FLAG_HIGHBITDEPTH);
5802}
5803
Yunqing Wang93b18f32018-06-08 21:08:29 -07005804aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
5805 YV12_BUFFER_CONFIG *new_frame,
5806 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005807 const int num_planes = av1_num_planes(cm);
5808 if (!equal_dimensions_and_border(new_frame, sd))
5809 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
5810 "Incorrect buffer dimensions");
5811 else
5812 aom_yv12_copy_frame(new_frame, sd, num_planes);
5813
5814 return cm->error.error_code;
5815}
5816
Yaowu Xuf883b422016-08-30 14:01:10 -07005817int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
5818 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005819 int hr = 0, hs = 0, vr = 0, vs = 0;
5820
5821 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5822
5823 Scale2Ratio(horiz_mode, &hr, &hs);
5824 Scale2Ratio(vert_mode, &vr, &vs);
5825
5826 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005827 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5828 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005829
5830 return 0;
5831}
5832
Yaowu Xuf883b422016-08-30 14:01:10 -07005833int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005834
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005835int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
5836 size_t output_size = 0;
5837 size_t total_bytes_read = 0;
5838 size_t remaining_size = *frame_size;
5839 uint8_t *buff_ptr = buffer;
5840
5841 // go through each OBUs
5842 while (total_bytes_read < *frame_size) {
5843 uint8_t saved_obu_header[2];
5844 uint64_t obu_payload_size;
5845 size_t length_of_payload_size;
5846 size_t length_of_obu_size;
5847 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
5848 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
5849
5850 // save the obu header (1 or 2 bytes)
5851 memmove(saved_obu_header, buff_ptr, obu_header_size);
5852 // clear the obu_has_size_field
5853 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
5854
5855 // get the payload_size and length of payload_size
5856 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
5857 &obu_payload_size, &length_of_payload_size) != 0) {
5858 return AOM_CODEC_ERROR;
5859 }
5860 obu_bytes_read += length_of_payload_size;
5861
5862 // calculate the length of size of the obu header plus payload
5863 length_of_obu_size =
5864 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
5865
5866 // move the rest of data to new location
5867 memmove(buff_ptr + length_of_obu_size + obu_header_size,
5868 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07005869 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005870
5871 // write the new obu size
5872 const uint64_t obu_size = obu_header_size + obu_payload_size;
5873 size_t coded_obu_size;
5874 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
5875 &coded_obu_size) != 0) {
5876 return AOM_CODEC_ERROR;
5877 }
5878
5879 // write the saved (modified) obu_header following obu size
5880 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
5881
5882 total_bytes_read += obu_bytes_read;
5883 remaining_size -= obu_bytes_read;
5884 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07005885 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005886 }
5887
5888 *frame_size = output_size;
5889 return AOM_CODEC_OK;
5890}
5891
Yaowu Xuf883b422016-08-30 14:01:10 -07005892void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005893 // TODO(yunqingwang): For what references to use, external encoding flags
5894 // should be consistent with internal reference frame selection. Need to
5895 // ensure that there is not conflict between the two. In AV1 encoder, the
5896 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
5897 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
5898 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005899 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005900 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005901 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
5902 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
5903 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005904 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005905 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005906 } else {
5907 int ref = AOM_REFFRAME_ALL;
5908
5909 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
5910 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
5911
5912 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
5913
5914 if (flags & AOM_EFLAG_NO_REF_ARF) {
5915 ref ^= AOM_ALT_FLAG;
5916 ref ^= AOM_BWD_FLAG;
5917 ref ^= AOM_ALT2_FLAG;
5918 } else {
5919 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
5920 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
5921 }
5922
5923 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005924 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005925 }
5926
5927 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005928 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005929 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005930
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005931 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
5932 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005933
Yaowu Xuf883b422016-08-30 14:01:10 -07005934 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005935
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005936 if (flags & AOM_EFLAG_NO_UPD_ARF) {
5937 upd ^= AOM_ALT_FLAG;
5938 upd ^= AOM_BWD_FLAG;
5939 upd ^= AOM_ALT2_FLAG;
5940 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005941
David Turnerce9b5902019-01-23 17:25:47 +00005942 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
5943 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
5944 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
5945 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
5946 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
5947 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00005948 } else {
5949 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005950 }
5951
sarahparker21dbca42018-03-30 17:43:44 -07005952 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
5953 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07005954 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
5955 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07005956 cpi->ext_use_s_frame =
5957 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07005958 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07005959
Yaowu Xuf883b422016-08-30 14:01:10 -07005960 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
5961 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005962 }
5963}
Andrey Norkin795ba872018-03-06 13:24:14 -08005964
Tom Fineganf8d6a162018-08-21 10:47:55 -07005965aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
5966 if (!cpi) return NULL;
5967
5968 uint8_t header_buf[512] = { 0 };
5969 const uint32_t sequence_header_size =
Yaowu Xu797674b2019-05-01 17:38:11 -07005970 av1_write_sequence_header_obu(cpi, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07005971 assert(sequence_header_size <= sizeof(header_buf));
5972 if (sequence_header_size == 0) return NULL;
5973
5974 const size_t obu_header_size = 1;
5975 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
5976 const size_t payload_offset = obu_header_size + size_field_size;
5977
5978 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
5979 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
5980
Hui Su4fd11762019-03-26 16:05:07 -07005981 if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
Tom Fineganf8d6a162018-08-21 10:47:55 -07005982 obu_header_size) {
5983 return NULL;
5984 }
5985
5986 size_t coded_size_field_size = 0;
5987 if (aom_uleb_encode(sequence_header_size, size_field_size,
5988 &header_buf[obu_header_size],
5989 &coded_size_field_size) != 0) {
5990 return NULL;
5991 }
5992 assert(coded_size_field_size == size_field_size);
5993
5994 aom_fixed_buf_t *global_headers =
5995 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
5996 if (!global_headers) return NULL;
5997
5998 const size_t global_header_buf_size =
5999 obu_header_size + size_field_size + sequence_header_size;
6000
6001 global_headers->buf = malloc(global_header_buf_size);
6002 if (!global_headers->buf) {
6003 free(global_headers);
6004 return NULL;
6005 }
6006
6007 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
6008 global_headers->sz = global_header_buf_size;
6009 return global_headers;
6010}