blob: f74da3ae7bbcb75a0dbffff4e9ae9c875862a536 [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
Jingning Hand064cf02017-06-01 10:00:39 -0700540 aom_free(cpi->td.mb.above_pred_buf);
541 cpi->td.mb.above_pred_buf = NULL;
542
543 aom_free(cpi->td.mb.left_pred_buf);
544 cpi->td.mb.left_pred_buf = NULL;
545
546 aom_free(cpi->td.mb.wsrc_buf);
547 cpi->td.mb.wsrc_buf = NULL;
548
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530549 aom_free(cpi->td.mb.inter_modes_info);
550 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530551
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530552 for (int i = 0; i < 2; i++)
553 for (int j = 0; j < 2; j++) {
554 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
555 cpi->td.mb.hash_value_buffer[i][j] = NULL;
556 }
Jingning Hand064cf02017-06-01 10:00:39 -0700557 aom_free(cpi->td.mb.mask_buf);
558 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700559
Jingning Han6cc1fd32017-10-13 09:05:36 -0700560 aom_free(cm->tpl_mvs);
561 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700562
Yaowu Xuf883b422016-08-30 14:01:10 -0700563 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700564 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700565 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700566
Yaowu Xuf883b422016-08-30 14:01:10 -0700567 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700568 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800569 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700570 aom_free_frame_buffer(&cpi->scaled_source);
571 aom_free_frame_buffer(&cpi->scaled_last_source);
572 aom_free_frame_buffer(&cpi->alt_ref_buffer);
573 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700574
Yaowu Xuf883b422016-08-30 14:01:10 -0700575 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700576 cpi->tile_tok[0][0] = 0;
577
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530578 aom_free(cpi->tplist[0][0]);
579 cpi->tplist[0][0] = NULL;
580
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000581 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700582
hui sud9a812b2017-07-06 14:34:37 -0700583 aom_free(cpi->td.mb.palette_buffer);
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700584
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700585 aom_free(cpi->td.mb.tmp_conv_dst);
586 for (int j = 0; j < 2; ++j) {
587 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
588 }
589
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700590#if CONFIG_DENOISE
591 if (cpi->denoise_and_model) {
592 aom_denoise_and_model_free(cpi->denoise_and_model);
593 cpi->denoise_and_model = NULL;
594 }
595#endif
596 if (cpi->film_grain_table) {
597 aom_film_grain_table_free(cpi->film_grain_table);
598 cpi->film_grain_table = NULL;
599 }
Hui Suc3a8d372019-05-28 15:52:45 -0700600
601 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
602 aom_free(cpi->level_info[i]);
603 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604}
605
Yaowu Xuf883b422016-08-30 14:01:10 -0700606static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700607 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700608 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700609
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200610 // Stores a snapshot of key state variables which can subsequently be
611 // restored with a call to av1_restore_coding_context. These functions are
612 // intended for use in a re-code loop in av1_compress_frame where the
613 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800614 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
615 av1_copy(cc->nmv_costs, cpi->nmv_costs);
616 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000617
Yaowu Xuc27fc142016-08-22 16:08:15 -0700618 cc->fc = *cm->fc;
619}
620
Yaowu Xuf883b422016-08-30 14:01:10 -0700621static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700622 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700623 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700624
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200625 // Restore key state variables to the snapshot state stored in the
626 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800627 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
628 av1_copy(cpi->nmv_costs, cc->nmv_costs);
629 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000630
Yaowu Xuc27fc142016-08-22 16:08:15 -0700631 *cm->fc = cc->fc;
632}
633
Yaowu Xuf883b422016-08-30 14:01:10 -0700634static void configure_static_seg_features(AV1_COMP *cpi) {
635 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700636 const RATE_CONTROL *const rc = &cpi->rc;
637 struct segmentation *const seg = &cm->seg;
638
639 int high_q = (int)(rc->avg_q > 48.0);
640 int qi_delta;
641
642 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000643 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700644 // Clear down the global segmentation map
645 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
646 seg->update_map = 0;
647 seg->update_data = 0;
648 cpi->static_mb_pct = 0;
649
650 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700651 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700652
653 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700654 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700655 } else if (cpi->refresh_alt_ref_frame) {
656 // If this is an alt ref frame
657 // Clear down the global segmentation map
658 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
659 seg->update_map = 0;
660 seg->update_data = 0;
661 cpi->static_mb_pct = 0;
662
663 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700664 av1_disable_segmentation(seg);
665 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700666
667 // Scan frames from current to arf frame.
668 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700669 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700670
671 // If segmentation was enabled set those features needed for the
672 // arf itself.
673 if (seg->enabled) {
674 seg->update_map = 1;
675 seg->update_data = 1;
676
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700677 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
678 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700679 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700680 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
681 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
682 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
683 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
684
685 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
686 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
687 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
688 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700689
Yaowu Xuf883b422016-08-30 14:01:10 -0700690 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700691 }
692 } else if (seg->enabled) {
693 // All other frames if segmentation has been enabled
694
695 // First normal frame in a valid gf or alt ref group
696 if (rc->frames_since_golden == 0) {
697 // Set up segment features for normal frames in an arf group
698 if (rc->source_alt_ref_active) {
699 seg->update_map = 0;
700 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700701
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700702 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
703 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700704 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
705 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700706
Cheng Chend8184da2017-09-26 18:15:22 -0700707 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
708 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
709 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
710 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
711
712 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
713 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
714 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
715 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700716
717 // Segment coding disabled for compred testing
718 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700719 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
720 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
721 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700722 }
723 } else {
724 // Disable segmentation and clear down features if alt ref
725 // is not active for this group
726
Yaowu Xuf883b422016-08-30 14:01:10 -0700727 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700728
729 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
730
731 seg->update_map = 0;
732 seg->update_data = 0;
733
Yaowu Xuf883b422016-08-30 14:01:10 -0700734 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700735 }
736 } else if (rc->is_src_frame_alt_ref) {
737 // Special case where we are coding over the top of a previous
738 // alt ref frame.
739 // Segment coding disabled for compred testing
740
741 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700742 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
743 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700744
745 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700746 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
747 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
748 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
749 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750
751 // Skip all MBs if high Q (0,0 mv and skip coeffs)
752 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700753 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
754 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700755 }
756 // Enable data update
757 seg->update_data = 1;
758 } else {
759 // All other frames.
760
761 // No updates.. leave things as they are.
762 seg->update_map = 0;
763 seg->update_data = 0;
764 }
765 }
766}
767
Yaowu Xuf883b422016-08-30 14:01:10 -0700768static void update_reference_segmentation_map(AV1_COMP *cpi) {
769 AV1_COMMON *const cm = &cpi->common;
Yushin Choa7f65922018-04-04 16:06:11 -0700770 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
David Turnerb757ce02018-11-12 15:01:28 +0000771 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700772 int row, col;
773
774 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700775 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700776 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700777 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
778 cache[0] = mi_4x4[0]->segment_id;
779 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700780 cache_ptr += cm->mi_cols;
781 }
782}
783
Yaowu Xuf883b422016-08-30 14:01:10 -0700784static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
785 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700786 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700787 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700788
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530789 if (!cpi->lookahead) {
790 int is_scale = (oxcf->resize_mode || oxcf->superres_mode);
Satish Kumar Suman29909962019-01-09 10:31:21 +0530791 cpi->lookahead = av1_lookahead_init(
792 oxcf->width, oxcf->height, seq_params->subsampling_x,
793 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530794 oxcf->lag_in_frames, oxcf->border_in_pixels, is_scale);
795 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700796 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700797 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700798 "Failed to allocate lag buffers");
799
800 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700801 if (aom_realloc_frame_buffer(
802 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
803 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530804 seq_params->use_highbitdepth, oxcf->border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700805 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700806 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700807 "Failed to allocate altref buffer");
808}
809
Yaowu Xuf883b422016-08-30 14:01:10 -0700810static void alloc_util_frame_buffers(AV1_COMP *cpi) {
811 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700812 const SequenceHeader *const seq_params = &cm->seq_params;
813 if (aom_realloc_frame_buffer(
814 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
815 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530816 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700817 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700818 "Failed to allocate last frame buffer");
819
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700820 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800821 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700822 cm->superres_upscaled_height, seq_params->subsampling_x,
823 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +0530824 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800825 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800826 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700827
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700828 if (aom_realloc_frame_buffer(
829 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
830 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530831 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700832 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700833 "Failed to allocate scaled source buffer");
834
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700835 if (aom_realloc_frame_buffer(
836 &cpi->scaled_last_source, cm->width, cm->height,
837 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530838 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700839 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700840 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700841 "Failed to allocate scaled last source buffer");
842}
843
Cheng Chen46f30c72017-09-07 11:13:33 -0700844static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700845 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000846 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700847
Yaowu Xuf883b422016-08-30 14:01:10 -0700848 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530850 int mi_rows_aligned_to_sb =
851 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
852 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
853
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700854 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700855
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856 alloc_context_buffers_ext(cpi);
857
Yaowu Xuf883b422016-08-30 14:01:10 -0700858 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859
860 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000861 unsigned int tokens =
862 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700863 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700864 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700865 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530866 aom_free(cpi->tplist[0][0]);
867
868 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
869 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
870 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700871
Yaowu Xuf883b422016-08-30 14:01:10 -0700872 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700873}
874
Yaowu Xuf883b422016-08-30 14:01:10 -0700875void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700876 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700877 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700878}
879
Hui Suef139e12019-05-20 15:51:22 -0700880double av1_get_compression_ratio(const AV1_COMMON *const cm,
881 size_t encoded_frame_size) {
882 const int upscaled_width = cm->superres_upscaled_width;
883 const int height = cm->height;
884 const int luma_pic_size = upscaled_width * height;
885 const SequenceHeader *const seq_params = &cm->seq_params;
886 const BITSTREAM_PROFILE profile = seq_params->profile;
887 const int pic_size_profile_factor =
888 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
889 encoded_frame_size =
890 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
891 const size_t uncompressed_frame_size =
892 (luma_pic_size * pic_size_profile_factor) >> 3;
893 return uncompressed_frame_size / (double)encoded_frame_size;
894}
895
Yunqing Wang75e20e82018-06-16 12:10:48 -0700896static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200897 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200898 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200899
900 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200901
902 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200903 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200904 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200905 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
906 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200907 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000908 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
909 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200910 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200911 cm->uniform_tile_spacing_flag = 0;
912 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
913 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200914 size_sb = cpi->oxcf.tile_widths[j++];
915 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000916 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200917 }
918 cm->tile_cols = i;
919 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200920 }
921 av1_calculate_tile_cols(cm);
922
923 // configure tile rows
924 if (cm->uniform_tile_spacing_flag) {
925 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
926 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200927 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000928 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
929 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200930 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200931 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
932 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200933 size_sb = cpi->oxcf.tile_heights[j++];
934 if (j >= cpi->oxcf.tile_height_count) j = 0;
935 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200936 }
937 cm->tile_rows = i;
938 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200939 }
940 av1_calculate_tile_rows(cm);
941}
942
Yaowu Xuf883b422016-08-30 14:01:10 -0700943static void update_frame_size(AV1_COMP *cpi) {
944 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700945 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
946
Yaowu Xuf883b422016-08-30 14:01:10 -0700947 av1_set_mb_mi(cm, cm->width, cm->height);
948 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500949 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700950 memset(cpi->mbmi_ext_base, 0,
951 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700952 set_tile_info(cpi);
953}
954
Yaowu Xuf883b422016-08-30 14:01:10 -0700955static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700956 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700957 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +0000958 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700959 cpi->rate_index = 0;
960 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700961}
962
Debargha Mukherjee57498692018-05-11 13:29:31 -0700963static INLINE int does_level_match(int width, int height, double fps,
964 int lvl_width, int lvl_height,
965 double lvl_fps, int lvl_dim_mult) {
966 const int64_t lvl_luma_pels = lvl_width * lvl_height;
967 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
968 const int64_t luma_pels = width * height;
969 const double display_sample_rate = luma_pels * fps;
970 return luma_pels <= lvl_luma_pels &&
971 display_sample_rate <= lvl_display_sample_rate &&
972 width <= lvl_width * lvl_dim_mult &&
973 height <= lvl_height * lvl_dim_mult;
974}
975
Andrey Norkin26495512018-06-20 17:13:11 -0700976static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -0700977 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700978 // TODO(any): This is a placeholder function that only addresses dimensions
979 // and max display sample rates.
980 // Need to add checks for max bit rate, max decoded luma sample rate, header
981 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -0700982 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700983 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
984 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700985 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700986 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
987 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700988 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700989 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
990 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700991 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700992 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
993 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700994 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700995 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
996 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -0700997 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700998 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
999 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001000 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001001 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1002 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001003 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001004 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1005 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001006 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001007 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1008 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001009 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001010 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1011 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001012 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001013 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1014 8192, 4352, 60.0, 2)) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001015 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1016 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001017 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001018 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1019 16384, 8704, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001020 level = SEQ_LEVEL_7_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001021 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1022 16384, 8704, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001023 level = SEQ_LEVEL_7_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001024 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1025 16384, 8704, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001026 level = SEQ_LEVEL_7_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001027 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001028 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001029 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001030 // Set the maximum parameters for bitrate and buffer size for this profile,
1031 // level, and tier
Yaowu Xu7e450882019-04-30 15:09:18 -07001032 cm->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001033 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001034 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1035 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001036 if (cm->op_params[i].bitrate == 0)
1037 aom_internal_error(
1038 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1039 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001040 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001041 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001042 }
1043}
1044
Andrey Norkin26495512018-06-20 17:13:11 -07001045static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
1046 const AV1EncoderConfig *oxcf) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001047 seq->still_picture = (oxcf->limit == 1);
1048 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001049 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001050 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001051 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001052 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001053 seq->frame_id_numbers_present_flag =
1054 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
1055 !oxcf->large_scale_tile && oxcf->error_resilient_mode;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001056 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001057 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001058 seq->force_screen_content_tools = 2;
1059 seq->force_integer_mv = 2;
1060 }
David Turnerebf96f42018-11-14 16:57:57 +00001061 seq->order_hint_info.order_hint_bits_minus_1 =
1062 seq->order_hint_info.enable_order_hint
1063 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1064 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001065
David Turner760a2f42018-12-07 15:25:36 +00001066 seq->max_frame_width =
1067 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1068 seq->max_frame_height = oxcf->forced_max_frame_height
1069 ? oxcf->forced_max_frame_height
1070 : oxcf->height;
1071 seq->num_bits_width =
1072 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1073 seq->num_bits_height =
1074 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1075 assert(seq->num_bits_width <= 16);
1076 assert(seq->num_bits_height <= 16);
1077
1078 seq->frame_id_length = FRAME_ID_LENGTH;
1079 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1080
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001081 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001082 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1083 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001084 seq->order_hint_info.enable_order_hint;
1085 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1086 seq->order_hint_info.enable_ref_frame_mvs &=
1087 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001088 seq->enable_superres = oxcf->enable_superres;
1089 seq->enable_cdef = oxcf->enable_cdef;
1090 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001091 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001092 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1093 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001094 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001095 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001096
Andrey Norkin26495512018-06-20 17:13:11 -07001097 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001098
1099 if (seq->operating_points_cnt_minus_1 == 0) {
1100 seq->operating_point_idc[0] = 0;
1101 } else {
1102 // Set operating_point_idc[] such that for the i-th operating point the
1103 // first (operating_points_cnt-i) spatial layers and the first temporal
1104 // layer are decoded Note that highest quality operating point should come
1105 // first
1106 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1107 seq->operating_point_idc[i] =
1108 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1109 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001110}
1111
Yaowu Xuf883b422016-08-30 14:01:10 -07001112static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1113 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001114
1115 cpi->oxcf = *oxcf;
1116 cpi->framerate = oxcf->init_framerate;
1117
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001118 cm->seq_params.profile = oxcf->profile;
1119 cm->seq_params.bit_depth = oxcf->bit_depth;
1120 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1121 cm->seq_params.color_primaries = oxcf->color_primaries;
1122 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1123 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001124 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001125 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1126 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001127 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001128 cm->timing_info.num_units_in_display_tick =
1129 oxcf->timing_info.num_units_in_display_tick;
1130 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1131 cm->timing_info.equal_picture_interval =
1132 oxcf->timing_info.equal_picture_interval;
1133 cm->timing_info.num_ticks_per_picture =
1134 oxcf->timing_info.num_ticks_per_picture;
1135
Andrey Norkin26495512018-06-20 17:13:11 -07001136 cm->seq_params.display_model_info_present_flag =
1137 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001138 cm->seq_params.decoder_model_info_present_flag =
1139 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001140 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001141 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001142 cm->buffer_model.num_units_in_decoding_tick =
1143 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001144 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07001145 av1_set_aom_dec_model_info(&cm->buffer_model);
1146 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07001147 } else if (cm->timing_info_present &&
1148 cm->timing_info.equal_picture_interval &&
1149 !cm->seq_params.decoder_model_info_present_flag) {
1150 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07001151 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001152 } else {
1153 cm->op_params[0].initial_display_delay =
1154 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001155 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001156
Tom Fineganf8d6a162018-08-21 10:47:55 -07001157 if (cm->seq_params.monochrome) {
1158 cm->seq_params.subsampling_x = 1;
1159 cm->seq_params.subsampling_y = 1;
1160 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1161 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1162 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1163 cm->seq_params.subsampling_x = 0;
1164 cm->seq_params.subsampling_y = 0;
1165 } else {
1166 if (cm->seq_params.profile == 0) {
1167 cm->seq_params.subsampling_x = 1;
1168 cm->seq_params.subsampling_y = 1;
1169 } else if (cm->seq_params.profile == 1) {
1170 cm->seq_params.subsampling_x = 0;
1171 cm->seq_params.subsampling_y = 0;
1172 } else {
1173 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1174 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1175 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1176 } else {
1177 cm->seq_params.subsampling_x = 1;
1178 cm->seq_params.subsampling_y = 0;
1179 }
1180 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001181 }
1182
Yaowu Xuc27fc142016-08-22 16:08:15 -07001183 cm->width = oxcf->width;
1184 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001185 set_sb_size(&cm->seq_params,
1186 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001187 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001188
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001189 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001190
1191 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001192 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001193
1194 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001195 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001196
1197 cpi->static_mb_pct = 0;
1198 cpi->ref_frame_flags = 0;
1199
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001200 // Reset resize pending flags
1201 cpi->resize_pending_width = 0;
1202 cpi->resize_pending_height = 0;
1203
Yaowu Xuc27fc142016-08-22 16:08:15 -07001204 init_buffer_indices(cpi);
1205}
1206
1207static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001208 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001209 const int64_t bandwidth = oxcf->target_bandwidth;
1210 const int64_t starting = oxcf->starting_buffer_level_ms;
1211 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1212 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1213
1214 rc->starting_buffer_level = starting * bandwidth / 1000;
1215 rc->optimal_buffer_level =
1216 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1217 rc->maximum_buffer_size =
1218 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1219}
1220
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001221#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1222 cpi->fn_ptr[BT].sdf = SDF; \
1223 cpi->fn_ptr[BT].sdaf = SDAF; \
1224 cpi->fn_ptr[BT].vf = VF; \
1225 cpi->fn_ptr[BT].svf = SVF; \
1226 cpi->fn_ptr[BT].svaf = SVAF; \
1227 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1228 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001229 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001230
1231#define MAKE_BFP_SAD_WRAPPER(fnname) \
1232 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1233 int source_stride, \
1234 const uint8_t *ref_ptr, int ref_stride) { \
1235 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1236 } \
1237 static unsigned int fnname##_bits10( \
1238 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1239 int ref_stride) { \
1240 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1241 } \
1242 static unsigned int fnname##_bits12( \
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) >> 4; \
1246 }
1247
1248#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1249 static unsigned int fnname##_bits8( \
1250 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1251 int ref_stride, const uint8_t *second_pred) { \
1252 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1253 } \
1254 static unsigned int fnname##_bits10( \
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 2; \
1259 } \
1260 static unsigned int fnname##_bits12( \
1261 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1262 int ref_stride, const uint8_t *second_pred) { \
1263 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1264 4; \
1265 }
1266
Yaowu Xuc27fc142016-08-22 16:08:15 -07001267#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1268 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1269 const uint8_t *const ref_ptr[], int ref_stride, \
1270 unsigned int *sad_array) { \
1271 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1272 } \
1273 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1274 const uint8_t *const ref_ptr[], int ref_stride, \
1275 unsigned int *sad_array) { \
1276 int i; \
1277 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1278 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1279 } \
1280 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1281 const uint8_t *const ref_ptr[], int ref_stride, \
1282 unsigned int *sad_array) { \
1283 int i; \
1284 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1285 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1286 }
1287
Cheng Chenbf3d4962017-11-01 14:48:52 -07001288#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1289 static unsigned int fnname##_bits8( \
1290 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1291 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001292 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001293 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1294 jcp_param); \
1295 } \
1296 static unsigned int fnname##_bits10( \
1297 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1298 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001299 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001300 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1301 jcp_param) >> \
1302 2; \
1303 } \
1304 static unsigned int fnname##_bits12( \
1305 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1306 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001307 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001308 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1309 jcp_param) >> \
1310 4; \
1311 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001312
Yaowu Xuf883b422016-08-30 14:01:10 -07001313MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1314MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001315MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1316MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1317MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1318MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1319MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1320MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1321MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001322MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1323MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1324MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1325MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1326MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1327MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1328MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1329MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1330MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1331MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1332MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1333MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1334MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1335MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001336MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1337MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1338MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001339MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1340MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1341MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001342MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1343MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1344MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001345MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1346MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1347MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001348MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1349MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1350MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001351MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1352MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1353MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001354MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1355MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1356MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001357MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1358MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1359MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001360MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001361
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001362MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1363MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1364MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1365MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1366MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1367MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1368MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1369MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1370MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1371MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1372MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1373MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001374MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1375MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1376MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1377MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1378MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1379MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001380
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001381MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1382MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1383MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1384MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1385MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1386MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1387MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1388MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1389MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1390MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1391MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1392MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1393MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1394MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1395MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1396MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1397MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1398MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1399MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1400MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1401MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1402MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001403
David Barker0f3c94e2017-05-16 15:21:50 +01001404#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001405 cpi->fn_ptr[BT].msdf = MCSDF; \
1406 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001407
David Barkerc155e012017-05-11 13:54:54 +01001408#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1409 static unsigned int fnname##_bits8( \
1410 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1411 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1412 int m_stride, int invert_mask) { \
1413 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1414 second_pred_ptr, m, m_stride, invert_mask); \
1415 } \
1416 static unsigned int fnname##_bits10( \
1417 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1418 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1419 int m_stride, int invert_mask) { \
1420 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1421 second_pred_ptr, m, m_stride, invert_mask) >> \
1422 2; \
1423 } \
1424 static unsigned int fnname##_bits12( \
1425 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1426 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1427 int m_stride, int invert_mask) { \
1428 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1429 second_pred_ptr, m, m_stride, invert_mask) >> \
1430 4; \
1431 }
1432
David Barkerf19f35f2017-05-22 16:33:22 +01001433MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1434MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1435MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001436MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1437MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1438MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1439MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1440MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1441MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1442MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1443MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1444MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1445MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1446MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1447MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1448MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001449MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1450MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1451MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1452MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001453MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1454MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001455
Yaowu Xuc27fc142016-08-22 16:08:15 -07001456#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1457 cpi->fn_ptr[BT].osdf = OSDF; \
1458 cpi->fn_ptr[BT].ovf = OVF; \
1459 cpi->fn_ptr[BT].osvf = OSVF;
1460
1461#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1462 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1463 const int32_t *wsrc, \
1464 const int32_t *msk) { \
1465 return fnname(ref, ref_stride, wsrc, msk); \
1466 } \
1467 static unsigned int fnname##_bits10(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) >> 2; \
1471 } \
1472 static unsigned int fnname##_bits12(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) >> 4; \
1476 }
1477
Yaowu Xuf883b422016-08-30 14:01:10 -07001478MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1479MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1480MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001481MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1482MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1483MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1484MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1485MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1486MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1487MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1488MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1489MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1490MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1491MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1492MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1493MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001494MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1495MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1496MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1497MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001498MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1499MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001500
Yaowu Xuf883b422016-08-30 14:01:10 -07001501static void highbd_set_var_fns(AV1_COMP *const cpi) {
1502 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001503 if (cm->seq_params.use_highbitdepth) {
1504 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001505 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001506 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1507 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1508 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001509 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001510 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001511 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1512 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001513
1514 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1515 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1516 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001517 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001518 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001519 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1520 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001521
1522 HIGHBD_BFP(
1523 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1524 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001525 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001526 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1527 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001528
1529 HIGHBD_BFP(
1530 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1531 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001532 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001533 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1534 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001535
1536 HIGHBD_BFP(
1537 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1538 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001539 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001540 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1541 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001542
1543 HIGHBD_BFP(
1544 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1545 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001546 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001547 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1548 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001549
1550 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1551 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1552 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001553 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001554 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001555 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1556 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001557
1558 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1559 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1560 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001561 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001562 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001563 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1564 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001565
1566 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1567 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1568 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001569 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001570 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001571 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1572 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001573
1574 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1575 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1576 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001577 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001578 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001579 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1580 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001581
1582 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1583 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1584 aom_highbd_8_sub_pixel_variance32x32,
1585 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001586 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001587 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1588 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001589
1590 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1591 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1592 aom_highbd_8_sub_pixel_variance64x64,
1593 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001594 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001595 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1596 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001597
1598 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1599 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1600 aom_highbd_8_sub_pixel_variance16x16,
1601 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001602 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001603 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1604 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001605
1606 HIGHBD_BFP(
1607 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1608 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001609 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001610 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1611 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001612
1613 HIGHBD_BFP(
1614 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1615 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001616 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001617 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1618 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001619
Cheng Chenbf3d4962017-11-01 14:48:52 -07001620 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001621 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1622 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1623 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1624 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1625 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1626
1627 HIGHBD_BFP(
1628 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1629 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1630 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1631 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1632 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1633
1634 HIGHBD_BFP(
1635 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1636 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1637 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1638 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1639 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1640
1641 HIGHBD_BFP(
1642 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1643 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1644 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1645 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1646 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1647
1648 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1649 aom_highbd_sad128x128_avg_bits8,
1650 aom_highbd_8_variance128x128,
1651 aom_highbd_8_sub_pixel_variance128x128,
1652 aom_highbd_8_sub_pixel_avg_variance128x128,
1653 aom_highbd_sad128x128x4d_bits8,
1654 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1655 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001656
1657 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1658 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1659 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001660 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001661 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001662 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1663 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001664
1665 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1666 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1667 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001668 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001669 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001670 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1671 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001672
David Barkerf19f35f2017-05-22 16:33:22 +01001673 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1674 aom_highbd_8_masked_sub_pixel_variance128x128)
1675 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1676 aom_highbd_8_masked_sub_pixel_variance128x64)
1677 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1678 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001679 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1680 aom_highbd_8_masked_sub_pixel_variance64x64)
1681 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1682 aom_highbd_8_masked_sub_pixel_variance64x32)
1683 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1684 aom_highbd_8_masked_sub_pixel_variance32x64)
1685 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1686 aom_highbd_8_masked_sub_pixel_variance32x32)
1687 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1688 aom_highbd_8_masked_sub_pixel_variance32x16)
1689 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1690 aom_highbd_8_masked_sub_pixel_variance16x32)
1691 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1692 aom_highbd_8_masked_sub_pixel_variance16x16)
1693 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1694 aom_highbd_8_masked_sub_pixel_variance8x16)
1695 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1696 aom_highbd_8_masked_sub_pixel_variance16x8)
1697 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1698 aom_highbd_8_masked_sub_pixel_variance8x8)
1699 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1700 aom_highbd_8_masked_sub_pixel_variance4x8)
1701 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1702 aom_highbd_8_masked_sub_pixel_variance8x4)
1703 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1704 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001705 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1706 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001707 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1708 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001709 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1710 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001711 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1712 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001713 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1714 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001715 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1716 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001717 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1718 aom_highbd_obmc_variance128x128,
1719 aom_highbd_obmc_sub_pixel_variance128x128)
1720 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1721 aom_highbd_obmc_variance128x64,
1722 aom_highbd_obmc_sub_pixel_variance128x64)
1723 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1724 aom_highbd_obmc_variance64x128,
1725 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001726 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1727 aom_highbd_obmc_variance64x64,
1728 aom_highbd_obmc_sub_pixel_variance64x64)
1729 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1730 aom_highbd_obmc_variance64x32,
1731 aom_highbd_obmc_sub_pixel_variance64x32)
1732 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1733 aom_highbd_obmc_variance32x64,
1734 aom_highbd_obmc_sub_pixel_variance32x64)
1735 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1736 aom_highbd_obmc_variance32x32,
1737 aom_highbd_obmc_sub_pixel_variance32x32)
1738 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1739 aom_highbd_obmc_variance32x16,
1740 aom_highbd_obmc_sub_pixel_variance32x16)
1741 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1742 aom_highbd_obmc_variance16x32,
1743 aom_highbd_obmc_sub_pixel_variance16x32)
1744 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1745 aom_highbd_obmc_variance16x16,
1746 aom_highbd_obmc_sub_pixel_variance16x16)
1747 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1748 aom_highbd_obmc_variance8x16,
1749 aom_highbd_obmc_sub_pixel_variance8x16)
1750 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1751 aom_highbd_obmc_variance16x8,
1752 aom_highbd_obmc_sub_pixel_variance16x8)
1753 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1754 aom_highbd_obmc_variance8x8,
1755 aom_highbd_obmc_sub_pixel_variance8x8)
1756 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1757 aom_highbd_obmc_variance4x8,
1758 aom_highbd_obmc_sub_pixel_variance4x8)
1759 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1760 aom_highbd_obmc_variance8x4,
1761 aom_highbd_obmc_sub_pixel_variance8x4)
1762 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1763 aom_highbd_obmc_variance4x4,
1764 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001765 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1766 aom_highbd_obmc_variance64x16,
1767 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001768 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1769 aom_highbd_obmc_variance16x64,
1770 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001771 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1772 aom_highbd_obmc_variance32x8,
1773 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001774 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1775 aom_highbd_obmc_variance8x32,
1776 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001777 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1778 aom_highbd_obmc_variance16x4,
1779 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001780 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1781 aom_highbd_obmc_variance4x16,
1782 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001783 break;
1784
Yaowu Xuf883b422016-08-30 14:01:10 -07001785 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001786 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1787 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1788 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001789 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001790 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001791 aom_highbd_dist_wtd_sad64x16_avg_bits10,
1792 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001793
1794 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1795 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1796 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001797 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001798 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001799 aom_highbd_dist_wtd_sad16x64_avg_bits10,
1800 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001801
1802 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1803 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1804 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001805 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001806 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001807 aom_highbd_dist_wtd_sad32x8_avg_bits10,
1808 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001809
1810 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1811 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1812 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001813 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001814 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001815 aom_highbd_dist_wtd_sad8x32_avg_bits10,
1816 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001817
1818 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1819 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1820 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001821 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001822 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001823 aom_highbd_dist_wtd_sad16x4_avg_bits10,
1824 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001825
1826 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1827 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1828 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001829 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001830 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001831 aom_highbd_dist_wtd_sad4x16_avg_bits10,
1832 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001833
1834 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1835 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1836 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001837 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001838 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001839 aom_highbd_dist_wtd_sad32x16_avg_bits10,
1840 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001841
1842 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1843 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1844 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001845 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001846 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001847 aom_highbd_dist_wtd_sad16x32_avg_bits10,
1848 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001849
1850 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1851 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1852 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001853 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001854 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001855 aom_highbd_dist_wtd_sad64x32_avg_bits10,
1856 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001857
1858 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1859 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1860 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001861 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001862 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001863 aom_highbd_dist_wtd_sad32x64_avg_bits10,
1864 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001865
1866 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1867 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1868 aom_highbd_10_sub_pixel_variance32x32,
1869 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001870 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001871 aom_highbd_dist_wtd_sad32x32_avg_bits10,
1872 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001873
1874 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1875 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1876 aom_highbd_10_sub_pixel_variance64x64,
1877 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001878 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001879 aom_highbd_dist_wtd_sad64x64_avg_bits10,
1880 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001881
1882 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1883 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1884 aom_highbd_10_sub_pixel_variance16x16,
1885 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001886 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001887 aom_highbd_dist_wtd_sad16x16_avg_bits10,
1888 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001889
1890 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1891 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1892 aom_highbd_10_sub_pixel_variance16x8,
1893 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001894 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001895 aom_highbd_dist_wtd_sad16x8_avg_bits10,
1896 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001897
1898 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1899 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1900 aom_highbd_10_sub_pixel_variance8x16,
1901 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001902 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001903 aom_highbd_dist_wtd_sad8x16_avg_bits10,
1904 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001905
1906 HIGHBD_BFP(
1907 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1908 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001909 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001910 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
1911 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001912
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001913 HIGHBD_BFP(
1914 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1915 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1916 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001917 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
1918 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001919
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001920 HIGHBD_BFP(
1921 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1922 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1923 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001924 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
1925 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001926
1927 HIGHBD_BFP(
1928 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1929 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001930 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001931 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
1932 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001933
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001934 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1935 aom_highbd_sad128x128_avg_bits10,
1936 aom_highbd_10_variance128x128,
1937 aom_highbd_10_sub_pixel_variance128x128,
1938 aom_highbd_10_sub_pixel_avg_variance128x128,
1939 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001940 aom_highbd_dist_wtd_sad128x128_avg_bits10,
1941 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001942
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001943 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1944 aom_highbd_sad128x64_avg_bits10,
1945 aom_highbd_10_variance128x64,
1946 aom_highbd_10_sub_pixel_variance128x64,
1947 aom_highbd_10_sub_pixel_avg_variance128x64,
1948 aom_highbd_sad128x64x4d_bits10,
1949 aom_highbd_dist_wtd_sad128x64_avg_bits10,
1950 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001951
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001952 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1953 aom_highbd_sad64x128_avg_bits10,
1954 aom_highbd_10_variance64x128,
1955 aom_highbd_10_sub_pixel_variance64x128,
1956 aom_highbd_10_sub_pixel_avg_variance64x128,
1957 aom_highbd_sad64x128x4d_bits10,
1958 aom_highbd_dist_wtd_sad64x128_avg_bits10,
1959 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001960
David Barkerf19f35f2017-05-22 16:33:22 +01001961 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1962 aom_highbd_10_masked_sub_pixel_variance128x128)
1963 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1964 aom_highbd_10_masked_sub_pixel_variance128x64)
1965 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1966 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001967 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1968 aom_highbd_10_masked_sub_pixel_variance64x64)
1969 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1970 aom_highbd_10_masked_sub_pixel_variance64x32)
1971 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1972 aom_highbd_10_masked_sub_pixel_variance32x64)
1973 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1974 aom_highbd_10_masked_sub_pixel_variance32x32)
1975 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1976 aom_highbd_10_masked_sub_pixel_variance32x16)
1977 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1978 aom_highbd_10_masked_sub_pixel_variance16x32)
1979 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1980 aom_highbd_10_masked_sub_pixel_variance16x16)
1981 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1982 aom_highbd_10_masked_sub_pixel_variance8x16)
1983 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1984 aom_highbd_10_masked_sub_pixel_variance16x8)
1985 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1986 aom_highbd_10_masked_sub_pixel_variance8x8)
1987 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1988 aom_highbd_10_masked_sub_pixel_variance4x8)
1989 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1990 aom_highbd_10_masked_sub_pixel_variance8x4)
1991 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1992 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001993 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1994 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001995 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1996 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001997 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1998 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001999 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2000 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002001 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2002 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002003 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2004 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002005 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2006 aom_highbd_10_obmc_variance128x128,
2007 aom_highbd_10_obmc_sub_pixel_variance128x128)
2008 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2009 aom_highbd_10_obmc_variance128x64,
2010 aom_highbd_10_obmc_sub_pixel_variance128x64)
2011 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2012 aom_highbd_10_obmc_variance64x128,
2013 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002014 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2015 aom_highbd_10_obmc_variance64x64,
2016 aom_highbd_10_obmc_sub_pixel_variance64x64)
2017 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2018 aom_highbd_10_obmc_variance64x32,
2019 aom_highbd_10_obmc_sub_pixel_variance64x32)
2020 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2021 aom_highbd_10_obmc_variance32x64,
2022 aom_highbd_10_obmc_sub_pixel_variance32x64)
2023 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2024 aom_highbd_10_obmc_variance32x32,
2025 aom_highbd_10_obmc_sub_pixel_variance32x32)
2026 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2027 aom_highbd_10_obmc_variance32x16,
2028 aom_highbd_10_obmc_sub_pixel_variance32x16)
2029 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2030 aom_highbd_10_obmc_variance16x32,
2031 aom_highbd_10_obmc_sub_pixel_variance16x32)
2032 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2033 aom_highbd_10_obmc_variance16x16,
2034 aom_highbd_10_obmc_sub_pixel_variance16x16)
2035 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2036 aom_highbd_10_obmc_variance8x16,
2037 aom_highbd_10_obmc_sub_pixel_variance8x16)
2038 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2039 aom_highbd_10_obmc_variance16x8,
2040 aom_highbd_10_obmc_sub_pixel_variance16x8)
2041 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2042 aom_highbd_10_obmc_variance8x8,
2043 aom_highbd_10_obmc_sub_pixel_variance8x8)
2044 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2045 aom_highbd_10_obmc_variance4x8,
2046 aom_highbd_10_obmc_sub_pixel_variance4x8)
2047 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2048 aom_highbd_10_obmc_variance8x4,
2049 aom_highbd_10_obmc_sub_pixel_variance8x4)
2050 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2051 aom_highbd_10_obmc_variance4x4,
2052 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002053
Rupert Swarbrick72678572017-08-02 12:05:26 +01002054 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2055 aom_highbd_10_obmc_variance64x16,
2056 aom_highbd_10_obmc_sub_pixel_variance64x16)
2057
2058 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2059 aom_highbd_10_obmc_variance16x64,
2060 aom_highbd_10_obmc_sub_pixel_variance16x64)
2061
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002062 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2063 aom_highbd_10_obmc_variance32x8,
2064 aom_highbd_10_obmc_sub_pixel_variance32x8)
2065
2066 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2067 aom_highbd_10_obmc_variance8x32,
2068 aom_highbd_10_obmc_sub_pixel_variance8x32)
2069
2070 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2071 aom_highbd_10_obmc_variance16x4,
2072 aom_highbd_10_obmc_sub_pixel_variance16x4)
2073
2074 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2075 aom_highbd_10_obmc_variance4x16,
2076 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002077 break;
2078
Yaowu Xuf883b422016-08-30 14:01:10 -07002079 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002080 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2081 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2082 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002083 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002084 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002085 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2086 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002087
2088 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2089 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2090 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002091 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002092 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002093 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2094 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002095
2096 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2097 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2098 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002099 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002100 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002101 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2102 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002103
2104 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2105 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2106 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002107 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002108 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002109 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2110 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002111
2112 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2113 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2114 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002115 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002116 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002117 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2118 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002119
2120 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2121 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2122 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002123 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002124 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002125 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2126 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002127
2128 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2129 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2130 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002131 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002132 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002133 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2134 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002135
2136 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2137 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2138 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002139 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002140 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002141 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2142 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002143
2144 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2145 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2146 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002147 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002148 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002149 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2150 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002151
2152 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2153 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2154 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002155 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002156 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002157 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2158 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002159
2160 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2161 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2162 aom_highbd_12_sub_pixel_variance32x32,
2163 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002164 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002165 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2166 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002167
2168 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2169 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2170 aom_highbd_12_sub_pixel_variance64x64,
2171 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002172 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002173 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2174 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002175
2176 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2177 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2178 aom_highbd_12_sub_pixel_variance16x16,
2179 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002180 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002181 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2182 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002183
2184 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2185 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2186 aom_highbd_12_sub_pixel_variance16x8,
2187 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002188 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002189 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2190 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002191
2192 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2193 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2194 aom_highbd_12_sub_pixel_variance8x16,
2195 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002196 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002197 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2198 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002199
2200 HIGHBD_BFP(
2201 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2202 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002203 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002204 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2205 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002206
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002207 HIGHBD_BFP(
2208 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2209 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2210 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002211 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2212 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002213
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002214 HIGHBD_BFP(
2215 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2216 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2217 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002218 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2219 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002220
2221 HIGHBD_BFP(
2222 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2223 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002224 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002225 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2226 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002227
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002228 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2229 aom_highbd_sad128x128_avg_bits12,
2230 aom_highbd_12_variance128x128,
2231 aom_highbd_12_sub_pixel_variance128x128,
2232 aom_highbd_12_sub_pixel_avg_variance128x128,
2233 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002234 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2235 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002236
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002237 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2238 aom_highbd_sad128x64_avg_bits12,
2239 aom_highbd_12_variance128x64,
2240 aom_highbd_12_sub_pixel_variance128x64,
2241 aom_highbd_12_sub_pixel_avg_variance128x64,
2242 aom_highbd_sad128x64x4d_bits12,
2243 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2244 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002245
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002246 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2247 aom_highbd_sad64x128_avg_bits12,
2248 aom_highbd_12_variance64x128,
2249 aom_highbd_12_sub_pixel_variance64x128,
2250 aom_highbd_12_sub_pixel_avg_variance64x128,
2251 aom_highbd_sad64x128x4d_bits12,
2252 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2253 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002254
David Barkerf19f35f2017-05-22 16:33:22 +01002255 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2256 aom_highbd_12_masked_sub_pixel_variance128x128)
2257 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2258 aom_highbd_12_masked_sub_pixel_variance128x64)
2259 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2260 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002261 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2262 aom_highbd_12_masked_sub_pixel_variance64x64)
2263 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2264 aom_highbd_12_masked_sub_pixel_variance64x32)
2265 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2266 aom_highbd_12_masked_sub_pixel_variance32x64)
2267 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2268 aom_highbd_12_masked_sub_pixel_variance32x32)
2269 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2270 aom_highbd_12_masked_sub_pixel_variance32x16)
2271 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2272 aom_highbd_12_masked_sub_pixel_variance16x32)
2273 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2274 aom_highbd_12_masked_sub_pixel_variance16x16)
2275 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2276 aom_highbd_12_masked_sub_pixel_variance8x16)
2277 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2278 aom_highbd_12_masked_sub_pixel_variance16x8)
2279 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2280 aom_highbd_12_masked_sub_pixel_variance8x8)
2281 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2282 aom_highbd_12_masked_sub_pixel_variance4x8)
2283 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2284 aom_highbd_12_masked_sub_pixel_variance8x4)
2285 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2286 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002287 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2288 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002289 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2290 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002291 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2292 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002293 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2294 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002295 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2296 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002297 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2298 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002299 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2300 aom_highbd_12_obmc_variance128x128,
2301 aom_highbd_12_obmc_sub_pixel_variance128x128)
2302 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2303 aom_highbd_12_obmc_variance128x64,
2304 aom_highbd_12_obmc_sub_pixel_variance128x64)
2305 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2306 aom_highbd_12_obmc_variance64x128,
2307 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002308 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2309 aom_highbd_12_obmc_variance64x64,
2310 aom_highbd_12_obmc_sub_pixel_variance64x64)
2311 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2312 aom_highbd_12_obmc_variance64x32,
2313 aom_highbd_12_obmc_sub_pixel_variance64x32)
2314 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2315 aom_highbd_12_obmc_variance32x64,
2316 aom_highbd_12_obmc_sub_pixel_variance32x64)
2317 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2318 aom_highbd_12_obmc_variance32x32,
2319 aom_highbd_12_obmc_sub_pixel_variance32x32)
2320 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2321 aom_highbd_12_obmc_variance32x16,
2322 aom_highbd_12_obmc_sub_pixel_variance32x16)
2323 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2324 aom_highbd_12_obmc_variance16x32,
2325 aom_highbd_12_obmc_sub_pixel_variance16x32)
2326 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2327 aom_highbd_12_obmc_variance16x16,
2328 aom_highbd_12_obmc_sub_pixel_variance16x16)
2329 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2330 aom_highbd_12_obmc_variance8x16,
2331 aom_highbd_12_obmc_sub_pixel_variance8x16)
2332 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2333 aom_highbd_12_obmc_variance16x8,
2334 aom_highbd_12_obmc_sub_pixel_variance16x8)
2335 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2336 aom_highbd_12_obmc_variance8x8,
2337 aom_highbd_12_obmc_sub_pixel_variance8x8)
2338 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2339 aom_highbd_12_obmc_variance4x8,
2340 aom_highbd_12_obmc_sub_pixel_variance4x8)
2341 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2342 aom_highbd_12_obmc_variance8x4,
2343 aom_highbd_12_obmc_sub_pixel_variance8x4)
2344 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2345 aom_highbd_12_obmc_variance4x4,
2346 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002347 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2348 aom_highbd_12_obmc_variance64x16,
2349 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002350 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2351 aom_highbd_12_obmc_variance16x64,
2352 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002353 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2354 aom_highbd_12_obmc_variance32x8,
2355 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002356 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2357 aom_highbd_12_obmc_variance8x32,
2358 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002359 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2360 aom_highbd_12_obmc_variance16x4,
2361 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002362 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2363 aom_highbd_12_obmc_variance4x16,
2364 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002365 break;
2366
2367 default:
2368 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002369 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002370 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002371 }
2372 }
2373}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002374
Yaowu Xuf883b422016-08-30 14:01:10 -07002375static void realloc_segmentation_maps(AV1_COMP *cpi) {
2376 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002377
2378 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002379 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002380 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002381 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002382
2383 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002384 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002385 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002386 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002387
2388 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002389 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002390 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002391 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392}
2393
Yaowu Xuf883b422016-08-30 14:01:10 -07002394void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2395 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002396 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002397 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002398 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002399 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002401 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2402 seq_params->bit_depth = oxcf->bit_depth;
2403 seq_params->color_primaries = oxcf->color_primaries;
2404 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2405 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2406 seq_params->monochrome = oxcf->monochrome;
2407 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2408 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002409
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002410 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2411 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002412
Hui Su2aa492c2019-03-12 15:18:18 -07002413 memcpy(cpi->target_seq_level_idx, oxcf->target_seq_level_idx,
2414 sizeof(cpi->target_seq_level_idx));
2415 cpi->keep_level_stats = 0;
2416 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
2417 if (cpi->target_seq_level_idx[i] < SEQ_LEVELS) {
Hui Su72ff0482019-05-28 14:07:37 -07002418 cpi->keep_level_stats |= 1u << i;
Hui Suc3a8d372019-05-28 15:52:45 -07002419 if (!cpi->level_info[i]) {
2420 CHECK_MEM_ERROR(cm, cpi->level_info[i],
2421 aom_calloc(1, sizeof(*cpi->level_info[i])));
2422 }
Hui Su2aa492c2019-03-12 15:18:18 -07002423 }
2424 }
kyslovabeeb7c2019-03-06 18:35:04 -08002425
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002426 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002427 cm->timing_info.num_units_in_display_tick =
2428 oxcf->timing_info.num_units_in_display_tick;
2429 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2430 cm->timing_info.equal_picture_interval =
2431 oxcf->timing_info.equal_picture_interval;
2432 cm->timing_info.num_ticks_per_picture =
2433 oxcf->timing_info.num_ticks_per_picture;
2434
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002435 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002436 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002437 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002438 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002439 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002440 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002441 cm->buffer_model.num_units_in_decoding_tick =
2442 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002443 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07002444 av1_set_aom_dec_model_info(&cm->buffer_model);
2445 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07002446 } else if (cm->timing_info_present &&
2447 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002448 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002449 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07002450 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002451 } else {
2452 cm->op_params[0].initial_display_delay =
2453 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002454 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002455
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002456 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002457
Yaowu Xuc27fc142016-08-22 16:08:15 -07002458 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002459 cpi->common.options = oxcf->cfg;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002460 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002461 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002462
Yaowu Xuf883b422016-08-30 14:01:10 -07002463 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002464 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2465 } else {
2466 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2467 }
2468
2469 cpi->refresh_last_frame = 1;
2470 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002471 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002472 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002473
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002474 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2475 ? REFRESH_FRAME_CONTEXT_DISABLED
2476 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002477 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002478 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002479
Alex Converse74ad0912017-07-18 10:22:58 -07002480 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002481 CHECK_MEM_ERROR(cm, x->palette_buffer,
2482 aom_memalign(16, sizeof(*x->palette_buffer)));
2483 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002484
2485 if (x->tmp_conv_dst == NULL) {
2486 CHECK_MEM_ERROR(
2487 cm, x->tmp_conv_dst,
2488 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002489 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002490 }
2491 for (int i = 0; i < 2; ++i) {
2492 if (x->tmp_obmc_bufs[i] == NULL) {
2493 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002494 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002495 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002496 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002497 }
2498 }
2499
Yaowu Xuf883b422016-08-30 14:01:10 -07002500 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002501 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002502
Yaowu Xuc27fc142016-08-22 16:08:15 -07002503 set_rc_buffer_sizes(rc, &cpi->oxcf);
2504
2505 // Under a configuration change, where maximum_buffer_size may change,
2506 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002507 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2508 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002509
2510 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002511 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002512
2513 // Set absolute upper and lower quality limits
2514 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2515 rc->best_quality = cpi->oxcf.best_allowed_q;
2516
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002517 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002518 cm->switchable_motion_mode = 1;
2519
Yaowu Xuc27fc142016-08-22 16:08:15 -07002520 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2521 cm->render_width = cpi->oxcf.render_width;
2522 cm->render_height = cpi->oxcf.render_height;
2523 } else {
2524 cm->render_width = cpi->oxcf.width;
2525 cm->render_height = cpi->oxcf.height;
2526 }
2527 cm->width = cpi->oxcf.width;
2528 cm->height = cpi->oxcf.height;
2529
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002530 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002531 // Superblock size should not be updated after the first key frame.
2532 if (!cpi->seq_params_locked) {
2533 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002534 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2535 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002536 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002537
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002538 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002539 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002540 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002541 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002542 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002543 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002544 realloc_segmentation_maps(cpi);
2545 cpi->initial_width = cpi->initial_height = 0;
2546 }
2547 }
2548 update_frame_size(cpi);
2549
2550 cpi->alt_ref_source = NULL;
2551 rc->is_src_frame_alt_ref = 0;
2552
Yaowu Xuc27fc142016-08-22 16:08:15 -07002553 set_tile_info(cpi);
2554
2555 cpi->ext_refresh_frame_flags_pending = 0;
2556 cpi->ext_refresh_frame_context_pending = 0;
2557
Yaowu Xuc27fc142016-08-22 16:08:15 -07002558 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002559
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002560 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002561 // This should not be called after the first key frame.
2562 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002563 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002564 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Andrey Norkin26495512018-06-20 17:13:11 -07002565 init_seq_coding_tools(&cm->seq_params, cm, oxcf);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002566 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002567}
2568
Yaowu Xuf883b422016-08-30 14:01:10 -07002569AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2570 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002571 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002572 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2573 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002574
2575 if (!cm) return NULL;
2576
Yaowu Xuf883b422016-08-30 14:01:10 -07002577 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002578
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002579 // The jmp_buf is valid only for the duration of the function that calls
2580 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2581 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002582 if (setjmp(cm->error.jmp)) {
2583 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002584 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002585 return 0;
2586 }
2587
2588 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002589 cm->alloc_mi = enc_alloc_mi;
2590 cm->free_mi = enc_free_mi;
2591 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002592
Angie Chianga5d96c42016-10-21 16:16:56 -07002593 CHECK_MEM_ERROR(cm, cm->fc,
2594 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002595 CHECK_MEM_ERROR(
2596 cm, cm->default_frame_context,
2597 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002598 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002599 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002600
2601 cpi->resize_state = 0;
2602 cpi->resize_avg_qp = 0;
2603 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002604
Yaowu Xuc27fc142016-08-22 16:08:15 -07002605 cpi->common.buffer_pool = pool;
2606
2607 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002608 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002609
David Turnerd2a592e2018-11-16 14:59:31 +00002610 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002611 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002612 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002613 cpi->partition_search_skippable_frame = 0;
2614 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00002615 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002616 realloc_segmentation_maps(cpi);
2617
Jingning Hanf050fc12018-03-09 14:53:33 -08002618 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2619 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002620
Yaowu Xuc27fc142016-08-22 16:08:15 -07002621 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2622 i++) {
2623 CHECK_MEM_ERROR(
2624 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002625 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002626 }
2627
Yaowu Xuc27fc142016-08-22 16:08:15 -07002628 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002629
Hui Su8ea87322019-03-29 14:44:32 -07002630 av1_init_level_info(cpi->level_info);
chiyotsaid16a2572019-03-05 17:48:10 -08002631
Yaowu Xuc27fc142016-08-22 16:08:15 -07002632 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2633#if CONFIG_INTERNAL_STATS
2634 cpi->b_calculate_blockiness = 1;
2635 cpi->b_calculate_consistency = 1;
2636 cpi->total_inconsistency = 0;
2637 cpi->psnr.worst = 100.0;
2638 cpi->worst_ssim = 100.0;
2639
2640 cpi->count = 0;
2641 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08002642#if CONFIG_SPEED_STATS
2643 cpi->tx_search_count = 0;
2644#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002645
2646 if (cpi->b_calculate_psnr) {
2647 cpi->total_sq_error = 0;
2648 cpi->total_samples = 0;
2649 cpi->tot_recode_hits = 0;
2650 cpi->summed_quality = 0;
2651 cpi->summed_weights = 0;
2652 }
2653
2654 cpi->fastssim.worst = 100.0;
2655 cpi->psnrhvs.worst = 100.0;
2656
2657 if (cpi->b_calculate_blockiness) {
2658 cpi->total_blockiness = 0;
2659 cpi->worst_blockiness = 0.0;
2660 }
2661
2662 if (cpi->b_calculate_consistency) {
2663 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002664 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665 cpi->common.mi_rows * cpi->common.mi_cols));
2666 cpi->worst_consistency = 100.0;
2667 }
2668#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002669#if CONFIG_ENTROPY_STATS
2670 av1_zero(aggregate_fc);
2671#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672
2673 cpi->first_time_stamp_ever = INT64_MAX;
2674
Jingning Hanf050fc12018-03-09 14:53:33 -08002675 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2676 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2677 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2678 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002679
Yaowu Xuc27fc142016-08-22 16:08:15 -07002680#ifdef OUTPUT_YUV_SKINMAP
2681 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2682#endif
2683#ifdef OUTPUT_YUV_REC
2684 yuv_rec_file = fopen("rec.yuv", "wb");
2685#endif
2686
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002687#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -07002688 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002689 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002690 } else if (oxcf->pass == 2) {
2691 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2692 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2693
Yaowu Xuc27fc142016-08-22 16:08:15 -07002694 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2695 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2696 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2697
Yaowu Xuf883b422016-08-30 14:01:10 -07002698 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002699 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002700#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002701
Jingning Hand064cf02017-06-01 10:00:39 -07002702 CHECK_MEM_ERROR(
2703 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002704 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002705 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002706 CHECK_MEM_ERROR(
2707 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002708 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002709 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002710
2711 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2712 (int32_t *)aom_memalign(
2713 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2714
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302715 CHECK_MEM_ERROR(
2716 cm, cpi->td.mb.inter_modes_info,
2717 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302718
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302719 for (int x = 0; x < 2; x++)
2720 for (int y = 0; y < 2; y++)
2721 CHECK_MEM_ERROR(
2722 cm, cpi->td.mb.hash_value_buffer[x][y],
2723 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2724 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2725
2726 cpi->td.mb.g_crc_initialized = 0;
2727
Jingning Hand064cf02017-06-01 10:00:39 -07002728 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2729 (int32_t *)aom_memalign(
2730 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2731
David Turner04b70d82019-01-24 15:39:19 +00002732 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
2733 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002734
Yue Chenc9b23e02019-04-10 16:54:03 -07002735 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chen7cae98f2018-08-24 10:43:16 -07002736 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2737 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2738
2739 CHECK_MEM_ERROR(cm, cpi->tpl_stats[frame].tpl_stats_ptr,
2740 aom_calloc(mi_rows * mi_cols,
2741 sizeof(*cpi->tpl_stats[frame].tpl_stats_ptr)));
2742 cpi->tpl_stats[frame].is_valid = 0;
2743 cpi->tpl_stats[frame].width = mi_cols;
2744 cpi->tpl_stats[frame].height = mi_rows;
2745 cpi->tpl_stats[frame].stride = mi_cols;
2746 cpi->tpl_stats[frame].mi_rows = cm->mi_rows;
2747 cpi->tpl_stats[frame].mi_cols = cm->mi_cols;
2748 }
2749
chiyotsai9c484b32019-03-07 16:01:50 -08002750#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08002751 av1_zero(cpi->partition_stats);
2752#endif
2753
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002754#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2755 cpi->fn_ptr[BT].sdf = SDF; \
2756 cpi->fn_ptr[BT].sdaf = SDAF; \
2757 cpi->fn_ptr[BT].vf = VF; \
2758 cpi->fn_ptr[BT].svf = SVF; \
2759 cpi->fn_ptr[BT].svaf = SVAF; \
2760 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2761 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002762 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002763
Cheng Chenf78632e2017-10-20 15:30:51 -07002764 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002765 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002766 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
2767 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002768
2769 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002770 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002771 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
2772 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002773
2774 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002775 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002776 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
2777 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002778
2779 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002780 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002781 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
2782 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002783
2784 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002785 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002786 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
2787 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002788
2789 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002790 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002791 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
2792 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002793
Cheng Chenf78632e2017-10-20 15:30:51 -07002794 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2795 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002796 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
2797 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002798
2799 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002800 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002801 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
2802 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002803
2804 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002805 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002806 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
2807 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002808
2809 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002810 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002811 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
2812 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002813
2814 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002815 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002816 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
2817 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002818
2819 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002820 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002821 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
2822 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002823
2824 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002825 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002826 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
2827 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002828
2829 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2830 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002831 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
2832 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002833
2834 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2835 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002836 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
2837 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002838
2839 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2840 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002841 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
2842 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002843
2844 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002845 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002846 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
2847 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002848
2849 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002850 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002851 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
2852 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002853
2854 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002855 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002856 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002857
2858 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002859 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002860 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002861
2862 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002863 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002864 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002865
2866 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002867 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002868 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002869
Yaowu Xuc27fc142016-08-22 16:08:15 -07002870#define OBFP(BT, OSDF, OVF, OSVF) \
2871 cpi->fn_ptr[BT].osdf = OSDF; \
2872 cpi->fn_ptr[BT].ovf = OVF; \
2873 cpi->fn_ptr[BT].osvf = OSVF;
2874
Yaowu Xuf883b422016-08-30 14:01:10 -07002875 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2876 aom_obmc_sub_pixel_variance128x128)
2877 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2878 aom_obmc_sub_pixel_variance128x64)
2879 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2880 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002881 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2882 aom_obmc_sub_pixel_variance64x64)
2883 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2884 aom_obmc_sub_pixel_variance64x32)
2885 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2886 aom_obmc_sub_pixel_variance32x64)
2887 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2888 aom_obmc_sub_pixel_variance32x32)
2889 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2890 aom_obmc_sub_pixel_variance32x16)
2891 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2892 aom_obmc_sub_pixel_variance16x32)
2893 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2894 aom_obmc_sub_pixel_variance16x16)
2895 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2896 aom_obmc_sub_pixel_variance16x8)
2897 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2898 aom_obmc_sub_pixel_variance8x16)
2899 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2900 aom_obmc_sub_pixel_variance8x8)
2901 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2902 aom_obmc_sub_pixel_variance4x8)
2903 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2904 aom_obmc_sub_pixel_variance8x4)
2905 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2906 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002907 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2908 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002909 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2910 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002911 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2912 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002913 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2914 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002915 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2916 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002917 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2918 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002919
David Barkerf19f35f2017-05-22 16:33:22 +01002920#define MBFP(BT, MCSDF, MCSVF) \
2921 cpi->fn_ptr[BT].msdf = MCSDF; \
2922 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002923
David Barkerf19f35f2017-05-22 16:33:22 +01002924 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2925 aom_masked_sub_pixel_variance128x128)
2926 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2927 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002928 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2929 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2930 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2931 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2932 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2933 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2934 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2935 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2936 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2937 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2938 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2939 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2940 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002941
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002942 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2943
2944 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2945
2946 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2947
2948 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002949
2950 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2951
2952 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002953
Yaowu Xuc27fc142016-08-22 16:08:15 -07002954 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002955
Yaowu Xuf883b422016-08-30 14:01:10 -07002956 /* av1_init_quantizer() is first called here. Add check in
2957 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002958 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002959 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002960 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002961 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08002962 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963
Yaowu Xuf883b422016-08-30 14:01:10 -07002964 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07002965 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002966 cm->superres_upscaled_width = oxcf->width;
2967 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002968 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969
2970 cm->error.setjmp = 0;
2971
2972 return cpi;
2973}
2974
Urvang Joshiee2c8112018-05-04 14:53:15 -07002975#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002976#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2977
2978#define SNPRINT2(H, T, V) \
2979 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07002980#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981
Yaowu Xuf883b422016-08-30 14:01:10 -07002982void av1_remove_compressor(AV1_COMP *cpi) {
2983 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002984 unsigned int i;
2985 int t;
2986
2987 if (!cpi) return;
2988
2989 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002990 const int num_planes = av1_num_planes(cm);
2991
David Turnerd2a592e2018-11-16 14:59:31 +00002992 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002993#if CONFIG_ENTROPY_STATS
2994 if (cpi->oxcf.pass != 1) {
2995 fprintf(stderr, "Writing counts.stt\n");
2996 FILE *f = fopen("counts.stt", "wb");
2997 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
2998 fclose(f);
2999 }
3000#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003001#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003002 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003003
3004 if (cpi->oxcf.pass != 1) {
3005 char headings[512] = { 0 };
3006 char results[512] = { 0 };
3007 FILE *f = fopen("opsnr.stt", "a");
3008 double time_encoded =
3009 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3010 10000000.000;
3011 double total_encode_time =
3012 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3013 const double dr =
3014 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3015 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3016 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3017 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3018
3019 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003020 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003021 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3022 const double total_ssim =
3023 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3024 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003025 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003026 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003027 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08003028 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003029 snprintf(results, sizeof(results),
3030 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3031 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003032 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08003033 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003034 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3035 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3036 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3037 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003038 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003039 cpi->psnr.stat[STAT_Y] / cpi->count,
3040 cpi->psnr.stat[STAT_U] / cpi->count,
3041 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003042
3043 if (cpi->b_calculate_blockiness) {
3044 SNPRINT(headings, "\t Block\tWstBlck");
3045 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3046 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3047 }
3048
3049 if (cpi->b_calculate_consistency) {
3050 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003051 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003052 (double)cpi->total_inconsistency);
3053
3054 SNPRINT(headings, "\tConsist\tWstCons");
3055 SNPRINT2(results, "\t%7.3f", consistency);
3056 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3057 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003058 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3059 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003060 rate_err, fabs(rate_err));
3061 }
3062
3063 fclose(f);
3064 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003065#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003066#if CONFIG_SPEED_STATS
3067 if (cpi->oxcf.pass != 1) {
3068 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3069 }
3070#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003071
chiyotsai9c484b32019-03-07 16:01:50 -08003072#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003073 if (cpi->oxcf.pass != 1) {
3074 av1_print_partition_stats(&cpi->partition_stats);
3075 }
3076#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003077 }
3078
Yue Chenc9b23e02019-04-10 16:54:03 -07003079 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chen7cae98f2018-08-24 10:43:16 -07003080 aom_free(cpi->tpl_stats[frame].tpl_stats_ptr);
3081 cpi->tpl_stats[frame].is_valid = 0;
3082 }
3083
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303084 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003085 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003086 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3087
3088 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003089 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003090
3091 // Deallocate allocated thread data.
Ravi Chaudhary1cf7d162018-10-09 17:00:43 +05303092 if (cpi->row_mt == 1) aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303093 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003094 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003095 aom_free(thread_data->td->tmp_conv_dst);
3096 for (int j = 0; j < 2; ++j) {
3097 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3098 }
Jingning Hand064cf02017-06-01 10:00:39 -07003099 aom_free(thread_data->td->above_pred_buf);
3100 aom_free(thread_data->td->left_pred_buf);
3101 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003102
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303103 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003104 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303105 for (int y = 0; y < 2; y++) {
3106 aom_free(thread_data->td->hash_value_buffer[x][y]);
3107 thread_data->td->hash_value_buffer[x][y] = NULL;
3108 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003109 }
Jingning Hand064cf02017-06-01 10:00:39 -07003110 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003111 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003112 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003113 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003114 }
3115 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303116#if CONFIG_MULTITHREAD
3117 if (cpi->row_mt == 1) {
3118 if (cpi->row_mt_mutex_ != NULL) {
3119 pthread_mutex_destroy(cpi->row_mt_mutex_);
3120 aom_free(cpi->row_mt_mutex_);
3121 }
3122 }
3123#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303124 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003125 aom_free(cpi->tile_thr_data);
3126 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003127
Deepa K G964e72e2018-05-16 16:56:01 +05303128 if (cpi->num_workers > 1) {
3129 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303130 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303131 }
3132
Yaowu Xuc27fc142016-08-22 16:08:15 -07003133 dealloc_compressor_data(cpi);
3134
3135 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3136 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003137 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003138 }
3139
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003140#if CONFIG_INTERNAL_STATS
3141 aom_free(cpi->ssim_vars);
3142 cpi->ssim_vars = NULL;
3143#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003144
Yaowu Xuf883b422016-08-30 14:01:10 -07003145 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003146 for (i = 0; i < FRAME_BUFFERS; ++i) {
3147 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3148 }
Yaowu Xu74e63352019-05-06 09:21:33 -07003149#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003150 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003151#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003152 av1_free_ref_frame_buffers(cm->buffer_pool);
3153 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003154
3155#ifdef OUTPUT_YUV_SKINMAP
3156 fclose(yuv_skinmap_file);
3157#endif
3158#ifdef OUTPUT_YUV_REC
3159 fclose(yuv_rec_file);
3160#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003161}
3162
Yaowu Xuf883b422016-08-30 14:01:10 -07003163static void generate_psnr_packet(AV1_COMP *cpi) {
3164 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003165 int i;
3166 PSNR_STATS psnr;
David Turnerc29e1a92018-12-06 14:10:14 +00003167 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003168 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003169
3170 for (i = 0; i < 4; ++i) {
3171 pkt.data.psnr.samples[i] = psnr.samples[i];
3172 pkt.data.psnr.sse[i] = psnr.sse[i];
3173 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3174 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003175 pkt.kind = AOM_CODEC_PSNR_PKT;
3176 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003177}
3178
Yaowu Xuf883b422016-08-30 14:01:10 -07003179int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003180 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3181
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003182 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003183 return 0;
3184}
3185
Thomas Daede497d1952017-08-08 17:33:06 -07003186int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3187 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003188 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003189 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003190 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003191 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003192 return 0;
3193 } else {
3194 return -1;
3195 }
3196}
3197
Thomas Daede497d1952017-08-08 17:33:06 -07003198int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3199 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003200 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003201 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003202 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003203 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003204 return 0;
3205 } else {
3206 return -1;
3207 }
3208}
3209
3210int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003211 cpi->ext_refresh_frame_context = update;
3212 cpi->ext_refresh_frame_context_pending = 1;
3213 return 0;
3214}
3215
3216#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3217// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3218// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3219// not denoise the UV channels at this time. If ever we implement UV channel
3220// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003221void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003222 uint8_t *src = s->y_buffer;
3223 int h = s->y_height;
3224
3225 do {
3226 fwrite(src, s->y_width, 1, f);
3227 src += s->y_stride;
3228 } while (--h);
3229
3230 src = s->u_buffer;
3231 h = s->uv_height;
3232
3233 do {
3234 fwrite(src, s->uv_width, 1, f);
3235 src += s->uv_stride;
3236 } while (--h);
3237
3238 src = s->v_buffer;
3239 h = s->uv_height;
3240
3241 do {
3242 fwrite(src, s->uv_width, 1, f);
3243 src += s->uv_stride;
3244 } while (--h);
3245}
3246#endif
3247
Yaowu Xuc27fc142016-08-22 16:08:15 -07003248#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003249void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003250 uint8_t *src = s->y_buffer;
3251 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003252 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003253 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3254 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3255
3256 do {
3257 fwrite(src16, s->y_width, 2, yuv_rec_file);
3258 src16 += s->y_stride;
3259 } while (--h);
3260
3261 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3262 h = s->uv_height;
3263
3264 do {
3265 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3266 src16 += s->uv_stride;
3267 } while (--h);
3268
3269 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3270 h = s->uv_height;
3271
3272 do {
3273 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3274 src16 += s->uv_stride;
3275 } while (--h);
3276
3277 fflush(yuv_rec_file);
3278 return;
3279 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003280
3281 do {
3282 fwrite(src, s->y_width, 1, yuv_rec_file);
3283 src += s->y_stride;
3284 } while (--h);
3285
3286 src = s->u_buffer;
3287 h = s->uv_height;
3288
3289 do {
3290 fwrite(src, s->uv_width, 1, yuv_rec_file);
3291 src += s->uv_stride;
3292 } while (--h);
3293
3294 src = s->v_buffer;
3295 h = s->uv_height;
3296
3297 do {
3298 fwrite(src, s->uv_width, 1, yuv_rec_file);
3299 src += s->uv_stride;
3300 } while (--h);
3301
3302 fflush(yuv_rec_file);
3303}
3304#endif // OUTPUT_YUV_REC
3305
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003306#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003307static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3308 int i;
3309 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003310 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003311 AV1_COMMON *const cm = &cpi->common;
3312 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3313 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003314 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003315 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003316 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003317 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003318 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003319 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003320 // TODO(sarahparker): The earlier condition for recoding here was:
3321 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3322 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003323 }
3324 }
3325 return recode;
3326}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003327
Yaowu Xuc27fc142016-08-22 16:08:15 -07003328// Function to test for conditions that indicate we should loop
3329// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003330static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3331 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003332 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003333 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003334 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3335 int force_recode = 0;
3336
3337 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3338 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3339 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003340 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3341 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3342 (rc->projected_frame_size < low_limit && q > minq)) {
3343 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003344 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003345 // Deal with frame undershoot and whether or not we are
3346 // below the automatically set cq level.
3347 if (q > oxcf->cq_level &&
3348 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3349 force_recode = 1;
3350 }
3351 }
3352 }
3353 return force_recode;
3354}
3355
Cheng Chen46f30c72017-09-07 11:13:33 -07003356static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003357 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003358 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003359 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003360 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003361 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3362 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003363 };
3364
3365 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003366 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003367 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3368 BufferPool *const pool = cm->buffer_pool;
3369 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003370 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003371
3372 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003373 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003374 continue;
3375 }
3376
Yaowu Xuc27fc142016-08-22 16:08:15 -07003377 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003378 // Replace the reference buffer with a copy having a thicker border,
3379 // if the reference buffer is higher resolution than the current
3380 // frame, and the border is thin.
3381 if ((ref->y_crop_width > cm->width ||
3382 ref->y_crop_height > cm->height) &&
3383 ref->border < AOM_BORDER_IN_PIXELS) {
3384 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3385 if (aom_yv12_realloc_with_new_border(
3386 &ref_fb->buf, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3387 num_planes) != 0) {
3388 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3389 "Failed to allocate frame buffer");
3390 }
3391 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003392 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003393 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3394 if (new_fb == NULL) {
3395 const int new_fb_idx = get_free_fb(cm);
3396 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003397 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3398 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003399 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003400 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003401 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003402 }
David Turnere7ebf902018-12-04 14:04:55 +00003403
3404 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3405 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003406 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003407 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003408 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003409 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003410 cm->byte_alignment, NULL, NULL, NULL)) {
3411 if (force_scaling) {
3412 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003413 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003414 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003415 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003417 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003418 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003419 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3420 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003421 alloc_frame_mvs(cm, new_fb);
3422 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003423 } else {
David Turnera21966b2018-12-05 14:48:49 +00003424 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003425 buf->buf.y_crop_width = ref->y_crop_width;
3426 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003427 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003428 ++buf->ref_count;
3429 }
3430 } else {
David Turnere7ebf902018-12-04 14:04:55 +00003431 if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003432 }
3433 }
3434}
3435
Yaowu Xuf883b422016-08-30 14:01:10 -07003436static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003437 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003438 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3439 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3440 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003441 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003442 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003443 }
3444 }
3445}
3446
Yaowu Xuf883b422016-08-30 14:01:10 -07003447static void set_mv_search_params(AV1_COMP *cpi) {
3448 const AV1_COMMON *const cm = &cpi->common;
3449 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003450
3451 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003452 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003453
3454 if (cpi->sf.mv.auto_mv_step_size) {
3455 if (frame_is_intra_only(cm)) {
3456 // Initialize max_mv_magnitude for use in the first INTER frame
3457 // after a key/intra-only frame.
3458 cpi->max_mv_magnitude = max_mv_def;
3459 } else {
3460 if (cm->show_frame) {
3461 // Allow mv_steps to correspond to twice the max mv magnitude found
3462 // in the previous frame, capped by the default max_mv_magnitude based
3463 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003464 cpi->mv_step_param = av1_init_search_range(
3465 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003466 }
3467 cpi->max_mv_magnitude = 0;
3468 }
3469 }
3470}
3471
Hui Subdf0c992019-02-14 14:52:41 -08003472static void set_screen_content_options(AV1_COMP *cpi) {
3473 AV1_COMMON *cm = &cpi->common;
3474
3475 if (cm->seq_params.force_screen_content_tools != 2) {
3476 cm->allow_screen_content_tools = cm->allow_intrabc =
3477 cm->seq_params.force_screen_content_tools;
3478 return;
3479 }
3480
3481 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3482 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3483 return;
3484 }
3485
3486 // Estimate if the source frame is screen content, based on the portion of
3487 // blocks that have few luma colors.
3488 const uint8_t *src = cpi->source->y_buffer;
3489 assert(src != NULL);
3490 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
3491 const int stride = cpi->source->y_stride;
3492 const int width = cpi->source->y_width;
3493 const int height = cpi->source->y_height;
3494 const int bd = cm->seq_params.bit_depth;
3495 const int blk_w = 16;
3496 const int blk_h = 16;
3497 // These threshold values are selected experimentally.
3498 const int color_thresh = 4;
3499 const unsigned int var_thresh = 0;
3500 // Counts of blocks with no more than color_thresh colors.
3501 int counts_1 = 0;
3502 // Counts of blocks with no more than color_thresh colors and variance larger
3503 // than var_thresh.
3504 int counts_2 = 0;
3505
3506 for (int r = 0; r + blk_h <= height; r += blk_h) {
3507 for (int c = 0; c + blk_w <= width; c += blk_w) {
3508 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3509 const uint8_t *const this_src = src + r * stride + c;
3510 const int n_colors =
3511 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3512 count_buf)
3513 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3514 if (n_colors > 1 && n_colors <= color_thresh) {
3515 ++counts_1;
3516 struct buf_2d buf;
3517 buf.stride = stride;
3518 buf.buf = (uint8_t *)this_src;
3519 const unsigned int var =
3520 use_hbd
3521 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3522 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3523 if (var > var_thresh) ++counts_2;
3524 }
3525 }
3526 }
3527
3528 // The threshold values are selected experimentally.
3529 cm->allow_screen_content_tools =
3530 counts_1 * blk_h * blk_w * 10 > width * height;
3531 // IntraBC would force loop filters off, so we use more strict rules that also
3532 // requires that the block has high variance.
3533 cm->allow_intrabc = cm->allow_screen_content_tools &&
Hui Su64a2ffb2019-04-11 16:47:13 -07003534 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08003535}
3536
Yaowu Xuf883b422016-08-30 14:01:10 -07003537static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003538 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003539 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003540 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003541 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003542 }
3543 cpi->global_motion_search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07003544
3545 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
3546 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
3547
David Turner04b70d82019-01-24 15:39:19 +00003548 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07003549 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003550 cm->interp_filter = SWITCHABLE;
3551 cm->switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552}
3553
Yaowu Xuf883b422016-08-30 14:01:10 -07003554static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003555 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003556 AV1_COMMON *const cm = &cpi->common;
3557 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003558
3559 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00003560 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003561
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003562 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003563 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3564 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003565
James Zern01a9d702017-08-25 19:09:33 +00003566 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003567 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003568 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003569 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003570
3571 // Configure experimental use of segmentation for enhanced coding of
3572 // static regions if indicated.
3573 // Only allowed in the second pass of a two pass encode, as it requires
3574 // lagged coding, and if the relevant speed feature flag is set.
3575 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3576 configure_static_seg_features(cpi);
3577}
3578
Yaowu Xuf883b422016-08-30 14:01:10 -07003579static void init_motion_estimation(AV1_COMP *cpi) {
chiyotsai836b69b2019-04-09 13:41:24 -07003580 const int y_stride = cpi->scaled_source.y_stride;
3581 const int y_stride_src = (cpi->oxcf.resize_mode || cpi->oxcf.superres_mode)
3582 ? y_stride
3583 : cpi->lookahead->buf->img.y_stride;
3584 // Update if ss_cfg is uninitialized or the current frame has a new stride
3585 const int should_update = !cpi->ss_cfg[SS_CFG_SRC].stride ||
3586 !cpi->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
3587 (y_stride != cpi->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003588
chiyotsai836b69b2019-04-09 13:41:24 -07003589 if (!should_update) {
3590 return;
3591 }
3592
3593 if (cpi->sf.mv.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303594 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3595 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
3596 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07003597 } else {
3598 // Update the offsets in search_sites as y_stride can change due to scaled
3599 // references. This update allows NSTEP to be used on scaled references as
3600 // long as sf.mv.search_method is not DIAMOND. Currently in the codebae,
3601 // sf.mv.search_method is never set to DIAMOND.
3602 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3603 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003604 }
3605}
3606
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003607#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003608static void set_restoration_unit_size(int width, int height, int sx, int sy,
3609 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003610 (void)width;
3611 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003612 (void)sx;
3613 (void)sy;
3614#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3615 int s = AOMMIN(sx, sy);
3616#else
3617 int s = 0;
3618#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3619
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003620 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003621 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003622 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003623 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003624 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3625 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003626}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003627
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303628static void init_ref_frame_bufs(AV1_COMP *cpi) {
3629 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07003630 int i;
3631 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00003632 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07003633 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00003634 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07003635 }
3636 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07003637 pool->frame_bufs[i].ref_count = 0;
3638 }
RogerZhou86902d02018-02-28 15:29:16 -08003639 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003640 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303641 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08003642 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003643 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003644}
3645
Yaowu Xud3e7c682017-12-21 14:08:25 -08003646static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003647 int subsampling_x, int subsampling_y) {
3648 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003649 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07003650
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003651 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3652 seq_params->subsampling_x != subsampling_x ||
3653 seq_params->subsampling_y != subsampling_y) {
3654 seq_params->subsampling_x = subsampling_x;
3655 seq_params->subsampling_y = subsampling_y;
3656 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003657
3658 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303659 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07003660 alloc_util_frame_buffers(cpi);
3661
3662 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3663
3664 cpi->initial_width = cm->width;
3665 cpi->initial_height = cm->height;
3666 cpi->initial_mbs = cm->MBs;
3667 }
3668}
3669
3670// Returns 1 if the assigned width or height was <= 0.
3671static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3672 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003673 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003674 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3675 cm->seq_params.subsampling_x,
3676 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003677
3678 if (width <= 0 || height <= 0) return 1;
3679
3680 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003681 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003682
3683 if (cpi->initial_width && cpi->initial_height &&
3684 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3685 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003686 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003687 alloc_compressor_data(cpi);
3688 realloc_segmentation_maps(cpi);
3689 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003690 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003691 update_frame_size(cpi);
3692
3693 return 0;
3694}
3695
David Turner475a3132019-01-18 15:17:17 +00003696void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003697 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003698 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003699 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003700 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003701 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003702
Fergus Simpsonbc189932017-05-16 17:02:39 -07003703 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003704 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003705 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003706 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003707 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07003708 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003709 }
3710
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003711 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003712 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003713 }
3714
David Turnere7ebf902018-12-04 14:04:55 +00003715 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003716
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303717 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05303718 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3719 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303720 cm->num_allocated_above_contexts < cm->tile_rows) {
3721 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3722 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3723 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3724 "Failed to allocate context buffers");
3725 }
3726
Yaowu Xuc27fc142016-08-22 16:08:15 -07003727 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003728 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00003729 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
3730 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05303731 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003732 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003733 "Failed to allocate frame buffer");
3734
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003735 const int frame_width = cm->superres_upscaled_width;
3736 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003737 set_restoration_unit_size(frame_width, frame_height,
3738 seq_params->subsampling_x,
3739 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003740 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003741 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003742
3743 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003744 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003745 init_motion_estimation(cpi);
3746
3747 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00003748 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00003749 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00003750 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
3751 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00003752 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003753 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00003754 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003755 }
3756 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003757
Hui Su5ebd8702018-01-08 18:09:20 -08003758 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003759 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003760
3761 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3762}
3763
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003764static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3765 // Choose an arbitrary random number
3766 static unsigned int seed = 56789;
3767 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003768 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3769 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003770
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07003771 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003772 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003773 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003774 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003775 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003776 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003777 else
Urvang Joshide71d142017-10-05 12:12:15 -07003778 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003779 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003780 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003781 default: assert(0);
3782 }
Urvang Joshide71d142017-10-05 12:12:15 -07003783 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003784}
3785
Debargha Mukherjeee3cd5a52018-11-29 11:05:22 -08003786#define ENERGY_BY_Q2_THRESH 0.01
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003787#define ENERGY_BY_AC_THRESH 0.2
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003788
3789static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003790 double threshq,
3791 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003792 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003793 const double tq = threshq * q * q;
3794 const double tp = threshp * energy[1];
3795 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003796 int k;
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003797 for (k = 16; k > 8; --k) {
3798 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003799 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003800 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003801}
3802
3803static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003804 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003805 analyze_hor_freq(cpi, energy);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003806 /*
3807 printf("\nenergy = [");
3808 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
3809 printf("]\n");
3810 */
3811 return get_superres_denom_from_qindex_energy(
3812 qindex, energy, ENERGY_BY_Q2_THRESH, ENERGY_BY_AC_THRESH);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08003813}
3814
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003815static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
3816 // Choose an arbitrary random number
3817 static unsigned int seed = 34567;
3818 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003819 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3820 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07003821
3822 // Make sure that superres mode of the frame is consistent with the
3823 // sequence-level flag.
3824 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
3825 cpi->common.seq_params.enable_superres));
3826 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
3827 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003828
3829 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003830 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003831 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003832 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003833 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003834 else
Urvang Joshide71d142017-10-05 12:12:15 -07003835 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003836 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003837 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003838 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003839 // Do not use superres when screen content tools are used.
3840 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08003841 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
3842 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi2c92b072018-03-19 17:23:31 -07003843 int bottom_index, top_index;
3844 const int q = av1_rc_pick_q_and_bounds(
3845 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08003846
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003847 const int qthresh = (frame_is_intra_only(&cpi->common))
3848 ? oxcf->superres_kf_qthresh
3849 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08003850 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07003851 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003852 } else {
Debargha Mukherjeee3cd5a52018-11-29 11:05:22 -08003853 new_denom = get_superres_denom_for_qindex(cpi, q);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003854 }
3855 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003856 }
Urvang Joshi36a83732019-01-31 15:31:57 -08003857 case SUPERRES_AUTO: {
3858 // Don't use when screen content tools are used.
3859 if (cpi->common.allow_screen_content_tools) break;
3860 // Don't use for inter frames.
3861 if (!frame_is_intra_only(&cpi->common)) break;
Urvang Joshibf5e7702019-04-01 11:55:09 -07003862 // Don't use for keyframes that can be used as references, except when
3863 // using AOM_Q mode.
3864 if (cpi->rc.frames_to_key != 1 && cpi->oxcf.rc_mode != AOM_Q) break;
Urvang Joshi36a83732019-01-31 15:31:57 -08003865
3866 // Now decide the use of superres based on 'q'.
3867 int bottom_index, top_index;
3868 const int q = av1_rc_pick_q_and_bounds(
3869 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
3870
3871 const int qthresh = 128;
3872 if (q <= qthresh) {
3873 new_denom = SCALE_NUMERATOR;
3874 } else {
3875 new_denom = get_superres_denom_for_qindex(cpi, q);
3876 }
3877 break;
3878 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003879 default: assert(0);
3880 }
Urvang Joshide71d142017-10-05 12:12:15 -07003881 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003882}
3883
Urvang Joshide71d142017-10-05 12:12:15 -07003884static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
3885 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
3886}
3887
3888static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08003889 // Only need to check the width, as scaling is horizontal only.
3890 (void)oheight;
3891 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07003892}
3893
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003894static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003895 SUPERRES_MODE superres_mode, int owidth,
3896 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07003897 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003898 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07003899 }
3900
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003901 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07003902 int resize_denom =
3903 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
3904 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
3905
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003906 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003907 // Alter superres scale as needed to enforce conformity.
3908 rsz->superres_denom =
3909 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
3910 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3911 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003912 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003913 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003914 // Alter resize scale as needed to enforce conformity.
3915 resize_denom =
3916 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003917 rsz->resize_width = owidth;
3918 rsz->resize_height = oheight;
3919 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003920 resize_denom);
3921 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3922 if (resize_denom > SCALE_NUMERATOR) {
3923 --resize_denom;
3924 rsz->resize_width = owidth;
3925 rsz->resize_height = oheight;
3926 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
3927 resize_denom);
3928 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003929 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003930 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003931 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003932 do {
Urvang Joshide71d142017-10-05 12:12:15 -07003933 if (resize_denom > rsz->superres_denom)
3934 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003935 else
Urvang Joshide71d142017-10-05 12:12:15 -07003936 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003937 rsz->resize_width = owidth;
3938 rsz->resize_height = oheight;
3939 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003940 resize_denom);
3941 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
3942 (resize_denom > SCALE_NUMERATOR ||
3943 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08003944 } else { // We are allowed to alter neither resize scale nor superres
3945 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003946 return 0;
3947 }
Urvang Joshide71d142017-10-05 12:12:15 -07003948 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003949}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003950
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003951// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00003952static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003953 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08003954 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshide71d142017-10-05 12:12:15 -07003955 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003956 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003957 if (cpi->resize_pending_width && cpi->resize_pending_height) {
3958 rsz.resize_width = cpi->resize_pending_width;
3959 rsz.resize_height = cpi->resize_pending_height;
3960 cpi->resize_pending_width = cpi->resize_pending_height = 0;
3961 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07003962 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003963 rsz.resize_width = cpi->oxcf.width;
3964 rsz.resize_height = cpi->oxcf.height;
3965 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003966 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003967 }
Urvang Joshide71d142017-10-05 12:12:15 -07003968 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003969 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
3970 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003971 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003972 return rsz;
3973}
3974
Urvang Joshi22b150b2019-01-10 14:32:32 -08003975static void setup_frame_size_from_params(AV1_COMP *cpi,
3976 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003977 int encode_width = rsz->resize_width;
3978 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003979
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003980 AV1_COMMON *cm = &cpi->common;
3981 cm->superres_upscaled_width = encode_width;
3982 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07003983 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003984 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
3985 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00003986 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003987}
3988
David Turnerdedd8ff2019-01-23 13:59:46 +00003989void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08003990 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08003991 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08003992 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00003993 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003994 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08003995
Yaowu Xu7e450882019-04-30 15:09:18 -07003996 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003997}
3998
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003999static void superres_post_encode(AV1_COMP *cpi) {
4000 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004001 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004002
Cheng Chen09c83a52018-06-05 12:27:36 -07004003 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004004
Urvang Joshid6b5d512018-03-20 13:34:38 -07004005 assert(cpi->oxcf.enable_superres);
4006 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004007 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004008
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004009 av1_superres_upscale(cm, NULL);
4010
4011 // If regular resizing is occurring the source will need to be downscaled to
4012 // match the upscaled superres resolution. Otherwise the original source is
4013 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004014 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004015 cpi->source = cpi->unscaled_source;
4016 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4017 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004018 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4019 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004020 // Do downscale. cm->(width|height) has been updated by
4021 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004022 if (aom_realloc_frame_buffer(
4023 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004024 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4025 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4026 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004027 aom_internal_error(
4028 &cm->error, AOM_CODEC_MEM_ERROR,
4029 "Failed to reallocate scaled source buffer for superres");
4030 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4031 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004032 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004033 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004034 cpi->source = &cpi->scaled_source;
4035 }
4036}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004037
4038static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004039 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004040 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004041
Urvang Joshic8b52d52018-03-23 13:16:51 -07004042 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4043 cm->coded_lossless && cm->all_lossless));
4044
logangwf95c9162019-02-20 12:02:32 -08004045 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4046 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4047 !cm->large_scale_tile;
4048 const int use_restoration = cm->seq_params.enable_restoration &&
4049 !cm->all_lossless && !cm->large_scale_tile;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004050
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004051 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004052
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004053#if CONFIG_COLLECT_COMPONENT_TIMING
4054 start_timing(cpi, loop_filter_time);
4055#endif
logangwf95c9162019-02-20 12:02:32 -08004056 if (use_loopfilter) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004057 aom_clear_system_state();
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004058 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
logangwf95c9162019-02-20 12:02:32 -08004059 } else {
4060 lf->filter_level[0] = 0;
4061 lf->filter_level[1] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004062 }
4063
Debargha Mukherjee2382b142018-02-26 14:31:32 -08004064 if (lf->filter_level[0] || lf->filter_level[1]) {
Deepa K G964e72e2018-05-16 16:56:01 +05304065 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004066 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
Yaowu Xu8e718cf2019-05-02 09:06:57 -07004067#if CONFIG_LPF_MASK
Cheng Chene3600cd2018-09-21 18:45:42 -07004068 0,
4069#endif
Deepa K G964e72e2018-05-16 16:56:01 +05304070 cpi->workers, cpi->num_workers,
4071 &cpi->lf_row_sync);
4072 else
David Turnerc29e1a92018-12-06 14:10:14 +00004073 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
Yaowu Xu8e718cf2019-05-02 09:06:57 -07004074#if CONFIG_LPF_MASK
Cheng Chen84b09932018-08-12 17:35:13 -07004075 0,
Cheng Chen8ab1f442018-04-27 18:01:52 -07004076#endif
Cheng Chen84b09932018-08-12 17:35:13 -07004077 0, num_planes, 0);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004078 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004079#if CONFIG_COLLECT_COMPONENT_TIMING
4080 end_timing(cpi, loop_filter_time);
4081#endif
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004082
logangwf95c9162019-02-20 12:02:32 -08004083 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004084 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004085
logangwf95c9162019-02-20 12:02:32 -08004086 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004087#if CONFIG_COLLECT_COMPONENT_TIMING
4088 start_timing(cpi, cdef_time);
4089#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004090 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004091 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
Hui Su805c7d22019-05-07 21:57:50 -07004092 cpi->sf.cdef_pick_method);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004093
4094 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004095 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004096#if CONFIG_COLLECT_COMPONENT_TIMING
4097 end_timing(cpi, cdef_time);
4098#endif
logangwf95c9162019-02-20 12:02:32 -08004099 } else {
4100 cm->cdef_info.cdef_bits = 0;
4101 cm->cdef_info.cdef_strengths[0] = 0;
4102 cm->cdef_info.nb_cdef_strengths = 1;
4103 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004104 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004105
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004106 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004107
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004108#if CONFIG_COLLECT_COMPONENT_TIMING
4109 start_timing(cpi, loop_restoration_time);
4110#endif
logangwf95c9162019-02-20 12:02:32 -08004111 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004112 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004113 av1_pick_filter_restoration(cpi->source, cpi);
4114 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4115 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4116 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304117 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004118 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304119 cpi->workers, cpi->num_workers,
4120 &cpi->lr_row_sync, &cpi->lr_ctxt);
4121 else
David Turnerc29e1a92018-12-06 14:10:14 +00004122 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304123 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004124 }
logangwf95c9162019-02-20 12:02:32 -08004125 } else {
4126 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4127 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4128 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004129 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004130#if CONFIG_COLLECT_COMPONENT_TIMING
4131 end_timing(cpi, loop_restoration_time);
4132#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004133}
4134
David Turnerf2b334c2018-12-13 13:00:55 +00004135static void fix_interp_filter(InterpFilter *const interp_filter,
4136 const FRAME_COUNTS *const counts) {
4137 if (*interp_filter == SWITCHABLE) {
4138 // Check to see if only one of the filters is actually used
4139 int count[SWITCHABLE_FILTERS] = { 0 };
4140 int num_filters_used = 0;
4141 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4142 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4143 count[i] += counts->switchable_interp[j][i];
4144 num_filters_used += (count[i] > 0);
4145 }
4146 if (num_filters_used == 1) {
4147 // Only one filter is used. So set the filter at frame level
4148 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4149 if (count[i]) {
4150 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4151 break;
4152 }
4153 }
4154 }
4155 }
4156}
4157
David Turner996b2c12018-12-07 15:52:30 +00004158static void finalize_encoded_frame(AV1_COMP *const cpi) {
4159 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004160 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004161
David Turner99e990e2018-12-10 12:54:26 +00004162 if (!cm->seq_params.reduced_still_picture_hdr &&
4163 encode_show_existing_frame(cm)) {
4164 RefCntBuffer *const frame_to_show =
4165 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4166
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004167 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004168 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4169 "Buffer does not contain a reconstructed frame");
4170 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004171 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004172 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004173 }
David Turner08f909c2018-12-18 13:29:14 +00004174
4175 if (!encode_show_existing_frame(cm) &&
4176 cm->seq_params.film_grain_params_present &&
4177 (cm->show_frame || cm->showable_frame)) {
4178 // Copy the current frame's film grain params to the its corresponding
4179 // RefCntBuffer slot.
4180 cm->cur_frame->film_grain_params = cm->film_grain_params;
4181
4182 // We must update the parameters if this is not an INTER_FRAME
4183 if (current_frame->frame_type != INTER_FRAME)
4184 cm->cur_frame->film_grain_params.update_parameters = 1;
4185
4186 // Iterate the random seed for the next frame.
4187 cm->film_grain_params.random_seed += 3381;
4188 if (cm->film_grain_params.random_seed == 0)
4189 cm->film_grain_params.random_seed = 7391;
4190 }
David Turnerf2b334c2018-12-13 13:00:55 +00004191
4192 // Initialise all tiles' contexts from the global frame context
4193 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4194 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4195 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4196 cpi->tile_data[tile_idx].tctx = *cm->fc;
4197 }
4198 }
4199
4200 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004201}
4202
Urvang Joshi57643372019-02-21 11:10:57 -08004203static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4204 int top_index, int bottom_index) {
4205 const AV1_COMMON *const cm = &cpi->common;
4206 const RATE_CONTROL *const rc = &cpi->rc;
4207
4208 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4209
4210 int q_regulated =
4211 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4212 AOMMAX(q_high, top_index), cm->width, cm->height);
4213
4214 int retries = 0;
4215 while (q_regulated < q_low && retries < 10) {
4216 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4217 q_regulated =
4218 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4219 AOMMAX(q_high, top_index), cm->width, cm->height);
4220 retries++;
4221 }
4222 return q_regulated;
4223}
4224
4225static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4226 int top_index, int bottom_index) {
4227 const AV1_COMMON *const cm = &cpi->common;
4228 const RATE_CONTROL *const rc = &cpi->rc;
4229
4230 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4231 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4232 top_index, cm->width, cm->height);
4233
4234 int retries = 0;
4235 while (q_regulated > q_high && retries < 10) {
4236 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4237 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4238 top_index, cm->width, cm->height);
4239 retries++;
4240 }
4241 return q_regulated;
4242}
4243
David Turner2f3b5df2019-01-02 14:30:50 +00004244// Called after encode_with_recode_loop() has just encoded a frame and packed
4245// its bitstream. This function works out whether we under- or over-shot
4246// our bitrate target and adjusts q as appropriate. Also decides whether
4247// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07004248static void recode_loop_update_q(
4249 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
4250 int *const q_high, const int top_index, const int bottom_index,
4251 int *const undershoot_seen, int *const overshoot_seen,
4252 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00004253 AV1_COMMON *const cm = &cpi->common;
4254 RATE_CONTROL *const rc = &cpi->rc;
4255
Hui Suef139e12019-05-20 15:51:22 -07004256 const int min_cr = cpi->oxcf.min_cr;
4257 if (min_cr > 0) {
4258 aom_clear_system_state();
4259 const double compression_ratio =
4260 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
4261 const double target_cr = min_cr / 100.0;
4262 if (compression_ratio < target_cr) {
4263 *low_cr_seen = 1;
4264 if (*q < rc->worst_quality) {
4265 const double cr_ratio = target_cr / compression_ratio;
4266 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
4267 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
4268 *q_low = AOMMAX(*q, *q_low);
4269 *q_high = AOMMAX(*q, *q_high);
4270 *loop = 1;
4271 }
4272 }
4273 if (*low_cr_seen) return;
4274 }
4275
4276 if (cpi->oxcf.rc_mode == AOM_Q) return;
4277
David Turner2f3b5df2019-01-02 14:30:50 +00004278 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4279 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4280 &frame_under_shoot_limit,
4281 &frame_over_shoot_limit);
4282 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4283
4284 if ((cm->current_frame.frame_type == KEY_FRAME) &&
4285 rc->this_key_frame_forced &&
4286 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4287 int last_q = *q;
4288 int64_t kf_err;
4289
4290 int64_t high_err_target = cpi->ambient_err;
4291 int64_t low_err_target = cpi->ambient_err >> 1;
4292
4293 if (cm->seq_params.use_highbitdepth) {
4294 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4295 } else {
4296 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4297 }
4298 // Prevent possible divide by zero error below for perfect KF
4299 kf_err += !kf_err;
4300
4301 // The key frame is not good enough or we can afford
4302 // to make it better without undue risk of popping.
4303 if ((kf_err > high_err_target &&
4304 rc->projected_frame_size <= frame_over_shoot_limit) ||
4305 (kf_err > low_err_target &&
4306 rc->projected_frame_size <= frame_under_shoot_limit)) {
4307 // Lower q_high
4308 *q_high = *q > *q_low ? *q - 1 : *q_low;
4309
4310 // Adjust Q
4311 *q = (int)((*q * high_err_target) / kf_err);
4312 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4313 } else if (kf_err < low_err_target &&
4314 rc->projected_frame_size >= frame_under_shoot_limit) {
4315 // The key frame is much better than the previous frame
4316 // Raise q_low
4317 *q_low = *q < *q_high ? *q + 1 : *q_high;
4318
4319 // Adjust Q
4320 *q = (int)((*q * low_err_target) / kf_err);
4321 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4322 }
4323
4324 // Clamp Q to upper and lower limits:
4325 *q = clamp(*q, *q_low, *q_high);
4326
4327 *loop = *q != last_q;
4328 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4329 frame_under_shoot_limit, *q,
4330 AOMMAX(*q_high, top_index), bottom_index)) {
4331 // Is the projected frame size out of range and are we allowed
4332 // to attempt to recode.
4333 int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004334
4335 // Frame size out of permitted range:
4336 // Update correction factor & compute new Q to try...
4337 // Frame is too large
4338 if (rc->projected_frame_size > rc->this_frame_target) {
4339 // Special case if the projected size is > the max allowed.
4340 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4341 *q_high = rc->worst_quality;
4342
4343 // Raise Qlow as to at least the current value
4344 *q_low = *q < *q_high ? *q + 1 : *q_high;
4345
Urvang Joshi57643372019-02-21 11:10:57 -08004346 if (*undershoot_seen || loop_at_this_size > 2 ||
4347 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004348 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4349
4350 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004351 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4352 const int q_mid = (*q_high + *q_low + 1) / 2;
4353 const int q_regulated = get_regulated_q_overshoot(
4354 cpi, *q_low, *q_high, top_index, bottom_index);
4355 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4356 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4357 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00004358 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004359 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
4360 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00004361 }
4362
4363 *overshoot_seen = 1;
4364 } else {
4365 // Frame is too small
4366 *q_high = *q > *q_low ? *q - 1 : *q_low;
4367
Urvang Joshi57643372019-02-21 11:10:57 -08004368 if (*overshoot_seen || loop_at_this_size > 2 ||
4369 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004370 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4371 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004372 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4373 const int q_mid = (*q_high + *q_low) / 2;
4374 const int q_regulated =
4375 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4376 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4377 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4378 *q = (q_mid + q_regulated) / 2;
4379
4380 // Special case reset for qlow for constrained quality.
4381 // This should only trigger where there is very substantial
4382 // undershoot on a frame and the auto cq level is above
4383 // the user passsed in value.
4384 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
4385 *q_low = *q;
4386 }
David Turner2f3b5df2019-01-02 14:30:50 +00004387 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004388 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4389
David Turner2f3b5df2019-01-02 14:30:50 +00004390 // Special case reset for qlow for constrained quality.
4391 // This should only trigger where there is very substantial
4392 // undershoot on a frame and the auto cq level is above
4393 // the user passsed in value.
4394 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
4395 *q_low = *q;
4396 }
David Turner2f3b5df2019-01-02 14:30:50 +00004397 }
4398
4399 *undershoot_seen = 1;
4400 }
4401
4402 // Clamp Q to upper and lower limits:
4403 *q = clamp(*q, *q_low, *q_high);
4404
4405 *loop = (*q != last_q);
4406 } else {
4407 *loop = 0;
4408 }
4409}
4410
Tom Finegane4099e32018-01-23 12:01:51 -08004411static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004412 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004413 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004414 const int allow_recode = (cpi->sf.recode_loop != DISALLOW_RECODE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004415
4416 set_size_independent_vars(cpi);
4417
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004418 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004419
David Turnerdedd8ff2019-01-23 13:59:46 +00004420 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004421
David Turner2f3b5df2019-01-02 14:30:50 +00004422 int top_index = 0, bottom_index = 0;
4423 int q = 0, q_low = 0, q_high = 0;
4424 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4425 q_low = bottom_index;
4426 q_high = top_index;
4427
4428 // Loop variables
4429 int loop_count = 0;
4430 int loop_at_this_size = 0;
4431 int loop = 0;
4432 int overshoot_seen = 0;
4433 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07004434 int low_cr_seen = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004435
4436#if CONFIG_COLLECT_COMPONENT_TIMING
4437 printf("\n Encoding a frame:");
4438#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004439 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004440 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004441 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004442
Urvang Joshif1fa6862018-01-08 16:39:33 -08004443 // if frame was scaled calculate global_motion_search again if already
4444 // done
David Turner2f3b5df2019-01-02 14:30:50 +00004445 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004446 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00004447 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004448 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00004449 }
4450 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004451 cpi->source =
4452 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004453 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004454 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4455 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004456 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004457
David Turner2f3b5df2019-01-02 14:30:50 +00004458 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004459 if (loop_count > 0) {
4460 release_scaled_references(cpi);
4461 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004462 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004463 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004464 av1_set_quantizer(cm, q);
Yue Chena7d80482019-01-31 09:48:09 -08004465 av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08004466
4467 av1_set_variance_partition_thresholds(cpi, q, 0);
4468
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004469 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
4470 // cm->current_frame.frame_number, cm->show_frame, q,
4471 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004472
David Turner2f3b5df2019-01-02 14:30:50 +00004473 if (loop_count == 0) {
4474 setup_frame(cpi);
4475 } else if (get_primary_ref_frame_buf(cm) == NULL) {
4476 // Base q-index may have changed, so we need to assign proper default coef
4477 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07004478 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004479 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004480 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004481
Yaowu Xuc27fc142016-08-22 16:08:15 -07004482 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004483 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004484 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004485 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00004486 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
4487 suppress_active_map(cpi);
4488 av1_cyclic_refresh_setup(cpi);
4489 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004490 }
David Turner2f3b5df2019-01-02 14:30:50 +00004491
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004492 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004493 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004494 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004495 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07004496 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004497 }
David Barkercab37552018-03-21 11:56:24 +00004498 } else {
4499 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004500 }
David Barkercab37552018-03-21 11:56:24 +00004501 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004502
David Turner2f3b5df2019-01-02 14:30:50 +00004503 if (allow_recode) save_coding_context(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004504#if CONFIG_COLLECT_COMPONENT_TIMING
4505 start_timing(cpi, av1_encode_frame_time);
4506#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004507 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004508 av1_encode_frame(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004509#if CONFIG_COLLECT_COMPONENT_TIMING
4510 end_timing(cpi, av1_encode_frame_time);
4511#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004512
Yaowu Xuf883b422016-08-30 14:01:10 -07004513 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004514
4515 // Dummy pack of the bitstream using up to date stats to get an
4516 // accurate estimate of output frame size to determine if we need
4517 // to recode.
4518 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004519 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004520
David Turner996b2c12018-12-07 15:52:30 +00004521 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00004522 int largest_tile_id = 0; // Output from bitstream: unused here
4523 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004524 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004525
4526 rc->projected_frame_size = (int)(*size) << 3;
4527 restore_coding_context(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004528 }
4529
Hui Suef139e12019-05-20 15:51:22 -07004530 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00004531 // Update q and decide whether to do a recode loop
4532 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
4533 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07004534 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004535 }
4536
4537 // Special case for overlay frame.
4538 if (rc->is_src_frame_alt_ref &&
4539 rc->projected_frame_size < rc->max_frame_bandwidth)
4540 loop = 0;
4541
David Turner2f3b5df2019-01-02 14:30:50 +00004542 if (allow_recode && !cpi->sf.gm_disable_recode &&
4543 recode_loop_test_global_motion(cpi)) {
4544 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004545 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004546
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004547 if (cpi->tpl_model_pass == 1) {
4548 assert(cpi->oxcf.enable_tpl_model == 2);
Debargha Mukherjee347c64d2019-05-08 13:53:46 -07004549 av1_tpl_setup_forward_stats(cpi);
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004550 cpi->tpl_model_pass = 0;
4551 loop = 1;
4552 }
4553
Yaowu Xuc27fc142016-08-22 16:08:15 -07004554 if (loop) {
4555 ++loop_count;
4556 ++loop_at_this_size;
4557
4558#if CONFIG_INTERNAL_STATS
4559 ++cpi->tot_recode_hits;
4560#endif
4561 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004562#if CONFIG_COLLECT_COMPONENT_TIMING
4563 if (loop) printf("\n Recoding:");
4564#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004565 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004566
4567 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004568}
4569
Yaowu Xuc27fc142016-08-22 16:08:15 -07004570#define DUMP_RECON_FRAMES 0
4571
4572#if DUMP_RECON_FRAMES == 1
4573// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004574static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4575 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00004576 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00004577 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004578
Zoe Liub4f31032017-11-03 23:48:35 -07004579 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00004580 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07004581 return;
4582 }
4583
Zoe Liu27deb382018-03-27 15:13:56 -07004584 static const int flag_list[REF_FRAMES] = { 0,
4585 AOM_LAST_FLAG,
4586 AOM_LAST2_FLAG,
4587 AOM_LAST3_FLAG,
4588 AOM_GOLD_FLAG,
4589 AOM_BWD_FLAG,
4590 AOM_ALT2_FLAG,
4591 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004592 printf(
4593 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4594 "show_existing_frame=%d) "
4595 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00004596 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07004597 cm->show_existing_frame);
4598 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004599 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
4600 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08004601 printf(" %d(%c)", ref_offset,
4602 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07004603 }
4604 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004605
4606 if (!cm->show_frame) {
4607 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00004608 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004609 return;
4610 }
4611
Zoe Liub4f31032017-11-03 23:48:35 -07004612 int h;
4613 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4614 FILE *f_recon = NULL;
4615
David Turnerd2a592e2018-11-16 14:59:31 +00004616 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004617 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4618 printf("Unable to open file %s to write.\n", file_name);
4619 return;
4620 }
4621 } else {
4622 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4623 printf("Unable to open file %s to append.\n", file_name);
4624 return;
4625 }
4626 }
4627 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004628 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4629 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07004630 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07004631 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
David Turnerd2a592e2018-11-16 14:59:31 +00004632 current_frame->frame_number, cpi->twopass.gf_group.index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004633 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
David Turnerd2a592e2018-11-16 14:59:31 +00004634 current_frame->order_hint, cm->show_frame, cm->show_existing_frame,
Zoe Liuf40a9572017-10-13 12:37:19 -07004635 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
Zoe Liuf40a9572017-10-13 12:37:19 -07004636 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004637#if 0
4638 int ref_frame;
4639 printf("get_ref_frame_map_idx: [");
4640 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00004641 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07004642 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07004643#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004644
4645 // --- Y ---
4646 for (h = 0; h < cm->height; ++h) {
4647 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4648 f_recon);
4649 }
4650 // --- U ---
4651 for (h = 0; h < (cm->height >> 1); ++h) {
4652 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4653 f_recon);
4654 }
4655 // --- V ---
4656 for (h = 0; h < (cm->height >> 1); ++h) {
4657 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4658 f_recon);
4659 }
4660
4661 fclose(f_recon);
4662}
4663#endif // DUMP_RECON_FRAMES
4664
David Turnerbc0993e2019-02-15 14:42:23 +00004665static int get_interp_filter_selected(const AV1_COMMON *const cm,
4666 MV_REFERENCE_FRAME ref,
4667 InterpFilters ifilter) {
4668 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
4669 if (buf == NULL) return 0;
4670 return buf->interp_filter_selected[ifilter];
4671}
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304672
David Turnerbc0993e2019-02-15 14:42:23 +00004673static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
4674 const AV1_COMMON *const cm = &cpi->common;
4675 int ref_total[REF_FRAMES] = { 0 };
4676
4677 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4678 return 0;
4679
4680 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
4681 for (InterpFilters ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4682 ++ifilter) {
4683 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
4684 }
4685 }
4686 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
4687 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
4688 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
4689
4690 int mask = 0;
4691 for (InterpFilters ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4692 ++ifilter) {
4693 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
4694 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
4695 int filter_score =
4696 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
4697 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
4698 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
4699 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
4700 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
4701 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
4702 if (filter_score < ref_total_total) mask |= 1 << ifilter;
4703 }
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304704 }
4705 return mask;
4706}
4707
David Turnerefed6372019-01-11 15:14:11 +00004708static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
4709 const YV12_BUFFER_CONFIG *last_picture,
4710 hash_table *last_hash_table) {
4711 aom_clear_system_state();
4712 // check use hash ME
4713 int k;
4714 uint32_t hash_value_1;
4715 uint32_t hash_value_2;
4716
4717 const int block_size = 8;
4718 const double threshold_current = 0.8;
4719 const double threshold_average = 0.95;
4720 const int max_history_size = 32;
4721 int T = 0; // total block
4722 int C = 0; // match with collocated block
4723 int S = 0; // smooth region but not match with collocated block
4724 int M = 0; // match with other block
4725
4726 const int pic_width = cur_picture->y_width;
4727 const int pic_height = cur_picture->y_height;
4728 for (int i = 0; i + block_size <= pic_height; i += block_size) {
4729 for (int j = 0; j + block_size <= pic_width; j += block_size) {
4730 const int x_pos = j;
4731 const int y_pos = i;
4732 int match = 1;
4733 T++;
4734
4735 // check whether collocated block match with current
4736 uint8_t *p_cur = cur_picture->y_buffer;
4737 uint8_t *p_ref = last_picture->y_buffer;
4738 int stride_cur = cur_picture->y_stride;
4739 int stride_ref = last_picture->y_stride;
4740 p_cur += (y_pos * stride_cur + x_pos);
4741 p_ref += (y_pos * stride_ref + x_pos);
4742
4743 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
4744 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
4745 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
4746 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4747 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4748 if (p16_cur[tmpX] != p16_ref[tmpX]) {
4749 match = 0;
4750 }
4751 }
4752 p16_cur += stride_cur;
4753 p16_ref += stride_ref;
4754 }
4755 } else {
4756 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4757 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4758 if (p_cur[tmpX] != p_ref[tmpX]) {
4759 match = 0;
4760 }
4761 }
4762 p_cur += stride_cur;
4763 p_ref += stride_ref;
4764 }
4765 }
4766
4767 if (match) {
4768 C++;
4769 continue;
4770 }
4771
4772 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
4773 y_pos) ||
4774 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
4775 S++;
4776 continue;
4777 }
4778
4779 av1_get_block_hash_value(
4780 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
4781 block_size, &hash_value_1, &hash_value_2,
4782 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
4783 // Hashing does not work for highbitdepth currently.
4784 // TODO(Roger): Make it work for highbitdepth.
4785 if (av1_use_hash_me(&cpi->common)) {
4786 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
4787 M++;
4788 }
4789 }
4790 }
4791 }
4792
4793 assert(T > 0);
4794 double csm_rate = ((double)(C + S + M)) / ((double)(T));
4795 double m_rate = ((double)(M)) / ((double)(T));
4796
4797 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
4798 cpi->m_rate_array[cpi->rate_index] = m_rate;
4799
4800 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
4801 cpi->rate_size++;
4802 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
4803
4804 if (csm_rate < threshold_current) {
4805 return 0;
4806 }
4807
4808 if (C == T) {
4809 return 1;
4810 }
4811
4812 double csm_average = 0.0;
4813 double m_average = 0.0;
4814
4815 for (k = 0; k < cpi->rate_size; k++) {
4816 csm_average += cpi->csm_rate_array[k];
4817 m_average += cpi->m_rate_array[k];
4818 }
4819 csm_average /= cpi->rate_size;
4820 m_average /= cpi->rate_size;
4821
4822 if (csm_average < threshold_average) {
4823 return 0;
4824 }
4825
4826 if (M > (T - C - S) / 3) {
4827 return 1;
4828 }
4829
4830 if (csm_rate > 0.99 && m_rate > 0.01) {
4831 return 1;
4832 }
4833
4834 if (csm_average + m_average > 1.01) {
4835 return 1;
4836 }
4837
4838 return 0;
4839}
4840
David Turner73245762019-02-11 16:42:34 +00004841// Refresh reference frame buffers according to refresh_frame_flags.
4842static void refresh_reference_frames(AV1_COMP *cpi) {
4843 AV1_COMMON *const cm = &cpi->common;
4844 // All buffers are refreshed for shown keyframes and S-frames.
4845
4846 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
4847 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
4848 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
4849 }
4850 }
4851}
4852
4853static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
4854 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004855 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004856 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00004857 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07004858 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004859 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01004860
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004861#if CONFIG_COLLECT_COMPONENT_TIMING
4862 start_timing(cpi, encode_frame_to_data_rate_time);
4863#endif
4864
Fangwen Fu8d164de2016-12-14 13:40:54 -08004865 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00004866 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07004867
Yunqing Wang9612d552018-05-15 14:58:30 -07004868 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
4869 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07004870
sarahparker21dbca42018-03-30 17:43:44 -07004871 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07004872 // cm->allow_ref_frame_mvs needs to be written into the frame header while
4873 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
4874 // separated from frame_might_allow_ref_frame_mvs().
4875 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
4876
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07004877 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07004878 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004879
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304880 cm->last_frame_type = current_frame->frame_type;
4881 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4882 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4883
Sarah Parker33005522018-07-27 14:46:25 -07004884 if (encode_show_existing_frame(cm)) {
Jingning Han8f661602017-08-19 08:16:50 -07004885 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07004886
David Turner996b2c12018-12-07 15:52:30 +00004887 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004888 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00004889 int largest_tile_id = 0; // Output from bitstream: unused here
4890 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004891 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004892
David Turner90311862018-11-29 13:34:36 +00004893 if (seq_params->frame_id_numbers_present_flag &&
4894 current_frame->frame_type == KEY_FRAME) {
4895 // Displaying a forward key-frame, so reset the ref buffer IDs
4896 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
4897 for (int i = 0; i < REF_FRAMES; i++)
4898 cm->ref_frame_id[i] = display_frame_id;
4899 }
4900
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004901 cpi->seq_params_locked = 1;
4902
Yaowu Xuc27fc142016-08-22 16:08:15 -07004903#if DUMP_RECON_FRAMES == 1
4904 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4905 dump_filtered_recon_frames(cpi);
4906#endif // DUMP_RECON_FRAMES
4907
David Turner73245762019-02-11 16:42:34 +00004908 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
4909 // for the purpose to verify no mismatch between encoder and decoder.
4910 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004911
David Turner73245762019-02-11 16:42:34 +00004912 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004913
Yaowu Xuc27fc142016-08-22 16:08:15 -07004914 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4915 // to do post-encoding update accordingly.
4916 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004917 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004918 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004919 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004920
David Turnerd2a592e2018-11-16 14:59:31 +00004921 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004922
Tom Finegane4099e32018-01-23 12:01:51 -08004923 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004924 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004925
David Turnerefed6372019-01-11 15:14:11 +00004926 // Work out whether to force_integer_mv this frame
4927 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
4928 !frame_is_intra_only(cm)) {
4929 if (cpi->common.seq_params.force_integer_mv == 2) {
4930 // Adaptive mode: see what previous frame encoded did
4931 if (cpi->unscaled_last_source != NULL) {
4932 cm->cur_frame_force_integer_mv =
4933 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
4934 cpi->previous_hash_table);
4935 } else {
4936 cpi->common.cur_frame_force_integer_mv = 0;
4937 }
4938 } else {
4939 cpi->common.cur_frame_force_integer_mv =
4940 cpi->common.seq_params.force_integer_mv;
4941 }
4942 } else {
4943 cpi->common.cur_frame_force_integer_mv = 0;
4944 }
4945
Yaowu Xuc27fc142016-08-22 16:08:15 -07004946 // Set default state for segment based loop filter update flags.
4947 cm->lf.mode_ref_delta_update = 0;
4948
Yaowu Xuc27fc142016-08-22 16:08:15 -07004949 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05004950 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004951 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004952 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004953
4954 // If segmentation is enabled force a map update for key frames.
4955 if (seg->enabled) {
4956 seg->update_map = 1;
4957 seg->update_data = 1;
4958 }
4959
4960 // The alternate reference frame cannot be active for a key frame.
4961 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004962 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00004963 if (cpi->oxcf.mtu == 0) {
4964 cm->num_tg = cpi->oxcf.num_tile_groups;
4965 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08004966 // Use a default value for the purposes of weighting costs in probability
4967 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00004968 cm->num_tg = DEFAULT_MAX_NUM_TG;
4969 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004970
4971 // For 1 pass CBR, check if we are dropping this frame.
4972 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004973 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00004974 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004975 if (av1_rc_drop_frame(cpi)) {
4976 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00004977 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004978 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004979 }
4980 }
4981
Yaowu Xuf883b422016-08-30 14:01:10 -07004982 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004983
4984#if CONFIG_INTERNAL_STATS
4985 memset(cpi->mode_chosen_counts, 0,
4986 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4987#endif
4988
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004989 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004990 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08004991 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004992 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00004993 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08004994 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07004995 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
4996 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
4997 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004998 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004999 lsb = cpi->source->y_buffer[0] & 0xff;
5000 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005001 }
David Turner760a2f42018-12-07 15:25:36 +00005002 cm->current_frame_id =
5003 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005004
5005 // S_frame is meant for stitching different streams of different
5006 // resolutions together, so current_frame_id must be the
5007 // same across different streams of the same content current_frame_id
5008 // should be the same and not random. 0x37 is a chosen number as start
5009 // point
5010 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005011 } else {
5012 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00005013 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
5014 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005015 }
5016 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005017
Hui Su483a8452018-02-26 12:28:48 -08005018 switch (cpi->oxcf.cdf_update_mode) {
5019 case 0: // No CDF update for any frames(4~6% compression loss).
5020 cm->disable_cdf_update = 1;
5021 break;
5022 case 1: // Enable CDF update for all frames.
5023 cm->disable_cdf_update = 0;
5024 break;
5025 case 2:
5026 // Strategically determine at which frames to do CDF update.
5027 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5028 // compression loss).
5029 // TODO(huisu@google.com): design schemes for various trade-offs between
5030 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005031 cm->disable_cdf_update =
5032 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005033 break;
5034 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005035 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005036
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005037#if CONFIG_COLLECT_COMPONENT_TIMING
5038 start_timing(cpi, encode_with_recode_loop_time);
5039#endif
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005040
5041 if (cpi->oxcf.pass == 2 && cpi->oxcf.enable_tpl_model == 2 &&
5042 current_frame->frame_type == INTER_FRAME) {
5043 if (!cm->show_frame) {
5044 assert(cpi->tpl_model_pass == 0);
5045 cpi->tpl_model_pass = 1;
5046 }
5047 }
5048
David Turner2f3b5df2019-01-02 14:30:50 +00005049 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
5050 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005051#if CONFIG_COLLECT_COMPONENT_TIMING
5052 end_timing(cpi, encode_with_recode_loop_time);
5053#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005054
5055#ifdef OUTPUT_YUV_SKINMAP
David Turnerd2a592e2018-11-16 14:59:31 +00005056 if (cpi->common.current_frame.frame_number > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005057 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005058 }
5059#endif // OUTPUT_YUV_SKINMAP
5060
5061 // Special case code to reduce pulsing when key frames are forced at a
5062 // fixed interval. Note the reconstruction error if it is the frame before
5063 // the force key frame
5064 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005065 if (seq_params->use_highbitdepth) {
Jack Haughtonddb80602018-11-21 16:41:49 +00005066 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005067 } else {
Jack Haughtonddb80602018-11-21 16:41:49 +00005068 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005069 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005070 }
5071
David Turnerc29e1a92018-12-06 14:10:14 +00005072 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5073 cm->cur_frame->buf.transfer_characteristics =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005074 seq_params->transfer_characteristics;
David Turnerc29e1a92018-12-06 14:10:14 +00005075 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5076 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5077 cm->cur_frame->buf.chroma_sample_position =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005078 seq_params->chroma_sample_position;
David Turnerc29e1a92018-12-06 14:10:14 +00005079 cm->cur_frame->buf.color_range = seq_params->color_range;
5080 cm->cur_frame->buf.render_width = cm->render_width;
5081 cm->cur_frame->buf.render_height = cm->render_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005082
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005083 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5084 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005085
5086 // Pick the loop filter level for the frame.
Cheng Chen68dc9142018-05-02 17:46:28 -07005087 if (!cm->allow_intrabc) {
David Barker218556e2018-02-14 14:23:12 +00005088 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08005089 } else {
Hui Su06463e42018-02-23 22:17:36 -08005090 cm->lf.filter_level[0] = 0;
5091 cm->lf.filter_level[1] = 0;
David Turnerebf96f42018-11-14 16:57:57 +00005092 cm->cdef_info.cdef_bits = 0;
5093 cm->cdef_info.cdef_strengths[0] = 0;
5094 cm->cdef_info.nb_cdef_strengths = 1;
5095 cm->cdef_info.cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005096 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5097 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5098 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08005099 }
David Barker218556e2018-02-14 14:23:12 +00005100
5101 // TODO(debargha): Fix mv search range on encoder side
David Turnerc29e1a92018-12-06 14:10:14 +00005102 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5103 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005104
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005105#ifdef OUTPUT_YUV_REC
David Turnerc29e1a92018-12-06 14:10:14 +00005106 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005107#endif
5108
David Turner996b2c12018-12-07 15:52:30 +00005109 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005110 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005111 int largest_tile_id = 0; // Output from pack_bitstream
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005112#if CONFIG_COLLECT_COMPONENT_TIMING
5113 start_timing(cpi, av1_pack_bitstream_final_time);
5114#endif
David Turner35cba132018-12-10 15:48:15 +00005115 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005116 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005117#if CONFIG_COLLECT_COMPONENT_TIMING
5118 end_timing(cpi, av1_pack_bitstream_final_time);
5119#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005120
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005121 cpi->seq_params_locked = 1;
5122
David Turner996b2c12018-12-07 15:52:30 +00005123 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005124 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00005125 for (int i = 0; i < REF_FRAMES; i++) {
5126 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005127 cm->ref_frame_id[i] = cm->current_frame_id;
5128 }
5129 }
5130 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005131
Yaowu Xuc27fc142016-08-22 16:08:15 -07005132#if DUMP_RECON_FRAMES == 1
5133 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005134 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005135#endif // DUMP_RECON_FRAMES
5136
Soo-Chul Han934af352017-10-15 15:21:51 -04005137 if (cm->seg.enabled) {
5138 if (cm->seg.update_map) {
5139 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005140 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00005141 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04005142 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5143 }
5144 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005145
5146 if (frame_is_intra_only(cm) == 0) {
5147 release_scaled_references(cpi);
5148 }
5149
David Turner73245762019-02-11 16:42:34 +00005150 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5151 // for the purpose to verify no mismatch between encoder and decoder.
5152 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
5153
5154 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005155
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005156#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005157 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005158#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005159
Hui Sudc54be62018-03-14 19:14:28 -07005160 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00005161 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07005162 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005163 }
David Turnera4c96252019-01-11 16:36:39 +00005164 if (!cm->large_scale_tile) {
5165 cm->cur_frame->frame_context = *cm->fc;
5166 }
5167#define EXT_TILE_DEBUG 0
5168#if EXT_TILE_DEBUG
5169 if (cm->large_scale_tile && oxcf->pass == 2) {
5170 char fn[20] = "./fc";
5171 fn[4] = current_frame->frame_number / 100 + '0';
5172 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
5173 fn[6] = (current_frame->frame_number % 10) + '0';
5174 fn[7] = '\0';
5175 av1_print_frame_contexts(cm->fc, fn);
5176 }
5177#endif // EXT_TILE_DEBUG
5178#undef EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07005179
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005180#if CONFIG_COLLECT_COMPONENT_TIMING
5181 end_timing(cpi, encode_frame_to_data_rate_time);
5182
5183 // Print out timing information.
5184 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08005185 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005186 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08005187 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005188 for (i = 0; i < kTimingComponents; i++) {
5189 cpi->component_time[i] += cpi->frame_component_time[i];
5190 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
5191 get_component_name(i), cpi->frame_component_time[i],
5192 cpi->component_time[i]);
5193 cpi->frame_component_time[i] = 0;
5194 }
5195#endif
5196
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305197 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005198
Yaowu Xuf883b422016-08-30 14:01:10 -07005199 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005200
David Turnerefed6372019-01-11 15:14:11 +00005201 // Store encoded frame's hash table for is_integer_mv() next time
5202 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
5203 cpi->previous_hash_table = &cm->cur_frame->hash_table;
5204 }
5205
Yaowu Xuc27fc142016-08-22 16:08:15 -07005206 // Clear the one shot update flags for segmentation map and mode/ref loop
5207 // filter deltas.
5208 cm->seg.update_map = 0;
5209 cm->seg.update_data = 0;
5210 cm->lf.mode_ref_delta_update = 0;
5211
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005212 // A droppable frame might not be shown but it always
5213 // takes a space in the gf group. Therefore, even when
5214 // it is not shown, we still need update the count down.
5215
Yaowu Xuc27fc142016-08-22 16:08:15 -07005216 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08005217 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5218 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005219 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005220 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005221 // Don't increment frame counters if this was an altref buffer
5222 // update not a real frame
Wei-Ting Lin96ee0eb2018-06-22 15:27:22 -07005223
David Turnerd2a592e2018-11-16 14:59:31 +00005224 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005225 }
5226
Tom Finegane4099e32018-01-23 12:01:51 -08005227 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005228}
5229
David Turner056f7cd2019-01-07 17:48:13 +00005230int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00005231 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00005232 const EncodeFrameParams *const frame_params,
5233 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00005234 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00005235 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005236
David Turnercb5e36f2019-01-17 17:15:25 +00005237 cpi->unscaled_source = frame_input->source;
5238 cpi->source = frame_input->source;
5239 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00005240
David Turner6e8b4d92019-02-18 15:01:33 +00005241 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00005242 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00005243 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00005244 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00005245 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005246 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00005247 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00005248 cm->show_existing_frame = frame_params->show_existing_frame;
5249 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00005250
David Turner73245762019-02-11 16:42:34 +00005251 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
5252 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
5253
David Turnerfe3aecb2019-02-06 14:42:42 +00005254 cpi->refresh_last_frame = frame_params->refresh_last_frame;
5255 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
5256 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
5257 cpi->refresh_alt2_ref_frame = frame_params->refresh_alt2_ref_frame;
5258 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
5259
David Turnera7f133c2019-01-22 14:47:16 +00005260 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
5261 current_frame->frame_number = 0;
5262
5263 if (cm->show_existing_frame) {
5264 current_frame->order_hint = cm->cur_frame->order_hint;
5265 } else {
5266 current_frame->order_hint =
5267 current_frame->frame_number + frame_params->order_offset;
5268 current_frame->order_hint %=
5269 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
5270 }
5271
David Turnercb5e36f2019-01-17 17:15:25 +00005272 if (cpi->oxcf.pass == 1) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005273#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00005274 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005275#endif
David Turnercb5e36f2019-01-17 17:15:25 +00005276 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00005277 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
5278 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00005279 return AOM_CODEC_ERROR;
5280 }
5281 } else {
David Turner056f7cd2019-01-07 17:48:13 +00005282 return AOM_CODEC_ERROR;
5283 }
5284
5285 return AOM_CODEC_OK;
5286}
5287
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005288#if CONFIG_DENOISE
5289static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5290 int block_size, float noise_level,
5291 int64_t time_stamp, int64_t end_time) {
5292 AV1_COMMON *const cm = &cpi->common;
5293 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005294 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5295 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005296 if (!cpi->denoise_and_model) {
5297 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5298 "Error allocating denoise and model");
5299 return -1;
5300 }
5301 }
5302 if (!cpi->film_grain_table) {
5303 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5304 if (!cpi->film_grain_table) {
5305 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5306 "Error allocating grain table");
5307 return -1;
5308 }
5309 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5310 }
5311 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5312 &cm->film_grain_params)) {
5313 if (cm->film_grain_params.apply_grain) {
5314 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5315 &cm->film_grain_params);
5316 }
5317 }
5318 return 0;
5319}
5320#endif
5321
James Zern3e2613b2017-03-30 23:14:40 -07005322int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005323 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5324 int64_t end_time) {
5325 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005326 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005327 int res = 0;
5328 const int subsampling_x = sd->subsampling_x;
5329 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005330 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005331
Yaowu Xuc27fc142016-08-22 16:08:15 -07005332 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005333
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005334#if CONFIG_INTERNAL_STATS
5335 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005336 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005337#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005338#if CONFIG_DENOISE
5339 if (cpi->oxcf.noise_level > 0)
5340 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5341 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5342 res = -1;
5343#endif // CONFIG_DENOISE
5344
Yaowu Xuf883b422016-08-30 14:01:10 -07005345 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005346 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005347 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005348#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005349 aom_usec_timer_mark(&timer);
5350 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005351#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005352 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005353 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005354 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005355 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005356 res = -1;
5357 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005358 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005359 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005360 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005361 "Profile 1 requires 4:4:4 color format");
5362 res = -1;
5363 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005364 if ((seq_params->profile == PROFILE_2) &&
5365 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005366 !(subsampling_x == 1 && subsampling_y == 0)) {
5367 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5368 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005369 res = -1;
5370 }
5371
5372 return res;
5373}
5374
Yaowu Xuc27fc142016-08-22 16:08:15 -07005375#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005376extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5377 const unsigned char *img2, int img2_pitch,
5378 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005379
5380static void adjust_image_stat(double y, double u, double v, double all,
5381 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07005382 s->stat[STAT_Y] += y;
5383 s->stat[STAT_U] += u;
5384 s->stat[STAT_V] += v;
5385 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005386 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005387}
5388
Angie Chiang08a22a62017-07-17 17:29:17 -07005389static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005390 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005391 double samples = 0.0;
5392 uint32_t in_bit_depth = 8;
5393 uint32_t bit_depth = 8;
5394
Angie Chiang08a22a62017-07-17 17:29:17 -07005395#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00005396 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005397#endif
5398 cpi->bytes += frame_bytes;
5399
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005400 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005401 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005402 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005403 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005404 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005405 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00005406 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005407 double y, u, v, frame_all;
5408
5409 cpi->count++;
5410 if (cpi->b_calculate_psnr) {
5411 PSNR_STATS psnr;
5412 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005413 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005414 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005415 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005416
5417 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5418 &cpi->psnr);
5419 cpi->total_sq_error += psnr.sse[0];
5420 cpi->total_samples += psnr.samples[0];
5421 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005422 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005423 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005424 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005425 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005426 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005427 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005428
Yaowu Xuf883b422016-08-30 14:01:10 -07005429 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005430 cpi->summed_quality += frame_ssim2 * weight;
5431 cpi->summed_weights += weight;
5432
5433#if 0
5434 {
5435 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005436 double y2 = psnr.psnr[1];
5437 double u2 = psnr.psnr[2];
5438 double v2 = psnr.psnr[3];
5439 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005440 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 +00005441 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005442 frame_psnr2, frame_ssim2);
5443 fclose(f);
5444 }
5445#endif
5446 }
5447 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005448 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005449 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005450 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5451 recon->y_stride, orig->y_width, orig->y_height);
5452 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005453 cpi->total_blockiness += frame_blockiness;
5454 }
5455
5456 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005457 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005458 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005459 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5460 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5461
5462 const double peak = (double)((1 << in_bit_depth) - 1);
5463 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005464 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005465 if (consistency > 0.0)
5466 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005467 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005468 cpi->total_inconsistency += this_inconsistency;
5469 }
5470 }
5471 }
5472
5473 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005474 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005475 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005476 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005477 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5478 }
5479}
5480#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08005481int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5482 size_t *size, uint8_t *dest, int64_t *time_stamp,
5483 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07005484 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005485 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5486 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005487
5488#if CONFIG_BITSTREAM_DEBUG
5489 assert(cpi->oxcf.max_threads == 0 &&
5490 "bitstream debug tool does not support multithreading");
5491 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07005492 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
5493 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005494#endif
5495
Sarah Parker740e8392019-01-23 15:47:53 -08005496 // Indicates whether or not to use an adaptive quantize b rather than
5497 // the traditional version
5498 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
5499
Dominic Symesd4929012018-01-31 17:32:01 +01005500 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00005501 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005502#if CONFIG_INTERNAL_STATS
5503 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005504 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005505#endif
RogerZhou3b635242017-09-19 10:06:46 -07005506 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005507
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07005508 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07005509 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
5510 ? REFRESH_FRAME_CONTEXT_DISABLED
5511 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005512 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08005513 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005514
Sarah Parkerb9041612018-05-22 19:06:47 -07005515 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005516 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005517
David Turnerdedd8ff2019-01-23 13:59:46 +00005518 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005519
Yue Chen1bc5be62018-08-24 13:57:32 -07005520 const int result =
5521 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
5522 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00005523 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00005524 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00005525 } else if (result == -1) {
5526 // Returning -1 indicates no frame encoded; more input is required
5527 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005528 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005529#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005530 aom_usec_timer_mark(&cmptimer);
5531 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07005532#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00005533 if (cpi->b_calculate_psnr) {
5534 if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
5535 generate_psnr_packet(cpi);
5536 }
5537 }
Hui Su8ea87322019-03-29 14:44:32 -07005538
5539 if (cpi->keep_level_stats && oxcf->pass != 1) {
5540 // Initialize level info. at the beginning of each sequence.
5541 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
5542 av1_init_level_info(cpi->level_info);
5543 }
kyslovabeeb7c2019-03-06 18:35:04 -08005544 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07005545 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005546
5547#if CONFIG_INTERNAL_STATS
5548 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07005549 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005550 }
5551#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005552#if CONFIG_SPEED_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00005553 if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005554 cpi->tx_search_count += cpi->td.mb.tx_search_count;
5555 cpi->td.mb.tx_search_count = 0;
5556 }
5557#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005558
Yaowu Xuf883b422016-08-30 14:01:10 -07005559 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005560
5561 return 0;
5562}
5563
Yaowu Xuf883b422016-08-30 14:01:10 -07005564int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5565 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005566 if (!cm->show_frame) {
5567 return -1;
5568 } else {
5569 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00005570 if (cm->cur_frame != NULL) {
5571 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005572 dest->y_width = cm->width;
5573 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005574 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
5575 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005576 ret = 0;
5577 } else {
5578 ret = -1;
5579 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005580 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005581 return ret;
5582 }
5583}
5584
Yaowu Xuf883b422016-08-30 14:01:10 -07005585int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00005586 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005587
David Turnere7ebf902018-12-04 14:04:55 +00005588 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005589 return 0;
5590}
5591
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005592static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
5593 const YV12_BUFFER_CONFIG *b) {
5594 return a->y_height == b->y_height && a->y_width == b->y_width &&
5595 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
5596 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
5597 a->border == b->border &&
5598 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
5599 (b->flags & YV12_FLAG_HIGHBITDEPTH);
5600}
5601
Yunqing Wang93b18f32018-06-08 21:08:29 -07005602aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
5603 YV12_BUFFER_CONFIG *new_frame,
5604 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005605 const int num_planes = av1_num_planes(cm);
5606 if (!equal_dimensions_and_border(new_frame, sd))
5607 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
5608 "Incorrect buffer dimensions");
5609 else
5610 aom_yv12_copy_frame(new_frame, sd, num_planes);
5611
5612 return cm->error.error_code;
5613}
5614
Yaowu Xuf883b422016-08-30 14:01:10 -07005615int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
5616 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005617 int hr = 0, hs = 0, vr = 0, vs = 0;
5618
5619 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5620
5621 Scale2Ratio(horiz_mode, &hr, &hs);
5622 Scale2Ratio(vert_mode, &vr, &vs);
5623
5624 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005625 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5626 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005627
5628 return 0;
5629}
5630
Yaowu Xuf883b422016-08-30 14:01:10 -07005631int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005632
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005633int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
5634 size_t output_size = 0;
5635 size_t total_bytes_read = 0;
5636 size_t remaining_size = *frame_size;
5637 uint8_t *buff_ptr = buffer;
5638
5639 // go through each OBUs
5640 while (total_bytes_read < *frame_size) {
5641 uint8_t saved_obu_header[2];
5642 uint64_t obu_payload_size;
5643 size_t length_of_payload_size;
5644 size_t length_of_obu_size;
5645 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
5646 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
5647
5648 // save the obu header (1 or 2 bytes)
5649 memmove(saved_obu_header, buff_ptr, obu_header_size);
5650 // clear the obu_has_size_field
5651 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
5652
5653 // get the payload_size and length of payload_size
5654 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
5655 &obu_payload_size, &length_of_payload_size) != 0) {
5656 return AOM_CODEC_ERROR;
5657 }
5658 obu_bytes_read += length_of_payload_size;
5659
5660 // calculate the length of size of the obu header plus payload
5661 length_of_obu_size =
5662 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
5663
5664 // move the rest of data to new location
5665 memmove(buff_ptr + length_of_obu_size + obu_header_size,
5666 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07005667 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005668
5669 // write the new obu size
5670 const uint64_t obu_size = obu_header_size + obu_payload_size;
5671 size_t coded_obu_size;
5672 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
5673 &coded_obu_size) != 0) {
5674 return AOM_CODEC_ERROR;
5675 }
5676
5677 // write the saved (modified) obu_header following obu size
5678 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
5679
5680 total_bytes_read += obu_bytes_read;
5681 remaining_size -= obu_bytes_read;
5682 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07005683 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005684 }
5685
5686 *frame_size = output_size;
5687 return AOM_CODEC_OK;
5688}
5689
Yaowu Xuf883b422016-08-30 14:01:10 -07005690void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005691 // TODO(yunqingwang): For what references to use, external encoding flags
5692 // should be consistent with internal reference frame selection. Need to
5693 // ensure that there is not conflict between the two. In AV1 encoder, the
5694 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
5695 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
5696 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005697 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005698 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005699 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
5700 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
5701 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005702 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005703 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005704 } else {
5705 int ref = AOM_REFFRAME_ALL;
5706
5707 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
5708 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
5709
5710 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
5711
5712 if (flags & AOM_EFLAG_NO_REF_ARF) {
5713 ref ^= AOM_ALT_FLAG;
5714 ref ^= AOM_BWD_FLAG;
5715 ref ^= AOM_ALT2_FLAG;
5716 } else {
5717 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
5718 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
5719 }
5720
5721 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005722 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005723 }
5724
5725 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005726 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005727 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005728
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005729 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
5730 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005731
Yaowu Xuf883b422016-08-30 14:01:10 -07005732 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005733
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005734 if (flags & AOM_EFLAG_NO_UPD_ARF) {
5735 upd ^= AOM_ALT_FLAG;
5736 upd ^= AOM_BWD_FLAG;
5737 upd ^= AOM_ALT2_FLAG;
5738 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005739
David Turnerce9b5902019-01-23 17:25:47 +00005740 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
5741 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
5742 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
5743 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
5744 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
5745 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00005746 } else {
5747 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005748 }
5749
sarahparker21dbca42018-03-30 17:43:44 -07005750 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
5751 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07005752 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
5753 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07005754 cpi->ext_use_s_frame =
5755 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07005756 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07005757
Yaowu Xuf883b422016-08-30 14:01:10 -07005758 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
5759 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005760 }
5761}
Andrey Norkin795ba872018-03-06 13:24:14 -08005762
Tom Fineganf8d6a162018-08-21 10:47:55 -07005763aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
5764 if (!cpi) return NULL;
5765
5766 uint8_t header_buf[512] = { 0 };
5767 const uint32_t sequence_header_size =
Yaowu Xu797674b2019-05-01 17:38:11 -07005768 av1_write_sequence_header_obu(cpi, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07005769 assert(sequence_header_size <= sizeof(header_buf));
5770 if (sequence_header_size == 0) return NULL;
5771
5772 const size_t obu_header_size = 1;
5773 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
5774 const size_t payload_offset = obu_header_size + size_field_size;
5775
5776 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
5777 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
5778
Hui Su4fd11762019-03-26 16:05:07 -07005779 if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
Tom Fineganf8d6a162018-08-21 10:47:55 -07005780 obu_header_size) {
5781 return NULL;
5782 }
5783
5784 size_t coded_size_field_size = 0;
5785 if (aom_uleb_encode(sequence_header_size, size_field_size,
5786 &header_buf[obu_header_size],
5787 &coded_size_field_size) != 0) {
5788 return NULL;
5789 }
5790 assert(coded_size_field_size == size_field_size);
5791
5792 aom_fixed_buf_t *global_headers =
5793 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
5794 if (!global_headers) return NULL;
5795
5796 const size_t global_header_buf_size =
5797 obu_header_size + size_field_size + sequence_header_size;
5798
5799 global_headers->buf = malloc(global_header_buf_size);
5800 if (!global_headers->buf) {
5801 free(global_headers);
5802 return NULL;
5803 }
5804
5805 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
5806 global_headers->sz = global_header_buf_size;
5807 return global_headers;
5808}