blob: ea81fffd34b5c70da54c65fbb5af98f781dc16b1 [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));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700389
390 memset(cm->mi_grid_base, 0,
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530391 cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi_grid_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700392}
393
Cheng Chen46f30c72017-09-07 11:13:33 -0700394static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700395 cm->mi = aom_calloc(mi_size, sizeof(*cm->mi));
396 if (!cm->mi) return 1;
397 cm->prev_mi = aom_calloc(mi_size, sizeof(*cm->prev_mi));
398 if (!cm->prev_mi) return 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700399 cm->mi_alloc_size = mi_size;
400
Yue Chen53b53f02018-03-29 14:31:23 -0700401 cm->mi_grid_base =
402 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700403 if (!cm->mi_grid_base) return 1;
404 cm->prev_mi_grid_base =
Yue Chen53b53f02018-03-29 14:31:23 -0700405 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700406 if (!cm->prev_mi_grid_base) return 1;
407
408 return 0;
409}
410
Cheng Chen46f30c72017-09-07 11:13:33 -0700411static void enc_free_mi(AV1_COMMON *cm) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700412 aom_free(cm->mi);
413 cm->mi = NULL;
414 aom_free(cm->prev_mi);
415 cm->prev_mi = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700416 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700418 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700419 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700420 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421}
422
Cheng Chen46f30c72017-09-07 11:13:33 -0700423static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
chiyotsai04ca87d2019-05-24 15:06:19 -0700424 // Current mi will be the prev_mi for the next frame.
Yue Chen53b53f02018-03-29 14:31:23 -0700425 MB_MODE_INFO **temp_base = cm->prev_mi_grid_base;
chiyotsai04ca87d2019-05-24 15:06:19 -0700426 MB_MODE_INFO *temp = cm->prev_mi;
427 cm->prev_mi = cm->mi;
428 cm->mi = temp;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700429
430 // Update the upper left visible macroblock ptrs.
Yaowu Xuc27fc142016-08-22 16:08:15 -0700431 cm->prev_mi_grid_base = cm->mi_grid_base;
432 cm->mi_grid_base = temp_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700433}
434
Yaowu Xuf883b422016-08-30 14:01:10 -0700435void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700436 av1_rtcd();
437 aom_dsp_rtcd();
438 aom_scale_rtcd();
439 av1_init_intra_predictors();
440 av1_init_me_luts();
441 av1_rc_init_minq_luts();
442 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700443}
444
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700445static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
446 if (cpi->mbmi_ext_base) {
447 aom_free(cpi->mbmi_ext_base);
448 cpi->mbmi_ext_base = NULL;
449 }
450}
451
452static void alloc_context_buffers_ext(AV1_COMP *cpi) {
453 AV1_COMMON *cm = &cpi->common;
454 int mi_size = cm->mi_cols * cm->mi_rows;
455
456 dealloc_context_buffers_ext(cpi);
457 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
458 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
459}
460
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800461static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
462 pars->num_cr_points = 0;
463 pars->cr_mult = 0;
464 pars->cr_luma_mult = 0;
465 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
466 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
467 pars->num_cb_points = 0;
468 pars->cb_mult = 0;
469 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800470 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800471 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
472 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
473}
474
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800475static void update_film_grain_parameters(struct AV1_COMP *cpi,
476 const AV1EncoderConfig *oxcf) {
477 AV1_COMMON *const cm = &cpi->common;
478 cpi->oxcf = *oxcf;
479
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700480 if (cpi->film_grain_table) {
481 aom_film_grain_table_free(cpi->film_grain_table);
482 aom_free(cpi->film_grain_table);
483 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700484 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700485
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800486 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700487 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000488 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800489 memcpy(&cm->film_grain_params,
490 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
491 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800492 if (oxcf->monochrome)
493 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700494 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
495 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800496 cm->film_grain_params.clip_to_restricted_range = 0;
497 }
498 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700499 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800500 cm->seq_params.film_grain_params_present = 1;
501
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700502 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
503 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700504
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700505 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700506 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800507 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800508#if CONFIG_DENOISE
509 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
510#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700511 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800512#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800513 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
514 }
515}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800516
Yaowu Xuf883b422016-08-30 14:01:10 -0700517static void dealloc_compressor_data(AV1_COMP *cpi) {
518 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000519 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700520
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700521 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700522
Yaowu Xuf883b422016-08-30 14:01:10 -0700523 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700524 cpi->tile_data = NULL;
525
526 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700527 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700528 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529
Yaowu Xuf883b422016-08-30 14:01:10 -0700530 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531 cpi->cyclic_refresh = NULL;
532
Yaowu Xuf883b422016-08-30 14:01:10 -0700533 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534 cpi->active_map.map = NULL;
535
sdengc23c7f12019-06-11 16:56:50 -0700536 aom_free(cpi->ssim_rdmult_scaling_factors);
537 cpi->ssim_rdmult_scaling_factors = NULL;
538
Jingning Hand064cf02017-06-01 10:00:39 -0700539 aom_free(cpi->td.mb.above_pred_buf);
540 cpi->td.mb.above_pred_buf = NULL;
541
542 aom_free(cpi->td.mb.left_pred_buf);
543 cpi->td.mb.left_pred_buf = NULL;
544
545 aom_free(cpi->td.mb.wsrc_buf);
546 cpi->td.mb.wsrc_buf = NULL;
547
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530548 aom_free(cpi->td.mb.inter_modes_info);
549 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530550
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530551 for (int i = 0; i < 2; i++)
552 for (int j = 0; j < 2; j++) {
553 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
554 cpi->td.mb.hash_value_buffer[i][j] = NULL;
555 }
Jingning Hand064cf02017-06-01 10:00:39 -0700556 aom_free(cpi->td.mb.mask_buf);
557 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700558
Jingning Han6cc1fd32017-10-13 09:05:36 -0700559 aom_free(cm->tpl_mvs);
560 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700561
Yaowu Xuf883b422016-08-30 14:01:10 -0700562 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700563 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700564 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700565
Yaowu Xuf883b422016-08-30 14:01:10 -0700566 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700567 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800568 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700569 aom_free_frame_buffer(&cpi->scaled_source);
570 aom_free_frame_buffer(&cpi->scaled_last_source);
571 aom_free_frame_buffer(&cpi->alt_ref_buffer);
Cheng Chen7abe3132019-06-19 11:55:28 -0700572 aom_free_frame_buffer(&cpi->source_kf_buffer);
Yaowu Xuf883b422016-08-30 14:01:10 -0700573 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);
Hui Su38711e72019-06-11 10:49:47 -0700584 av1_release_compound_type_rd_buffers(&cpi->td.mb.comp_rd_buffer);
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
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700667#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -0700668 // Scan frames from current to arf frame.
669 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700670 av1_update_mbgraph_stats(cpi);
Jerome Jiang6acbfdc2019-05-30 10:05:17 -0700671#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -0700672
673 // If segmentation was enabled set those features needed for the
674 // arf itself.
675 if (seg->enabled) {
676 seg->update_map = 1;
677 seg->update_data = 1;
678
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700679 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
680 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700681 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700682 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
683 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
684 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
685 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
686
687 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
688 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
689 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
690 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700691
Yaowu Xuf883b422016-08-30 14:01:10 -0700692 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700693 }
694 } else if (seg->enabled) {
695 // All other frames if segmentation has been enabled
696
697 // First normal frame in a valid gf or alt ref group
698 if (rc->frames_since_golden == 0) {
699 // Set up segment features for normal frames in an arf group
700 if (rc->source_alt_ref_active) {
701 seg->update_map = 0;
702 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700703
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700704 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
705 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700706 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
707 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700708
Cheng Chend8184da2017-09-26 18:15:22 -0700709 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
710 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
711 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
712 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
713
714 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
715 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
716 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
717 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700718
719 // Segment coding disabled for compred testing
720 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700721 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
722 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
723 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700724 }
725 } else {
726 // Disable segmentation and clear down features if alt ref
727 // is not active for this group
728
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730
731 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
732
733 seg->update_map = 0;
734 seg->update_data = 0;
735
Yaowu Xuf883b422016-08-30 14:01:10 -0700736 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700737 }
738 } else if (rc->is_src_frame_alt_ref) {
739 // Special case where we are coding over the top of a previous
740 // alt ref frame.
741 // Segment coding disabled for compred testing
742
743 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700744 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
745 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700746
747 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700748 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
749 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
750 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
751 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700752
753 // Skip all MBs if high Q (0,0 mv and skip coeffs)
754 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700755 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
756 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700757 }
758 // Enable data update
759 seg->update_data = 1;
760 } else {
761 // All other frames.
762
763 // No updates.. leave things as they are.
764 seg->update_map = 0;
765 seg->update_data = 0;
766 }
767 }
768}
769
Yaowu Xuf883b422016-08-30 14:01:10 -0700770static void update_reference_segmentation_map(AV1_COMP *cpi) {
771 AV1_COMMON *const cm = &cpi->common;
chiyotsai2202ba02019-07-12 16:09:21 -0700772 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_base;
David Turnerb757ce02018-11-12 15:01:28 +0000773 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700774 int row, col;
775
776 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700777 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700778 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700779 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
780 cache[0] = mi_4x4[0]->segment_id;
781 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700782 cache_ptr += cm->mi_cols;
783 }
784}
785
Yaowu Xuf883b422016-08-30 14:01:10 -0700786static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
787 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700788 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700789 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700790
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530791 if (!cpi->lookahead) {
792 int is_scale = (oxcf->resize_mode || oxcf->superres_mode);
Satish Kumar Suman29909962019-01-09 10:31:21 +0530793 cpi->lookahead = av1_lookahead_init(
794 oxcf->width, oxcf->height, seq_params->subsampling_x,
795 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman7a7239b2019-03-13 14:48:14 +0530796 oxcf->lag_in_frames, oxcf->border_in_pixels, is_scale);
797 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700798 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 "Failed to allocate lag buffers");
801
802 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700803 if (aom_realloc_frame_buffer(
804 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
805 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530806 seq_params->use_highbitdepth, oxcf->border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700807 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700808 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700809 "Failed to allocate altref buffer");
Cheng Chen7abe3132019-06-19 11:55:28 -0700810
811 // Allocate frame buffer to hold source frame whey key frame filtering
812 // is applied.
813 if (aom_realloc_frame_buffer(
814 &cpi->source_kf_buffer, oxcf->width, oxcf->height,
815 seq_params->subsampling_x, seq_params->subsampling_y,
816 seq_params->use_highbitdepth, oxcf->border_in_pixels,
817 cm->byte_alignment, NULL, NULL, NULL))
818 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
819 "Failed to allocate altref buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820}
821
Yaowu Xuf883b422016-08-30 14:01:10 -0700822static void alloc_util_frame_buffers(AV1_COMP *cpi) {
823 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700824 const SequenceHeader *const seq_params = &cm->seq_params;
825 if (aom_realloc_frame_buffer(
826 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
827 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530828 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700829 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700830 "Failed to allocate last frame buffer");
831
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700832 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800833 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700834 cm->superres_upscaled_height, seq_params->subsampling_x,
835 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +0530836 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800837 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800838 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700839
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700840 if (aom_realloc_frame_buffer(
841 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
842 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530843 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700844 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700845 "Failed to allocate scaled source buffer");
846
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700847 if (aom_realloc_frame_buffer(
848 &cpi->scaled_last_source, cm->width, cm->height,
849 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530850 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700851 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700852 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700853 "Failed to allocate scaled last source buffer");
854}
855
Cheng Chen46f30c72017-09-07 11:13:33 -0700856static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700857 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000858 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700859
Yaowu Xuf883b422016-08-30 14:01:10 -0700860 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700861
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530862 int mi_rows_aligned_to_sb =
863 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
864 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
865
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700866 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700867
Yaowu Xuc27fc142016-08-22 16:08:15 -0700868 alloc_context_buffers_ext(cpi);
869
Yaowu Xuf883b422016-08-30 14:01:10 -0700870 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700871
872 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000873 unsigned int tokens =
874 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700876 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700877 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530878 aom_free(cpi->tplist[0][0]);
879
880 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
881 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
882 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700883
Yaowu Xuf883b422016-08-30 14:01:10 -0700884 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700885}
886
Yaowu Xuf883b422016-08-30 14:01:10 -0700887void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700888 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700889 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700890}
891
Hui Suef139e12019-05-20 15:51:22 -0700892double av1_get_compression_ratio(const AV1_COMMON *const cm,
893 size_t encoded_frame_size) {
894 const int upscaled_width = cm->superres_upscaled_width;
895 const int height = cm->height;
896 const int luma_pic_size = upscaled_width * height;
897 const SequenceHeader *const seq_params = &cm->seq_params;
898 const BITSTREAM_PROFILE profile = seq_params->profile;
899 const int pic_size_profile_factor =
900 profile == PROFILE_0 ? 15 : (profile == PROFILE_1 ? 30 : 36);
901 encoded_frame_size =
902 (encoded_frame_size > 129 ? encoded_frame_size - 128 : 1);
903 const size_t uncompressed_frame_size =
904 (luma_pic_size * pic_size_profile_factor) >> 3;
905 return uncompressed_frame_size / (double)encoded_frame_size;
906}
907
Yunqing Wang75e20e82018-06-16 12:10:48 -0700908static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200909 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200910 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200911
912 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200913
914 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200915 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200916 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200917 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
918 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200919 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000920 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
921 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200922 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200923 cm->uniform_tile_spacing_flag = 0;
924 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
925 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200926 size_sb = cpi->oxcf.tile_widths[j++];
927 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000928 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200929 }
930 cm->tile_cols = i;
931 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200932 }
933 av1_calculate_tile_cols(cm);
934
935 // configure tile rows
936 if (cm->uniform_tile_spacing_flag) {
937 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
938 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200939 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000940 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
941 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200942 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200943 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
944 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200945 size_sb = cpi->oxcf.tile_heights[j++];
946 if (j >= cpi->oxcf.tile_height_count) j = 0;
947 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200948 }
949 cm->tile_rows = i;
950 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200951 }
952 av1_calculate_tile_rows(cm);
953}
954
Yaowu Xuf883b422016-08-30 14:01:10 -0700955static void update_frame_size(AV1_COMP *cpi) {
956 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700957 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
958
Yaowu Xuf883b422016-08-30 14:01:10 -0700959 av1_set_mb_mi(cm, cm->width, cm->height);
960 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500961 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700962 memset(cpi->mbmi_ext_base, 0,
963 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700964 set_tile_info(cpi);
965}
966
Yaowu Xuf883b422016-08-30 14:01:10 -0700967static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700968 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700969 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +0000970 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700971 cpi->rate_index = 0;
972 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700973}
974
Debargha Mukherjee57498692018-05-11 13:29:31 -0700975static INLINE int does_level_match(int width, int height, double fps,
976 int lvl_width, int lvl_height,
977 double lvl_fps, int lvl_dim_mult) {
978 const int64_t lvl_luma_pels = lvl_width * lvl_height;
979 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
980 const int64_t luma_pels = width * height;
981 const double display_sample_rate = luma_pels * fps;
982 return luma_pels <= lvl_luma_pels &&
983 display_sample_rate <= lvl_display_sample_rate &&
984 width <= lvl_width * lvl_dim_mult &&
985 height <= lvl_height * lvl_dim_mult;
986}
987
Andrey Norkin26495512018-06-20 17:13:11 -0700988static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -0700989 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700990 // TODO(any): This is a placeholder function that only addresses dimensions
991 // and max display sample rates.
992 // Need to add checks for max bit rate, max decoded luma sample rate, header
993 // rate, etc. that are not covered by this function.
Hui Su8427ff22019-03-11 10:14:33 -0700994 AV1_LEVEL level = SEQ_LEVEL_MAX;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700995 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
996 288, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -0700997 level = SEQ_LEVEL_2_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700998 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
999 704, 396, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001000 level = SEQ_LEVEL_2_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001001 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1002 1088, 612, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001003 level = SEQ_LEVEL_3_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001004 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1005 1376, 774, 30.0, 4)) {
Hui Su8427ff22019-03-11 10:14:33 -07001006 level = SEQ_LEVEL_3_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001007 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1008 2048, 1152, 30.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001009 level = SEQ_LEVEL_4_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001010 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1011 2048, 1152, 60.0, 3)) {
Hui Su8427ff22019-03-11 10:14:33 -07001012 level = SEQ_LEVEL_4_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001013 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1014 4096, 2176, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001015 level = SEQ_LEVEL_5_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001016 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1017 4096, 2176, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001018 level = SEQ_LEVEL_5_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001019 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1020 4096, 2176, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001021 level = SEQ_LEVEL_5_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001022 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1023 8192, 4352, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001024 level = SEQ_LEVEL_6_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001025 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1026 8192, 4352, 60.0, 2)) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001027 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1028 8192, 4352, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001029 level = SEQ_LEVEL_6_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001030 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1031 16384, 8704, 30.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001032 level = SEQ_LEVEL_7_0;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001033 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1034 16384, 8704, 60.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001035 level = SEQ_LEVEL_7_1;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001036 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1037 16384, 8704, 120.0, 2)) {
Hui Su8427ff22019-03-11 10:14:33 -07001038 level = SEQ_LEVEL_7_2;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001039 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001040 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su8427ff22019-03-11 10:14:33 -07001041 seq->seq_level_idx[i] = level;
Andrey Norkin26495512018-06-20 17:13:11 -07001042 // Set the maximum parameters for bitrate and buffer size for this profile,
1043 // level, and tier
Yaowu Xu7e450882019-04-30 15:09:18 -07001044 cm->op_params[i].bitrate = av1_max_level_bitrate(
Hui Su8427ff22019-03-11 10:14:33 -07001045 cm->seq_params.profile, seq->seq_level_idx[i], seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001046 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1047 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001048 if (cm->op_params[i].bitrate == 0)
1049 aom_internal_error(
1050 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1051 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001052 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001053 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001054 }
1055}
1056
Andrey Norkin26495512018-06-20 17:13:11 -07001057static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
1058 const AV1EncoderConfig *oxcf) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001059 seq->still_picture = (oxcf->limit == 1);
1060 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001061 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
kyslov94243382019-05-02 15:33:32 -07001062 seq->force_screen_content_tools = (oxcf->mode == REALTIME) ? 0 : 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001063 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001064 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001065 seq->frame_id_numbers_present_flag =
1066 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
1067 !oxcf->large_scale_tile && oxcf->error_resilient_mode;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001068 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001069 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001070 seq->force_screen_content_tools = 2;
1071 seq->force_integer_mv = 2;
1072 }
David Turnerebf96f42018-11-14 16:57:57 +00001073 seq->order_hint_info.order_hint_bits_minus_1 =
1074 seq->order_hint_info.enable_order_hint
1075 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1076 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001077
David Turner760a2f42018-12-07 15:25:36 +00001078 seq->max_frame_width =
1079 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1080 seq->max_frame_height = oxcf->forced_max_frame_height
1081 ? oxcf->forced_max_frame_height
1082 : oxcf->height;
1083 seq->num_bits_width =
1084 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1085 seq->num_bits_height =
1086 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1087 assert(seq->num_bits_width <= 16);
1088 assert(seq->num_bits_height <= 16);
1089
1090 seq->frame_id_length = FRAME_ID_LENGTH;
1091 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1092
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001093 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001094 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1095 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001096 seq->order_hint_info.enable_order_hint;
1097 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1098 seq->order_hint_info.enable_ref_frame_mvs &=
1099 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001100 seq->enable_superres = oxcf->enable_superres;
1101 seq->enable_cdef = oxcf->enable_cdef;
1102 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001103 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001104 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1105 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001106 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001107 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001108
Andrey Norkin26495512018-06-20 17:13:11 -07001109 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001110
1111 if (seq->operating_points_cnt_minus_1 == 0) {
1112 seq->operating_point_idc[0] = 0;
1113 } else {
1114 // Set operating_point_idc[] such that for the i-th operating point the
1115 // first (operating_points_cnt-i) spatial layers and the first temporal
1116 // layer are decoded Note that highest quality operating point should come
1117 // first
1118 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1119 seq->operating_point_idc[i] =
1120 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1121 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001122}
1123
Yaowu Xuf883b422016-08-30 14:01:10 -07001124static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1125 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001126
1127 cpi->oxcf = *oxcf;
1128 cpi->framerate = oxcf->init_framerate;
1129
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001130 cm->seq_params.profile = oxcf->profile;
1131 cm->seq_params.bit_depth = oxcf->bit_depth;
1132 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1133 cm->seq_params.color_primaries = oxcf->color_primaries;
1134 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1135 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001136 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001137 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1138 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001139 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001140 cm->timing_info.num_units_in_display_tick =
1141 oxcf->timing_info.num_units_in_display_tick;
1142 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1143 cm->timing_info.equal_picture_interval =
1144 oxcf->timing_info.equal_picture_interval;
1145 cm->timing_info.num_ticks_per_picture =
1146 oxcf->timing_info.num_ticks_per_picture;
1147
Andrey Norkin26495512018-06-20 17:13:11 -07001148 cm->seq_params.display_model_info_present_flag =
1149 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001150 cm->seq_params.decoder_model_info_present_flag =
1151 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001152 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001153 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001154 cm->buffer_model.num_units_in_decoding_tick =
1155 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001156 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07001157 av1_set_aom_dec_model_info(&cm->buffer_model);
1158 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07001159 } else if (cm->timing_info_present &&
1160 cm->timing_info.equal_picture_interval &&
1161 !cm->seq_params.decoder_model_info_present_flag) {
1162 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07001163 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001164 } else {
1165 cm->op_params[0].initial_display_delay =
1166 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001167 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001168
Tom Fineganf8d6a162018-08-21 10:47:55 -07001169 if (cm->seq_params.monochrome) {
1170 cm->seq_params.subsampling_x = 1;
1171 cm->seq_params.subsampling_y = 1;
1172 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1173 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1174 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1175 cm->seq_params.subsampling_x = 0;
1176 cm->seq_params.subsampling_y = 0;
1177 } else {
1178 if (cm->seq_params.profile == 0) {
1179 cm->seq_params.subsampling_x = 1;
1180 cm->seq_params.subsampling_y = 1;
1181 } else if (cm->seq_params.profile == 1) {
1182 cm->seq_params.subsampling_x = 0;
1183 cm->seq_params.subsampling_y = 0;
1184 } else {
1185 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1186 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1187 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1188 } else {
1189 cm->seq_params.subsampling_x = 1;
1190 cm->seq_params.subsampling_y = 0;
1191 }
1192 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001193 }
1194
Yaowu Xuc27fc142016-08-22 16:08:15 -07001195 cm->width = oxcf->width;
1196 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001197 set_sb_size(&cm->seq_params,
1198 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001199 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001200
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001201 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001202
1203 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001204 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001205
1206 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001207 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001208
1209 cpi->static_mb_pct = 0;
1210 cpi->ref_frame_flags = 0;
1211
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001212 // Reset resize pending flags
1213 cpi->resize_pending_width = 0;
1214 cpi->resize_pending_height = 0;
1215
Yaowu Xuc27fc142016-08-22 16:08:15 -07001216 init_buffer_indices(cpi);
1217}
1218
1219static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001220 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001221 const int64_t bandwidth = oxcf->target_bandwidth;
1222 const int64_t starting = oxcf->starting_buffer_level_ms;
1223 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1224 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1225
1226 rc->starting_buffer_level = starting * bandwidth / 1000;
1227 rc->optimal_buffer_level =
1228 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1229 rc->maximum_buffer_size =
1230 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1231}
1232
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001233#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1234 cpi->fn_ptr[BT].sdf = SDF; \
1235 cpi->fn_ptr[BT].sdaf = SDAF; \
1236 cpi->fn_ptr[BT].vf = VF; \
1237 cpi->fn_ptr[BT].svf = SVF; \
1238 cpi->fn_ptr[BT].svaf = SVAF; \
1239 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1240 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001241 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001242
1243#define MAKE_BFP_SAD_WRAPPER(fnname) \
1244 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1245 int source_stride, \
1246 const uint8_t *ref_ptr, int ref_stride) { \
1247 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1248 } \
1249 static unsigned int fnname##_bits10( \
1250 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1251 int ref_stride) { \
1252 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1253 } \
1254 static unsigned int fnname##_bits12( \
1255 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1256 int ref_stride) { \
1257 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1258 }
1259
1260#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1261 static unsigned int fnname##_bits8( \
1262 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1263 int ref_stride, const uint8_t *second_pred) { \
1264 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1265 } \
1266 static unsigned int fnname##_bits10( \
1267 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1268 int ref_stride, const uint8_t *second_pred) { \
1269 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1270 2; \
1271 } \
1272 static unsigned int fnname##_bits12( \
1273 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1274 int ref_stride, const uint8_t *second_pred) { \
1275 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1276 4; \
1277 }
1278
Yaowu Xuc27fc142016-08-22 16:08:15 -07001279#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1280 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1281 const uint8_t *const ref_ptr[], int ref_stride, \
1282 unsigned int *sad_array) { \
1283 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1284 } \
1285 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1286 const uint8_t *const ref_ptr[], int ref_stride, \
1287 unsigned int *sad_array) { \
1288 int i; \
1289 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1290 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1291 } \
1292 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1293 const uint8_t *const ref_ptr[], int ref_stride, \
1294 unsigned int *sad_array) { \
1295 int i; \
1296 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1297 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1298 }
1299
Cheng Chenbf3d4962017-11-01 14:48:52 -07001300#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1301 static unsigned int fnname##_bits8( \
1302 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1303 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001304 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001305 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1306 jcp_param); \
1307 } \
1308 static unsigned int fnname##_bits10( \
1309 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1310 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001311 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001312 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1313 jcp_param) >> \
1314 2; \
1315 } \
1316 static unsigned int fnname##_bits12( \
1317 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1318 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001319 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001320 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1321 jcp_param) >> \
1322 4; \
1323 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001324
Yaowu Xuf883b422016-08-30 14:01:10 -07001325MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1326MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001327MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1328MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1329MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1330MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1331MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1332MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1333MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001334MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1335MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1336MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1337MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1338MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1339MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1340MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1341MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1342MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1343MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1344MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1345MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1346MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1347MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001348MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1349MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1350MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001351MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1352MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1353MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001354MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1355MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1356MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001357MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1358MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1359MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001360MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1361MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1362MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001363MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1364MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1365MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001366MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1367MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1368MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001369MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1370MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1371MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001372MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001373
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001374MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1375MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1376MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1377MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1378MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1379MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1380MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1381MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1382MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1383MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1384MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1385MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001386MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1387MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1388MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1389MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1390MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1391MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001392
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001393MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1394MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1395MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1396MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1397MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1398MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1399MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1400MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1401MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1402MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1403MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1404MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1405MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1406MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1407MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1408MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1409MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1410MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1411MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1412MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1413MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1414MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001415
David Barker0f3c94e2017-05-16 15:21:50 +01001416#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001417 cpi->fn_ptr[BT].msdf = MCSDF; \
1418 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001419
David Barkerc155e012017-05-11 13:54:54 +01001420#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1421 static unsigned int fnname##_bits8( \
1422 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1423 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1424 int m_stride, int invert_mask) { \
1425 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1426 second_pred_ptr, m, m_stride, invert_mask); \
1427 } \
1428 static unsigned int fnname##_bits10( \
1429 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1430 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1431 int m_stride, int invert_mask) { \
1432 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1433 second_pred_ptr, m, m_stride, invert_mask) >> \
1434 2; \
1435 } \
1436 static unsigned int fnname##_bits12( \
1437 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1438 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1439 int m_stride, int invert_mask) { \
1440 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1441 second_pred_ptr, m, m_stride, invert_mask) >> \
1442 4; \
1443 }
1444
David Barkerf19f35f2017-05-22 16:33:22 +01001445MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1446MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1447MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001448MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1449MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1450MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1451MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1452MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1453MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1454MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1455MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1456MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1457MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1458MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1459MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1460MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001461MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1462MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1463MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1464MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001465MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1466MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001467
Yaowu Xuc27fc142016-08-22 16:08:15 -07001468#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1469 cpi->fn_ptr[BT].osdf = OSDF; \
1470 cpi->fn_ptr[BT].ovf = OVF; \
1471 cpi->fn_ptr[BT].osvf = OSVF;
1472
1473#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1474 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1475 const int32_t *wsrc, \
1476 const int32_t *msk) { \
1477 return fnname(ref, ref_stride, wsrc, msk); \
1478 } \
1479 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1480 const int32_t *wsrc, \
1481 const int32_t *msk) { \
1482 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1483 } \
1484 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1485 const int32_t *wsrc, \
1486 const int32_t *msk) { \
1487 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1488 }
1489
Yaowu Xuf883b422016-08-30 14:01:10 -07001490MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1491MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1492MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001493MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1494MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1495MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1496MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1497MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1498MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1499MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1500MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1501MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1502MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1503MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1504MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1505MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001506MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1507MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1508MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1509MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001510MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1511MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001512
Yaowu Xuf883b422016-08-30 14:01:10 -07001513static void highbd_set_var_fns(AV1_COMP *const cpi) {
1514 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001515 if (cm->seq_params.use_highbitdepth) {
1516 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001517 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001518 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1519 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1520 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001521 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001522 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001523 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1524 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001525
1526 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1527 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1528 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001529 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001530 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001531 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1532 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001533
1534 HIGHBD_BFP(
1535 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1536 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001537 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001538 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1539 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001540
1541 HIGHBD_BFP(
1542 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1543 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001544 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001545 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1546 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001547
1548 HIGHBD_BFP(
1549 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1550 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001551 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001552 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1553 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001554
1555 HIGHBD_BFP(
1556 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1557 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001558 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001559 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1560 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001561
1562 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1563 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1564 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001565 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001566 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001567 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1568 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001569
1570 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1571 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1572 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001573 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001574 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001575 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1576 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001577
1578 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1579 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1580 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001581 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001582 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001583 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1584 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001585
1586 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1587 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1588 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001589 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001590 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001591 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1592 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001593
1594 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1595 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1596 aom_highbd_8_sub_pixel_variance32x32,
1597 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001598 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001599 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1600 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001601
1602 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1603 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1604 aom_highbd_8_sub_pixel_variance64x64,
1605 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001606 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001607 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1608 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001609
1610 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1611 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1612 aom_highbd_8_sub_pixel_variance16x16,
1613 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001614 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001615 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1616 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001617
1618 HIGHBD_BFP(
1619 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1620 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001621 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001622 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1623 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001624
1625 HIGHBD_BFP(
1626 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1627 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001628 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001629 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1630 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001631
Cheng Chenbf3d4962017-11-01 14:48:52 -07001632 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001633 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1634 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1635 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1636 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1637 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1638
1639 HIGHBD_BFP(
1640 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1641 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1642 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1643 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1644 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1645
1646 HIGHBD_BFP(
1647 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1648 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1649 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1650 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1651 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1652
1653 HIGHBD_BFP(
1654 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1655 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1656 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1657 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1658 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1659
1660 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1661 aom_highbd_sad128x128_avg_bits8,
1662 aom_highbd_8_variance128x128,
1663 aom_highbd_8_sub_pixel_variance128x128,
1664 aom_highbd_8_sub_pixel_avg_variance128x128,
1665 aom_highbd_sad128x128x4d_bits8,
1666 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1667 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001668
1669 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1670 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1671 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001672 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001673 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001674 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1675 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001676
1677 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1678 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1679 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001680 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001681 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001682 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1683 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001684
David Barkerf19f35f2017-05-22 16:33:22 +01001685 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1686 aom_highbd_8_masked_sub_pixel_variance128x128)
1687 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1688 aom_highbd_8_masked_sub_pixel_variance128x64)
1689 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1690 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001691 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1692 aom_highbd_8_masked_sub_pixel_variance64x64)
1693 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1694 aom_highbd_8_masked_sub_pixel_variance64x32)
1695 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1696 aom_highbd_8_masked_sub_pixel_variance32x64)
1697 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1698 aom_highbd_8_masked_sub_pixel_variance32x32)
1699 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1700 aom_highbd_8_masked_sub_pixel_variance32x16)
1701 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1702 aom_highbd_8_masked_sub_pixel_variance16x32)
1703 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1704 aom_highbd_8_masked_sub_pixel_variance16x16)
1705 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1706 aom_highbd_8_masked_sub_pixel_variance8x16)
1707 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1708 aom_highbd_8_masked_sub_pixel_variance16x8)
1709 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1710 aom_highbd_8_masked_sub_pixel_variance8x8)
1711 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1712 aom_highbd_8_masked_sub_pixel_variance4x8)
1713 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1714 aom_highbd_8_masked_sub_pixel_variance8x4)
1715 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1716 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001717 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1718 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001719 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1720 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001721 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1722 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001723 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1724 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001725 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1726 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001727 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1728 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001729 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1730 aom_highbd_obmc_variance128x128,
1731 aom_highbd_obmc_sub_pixel_variance128x128)
1732 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1733 aom_highbd_obmc_variance128x64,
1734 aom_highbd_obmc_sub_pixel_variance128x64)
1735 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1736 aom_highbd_obmc_variance64x128,
1737 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001738 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1739 aom_highbd_obmc_variance64x64,
1740 aom_highbd_obmc_sub_pixel_variance64x64)
1741 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1742 aom_highbd_obmc_variance64x32,
1743 aom_highbd_obmc_sub_pixel_variance64x32)
1744 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1745 aom_highbd_obmc_variance32x64,
1746 aom_highbd_obmc_sub_pixel_variance32x64)
1747 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1748 aom_highbd_obmc_variance32x32,
1749 aom_highbd_obmc_sub_pixel_variance32x32)
1750 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1751 aom_highbd_obmc_variance32x16,
1752 aom_highbd_obmc_sub_pixel_variance32x16)
1753 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1754 aom_highbd_obmc_variance16x32,
1755 aom_highbd_obmc_sub_pixel_variance16x32)
1756 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1757 aom_highbd_obmc_variance16x16,
1758 aom_highbd_obmc_sub_pixel_variance16x16)
1759 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1760 aom_highbd_obmc_variance8x16,
1761 aom_highbd_obmc_sub_pixel_variance8x16)
1762 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1763 aom_highbd_obmc_variance16x8,
1764 aom_highbd_obmc_sub_pixel_variance16x8)
1765 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1766 aom_highbd_obmc_variance8x8,
1767 aom_highbd_obmc_sub_pixel_variance8x8)
1768 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1769 aom_highbd_obmc_variance4x8,
1770 aom_highbd_obmc_sub_pixel_variance4x8)
1771 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1772 aom_highbd_obmc_variance8x4,
1773 aom_highbd_obmc_sub_pixel_variance8x4)
1774 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1775 aom_highbd_obmc_variance4x4,
1776 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001777 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1778 aom_highbd_obmc_variance64x16,
1779 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001780 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1781 aom_highbd_obmc_variance16x64,
1782 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001783 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1784 aom_highbd_obmc_variance32x8,
1785 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001786 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1787 aom_highbd_obmc_variance8x32,
1788 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001789 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1790 aom_highbd_obmc_variance16x4,
1791 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001792 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1793 aom_highbd_obmc_variance4x16,
1794 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001795 break;
1796
Yaowu Xuf883b422016-08-30 14:01:10 -07001797 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001798 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1799 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1800 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001801 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001802 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001803 aom_highbd_dist_wtd_sad64x16_avg_bits10,
1804 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001805
1806 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1807 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1808 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001809 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001810 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001811 aom_highbd_dist_wtd_sad16x64_avg_bits10,
1812 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001813
1814 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1815 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1816 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001817 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001818 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001819 aom_highbd_dist_wtd_sad32x8_avg_bits10,
1820 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001821
1822 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1823 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1824 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001825 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001826 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001827 aom_highbd_dist_wtd_sad8x32_avg_bits10,
1828 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001829
1830 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1831 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1832 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001833 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001834 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001835 aom_highbd_dist_wtd_sad16x4_avg_bits10,
1836 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001837
1838 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1839 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1840 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001841 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001842 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001843 aom_highbd_dist_wtd_sad4x16_avg_bits10,
1844 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001845
1846 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1847 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1848 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001849 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001850 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001851 aom_highbd_dist_wtd_sad32x16_avg_bits10,
1852 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001853
1854 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1855 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1856 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001857 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001858 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001859 aom_highbd_dist_wtd_sad16x32_avg_bits10,
1860 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001861
1862 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1863 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1864 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001865 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001866 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001867 aom_highbd_dist_wtd_sad64x32_avg_bits10,
1868 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001869
1870 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1871 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1872 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001873 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001874 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001875 aom_highbd_dist_wtd_sad32x64_avg_bits10,
1876 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001877
1878 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1879 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1880 aom_highbd_10_sub_pixel_variance32x32,
1881 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001882 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001883 aom_highbd_dist_wtd_sad32x32_avg_bits10,
1884 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001885
1886 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1887 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1888 aom_highbd_10_sub_pixel_variance64x64,
1889 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001890 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001891 aom_highbd_dist_wtd_sad64x64_avg_bits10,
1892 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001893
1894 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1895 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1896 aom_highbd_10_sub_pixel_variance16x16,
1897 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001898 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001899 aom_highbd_dist_wtd_sad16x16_avg_bits10,
1900 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001901
1902 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1903 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1904 aom_highbd_10_sub_pixel_variance16x8,
1905 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001906 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001907 aom_highbd_dist_wtd_sad16x8_avg_bits10,
1908 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001909
1910 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1911 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1912 aom_highbd_10_sub_pixel_variance8x16,
1913 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001914 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001915 aom_highbd_dist_wtd_sad8x16_avg_bits10,
1916 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001917
1918 HIGHBD_BFP(
1919 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1920 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001921 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001922 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
1923 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001924
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001925 HIGHBD_BFP(
1926 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1927 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1928 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001929 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
1930 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001931
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001932 HIGHBD_BFP(
1933 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1934 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1935 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001936 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
1937 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001938
1939 HIGHBD_BFP(
1940 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1941 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001942 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001943 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
1944 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001945
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001946 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1947 aom_highbd_sad128x128_avg_bits10,
1948 aom_highbd_10_variance128x128,
1949 aom_highbd_10_sub_pixel_variance128x128,
1950 aom_highbd_10_sub_pixel_avg_variance128x128,
1951 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001952 aom_highbd_dist_wtd_sad128x128_avg_bits10,
1953 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001954
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001955 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1956 aom_highbd_sad128x64_avg_bits10,
1957 aom_highbd_10_variance128x64,
1958 aom_highbd_10_sub_pixel_variance128x64,
1959 aom_highbd_10_sub_pixel_avg_variance128x64,
1960 aom_highbd_sad128x64x4d_bits10,
1961 aom_highbd_dist_wtd_sad128x64_avg_bits10,
1962 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001963
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001964 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1965 aom_highbd_sad64x128_avg_bits10,
1966 aom_highbd_10_variance64x128,
1967 aom_highbd_10_sub_pixel_variance64x128,
1968 aom_highbd_10_sub_pixel_avg_variance64x128,
1969 aom_highbd_sad64x128x4d_bits10,
1970 aom_highbd_dist_wtd_sad64x128_avg_bits10,
1971 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001972
David Barkerf19f35f2017-05-22 16:33:22 +01001973 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1974 aom_highbd_10_masked_sub_pixel_variance128x128)
1975 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1976 aom_highbd_10_masked_sub_pixel_variance128x64)
1977 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1978 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001979 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1980 aom_highbd_10_masked_sub_pixel_variance64x64)
1981 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1982 aom_highbd_10_masked_sub_pixel_variance64x32)
1983 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1984 aom_highbd_10_masked_sub_pixel_variance32x64)
1985 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1986 aom_highbd_10_masked_sub_pixel_variance32x32)
1987 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1988 aom_highbd_10_masked_sub_pixel_variance32x16)
1989 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1990 aom_highbd_10_masked_sub_pixel_variance16x32)
1991 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1992 aom_highbd_10_masked_sub_pixel_variance16x16)
1993 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1994 aom_highbd_10_masked_sub_pixel_variance8x16)
1995 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1996 aom_highbd_10_masked_sub_pixel_variance16x8)
1997 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1998 aom_highbd_10_masked_sub_pixel_variance8x8)
1999 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
2000 aom_highbd_10_masked_sub_pixel_variance4x8)
2001 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
2002 aom_highbd_10_masked_sub_pixel_variance8x4)
2003 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
2004 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002005 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
2006 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002007 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
2008 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002009 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2010 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002011 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2012 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002013 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2014 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002015 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2016 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002017 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2018 aom_highbd_10_obmc_variance128x128,
2019 aom_highbd_10_obmc_sub_pixel_variance128x128)
2020 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2021 aom_highbd_10_obmc_variance128x64,
2022 aom_highbd_10_obmc_sub_pixel_variance128x64)
2023 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2024 aom_highbd_10_obmc_variance64x128,
2025 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002026 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2027 aom_highbd_10_obmc_variance64x64,
2028 aom_highbd_10_obmc_sub_pixel_variance64x64)
2029 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2030 aom_highbd_10_obmc_variance64x32,
2031 aom_highbd_10_obmc_sub_pixel_variance64x32)
2032 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2033 aom_highbd_10_obmc_variance32x64,
2034 aom_highbd_10_obmc_sub_pixel_variance32x64)
2035 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2036 aom_highbd_10_obmc_variance32x32,
2037 aom_highbd_10_obmc_sub_pixel_variance32x32)
2038 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2039 aom_highbd_10_obmc_variance32x16,
2040 aom_highbd_10_obmc_sub_pixel_variance32x16)
2041 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2042 aom_highbd_10_obmc_variance16x32,
2043 aom_highbd_10_obmc_sub_pixel_variance16x32)
2044 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2045 aom_highbd_10_obmc_variance16x16,
2046 aom_highbd_10_obmc_sub_pixel_variance16x16)
2047 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2048 aom_highbd_10_obmc_variance8x16,
2049 aom_highbd_10_obmc_sub_pixel_variance8x16)
2050 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2051 aom_highbd_10_obmc_variance16x8,
2052 aom_highbd_10_obmc_sub_pixel_variance16x8)
2053 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2054 aom_highbd_10_obmc_variance8x8,
2055 aom_highbd_10_obmc_sub_pixel_variance8x8)
2056 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2057 aom_highbd_10_obmc_variance4x8,
2058 aom_highbd_10_obmc_sub_pixel_variance4x8)
2059 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2060 aom_highbd_10_obmc_variance8x4,
2061 aom_highbd_10_obmc_sub_pixel_variance8x4)
2062 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2063 aom_highbd_10_obmc_variance4x4,
2064 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002065
Rupert Swarbrick72678572017-08-02 12:05:26 +01002066 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2067 aom_highbd_10_obmc_variance64x16,
2068 aom_highbd_10_obmc_sub_pixel_variance64x16)
2069
2070 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2071 aom_highbd_10_obmc_variance16x64,
2072 aom_highbd_10_obmc_sub_pixel_variance16x64)
2073
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002074 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2075 aom_highbd_10_obmc_variance32x8,
2076 aom_highbd_10_obmc_sub_pixel_variance32x8)
2077
2078 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2079 aom_highbd_10_obmc_variance8x32,
2080 aom_highbd_10_obmc_sub_pixel_variance8x32)
2081
2082 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2083 aom_highbd_10_obmc_variance16x4,
2084 aom_highbd_10_obmc_sub_pixel_variance16x4)
2085
2086 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2087 aom_highbd_10_obmc_variance4x16,
2088 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002089 break;
2090
Yaowu Xuf883b422016-08-30 14:01:10 -07002091 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002092 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2093 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2094 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002095 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002096 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002097 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2098 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002099
2100 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2101 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2102 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002103 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002104 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002105 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2106 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002107
2108 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2109 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2110 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002111 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002112 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002113 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2114 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002115
2116 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2117 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2118 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002119 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002120 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002121 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2122 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002123
2124 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2125 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2126 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002127 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002128 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002129 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2130 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002131
2132 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2133 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2134 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002135 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002136 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002137 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2138 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002139
2140 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2141 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2142 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002143 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002144 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002145 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2146 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002147
2148 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2149 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2150 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002151 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002152 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002153 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2154 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002155
2156 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2157 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2158 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002159 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002160 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002161 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2162 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002163
2164 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2165 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2166 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002167 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002168 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002169 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2170 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002171
2172 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2173 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2174 aom_highbd_12_sub_pixel_variance32x32,
2175 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002176 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002177 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2178 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002179
2180 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2181 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2182 aom_highbd_12_sub_pixel_variance64x64,
2183 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002184 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002185 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2186 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002187
2188 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2189 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2190 aom_highbd_12_sub_pixel_variance16x16,
2191 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002192 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002193 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2194 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002195
2196 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2197 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2198 aom_highbd_12_sub_pixel_variance16x8,
2199 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002200 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002201 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2202 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002203
2204 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2205 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2206 aom_highbd_12_sub_pixel_variance8x16,
2207 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002208 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002209 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2210 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002211
2212 HIGHBD_BFP(
2213 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2214 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002215 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002216 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2217 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002218
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002219 HIGHBD_BFP(
2220 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2221 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2222 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002223 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2224 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002225
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002226 HIGHBD_BFP(
2227 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2228 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2229 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002230 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2231 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002232
2233 HIGHBD_BFP(
2234 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2235 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002236 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002237 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2238 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002239
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002240 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2241 aom_highbd_sad128x128_avg_bits12,
2242 aom_highbd_12_variance128x128,
2243 aom_highbd_12_sub_pixel_variance128x128,
2244 aom_highbd_12_sub_pixel_avg_variance128x128,
2245 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002246 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2247 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002248
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002249 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2250 aom_highbd_sad128x64_avg_bits12,
2251 aom_highbd_12_variance128x64,
2252 aom_highbd_12_sub_pixel_variance128x64,
2253 aom_highbd_12_sub_pixel_avg_variance128x64,
2254 aom_highbd_sad128x64x4d_bits12,
2255 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2256 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002257
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002258 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2259 aom_highbd_sad64x128_avg_bits12,
2260 aom_highbd_12_variance64x128,
2261 aom_highbd_12_sub_pixel_variance64x128,
2262 aom_highbd_12_sub_pixel_avg_variance64x128,
2263 aom_highbd_sad64x128x4d_bits12,
2264 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2265 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002266
David Barkerf19f35f2017-05-22 16:33:22 +01002267 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2268 aom_highbd_12_masked_sub_pixel_variance128x128)
2269 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2270 aom_highbd_12_masked_sub_pixel_variance128x64)
2271 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2272 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002273 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2274 aom_highbd_12_masked_sub_pixel_variance64x64)
2275 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2276 aom_highbd_12_masked_sub_pixel_variance64x32)
2277 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2278 aom_highbd_12_masked_sub_pixel_variance32x64)
2279 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2280 aom_highbd_12_masked_sub_pixel_variance32x32)
2281 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2282 aom_highbd_12_masked_sub_pixel_variance32x16)
2283 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2284 aom_highbd_12_masked_sub_pixel_variance16x32)
2285 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2286 aom_highbd_12_masked_sub_pixel_variance16x16)
2287 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2288 aom_highbd_12_masked_sub_pixel_variance8x16)
2289 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2290 aom_highbd_12_masked_sub_pixel_variance16x8)
2291 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2292 aom_highbd_12_masked_sub_pixel_variance8x8)
2293 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2294 aom_highbd_12_masked_sub_pixel_variance4x8)
2295 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2296 aom_highbd_12_masked_sub_pixel_variance8x4)
2297 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2298 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002299 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2300 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002301 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2302 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002303 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2304 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002305 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2306 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002307 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2308 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002309 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2310 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002311 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2312 aom_highbd_12_obmc_variance128x128,
2313 aom_highbd_12_obmc_sub_pixel_variance128x128)
2314 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2315 aom_highbd_12_obmc_variance128x64,
2316 aom_highbd_12_obmc_sub_pixel_variance128x64)
2317 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2318 aom_highbd_12_obmc_variance64x128,
2319 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002320 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2321 aom_highbd_12_obmc_variance64x64,
2322 aom_highbd_12_obmc_sub_pixel_variance64x64)
2323 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2324 aom_highbd_12_obmc_variance64x32,
2325 aom_highbd_12_obmc_sub_pixel_variance64x32)
2326 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2327 aom_highbd_12_obmc_variance32x64,
2328 aom_highbd_12_obmc_sub_pixel_variance32x64)
2329 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2330 aom_highbd_12_obmc_variance32x32,
2331 aom_highbd_12_obmc_sub_pixel_variance32x32)
2332 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2333 aom_highbd_12_obmc_variance32x16,
2334 aom_highbd_12_obmc_sub_pixel_variance32x16)
2335 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2336 aom_highbd_12_obmc_variance16x32,
2337 aom_highbd_12_obmc_sub_pixel_variance16x32)
2338 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2339 aom_highbd_12_obmc_variance16x16,
2340 aom_highbd_12_obmc_sub_pixel_variance16x16)
2341 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2342 aom_highbd_12_obmc_variance8x16,
2343 aom_highbd_12_obmc_sub_pixel_variance8x16)
2344 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2345 aom_highbd_12_obmc_variance16x8,
2346 aom_highbd_12_obmc_sub_pixel_variance16x8)
2347 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2348 aom_highbd_12_obmc_variance8x8,
2349 aom_highbd_12_obmc_sub_pixel_variance8x8)
2350 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2351 aom_highbd_12_obmc_variance4x8,
2352 aom_highbd_12_obmc_sub_pixel_variance4x8)
2353 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2354 aom_highbd_12_obmc_variance8x4,
2355 aom_highbd_12_obmc_sub_pixel_variance8x4)
2356 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2357 aom_highbd_12_obmc_variance4x4,
2358 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002359 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2360 aom_highbd_12_obmc_variance64x16,
2361 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002362 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2363 aom_highbd_12_obmc_variance16x64,
2364 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002365 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2366 aom_highbd_12_obmc_variance32x8,
2367 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002368 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2369 aom_highbd_12_obmc_variance8x32,
2370 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002371 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2372 aom_highbd_12_obmc_variance16x4,
2373 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002374 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2375 aom_highbd_12_obmc_variance4x16,
2376 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002377 break;
2378
2379 default:
2380 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002381 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002382 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002383 }
2384 }
2385}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386
Yaowu Xuf883b422016-08-30 14:01:10 -07002387static void realloc_segmentation_maps(AV1_COMP *cpi) {
2388 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002389
2390 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002391 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002393 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002394
2395 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002396 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002397 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002398 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002399
2400 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002401 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002402 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002403 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002404}
2405
Hui Su38711e72019-06-11 10:49:47 -07002406void av1_alloc_compound_type_rd_buffers(AV1_COMMON *const cm,
2407 CompoundTypeRdBuffers *const bufs) {
2408 CHECK_MEM_ERROR(
2409 cm, bufs->pred0,
2410 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred0)));
2411 CHECK_MEM_ERROR(
2412 cm, bufs->pred1,
2413 (uint8_t *)aom_memalign(16, 2 * MAX_SB_SQUARE * sizeof(*bufs->pred1)));
2414 CHECK_MEM_ERROR(
2415 cm, bufs->residual1,
2416 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->residual1)));
2417 CHECK_MEM_ERROR(
2418 cm, bufs->diff10,
2419 (int16_t *)aom_memalign(32, MAX_SB_SQUARE * sizeof(*bufs->diff10)));
2420 CHECK_MEM_ERROR(cm, bufs->tmp_best_mask_buf,
2421 (uint8_t *)aom_malloc(2 * MAX_SB_SQUARE *
2422 sizeof(*bufs->tmp_best_mask_buf)));
2423}
2424
2425void av1_release_compound_type_rd_buffers(CompoundTypeRdBuffers *const bufs) {
2426 aom_free(bufs->pred0);
2427 aom_free(bufs->pred1);
2428 aom_free(bufs->residual1);
2429 aom_free(bufs->diff10);
2430 aom_free(bufs->tmp_best_mask_buf);
2431 av1_zero(*bufs); // Set all pointers to NULL for safety.
2432}
2433
Yaowu Xuf883b422016-08-30 14:01:10 -07002434void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2435 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002436 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002437 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002438 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002439 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002440
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002441 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2442 seq_params->bit_depth = oxcf->bit_depth;
2443 seq_params->color_primaries = oxcf->color_primaries;
2444 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2445 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2446 seq_params->monochrome = oxcf->monochrome;
2447 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2448 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002449
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002450 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2451 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002452
Hui Su2aa492c2019-03-12 15:18:18 -07002453 memcpy(cpi->target_seq_level_idx, oxcf->target_seq_level_idx,
2454 sizeof(cpi->target_seq_level_idx));
2455 cpi->keep_level_stats = 0;
2456 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Hui Su6d666502019-06-13 15:16:50 -07002457 if (cpi->target_seq_level_idx[i] <= SEQ_LEVELS) {
Hui Su72ff0482019-05-28 14:07:37 -07002458 cpi->keep_level_stats |= 1u << i;
Hui Suc3a8d372019-05-28 15:52:45 -07002459 if (!cpi->level_info[i]) {
2460 CHECK_MEM_ERROR(cm, cpi->level_info[i],
2461 aom_calloc(1, sizeof(*cpi->level_info[i])));
2462 }
Hui Su2aa492c2019-03-12 15:18:18 -07002463 }
2464 }
kyslovabeeb7c2019-03-06 18:35:04 -08002465
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002466 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002467 cm->timing_info.num_units_in_display_tick =
2468 oxcf->timing_info.num_units_in_display_tick;
2469 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2470 cm->timing_info.equal_picture_interval =
2471 oxcf->timing_info.equal_picture_interval;
2472 cm->timing_info.num_ticks_per_picture =
2473 oxcf->timing_info.num_ticks_per_picture;
2474
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002475 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002476 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002477 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002478 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002479 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002480 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002481 cm->buffer_model.num_units_in_decoding_tick =
2482 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002483 cm->buffer_removal_time_present = 1;
Yaowu Xueb40e472019-05-03 09:17:37 -07002484 av1_set_aom_dec_model_info(&cm->buffer_model);
2485 av1_set_dec_model_op_parameters(&cm->op_params[0]);
Andrey Norkin26495512018-06-20 17:13:11 -07002486 } else if (cm->timing_info_present &&
2487 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002488 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002489 // set the decoder model parameters in resource availability mode
Yaowu Xueb40e472019-05-03 09:17:37 -07002490 av1_set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002491 } else {
2492 cm->op_params[0].initial_display_delay =
2493 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002494 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002495
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002496 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002497
Yaowu Xuc27fc142016-08-22 16:08:15 -07002498 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002499 cpi->common.options = oxcf->cfg;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002500 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002501 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002502
Yaowu Xuf883b422016-08-30 14:01:10 -07002503 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002504 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2505 } else {
2506 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2507 }
2508
2509 cpi->refresh_last_frame = 1;
2510 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002511 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002512 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002513
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002514 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2515 ? REFRESH_FRAME_CONTEXT_DISABLED
2516 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002517 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002518 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002519
Alex Converse74ad0912017-07-18 10:22:58 -07002520 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002521 CHECK_MEM_ERROR(cm, x->palette_buffer,
2522 aom_memalign(16, sizeof(*x->palette_buffer)));
2523 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002524
Hui Su38711e72019-06-11 10:49:47 -07002525 if (x->comp_rd_buffer.pred0 == NULL) {
2526 av1_alloc_compound_type_rd_buffers(cm, &x->comp_rd_buffer);
2527 }
2528
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002529 if (x->tmp_conv_dst == NULL) {
2530 CHECK_MEM_ERROR(
2531 cm, x->tmp_conv_dst,
2532 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002533 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002534 }
2535 for (int i = 0; i < 2; ++i) {
2536 if (x->tmp_obmc_bufs[i] == NULL) {
2537 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002538 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002539 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002540 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002541 }
2542 }
2543
Yaowu Xuf883b422016-08-30 14:01:10 -07002544 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002545 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002546
Yaowu Xuc27fc142016-08-22 16:08:15 -07002547 set_rc_buffer_sizes(rc, &cpi->oxcf);
2548
2549 // Under a configuration change, where maximum_buffer_size may change,
2550 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002551 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2552 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002553
2554 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002555 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002556
2557 // Set absolute upper and lower quality limits
2558 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2559 rc->best_quality = cpi->oxcf.best_allowed_q;
2560
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002561 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002562 cm->switchable_motion_mode = 1;
2563
Yaowu Xuc27fc142016-08-22 16:08:15 -07002564 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2565 cm->render_width = cpi->oxcf.render_width;
2566 cm->render_height = cpi->oxcf.render_height;
2567 } else {
2568 cm->render_width = cpi->oxcf.width;
2569 cm->render_height = cpi->oxcf.height;
2570 }
2571 cm->width = cpi->oxcf.width;
2572 cm->height = cpi->oxcf.height;
2573
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002574 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002575 // Superblock size should not be updated after the first key frame.
2576 if (!cpi->seq_params_locked) {
2577 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Hui Sud909c2c2019-03-08 11:51:14 -08002578 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i)
2579 seq_params->tier[i] = (oxcf->tier_mask >> i) & 1;
Urvang Joshie4530f82018-01-09 11:43:37 -08002580 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002581
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002582 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002583 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002584 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002585 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002586 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002587 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002588 realloc_segmentation_maps(cpi);
2589 cpi->initial_width = cpi->initial_height = 0;
2590 }
2591 }
2592 update_frame_size(cpi);
2593
2594 cpi->alt_ref_source = NULL;
2595 rc->is_src_frame_alt_ref = 0;
2596
Yaowu Xuc27fc142016-08-22 16:08:15 -07002597 set_tile_info(cpi);
2598
2599 cpi->ext_refresh_frame_flags_pending = 0;
2600 cpi->ext_refresh_frame_context_pending = 0;
2601
Yaowu Xuc27fc142016-08-22 16:08:15 -07002602 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002603
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002604 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002605 // This should not be called after the first key frame.
2606 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002607 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002608 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Andrey Norkin26495512018-06-20 17:13:11 -07002609 init_seq_coding_tools(&cm->seq_params, cm, oxcf);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002610 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002611}
2612
Yaowu Xuf883b422016-08-30 14:01:10 -07002613AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2614 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002615 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002616 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2617 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002618
2619 if (!cm) return NULL;
2620
Yaowu Xuf883b422016-08-30 14:01:10 -07002621 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002622
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002623 // The jmp_buf is valid only for the duration of the function that calls
2624 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2625 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002626 if (setjmp(cm->error.jmp)) {
2627 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002628 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002629 return 0;
2630 }
2631
2632 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002633 cm->alloc_mi = enc_alloc_mi;
2634 cm->free_mi = enc_free_mi;
2635 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002636
Angie Chianga5d96c42016-10-21 16:16:56 -07002637 CHECK_MEM_ERROR(cm, cm->fc,
2638 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002639 CHECK_MEM_ERROR(
2640 cm, cm->default_frame_context,
2641 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002642 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002643 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002644
2645 cpi->resize_state = 0;
2646 cpi->resize_avg_qp = 0;
2647 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002648
Yaowu Xuc27fc142016-08-22 16:08:15 -07002649 cpi->common.buffer_pool = pool;
2650
2651 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002652 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002653
David Turnerd2a592e2018-11-16 14:59:31 +00002654 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002655 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002656 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002657 cpi->partition_search_skippable_frame = 0;
2658 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00002659 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002660 realloc_segmentation_maps(cpi);
2661
Jingning Hanf050fc12018-03-09 14:53:33 -08002662 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2663 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002664
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2666 i++) {
2667 CHECK_MEM_ERROR(
2668 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002669 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002670 }
2671
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002673
2674 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2675#if CONFIG_INTERNAL_STATS
2676 cpi->b_calculate_blockiness = 1;
2677 cpi->b_calculate_consistency = 1;
2678 cpi->total_inconsistency = 0;
2679 cpi->psnr.worst = 100.0;
2680 cpi->worst_ssim = 100.0;
2681
2682 cpi->count = 0;
2683 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08002684#if CONFIG_SPEED_STATS
2685 cpi->tx_search_count = 0;
2686#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002687
2688 if (cpi->b_calculate_psnr) {
2689 cpi->total_sq_error = 0;
2690 cpi->total_samples = 0;
2691 cpi->tot_recode_hits = 0;
2692 cpi->summed_quality = 0;
2693 cpi->summed_weights = 0;
2694 }
2695
2696 cpi->fastssim.worst = 100.0;
2697 cpi->psnrhvs.worst = 100.0;
2698
2699 if (cpi->b_calculate_blockiness) {
2700 cpi->total_blockiness = 0;
2701 cpi->worst_blockiness = 0.0;
2702 }
2703
2704 if (cpi->b_calculate_consistency) {
2705 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002706 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002707 cpi->common.mi_rows * cpi->common.mi_cols));
2708 cpi->worst_consistency = 100.0;
2709 }
2710#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002711#if CONFIG_ENTROPY_STATS
2712 av1_zero(aggregate_fc);
2713#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002714
2715 cpi->first_time_stamp_ever = INT64_MAX;
2716
Jingning Hanf050fc12018-03-09 14:53:33 -08002717 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2718 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2719 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2720 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002721
Yaowu Xuc27fc142016-08-22 16:08:15 -07002722#ifdef OUTPUT_YUV_SKINMAP
2723 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2724#endif
2725#ifdef OUTPUT_YUV_REC
2726 yuv_rec_file = fopen("rec.yuv", "wb");
2727#endif
2728
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002729#if !CONFIG_REALTIME_ONLY
Yaowu Xuc27fc142016-08-22 16:08:15 -07002730 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002731 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002732 } else if (oxcf->pass == 2) {
2733 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2734 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2735
Yaowu Xuc27fc142016-08-22 16:08:15 -07002736 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2737 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2738 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2739
Yaowu Xuf883b422016-08-30 14:01:10 -07002740 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002741 }
Jerome Jiang2612b4d2019-05-29 17:46:47 -07002742#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07002743
Jingning Hand064cf02017-06-01 10:00:39 -07002744 CHECK_MEM_ERROR(
2745 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002746 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002747 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002748 CHECK_MEM_ERROR(
2749 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002750 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002751 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002752
2753 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2754 (int32_t *)aom_memalign(
2755 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2756
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302757 CHECK_MEM_ERROR(
2758 cm, cpi->td.mb.inter_modes_info,
2759 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302760
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302761 for (int x = 0; x < 2; x++)
2762 for (int y = 0; y < 2; y++)
2763 CHECK_MEM_ERROR(
2764 cm, cpi->td.mb.hash_value_buffer[x][y],
2765 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2766 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2767
2768 cpi->td.mb.g_crc_initialized = 0;
2769
Jingning Hand064cf02017-06-01 10:00:39 -07002770 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2771 (int32_t *)aom_memalign(
2772 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2773
David Turner04b70d82019-01-24 15:39:19 +00002774 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
2775 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002776
sdengc23c7f12019-06-11 16:56:50 -07002777 {
2778 const int bsize = BLOCK_16X16;
2779 const int w = mi_size_wide[bsize];
2780 const int h = mi_size_high[bsize];
2781 const int num_cols = (cm->mi_cols + w - 1) / w;
2782 const int num_rows = (cm->mi_rows + h - 1) / h;
2783 CHECK_MEM_ERROR(cm, cpi->ssim_rdmult_scaling_factors,
2784 aom_calloc(num_rows * num_cols,
2785 sizeof(*cpi->ssim_rdmult_scaling_factors)));
2786 }
2787
Yue Chenc9b23e02019-04-10 16:54:03 -07002788 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chen7cae98f2018-08-24 10:43:16 -07002789 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2790 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2791
2792 CHECK_MEM_ERROR(cm, cpi->tpl_stats[frame].tpl_stats_ptr,
2793 aom_calloc(mi_rows * mi_cols,
2794 sizeof(*cpi->tpl_stats[frame].tpl_stats_ptr)));
2795 cpi->tpl_stats[frame].is_valid = 0;
2796 cpi->tpl_stats[frame].width = mi_cols;
2797 cpi->tpl_stats[frame].height = mi_rows;
2798 cpi->tpl_stats[frame].stride = mi_cols;
2799 cpi->tpl_stats[frame].mi_rows = cm->mi_rows;
2800 cpi->tpl_stats[frame].mi_cols = cm->mi_cols;
2801 }
2802
chiyotsai9c484b32019-03-07 16:01:50 -08002803#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08002804 av1_zero(cpi->partition_stats);
2805#endif
2806
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002807#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2808 cpi->fn_ptr[BT].sdf = SDF; \
2809 cpi->fn_ptr[BT].sdaf = SDAF; \
2810 cpi->fn_ptr[BT].vf = VF; \
2811 cpi->fn_ptr[BT].svf = SVF; \
2812 cpi->fn_ptr[BT].svaf = SVAF; \
2813 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2814 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002815 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002816
Cheng Chenf78632e2017-10-20 15:30:51 -07002817 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002818 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002819 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
2820 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002821
2822 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002823 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002824 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
2825 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002826
2827 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002828 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002829 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
2830 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002831
2832 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002833 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002834 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
2835 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002836
2837 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002838 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002839 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
2840 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002841
2842 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002843 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002844 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
2845 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002846
Cheng Chenf78632e2017-10-20 15:30:51 -07002847 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2848 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002849 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
2850 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002851
2852 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002853 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002854 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
2855 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002856
2857 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002858 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002859 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
2860 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002861
2862 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002863 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002864 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
2865 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002866
2867 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002868 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002869 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
2870 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002871
2872 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002873 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002874 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
2875 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002876
2877 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002878 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002879 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
2880 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002881
2882 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2883 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002884 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
2885 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002886
2887 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2888 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002889 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
2890 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002891
2892 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2893 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002894 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
2895 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002896
2897 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002898 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002899 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
2900 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002901
2902 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002903 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002904 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
2905 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002906
2907 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002908 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002909 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002910
2911 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002912 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002913 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002914
2915 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002916 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002917 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002918
2919 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002920 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002921 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002922
Yaowu Xuc27fc142016-08-22 16:08:15 -07002923#define OBFP(BT, OSDF, OVF, OSVF) \
2924 cpi->fn_ptr[BT].osdf = OSDF; \
2925 cpi->fn_ptr[BT].ovf = OVF; \
2926 cpi->fn_ptr[BT].osvf = OSVF;
2927
Yaowu Xuf883b422016-08-30 14:01:10 -07002928 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2929 aom_obmc_sub_pixel_variance128x128)
2930 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2931 aom_obmc_sub_pixel_variance128x64)
2932 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2933 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002934 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2935 aom_obmc_sub_pixel_variance64x64)
2936 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2937 aom_obmc_sub_pixel_variance64x32)
2938 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2939 aom_obmc_sub_pixel_variance32x64)
2940 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2941 aom_obmc_sub_pixel_variance32x32)
2942 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2943 aom_obmc_sub_pixel_variance32x16)
2944 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2945 aom_obmc_sub_pixel_variance16x32)
2946 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2947 aom_obmc_sub_pixel_variance16x16)
2948 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2949 aom_obmc_sub_pixel_variance16x8)
2950 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2951 aom_obmc_sub_pixel_variance8x16)
2952 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2953 aom_obmc_sub_pixel_variance8x8)
2954 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2955 aom_obmc_sub_pixel_variance4x8)
2956 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2957 aom_obmc_sub_pixel_variance8x4)
2958 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2959 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002960 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2961 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002962 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2963 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002964 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2965 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002966 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2967 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002968 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2969 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002970 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2971 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002972
David Barkerf19f35f2017-05-22 16:33:22 +01002973#define MBFP(BT, MCSDF, MCSVF) \
2974 cpi->fn_ptr[BT].msdf = MCSDF; \
2975 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002976
David Barkerf19f35f2017-05-22 16:33:22 +01002977 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2978 aom_masked_sub_pixel_variance128x128)
2979 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2980 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002981 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2982 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2983 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2984 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2985 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2986 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2987 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2988 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2989 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2990 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2991 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2992 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2993 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002994
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002995 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2996
2997 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2998
2999 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
3000
3001 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01003002
3003 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
3004
3005 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01003006
Yaowu Xuc27fc142016-08-22 16:08:15 -07003007 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003008
Yaowu Xuf883b422016-08-30 14:01:10 -07003009 /* av1_init_quantizer() is first called here. Add check in
3010 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07003011 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07003012 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07003013 */
Yaowu Xuf883b422016-08-30 14:01:10 -07003014 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08003015 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003016
Yaowu Xuf883b422016-08-30 14:01:10 -07003017 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07003018 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07003019 cm->superres_upscaled_width = oxcf->width;
3020 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07003021 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003022
3023 cm->error.setjmp = 0;
3024
3025 return cpi;
3026}
3027
Urvang Joshiee2c8112018-05-04 14:53:15 -07003028#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003029#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
3030
3031#define SNPRINT2(H, T, V) \
3032 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07003033#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003034
Yaowu Xuf883b422016-08-30 14:01:10 -07003035void av1_remove_compressor(AV1_COMP *cpi) {
3036 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003037 unsigned int i;
3038 int t;
3039
3040 if (!cpi) return;
3041
3042 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003043 const int num_planes = av1_num_planes(cm);
3044
David Turnerd2a592e2018-11-16 14:59:31 +00003045 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003046#if CONFIG_ENTROPY_STATS
3047 if (cpi->oxcf.pass != 1) {
3048 fprintf(stderr, "Writing counts.stt\n");
3049 FILE *f = fopen("counts.stt", "wb");
3050 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3051 fclose(f);
3052 }
3053#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003054#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003055 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003056
3057 if (cpi->oxcf.pass != 1) {
3058 char headings[512] = { 0 };
3059 char results[512] = { 0 };
3060 FILE *f = fopen("opsnr.stt", "a");
3061 double time_encoded =
3062 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3063 10000000.000;
3064 double total_encode_time =
3065 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3066 const double dr =
3067 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3068 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3069 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3070 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3071
3072 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003073 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003074 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3075 const double total_ssim =
3076 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3077 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003078 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003079 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003080 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08003081 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003082 snprintf(results, sizeof(results),
3083 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3084 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003085 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08003086 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003087 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3088 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3089 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3090 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003091 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003092 cpi->psnr.stat[STAT_Y] / cpi->count,
3093 cpi->psnr.stat[STAT_U] / cpi->count,
3094 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003095
3096 if (cpi->b_calculate_blockiness) {
3097 SNPRINT(headings, "\t Block\tWstBlck");
3098 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3099 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3100 }
3101
3102 if (cpi->b_calculate_consistency) {
3103 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003104 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003105 (double)cpi->total_inconsistency);
3106
3107 SNPRINT(headings, "\tConsist\tWstCons");
3108 SNPRINT2(results, "\t%7.3f", consistency);
3109 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3110 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003111 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3112 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003113 rate_err, fabs(rate_err));
3114 }
3115
3116 fclose(f);
3117 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003118#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003119#if CONFIG_SPEED_STATS
3120 if (cpi->oxcf.pass != 1) {
3121 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3122 }
3123#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003124
chiyotsai9c484b32019-03-07 16:01:50 -08003125#if CONFIG_COLLECT_PARTITION_STATS == 2
chiyotsai92ed0dd2019-01-25 14:50:14 -08003126 if (cpi->oxcf.pass != 1) {
3127 av1_print_partition_stats(&cpi->partition_stats);
3128 }
3129#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003130 }
3131
Yue Chenc9b23e02019-04-10 16:54:03 -07003132 for (int frame = 0; frame < MAX_LENGTH_TPL_FRAME_STATS; ++frame) {
Yue Chen7cae98f2018-08-24 10:43:16 -07003133 aom_free(cpi->tpl_stats[frame].tpl_stats_ptr);
3134 cpi->tpl_stats[frame].is_valid = 0;
3135 }
3136
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303137 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003138 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003139 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3140
3141 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003142 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003143
3144 // Deallocate allocated thread data.
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303145 aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303146 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003147 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003148 aom_free(thread_data->td->tmp_conv_dst);
Hui Su38711e72019-06-11 10:49:47 -07003149 av1_release_compound_type_rd_buffers(&thread_data->td->comp_rd_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003150 for (int j = 0; j < 2; ++j) {
3151 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3152 }
Jingning Hand064cf02017-06-01 10:00:39 -07003153 aom_free(thread_data->td->above_pred_buf);
3154 aom_free(thread_data->td->left_pred_buf);
3155 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003156
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303157 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003158 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303159 for (int y = 0; y < 2; y++) {
3160 aom_free(thread_data->td->hash_value_buffer[x][y]);
3161 thread_data->td->hash_value_buffer[x][y] = NULL;
3162 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003163 }
Jingning Hand064cf02017-06-01 10:00:39 -07003164 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003165 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003166 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003167 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003168 }
3169 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303170#if CONFIG_MULTITHREAD
Ravi Chaudhary4cd458b2019-06-04 17:42:35 +05303171 if (cpi->row_mt_mutex_ != NULL) {
3172 pthread_mutex_destroy(cpi->row_mt_mutex_);
3173 aom_free(cpi->row_mt_mutex_);
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303174 }
3175#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303176 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003177 aom_free(cpi->tile_thr_data);
3178 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003179
Deepa K G964e72e2018-05-16 16:56:01 +05303180 if (cpi->num_workers > 1) {
3181 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303182 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303183 }
3184
Yaowu Xuc27fc142016-08-22 16:08:15 -07003185 dealloc_compressor_data(cpi);
3186
3187 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3188 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003189 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003190 }
3191
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003192#if CONFIG_INTERNAL_STATS
3193 aom_free(cpi->ssim_vars);
3194 cpi->ssim_vars = NULL;
3195#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003196
Yaowu Xuf883b422016-08-30 14:01:10 -07003197 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003198 for (i = 0; i < FRAME_BUFFERS; ++i) {
3199 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3200 }
Yaowu Xu74e63352019-05-06 09:21:33 -07003201#if CONFIG_HTB_TRELLIS
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003202 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xu74e63352019-05-06 09:21:33 -07003203#endif // CONFIG_HTB_TRELLIS
Yaowu Xuf883b422016-08-30 14:01:10 -07003204 av1_free_ref_frame_buffers(cm->buffer_pool);
3205 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003206
3207#ifdef OUTPUT_YUV_SKINMAP
3208 fclose(yuv_skinmap_file);
3209#endif
3210#ifdef OUTPUT_YUV_REC
3211 fclose(yuv_rec_file);
3212#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003213}
3214
Yaowu Xuf883b422016-08-30 14:01:10 -07003215static void generate_psnr_packet(AV1_COMP *cpi) {
3216 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003217 int i;
3218 PSNR_STATS psnr;
David Turnerc29e1a92018-12-06 14:10:14 +00003219 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003220 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003221
3222 for (i = 0; i < 4; ++i) {
3223 pkt.data.psnr.samples[i] = psnr.samples[i];
3224 pkt.data.psnr.sse[i] = psnr.sse[i];
3225 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3226 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003227 pkt.kind = AOM_CODEC_PSNR_PKT;
3228 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003229}
3230
Yaowu Xuf883b422016-08-30 14:01:10 -07003231int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003232 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3233
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003234 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003235 return 0;
3236}
3237
Thomas Daede497d1952017-08-08 17:33:06 -07003238int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3239 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003240 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003241 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003242 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003243 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003244 return 0;
3245 } else {
3246 return -1;
3247 }
3248}
3249
Thomas Daede497d1952017-08-08 17:33:06 -07003250int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3251 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003252 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003253 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003254 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003255 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003256 return 0;
3257 } else {
3258 return -1;
3259 }
3260}
3261
3262int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003263 cpi->ext_refresh_frame_context = update;
3264 cpi->ext_refresh_frame_context_pending = 1;
3265 return 0;
3266}
3267
3268#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3269// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3270// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3271// not denoise the UV channels at this time. If ever we implement UV channel
3272// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003273void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003274 uint8_t *src = s->y_buffer;
3275 int h = s->y_height;
3276
3277 do {
3278 fwrite(src, s->y_width, 1, f);
3279 src += s->y_stride;
3280 } while (--h);
3281
3282 src = s->u_buffer;
3283 h = s->uv_height;
3284
3285 do {
3286 fwrite(src, s->uv_width, 1, f);
3287 src += s->uv_stride;
3288 } while (--h);
3289
3290 src = s->v_buffer;
3291 h = s->uv_height;
3292
3293 do {
3294 fwrite(src, s->uv_width, 1, f);
3295 src += s->uv_stride;
3296 } while (--h);
3297}
3298#endif
3299
Yaowu Xuc27fc142016-08-22 16:08:15 -07003300#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003301void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003302 uint8_t *src = s->y_buffer;
3303 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003304 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003305 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3306 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3307
3308 do {
3309 fwrite(src16, s->y_width, 2, yuv_rec_file);
3310 src16 += s->y_stride;
3311 } while (--h);
3312
3313 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3314 h = s->uv_height;
3315
3316 do {
3317 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3318 src16 += s->uv_stride;
3319 } while (--h);
3320
3321 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3322 h = s->uv_height;
3323
3324 do {
3325 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3326 src16 += s->uv_stride;
3327 } while (--h);
3328
3329 fflush(yuv_rec_file);
3330 return;
3331 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003332
3333 do {
3334 fwrite(src, s->y_width, 1, yuv_rec_file);
3335 src += s->y_stride;
3336 } while (--h);
3337
3338 src = s->u_buffer;
3339 h = s->uv_height;
3340
3341 do {
3342 fwrite(src, s->uv_width, 1, yuv_rec_file);
3343 src += s->uv_stride;
3344 } while (--h);
3345
3346 src = s->v_buffer;
3347 h = s->uv_height;
3348
3349 do {
3350 fwrite(src, s->uv_width, 1, yuv_rec_file);
3351 src += s->uv_stride;
3352 } while (--h);
3353
3354 fflush(yuv_rec_file);
3355}
3356#endif // OUTPUT_YUV_REC
3357
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003358#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003359static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3360 int i;
3361 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003362 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003363 AV1_COMMON *const cm = &cpi->common;
3364 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3365 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003366 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003367 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003368 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003369 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003370 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003371 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003372 // TODO(sarahparker): The earlier condition for recoding here was:
3373 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3374 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003375 }
3376 }
3377 return recode;
3378}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003379
Yaowu Xuc27fc142016-08-22 16:08:15 -07003380// Function to test for conditions that indicate we should loop
3381// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003382static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3383 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003384 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003385 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3387 int force_recode = 0;
3388
3389 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3390 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3391 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003392 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3393 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3394 (rc->projected_frame_size < low_limit && q > minq)) {
3395 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003396 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003397 // Deal with frame undershoot and whether or not we are
3398 // below the automatically set cq level.
3399 if (q > oxcf->cq_level &&
3400 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3401 force_recode = 1;
3402 }
3403 }
3404 }
3405 return force_recode;
3406}
3407
Cheng Chen46f30c72017-09-07 11:13:33 -07003408static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003409 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003410 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003412 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003413 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3414 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003415 };
3416
3417 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003418 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003419 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3420 BufferPool *const pool = cm->buffer_pool;
3421 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003422 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003423
3424 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003425 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003426 continue;
3427 }
3428
Yaowu Xuc27fc142016-08-22 16:08:15 -07003429 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Debargha Mukherjee13cccf22019-03-27 23:49:14 -07003430 // Replace the reference buffer with a copy having a thicker border,
3431 // if the reference buffer is higher resolution than the current
3432 // frame, and the border is thin.
3433 if ((ref->y_crop_width > cm->width ||
3434 ref->y_crop_height > cm->height) &&
3435 ref->border < AOM_BORDER_IN_PIXELS) {
3436 RefCntBuffer *ref_fb = get_ref_frame_buf(cm, ref_frame);
3437 if (aom_yv12_realloc_with_new_border(
3438 &ref_fb->buf, AOM_BORDER_IN_PIXELS, cm->byte_alignment,
3439 num_planes) != 0) {
3440 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3441 "Failed to allocate frame buffer");
3442 }
3443 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003444 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003445 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3446 if (new_fb == NULL) {
3447 const int new_fb_idx = get_free_fb(cm);
3448 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003449 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3450 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003451 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003452 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003453 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003454 }
David Turnere7ebf902018-12-04 14:04:55 +00003455
3456 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3457 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003458 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003459 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003460 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Debargha Mukherjeefa946af2019-03-26 16:58:55 -07003461 cm->seq_params.use_highbitdepth, AOM_BORDER_IN_PIXELS,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003462 cm->byte_alignment, NULL, NULL, NULL)) {
3463 if (force_scaling) {
3464 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003465 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003466 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003467 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003468 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003469 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003470 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003471 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3472 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003473 alloc_frame_mvs(cm, new_fb);
3474 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003475 } else {
David Turnera21966b2018-12-05 14:48:49 +00003476 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003477 buf->buf.y_crop_width = ref->y_crop_width;
3478 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003479 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003480 ++buf->ref_count;
3481 }
3482 } else {
David Turnere7ebf902018-12-04 14:04:55 +00003483 if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003484 }
3485 }
3486}
3487
Yaowu Xuf883b422016-08-30 14:01:10 -07003488static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003489 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003490 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3491 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3492 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003493 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003494 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003495 }
3496 }
3497}
3498
Yaowu Xuf883b422016-08-30 14:01:10 -07003499static void set_mv_search_params(AV1_COMP *cpi) {
3500 const AV1_COMMON *const cm = &cpi->common;
3501 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003502
3503 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003504 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003505
3506 if (cpi->sf.mv.auto_mv_step_size) {
3507 if (frame_is_intra_only(cm)) {
3508 // Initialize max_mv_magnitude for use in the first INTER frame
3509 // after a key/intra-only frame.
3510 cpi->max_mv_magnitude = max_mv_def;
3511 } else {
3512 if (cm->show_frame) {
3513 // Allow mv_steps to correspond to twice the max mv magnitude found
3514 // in the previous frame, capped by the default max_mv_magnitude based
3515 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003516 cpi->mv_step_param = av1_init_search_range(
3517 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003518 }
3519 cpi->max_mv_magnitude = 0;
3520 }
3521 }
3522}
3523
Hui Subdf0c992019-02-14 14:52:41 -08003524static void set_screen_content_options(AV1_COMP *cpi) {
3525 AV1_COMMON *cm = &cpi->common;
3526
3527 if (cm->seq_params.force_screen_content_tools != 2) {
3528 cm->allow_screen_content_tools = cm->allow_intrabc =
3529 cm->seq_params.force_screen_content_tools;
3530 return;
3531 }
3532
3533 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3534 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3535 return;
3536 }
3537
3538 // Estimate if the source frame is screen content, based on the portion of
3539 // blocks that have few luma colors.
3540 const uint8_t *src = cpi->source->y_buffer;
3541 assert(src != NULL);
3542 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
3543 const int stride = cpi->source->y_stride;
3544 const int width = cpi->source->y_width;
3545 const int height = cpi->source->y_height;
3546 const int bd = cm->seq_params.bit_depth;
3547 const int blk_w = 16;
3548 const int blk_h = 16;
3549 // These threshold values are selected experimentally.
3550 const int color_thresh = 4;
3551 const unsigned int var_thresh = 0;
3552 // Counts of blocks with no more than color_thresh colors.
3553 int counts_1 = 0;
3554 // Counts of blocks with no more than color_thresh colors and variance larger
3555 // than var_thresh.
3556 int counts_2 = 0;
3557
3558 for (int r = 0; r + blk_h <= height; r += blk_h) {
3559 for (int c = 0; c + blk_w <= width; c += blk_w) {
3560 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3561 const uint8_t *const this_src = src + r * stride + c;
3562 const int n_colors =
3563 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3564 count_buf)
3565 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3566 if (n_colors > 1 && n_colors <= color_thresh) {
3567 ++counts_1;
3568 struct buf_2d buf;
3569 buf.stride = stride;
3570 buf.buf = (uint8_t *)this_src;
3571 const unsigned int var =
3572 use_hbd
3573 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3574 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3575 if (var > var_thresh) ++counts_2;
3576 }
3577 }
3578 }
3579
3580 // The threshold values are selected experimentally.
3581 cm->allow_screen_content_tools =
3582 counts_1 * blk_h * blk_w * 10 > width * height;
3583 // IntraBC would force loop filters off, so we use more strict rules that also
3584 // requires that the block has high variance.
3585 cm->allow_intrabc = cm->allow_screen_content_tools &&
Hui Su64a2ffb2019-04-11 16:47:13 -07003586 counts_2 * blk_h * blk_w * 12 > width * height;
Hui Subdf0c992019-02-14 14:52:41 -08003587}
3588
Yaowu Xuf883b422016-08-30 14:01:10 -07003589static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003590 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003591 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003592 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003593 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003594 }
3595 cpi->global_motion_search_done = 0;
Yunqing Wang9411e432019-03-14 15:53:23 -07003596
3597 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
3598 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
3599
David Turner04b70d82019-01-24 15:39:19 +00003600 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07003601 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003602 cm->interp_filter = SWITCHABLE;
3603 cm->switchable_motion_mode = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003604}
3605
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003606static int get_gfu_boost_from_r0(double r0, int frames_to_key) {
3607 double factor = sqrt((double)frames_to_key);
3608 factor = AOMMIN(factor, 10.0);
3609 factor = AOMMAX(factor, 4.0);
3610 const int boost = (int)rint((200.0 + 10.0 * factor) / r0);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003611 return boost;
3612}
3613
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003614static int get_kf_boost_from_r0(double r0, int frames_to_key) {
3615 double factor = sqrt((double)frames_to_key);
3616 factor = AOMMIN(factor, 10.0);
3617 factor = AOMMAX(factor, 4.0);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003618 const int boost = (int)rint((75.0 + 14.0 * factor) / r0);
3619 return boost;
3620}
3621
3622int combine_prior_with_tpl_boost(int prior_boost, int tpl_boost,
3623 int frames_to_key) {
3624 double factor = sqrt((double)frames_to_key);
3625 factor = AOMMIN(factor, 12.0);
3626 factor = AOMMAX(factor, 4.0);
3627 factor -= 4.0;
3628 int boost = (int)((factor * prior_boost + (8.0 - factor) * tpl_boost) / 8.0);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003629 return boost;
3630}
3631
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003632static void process_tpl_stats_frame(AV1_COMP *cpi) {
Sarah Parkere1b22012019-06-06 16:35:25 -07003633 const GF_GROUP *const gf_group = &cpi->gf_group;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003634 AV1_COMMON *const cm = &cpi->common;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003635
Sarah Parkere1b22012019-06-06 16:35:25 -07003636 assert(IMPLIES(gf_group->size > 0, gf_group->index < gf_group->size));
3637 const int tpl_idx = gf_group->frame_disp_idx[gf_group->index];
Yue Chen4e585cc2019-06-03 14:47:16 -07003638 TplDepFrame *tpl_frame = &cpi->tpl_stats[tpl_idx];
3639 TplDepStats *tpl_stats = tpl_frame->tpl_stats_ptr;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003640
Yue Chen4e585cc2019-06-03 14:47:16 -07003641 if (tpl_frame->is_valid) {
3642 int tpl_stride = tpl_frame->stride;
3643 int64_t intra_cost_base = 0;
3644 int64_t mc_dep_cost_base = 0;
3645 int64_t mc_saved_base = 0;
3646 int64_t mc_count_base = 0;
3647 int row, col;
3648
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07003649 const int mi_cols_sr = av1_pixels_to_mi(cm->superres_upscaled_width);
Yue Chen4e585cc2019-06-03 14:47:16 -07003650 for (row = 0; row < cm->mi_rows; ++row) {
Debargha Mukherjeeedfa4fe2019-07-08 14:23:39 -07003651 for (col = 0; col < mi_cols_sr; ++col) {
Yue Chen4e585cc2019-06-03 14:47:16 -07003652 TplDepStats *this_stats = &tpl_stats[row * tpl_stride + col];
3653 intra_cost_base += this_stats->intra_cost;
3654 mc_dep_cost_base += this_stats->intra_cost + this_stats->mc_flow;
3655 mc_count_base += this_stats->mc_count;
3656 mc_saved_base += this_stats->mc_saved;
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003657 }
Yue Chen4e585cc2019-06-03 14:47:16 -07003658 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003659
Yue Chen4e585cc2019-06-03 14:47:16 -07003660 if (mc_dep_cost_base == 0) {
3661 tpl_frame->is_valid = 0;
3662 } else {
3663 aom_clear_system_state();
3664 cpi->rd.r0 = (double)intra_cost_base / mc_dep_cost_base;
3665 if (is_frame_arf_and_tpl_eligible(cpi)) {
3666 cpi->rd.arf_r0 = cpi->rd.r0;
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003667 const int gfu_boost =
3668 get_gfu_boost_from_r0(cpi->rd.arf_r0, cpi->rc.frames_to_key);
Yue Chen4e585cc2019-06-03 14:47:16 -07003669 // printf("old boost %d new boost %d\n", cpi->rc.gfu_boost,
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003670 // gfu_boost);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003671 cpi->rc.gfu_boost = combine_prior_with_tpl_boost(
3672 cpi->rc.gfu_boost, gfu_boost, cpi->rc.frames_to_key);
Debargha Mukherjeeafd3cc22019-06-10 11:35:17 -07003673 } else if (frame_is_intra_only(cm)) {
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003674 // TODO(debargha): Turn off q adjustment for kf temporarily to
3675 // reduce impact on speed of encoding. Need to investigate how
3676 // to mitigate the issue.
3677 if (cpi->oxcf.rc_mode == AOM_Q) {
3678 const int kf_boost =
3679 get_kf_boost_from_r0(cpi->rd.r0, cpi->rc.frames_to_key);
3680 // printf("old kf boost %d new kf boost %d [%d]\n", cpi->rc.kf_boost,
3681 // kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee2e2b09c2019-06-14 13:50:42 -07003682 cpi->rc.kf_boost = combine_prior_with_tpl_boost(
3683 cpi->rc.kf_boost, kf_boost, cpi->rc.frames_to_key);
Debargha Mukherjee0a121a92019-06-19 13:12:46 -07003684 }
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003685 }
Yue Chen4e585cc2019-06-03 14:47:16 -07003686 cpi->rd.mc_count_base =
3687 (double)mc_count_base / (cm->mi_rows * cm->mi_cols);
3688 cpi->rd.mc_saved_base =
3689 (double)mc_saved_base / (cm->mi_rows * cm->mi_cols);
3690 aom_clear_system_state();
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003691 }
3692 }
3693}
3694
Yaowu Xuf883b422016-08-30 14:01:10 -07003695static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003696 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003697 AV1_COMMON *const cm = &cpi->common;
3698 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003699
3700 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00003701 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003702
Yue Chen4e585cc2019-06-03 14:47:16 -07003703 if (cpi->oxcf.enable_tpl_model && cpi->tpl_model_pass == 0 &&
3704 is_frame_tpl_eligible(cpi))
3705 process_tpl_stats_frame(cpi);
Debargha Mukherjeed0c0b772019-05-27 23:05:06 -07003706
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003707 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003708 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3709 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003710
James Zern01a9d702017-08-25 19:09:33 +00003711 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003712 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003713 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003714 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715
3716 // Configure experimental use of segmentation for enhanced coding of
3717 // static regions if indicated.
3718 // Only allowed in the second pass of a two pass encode, as it requires
3719 // lagged coding, and if the relevant speed feature flag is set.
3720 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3721 configure_static_seg_features(cpi);
3722}
3723
Yaowu Xuf883b422016-08-30 14:01:10 -07003724static void init_motion_estimation(AV1_COMP *cpi) {
Urvang Joshi510d8f62019-01-10 12:11:50 -08003725 AV1_COMMON *const cm = &cpi->common;
chiyotsai836b69b2019-04-09 13:41:24 -07003726 const int y_stride = cpi->scaled_source.y_stride;
Urvang Joshi510d8f62019-01-10 12:11:50 -08003727 const int y_stride_src =
3728 ((cpi->oxcf.width != cm->width || cpi->oxcf.height != cm->height) ||
3729 av1_superres_scaled(cm))
3730 ? y_stride
3731 : cpi->lookahead->buf->img.y_stride;
chiyotsai836b69b2019-04-09 13:41:24 -07003732 // Update if ss_cfg is uninitialized or the current frame has a new stride
3733 const int should_update = !cpi->ss_cfg[SS_CFG_SRC].stride ||
3734 !cpi->ss_cfg[SS_CFG_LOOKAHEAD].stride ||
3735 (y_stride != cpi->ss_cfg[SS_CFG_SRC].stride);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003736
chiyotsai836b69b2019-04-09 13:41:24 -07003737 if (!should_update) {
3738 return;
3739 }
3740
3741 if (cpi->sf.mv.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303742 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3743 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
3744 y_stride_src);
chiyotsai836b69b2019-04-09 13:41:24 -07003745 } else {
3746 // Update the offsets in search_sites as y_stride can change due to scaled
3747 // references. This update allows NSTEP to be used on scaled references as
3748 // long as sf.mv.search_method is not DIAMOND. Currently in the codebae,
3749 // sf.mv.search_method is never set to DIAMOND.
3750 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3751 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003752 }
3753}
3754
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003755#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003756static void set_restoration_unit_size(int width, int height, int sx, int sy,
3757 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003758 (void)width;
3759 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003760 (void)sx;
3761 (void)sy;
3762#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3763 int s = AOMMIN(sx, sy);
3764#else
3765 int s = 0;
3766#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3767
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003768 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003769 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003770 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003771 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003772 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3773 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003774}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003775
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303776static void init_ref_frame_bufs(AV1_COMP *cpi) {
3777 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07003778 int i;
3779 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00003780 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07003781 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00003782 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07003783 }
3784 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07003785 pool->frame_bufs[i].ref_count = 0;
3786 }
RogerZhou86902d02018-02-28 15:29:16 -08003787 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003788 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303789 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08003790 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003791 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003792}
3793
Yaowu Xud3e7c682017-12-21 14:08:25 -08003794static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003795 int subsampling_x, int subsampling_y) {
3796 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003797 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07003798
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003799 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3800 seq_params->subsampling_x != subsampling_x ||
3801 seq_params->subsampling_y != subsampling_y) {
3802 seq_params->subsampling_x = subsampling_x;
3803 seq_params->subsampling_y = subsampling_y;
3804 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003805
3806 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303807 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07003808 alloc_util_frame_buffers(cpi);
3809
3810 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3811
3812 cpi->initial_width = cm->width;
3813 cpi->initial_height = cm->height;
3814 cpi->initial_mbs = cm->MBs;
3815 }
3816}
3817
3818// Returns 1 if the assigned width or height was <= 0.
3819static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3820 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003821 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003822 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3823 cm->seq_params.subsampling_x,
3824 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003825
3826 if (width <= 0 || height <= 0) return 1;
3827
3828 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003829 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003830
3831 if (cpi->initial_width && cpi->initial_height &&
3832 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3833 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003834 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003835 alloc_compressor_data(cpi);
3836 realloc_segmentation_maps(cpi);
3837 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003838 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003839 update_frame_size(cpi);
3840
3841 return 0;
3842}
3843
David Turner475a3132019-01-18 15:17:17 +00003844void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003845 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003846 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003847 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003848 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003849 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003850
Fergus Simpsonbc189932017-05-16 17:02:39 -07003851 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003852 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003853 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003854 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003855 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07003856 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003857 }
3858
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003859 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003860 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003861 }
3862
David Turnere7ebf902018-12-04 14:04:55 +00003863 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003864
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303865 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05303866 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3867 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303868 cm->num_allocated_above_contexts < cm->tile_rows) {
3869 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3870 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3871 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3872 "Failed to allocate context buffers");
3873 }
3874
Yaowu Xuc27fc142016-08-22 16:08:15 -07003875 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003876 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00003877 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
3878 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05303879 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003880 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003881 "Failed to allocate frame buffer");
3882
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003883 const int frame_width = cm->superres_upscaled_width;
3884 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003885 set_restoration_unit_size(frame_width, frame_height,
3886 seq_params->subsampling_x,
3887 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003888 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003889 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003890
3891 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003892 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003893 init_motion_estimation(cpi);
3894
3895 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00003896 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00003897 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00003898 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
3899 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00003900 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003901 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00003902 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003903 }
3904 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003905
Hui Su5ebd8702018-01-08 18:09:20 -08003906 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003907 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003908
3909 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3910}
3911
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003912static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3913 // Choose an arbitrary random number
3914 static unsigned int seed = 56789;
3915 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003916 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3917 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003918
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07003919 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003920 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003921 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003922 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003923 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003924 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003925 else
Urvang Joshide71d142017-10-05 12:12:15 -07003926 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003927 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003928 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003929 default: assert(0);
3930 }
Urvang Joshide71d142017-10-05 12:12:15 -07003931 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003932}
3933
Urvang Joshi510d8f62019-01-10 12:11:50 -08003934#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO 0.012
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07003935#define SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME 0.008
3936#define SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME 0.008
Urvang Joshi510d8f62019-01-10 12:11:50 -08003937#define SUPERRES_ENERGY_BY_AC_THRESH 0.2
3938
3939static double get_energy_by_q2_thresh(const GF_GROUP *gf_group,
3940 const RATE_CONTROL *rc) {
3941 // TODO(now): Return keyframe thresh * factor based on frame type / pyramid
3942 // level.
3943 if (gf_group->update_type[gf_group->index] == ARF_UPDATE) {
3944 return SUPERRES_ENERGY_BY_Q2_THRESH_ARFFRAME;
3945 } else if (gf_group->update_type[gf_group->index] == KF_UPDATE) {
3946 if (rc->frames_to_key <= 1)
3947 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME_SOLO;
3948 else
3949 return SUPERRES_ENERGY_BY_Q2_THRESH_KEYFRAME;
3950 } else {
3951 assert(0);
3952 }
3953 return 0;
3954}
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003955
3956static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003957 double threshq,
3958 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003959 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003960 const double tq = threshq * q * q;
3961 const double tp = threshp * energy[1];
3962 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003963 int k;
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07003964 for (k = SCALE_NUMERATOR * 2; k > SCALE_NUMERATOR; --k) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003965 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003966 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003967 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003968}
3969
Urvang Joshi510d8f62019-01-10 12:11:50 -08003970static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex,
3971 int sr_kf, int sr_arf) {
3972 // Use superres for Key-frames and Alt-ref frames only.
3973 const GF_GROUP *gf_group = &cpi->gf_group;
3974 if (gf_group->update_type[gf_group->index] != KF_UPDATE &&
3975 gf_group->update_type[gf_group->index] != ARF_UPDATE) {
3976 return SCALE_NUMERATOR;
3977 }
3978 if (gf_group->update_type[gf_group->index] == KF_UPDATE && !sr_kf) {
3979 return SCALE_NUMERATOR;
3980 }
3981 if (gf_group->update_type[gf_group->index] == ARF_UPDATE && !sr_arf) {
3982 return SCALE_NUMERATOR;
3983 }
3984
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003985 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003986 analyze_hor_freq(cpi, energy);
Urvang Joshi510d8f62019-01-10 12:11:50 -08003987
3988 const double energy_by_q2_thresh =
3989 get_energy_by_q2_thresh(gf_group, &cpi->rc);
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07003990 const int denom = get_superres_denom_from_qindex_energy(
3991 qindex, energy, energy_by_q2_thresh, SUPERRES_ENERGY_BY_AC_THRESH);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003992 /*
3993 printf("\nenergy = [");
3994 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
3995 printf("]\n");
Debargha Mukherjeec94082f2019-07-09 13:42:57 -07003996 printf("boost = %d\n",
3997 (gf_group->update_type[gf_group->index] == KF_UPDATE)
3998 ? cpi->rc.kf_boost
3999 : cpi->rc.gfu_boost);
4000 printf("denom = %d\n", denom);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08004001 */
Urvang Joshi510d8f62019-01-10 12:11:50 -08004002 return denom;
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004003}
4004
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004005static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
4006 // Choose an arbitrary random number
4007 static unsigned int seed = 34567;
4008 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07004009 if (oxcf->pass == 1) return SCALE_NUMERATOR;
4010 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07004011
4012 // Make sure that superres mode of the frame is consistent with the
4013 // sequence-level flag.
4014 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
4015 cpi->common.seq_params.enable_superres));
4016 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
4017 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004018
4019 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07004020 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004021 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00004022 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07004023 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004024 else
Urvang Joshide71d142017-10-05 12:12:15 -07004025 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004026 break;
Urvang Joshide71d142017-10-05 12:12:15 -07004027 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004028 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07004029 // Do not use superres when screen content tools are used.
4030 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08004031 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4032 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi510d8f62019-01-10 12:11:50 -08004033
4034 // Now decide the use of superres based on 'q'.
Urvang Joshi2c92b072018-03-19 17:23:31 -07004035 int bottom_index, top_index;
4036 const int q = av1_rc_pick_q_and_bounds(
4037 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004038
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08004039 const int qthresh = (frame_is_intra_only(&cpi->common))
4040 ? oxcf->superres_kf_qthresh
4041 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08004042 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07004043 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004044 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004045 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004046 }
4047 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08004048 }
Urvang Joshi36a83732019-01-31 15:31:57 -08004049 case SUPERRES_AUTO: {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004050 // Do not use superres when screen content tools are used.
Urvang Joshi36a83732019-01-31 15:31:57 -08004051 if (cpi->common.allow_screen_content_tools) break;
Urvang Joshi510d8f62019-01-10 12:11:50 -08004052 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
4053 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi36a83732019-01-31 15:31:57 -08004054
4055 // Now decide the use of superres based on 'q'.
4056 int bottom_index, top_index;
4057 const int q = av1_rc_pick_q_and_bounds(
4058 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
4059
4060 const int qthresh = 128;
4061 if (q <= qthresh) {
4062 new_denom = SCALE_NUMERATOR;
4063 } else {
Urvang Joshi510d8f62019-01-10 12:11:50 -08004064 new_denom = get_superres_denom_for_qindex(cpi, q, 1, 1);
Urvang Joshi36a83732019-01-31 15:31:57 -08004065 }
4066 break;
4067 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004068 default: assert(0);
4069 }
Urvang Joshide71d142017-10-05 12:12:15 -07004070 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004071}
4072
Urvang Joshide71d142017-10-05 12:12:15 -07004073static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
4074 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
4075}
4076
4077static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08004078 // Only need to check the width, as scaling is horizontal only.
4079 (void)oheight;
4080 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07004081}
4082
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004083static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004084 SUPERRES_MODE superres_mode, int owidth,
4085 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07004086 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004087 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07004088 }
4089
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004090 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07004091 int resize_denom =
4092 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
4093 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
4094
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004095 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004096 // Alter superres scale as needed to enforce conformity.
4097 rsz->superres_denom =
4098 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
4099 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4100 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004101 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004102 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004103 // Alter resize scale as needed to enforce conformity.
4104 resize_denom =
4105 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004106 rsz->resize_width = owidth;
4107 rsz->resize_height = oheight;
4108 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004109 resize_denom);
4110 if (!dimensions_are_ok(owidth, oheight, rsz)) {
4111 if (resize_denom > SCALE_NUMERATOR) {
4112 --resize_denom;
4113 rsz->resize_width = owidth;
4114 rsz->resize_height = oheight;
4115 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
4116 resize_denom);
4117 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004118 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004119 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07004120 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004121 do {
Urvang Joshide71d142017-10-05 12:12:15 -07004122 if (resize_denom > rsz->superres_denom)
4123 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004124 else
Urvang Joshide71d142017-10-05 12:12:15 -07004125 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004126 rsz->resize_width = owidth;
4127 rsz->resize_height = oheight;
4128 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004129 resize_denom);
4130 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
4131 (resize_denom > SCALE_NUMERATOR ||
4132 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08004133 } else { // We are allowed to alter neither resize scale nor superres
4134 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004135 return 0;
4136 }
Urvang Joshide71d142017-10-05 12:12:15 -07004137 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004138}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004139
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004140// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00004141static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004142 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08004143 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshi510d8f62019-01-10 12:11:50 -08004144 int resize_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004145 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004146 if (cpi->resize_pending_width && cpi->resize_pending_height) {
4147 rsz.resize_width = cpi->resize_pending_width;
4148 rsz.resize_height = cpi->resize_pending_height;
4149 cpi->resize_pending_width = cpi->resize_pending_height = 0;
4150 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07004151 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004152 rsz.resize_width = cpi->oxcf.width;
4153 rsz.resize_height = cpi->oxcf.height;
4154 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07004155 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004156 }
Urvang Joshide71d142017-10-05 12:12:15 -07004157 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004158 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
4159 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004160 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004161 return rsz;
4162}
4163
Urvang Joshi22b150b2019-01-10 14:32:32 -08004164static void setup_frame_size_from_params(AV1_COMP *cpi,
4165 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004166 int encode_width = rsz->resize_width;
4167 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004168
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004169 AV1_COMMON *cm = &cpi->common;
4170 cm->superres_upscaled_width = encode_width;
4171 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07004172 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07004173 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
4174 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00004175 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004176}
4177
David Turnerdedd8ff2019-01-23 13:59:46 +00004178void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004179 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08004180 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004181 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00004182 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004183 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08004184
Yaowu Xu7e450882019-04-30 15:09:18 -07004185 assert(av1_is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004186}
4187
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004188static void superres_post_encode(AV1_COMP *cpi) {
4189 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004190 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004191
Cheng Chen09c83a52018-06-05 12:27:36 -07004192 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004193
Urvang Joshid6b5d512018-03-20 13:34:38 -07004194 assert(cpi->oxcf.enable_superres);
4195 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07004196 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07004197
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004198 av1_superres_upscale(cm, NULL);
4199
4200 // If regular resizing is occurring the source will need to be downscaled to
4201 // match the upscaled superres resolution. Otherwise the original source is
4202 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004203 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004204 cpi->source = cpi->unscaled_source;
4205 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4206 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004207 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4208 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004209 // Do downscale. cm->(width|height) has been updated by
4210 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004211 if (aom_realloc_frame_buffer(
4212 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004213 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4214 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4215 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004216 aom_internal_error(
4217 &cm->error, AOM_CODEC_MEM_ERROR,
4218 "Failed to reallocate scaled source buffer for superres");
4219 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4220 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004221 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004222 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004223 cpi->source = &cpi->scaled_source;
4224 }
4225}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004226
Yue Chen92271e12019-07-09 16:28:02 -07004227static void cdef_restoration_frame(AV1_COMP *cpi, AV1_COMMON *cm,
4228 MACROBLOCKD *xd, int use_restoration,
4229 int use_cdef) {
logangwf95c9162019-02-20 12:02:32 -08004230 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004231 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004232
logangwf95c9162019-02-20 12:02:32 -08004233 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004234#if CONFIG_COLLECT_COMPONENT_TIMING
4235 start_timing(cpi, cdef_time);
4236#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004237 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004238 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
Hui Sub93880a2019-06-27 18:29:30 -07004239 cpi->sf.cdef_pick_method, cpi->td.mb.rdmult);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004240
4241 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004242 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004243#if CONFIG_COLLECT_COMPONENT_TIMING
4244 end_timing(cpi, cdef_time);
4245#endif
logangwf95c9162019-02-20 12:02:32 -08004246 } else {
4247 cm->cdef_info.cdef_bits = 0;
4248 cm->cdef_info.cdef_strengths[0] = 0;
4249 cm->cdef_info.nb_cdef_strengths = 1;
4250 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004251 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004252
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004253 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004254
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004255#if CONFIG_COLLECT_COMPONENT_TIMING
4256 start_timing(cpi, loop_restoration_time);
4257#endif
logangwf95c9162019-02-20 12:02:32 -08004258 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004259 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004260 av1_pick_filter_restoration(cpi->source, cpi);
4261 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4262 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4263 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304264 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004265 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304266 cpi->workers, cpi->num_workers,
4267 &cpi->lr_row_sync, &cpi->lr_ctxt);
4268 else
David Turnerc29e1a92018-12-06 14:10:14 +00004269 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304270 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004271 }
logangwf95c9162019-02-20 12:02:32 -08004272 } else {
4273 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4274 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4275 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004276 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004277#if CONFIG_COLLECT_COMPONENT_TIMING
4278 end_timing(cpi, loop_restoration_time);
4279#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004280}
4281
Yue Chen92271e12019-07-09 16:28:02 -07004282static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
4283 const int num_planes = av1_num_planes(cm);
4284 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
4285
4286 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4287 cm->coded_lossless && cm->all_lossless));
4288
4289 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4290 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4291 !cm->large_scale_tile;
4292 const int use_restoration = cm->seq_params.enable_restoration &&
4293 !cm->all_lossless && !cm->large_scale_tile;
4294
4295 struct loopfilter *lf = &cm->lf;
4296
4297#if CONFIG_COLLECT_COMPONENT_TIMING
4298 start_timing(cpi, loop_filter_time);
4299#endif
4300 if (use_loopfilter) {
4301 aom_clear_system_state();
4302 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
4303 } else {
4304 lf->filter_level[0] = 0;
4305 lf->filter_level[1] = 0;
4306 }
4307
4308 if (lf->filter_level[0] || lf->filter_level[1]) {
4309 if (cpi->num_workers > 1)
4310 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
4311#if CONFIG_LPF_MASK
4312 0,
4313#endif
4314 cpi->workers, cpi->num_workers,
4315 &cpi->lf_row_sync);
4316 else
4317 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
4318#if CONFIG_LPF_MASK
4319 0,
4320#endif
4321 0, num_planes, 0);
4322 }
4323#if CONFIG_COLLECT_COMPONENT_TIMING
4324 end_timing(cpi, loop_filter_time);
4325#endif
4326
4327 cdef_restoration_frame(cpi, cm, xd, use_restoration, use_cdef);
4328}
4329
David Turnerf2b334c2018-12-13 13:00:55 +00004330static void fix_interp_filter(InterpFilter *const interp_filter,
4331 const FRAME_COUNTS *const counts) {
4332 if (*interp_filter == SWITCHABLE) {
4333 // Check to see if only one of the filters is actually used
4334 int count[SWITCHABLE_FILTERS] = { 0 };
4335 int num_filters_used = 0;
4336 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4337 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4338 count[i] += counts->switchable_interp[j][i];
4339 num_filters_used += (count[i] > 0);
4340 }
4341 if (num_filters_used == 1) {
4342 // Only one filter is used. So set the filter at frame level
4343 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4344 if (count[i]) {
4345 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4346 break;
4347 }
4348 }
4349 }
4350 }
4351}
4352
David Turner996b2c12018-12-07 15:52:30 +00004353static void finalize_encoded_frame(AV1_COMP *const cpi) {
4354 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004355 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004356
David Turner99e990e2018-12-10 12:54:26 +00004357 if (!cm->seq_params.reduced_still_picture_hdr &&
4358 encode_show_existing_frame(cm)) {
4359 RefCntBuffer *const frame_to_show =
4360 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4361
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004362 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004363 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4364 "Buffer does not contain a reconstructed frame");
4365 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004366 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004367 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004368 }
David Turner08f909c2018-12-18 13:29:14 +00004369
4370 if (!encode_show_existing_frame(cm) &&
4371 cm->seq_params.film_grain_params_present &&
4372 (cm->show_frame || cm->showable_frame)) {
4373 // Copy the current frame's film grain params to the its corresponding
4374 // RefCntBuffer slot.
4375 cm->cur_frame->film_grain_params = cm->film_grain_params;
4376
4377 // We must update the parameters if this is not an INTER_FRAME
4378 if (current_frame->frame_type != INTER_FRAME)
4379 cm->cur_frame->film_grain_params.update_parameters = 1;
4380
4381 // Iterate the random seed for the next frame.
4382 cm->film_grain_params.random_seed += 3381;
4383 if (cm->film_grain_params.random_seed == 0)
4384 cm->film_grain_params.random_seed = 7391;
4385 }
David Turnerf2b334c2018-12-13 13:00:55 +00004386
4387 // Initialise all tiles' contexts from the global frame context
4388 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4389 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4390 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4391 cpi->tile_data[tile_idx].tctx = *cm->fc;
4392 }
4393 }
4394
4395 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004396}
4397
Urvang Joshi57643372019-02-21 11:10:57 -08004398static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4399 int top_index, int bottom_index) {
4400 const AV1_COMMON *const cm = &cpi->common;
4401 const RATE_CONTROL *const rc = &cpi->rc;
4402
4403 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4404
4405 int q_regulated =
4406 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4407 AOMMAX(q_high, top_index), cm->width, cm->height);
4408
4409 int retries = 0;
4410 while (q_regulated < q_low && retries < 10) {
4411 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4412 q_regulated =
4413 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4414 AOMMAX(q_high, top_index), cm->width, cm->height);
4415 retries++;
4416 }
4417 return q_regulated;
4418}
4419
4420static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4421 int top_index, int bottom_index) {
4422 const AV1_COMMON *const cm = &cpi->common;
4423 const RATE_CONTROL *const rc = &cpi->rc;
4424
4425 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4426 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4427 top_index, cm->width, cm->height);
4428
4429 int retries = 0;
4430 while (q_regulated > q_high && retries < 10) {
4431 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4432 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4433 top_index, cm->width, cm->height);
4434 retries++;
4435 }
4436 return q_regulated;
4437}
4438
David Turner2f3b5df2019-01-02 14:30:50 +00004439// Called after encode_with_recode_loop() has just encoded a frame and packed
4440// its bitstream. This function works out whether we under- or over-shot
4441// our bitrate target and adjusts q as appropriate. Also decides whether
4442// or not we should do another recode loop, indicated by *loop
Hui Suef139e12019-05-20 15:51:22 -07004443static void recode_loop_update_q(
4444 AV1_COMP *const cpi, int *const loop, int *const q, int *const q_low,
4445 int *const q_high, const int top_index, const int bottom_index,
4446 int *const undershoot_seen, int *const overshoot_seen,
4447 int *const low_cr_seen, const int loop_at_this_size) {
David Turner2f3b5df2019-01-02 14:30:50 +00004448 AV1_COMMON *const cm = &cpi->common;
4449 RATE_CONTROL *const rc = &cpi->rc;
4450
Hui Suef139e12019-05-20 15:51:22 -07004451 const int min_cr = cpi->oxcf.min_cr;
4452 if (min_cr > 0) {
4453 aom_clear_system_state();
4454 const double compression_ratio =
4455 av1_get_compression_ratio(cm, rc->projected_frame_size >> 3);
4456 const double target_cr = min_cr / 100.0;
4457 if (compression_ratio < target_cr) {
4458 *low_cr_seen = 1;
4459 if (*q < rc->worst_quality) {
4460 const double cr_ratio = target_cr / compression_ratio;
4461 const int projected_q = AOMMAX(*q + 1, (int)(*q * cr_ratio * cr_ratio));
4462 *q = AOMMIN(AOMMIN(projected_q, *q + 32), rc->worst_quality);
4463 *q_low = AOMMAX(*q, *q_low);
4464 *q_high = AOMMAX(*q, *q_high);
4465 *loop = 1;
4466 }
4467 }
4468 if (*low_cr_seen) return;
4469 }
4470
4471 if (cpi->oxcf.rc_mode == AOM_Q) return;
4472
David Turner2f3b5df2019-01-02 14:30:50 +00004473 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4474 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4475 &frame_under_shoot_limit,
4476 &frame_over_shoot_limit);
4477 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4478
4479 if ((cm->current_frame.frame_type == KEY_FRAME) &&
4480 rc->this_key_frame_forced &&
4481 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4482 int last_q = *q;
4483 int64_t kf_err;
4484
4485 int64_t high_err_target = cpi->ambient_err;
4486 int64_t low_err_target = cpi->ambient_err >> 1;
4487
4488 if (cm->seq_params.use_highbitdepth) {
4489 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4490 } else {
4491 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4492 }
4493 // Prevent possible divide by zero error below for perfect KF
4494 kf_err += !kf_err;
4495
4496 // The key frame is not good enough or we can afford
4497 // to make it better without undue risk of popping.
4498 if ((kf_err > high_err_target &&
4499 rc->projected_frame_size <= frame_over_shoot_limit) ||
4500 (kf_err > low_err_target &&
4501 rc->projected_frame_size <= frame_under_shoot_limit)) {
4502 // Lower q_high
4503 *q_high = *q > *q_low ? *q - 1 : *q_low;
4504
4505 // Adjust Q
4506 *q = (int)((*q * high_err_target) / kf_err);
4507 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4508 } else if (kf_err < low_err_target &&
4509 rc->projected_frame_size >= frame_under_shoot_limit) {
4510 // The key frame is much better than the previous frame
4511 // Raise q_low
4512 *q_low = *q < *q_high ? *q + 1 : *q_high;
4513
4514 // Adjust Q
4515 *q = (int)((*q * low_err_target) / kf_err);
4516 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4517 }
4518
4519 // Clamp Q to upper and lower limits:
4520 *q = clamp(*q, *q_low, *q_high);
4521
4522 *loop = *q != last_q;
4523 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4524 frame_under_shoot_limit, *q,
4525 AOMMAX(*q_high, top_index), bottom_index)) {
4526 // Is the projected frame size out of range and are we allowed
4527 // to attempt to recode.
4528 int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004529
4530 // Frame size out of permitted range:
4531 // Update correction factor & compute new Q to try...
4532 // Frame is too large
4533 if (rc->projected_frame_size > rc->this_frame_target) {
4534 // Special case if the projected size is > the max allowed.
4535 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4536 *q_high = rc->worst_quality;
4537
4538 // Raise Qlow as to at least the current value
4539 *q_low = *q < *q_high ? *q + 1 : *q_high;
4540
Urvang Joshi57643372019-02-21 11:10:57 -08004541 if (*undershoot_seen || loop_at_this_size > 2 ||
4542 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004543 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4544
4545 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004546 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4547 const int q_mid = (*q_high + *q_low + 1) / 2;
4548 const int q_regulated = get_regulated_q_overshoot(
4549 cpi, *q_low, *q_high, top_index, bottom_index);
4550 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4551 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4552 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00004553 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004554 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
4555 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00004556 }
4557
4558 *overshoot_seen = 1;
4559 } else {
4560 // Frame is too small
4561 *q_high = *q > *q_low ? *q - 1 : *q_low;
4562
Urvang Joshi57643372019-02-21 11:10:57 -08004563 if (*overshoot_seen || loop_at_this_size > 2 ||
4564 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004565 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4566 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004567 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4568 const int q_mid = (*q_high + *q_low) / 2;
4569 const int q_regulated =
4570 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4571 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4572 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4573 *q = (q_mid + q_regulated) / 2;
4574
4575 // Special case reset for qlow for constrained quality.
4576 // This should only trigger where there is very substantial
4577 // undershoot on a frame and the auto cq level is above
4578 // the user passsed in value.
4579 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
4580 *q_low = *q;
4581 }
David Turner2f3b5df2019-01-02 14:30:50 +00004582 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004583 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4584
David Turner2f3b5df2019-01-02 14:30:50 +00004585 // Special case reset for qlow for constrained quality.
4586 // This should only trigger where there is very substantial
4587 // undershoot on a frame and the auto cq level is above
4588 // the user passsed in value.
4589 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
4590 *q_low = *q;
4591 }
David Turner2f3b5df2019-01-02 14:30:50 +00004592 }
4593
4594 *undershoot_seen = 1;
4595 }
4596
4597 // Clamp Q to upper and lower limits:
4598 *q = clamp(*q, *q_low, *q_high);
4599
4600 *loop = (*q != last_q);
4601 } else {
4602 *loop = 0;
4603 }
4604}
4605
Tom Finegane4099e32018-01-23 12:01:51 -08004606static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004607 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004608 RATE_CONTROL *const rc = &cpi->rc;
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004609 const int allow_recode = (cpi->sf.recode_loop != DISALLOW_RECODE);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004610
4611 set_size_independent_vars(cpi);
4612
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004613 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004614
David Turnerdedd8ff2019-01-23 13:59:46 +00004615 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004616
David Turner2f3b5df2019-01-02 14:30:50 +00004617 int top_index = 0, bottom_index = 0;
4618 int q = 0, q_low = 0, q_high = 0;
4619 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4620 q_low = bottom_index;
4621 q_high = top_index;
4622
4623 // Loop variables
4624 int loop_count = 0;
4625 int loop_at_this_size = 0;
4626 int loop = 0;
4627 int overshoot_seen = 0;
4628 int undershoot_seen = 0;
Hui Suef139e12019-05-20 15:51:22 -07004629 int low_cr_seen = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004630
4631#if CONFIG_COLLECT_COMPONENT_TIMING
4632 printf("\n Encoding a frame:");
4633#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004634 do {
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004635 loop = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07004636 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004637
Urvang Joshif1fa6862018-01-08 16:39:33 -08004638 // if frame was scaled calculate global_motion_search again if already
4639 // done
David Turner2f3b5df2019-01-02 14:30:50 +00004640 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004641 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00004642 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004643 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00004644 }
4645 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004646 cpi->source =
4647 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004648 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004649 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4650 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004651 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004652
David Turner2f3b5df2019-01-02 14:30:50 +00004653 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004654 if (loop_count > 0) {
4655 release_scaled_references(cpi);
4656 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004657 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004658 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004659 av1_set_quantizer(cm, q);
Yue Chena7d80482019-01-31 09:48:09 -08004660 av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08004661
4662 av1_set_variance_partition_thresholds(cpi, q, 0);
4663
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004664 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
4665 // cm->current_frame.frame_number, cm->show_frame, q,
4666 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004667
David Turner2f3b5df2019-01-02 14:30:50 +00004668 if (loop_count == 0) {
4669 setup_frame(cpi);
4670 } else if (get_primary_ref_frame_buf(cm) == NULL) {
4671 // Base q-index may have changed, so we need to assign proper default coef
4672 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07004673 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004674 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004675 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004676
Yaowu Xuc27fc142016-08-22 16:08:15 -07004677 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004678 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004679 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004680 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00004681 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
4682 suppress_active_map(cpi);
4683 av1_cyclic_refresh_setup(cpi);
4684 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004685 }
David Turner2f3b5df2019-01-02 14:30:50 +00004686
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004687 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004688 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004689 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004690 } else {
Yaowu Xu7e450882019-04-30 15:09:18 -07004691 av1_calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004692 }
David Barkercab37552018-03-21 11:56:24 +00004693 } else {
4694 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004695 }
David Barkercab37552018-03-21 11:56:24 +00004696 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004697
David Turner2f3b5df2019-01-02 14:30:50 +00004698 if (allow_recode) save_coding_context(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004699#if CONFIG_COLLECT_COMPONENT_TIMING
4700 start_timing(cpi, av1_encode_frame_time);
4701#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004702 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004703 av1_encode_frame(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004704#if CONFIG_COLLECT_COMPONENT_TIMING
4705 end_timing(cpi, av1_encode_frame_time);
4706#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004707
Yaowu Xuf883b422016-08-30 14:01:10 -07004708 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004709
4710 // Dummy pack of the bitstream using up to date stats to get an
4711 // accurate estimate of output frame size to determine if we need
4712 // to recode.
4713 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004714 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004715
David Turner996b2c12018-12-07 15:52:30 +00004716 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00004717 int largest_tile_id = 0; // Output from bitstream: unused here
4718 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004719 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004720
4721 rc->projected_frame_size = (int)(*size) << 3;
4722 restore_coding_context(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004723 }
4724
Hui Suef139e12019-05-20 15:51:22 -07004725 if (allow_recode) {
David Turner2f3b5df2019-01-02 14:30:50 +00004726 // Update q and decide whether to do a recode loop
4727 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
4728 bottom_index, &undershoot_seen, &overshoot_seen,
Hui Suef139e12019-05-20 15:51:22 -07004729 &low_cr_seen, loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004730 }
4731
4732 // Special case for overlay frame.
4733 if (rc->is_src_frame_alt_ref &&
4734 rc->projected_frame_size < rc->max_frame_bandwidth)
4735 loop = 0;
4736
David Turner2f3b5df2019-01-02 14:30:50 +00004737 if (allow_recode && !cpi->sf.gm_disable_recode &&
4738 recode_loop_test_global_motion(cpi)) {
4739 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004740 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004741
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004742 if (cpi->tpl_model_pass == 1) {
4743 assert(cpi->oxcf.enable_tpl_model == 2);
Debargha Mukherjee347c64d2019-05-08 13:53:46 -07004744 av1_tpl_setup_forward_stats(cpi);
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07004745 cpi->tpl_model_pass = 0;
4746 loop = 1;
4747 }
4748
Yaowu Xuc27fc142016-08-22 16:08:15 -07004749 if (loop) {
4750 ++loop_count;
4751 ++loop_at_this_size;
4752
4753#if CONFIG_INTERNAL_STATS
4754 ++cpi->tot_recode_hits;
4755#endif
4756 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004757#if CONFIG_COLLECT_COMPONENT_TIMING
4758 if (loop) printf("\n Recoding:");
4759#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004760 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004761
4762 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004763}
4764
Yaowu Xuc27fc142016-08-22 16:08:15 -07004765#define DUMP_RECON_FRAMES 0
4766
4767#if DUMP_RECON_FRAMES == 1
4768// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004769static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4770 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00004771 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00004772 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004773
Zoe Liub4f31032017-11-03 23:48:35 -07004774 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00004775 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07004776 return;
4777 }
4778
Zoe Liu27deb382018-03-27 15:13:56 -07004779 static const int flag_list[REF_FRAMES] = { 0,
4780 AOM_LAST_FLAG,
4781 AOM_LAST2_FLAG,
4782 AOM_LAST3_FLAG,
4783 AOM_GOLD_FLAG,
4784 AOM_BWD_FLAG,
4785 AOM_ALT2_FLAG,
4786 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004787 printf(
4788 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4789 "show_existing_frame=%d) "
4790 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00004791 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07004792 cm->show_existing_frame);
4793 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004794 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
4795 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08004796 printf(" %d(%c)", ref_offset,
4797 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07004798 }
4799 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004800
4801 if (!cm->show_frame) {
4802 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00004803 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004804 return;
4805 }
4806
Zoe Liub4f31032017-11-03 23:48:35 -07004807 int h;
4808 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4809 FILE *f_recon = NULL;
4810
David Turnerd2a592e2018-11-16 14:59:31 +00004811 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004812 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4813 printf("Unable to open file %s to write.\n", file_name);
4814 return;
4815 }
4816 } else {
4817 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4818 printf("Unable to open file %s to append.\n", file_name);
4819 return;
4820 }
4821 }
4822 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004823 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4824 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
Urvang Joshi7a890232019-03-22 17:00:31 -07004825 "refresh_alt_ref_frame=%d, "
Zoe Liuf40a9572017-10-13 12:37:19 -07004826 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
Sarah Parkere1b22012019-06-06 16:35:25 -07004827 current_frame->frame_number, cpi->gf_group.index,
4828 cpi->gf_group.update_type[cpi->gf_group.index], current_frame->order_hint,
4829 cm->show_frame, cm->show_existing_frame, cpi->rc.source_alt_ref_active,
4830 cpi->refresh_alt_ref_frame, recon_buf->y_stride, recon_buf->uv_stride,
4831 cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004832#if 0
4833 int ref_frame;
4834 printf("get_ref_frame_map_idx: [");
4835 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00004836 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07004837 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07004838#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004839
4840 // --- Y ---
4841 for (h = 0; h < cm->height; ++h) {
4842 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4843 f_recon);
4844 }
4845 // --- U ---
4846 for (h = 0; h < (cm->height >> 1); ++h) {
4847 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4848 f_recon);
4849 }
4850 // --- V ---
4851 for (h = 0; h < (cm->height >> 1); ++h) {
4852 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4853 f_recon);
4854 }
4855
4856 fclose(f_recon);
4857}
4858#endif // DUMP_RECON_FRAMES
4859
David Turnerbc0993e2019-02-15 14:42:23 +00004860static int get_interp_filter_selected(const AV1_COMMON *const cm,
4861 MV_REFERENCE_FRAME ref,
Sachin Kumar Garg952bb702019-06-18 20:15:07 +05304862 InterpFilter ifilter) {
David Turnerbc0993e2019-02-15 14:42:23 +00004863 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
4864 if (buf == NULL) return 0;
4865 return buf->interp_filter_selected[ifilter];
4866}
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304867
David Turnerbc0993e2019-02-15 14:42:23 +00004868static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
4869 const AV1_COMMON *const cm = &cpi->common;
4870 int ref_total[REF_FRAMES] = { 0 };
4871
4872 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4873 return 0;
4874
4875 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
Sachin Kumar Garg952bb702019-06-18 20:15:07 +05304876 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
David Turnerbc0993e2019-02-15 14:42:23 +00004877 ++ifilter) {
4878 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
4879 }
4880 }
4881 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
4882 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
4883 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
4884
4885 int mask = 0;
Sachin Kumar Garg952bb702019-06-18 20:15:07 +05304886 for (InterpFilter ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
David Turnerbc0993e2019-02-15 14:42:23 +00004887 ++ifilter) {
4888 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
4889 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
4890 int filter_score =
4891 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
4892 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
4893 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
4894 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
4895 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
4896 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
4897 if (filter_score < ref_total_total) mask |= 1 << ifilter;
4898 }
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304899 }
4900 return mask;
4901}
4902
David Turnerefed6372019-01-11 15:14:11 +00004903static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
4904 const YV12_BUFFER_CONFIG *last_picture,
4905 hash_table *last_hash_table) {
4906 aom_clear_system_state();
4907 // check use hash ME
4908 int k;
4909 uint32_t hash_value_1;
4910 uint32_t hash_value_2;
4911
4912 const int block_size = 8;
4913 const double threshold_current = 0.8;
4914 const double threshold_average = 0.95;
4915 const int max_history_size = 32;
4916 int T = 0; // total block
4917 int C = 0; // match with collocated block
4918 int S = 0; // smooth region but not match with collocated block
4919 int M = 0; // match with other block
4920
4921 const int pic_width = cur_picture->y_width;
4922 const int pic_height = cur_picture->y_height;
4923 for (int i = 0; i + block_size <= pic_height; i += block_size) {
4924 for (int j = 0; j + block_size <= pic_width; j += block_size) {
4925 const int x_pos = j;
4926 const int y_pos = i;
4927 int match = 1;
4928 T++;
4929
4930 // check whether collocated block match with current
4931 uint8_t *p_cur = cur_picture->y_buffer;
4932 uint8_t *p_ref = last_picture->y_buffer;
4933 int stride_cur = cur_picture->y_stride;
4934 int stride_ref = last_picture->y_stride;
4935 p_cur += (y_pos * stride_cur + x_pos);
4936 p_ref += (y_pos * stride_ref + x_pos);
4937
4938 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
4939 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
4940 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
4941 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4942 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4943 if (p16_cur[tmpX] != p16_ref[tmpX]) {
4944 match = 0;
4945 }
4946 }
4947 p16_cur += stride_cur;
4948 p16_ref += stride_ref;
4949 }
4950 } else {
4951 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4952 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4953 if (p_cur[tmpX] != p_ref[tmpX]) {
4954 match = 0;
4955 }
4956 }
4957 p_cur += stride_cur;
4958 p_ref += stride_ref;
4959 }
4960 }
4961
4962 if (match) {
4963 C++;
4964 continue;
4965 }
4966
4967 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
4968 y_pos) ||
4969 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
4970 S++;
4971 continue;
4972 }
4973
4974 av1_get_block_hash_value(
4975 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
4976 block_size, &hash_value_1, &hash_value_2,
4977 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
4978 // Hashing does not work for highbitdepth currently.
4979 // TODO(Roger): Make it work for highbitdepth.
4980 if (av1_use_hash_me(&cpi->common)) {
4981 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
4982 M++;
4983 }
4984 }
4985 }
4986 }
4987
4988 assert(T > 0);
4989 double csm_rate = ((double)(C + S + M)) / ((double)(T));
4990 double m_rate = ((double)(M)) / ((double)(T));
4991
4992 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
4993 cpi->m_rate_array[cpi->rate_index] = m_rate;
4994
4995 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
4996 cpi->rate_size++;
4997 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
4998
4999 if (csm_rate < threshold_current) {
5000 return 0;
5001 }
5002
5003 if (C == T) {
5004 return 1;
5005 }
5006
5007 double csm_average = 0.0;
5008 double m_average = 0.0;
5009
5010 for (k = 0; k < cpi->rate_size; k++) {
5011 csm_average += cpi->csm_rate_array[k];
5012 m_average += cpi->m_rate_array[k];
5013 }
5014 csm_average /= cpi->rate_size;
5015 m_average /= cpi->rate_size;
5016
5017 if (csm_average < threshold_average) {
5018 return 0;
5019 }
5020
5021 if (M > (T - C - S) / 3) {
5022 return 1;
5023 }
5024
5025 if (csm_rate > 0.99 && m_rate > 0.01) {
5026 return 1;
5027 }
5028
5029 if (csm_average + m_average > 1.01) {
5030 return 1;
5031 }
5032
5033 return 0;
5034}
5035
David Turner73245762019-02-11 16:42:34 +00005036// Refresh reference frame buffers according to refresh_frame_flags.
5037static void refresh_reference_frames(AV1_COMP *cpi) {
5038 AV1_COMMON *const cm = &cpi->common;
5039 // All buffers are refreshed for shown keyframes and S-frames.
5040
5041 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
5042 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
5043 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
5044 }
5045 }
5046}
5047
sdengc23c7f12019-06-11 16:56:50 -07005048// Implementation and modifications of C. Yeo, H. L. Tan, and Y. H. Tan, "On
5049// rate distortion optimization using SSIM," Circuits and Systems for Video
5050// Technology, IEEE Transactions on, vol. 23, no. 7, pp. 1170-1181, 2013.
sdeng32185d12019-06-19 14:47:09 -07005051// SSIM_VAR_SCALE defines the strength of the bias towards SSIM in RDO:
5052// Test data set: mid_res (33 frames)
5053// SSIM_VAR_SCALE avg_psnr ssim ms-ssim
5054// 8 8.2 -6.0 -6.4
5055// 16 4.0 -5.7 -5.9
5056// 32 1.6 -4.4 -4.5
sdengc23c7f12019-06-11 16:56:50 -07005057#define SSIM_VAR_SCALE 16.0
5058static void set_mb_ssim_rdmult_scaling(AV1_COMP *cpi) {
5059 AV1_COMMON *cm = &cpi->common;
5060 ThreadData *td = &cpi->td;
5061 MACROBLOCK *x = &td->mb;
5062 MACROBLOCKD *xd = &x->e_mbd;
5063 uint8_t *y_buffer = cpi->source->y_buffer;
5064 const int y_stride = cpi->source->y_stride;
5065 const int block_size = BLOCK_16X16;
5066
5067 const int num_mi_w = mi_size_wide[block_size];
5068 const int num_mi_h = mi_size_high[block_size];
5069 const int num_cols = (cm->mi_cols + num_mi_w - 1) / num_mi_w;
5070 const int num_rows = (cm->mi_rows + num_mi_h - 1) / num_mi_h;
5071 double log_sum = 0.0;
5072 int row, col;
5073 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
5074
sdeng32185d12019-06-19 14:47:09 -07005075 // TODO(sdeng): tune this param for 12bit videos.
5076 double c2 = 58.5225; // (.03*255)^2
5077 c2 *= SSIM_VAR_SCALE;
sdengc23c7f12019-06-11 16:56:50 -07005078
5079 // Loop through each 16x16 block.
5080 for (row = 0; row < num_rows; ++row) {
5081 for (col = 0; col < num_cols; ++col) {
5082 int mi_row, mi_col;
5083 double var = 0.0, num_of_var = 0.0;
5084 const int index = row * num_cols + col;
5085
5086 // Loop through each 8x8 block.
5087 for (mi_row = row * num_mi_h;
5088 mi_row < cm->mi_rows && mi_row < (row + 1) * num_mi_h; mi_row += 2) {
5089 for (mi_col = col * num_mi_w;
5090 mi_col < cm->mi_cols && mi_col < (col + 1) * num_mi_w;
5091 mi_col += 2) {
5092 struct buf_2d buf;
5093 const int row_offset_y = mi_row << 2;
5094 const int col_offset_y = mi_col << 2;
5095
5096 buf.buf = y_buffer + row_offset_y * y_stride + col_offset_y;
5097 buf.stride = y_stride;
5098
5099 if (use_hbd) {
5100 var += av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8,
5101 xd->bd);
5102 } else {
5103 var += av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_8X8);
5104 }
5105
5106 num_of_var += 1.0;
5107 }
5108 }
sdeng32185d12019-06-19 14:47:09 -07005109 var = var / num_of_var;
sdengc23c7f12019-06-11 16:56:50 -07005110 var = 2.0 * var + c2;
5111 cpi->ssim_rdmult_scaling_factors[index] = var;
5112 log_sum += log(var);
5113 }
5114 }
5115 log_sum = exp(log_sum / (double)(num_rows * num_cols));
5116
5117 for (row = 0; row < num_rows; ++row) {
5118 for (col = 0; col < num_cols; ++col) {
5119 const int index = row * num_cols + col;
5120 cpi->ssim_rdmult_scaling_factors[index] /= log_sum;
5121 }
5122 }
5123
5124 (void)xd;
5125}
5126
David Turner73245762019-02-11 16:42:34 +00005127static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
5128 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005129 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005130 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00005131 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07005132 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005133 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01005134
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005135#if CONFIG_COLLECT_COMPONENT_TIMING
5136 start_timing(cpi, encode_frame_to_data_rate_time);
5137#endif
5138
Fangwen Fu8d164de2016-12-14 13:40:54 -08005139 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00005140 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07005141
Yunqing Wang9612d552018-05-15 14:58:30 -07005142 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
5143 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07005144
sarahparker21dbca42018-03-30 17:43:44 -07005145 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07005146 // cm->allow_ref_frame_mvs needs to be written into the frame header while
5147 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
5148 // separated from frame_might_allow_ref_frame_mvs().
5149 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
5150
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07005151 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07005152 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005153
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305154 cm->last_frame_type = current_frame->frame_type;
5155 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
5156 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
5157
Sarah Parker33005522018-07-27 14:46:25 -07005158 if (encode_show_existing_frame(cm)) {
Jingning Han8f661602017-08-19 08:16:50 -07005159 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07005160
David Turner996b2c12018-12-07 15:52:30 +00005161 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005162 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005163 int largest_tile_id = 0; // Output from bitstream: unused here
5164 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005165 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005166
David Turner90311862018-11-29 13:34:36 +00005167 if (seq_params->frame_id_numbers_present_flag &&
5168 current_frame->frame_type == KEY_FRAME) {
5169 // Displaying a forward key-frame, so reset the ref buffer IDs
5170 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
5171 for (int i = 0; i < REF_FRAMES; i++)
5172 cm->ref_frame_id[i] = display_frame_id;
5173 }
5174
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005175 cpi->seq_params_locked = 1;
5176
Yaowu Xuc27fc142016-08-22 16:08:15 -07005177#if DUMP_RECON_FRAMES == 1
5178 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
5179 dump_filtered_recon_frames(cpi);
5180#endif // DUMP_RECON_FRAMES
5181
David Turner73245762019-02-11 16:42:34 +00005182 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5183 // for the purpose to verify no mismatch between encoder and decoder.
5184 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005185
David Turner73245762019-02-11 16:42:34 +00005186 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005187
Yaowu Xuc27fc142016-08-22 16:08:15 -07005188 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
5189 // to do post-encoding update accordingly.
5190 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07005191 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07005192 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005193 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005194
David Turnerd2a592e2018-11-16 14:59:31 +00005195 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005196
Tom Finegane4099e32018-01-23 12:01:51 -08005197 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005198 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005199
David Turnerefed6372019-01-11 15:14:11 +00005200 // Work out whether to force_integer_mv this frame
5201 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
5202 !frame_is_intra_only(cm)) {
5203 if (cpi->common.seq_params.force_integer_mv == 2) {
5204 // Adaptive mode: see what previous frame encoded did
5205 if (cpi->unscaled_last_source != NULL) {
5206 cm->cur_frame_force_integer_mv =
5207 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
5208 cpi->previous_hash_table);
5209 } else {
5210 cpi->common.cur_frame_force_integer_mv = 0;
5211 }
5212 } else {
5213 cpi->common.cur_frame_force_integer_mv =
5214 cpi->common.seq_params.force_integer_mv;
5215 }
5216 } else {
5217 cpi->common.cur_frame_force_integer_mv = 0;
5218 }
5219
Yaowu Xuc27fc142016-08-22 16:08:15 -07005220 // Set default state for segment based loop filter update flags.
5221 cm->lf.mode_ref_delta_update = 0;
5222
Yaowu Xuc27fc142016-08-22 16:08:15 -07005223 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05005224 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005225 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07005226 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005227
5228 // If segmentation is enabled force a map update for key frames.
5229 if (seg->enabled) {
5230 seg->update_map = 1;
5231 seg->update_data = 1;
5232 }
5233
5234 // The alternate reference frame cannot be active for a key frame.
5235 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005236 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00005237 if (cpi->oxcf.mtu == 0) {
5238 cm->num_tg = cpi->oxcf.num_tile_groups;
5239 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08005240 // Use a default value for the purposes of weighting costs in probability
5241 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00005242 cm->num_tg = DEFAULT_MAX_NUM_TG;
5243 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005244
5245 // For 1 pass CBR, check if we are dropping this frame.
5246 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07005247 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00005248 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005249 if (av1_rc_drop_frame(cpi)) {
5250 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00005251 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08005252 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005253 }
5254 }
5255
sdengc23c7f12019-06-11 16:56:50 -07005256 if (oxcf->tuning == AOM_TUNE_SSIM) set_mb_ssim_rdmult_scaling(cpi);
5257
Yaowu Xuf883b422016-08-30 14:01:10 -07005258 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005259
5260#if CONFIG_INTERNAL_STATS
5261 memset(cpi->mode_chosen_counts, 0,
5262 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
5263#endif
5264
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005265 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005266 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08005267 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005268 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00005269 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08005270 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07005271 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
5272 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
5273 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005274 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005275 lsb = cpi->source->y_buffer[0] & 0xff;
5276 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00005277 }
David Turner760a2f42018-12-07 15:25:36 +00005278 cm->current_frame_id =
5279 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05005280
5281 // S_frame is meant for stitching different streams of different
5282 // resolutions together, so current_frame_id must be the
5283 // same across different streams of the same content current_frame_id
5284 // should be the same and not random. 0x37 is a chosen number as start
5285 // point
5286 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005287 } else {
5288 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00005289 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
5290 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005291 }
5292 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005293
Hui Su483a8452018-02-26 12:28:48 -08005294 switch (cpi->oxcf.cdf_update_mode) {
5295 case 0: // No CDF update for any frames(4~6% compression loss).
5296 cm->disable_cdf_update = 1;
5297 break;
5298 case 1: // Enable CDF update for all frames.
5299 cm->disable_cdf_update = 0;
5300 break;
5301 case 2:
5302 // Strategically determine at which frames to do CDF update.
5303 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5304 // compression loss).
5305 // TODO(huisu@google.com): design schemes for various trade-offs between
5306 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005307 cm->disable_cdf_update =
5308 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005309 break;
5310 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005311 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005312
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005313#if CONFIG_COLLECT_COMPONENT_TIMING
5314 start_timing(cpi, encode_with_recode_loop_time);
5315#endif
Debargha Mukherjee9b70d1f2019-05-07 18:34:45 -07005316
5317 if (cpi->oxcf.pass == 2 && cpi->oxcf.enable_tpl_model == 2 &&
5318 current_frame->frame_type == INTER_FRAME) {
5319 if (!cm->show_frame) {
5320 assert(cpi->tpl_model_pass == 0);
5321 cpi->tpl_model_pass = 1;
5322 }
5323 }
5324
David Turner2f3b5df2019-01-02 14:30:50 +00005325 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
5326 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005327#if CONFIG_COLLECT_COMPONENT_TIMING
5328 end_timing(cpi, encode_with_recode_loop_time);
5329#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005330
5331#ifdef OUTPUT_YUV_SKINMAP
David Turnerd2a592e2018-11-16 14:59:31 +00005332 if (cpi->common.current_frame.frame_number > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005333 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005334 }
5335#endif // OUTPUT_YUV_SKINMAP
5336
5337 // Special case code to reduce pulsing when key frames are forced at a
5338 // fixed interval. Note the reconstruction error if it is the frame before
5339 // the force key frame
5340 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005341 if (seq_params->use_highbitdepth) {
Jack Haughtonddb80602018-11-21 16:41:49 +00005342 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005343 } else {
Jack Haughtonddb80602018-11-21 16:41:49 +00005344 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005345 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005346 }
5347
David Turnerc29e1a92018-12-06 14:10:14 +00005348 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5349 cm->cur_frame->buf.transfer_characteristics =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005350 seq_params->transfer_characteristics;
David Turnerc29e1a92018-12-06 14:10:14 +00005351 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5352 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5353 cm->cur_frame->buf.chroma_sample_position =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005354 seq_params->chroma_sample_position;
David Turnerc29e1a92018-12-06 14:10:14 +00005355 cm->cur_frame->buf.color_range = seq_params->color_range;
5356 cm->cur_frame->buf.render_width = cm->render_width;
5357 cm->cur_frame->buf.render_height = cm->render_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005358
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005359 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5360 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005361
5362 // Pick the loop filter level for the frame.
Cheng Chen68dc9142018-05-02 17:46:28 -07005363 if (!cm->allow_intrabc) {
David Barker218556e2018-02-14 14:23:12 +00005364 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08005365 } else {
Hui Su06463e42018-02-23 22:17:36 -08005366 cm->lf.filter_level[0] = 0;
5367 cm->lf.filter_level[1] = 0;
David Turnerebf96f42018-11-14 16:57:57 +00005368 cm->cdef_info.cdef_bits = 0;
5369 cm->cdef_info.cdef_strengths[0] = 0;
5370 cm->cdef_info.nb_cdef_strengths = 1;
5371 cm->cdef_info.cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005372 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5373 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5374 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08005375 }
David Barker218556e2018-02-14 14:23:12 +00005376
5377 // TODO(debargha): Fix mv search range on encoder side
David Turnerc29e1a92018-12-06 14:10:14 +00005378 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5379 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005380
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005381#ifdef OUTPUT_YUV_REC
David Turnerc29e1a92018-12-06 14:10:14 +00005382 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005383#endif
5384
David Turner996b2c12018-12-07 15:52:30 +00005385 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005386 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005387 int largest_tile_id = 0; // Output from pack_bitstream
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005388#if CONFIG_COLLECT_COMPONENT_TIMING
5389 start_timing(cpi, av1_pack_bitstream_final_time);
5390#endif
David Turner35cba132018-12-10 15:48:15 +00005391 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005392 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005393#if CONFIG_COLLECT_COMPONENT_TIMING
5394 end_timing(cpi, av1_pack_bitstream_final_time);
5395#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005396
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005397 cpi->seq_params_locked = 1;
5398
David Turner996b2c12018-12-07 15:52:30 +00005399 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005400 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00005401 for (int i = 0; i < REF_FRAMES; i++) {
5402 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005403 cm->ref_frame_id[i] = cm->current_frame_id;
5404 }
5405 }
5406 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005407
Yaowu Xuc27fc142016-08-22 16:08:15 -07005408#if DUMP_RECON_FRAMES == 1
5409 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005410 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005411#endif // DUMP_RECON_FRAMES
5412
Soo-Chul Han934af352017-10-15 15:21:51 -04005413 if (cm->seg.enabled) {
5414 if (cm->seg.update_map) {
5415 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005416 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00005417 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04005418 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5419 }
5420 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005421
5422 if (frame_is_intra_only(cm) == 0) {
5423 release_scaled_references(cpi);
5424 }
5425
David Turner73245762019-02-11 16:42:34 +00005426 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5427 // for the purpose to verify no mismatch between encoder and decoder.
5428 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
5429
5430 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005431
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005432#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005433 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005434#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005435
Hui Sudc54be62018-03-14 19:14:28 -07005436 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00005437 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07005438 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005439 }
David Turnera4c96252019-01-11 16:36:39 +00005440 if (!cm->large_scale_tile) {
5441 cm->cur_frame->frame_context = *cm->fc;
5442 }
5443#define EXT_TILE_DEBUG 0
5444#if EXT_TILE_DEBUG
5445 if (cm->large_scale_tile && oxcf->pass == 2) {
5446 char fn[20] = "./fc";
5447 fn[4] = current_frame->frame_number / 100 + '0';
5448 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
5449 fn[6] = (current_frame->frame_number % 10) + '0';
5450 fn[7] = '\0';
5451 av1_print_frame_contexts(cm->fc, fn);
5452 }
5453#endif // EXT_TILE_DEBUG
5454#undef EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07005455
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005456#if CONFIG_COLLECT_COMPONENT_TIMING
5457 end_timing(cpi, encode_frame_to_data_rate_time);
5458
5459 // Print out timing information.
5460 int i;
chiyotsai9c484b32019-03-07 16:01:50 -08005461 fprintf(stderr, "\n Frame number: %d, Frame type: %s, Show Frame: %d\n",
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005462 cm->current_frame.frame_number,
chiyotsai9c484b32019-03-07 16:01:50 -08005463 get_frame_type_enum(cm->current_frame.frame_type), cm->show_frame);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005464 for (i = 0; i < kTimingComponents; i++) {
5465 cpi->component_time[i] += cpi->frame_component_time[i];
5466 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
5467 get_component_name(i), cpi->frame_component_time[i],
5468 cpi->component_time[i]);
5469 cpi->frame_component_time[i] = 0;
5470 }
5471#endif
5472
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305473 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005474
Yaowu Xuf883b422016-08-30 14:01:10 -07005475 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005476
David Turnerefed6372019-01-11 15:14:11 +00005477 // Store encoded frame's hash table for is_integer_mv() next time
5478 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
5479 cpi->previous_hash_table = &cm->cur_frame->hash_table;
5480 }
5481
Yaowu Xuc27fc142016-08-22 16:08:15 -07005482 // Clear the one shot update flags for segmentation map and mode/ref loop
5483 // filter deltas.
5484 cm->seg.update_map = 0;
5485 cm->seg.update_data = 0;
5486 cm->lf.mode_ref_delta_update = 0;
5487
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005488 // A droppable frame might not be shown but it always
5489 // takes a space in the gf group. Therefore, even when
5490 // it is not shown, we still need update the count down.
5491
Yaowu Xuc27fc142016-08-22 16:08:15 -07005492 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08005493 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5494 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005495 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005496 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005497 // Don't increment frame counters if this was an altref buffer
5498 // update not a real frame
Wei-Ting Lin96ee0eb2018-06-22 15:27:22 -07005499
David Turnerd2a592e2018-11-16 14:59:31 +00005500 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005501 }
5502
Tom Finegane4099e32018-01-23 12:01:51 -08005503 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005504}
5505
David Turner056f7cd2019-01-07 17:48:13 +00005506int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00005507 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00005508 const EncodeFrameParams *const frame_params,
5509 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00005510 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00005511 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005512
David Turnercb5e36f2019-01-17 17:15:25 +00005513 cpi->unscaled_source = frame_input->source;
5514 cpi->source = frame_input->source;
5515 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00005516
David Turner6e8b4d92019-02-18 15:01:33 +00005517 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00005518 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00005519 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00005520 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00005521 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005522 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00005523 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00005524 cm->show_existing_frame = frame_params->show_existing_frame;
5525 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00005526
David Turner73245762019-02-11 16:42:34 +00005527 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
5528 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
5529
David Turnerfe3aecb2019-02-06 14:42:42 +00005530 cpi->refresh_last_frame = frame_params->refresh_last_frame;
5531 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
5532 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
5533 cpi->refresh_alt2_ref_frame = frame_params->refresh_alt2_ref_frame;
5534 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
5535
David Turnera7f133c2019-01-22 14:47:16 +00005536 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
5537 current_frame->frame_number = 0;
5538
5539 if (cm->show_existing_frame) {
5540 current_frame->order_hint = cm->cur_frame->order_hint;
5541 } else {
5542 current_frame->order_hint =
5543 current_frame->frame_number + frame_params->order_offset;
5544 current_frame->order_hint %=
5545 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
5546 }
5547
David Turnercb5e36f2019-01-17 17:15:25 +00005548 if (cpi->oxcf.pass == 1) {
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005549#if !CONFIG_REALTIME_ONLY
David Turnercb5e36f2019-01-17 17:15:25 +00005550 av1_first_pass(cpi, frame_input->ts_duration);
Jerome Jiang2612b4d2019-05-29 17:46:47 -07005551#endif
David Turnercb5e36f2019-01-17 17:15:25 +00005552 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00005553 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
5554 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00005555 return AOM_CODEC_ERROR;
5556 }
5557 } else {
David Turner056f7cd2019-01-07 17:48:13 +00005558 return AOM_CODEC_ERROR;
5559 }
5560
5561 return AOM_CODEC_OK;
5562}
5563
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005564#if CONFIG_DENOISE
5565static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5566 int block_size, float noise_level,
5567 int64_t time_stamp, int64_t end_time) {
5568 AV1_COMMON *const cm = &cpi->common;
5569 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005570 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5571 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005572 if (!cpi->denoise_and_model) {
5573 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5574 "Error allocating denoise and model");
5575 return -1;
5576 }
5577 }
5578 if (!cpi->film_grain_table) {
5579 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5580 if (!cpi->film_grain_table) {
5581 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5582 "Error allocating grain table");
5583 return -1;
5584 }
5585 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5586 }
5587 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5588 &cm->film_grain_params)) {
5589 if (cm->film_grain_params.apply_grain) {
5590 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5591 &cm->film_grain_params);
5592 }
5593 }
5594 return 0;
5595}
5596#endif
5597
James Zern3e2613b2017-03-30 23:14:40 -07005598int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005599 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5600 int64_t end_time) {
5601 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005602 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005603 int res = 0;
5604 const int subsampling_x = sd->subsampling_x;
5605 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005606 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005607
Yaowu Xuc27fc142016-08-22 16:08:15 -07005608 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005609
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005610#if CONFIG_INTERNAL_STATS
5611 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005612 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005613#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005614#if CONFIG_DENOISE
5615 if (cpi->oxcf.noise_level > 0)
5616 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5617 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5618 res = -1;
5619#endif // CONFIG_DENOISE
5620
Yaowu Xuf883b422016-08-30 14:01:10 -07005621 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005622 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005623 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005624#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005625 aom_usec_timer_mark(&timer);
5626 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005627#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005628 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005629 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005630 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005631 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005632 res = -1;
5633 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005634 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005635 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005636 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005637 "Profile 1 requires 4:4:4 color format");
5638 res = -1;
5639 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005640 if ((seq_params->profile == PROFILE_2) &&
5641 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005642 !(subsampling_x == 1 && subsampling_y == 0)) {
5643 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5644 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005645 res = -1;
5646 }
5647
5648 return res;
5649}
5650
Yaowu Xuc27fc142016-08-22 16:08:15 -07005651#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005652extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5653 const unsigned char *img2, int img2_pitch,
5654 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005655
5656static void adjust_image_stat(double y, double u, double v, double all,
5657 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07005658 s->stat[STAT_Y] += y;
5659 s->stat[STAT_U] += u;
5660 s->stat[STAT_V] += v;
5661 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005662 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005663}
5664
Angie Chiang08a22a62017-07-17 17:29:17 -07005665static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005666 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005667 double samples = 0.0;
5668 uint32_t in_bit_depth = 8;
5669 uint32_t bit_depth = 8;
5670
Angie Chiang08a22a62017-07-17 17:29:17 -07005671#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00005672 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005673#endif
5674 cpi->bytes += frame_bytes;
5675
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005676 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005677 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005678 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005679 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005680 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005681 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00005682 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005683 double y, u, v, frame_all;
5684
5685 cpi->count++;
5686 if (cpi->b_calculate_psnr) {
5687 PSNR_STATS psnr;
5688 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005689 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005690 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005691 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005692
5693 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5694 &cpi->psnr);
5695 cpi->total_sq_error += psnr.sse[0];
5696 cpi->total_samples += psnr.samples[0];
5697 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005698 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005699 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005700 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005701 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005702 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005703 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005704
Yaowu Xuf883b422016-08-30 14:01:10 -07005705 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005706 cpi->summed_quality += frame_ssim2 * weight;
5707 cpi->summed_weights += weight;
5708
5709#if 0
5710 {
5711 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005712 double y2 = psnr.psnr[1];
5713 double u2 = psnr.psnr[2];
5714 double v2 = psnr.psnr[3];
5715 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005716 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 +00005717 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005718 frame_psnr2, frame_ssim2);
5719 fclose(f);
5720 }
5721#endif
5722 }
5723 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005724 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005725 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005726 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5727 recon->y_stride, orig->y_width, orig->y_height);
5728 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005729 cpi->total_blockiness += frame_blockiness;
5730 }
5731
5732 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005733 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005734 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005735 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5736 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5737
5738 const double peak = (double)((1 << in_bit_depth) - 1);
5739 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005740 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005741 if (consistency > 0.0)
5742 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005743 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005744 cpi->total_inconsistency += this_inconsistency;
5745 }
5746 }
5747 }
5748
5749 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005750 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005751 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005752 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005753 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5754 }
5755}
5756#endif // CONFIG_INTERNAL_STATS
Andrey Norkin795ba872018-03-06 13:24:14 -08005757int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5758 size_t *size, uint8_t *dest, int64_t *time_stamp,
5759 int64_t *time_end, int flush,
Yue Chen1bc5be62018-08-24 13:57:32 -07005760 const aom_rational64_t *timestamp_ratio) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005761 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5762 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005763
5764#if CONFIG_BITSTREAM_DEBUG
5765 assert(cpi->oxcf.max_threads == 0 &&
5766 "bitstream debug tool does not support multithreading");
5767 bitstream_queue_record_write();
Yaowu Xu63f2ea32019-04-29 10:47:42 -07005768 aom_bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
5769 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005770#endif
5771
Sarah Parker740e8392019-01-23 15:47:53 -08005772 // Indicates whether or not to use an adaptive quantize b rather than
5773 // the traditional version
5774 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
5775
Dominic Symesd4929012018-01-31 17:32:01 +01005776 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00005777 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005778#if CONFIG_INTERNAL_STATS
5779 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005780 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005781#endif
RogerZhou3b635242017-09-19 10:06:46 -07005782 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005783
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07005784 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07005785 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
5786 ? REFRESH_FRAME_CONTEXT_DISABLED
5787 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005788 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08005789 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005790
Sarah Parkerb9041612018-05-22 19:06:47 -07005791 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005792 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005793
David Turnerdedd8ff2019-01-23 13:59:46 +00005794 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005795
Yue Chen1bc5be62018-08-24 13:57:32 -07005796 const int result =
5797 av1_encode_strategy(cpi, size, dest, frame_flags, time_stamp, time_end,
5798 timestamp_ratio, flush);
David Turnerdedd8ff2019-01-23 13:59:46 +00005799 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00005800 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00005801 } else if (result == -1) {
5802 // Returning -1 indicates no frame encoded; more input is required
5803 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005804 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005805#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005806 aom_usec_timer_mark(&cmptimer);
5807 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yue Chen1bc5be62018-08-24 13:57:32 -07005808#endif // CONFIG_INTERNAL_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00005809 if (cpi->b_calculate_psnr) {
5810 if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
5811 generate_psnr_packet(cpi);
5812 }
5813 }
Hui Su8ea87322019-03-29 14:44:32 -07005814
5815 if (cpi->keep_level_stats && oxcf->pass != 1) {
5816 // Initialize level info. at the beginning of each sequence.
5817 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Hui Su58753d62019-05-29 09:56:19 -07005818 av1_init_level_info(cpi);
Hui Su8ea87322019-03-29 14:44:32 -07005819 }
kyslovabeeb7c2019-03-06 18:35:04 -08005820 av1_update_level_info(cpi, *size, *time_stamp, *time_end);
Hui Su8ea87322019-03-29 14:44:32 -07005821 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005822
5823#if CONFIG_INTERNAL_STATS
5824 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07005825 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005826 }
5827#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005828#if CONFIG_SPEED_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00005829 if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005830 cpi->tx_search_count += cpi->td.mb.tx_search_count;
5831 cpi->td.mb.tx_search_count = 0;
5832 }
5833#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005834
Yaowu Xuf883b422016-08-30 14:01:10 -07005835 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005836
5837 return 0;
5838}
5839
Yaowu Xuf883b422016-08-30 14:01:10 -07005840int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5841 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005842 if (!cm->show_frame) {
5843 return -1;
5844 } else {
5845 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00005846 if (cm->cur_frame != NULL) {
5847 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005848 dest->y_width = cm->width;
5849 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005850 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
5851 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005852 ret = 0;
5853 } else {
5854 ret = -1;
5855 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005856 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005857 return ret;
5858 }
5859}
5860
Yaowu Xuf883b422016-08-30 14:01:10 -07005861int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00005862 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005863
David Turnere7ebf902018-12-04 14:04:55 +00005864 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005865 return 0;
5866}
5867
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005868static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
5869 const YV12_BUFFER_CONFIG *b) {
5870 return a->y_height == b->y_height && a->y_width == b->y_width &&
5871 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
5872 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
5873 a->border == b->border &&
5874 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
5875 (b->flags & YV12_FLAG_HIGHBITDEPTH);
5876}
5877
Yunqing Wang93b18f32018-06-08 21:08:29 -07005878aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
5879 YV12_BUFFER_CONFIG *new_frame,
5880 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005881 const int num_planes = av1_num_planes(cm);
5882 if (!equal_dimensions_and_border(new_frame, sd))
5883 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
5884 "Incorrect buffer dimensions");
5885 else
5886 aom_yv12_copy_frame(new_frame, sd, num_planes);
5887
5888 return cm->error.error_code;
5889}
5890
Yaowu Xuf883b422016-08-30 14:01:10 -07005891int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
5892 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005893 int hr = 0, hs = 0, vr = 0, vs = 0;
5894
5895 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5896
5897 Scale2Ratio(horiz_mode, &hr, &hs);
5898 Scale2Ratio(vert_mode, &vr, &vs);
5899
5900 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005901 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5902 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005903
5904 return 0;
5905}
5906
Yaowu Xuf883b422016-08-30 14:01:10 -07005907int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005908
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005909int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
5910 size_t output_size = 0;
5911 size_t total_bytes_read = 0;
5912 size_t remaining_size = *frame_size;
5913 uint8_t *buff_ptr = buffer;
5914
5915 // go through each OBUs
5916 while (total_bytes_read < *frame_size) {
5917 uint8_t saved_obu_header[2];
5918 uint64_t obu_payload_size;
5919 size_t length_of_payload_size;
5920 size_t length_of_obu_size;
5921 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
5922 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
5923
5924 // save the obu header (1 or 2 bytes)
5925 memmove(saved_obu_header, buff_ptr, obu_header_size);
5926 // clear the obu_has_size_field
5927 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
5928
5929 // get the payload_size and length of payload_size
5930 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
5931 &obu_payload_size, &length_of_payload_size) != 0) {
5932 return AOM_CODEC_ERROR;
5933 }
5934 obu_bytes_read += length_of_payload_size;
5935
5936 // calculate the length of size of the obu header plus payload
5937 length_of_obu_size =
5938 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
5939
5940 // move the rest of data to new location
5941 memmove(buff_ptr + length_of_obu_size + obu_header_size,
5942 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07005943 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005944
5945 // write the new obu size
5946 const uint64_t obu_size = obu_header_size + obu_payload_size;
5947 size_t coded_obu_size;
5948 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
5949 &coded_obu_size) != 0) {
5950 return AOM_CODEC_ERROR;
5951 }
5952
5953 // write the saved (modified) obu_header following obu size
5954 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
5955
5956 total_bytes_read += obu_bytes_read;
5957 remaining_size -= obu_bytes_read;
5958 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07005959 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005960 }
5961
5962 *frame_size = output_size;
5963 return AOM_CODEC_OK;
5964}
5965
Yaowu Xuf883b422016-08-30 14:01:10 -07005966void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005967 // TODO(yunqingwang): For what references to use, external encoding flags
5968 // should be consistent with internal reference frame selection. Need to
5969 // ensure that there is not conflict between the two. In AV1 encoder, the
5970 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
5971 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
5972 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005973 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005974 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005975 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
5976 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
5977 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005978 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005979 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005980 } else {
5981 int ref = AOM_REFFRAME_ALL;
5982
5983 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
5984 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
5985
5986 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
5987
5988 if (flags & AOM_EFLAG_NO_REF_ARF) {
5989 ref ^= AOM_ALT_FLAG;
5990 ref ^= AOM_BWD_FLAG;
5991 ref ^= AOM_ALT2_FLAG;
5992 } else {
5993 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
5994 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
5995 }
5996
5997 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005998 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005999 }
6000
6001 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006002 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07006003 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006004
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006005 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
6006 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006007
Yaowu Xuf883b422016-08-30 14:01:10 -07006008 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006009
Yunqing Wang9a50fec2017-11-02 17:02:00 -07006010 if (flags & AOM_EFLAG_NO_UPD_ARF) {
6011 upd ^= AOM_ALT_FLAG;
6012 upd ^= AOM_BWD_FLAG;
6013 upd ^= AOM_ALT2_FLAG;
6014 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07006015
David Turnerce9b5902019-01-23 17:25:47 +00006016 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
6017 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
6018 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
6019 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
6020 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
6021 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00006022 } else {
6023 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07006024 }
6025
sarahparker21dbca42018-03-30 17:43:44 -07006026 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
6027 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07006028 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
6029 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07006030 cpi->ext_use_s_frame =
6031 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07006032 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07006033
Yaowu Xuf883b422016-08-30 14:01:10 -07006034 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
6035 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07006036 }
6037}
Andrey Norkin795ba872018-03-06 13:24:14 -08006038
Tom Fineganf8d6a162018-08-21 10:47:55 -07006039aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
6040 if (!cpi) return NULL;
6041
6042 uint8_t header_buf[512] = { 0 };
6043 const uint32_t sequence_header_size =
Yaowu Xu797674b2019-05-01 17:38:11 -07006044 av1_write_sequence_header_obu(cpi, &header_buf[0]);
Tom Fineganf8d6a162018-08-21 10:47:55 -07006045 assert(sequence_header_size <= sizeof(header_buf));
6046 if (sequence_header_size == 0) return NULL;
6047
6048 const size_t obu_header_size = 1;
6049 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
6050 const size_t payload_offset = obu_header_size + size_field_size;
6051
6052 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
6053 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
6054
Hui Su4fd11762019-03-26 16:05:07 -07006055 if (av1_write_obu_header(cpi, OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
Tom Fineganf8d6a162018-08-21 10:47:55 -07006056 obu_header_size) {
6057 return NULL;
6058 }
6059
6060 size_t coded_size_field_size = 0;
6061 if (aom_uleb_encode(sequence_header_size, size_field_size,
6062 &header_buf[obu_header_size],
6063 &coded_size_field_size) != 0) {
6064 return NULL;
6065 }
6066 assert(coded_size_field_size == size_field_size);
6067
6068 aom_fixed_buf_t *global_headers =
6069 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
6070 if (!global_headers) return NULL;
6071
6072 const size_t global_header_buf_size =
6073 obu_header_size + size_field_size + sequence_header_size;
6074
6075 global_headers->buf = malloc(global_header_buf_size);
6076 if (!global_headers->buf) {
6077 free(global_headers);
6078 return NULL;
6079 }
6080
6081 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
6082 global_headers->sz = global_header_buf_size;
6083 return global_headers;
6084}