blob: 95362c43d93cb79fb6b2671726440c63d25e7632 [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"
Yue Chen7cae98f2018-08-24 10:43:16 -070074#include "av1/encoder/reconinter_enc.h"
kyslov7b9d0d62018-12-21 11:12:26 -080075#include "av1/encoder/var_based_part.h"
Yaowu Xuc27fc142016-08-22 16:08:15 -070076
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000077#define DEFAULT_EXPLICIT_ORDER_HINT_BITS 7
Imdad Sardharwallae68aa8a2018-03-07 18:52:54 +000078
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -080079#if CONFIG_ENTROPY_STATS
80FRAME_COUNTS aggregate_fc;
81#endif // CONFIG_ENTROPY_STATS
82
Yaowu Xuc27fc142016-08-22 16:08:15 -070083#define AM_SEGMENT_ID_INACTIVE 7
84#define AM_SEGMENT_ID_ACTIVE 0
85
Johannb0ef6ff2018-02-08 14:32:21 -080086// Whether to use high precision mv for altref computation.
87#define ALTREF_HIGH_PRECISION_MV 1
88
89// Q threshold for high precision mv. Choose a very high value for now so that
90// HIGH_PRECISION is always chosen.
91#define HIGH_PRECISION_MV_QTHRESH 200
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -070092
Yaowu Xuc27fc142016-08-22 16:08:15 -070093// #define OUTPUT_YUV_REC
Yaowu Xuc27fc142016-08-22 16:08:15 -070094#ifdef OUTPUT_YUV_SKINMAP
95FILE *yuv_skinmap_file = NULL;
96#endif
97#ifdef OUTPUT_YUV_REC
98FILE *yuv_rec_file;
99#define FILE_NAME_LEN 100
100#endif
101
Yaowu Xuf883b422016-08-30 14:01:10 -0700102static INLINE void Scale2Ratio(AOM_SCALING mode, int *hr, int *hs) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700103 switch (mode) {
104 case NORMAL:
105 *hr = 1;
106 *hs = 1;
107 break;
108 case FOURFIVE:
109 *hr = 4;
110 *hs = 5;
111 break;
112 case THREEFIVE:
113 *hr = 3;
114 *hs = 5;
115 break;
116 case ONETWO:
117 *hr = 1;
118 *hs = 2;
119 break;
120 default:
121 *hr = 1;
122 *hs = 1;
123 assert(0);
124 break;
125 }
126}
127
128// Mark all inactive blocks as active. Other segmentation features may be set
129// so memset cannot be used, instead only inactive blocks should be reset.
Yaowu Xuf883b422016-08-30 14:01:10 -0700130static void suppress_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700131 unsigned char *const seg_map = cpi->segmentation_map;
132 int i;
133 if (cpi->active_map.enabled || cpi->active_map.update)
134 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
135 if (seg_map[i] == AM_SEGMENT_ID_INACTIVE)
136 seg_map[i] = AM_SEGMENT_ID_ACTIVE;
137}
138
Yaowu Xuf883b422016-08-30 14:01:10 -0700139static void apply_active_map(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700140 struct segmentation *const seg = &cpi->common.seg;
141 unsigned char *const seg_map = cpi->segmentation_map;
142 const unsigned char *const active_map = cpi->active_map.map;
143 int i;
144
145 assert(AM_SEGMENT_ID_ACTIVE == CR_SEGMENT_ID_BASE);
146
147 if (frame_is_intra_only(&cpi->common)) {
148 cpi->active_map.enabled = 0;
149 cpi->active_map.update = 1;
150 }
151
152 if (cpi->active_map.update) {
153 if (cpi->active_map.enabled) {
154 for (i = 0; i < cpi->common.mi_rows * cpi->common.mi_cols; ++i)
155 if (seg_map[i] == AM_SEGMENT_ID_ACTIVE) seg_map[i] = active_map[i];
Yaowu Xuf883b422016-08-30 14:01:10 -0700156 av1_enable_segmentation(seg);
157 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700158 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
159 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
160 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
161 av1_enable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
162
163 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H,
164 -MAX_LOOP_FILTER);
165 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V,
166 -MAX_LOOP_FILTER);
167 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U,
168 -MAX_LOOP_FILTER);
169 av1_set_segdata(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V,
170 -MAX_LOOP_FILTER);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700171 } else {
Yaowu Xuf883b422016-08-30 14:01:10 -0700172 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_SKIP);
Cheng Chend8184da2017-09-26 18:15:22 -0700173 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_H);
174 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_Y_V);
175 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_U);
176 av1_disable_segfeature(seg, AM_SEGMENT_ID_INACTIVE, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700177 if (seg->enabled) {
178 seg->update_data = 1;
179 seg->update_map = 1;
180 }
181 }
182 cpi->active_map.update = 0;
183 }
184}
185
Yaowu Xuf883b422016-08-30 14:01:10 -0700186int av1_set_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
187 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700188 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols) {
189 unsigned char *const active_map_8x8 = cpi->active_map.map;
190 const int mi_rows = cpi->common.mi_rows;
191 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700192 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
193 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700194 cpi->active_map.update = 1;
195 if (new_map_16x16) {
196 int r, c;
197 for (r = 0; r < mi_rows; ++r) {
198 for (c = 0; c < mi_cols; ++c) {
199 active_map_8x8[r * mi_cols + c] =
Jingning Han9d533022017-04-07 10:14:42 -0700200 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)]
Yaowu Xuc27fc142016-08-22 16:08:15 -0700201 ? AM_SEGMENT_ID_ACTIVE
202 : AM_SEGMENT_ID_INACTIVE;
203 }
204 }
205 cpi->active_map.enabled = 1;
206 } else {
207 cpi->active_map.enabled = 0;
208 }
209 return 0;
210 } else {
211 return -1;
212 }
213}
214
Yaowu Xuf883b422016-08-30 14:01:10 -0700215int av1_get_active_map(AV1_COMP *cpi, unsigned char *new_map_16x16, int rows,
216 int cols) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700217 if (rows == cpi->common.mb_rows && cols == cpi->common.mb_cols &&
218 new_map_16x16) {
219 unsigned char *const seg_map_8x8 = cpi->segmentation_map;
220 const int mi_rows = cpi->common.mi_rows;
221 const int mi_cols = cpi->common.mi_cols;
Jingning Han9d533022017-04-07 10:14:42 -0700222 const int row_scale = mi_size_high[BLOCK_16X16] == 2 ? 1 : 2;
223 const int col_scale = mi_size_wide[BLOCK_16X16] == 2 ? 1 : 2;
224
Yaowu Xuc27fc142016-08-22 16:08:15 -0700225 memset(new_map_16x16, !cpi->active_map.enabled, rows * cols);
226 if (cpi->active_map.enabled) {
227 int r, c;
228 for (r = 0; r < mi_rows; ++r) {
229 for (c = 0; c < mi_cols; ++c) {
230 // Cyclic refresh segments are considered active despite not having
231 // AM_SEGMENT_ID_ACTIVE
Jingning Han9d533022017-04-07 10:14:42 -0700232 new_map_16x16[(r >> row_scale) * cols + (c >> col_scale)] |=
Yaowu Xuc27fc142016-08-22 16:08:15 -0700233 seg_map_8x8[r * mi_cols + c] != AM_SEGMENT_ID_INACTIVE;
234 }
235 }
236 }
237 return 0;
238 } else {
239 return -1;
240 }
241}
242
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800243// Compute the horizontal frequency components' energy in a frame
244// by calculuating the 16x4 Horizontal DCT. This is to be used to
245// decide the superresolution parameters.
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800246void analyze_hor_freq(const AV1_COMP *cpi, double *energy) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800247 uint64_t freq_energy[16] = { 0 };
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800248 const YV12_BUFFER_CONFIG *buf = cpi->source;
249 const int bd = cpi->td.mb.e_mbd.bd;
250 const int width = buf->y_crop_width;
251 const int height = buf->y_crop_height;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800252 DECLARE_ALIGNED(16, int32_t, coeff[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800253 int n = 0;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800254 memset(freq_energy, 0, sizeof(freq_energy));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800255 if (buf->flags & YV12_FLAG_HIGHBITDEPTH) {
256 const int16_t *src16 = (const int16_t *)CONVERT_TO_SHORTPTR(buf->y_buffer);
257 for (int i = 0; i < height - 4; i += 4) {
258 for (int j = 0; j < width - 16; j += 16) {
259 av1_fwd_txfm2d_16x4(src16 + i * buf->y_stride + j, coeff, buf->y_stride,
260 H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800261 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800262 const uint64_t this_energy =
263 ((int64_t)coeff[k] * coeff[k]) +
264 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
265 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
266 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800267 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2 + 2 * (bd - 8));
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800268 }
269 n++;
270 }
271 }
272 } else {
Debargha Mukherjeeac28c722018-11-14 22:09:46 -0800273 assert(bd == 8);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800274 DECLARE_ALIGNED(16, int16_t, src16[16 * 4]);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800275 for (int i = 0; i < height - 4; i += 4) {
276 for (int j = 0; j < width - 16; j += 16) {
277 for (int ii = 0; ii < 4; ++ii)
278 for (int jj = 0; jj < 16; ++jj)
279 src16[ii * 16 + jj] =
280 buf->y_buffer[(i + ii) * buf->y_stride + (j + jj)];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800281 av1_fwd_txfm2d_16x4(src16, coeff, 16, H_DCT, bd);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800282 for (int k = 1; k < 16; ++k) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800283 const uint64_t this_energy =
284 ((int64_t)coeff[k] * coeff[k]) +
285 ((int64_t)coeff[k + 16] * coeff[k + 16]) +
286 ((int64_t)coeff[k + 32] * coeff[k + 32]) +
287 ((int64_t)coeff[k + 48] * coeff[k + 48]);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800288 freq_energy[k] += ROUND_POWER_OF_TWO(this_energy, 2);
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800289 }
290 n++;
291 }
292 }
293 }
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800294 if (n) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800295 for (int k = 1; k < 16; ++k) energy[k] = (double)freq_energy[k] / n;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800296 // Convert to cumulative energy
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800297 for (int k = 14; k > 0; --k) energy[k] += energy[k + 1];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800298 } else {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -0800299 for (int k = 1; k < 16; ++k) energy[k] = 1e+20;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -0800300 }
Debargha Mukherjeef50fdce2018-11-13 11:13:00 -0800301}
302
Yaowu Xu45295c32018-03-29 12:06:10 -0700303static void set_high_precision_mv(AV1_COMP *cpi, int allow_high_precision_mv,
304 int cur_frame_force_integer_mv) {
James Zern01a9d702017-08-25 19:09:33 +0000305 MACROBLOCK *const mb = &cpi->td.mb;
Hui Su50361152018-03-02 11:01:42 -0800306 cpi->common.allow_high_precision_mv =
307 allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Rupert Swarbricka84faf22017-12-11 13:56:40 +0000308 const int copy_hp =
309 cpi->common.allow_high_precision_mv && cur_frame_force_integer_mv == 0;
Jingning Hanf050fc12018-03-09 14:53:33 -0800310 int *(*src)[2] = copy_hp ? &mb->nmvcost_hp : &mb->nmvcost;
311 mb->mv_cost_stack = *src;
James Zern01a9d702017-08-25 19:09:33 +0000312}
313
Yaowu Xuf883b422016-08-30 14:01:10 -0700314static BLOCK_SIZE select_sb_size(const AV1_COMP *const cpi) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800315 const AV1_COMMON *const cm = &cpi->common;
316
Yaowu Xuf883b422016-08-30 14:01:10 -0700317 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_64X64)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700318 return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100319#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800320 if (cm->options && cm->options->ext_partition)
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100321#endif
322 if (cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_128X128)
323 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700324
Yaowu Xuf883b422016-08-30 14:01:10 -0700325 assert(cpi->oxcf.superblock_size == AOM_SUPERBLOCK_SIZE_DYNAMIC);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700326
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100327// TODO(any): Possibly could improve this with a heuristic.
328#if CONFIG_FILEOPTIONS
Urvang Joshie4530f82018-01-09 11:43:37 -0800329 if (cm->options && !cm->options->ext_partition) return BLOCK_64X64;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +0100330#endif
Urvang Joshie4530f82018-01-09 11:43:37 -0800331
Urvang Joshiaab74432018-06-01 12:06:22 -0700332 // When superres / resize is on, 'cm->width / height' can change between
333 // calls, so we don't apply this heuristic there. Also, this heuristic gives
334 // compression gain for speed >= 2 only.
David Turner04b70d82019-01-24 15:39:19 +0000335 // Things break if superblock size changes per-frame which is why this
336 // heuristic is set based on configured speed rather than actual
337 // speed-features (which may change per-frame in future)
Urvang Joshiaab74432018-06-01 12:06:22 -0700338 if (cpi->oxcf.superres_mode == SUPERRES_NONE &&
339 cpi->oxcf.resize_mode == RESIZE_NONE && cpi->oxcf.speed >= 2) {
Urvang Joshie4530f82018-01-09 11:43:37 -0800340 return (cm->width >= 480 && cm->height >= 360) ? BLOCK_128X128
341 : BLOCK_64X64;
342 }
343
Yaowu Xuc27fc142016-08-22 16:08:15 -0700344 return BLOCK_128X128;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700345}
346
Yaowu Xuf883b422016-08-30 14:01:10 -0700347static void setup_frame(AV1_COMP *cpi) {
348 AV1_COMMON *const cm = &cpi->common;
Johannb0ef6ff2018-02-08 14:32:21 -0800349 // Set up entropy context depending on frame type. The decoder mandates
350 // the use of the default context, index 0, for keyframes and inter
351 // frames where the error_resilient_mode or intra_only flag is set. For
352 // other inter-frames the encoder currently uses only two contexts;
353 // context 1 for ALTREF frames and context 0 for the others.
Soo-Chul Han85e8c792018-01-21 01:58:15 -0500354
Sarah Parker50b6d6e2018-04-11 19:21:54 -0700355 if (frame_is_intra_only(cm) || cm->error_resilient_mode ||
David Turnera7f133c2019-01-22 14:47:16 +0000356 cpi->ext_use_primary_ref_none) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700357 av1_setup_past_independence(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700358 }
359
David Turnerd2a592e2018-11-16 14:59:31 +0000360 if (cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000361 set_sb_size(&cm->seq_params, select_sb_size(cpi));
Tarek AMARAc9813852018-03-05 18:40:18 -0500362 } else if (frame_is_sframe(cm)) {
Tarek AMARAc9813852018-03-05 18:40:18 -0500363 set_sb_size(&cm->seq_params, select_sb_size(cpi));
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);
Yunqing Wang19b9f722018-02-20 16:22:01 -0800383 cm->mi = cm->mip;
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530384 memset(cm->mip, 0, cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mip));
Yunqing Wang19b9f722018-02-20 16:22:01 -0800385 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700386 // Clear top border row
387 memset(cm->prev_mip, 0, sizeof(*cm->prev_mip) * cm->mi_stride);
388 // Clear left border column
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530389 for (i = 0; i < mi_rows_sb_aligned; ++i)
Yaowu Xuc27fc142016-08-22 16:08:15 -0700390 memset(&cm->prev_mip[i * cm->mi_stride], 0, sizeof(*cm->prev_mip));
Yunqing Wang19b9f722018-02-20 16:22:01 -0800391 cm->mi_grid_visible = cm->mi_grid_base;
392 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700393
394 memset(cm->mi_grid_base, 0,
Ravi Chaudhary75c4c5f2018-07-17 16:32:08 +0530395 cm->mi_stride * mi_rows_sb_aligned * sizeof(*cm->mi_grid_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700396}
397
Cheng Chen46f30c72017-09-07 11:13:33 -0700398static int enc_alloc_mi(AV1_COMMON *cm, int mi_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700399 cm->mip = aom_calloc(mi_size, sizeof(*cm->mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700400 if (!cm->mip) return 1;
Yaowu Xuf883b422016-08-30 14:01:10 -0700401 cm->prev_mip = aom_calloc(mi_size, sizeof(*cm->prev_mip));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700402 if (!cm->prev_mip) return 1;
403 cm->mi_alloc_size = mi_size;
404
Yue Chen53b53f02018-03-29 14:31:23 -0700405 cm->mi_grid_base =
406 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700407 if (!cm->mi_grid_base) return 1;
408 cm->prev_mi_grid_base =
Yue Chen53b53f02018-03-29 14:31:23 -0700409 (MB_MODE_INFO **)aom_calloc(mi_size, sizeof(MB_MODE_INFO *));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700410 if (!cm->prev_mi_grid_base) return 1;
411
412 return 0;
413}
414
Cheng Chen46f30c72017-09-07 11:13:33 -0700415static void enc_free_mi(AV1_COMMON *cm) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700416 aom_free(cm->mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700417 cm->mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700418 aom_free(cm->prev_mip);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700419 cm->prev_mip = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700420 aom_free(cm->mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700421 cm->mi_grid_base = NULL;
Yaowu Xuf883b422016-08-30 14:01:10 -0700422 aom_free(cm->prev_mi_grid_base);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700423 cm->prev_mi_grid_base = NULL;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700424 cm->mi_alloc_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700425}
426
Cheng Chen46f30c72017-09-07 11:13:33 -0700427static void swap_mi_and_prev_mi(AV1_COMMON *cm) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700428 // Current mip will be the prev_mip for the next frame.
Yue Chen53b53f02018-03-29 14:31:23 -0700429 MB_MODE_INFO **temp_base = cm->prev_mi_grid_base;
430 MB_MODE_INFO *temp = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700431 cm->prev_mip = cm->mip;
432 cm->mip = temp;
433
434 // Update the upper left visible macroblock ptrs.
Yunqing Wang19b9f722018-02-20 16:22:01 -0800435 cm->mi = cm->mip;
436 cm->prev_mi = cm->prev_mip;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700437
438 cm->prev_mi_grid_base = cm->mi_grid_base;
439 cm->mi_grid_base = temp_base;
Yunqing Wang19b9f722018-02-20 16:22:01 -0800440 cm->mi_grid_visible = cm->mi_grid_base;
441 cm->prev_mi_grid_visible = cm->prev_mi_grid_base;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700442}
443
Yaowu Xuf883b422016-08-30 14:01:10 -0700444void av1_initialize_enc(void) {
Wan-Teh Chang3cac4542018-06-29 10:21:39 -0700445 av1_rtcd();
446 aom_dsp_rtcd();
447 aom_scale_rtcd();
448 av1_init_intra_predictors();
449 av1_init_me_luts();
450 av1_rc_init_minq_luts();
451 av1_init_wedge_masks();
Yaowu Xuc27fc142016-08-22 16:08:15 -0700452}
453
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700454static void dealloc_context_buffers_ext(AV1_COMP *cpi) {
455 if (cpi->mbmi_ext_base) {
456 aom_free(cpi->mbmi_ext_base);
457 cpi->mbmi_ext_base = NULL;
458 }
459}
460
461static void alloc_context_buffers_ext(AV1_COMP *cpi) {
462 AV1_COMMON *cm = &cpi->common;
463 int mi_size = cm->mi_cols * cm->mi_rows;
464
465 dealloc_context_buffers_ext(cpi);
466 CHECK_MEM_ERROR(cm, cpi->mbmi_ext_base,
467 aom_calloc(mi_size, sizeof(*cpi->mbmi_ext_base)));
468}
469
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800470static void reset_film_grain_chroma_params(aom_film_grain_t *pars) {
471 pars->num_cr_points = 0;
472 pars->cr_mult = 0;
473 pars->cr_luma_mult = 0;
474 memset(pars->scaling_points_cr, 0, sizeof(pars->scaling_points_cr));
475 memset(pars->ar_coeffs_cr, 0, sizeof(pars->ar_coeffs_cr));
476 pars->num_cb_points = 0;
477 pars->cb_mult = 0;
478 pars->cb_luma_mult = 0;
Yaowu Xufda7dcb2019-01-16 13:04:33 -0800479 pars->chroma_scaling_from_luma = 0;
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800480 memset(pars->scaling_points_cb, 0, sizeof(pars->scaling_points_cb));
481 memset(pars->ar_coeffs_cb, 0, sizeof(pars->ar_coeffs_cb));
482}
483
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800484static void update_film_grain_parameters(struct AV1_COMP *cpi,
485 const AV1EncoderConfig *oxcf) {
486 AV1_COMMON *const cm = &cpi->common;
487 cpi->oxcf = *oxcf;
488
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700489 if (cpi->film_grain_table) {
490 aom_film_grain_table_free(cpi->film_grain_table);
491 aom_free(cpi->film_grain_table);
492 cpi->film_grain_table = NULL;
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700493 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700494
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800495 if (oxcf->film_grain_test_vector) {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700496 cm->seq_params.film_grain_params_present = 1;
David Turnerd2a592e2018-11-16 14:59:31 +0000497 if (cm->current_frame.frame_type == KEY_FRAME) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800498 memcpy(&cm->film_grain_params,
499 film_grain_test_vectors + oxcf->film_grain_test_vector - 1,
500 sizeof(cm->film_grain_params));
Yaowu Xuc0ea2582019-01-15 10:17:16 -0800501 if (oxcf->monochrome)
502 reset_film_grain_chroma_params(&cm->film_grain_params);
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700503 cm->film_grain_params.bit_depth = cm->seq_params.bit_depth;
504 if (cm->seq_params.color_range == AOM_CR_FULL_RANGE) {
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800505 cm->film_grain_params.clip_to_restricted_range = 0;
506 }
507 }
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700508 } else if (oxcf->film_grain_table_filename) {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800509 cm->seq_params.film_grain_params_present = 1;
510
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700511 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
512 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700513
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700514 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700515 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800516 } else {
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800517#if CONFIG_DENOISE
518 cm->seq_params.film_grain_params_present = (cpi->oxcf.noise_level > 0);
519#else
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700520 cm->seq_params.film_grain_params_present = 0;
Neil Birkbeckbd40ca72019-03-02 13:25:50 -0800521#endif
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800522 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
523 }
524}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800525
Yaowu Xuf883b422016-08-30 14:01:10 -0700526static void dealloc_compressor_data(AV1_COMP *cpi) {
527 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000528 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700529
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700530 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531
Yaowu Xuf883b422016-08-30 14:01:10 -0700532 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700533 cpi->tile_data = NULL;
534
535 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700536 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700538
Yaowu Xuf883b422016-08-30 14:01:10 -0700539 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700540 cpi->cyclic_refresh = NULL;
541
Yaowu Xuf883b422016-08-30 14:01:10 -0700542 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700543 cpi->active_map.map = NULL;
544
Jingning Hand064cf02017-06-01 10:00:39 -0700545 aom_free(cpi->td.mb.above_pred_buf);
546 cpi->td.mb.above_pred_buf = NULL;
547
548 aom_free(cpi->td.mb.left_pred_buf);
549 cpi->td.mb.left_pred_buf = NULL;
550
551 aom_free(cpi->td.mb.wsrc_buf);
552 cpi->td.mb.wsrc_buf = NULL;
553
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530554 aom_free(cpi->td.mb.inter_modes_info);
555 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530556
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530557 for (int i = 0; i < 2; i++)
558 for (int j = 0; j < 2; j++) {
559 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
560 cpi->td.mb.hash_value_buffer[i][j] = NULL;
561 }
Jingning Hand064cf02017-06-01 10:00:39 -0700562 aom_free(cpi->td.mb.mask_buf);
563 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700564
Jingning Han6cc1fd32017-10-13 09:05:36 -0700565 aom_free(cm->tpl_mvs);
566 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700567
Yaowu Xuf883b422016-08-30 14:01:10 -0700568 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700569 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700570 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700571
Yaowu Xuf883b422016-08-30 14:01:10 -0700572 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700573 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800574 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700575 aom_free_frame_buffer(&cpi->scaled_source);
576 aom_free_frame_buffer(&cpi->scaled_last_source);
577 aom_free_frame_buffer(&cpi->alt_ref_buffer);
578 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700579
Yaowu Xuf883b422016-08-30 14:01:10 -0700580 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581 cpi->tile_tok[0][0] = 0;
582
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530583 aom_free(cpi->tplist[0][0]);
584 cpi->tplist[0][0] = NULL;
585
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000586 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700587
hui sud9a812b2017-07-06 14:34:37 -0700588 aom_free(cpi->td.mb.palette_buffer);
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700589
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700590 aom_free(cpi->td.mb.tmp_conv_dst);
591 for (int j = 0; j < 2; ++j) {
592 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
593 }
594
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700595#if CONFIG_DENOISE
596 if (cpi->denoise_and_model) {
597 aom_denoise_and_model_free(cpi->denoise_and_model);
598 cpi->denoise_and_model = NULL;
599 }
600#endif
601 if (cpi->film_grain_table) {
602 aom_film_grain_table_free(cpi->film_grain_table);
603 cpi->film_grain_table = NULL;
604 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700605}
606
Yaowu Xuf883b422016-08-30 14:01:10 -0700607static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700608 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700609 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700610
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200611 // Stores a snapshot of key state variables which can subsequently be
612 // restored with a call to av1_restore_coding_context. These functions are
613 // intended for use in a re-code loop in av1_compress_frame where the
614 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800615 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
616 av1_copy(cc->nmv_costs, cpi->nmv_costs);
617 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000618
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619 cc->fc = *cm->fc;
620}
621
Yaowu Xuf883b422016-08-30 14:01:10 -0700622static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700623 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700624 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700625
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200626 // Restore key state variables to the snapshot state stored in the
627 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800628 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
629 av1_copy(cpi->nmv_costs, cc->nmv_costs);
630 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000631
Yaowu Xuc27fc142016-08-22 16:08:15 -0700632 *cm->fc = cc->fc;
633}
634
Yaowu Xuf883b422016-08-30 14:01:10 -0700635static void configure_static_seg_features(AV1_COMP *cpi) {
636 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700637 const RATE_CONTROL *const rc = &cpi->rc;
638 struct segmentation *const seg = &cm->seg;
639
640 int high_q = (int)(rc->avg_q > 48.0);
641 int qi_delta;
642
643 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000644 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700645 // Clear down the global segmentation map
646 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
647 seg->update_map = 0;
648 seg->update_data = 0;
649 cpi->static_mb_pct = 0;
650
651 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700652 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700653
654 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700655 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700656 } else if (cpi->refresh_alt_ref_frame) {
657 // If this is an alt ref frame
658 // Clear down the global segmentation map
659 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
660 seg->update_map = 0;
661 seg->update_data = 0;
662 cpi->static_mb_pct = 0;
663
664 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700665 av1_disable_segmentation(seg);
666 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700667
668 // 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);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700671
672 // If segmentation was enabled set those features needed for the
673 // arf itself.
674 if (seg->enabled) {
675 seg->update_map = 1;
676 seg->update_data = 1;
677
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700678 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
679 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700680 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700681 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
682 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
683 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
684 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
685
686 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
687 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
688 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
689 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700690
Yaowu Xuf883b422016-08-30 14:01:10 -0700691 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700692 }
693 } else if (seg->enabled) {
694 // All other frames if segmentation has been enabled
695
696 // First normal frame in a valid gf or alt ref group
697 if (rc->frames_since_golden == 0) {
698 // Set up segment features for normal frames in an arf group
699 if (rc->source_alt_ref_active) {
700 seg->update_map = 0;
701 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700702
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700703 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
704 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700705 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
706 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700707
Cheng Chend8184da2017-09-26 18:15:22 -0700708 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
709 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
710 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
711 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
712
713 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
714 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
715 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
716 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700717
718 // Segment coding disabled for compred testing
719 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700720 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
721 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
722 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723 }
724 } else {
725 // Disable segmentation and clear down features if alt ref
726 // is not active for this group
727
Yaowu Xuf883b422016-08-30 14:01:10 -0700728 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700729
730 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
731
732 seg->update_map = 0;
733 seg->update_data = 0;
734
Yaowu Xuf883b422016-08-30 14:01:10 -0700735 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700736 }
737 } else if (rc->is_src_frame_alt_ref) {
738 // Special case where we are coding over the top of a previous
739 // alt ref frame.
740 // Segment coding disabled for compred testing
741
742 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700743 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
744 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700745
746 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700747 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
748 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
749 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
750 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700751
752 // Skip all MBs if high Q (0,0 mv and skip coeffs)
753 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700754 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
755 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700756 }
757 // Enable data update
758 seg->update_data = 1;
759 } else {
760 // All other frames.
761
762 // No updates.. leave things as they are.
763 seg->update_map = 0;
764 seg->update_data = 0;
765 }
766 }
767}
768
Yaowu Xuf883b422016-08-30 14:01:10 -0700769static void update_reference_segmentation_map(AV1_COMP *cpi) {
770 AV1_COMMON *const cm = &cpi->common;
Yushin Choa7f65922018-04-04 16:06:11 -0700771 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
David Turnerb757ce02018-11-12 15:01:28 +0000772 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700773 int row, col;
774
775 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700776 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700777 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700778 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
779 cache[0] = mi_4x4[0]->segment_id;
780 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700781 cache_ptr += cm->mi_cols;
782 }
783}
784
Yaowu Xuf883b422016-08-30 14:01:10 -0700785static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
786 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700787 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700788 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700789
790 if (!cpi->lookahead)
Satish Kumar Suman29909962019-01-09 10:31:21 +0530791 cpi->lookahead = av1_lookahead_init(
792 oxcf->width, oxcf->height, seq_params->subsampling_x,
793 seq_params->subsampling_y, seq_params->use_highbitdepth,
794 oxcf->lag_in_frames, oxcf->border_in_pixels);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700795 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700796 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700797 "Failed to allocate lag buffers");
798
799 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700800 if (aom_realloc_frame_buffer(
801 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
802 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530803 seq_params->use_highbitdepth, oxcf->border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700804 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700805 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700806 "Failed to allocate altref buffer");
807}
808
Yaowu Xuf883b422016-08-30 14:01:10 -0700809static void alloc_util_frame_buffers(AV1_COMP *cpi) {
810 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700811 const SequenceHeader *const seq_params = &cm->seq_params;
812 if (aom_realloc_frame_buffer(
813 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
814 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530815 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700816 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700817 "Failed to allocate last frame buffer");
818
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700819 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800820 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700821 cm->superres_upscaled_height, seq_params->subsampling_x,
822 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +0530823 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800824 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800825 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700827 if (aom_realloc_frame_buffer(
828 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
829 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530830 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700831 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700832 "Failed to allocate scaled source buffer");
833
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700834 if (aom_realloc_frame_buffer(
835 &cpi->scaled_last_source, cm->width, cm->height,
836 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530837 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700838 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700839 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700840 "Failed to allocate scaled last source buffer");
841}
842
Cheng Chen46f30c72017-09-07 11:13:33 -0700843static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700844 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000845 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700846
Yaowu Xuf883b422016-08-30 14:01:10 -0700847 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700848
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530849 int mi_rows_aligned_to_sb =
850 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
851 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
852
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700853 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700854
Yaowu Xuc27fc142016-08-22 16:08:15 -0700855 alloc_context_buffers_ext(cpi);
856
Yaowu Xuf883b422016-08-30 14:01:10 -0700857 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700858
859 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000860 unsigned int tokens =
861 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700862 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700863 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530865 aom_free(cpi->tplist[0][0]);
866
867 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
868 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
869 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700870
Yaowu Xuf883b422016-08-30 14:01:10 -0700871 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700872}
873
Yaowu Xuf883b422016-08-30 14:01:10 -0700874void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700875 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700876 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700877}
878
Yunqing Wang75e20e82018-06-16 12:10:48 -0700879static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200880 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200881 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200882
883 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200884
885 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200886 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200887 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200888 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
889 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200890 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000891 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
892 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200893 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200894 cm->uniform_tile_spacing_flag = 0;
895 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
896 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200897 size_sb = cpi->oxcf.tile_widths[j++];
898 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000899 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200900 }
901 cm->tile_cols = i;
902 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200903 }
904 av1_calculate_tile_cols(cm);
905
906 // configure tile rows
907 if (cm->uniform_tile_spacing_flag) {
908 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
909 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200910 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000911 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
912 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200913 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200914 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
915 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200916 size_sb = cpi->oxcf.tile_heights[j++];
917 if (j >= cpi->oxcf.tile_height_count) j = 0;
918 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200919 }
920 cm->tile_rows = i;
921 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200922 }
923 av1_calculate_tile_rows(cm);
924}
925
Yaowu Xuf883b422016-08-30 14:01:10 -0700926static void update_frame_size(AV1_COMP *cpi) {
927 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700928 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
929
Yaowu Xuf883b422016-08-30 14:01:10 -0700930 av1_set_mb_mi(cm, cm->width, cm->height);
931 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500932 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700933 memset(cpi->mbmi_ext_base, 0,
934 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700935 set_tile_info(cpi);
936}
937
Yaowu Xuf883b422016-08-30 14:01:10 -0700938static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700939 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700940 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +0000941 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700942 cpi->rate_index = 0;
943 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700944}
945
Debargha Mukherjee57498692018-05-11 13:29:31 -0700946static INLINE int does_level_match(int width, int height, double fps,
947 int lvl_width, int lvl_height,
948 double lvl_fps, int lvl_dim_mult) {
949 const int64_t lvl_luma_pels = lvl_width * lvl_height;
950 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
951 const int64_t luma_pels = width * height;
952 const double display_sample_rate = luma_pels * fps;
953 return luma_pels <= lvl_luma_pels &&
954 display_sample_rate <= lvl_display_sample_rate &&
955 width <= lvl_width * lvl_dim_mult &&
956 height <= lvl_height * lvl_dim_mult;
957}
958
Andrey Norkin26495512018-06-20 17:13:11 -0700959static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -0700960 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700961 // TODO(any): This is a placeholder function that only addresses dimensions
962 // and max display sample rates.
963 // Need to add checks for max bit rate, max decoded luma sample rate, header
964 // rate, etc. that are not covered by this function.
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700965 (void)oxcf;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700966 BitstreamLevel bl = { 9, 3 };
967 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
968 288, 30.0, 4)) {
969 bl.major = 2;
970 bl.minor = 0;
971 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
972 704, 396, 30.0, 4)) {
973 bl.major = 2;
974 bl.minor = 1;
975 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
976 1088, 612, 30.0, 4)) {
977 bl.major = 3;
978 bl.minor = 0;
979 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
980 1376, 774, 30.0, 4)) {
981 bl.major = 3;
982 bl.minor = 1;
983 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
984 2048, 1152, 30.0, 3)) {
985 bl.major = 4;
986 bl.minor = 0;
987 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
988 2048, 1152, 60.0, 3)) {
989 bl.major = 4;
990 bl.minor = 1;
991 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
992 4096, 2176, 30.0, 2)) {
993 bl.major = 5;
994 bl.minor = 0;
995 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
996 4096, 2176, 60.0, 2)) {
997 bl.major = 5;
998 bl.minor = 1;
999 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1000 4096, 2176, 120.0, 2)) {
1001 bl.major = 5;
1002 bl.minor = 2;
1003 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1004 8192, 4352, 30.0, 2)) {
1005 bl.major = 6;
1006 bl.minor = 0;
1007 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1008 8192, 4352, 60.0, 2)) {
1009 bl.major = 6;
1010 bl.minor = 1;
1011 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1012 8192, 4352, 120.0, 2)) {
1013 bl.major = 6;
1014 bl.minor = 2;
1015 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1016 16384, 8704, 30.0, 2)) {
1017 bl.major = 7;
1018 bl.minor = 0;
1019 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1020 16384, 8704, 60.0, 2)) {
1021 bl.major = 7;
1022 bl.minor = 1;
1023 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1024 16384, 8704, 120.0, 2)) {
1025 bl.major = 7;
1026 bl.minor = 2;
1027 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001028 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001029 seq->level[i] = bl;
Andrey Norkinf481d982018-05-15 12:05:31 -07001030 seq->tier[i] = 0; // setting main tier by default
Andrey Norkin26495512018-06-20 17:13:11 -07001031 // Set the maximum parameters for bitrate and buffer size for this profile,
1032 // level, and tier
1033 cm->op_params[i].bitrate = max_level_bitrate(
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001034 cm->seq_params.profile, major_minor_to_seq_level_idx(seq->level[i]),
1035 seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001036 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1037 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001038 if (cm->op_params[i].bitrate == 0)
1039 aom_internal_error(
1040 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1041 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001042 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001043 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001044 }
1045}
1046
Andrey Norkin26495512018-06-20 17:13:11 -07001047static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
1048 const AV1EncoderConfig *oxcf) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001049 seq->still_picture = (oxcf->limit == 1);
1050 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001051 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001052 seq->force_screen_content_tools = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001053 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001054 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001055 seq->frame_id_numbers_present_flag =
1056 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
1057 !oxcf->large_scale_tile && oxcf->error_resilient_mode;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001058 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001059 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001060 seq->force_screen_content_tools = 2;
1061 seq->force_integer_mv = 2;
1062 }
David Turnerebf96f42018-11-14 16:57:57 +00001063 seq->order_hint_info.order_hint_bits_minus_1 =
1064 seq->order_hint_info.enable_order_hint
1065 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1066 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001067
David Turner760a2f42018-12-07 15:25:36 +00001068 seq->max_frame_width =
1069 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1070 seq->max_frame_height = oxcf->forced_max_frame_height
1071 ? oxcf->forced_max_frame_height
1072 : oxcf->height;
1073 seq->num_bits_width =
1074 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1075 seq->num_bits_height =
1076 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1077 assert(seq->num_bits_width <= 16);
1078 assert(seq->num_bits_height <= 16);
1079
1080 seq->frame_id_length = FRAME_ID_LENGTH;
1081 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1082
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001083 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001084 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1085 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001086 seq->order_hint_info.enable_order_hint;
1087 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1088 seq->order_hint_info.enable_ref_frame_mvs &=
1089 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001090 seq->enable_superres = oxcf->enable_superres;
1091 seq->enable_cdef = oxcf->enable_cdef;
1092 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001093 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001094 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1095 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001096 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001097 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001098
Andrey Norkin26495512018-06-20 17:13:11 -07001099 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001100
1101 if (seq->operating_points_cnt_minus_1 == 0) {
1102 seq->operating_point_idc[0] = 0;
1103 } else {
1104 // Set operating_point_idc[] such that for the i-th operating point the
1105 // first (operating_points_cnt-i) spatial layers and the first temporal
1106 // layer are decoded Note that highest quality operating point should come
1107 // first
1108 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1109 seq->operating_point_idc[i] =
1110 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1111 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001112}
1113
Yaowu Xuf883b422016-08-30 14:01:10 -07001114static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1115 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001116
1117 cpi->oxcf = *oxcf;
1118 cpi->framerate = oxcf->init_framerate;
1119
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001120 cm->seq_params.profile = oxcf->profile;
1121 cm->seq_params.bit_depth = oxcf->bit_depth;
1122 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1123 cm->seq_params.color_primaries = oxcf->color_primaries;
1124 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1125 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001126 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001127 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1128 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001129 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001130 cm->timing_info.num_units_in_display_tick =
1131 oxcf->timing_info.num_units_in_display_tick;
1132 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1133 cm->timing_info.equal_picture_interval =
1134 oxcf->timing_info.equal_picture_interval;
1135 cm->timing_info.num_ticks_per_picture =
1136 oxcf->timing_info.num_ticks_per_picture;
1137
Andrey Norkin26495512018-06-20 17:13:11 -07001138 cm->seq_params.display_model_info_present_flag =
1139 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001140 cm->seq_params.decoder_model_info_present_flag =
1141 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001142 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001143 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001144 cm->buffer_model.num_units_in_decoding_tick =
1145 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001146 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08001147 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07001148 set_dec_model_op_parameters(&cm->op_params[0]);
1149 } else if (cm->timing_info_present &&
1150 cm->timing_info.equal_picture_interval &&
1151 !cm->seq_params.decoder_model_info_present_flag) {
1152 // set the decoder model parameters in resource availability mode
1153 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001154 } else {
1155 cm->op_params[0].initial_display_delay =
1156 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001157 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001158
Tom Fineganf8d6a162018-08-21 10:47:55 -07001159 if (cm->seq_params.monochrome) {
1160 cm->seq_params.subsampling_x = 1;
1161 cm->seq_params.subsampling_y = 1;
1162 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1163 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1164 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1165 cm->seq_params.subsampling_x = 0;
1166 cm->seq_params.subsampling_y = 0;
1167 } else {
1168 if (cm->seq_params.profile == 0) {
1169 cm->seq_params.subsampling_x = 1;
1170 cm->seq_params.subsampling_y = 1;
1171 } else if (cm->seq_params.profile == 1) {
1172 cm->seq_params.subsampling_x = 0;
1173 cm->seq_params.subsampling_y = 0;
1174 } else {
1175 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1176 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1177 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1178 } else {
1179 cm->seq_params.subsampling_x = 1;
1180 cm->seq_params.subsampling_y = 0;
1181 }
1182 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001183 }
1184
Yaowu Xuc27fc142016-08-22 16:08:15 -07001185 cm->width = oxcf->width;
1186 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001187 set_sb_size(&cm->seq_params,
1188 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001189 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001190
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001191 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001192
1193 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001194 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001195
1196 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001197 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001198
1199 cpi->static_mb_pct = 0;
1200 cpi->ref_frame_flags = 0;
1201
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001202 // Reset resize pending flags
1203 cpi->resize_pending_width = 0;
1204 cpi->resize_pending_height = 0;
1205
Yaowu Xuc27fc142016-08-22 16:08:15 -07001206 init_buffer_indices(cpi);
1207}
1208
1209static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001210 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001211 const int64_t bandwidth = oxcf->target_bandwidth;
1212 const int64_t starting = oxcf->starting_buffer_level_ms;
1213 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1214 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1215
1216 rc->starting_buffer_level = starting * bandwidth / 1000;
1217 rc->optimal_buffer_level =
1218 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1219 rc->maximum_buffer_size =
1220 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1221}
1222
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001223#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1224 cpi->fn_ptr[BT].sdf = SDF; \
1225 cpi->fn_ptr[BT].sdaf = SDAF; \
1226 cpi->fn_ptr[BT].vf = VF; \
1227 cpi->fn_ptr[BT].svf = SVF; \
1228 cpi->fn_ptr[BT].svaf = SVAF; \
1229 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1230 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001231 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001232
1233#define MAKE_BFP_SAD_WRAPPER(fnname) \
1234 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1235 int source_stride, \
1236 const uint8_t *ref_ptr, int ref_stride) { \
1237 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1238 } \
1239 static unsigned int fnname##_bits10( \
1240 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1241 int ref_stride) { \
1242 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1243 } \
1244 static unsigned int fnname##_bits12( \
1245 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1246 int ref_stride) { \
1247 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1248 }
1249
1250#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1251 static unsigned int fnname##_bits8( \
1252 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1253 int ref_stride, const uint8_t *second_pred) { \
1254 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1255 } \
1256 static unsigned int fnname##_bits10( \
1257 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1258 int ref_stride, const uint8_t *second_pred) { \
1259 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1260 2; \
1261 } \
1262 static unsigned int fnname##_bits12( \
1263 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1264 int ref_stride, const uint8_t *second_pred) { \
1265 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1266 4; \
1267 }
1268
Yaowu Xuc27fc142016-08-22 16:08:15 -07001269#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1270 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1271 const uint8_t *const ref_ptr[], int ref_stride, \
1272 unsigned int *sad_array) { \
1273 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1274 } \
1275 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1276 const uint8_t *const ref_ptr[], int ref_stride, \
1277 unsigned int *sad_array) { \
1278 int i; \
1279 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1280 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1281 } \
1282 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1283 const uint8_t *const ref_ptr[], int ref_stride, \
1284 unsigned int *sad_array) { \
1285 int i; \
1286 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1287 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1288 }
1289
Cheng Chenbf3d4962017-11-01 14:48:52 -07001290#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1291 static unsigned int fnname##_bits8( \
1292 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1293 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001294 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001295 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1296 jcp_param); \
1297 } \
1298 static unsigned int fnname##_bits10( \
1299 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1300 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001301 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001302 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1303 jcp_param) >> \
1304 2; \
1305 } \
1306 static unsigned int fnname##_bits12( \
1307 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1308 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001309 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001310 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1311 jcp_param) >> \
1312 4; \
1313 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001314
Yaowu Xuf883b422016-08-30 14:01:10 -07001315MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1316MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001317MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1318MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1319MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1320MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1321MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1322MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1323MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001324MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1325MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1326MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1327MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1328MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1329MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1330MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1331MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1332MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1333MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1334MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1335MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1336MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1337MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001338MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1339MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1340MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001341MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1342MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1343MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001344MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1345MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1346MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001347MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1348MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1349MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001350MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1351MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1352MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001353MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1354MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1355MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001356MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1357MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1358MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001359MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1360MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1361MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001362MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001363
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001364MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1365MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1366MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1367MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1368MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1369MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1370MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1371MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1372MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1373MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1374MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1375MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001376MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1377MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1378MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1379MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1380MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1381MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001382
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001383MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1384MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1385MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1386MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1387MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1388MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1389MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1390MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1391MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1392MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1393MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1394MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1395MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1396MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1397MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1398MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1399MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1400MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1401MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1402MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1403MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1404MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001405
David Barker0f3c94e2017-05-16 15:21:50 +01001406#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001407 cpi->fn_ptr[BT].msdf = MCSDF; \
1408 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001409
David Barkerc155e012017-05-11 13:54:54 +01001410#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1411 static unsigned int fnname##_bits8( \
1412 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1413 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1414 int m_stride, int invert_mask) { \
1415 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1416 second_pred_ptr, m, m_stride, invert_mask); \
1417 } \
1418 static unsigned int fnname##_bits10( \
1419 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1420 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1421 int m_stride, int invert_mask) { \
1422 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1423 second_pred_ptr, m, m_stride, invert_mask) >> \
1424 2; \
1425 } \
1426 static unsigned int fnname##_bits12( \
1427 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1428 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1429 int m_stride, int invert_mask) { \
1430 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1431 second_pred_ptr, m, m_stride, invert_mask) >> \
1432 4; \
1433 }
1434
David Barkerf19f35f2017-05-22 16:33:22 +01001435MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1436MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1437MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001438MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1439MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1440MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1441MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1442MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1443MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1444MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1445MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1446MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1447MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1448MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1449MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1450MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001451MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1452MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1453MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1454MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001455MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1456MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001457
Yaowu Xuc27fc142016-08-22 16:08:15 -07001458#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1459 cpi->fn_ptr[BT].osdf = OSDF; \
1460 cpi->fn_ptr[BT].ovf = OVF; \
1461 cpi->fn_ptr[BT].osvf = OSVF;
1462
1463#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1464 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1465 const int32_t *wsrc, \
1466 const int32_t *msk) { \
1467 return fnname(ref, ref_stride, wsrc, msk); \
1468 } \
1469 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1470 const int32_t *wsrc, \
1471 const int32_t *msk) { \
1472 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1473 } \
1474 static unsigned int fnname##_bits12(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) >> 4; \
1478 }
1479
Yaowu Xuf883b422016-08-30 14:01:10 -07001480MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1481MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1482MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001483MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1484MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1485MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1486MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1487MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1488MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1489MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1490MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1491MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1492MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1493MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1494MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1495MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001496MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1497MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1498MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1499MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001500MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1501MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001502
Yaowu Xuf883b422016-08-30 14:01:10 -07001503static void highbd_set_var_fns(AV1_COMP *const cpi) {
1504 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001505 if (cm->seq_params.use_highbitdepth) {
1506 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001507 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001508 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1509 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1510 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001511 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001512 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001513 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1514 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001515
1516 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1517 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1518 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001519 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001520 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001521 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1522 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001523
1524 HIGHBD_BFP(
1525 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1526 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001527 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001528 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1529 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001530
1531 HIGHBD_BFP(
1532 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1533 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001534 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001535 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1536 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001537
1538 HIGHBD_BFP(
1539 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1540 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001541 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001542 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1543 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001544
1545 HIGHBD_BFP(
1546 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1547 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001548 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001549 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1550 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001551
1552 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1553 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1554 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001555 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001556 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001557 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1558 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001559
1560 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1561 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1562 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001563 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001564 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001565 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1566 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001567
1568 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1569 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1570 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001571 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001572 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001573 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1574 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001575
1576 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1577 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1578 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001579 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001580 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001581 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1582 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001583
1584 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1585 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1586 aom_highbd_8_sub_pixel_variance32x32,
1587 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001588 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001589 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1590 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001591
1592 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1593 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1594 aom_highbd_8_sub_pixel_variance64x64,
1595 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001596 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001597 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1598 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001599
1600 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1601 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1602 aom_highbd_8_sub_pixel_variance16x16,
1603 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001604 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001605 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1606 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001607
1608 HIGHBD_BFP(
1609 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1610 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001611 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001612 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1613 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001614
1615 HIGHBD_BFP(
1616 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1617 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001618 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001619 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1620 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001621
Cheng Chenbf3d4962017-11-01 14:48:52 -07001622 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001623 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1624 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1625 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1626 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1627 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1628
1629 HIGHBD_BFP(
1630 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1631 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1632 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1633 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1634 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1635
1636 HIGHBD_BFP(
1637 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1638 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1639 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1640 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1641 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1642
1643 HIGHBD_BFP(
1644 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1645 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1646 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1647 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1648 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1649
1650 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1651 aom_highbd_sad128x128_avg_bits8,
1652 aom_highbd_8_variance128x128,
1653 aom_highbd_8_sub_pixel_variance128x128,
1654 aom_highbd_8_sub_pixel_avg_variance128x128,
1655 aom_highbd_sad128x128x4d_bits8,
1656 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1657 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001658
1659 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1660 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1661 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001662 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001663 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001664 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1665 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001666
1667 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1668 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1669 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001670 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001671 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001672 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1673 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001674
David Barkerf19f35f2017-05-22 16:33:22 +01001675 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1676 aom_highbd_8_masked_sub_pixel_variance128x128)
1677 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1678 aom_highbd_8_masked_sub_pixel_variance128x64)
1679 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1680 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001681 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1682 aom_highbd_8_masked_sub_pixel_variance64x64)
1683 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1684 aom_highbd_8_masked_sub_pixel_variance64x32)
1685 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1686 aom_highbd_8_masked_sub_pixel_variance32x64)
1687 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1688 aom_highbd_8_masked_sub_pixel_variance32x32)
1689 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1690 aom_highbd_8_masked_sub_pixel_variance32x16)
1691 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1692 aom_highbd_8_masked_sub_pixel_variance16x32)
1693 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1694 aom_highbd_8_masked_sub_pixel_variance16x16)
1695 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1696 aom_highbd_8_masked_sub_pixel_variance8x16)
1697 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1698 aom_highbd_8_masked_sub_pixel_variance16x8)
1699 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1700 aom_highbd_8_masked_sub_pixel_variance8x8)
1701 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1702 aom_highbd_8_masked_sub_pixel_variance4x8)
1703 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1704 aom_highbd_8_masked_sub_pixel_variance8x4)
1705 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1706 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001707 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1708 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001709 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1710 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001711 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1712 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001713 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1714 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001715 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1716 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001717 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1718 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001719 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1720 aom_highbd_obmc_variance128x128,
1721 aom_highbd_obmc_sub_pixel_variance128x128)
1722 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1723 aom_highbd_obmc_variance128x64,
1724 aom_highbd_obmc_sub_pixel_variance128x64)
1725 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1726 aom_highbd_obmc_variance64x128,
1727 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001728 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1729 aom_highbd_obmc_variance64x64,
1730 aom_highbd_obmc_sub_pixel_variance64x64)
1731 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1732 aom_highbd_obmc_variance64x32,
1733 aom_highbd_obmc_sub_pixel_variance64x32)
1734 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1735 aom_highbd_obmc_variance32x64,
1736 aom_highbd_obmc_sub_pixel_variance32x64)
1737 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1738 aom_highbd_obmc_variance32x32,
1739 aom_highbd_obmc_sub_pixel_variance32x32)
1740 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1741 aom_highbd_obmc_variance32x16,
1742 aom_highbd_obmc_sub_pixel_variance32x16)
1743 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1744 aom_highbd_obmc_variance16x32,
1745 aom_highbd_obmc_sub_pixel_variance16x32)
1746 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1747 aom_highbd_obmc_variance16x16,
1748 aom_highbd_obmc_sub_pixel_variance16x16)
1749 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1750 aom_highbd_obmc_variance8x16,
1751 aom_highbd_obmc_sub_pixel_variance8x16)
1752 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1753 aom_highbd_obmc_variance16x8,
1754 aom_highbd_obmc_sub_pixel_variance16x8)
1755 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1756 aom_highbd_obmc_variance8x8,
1757 aom_highbd_obmc_sub_pixel_variance8x8)
1758 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1759 aom_highbd_obmc_variance4x8,
1760 aom_highbd_obmc_sub_pixel_variance4x8)
1761 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1762 aom_highbd_obmc_variance8x4,
1763 aom_highbd_obmc_sub_pixel_variance8x4)
1764 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1765 aom_highbd_obmc_variance4x4,
1766 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001767 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1768 aom_highbd_obmc_variance64x16,
1769 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001770 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1771 aom_highbd_obmc_variance16x64,
1772 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001773 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1774 aom_highbd_obmc_variance32x8,
1775 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001776 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1777 aom_highbd_obmc_variance8x32,
1778 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001779 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1780 aom_highbd_obmc_variance16x4,
1781 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001782 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1783 aom_highbd_obmc_variance4x16,
1784 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001785 break;
1786
Yaowu Xuf883b422016-08-30 14:01:10 -07001787 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001788 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1789 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1790 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001791 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001792 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001793 aom_highbd_dist_wtd_sad64x16_avg_bits10,
1794 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001795
1796 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1797 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1798 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001799 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001800 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001801 aom_highbd_dist_wtd_sad16x64_avg_bits10,
1802 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001803
1804 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1805 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1806 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001807 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001808 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001809 aom_highbd_dist_wtd_sad32x8_avg_bits10,
1810 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001811
1812 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1813 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1814 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001815 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001816 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001817 aom_highbd_dist_wtd_sad8x32_avg_bits10,
1818 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001819
1820 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1821 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1822 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001823 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001824 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001825 aom_highbd_dist_wtd_sad16x4_avg_bits10,
1826 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001827
1828 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1829 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1830 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001831 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001832 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001833 aom_highbd_dist_wtd_sad4x16_avg_bits10,
1834 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001835
1836 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1837 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1838 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001839 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001840 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001841 aom_highbd_dist_wtd_sad32x16_avg_bits10,
1842 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001843
1844 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1845 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1846 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001847 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001848 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001849 aom_highbd_dist_wtd_sad16x32_avg_bits10,
1850 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001851
1852 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1853 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1854 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001855 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001856 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001857 aom_highbd_dist_wtd_sad64x32_avg_bits10,
1858 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001859
1860 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1861 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1862 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001863 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001864 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001865 aom_highbd_dist_wtd_sad32x64_avg_bits10,
1866 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001867
1868 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1869 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1870 aom_highbd_10_sub_pixel_variance32x32,
1871 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001872 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001873 aom_highbd_dist_wtd_sad32x32_avg_bits10,
1874 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001875
1876 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1877 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1878 aom_highbd_10_sub_pixel_variance64x64,
1879 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001880 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001881 aom_highbd_dist_wtd_sad64x64_avg_bits10,
1882 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001883
1884 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1885 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1886 aom_highbd_10_sub_pixel_variance16x16,
1887 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001888 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001889 aom_highbd_dist_wtd_sad16x16_avg_bits10,
1890 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001891
1892 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1893 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1894 aom_highbd_10_sub_pixel_variance16x8,
1895 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001896 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001897 aom_highbd_dist_wtd_sad16x8_avg_bits10,
1898 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001899
1900 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1901 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1902 aom_highbd_10_sub_pixel_variance8x16,
1903 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001904 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001905 aom_highbd_dist_wtd_sad8x16_avg_bits10,
1906 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001907
1908 HIGHBD_BFP(
1909 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1910 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001911 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001912 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
1913 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001914
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001915 HIGHBD_BFP(
1916 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1917 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1918 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001919 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
1920 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001921
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001922 HIGHBD_BFP(
1923 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1924 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1925 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001926 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
1927 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001928
1929 HIGHBD_BFP(
1930 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1931 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001932 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001933 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
1934 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001935
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001936 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1937 aom_highbd_sad128x128_avg_bits10,
1938 aom_highbd_10_variance128x128,
1939 aom_highbd_10_sub_pixel_variance128x128,
1940 aom_highbd_10_sub_pixel_avg_variance128x128,
1941 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001942 aom_highbd_dist_wtd_sad128x128_avg_bits10,
1943 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001944
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001945 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1946 aom_highbd_sad128x64_avg_bits10,
1947 aom_highbd_10_variance128x64,
1948 aom_highbd_10_sub_pixel_variance128x64,
1949 aom_highbd_10_sub_pixel_avg_variance128x64,
1950 aom_highbd_sad128x64x4d_bits10,
1951 aom_highbd_dist_wtd_sad128x64_avg_bits10,
1952 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001953
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001954 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1955 aom_highbd_sad64x128_avg_bits10,
1956 aom_highbd_10_variance64x128,
1957 aom_highbd_10_sub_pixel_variance64x128,
1958 aom_highbd_10_sub_pixel_avg_variance64x128,
1959 aom_highbd_sad64x128x4d_bits10,
1960 aom_highbd_dist_wtd_sad64x128_avg_bits10,
1961 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001962
David Barkerf19f35f2017-05-22 16:33:22 +01001963 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1964 aom_highbd_10_masked_sub_pixel_variance128x128)
1965 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1966 aom_highbd_10_masked_sub_pixel_variance128x64)
1967 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1968 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001969 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1970 aom_highbd_10_masked_sub_pixel_variance64x64)
1971 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1972 aom_highbd_10_masked_sub_pixel_variance64x32)
1973 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1974 aom_highbd_10_masked_sub_pixel_variance32x64)
1975 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1976 aom_highbd_10_masked_sub_pixel_variance32x32)
1977 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1978 aom_highbd_10_masked_sub_pixel_variance32x16)
1979 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1980 aom_highbd_10_masked_sub_pixel_variance16x32)
1981 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1982 aom_highbd_10_masked_sub_pixel_variance16x16)
1983 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1984 aom_highbd_10_masked_sub_pixel_variance8x16)
1985 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1986 aom_highbd_10_masked_sub_pixel_variance16x8)
1987 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1988 aom_highbd_10_masked_sub_pixel_variance8x8)
1989 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1990 aom_highbd_10_masked_sub_pixel_variance4x8)
1991 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1992 aom_highbd_10_masked_sub_pixel_variance8x4)
1993 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1994 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001995 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1996 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001997 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1998 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001999 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
2000 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002001 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
2002 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002003 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
2004 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002005 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2006 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002007 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2008 aom_highbd_10_obmc_variance128x128,
2009 aom_highbd_10_obmc_sub_pixel_variance128x128)
2010 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2011 aom_highbd_10_obmc_variance128x64,
2012 aom_highbd_10_obmc_sub_pixel_variance128x64)
2013 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2014 aom_highbd_10_obmc_variance64x128,
2015 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002016 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2017 aom_highbd_10_obmc_variance64x64,
2018 aom_highbd_10_obmc_sub_pixel_variance64x64)
2019 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2020 aom_highbd_10_obmc_variance64x32,
2021 aom_highbd_10_obmc_sub_pixel_variance64x32)
2022 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2023 aom_highbd_10_obmc_variance32x64,
2024 aom_highbd_10_obmc_sub_pixel_variance32x64)
2025 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2026 aom_highbd_10_obmc_variance32x32,
2027 aom_highbd_10_obmc_sub_pixel_variance32x32)
2028 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2029 aom_highbd_10_obmc_variance32x16,
2030 aom_highbd_10_obmc_sub_pixel_variance32x16)
2031 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2032 aom_highbd_10_obmc_variance16x32,
2033 aom_highbd_10_obmc_sub_pixel_variance16x32)
2034 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2035 aom_highbd_10_obmc_variance16x16,
2036 aom_highbd_10_obmc_sub_pixel_variance16x16)
2037 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2038 aom_highbd_10_obmc_variance8x16,
2039 aom_highbd_10_obmc_sub_pixel_variance8x16)
2040 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2041 aom_highbd_10_obmc_variance16x8,
2042 aom_highbd_10_obmc_sub_pixel_variance16x8)
2043 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2044 aom_highbd_10_obmc_variance8x8,
2045 aom_highbd_10_obmc_sub_pixel_variance8x8)
2046 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2047 aom_highbd_10_obmc_variance4x8,
2048 aom_highbd_10_obmc_sub_pixel_variance4x8)
2049 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2050 aom_highbd_10_obmc_variance8x4,
2051 aom_highbd_10_obmc_sub_pixel_variance8x4)
2052 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2053 aom_highbd_10_obmc_variance4x4,
2054 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002055
Rupert Swarbrick72678572017-08-02 12:05:26 +01002056 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2057 aom_highbd_10_obmc_variance64x16,
2058 aom_highbd_10_obmc_sub_pixel_variance64x16)
2059
2060 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2061 aom_highbd_10_obmc_variance16x64,
2062 aom_highbd_10_obmc_sub_pixel_variance16x64)
2063
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002064 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2065 aom_highbd_10_obmc_variance32x8,
2066 aom_highbd_10_obmc_sub_pixel_variance32x8)
2067
2068 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2069 aom_highbd_10_obmc_variance8x32,
2070 aom_highbd_10_obmc_sub_pixel_variance8x32)
2071
2072 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2073 aom_highbd_10_obmc_variance16x4,
2074 aom_highbd_10_obmc_sub_pixel_variance16x4)
2075
2076 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2077 aom_highbd_10_obmc_variance4x16,
2078 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002079 break;
2080
Yaowu Xuf883b422016-08-30 14:01:10 -07002081 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002082 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2083 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2084 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002085 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002086 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002087 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2088 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002089
2090 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2091 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2092 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002093 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002094 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002095 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2096 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002097
2098 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2099 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2100 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002101 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002102 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002103 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2104 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002105
2106 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2107 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2108 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002109 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002110 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002111 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2112 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002113
2114 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2115 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2116 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002117 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002118 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002119 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2120 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002121
2122 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2123 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2124 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002125 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002126 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002127 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2128 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002129
2130 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2131 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2132 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002133 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002134 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002135 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2136 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002137
2138 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2139 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2140 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002141 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002142 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002143 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2144 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002145
2146 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2147 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2148 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002149 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002150 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002151 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2152 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002153
2154 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2155 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2156 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002157 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002158 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002159 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2160 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002161
2162 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2163 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2164 aom_highbd_12_sub_pixel_variance32x32,
2165 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002166 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002167 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2168 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002169
2170 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2171 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2172 aom_highbd_12_sub_pixel_variance64x64,
2173 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002174 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002175 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2176 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002177
2178 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2179 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2180 aom_highbd_12_sub_pixel_variance16x16,
2181 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002182 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002183 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2184 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002185
2186 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2187 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2188 aom_highbd_12_sub_pixel_variance16x8,
2189 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002190 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002191 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2192 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002193
2194 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2195 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2196 aom_highbd_12_sub_pixel_variance8x16,
2197 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002198 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002199 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2200 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002201
2202 HIGHBD_BFP(
2203 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2204 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002205 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002206 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2207 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002208
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002209 HIGHBD_BFP(
2210 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2211 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2212 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002213 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2214 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002215
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002216 HIGHBD_BFP(
2217 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2218 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2219 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002220 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2221 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002222
2223 HIGHBD_BFP(
2224 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2225 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002226 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002227 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2228 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002229
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002230 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2231 aom_highbd_sad128x128_avg_bits12,
2232 aom_highbd_12_variance128x128,
2233 aom_highbd_12_sub_pixel_variance128x128,
2234 aom_highbd_12_sub_pixel_avg_variance128x128,
2235 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002236 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2237 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002238
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002239 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2240 aom_highbd_sad128x64_avg_bits12,
2241 aom_highbd_12_variance128x64,
2242 aom_highbd_12_sub_pixel_variance128x64,
2243 aom_highbd_12_sub_pixel_avg_variance128x64,
2244 aom_highbd_sad128x64x4d_bits12,
2245 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2246 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002247
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002248 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2249 aom_highbd_sad64x128_avg_bits12,
2250 aom_highbd_12_variance64x128,
2251 aom_highbd_12_sub_pixel_variance64x128,
2252 aom_highbd_12_sub_pixel_avg_variance64x128,
2253 aom_highbd_sad64x128x4d_bits12,
2254 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2255 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002256
David Barkerf19f35f2017-05-22 16:33:22 +01002257 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2258 aom_highbd_12_masked_sub_pixel_variance128x128)
2259 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2260 aom_highbd_12_masked_sub_pixel_variance128x64)
2261 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2262 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002263 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2264 aom_highbd_12_masked_sub_pixel_variance64x64)
2265 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2266 aom_highbd_12_masked_sub_pixel_variance64x32)
2267 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2268 aom_highbd_12_masked_sub_pixel_variance32x64)
2269 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2270 aom_highbd_12_masked_sub_pixel_variance32x32)
2271 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2272 aom_highbd_12_masked_sub_pixel_variance32x16)
2273 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2274 aom_highbd_12_masked_sub_pixel_variance16x32)
2275 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2276 aom_highbd_12_masked_sub_pixel_variance16x16)
2277 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2278 aom_highbd_12_masked_sub_pixel_variance8x16)
2279 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2280 aom_highbd_12_masked_sub_pixel_variance16x8)
2281 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2282 aom_highbd_12_masked_sub_pixel_variance8x8)
2283 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2284 aom_highbd_12_masked_sub_pixel_variance4x8)
2285 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2286 aom_highbd_12_masked_sub_pixel_variance8x4)
2287 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2288 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002289 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2290 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002291 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2292 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002293 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2294 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002295 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2296 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002297 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2298 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002299 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2300 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002301 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2302 aom_highbd_12_obmc_variance128x128,
2303 aom_highbd_12_obmc_sub_pixel_variance128x128)
2304 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2305 aom_highbd_12_obmc_variance128x64,
2306 aom_highbd_12_obmc_sub_pixel_variance128x64)
2307 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2308 aom_highbd_12_obmc_variance64x128,
2309 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002310 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2311 aom_highbd_12_obmc_variance64x64,
2312 aom_highbd_12_obmc_sub_pixel_variance64x64)
2313 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2314 aom_highbd_12_obmc_variance64x32,
2315 aom_highbd_12_obmc_sub_pixel_variance64x32)
2316 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2317 aom_highbd_12_obmc_variance32x64,
2318 aom_highbd_12_obmc_sub_pixel_variance32x64)
2319 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2320 aom_highbd_12_obmc_variance32x32,
2321 aom_highbd_12_obmc_sub_pixel_variance32x32)
2322 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2323 aom_highbd_12_obmc_variance32x16,
2324 aom_highbd_12_obmc_sub_pixel_variance32x16)
2325 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2326 aom_highbd_12_obmc_variance16x32,
2327 aom_highbd_12_obmc_sub_pixel_variance16x32)
2328 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2329 aom_highbd_12_obmc_variance16x16,
2330 aom_highbd_12_obmc_sub_pixel_variance16x16)
2331 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2332 aom_highbd_12_obmc_variance8x16,
2333 aom_highbd_12_obmc_sub_pixel_variance8x16)
2334 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2335 aom_highbd_12_obmc_variance16x8,
2336 aom_highbd_12_obmc_sub_pixel_variance16x8)
2337 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2338 aom_highbd_12_obmc_variance8x8,
2339 aom_highbd_12_obmc_sub_pixel_variance8x8)
2340 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2341 aom_highbd_12_obmc_variance4x8,
2342 aom_highbd_12_obmc_sub_pixel_variance4x8)
2343 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2344 aom_highbd_12_obmc_variance8x4,
2345 aom_highbd_12_obmc_sub_pixel_variance8x4)
2346 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2347 aom_highbd_12_obmc_variance4x4,
2348 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002349 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2350 aom_highbd_12_obmc_variance64x16,
2351 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002352 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2353 aom_highbd_12_obmc_variance16x64,
2354 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002355 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2356 aom_highbd_12_obmc_variance32x8,
2357 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002358 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2359 aom_highbd_12_obmc_variance8x32,
2360 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002361 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2362 aom_highbd_12_obmc_variance16x4,
2363 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002364 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2365 aom_highbd_12_obmc_variance4x16,
2366 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002367 break;
2368
2369 default:
2370 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002371 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002372 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002373 }
2374 }
2375}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002376
Yaowu Xuf883b422016-08-30 14:01:10 -07002377static void realloc_segmentation_maps(AV1_COMP *cpi) {
2378 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002379
2380 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002381 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002382 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002383 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002384
2385 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002386 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002387 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002388 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002389
2390 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002391 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002392 CHECK_MEM_ERROR(cm, cpi->active_map.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
Yaowu Xuf883b422016-08-30 14:01:10 -07002396void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2397 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002398 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002399 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002400 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002401 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002402
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002403 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2404 seq_params->bit_depth = oxcf->bit_depth;
2405 seq_params->color_primaries = oxcf->color_primaries;
2406 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2407 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2408 seq_params->monochrome = oxcf->monochrome;
2409 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2410 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002411
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002412 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2413 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002414
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002415 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002416 cm->timing_info.num_units_in_display_tick =
2417 oxcf->timing_info.num_units_in_display_tick;
2418 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2419 cm->timing_info.equal_picture_interval =
2420 oxcf->timing_info.equal_picture_interval;
2421 cm->timing_info.num_ticks_per_picture =
2422 oxcf->timing_info.num_ticks_per_picture;
2423
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002424 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002425 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002426 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002427 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002428 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002429 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002430 cm->buffer_model.num_units_in_decoding_tick =
2431 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002432 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08002433 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07002434 set_dec_model_op_parameters(&cm->op_params[0]);
2435 } else if (cm->timing_info_present &&
2436 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002437 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002438 // set the decoder model parameters in resource availability mode
2439 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002440 } else {
2441 cm->op_params[0].initial_display_delay =
2442 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002443 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002444
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002445 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002446
Yaowu Xuc27fc142016-08-22 16:08:15 -07002447 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002448 cpi->common.options = oxcf->cfg;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002449 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002450 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002451
Yaowu Xuf883b422016-08-30 14:01:10 -07002452 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002453 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2454 } else {
2455 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2456 }
2457
2458 cpi->refresh_last_frame = 1;
2459 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002460 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002461 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002462
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002463 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2464 ? REFRESH_FRAME_CONTEXT_DISABLED
2465 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002466 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002467 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002468
Alex Converse74ad0912017-07-18 10:22:58 -07002469 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002470 CHECK_MEM_ERROR(cm, x->palette_buffer,
2471 aom_memalign(16, sizeof(*x->palette_buffer)));
2472 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002473
2474 if (x->tmp_conv_dst == NULL) {
2475 CHECK_MEM_ERROR(
2476 cm, x->tmp_conv_dst,
2477 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002478 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002479 }
2480 for (int i = 0; i < 2; ++i) {
2481 if (x->tmp_obmc_bufs[i] == NULL) {
2482 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002483 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002484 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002485 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002486 }
2487 }
2488
Yaowu Xuf883b422016-08-30 14:01:10 -07002489 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002490 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002491
Yaowu Xuc27fc142016-08-22 16:08:15 -07002492 set_rc_buffer_sizes(rc, &cpi->oxcf);
2493
2494 // Under a configuration change, where maximum_buffer_size may change,
2495 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002496 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2497 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002498
2499 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002500 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002501
2502 // Set absolute upper and lower quality limits
2503 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2504 rc->best_quality = cpi->oxcf.best_allowed_q;
2505
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002506 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002507 cm->switchable_motion_mode = 1;
2508
Yaowu Xuc27fc142016-08-22 16:08:15 -07002509 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2510 cm->render_width = cpi->oxcf.render_width;
2511 cm->render_height = cpi->oxcf.render_height;
2512 } else {
2513 cm->render_width = cpi->oxcf.width;
2514 cm->render_height = cpi->oxcf.height;
2515 }
2516 cm->width = cpi->oxcf.width;
2517 cm->height = cpi->oxcf.height;
2518
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002519 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002520 // Superblock size should not be updated after the first key frame.
2521 if (!cpi->seq_params_locked) {
2522 set_sb_size(&cm->seq_params, select_sb_size(cpi));
2523 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002524
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002525 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002526 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002527 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002528 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002529 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002530 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002531 realloc_segmentation_maps(cpi);
2532 cpi->initial_width = cpi->initial_height = 0;
2533 }
2534 }
2535 update_frame_size(cpi);
2536
2537 cpi->alt_ref_source = NULL;
2538 rc->is_src_frame_alt_ref = 0;
2539
Yaowu Xuc27fc142016-08-22 16:08:15 -07002540 rc->is_bwd_ref_frame = 0;
2541 rc->is_last_bipred_frame = 0;
2542 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002543
Yaowu Xuc27fc142016-08-22 16:08:15 -07002544 set_tile_info(cpi);
2545
2546 cpi->ext_refresh_frame_flags_pending = 0;
2547 cpi->ext_refresh_frame_context_pending = 0;
2548
Yaowu Xuc27fc142016-08-22 16:08:15 -07002549 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002550
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002551 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002552 // This should not be called after the first key frame.
2553 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002554 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002555 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Andrey Norkin26495512018-06-20 17:13:11 -07002556 init_seq_coding_tools(&cm->seq_params, cm, oxcf);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002557 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002558}
2559
Yaowu Xuf883b422016-08-30 14:01:10 -07002560AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2561 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002562 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002563 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2564 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002565
2566 if (!cm) return NULL;
2567
Yaowu Xuf883b422016-08-30 14:01:10 -07002568 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002569
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002570 // The jmp_buf is valid only for the duration of the function that calls
2571 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2572 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002573 if (setjmp(cm->error.jmp)) {
2574 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002575 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002576 return 0;
2577 }
2578
2579 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002580 cm->alloc_mi = enc_alloc_mi;
2581 cm->free_mi = enc_free_mi;
2582 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002583
Angie Chianga5d96c42016-10-21 16:16:56 -07002584 CHECK_MEM_ERROR(cm, cm->fc,
2585 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002586 CHECK_MEM_ERROR(
2587 cm, cm->default_frame_context,
2588 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002589 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002590 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002591
2592 cpi->resize_state = 0;
2593 cpi->resize_avg_qp = 0;
2594 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002595
Yaowu Xuc27fc142016-08-22 16:08:15 -07002596 cpi->common.buffer_pool = pool;
2597
2598 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002599 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002600
David Turnerd2a592e2018-11-16 14:59:31 +00002601 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002602 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002603 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002604 cpi->partition_search_skippable_frame = 0;
2605 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00002606 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002607 realloc_segmentation_maps(cpi);
2608
Jingning Hanf050fc12018-03-09 14:53:33 -08002609 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2610 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002611
Yaowu Xuc27fc142016-08-22 16:08:15 -07002612 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2613 i++) {
2614 CHECK_MEM_ERROR(
2615 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002616 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002617 }
2618
2619#if CONFIG_FP_MB_STATS
2620 cpi->use_fp_mb_stats = 0;
2621 if (cpi->use_fp_mb_stats) {
2622 // a place holder used to store the first pass mb stats in the first pass
2623 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002624 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002625 } else {
2626 cpi->twopass.frame_mb_stats_buf = NULL;
2627 }
2628#endif
2629
2630 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002631
2632 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2633#if CONFIG_INTERNAL_STATS
2634 cpi->b_calculate_blockiness = 1;
2635 cpi->b_calculate_consistency = 1;
2636 cpi->total_inconsistency = 0;
2637 cpi->psnr.worst = 100.0;
2638 cpi->worst_ssim = 100.0;
2639
2640 cpi->count = 0;
2641 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08002642#if CONFIG_SPEED_STATS
2643 cpi->tx_search_count = 0;
2644#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002645
2646 if (cpi->b_calculate_psnr) {
2647 cpi->total_sq_error = 0;
2648 cpi->total_samples = 0;
2649 cpi->tot_recode_hits = 0;
2650 cpi->summed_quality = 0;
2651 cpi->summed_weights = 0;
2652 }
2653
2654 cpi->fastssim.worst = 100.0;
2655 cpi->psnrhvs.worst = 100.0;
2656
2657 if (cpi->b_calculate_blockiness) {
2658 cpi->total_blockiness = 0;
2659 cpi->worst_blockiness = 0.0;
2660 }
2661
2662 if (cpi->b_calculate_consistency) {
2663 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002664 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002665 cpi->common.mi_rows * cpi->common.mi_cols));
2666 cpi->worst_consistency = 100.0;
2667 }
2668#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002669#if CONFIG_ENTROPY_STATS
2670 av1_zero(aggregate_fc);
2671#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002672
2673 cpi->first_time_stamp_ever = INT64_MAX;
2674
Jingning Hanf050fc12018-03-09 14:53:33 -08002675 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2676 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2677 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2678 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002679
Yaowu Xuc27fc142016-08-22 16:08:15 -07002680#ifdef OUTPUT_YUV_SKINMAP
2681 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2682#endif
2683#ifdef OUTPUT_YUV_REC
2684 yuv_rec_file = fopen("rec.yuv", "wb");
2685#endif
2686
Yaowu Xuc27fc142016-08-22 16:08:15 -07002687 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002688 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002689 } else if (oxcf->pass == 2) {
2690 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2691 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2692
2693#if CONFIG_FP_MB_STATS
2694 if (cpi->use_fp_mb_stats) {
2695 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2696 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2697
2698 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2699 oxcf->firstpass_mb_stats_in.buf;
2700 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2701 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2702 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2703 }
2704#endif
2705
2706 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2707 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2708 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2709
Yaowu Xuf883b422016-08-30 14:01:10 -07002710 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002711 }
2712
Jingning Hand064cf02017-06-01 10:00:39 -07002713 CHECK_MEM_ERROR(
2714 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002715 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002716 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002717 CHECK_MEM_ERROR(
2718 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002719 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002720 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002721
2722 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2723 (int32_t *)aom_memalign(
2724 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2725
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302726 CHECK_MEM_ERROR(
2727 cm, cpi->td.mb.inter_modes_info,
2728 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302729
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302730 for (int x = 0; x < 2; x++)
2731 for (int y = 0; y < 2; y++)
2732 CHECK_MEM_ERROR(
2733 cm, cpi->td.mb.hash_value_buffer[x][y],
2734 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2735 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2736
2737 cpi->td.mb.g_crc_initialized = 0;
2738
Jingning Hand064cf02017-06-01 10:00:39 -07002739 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2740 (int32_t *)aom_memalign(
2741 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2742
David Turner04b70d82019-01-24 15:39:19 +00002743 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
2744 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002745
Yue Chen7cae98f2018-08-24 10:43:16 -07002746 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
2747 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2748 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2749
2750 CHECK_MEM_ERROR(cm, cpi->tpl_stats[frame].tpl_stats_ptr,
2751 aom_calloc(mi_rows * mi_cols,
2752 sizeof(*cpi->tpl_stats[frame].tpl_stats_ptr)));
2753 cpi->tpl_stats[frame].is_valid = 0;
2754 cpi->tpl_stats[frame].width = mi_cols;
2755 cpi->tpl_stats[frame].height = mi_rows;
2756 cpi->tpl_stats[frame].stride = mi_cols;
2757 cpi->tpl_stats[frame].mi_rows = cm->mi_rows;
2758 cpi->tpl_stats[frame].mi_cols = cm->mi_cols;
2759 }
2760
chiyotsai92ed0dd2019-01-25 14:50:14 -08002761#if CONFIG_COLLECT_PARTITION_STATS
2762 av1_zero(cpi->partition_stats);
2763#endif
2764
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002765#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2766 cpi->fn_ptr[BT].sdf = SDF; \
2767 cpi->fn_ptr[BT].sdaf = SDAF; \
2768 cpi->fn_ptr[BT].vf = VF; \
2769 cpi->fn_ptr[BT].svf = SVF; \
2770 cpi->fn_ptr[BT].svaf = SVAF; \
2771 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2772 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002773 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002774
Cheng Chenf78632e2017-10-20 15:30:51 -07002775 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002776 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002777 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
2778 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002779
2780 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002781 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002782 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
2783 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002784
2785 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002786 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002787 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
2788 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002789
2790 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002791 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002792 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
2793 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002794
2795 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002796 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002797 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
2798 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002799
2800 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002801 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002802 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
2803 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002804
Cheng Chenf78632e2017-10-20 15:30:51 -07002805 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2806 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002807 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
2808 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002809
2810 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002811 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002812 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
2813 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002814
2815 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002816 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002817 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
2818 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002819
2820 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002821 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002822 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
2823 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002824
2825 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002826 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002827 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
2828 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002829
2830 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002831 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002832 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
2833 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002834
2835 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002836 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002837 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
2838 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002839
2840 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2841 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002842 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
2843 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002844
2845 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2846 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002847 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
2848 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002849
2850 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2851 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002852 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
2853 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002854
2855 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002856 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002857 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
2858 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002859
2860 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002861 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002862 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
2863 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002864
2865 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002866 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002867 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002868
2869 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002870 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002871 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002872
2873 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002874 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002875 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002876
2877 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002878 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002879 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002880
Yaowu Xuc27fc142016-08-22 16:08:15 -07002881#define OBFP(BT, OSDF, OVF, OSVF) \
2882 cpi->fn_ptr[BT].osdf = OSDF; \
2883 cpi->fn_ptr[BT].ovf = OVF; \
2884 cpi->fn_ptr[BT].osvf = OSVF;
2885
Yaowu Xuf883b422016-08-30 14:01:10 -07002886 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2887 aom_obmc_sub_pixel_variance128x128)
2888 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2889 aom_obmc_sub_pixel_variance128x64)
2890 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2891 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002892 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2893 aom_obmc_sub_pixel_variance64x64)
2894 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2895 aom_obmc_sub_pixel_variance64x32)
2896 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2897 aom_obmc_sub_pixel_variance32x64)
2898 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2899 aom_obmc_sub_pixel_variance32x32)
2900 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2901 aom_obmc_sub_pixel_variance32x16)
2902 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2903 aom_obmc_sub_pixel_variance16x32)
2904 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2905 aom_obmc_sub_pixel_variance16x16)
2906 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2907 aom_obmc_sub_pixel_variance16x8)
2908 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2909 aom_obmc_sub_pixel_variance8x16)
2910 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2911 aom_obmc_sub_pixel_variance8x8)
2912 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2913 aom_obmc_sub_pixel_variance4x8)
2914 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2915 aom_obmc_sub_pixel_variance8x4)
2916 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2917 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002918 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2919 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002920 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2921 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002922 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2923 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002924 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2925 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002926 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2927 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002928 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2929 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002930
David Barkerf19f35f2017-05-22 16:33:22 +01002931#define MBFP(BT, MCSDF, MCSVF) \
2932 cpi->fn_ptr[BT].msdf = MCSDF; \
2933 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002934
David Barkerf19f35f2017-05-22 16:33:22 +01002935 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2936 aom_masked_sub_pixel_variance128x128)
2937 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2938 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002939 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2940 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2941 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2942 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2943 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2944 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2945 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2946 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2947 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2948 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2949 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2950 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2951 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002952
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002953 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2954
2955 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2956
2957 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2958
2959 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002960
2961 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2962
2963 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002964
Yaowu Xuc27fc142016-08-22 16:08:15 -07002965 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002966
Yaowu Xuf883b422016-08-30 14:01:10 -07002967 /* av1_init_quantizer() is first called here. Add check in
2968 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002969 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002970 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002971 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002972 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08002973 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002974
Yaowu Xuf883b422016-08-30 14:01:10 -07002975 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07002976 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002977 cm->superres_upscaled_width = oxcf->width;
2978 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002979 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002980
2981 cm->error.setjmp = 0;
2982
2983 return cpi;
2984}
2985
Urvang Joshiee2c8112018-05-04 14:53:15 -07002986#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002987#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2988
2989#define SNPRINT2(H, T, V) \
2990 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07002991#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002992
Yaowu Xuf883b422016-08-30 14:01:10 -07002993void av1_remove_compressor(AV1_COMP *cpi) {
2994 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002995 unsigned int i;
2996 int t;
2997
2998 if (!cpi) return;
2999
3000 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003001 const int num_planes = av1_num_planes(cm);
3002
David Turnerd2a592e2018-11-16 14:59:31 +00003003 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08003004#if CONFIG_ENTROPY_STATS
3005 if (cpi->oxcf.pass != 1) {
3006 fprintf(stderr, "Writing counts.stt\n");
3007 FILE *f = fopen("counts.stt", "wb");
3008 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3009 fclose(f);
3010 }
3011#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003012#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003013 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003014
3015 if (cpi->oxcf.pass != 1) {
3016 char headings[512] = { 0 };
3017 char results[512] = { 0 };
3018 FILE *f = fopen("opsnr.stt", "a");
3019 double time_encoded =
3020 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3021 10000000.000;
3022 double total_encode_time =
3023 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3024 const double dr =
3025 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3026 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3027 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3028 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3029
3030 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003031 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003032 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3033 const double total_ssim =
3034 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3035 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003036 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003037 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003038 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08003039 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003040 snprintf(results, sizeof(results),
3041 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3042 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003043 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08003044 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003045 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3046 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3047 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3048 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003049 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003050 cpi->psnr.stat[STAT_Y] / cpi->count,
3051 cpi->psnr.stat[STAT_U] / cpi->count,
3052 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003053
3054 if (cpi->b_calculate_blockiness) {
3055 SNPRINT(headings, "\t Block\tWstBlck");
3056 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3057 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3058 }
3059
3060 if (cpi->b_calculate_consistency) {
3061 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003062 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003063 (double)cpi->total_inconsistency);
3064
3065 SNPRINT(headings, "\tConsist\tWstCons");
3066 SNPRINT2(results, "\t%7.3f", consistency);
3067 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3068 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003069 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3070 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003071 rate_err, fabs(rate_err));
3072 }
3073
3074 fclose(f);
3075 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003076#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003077#if CONFIG_SPEED_STATS
3078 if (cpi->oxcf.pass != 1) {
3079 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3080 }
3081#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003082
3083#if CONFIG_COLLECT_PARTITION_STATS
3084 if (cpi->oxcf.pass != 1) {
3085 av1_print_partition_stats(&cpi->partition_stats);
3086 }
3087#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003088 }
3089
Yue Chen7cae98f2018-08-24 10:43:16 -07003090 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
3091 aom_free(cpi->tpl_stats[frame].tpl_stats_ptr);
3092 cpi->tpl_stats[frame].is_valid = 0;
3093 }
3094
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303095 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003096 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003097 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3098
3099 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003100 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003101
3102 // Deallocate allocated thread data.
Ravi Chaudhary1cf7d162018-10-09 17:00:43 +05303103 if (cpi->row_mt == 1) aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303104 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003105 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003106 aom_free(thread_data->td->tmp_conv_dst);
3107 for (int j = 0; j < 2; ++j) {
3108 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3109 }
Jingning Hand064cf02017-06-01 10:00:39 -07003110 aom_free(thread_data->td->above_pred_buf);
3111 aom_free(thread_data->td->left_pred_buf);
3112 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003113
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303114 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003115 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303116 for (int y = 0; y < 2; y++) {
3117 aom_free(thread_data->td->hash_value_buffer[x][y]);
3118 thread_data->td->hash_value_buffer[x][y] = NULL;
3119 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003120 }
Jingning Hand064cf02017-06-01 10:00:39 -07003121 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003122 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003123 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003124 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003125 }
3126 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303127#if CONFIG_MULTITHREAD
3128 if (cpi->row_mt == 1) {
3129 if (cpi->row_mt_mutex_ != NULL) {
3130 pthread_mutex_destroy(cpi->row_mt_mutex_);
3131 aom_free(cpi->row_mt_mutex_);
3132 }
3133 }
3134#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303135 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003136 aom_free(cpi->tile_thr_data);
3137 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003138
Deepa K G964e72e2018-05-16 16:56:01 +05303139 if (cpi->num_workers > 1) {
3140 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303141 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303142 }
3143
Yaowu Xuc27fc142016-08-22 16:08:15 -07003144 dealloc_compressor_data(cpi);
3145
3146 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3147 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003148 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003149 }
3150
3151#if CONFIG_FP_MB_STATS
3152 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003153 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003154 cpi->twopass.frame_mb_stats_buf = NULL;
3155 }
3156#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003157#if CONFIG_INTERNAL_STATS
3158 aom_free(cpi->ssim_vars);
3159 cpi->ssim_vars = NULL;
3160#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003161
Yaowu Xuf883b422016-08-30 14:01:10 -07003162 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003163 for (i = 0; i < FRAME_BUFFERS; ++i) {
3164 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3165 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003166 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xuf883b422016-08-30 14:01:10 -07003167 av1_free_ref_frame_buffers(cm->buffer_pool);
3168 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003169
3170#ifdef OUTPUT_YUV_SKINMAP
3171 fclose(yuv_skinmap_file);
3172#endif
3173#ifdef OUTPUT_YUV_REC
3174 fclose(yuv_rec_file);
3175#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003176}
3177
Yaowu Xuf883b422016-08-30 14:01:10 -07003178static void generate_psnr_packet(AV1_COMP *cpi) {
3179 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003180 int i;
3181 PSNR_STATS psnr;
David Turnerc29e1a92018-12-06 14:10:14 +00003182 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003183 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003184
3185 for (i = 0; i < 4; ++i) {
3186 pkt.data.psnr.samples[i] = psnr.samples[i];
3187 pkt.data.psnr.sse[i] = psnr.sse[i];
3188 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3189 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003190 pkt.kind = AOM_CODEC_PSNR_PKT;
3191 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003192}
3193
Yaowu Xuf883b422016-08-30 14:01:10 -07003194int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003195 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3196
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003197 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003198 return 0;
3199}
3200
Thomas Daede497d1952017-08-08 17:33:06 -07003201int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3202 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003203 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003204 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003205 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003206 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003207 return 0;
3208 } else {
3209 return -1;
3210 }
3211}
3212
Thomas Daede497d1952017-08-08 17:33:06 -07003213int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3214 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003215 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003216 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003217 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003218 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003219 return 0;
3220 } else {
3221 return -1;
3222 }
3223}
3224
3225int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003226 cpi->ext_refresh_frame_context = update;
3227 cpi->ext_refresh_frame_context_pending = 1;
3228 return 0;
3229}
3230
3231#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3232// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3233// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3234// not denoise the UV channels at this time. If ever we implement UV channel
3235// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003236void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003237 uint8_t *src = s->y_buffer;
3238 int h = s->y_height;
3239
3240 do {
3241 fwrite(src, s->y_width, 1, f);
3242 src += s->y_stride;
3243 } while (--h);
3244
3245 src = s->u_buffer;
3246 h = s->uv_height;
3247
3248 do {
3249 fwrite(src, s->uv_width, 1, f);
3250 src += s->uv_stride;
3251 } while (--h);
3252
3253 src = s->v_buffer;
3254 h = s->uv_height;
3255
3256 do {
3257 fwrite(src, s->uv_width, 1, f);
3258 src += s->uv_stride;
3259 } while (--h);
3260}
3261#endif
3262
Yaowu Xuc27fc142016-08-22 16:08:15 -07003263#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003264void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003265 uint8_t *src = s->y_buffer;
3266 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003267 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003268 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3269 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3270
3271 do {
3272 fwrite(src16, s->y_width, 2, yuv_rec_file);
3273 src16 += s->y_stride;
3274 } while (--h);
3275
3276 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3277 h = s->uv_height;
3278
3279 do {
3280 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3281 src16 += s->uv_stride;
3282 } while (--h);
3283
3284 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3285 h = s->uv_height;
3286
3287 do {
3288 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3289 src16 += s->uv_stride;
3290 } while (--h);
3291
3292 fflush(yuv_rec_file);
3293 return;
3294 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003295
3296 do {
3297 fwrite(src, s->y_width, 1, yuv_rec_file);
3298 src += s->y_stride;
3299 } while (--h);
3300
3301 src = s->u_buffer;
3302 h = s->uv_height;
3303
3304 do {
3305 fwrite(src, s->uv_width, 1, yuv_rec_file);
3306 src += s->uv_stride;
3307 } while (--h);
3308
3309 src = s->v_buffer;
3310 h = s->uv_height;
3311
3312 do {
3313 fwrite(src, s->uv_width, 1, yuv_rec_file);
3314 src += s->uv_stride;
3315 } while (--h);
3316
3317 fflush(yuv_rec_file);
3318}
3319#endif // OUTPUT_YUV_REC
3320
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003321#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003322static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3323 int i;
3324 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003325 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003326 AV1_COMMON *const cm = &cpi->common;
3327 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3328 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003329 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003330 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003331 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003332 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003333 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003334 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003335 // TODO(sarahparker): The earlier condition for recoding here was:
3336 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3337 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003338 }
3339 }
3340 return recode;
3341}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003342
Yaowu Xuc27fc142016-08-22 16:08:15 -07003343// Function to test for conditions that indicate we should loop
3344// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003345static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3346 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003347 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003348 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003349 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3350 int force_recode = 0;
3351
3352 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3353 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3354 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003355 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3356 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3357 (rc->projected_frame_size < low_limit && q > minq)) {
3358 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003359 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003360 // Deal with frame undershoot and whether or not we are
3361 // below the automatically set cq level.
3362 if (q > oxcf->cq_level &&
3363 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3364 force_recode = 1;
3365 }
3366 }
3367 }
3368 return force_recode;
3369}
3370
Cheng Chen46f30c72017-09-07 11:13:33 -07003371static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003372 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003373 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003374 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003375 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003376 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3377 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003378 };
3379
3380 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003381 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003382 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3383 BufferPool *const pool = cm->buffer_pool;
3384 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003385 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386
3387 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003388 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003389 continue;
3390 }
3391
Yaowu Xuc27fc142016-08-22 16:08:15 -07003392 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003393 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003394 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3395 if (new_fb == NULL) {
3396 const int new_fb_idx = get_free_fb(cm);
3397 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003398 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3399 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003400 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003401 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003402 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003403 }
David Turnere7ebf902018-12-04 14:04:55 +00003404
3405 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3406 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003407 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003408 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003409 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +05303410 cm->seq_params.use_highbitdepth, cpi->oxcf.border_in_pixels,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003411 cm->byte_alignment, NULL, NULL, NULL)) {
3412 if (force_scaling) {
3413 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003414 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003415 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003416 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003417 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003418 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003419 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003420 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3421 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003422 alloc_frame_mvs(cm, new_fb);
3423 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003424 } else {
David Turnera21966b2018-12-05 14:48:49 +00003425 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003426 buf->buf.y_crop_width = ref->y_crop_width;
3427 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003428 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003429 ++buf->ref_count;
3430 }
3431 } else {
David Turnere7ebf902018-12-04 14:04:55 +00003432 if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003433 }
3434 }
3435}
3436
Yaowu Xuf883b422016-08-30 14:01:10 -07003437static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003438 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003439 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3440 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3441 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003442 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003443 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003444 }
3445 }
3446}
3447
Yaowu Xuf883b422016-08-30 14:01:10 -07003448static void set_mv_search_params(AV1_COMP *cpi) {
3449 const AV1_COMMON *const cm = &cpi->common;
3450 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003451
3452 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003453 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003454
3455 if (cpi->sf.mv.auto_mv_step_size) {
3456 if (frame_is_intra_only(cm)) {
3457 // Initialize max_mv_magnitude for use in the first INTER frame
3458 // after a key/intra-only frame.
3459 cpi->max_mv_magnitude = max_mv_def;
3460 } else {
3461 if (cm->show_frame) {
3462 // Allow mv_steps to correspond to twice the max mv magnitude found
3463 // in the previous frame, capped by the default max_mv_magnitude based
3464 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003465 cpi->mv_step_param = av1_init_search_range(
3466 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003467 }
3468 cpi->max_mv_magnitude = 0;
3469 }
3470 }
3471}
3472
Hui Subdf0c992019-02-14 14:52:41 -08003473static void set_screen_content_options(AV1_COMP *cpi) {
3474 AV1_COMMON *cm = &cpi->common;
3475
3476 if (cm->seq_params.force_screen_content_tools != 2) {
3477 cm->allow_screen_content_tools = cm->allow_intrabc =
3478 cm->seq_params.force_screen_content_tools;
3479 return;
3480 }
3481
3482 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3483 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3484 return;
3485 }
3486
3487 // Estimate if the source frame is screen content, based on the portion of
3488 // blocks that have few luma colors.
3489 const uint8_t *src = cpi->source->y_buffer;
3490 assert(src != NULL);
3491 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
3492 const int stride = cpi->source->y_stride;
3493 const int width = cpi->source->y_width;
3494 const int height = cpi->source->y_height;
3495 const int bd = cm->seq_params.bit_depth;
3496 const int blk_w = 16;
3497 const int blk_h = 16;
3498 // These threshold values are selected experimentally.
3499 const int color_thresh = 4;
3500 const unsigned int var_thresh = 0;
3501 // Counts of blocks with no more than color_thresh colors.
3502 int counts_1 = 0;
3503 // Counts of blocks with no more than color_thresh colors and variance larger
3504 // than var_thresh.
3505 int counts_2 = 0;
3506
3507 for (int r = 0; r + blk_h <= height; r += blk_h) {
3508 for (int c = 0; c + blk_w <= width; c += blk_w) {
3509 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3510 const uint8_t *const this_src = src + r * stride + c;
3511 const int n_colors =
3512 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3513 count_buf)
3514 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3515 if (n_colors > 1 && n_colors <= color_thresh) {
3516 ++counts_1;
3517 struct buf_2d buf;
3518 buf.stride = stride;
3519 buf.buf = (uint8_t *)this_src;
3520 const unsigned int var =
3521 use_hbd
3522 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3523 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3524 if (var > var_thresh) ++counts_2;
3525 }
3526 }
3527 }
3528
3529 // The threshold values are selected experimentally.
3530 cm->allow_screen_content_tools =
3531 counts_1 * blk_h * blk_w * 10 > width * height;
3532 // IntraBC would force loop filters off, so we use more strict rules that also
3533 // requires that the block has high variance.
3534 cm->allow_intrabc = cm->allow_screen_content_tools &&
3535 counts_2 * blk_h * blk_w * 15 > width * height;
3536}
3537
Yaowu Xuf883b422016-08-30 14:01:10 -07003538static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003539 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003540 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003541 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003542 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003543 }
3544 cpi->global_motion_search_done = 0;
David Turner04b70d82019-01-24 15:39:19 +00003545 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07003546 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003547 cm->interp_filter = SWITCHABLE;
3548 cm->switchable_motion_mode = 1;
3549
Hui Subdf0c992019-02-14 14:52:41 -08003550 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
Aniket Dhokf6d7ed82019-01-04 14:05:57 +05303551 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552}
3553
Yaowu Xuf883b422016-08-30 14:01:10 -07003554static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003555 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003556 AV1_COMMON *const cm = &cpi->common;
3557 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003558
3559 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00003560 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003561
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003562 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003563 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3564 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003565
James Zern01a9d702017-08-25 19:09:33 +00003566 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003567 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003568 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003569 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003570
3571 // Configure experimental use of segmentation for enhanced coding of
3572 // static regions if indicated.
3573 // Only allowed in the second pass of a two pass encode, as it requires
3574 // lagged coding, and if the relevant speed feature flag is set.
3575 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3576 configure_static_seg_features(cpi);
3577}
3578
Yaowu Xuf883b422016-08-30 14:01:10 -07003579static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003580 int y_stride = cpi->scaled_source.y_stride;
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303581 int y_stride_src = (cpi->oxcf.resize_mode || cpi->oxcf.superres_mode)
3582 ? y_stride
3583 : cpi->lookahead->buf->img.y_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003584
3585 if (cpi->sf.mv.search_method == NSTEP) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303586 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3587 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003588 } else if (cpi->sf.mv.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303589 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3590 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
3591 y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003592 }
3593}
3594
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003595#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003596static void set_restoration_unit_size(int width, int height, int sx, int sy,
3597 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003598 (void)width;
3599 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003600 (void)sx;
3601 (void)sy;
3602#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3603 int s = AOMMIN(sx, sy);
3604#else
3605 int s = 0;
3606#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3607
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003608 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003609 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003610 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003611 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003612 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3613 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003614}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003615
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303616static void init_ref_frame_bufs(AV1_COMP *cpi) {
3617 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07003618 int i;
3619 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00003620 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07003621 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00003622 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07003623 }
3624 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07003625 pool->frame_bufs[i].ref_count = 0;
3626 }
RogerZhou86902d02018-02-28 15:29:16 -08003627 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003628 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303629 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08003630 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003631 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003632}
3633
Yaowu Xud3e7c682017-12-21 14:08:25 -08003634static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003635 int subsampling_x, int subsampling_y) {
3636 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003637 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07003638
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003639 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3640 seq_params->subsampling_x != subsampling_x ||
3641 seq_params->subsampling_y != subsampling_y) {
3642 seq_params->subsampling_x = subsampling_x;
3643 seq_params->subsampling_y = subsampling_y;
3644 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003645
3646 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303647 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07003648 alloc_util_frame_buffers(cpi);
3649
3650 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3651
3652 cpi->initial_width = cm->width;
3653 cpi->initial_height = cm->height;
3654 cpi->initial_mbs = cm->MBs;
3655 }
3656}
3657
3658// Returns 1 if the assigned width or height was <= 0.
3659static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3660 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003661 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003662 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3663 cm->seq_params.subsampling_x,
3664 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003665
3666 if (width <= 0 || height <= 0) return 1;
3667
3668 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003669 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003670
3671 if (cpi->initial_width && cpi->initial_height &&
3672 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3673 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003674 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003675 alloc_compressor_data(cpi);
3676 realloc_segmentation_maps(cpi);
3677 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003678 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003679 update_frame_size(cpi);
3680
3681 return 0;
3682}
3683
David Turner475a3132019-01-18 15:17:17 +00003684void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003685 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003686 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003687 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003688 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003689 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003690
Fergus Simpsonbc189932017-05-16 17:02:39 -07003691 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003692 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003693 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003694 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003695 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07003696 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003697 }
3698
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003699 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003700 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003701 }
3702
David Turnere7ebf902018-12-04 14:04:55 +00003703 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003704
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303705 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05303706 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3707 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303708 cm->num_allocated_above_contexts < cm->tile_rows) {
3709 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3710 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3711 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3712 "Failed to allocate context buffers");
3713 }
3714
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003716 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00003717 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
3718 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05303719 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003720 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003721 "Failed to allocate frame buffer");
3722
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003723 const int frame_width = cm->superres_upscaled_width;
3724 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003725 set_restoration_unit_size(frame_width, frame_height,
3726 seq_params->subsampling_x,
3727 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003728 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003729 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003730
3731 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003732 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003733 init_motion_estimation(cpi);
3734
3735 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00003736 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00003737 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00003738 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
3739 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00003740 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003741 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00003742 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003743 }
3744 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003745
Hui Su5ebd8702018-01-08 18:09:20 -08003746 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003747 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003748
3749 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3750}
3751
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003752static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3753 // Choose an arbitrary random number
3754 static unsigned int seed = 56789;
3755 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003756 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3757 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003758
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07003759 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003760 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003761 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003762 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003763 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003764 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003765 else
Urvang Joshide71d142017-10-05 12:12:15 -07003766 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003767 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003768 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003769 default: assert(0);
3770 }
Urvang Joshide71d142017-10-05 12:12:15 -07003771 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003772}
3773
Debargha Mukherjeee3cd5a52018-11-29 11:05:22 -08003774#define ENERGY_BY_Q2_THRESH 0.01
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003775#define ENERGY_BY_AC_THRESH 0.2
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003776
3777static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003778 double threshq,
3779 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003780 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003781 const double tq = threshq * q * q;
3782 const double tp = threshp * energy[1];
3783 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003784 int k;
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003785 for (k = 16; k > 8; --k) {
3786 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003787 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003788 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003789}
3790
3791static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003792 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003793 analyze_hor_freq(cpi, energy);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003794 /*
3795 printf("\nenergy = [");
3796 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
3797 printf("]\n");
3798 */
3799 return get_superres_denom_from_qindex_energy(
3800 qindex, energy, ENERGY_BY_Q2_THRESH, ENERGY_BY_AC_THRESH);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08003801}
3802
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003803static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
3804 // Choose an arbitrary random number
3805 static unsigned int seed = 34567;
3806 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003807 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3808 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07003809
3810 // Make sure that superres mode of the frame is consistent with the
3811 // sequence-level flag.
3812 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
3813 cpi->common.seq_params.enable_superres));
3814 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
3815 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003816
3817 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003818 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003819 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003820 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003821 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003822 else
Urvang Joshide71d142017-10-05 12:12:15 -07003823 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003824 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003825 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003826 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003827 // Do not use superres when screen content tools are used.
3828 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08003829 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
3830 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi2c92b072018-03-19 17:23:31 -07003831 int bottom_index, top_index;
3832 const int q = av1_rc_pick_q_and_bounds(
3833 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08003834
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003835 const int qthresh = (frame_is_intra_only(&cpi->common))
3836 ? oxcf->superres_kf_qthresh
3837 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08003838 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07003839 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003840 } else {
Debargha Mukherjeee3cd5a52018-11-29 11:05:22 -08003841 new_denom = get_superres_denom_for_qindex(cpi, q);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003842 }
3843 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003844 }
Urvang Joshi36a83732019-01-31 15:31:57 -08003845 case SUPERRES_AUTO: {
3846 // Don't use when screen content tools are used.
3847 if (cpi->common.allow_screen_content_tools) break;
3848 // Don't use for inter frames.
3849 if (!frame_is_intra_only(&cpi->common)) break;
3850 // Don't use for keyframes that can be used as references.
3851 if (cpi->rc.frames_to_key != 1) break;
Urvang Joshi36a83732019-01-31 15:31:57 -08003852
3853 // Now decide the use of superres based on 'q'.
3854 int bottom_index, top_index;
3855 const int q = av1_rc_pick_q_and_bounds(
3856 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
3857
3858 const int qthresh = 128;
3859 if (q <= qthresh) {
3860 new_denom = SCALE_NUMERATOR;
3861 } else {
3862 new_denom = get_superres_denom_for_qindex(cpi, q);
3863 }
3864 break;
3865 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003866 default: assert(0);
3867 }
Urvang Joshide71d142017-10-05 12:12:15 -07003868 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003869}
3870
Urvang Joshide71d142017-10-05 12:12:15 -07003871static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
3872 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
3873}
3874
3875static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08003876 // Only need to check the width, as scaling is horizontal only.
3877 (void)oheight;
3878 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07003879}
3880
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003881static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003882 SUPERRES_MODE superres_mode, int owidth,
3883 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07003884 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003885 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07003886 }
3887
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003888 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07003889 int resize_denom =
3890 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
3891 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
3892
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003893 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003894 // Alter superres scale as needed to enforce conformity.
3895 rsz->superres_denom =
3896 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
3897 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3898 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003899 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003900 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003901 // Alter resize scale as needed to enforce conformity.
3902 resize_denom =
3903 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003904 rsz->resize_width = owidth;
3905 rsz->resize_height = oheight;
3906 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003907 resize_denom);
3908 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3909 if (resize_denom > SCALE_NUMERATOR) {
3910 --resize_denom;
3911 rsz->resize_width = owidth;
3912 rsz->resize_height = oheight;
3913 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
3914 resize_denom);
3915 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003916 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003917 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003918 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003919 do {
Urvang Joshide71d142017-10-05 12:12:15 -07003920 if (resize_denom > rsz->superres_denom)
3921 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003922 else
Urvang Joshide71d142017-10-05 12:12:15 -07003923 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003924 rsz->resize_width = owidth;
3925 rsz->resize_height = oheight;
3926 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003927 resize_denom);
3928 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
3929 (resize_denom > SCALE_NUMERATOR ||
3930 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08003931 } else { // We are allowed to alter neither resize scale nor superres
3932 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003933 return 0;
3934 }
Urvang Joshide71d142017-10-05 12:12:15 -07003935 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003936}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003937
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003938// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00003939static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003940 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08003941 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshide71d142017-10-05 12:12:15 -07003942 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003943 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003944 if (cpi->resize_pending_width && cpi->resize_pending_height) {
3945 rsz.resize_width = cpi->resize_pending_width;
3946 rsz.resize_height = cpi->resize_pending_height;
3947 cpi->resize_pending_width = cpi->resize_pending_height = 0;
3948 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07003949 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003950 rsz.resize_width = cpi->oxcf.width;
3951 rsz.resize_height = cpi->oxcf.height;
3952 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003953 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003954 }
Urvang Joshide71d142017-10-05 12:12:15 -07003955 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003956 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
3957 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003958 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003959 return rsz;
3960}
3961
Urvang Joshi22b150b2019-01-10 14:32:32 -08003962static void setup_frame_size_from_params(AV1_COMP *cpi,
3963 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003964 int encode_width = rsz->resize_width;
3965 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003966
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003967 AV1_COMMON *cm = &cpi->common;
3968 cm->superres_upscaled_width = encode_width;
3969 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07003970 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003971 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
3972 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00003973 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003974}
3975
David Turnerdedd8ff2019-01-23 13:59:46 +00003976void av1_setup_frame_size(AV1_COMP *cpi) {
Debargha Mukherjeea082f762019-03-04 15:05:18 -08003977 AV1_COMMON *cm = &cpi->common;
Urvang Joshi22b150b2019-01-10 14:32:32 -08003978 // Reset superres params from previous frame.
Debargha Mukherjeea082f762019-03-04 15:05:18 -08003979 cm->superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00003980 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003981 setup_frame_size_from_params(cpi, &rsz);
Debargha Mukherjeea082f762019-03-04 15:05:18 -08003982
3983 assert(is_min_tile_width_satisfied(cm));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003984}
3985
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003986static void superres_post_encode(AV1_COMP *cpi) {
3987 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003988 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003989
Cheng Chen09c83a52018-06-05 12:27:36 -07003990 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003991
Urvang Joshid6b5d512018-03-20 13:34:38 -07003992 assert(cpi->oxcf.enable_superres);
3993 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07003994 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07003995
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003996 av1_superres_upscale(cm, NULL);
3997
3998 // If regular resizing is occurring the source will need to be downscaled to
3999 // match the upscaled superres resolution. Otherwise the original source is
4000 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07004001 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004002 cpi->source = cpi->unscaled_source;
4003 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
4004 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07004005 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
4006 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08004007 // Do downscale. cm->(width|height) has been updated by
4008 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004009 if (aom_realloc_frame_buffer(
4010 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004011 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4012 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4013 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004014 aom_internal_error(
4015 &cm->error, AOM_CODEC_MEM_ERROR,
4016 "Failed to reallocate scaled source buffer for superres");
4017 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4018 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004019 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004020 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004021 cpi->source = &cpi->scaled_source;
4022 }
4023}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004024
4025static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004026 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004027 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004028
Urvang Joshic8b52d52018-03-23 13:16:51 -07004029 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4030 cm->coded_lossless && cm->all_lossless));
4031
logangwf95c9162019-02-20 12:02:32 -08004032 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4033 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4034 !cm->large_scale_tile;
4035 const int use_restoration = cm->seq_params.enable_restoration &&
4036 !cm->all_lossless && !cm->large_scale_tile;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004037
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004038 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004039
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004040#if CONFIG_COLLECT_COMPONENT_TIMING
4041 start_timing(cpi, loop_filter_time);
4042#endif
logangwf95c9162019-02-20 12:02:32 -08004043 if (use_loopfilter) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004044 aom_clear_system_state();
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004045 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
logangwf95c9162019-02-20 12:02:32 -08004046 } else {
4047 lf->filter_level[0] = 0;
4048 lf->filter_level[1] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004049 }
4050
Debargha Mukherjee2382b142018-02-26 14:31:32 -08004051 if (lf->filter_level[0] || lf->filter_level[1]) {
Deepa K G964e72e2018-05-16 16:56:01 +05304052 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004053 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
Cheng Chene3600cd2018-09-21 18:45:42 -07004054#if LOOP_FILTER_BITMASK
4055 0,
4056#endif
Deepa K G964e72e2018-05-16 16:56:01 +05304057 cpi->workers, cpi->num_workers,
4058 &cpi->lf_row_sync);
4059 else
David Turnerc29e1a92018-12-06 14:10:14 +00004060 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
Cheng Chen84b09932018-08-12 17:35:13 -07004061#if LOOP_FILTER_BITMASK
4062 0,
Cheng Chen8ab1f442018-04-27 18:01:52 -07004063#endif
Cheng Chen84b09932018-08-12 17:35:13 -07004064 0, num_planes, 0);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004065 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004066#if CONFIG_COLLECT_COMPONENT_TIMING
4067 end_timing(cpi, loop_filter_time);
4068#endif
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004069
logangwf95c9162019-02-20 12:02:32 -08004070 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004071 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004072
logangwf95c9162019-02-20 12:02:32 -08004073 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004074#if CONFIG_COLLECT_COMPONENT_TIMING
4075 start_timing(cpi, cdef_time);
4076#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004077 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004078 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004079 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004080
4081 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004082 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004083#if CONFIG_COLLECT_COMPONENT_TIMING
4084 end_timing(cpi, cdef_time);
4085#endif
logangwf95c9162019-02-20 12:02:32 -08004086 } else {
4087 cm->cdef_info.cdef_bits = 0;
4088 cm->cdef_info.cdef_strengths[0] = 0;
4089 cm->cdef_info.nb_cdef_strengths = 1;
4090 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004091 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004092
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004093 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004094
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004095#if CONFIG_COLLECT_COMPONENT_TIMING
4096 start_timing(cpi, loop_restoration_time);
4097#endif
logangwf95c9162019-02-20 12:02:32 -08004098 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004099 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004100 av1_pick_filter_restoration(cpi->source, cpi);
4101 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4102 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4103 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304104 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004105 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304106 cpi->workers, cpi->num_workers,
4107 &cpi->lr_row_sync, &cpi->lr_ctxt);
4108 else
David Turnerc29e1a92018-12-06 14:10:14 +00004109 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304110 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004111 }
logangwf95c9162019-02-20 12:02:32 -08004112 } else {
4113 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4114 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4115 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004116 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004117#if CONFIG_COLLECT_COMPONENT_TIMING
4118 end_timing(cpi, loop_restoration_time);
4119#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004120}
4121
David Turnerf2b334c2018-12-13 13:00:55 +00004122static void fix_interp_filter(InterpFilter *const interp_filter,
4123 const FRAME_COUNTS *const counts) {
4124 if (*interp_filter == SWITCHABLE) {
4125 // Check to see if only one of the filters is actually used
4126 int count[SWITCHABLE_FILTERS] = { 0 };
4127 int num_filters_used = 0;
4128 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4129 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4130 count[i] += counts->switchable_interp[j][i];
4131 num_filters_used += (count[i] > 0);
4132 }
4133 if (num_filters_used == 1) {
4134 // Only one filter is used. So set the filter at frame level
4135 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4136 if (count[i]) {
4137 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4138 break;
4139 }
4140 }
4141 }
4142 }
4143}
4144
David Turner996b2c12018-12-07 15:52:30 +00004145static void finalize_encoded_frame(AV1_COMP *const cpi) {
4146 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004147 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004148
David Turner99e990e2018-12-10 12:54:26 +00004149 if (!cm->seq_params.reduced_still_picture_hdr &&
4150 encode_show_existing_frame(cm)) {
4151 RefCntBuffer *const frame_to_show =
4152 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4153
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004154 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004155 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4156 "Buffer does not contain a reconstructed frame");
4157 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004158 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004159 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004160 }
David Turner08f909c2018-12-18 13:29:14 +00004161
4162 if (!encode_show_existing_frame(cm) &&
4163 cm->seq_params.film_grain_params_present &&
4164 (cm->show_frame || cm->showable_frame)) {
4165 // Copy the current frame's film grain params to the its corresponding
4166 // RefCntBuffer slot.
4167 cm->cur_frame->film_grain_params = cm->film_grain_params;
4168
4169 // We must update the parameters if this is not an INTER_FRAME
4170 if (current_frame->frame_type != INTER_FRAME)
4171 cm->cur_frame->film_grain_params.update_parameters = 1;
4172
4173 // Iterate the random seed for the next frame.
4174 cm->film_grain_params.random_seed += 3381;
4175 if (cm->film_grain_params.random_seed == 0)
4176 cm->film_grain_params.random_seed = 7391;
4177 }
David Turnerf2b334c2018-12-13 13:00:55 +00004178
4179 // Initialise all tiles' contexts from the global frame context
4180 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4181 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4182 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4183 cpi->tile_data[tile_idx].tctx = *cm->fc;
4184 }
4185 }
4186
4187 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004188}
4189
Urvang Joshi57643372019-02-21 11:10:57 -08004190static int get_regulated_q_overshoot(AV1_COMP *const cpi, int q_low, int q_high,
4191 int top_index, int bottom_index) {
4192 const AV1_COMMON *const cm = &cpi->common;
4193 const RATE_CONTROL *const rc = &cpi->rc;
4194
4195 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4196
4197 int q_regulated =
4198 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4199 AOMMAX(q_high, top_index), cm->width, cm->height);
4200
4201 int retries = 0;
4202 while (q_regulated < q_low && retries < 10) {
4203 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4204 q_regulated =
4205 av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4206 AOMMAX(q_high, top_index), cm->width, cm->height);
4207 retries++;
4208 }
4209 return q_regulated;
4210}
4211
4212static int get_regulated_q_undershoot(AV1_COMP *const cpi, int q_high,
4213 int top_index, int bottom_index) {
4214 const AV1_COMMON *const cm = &cpi->common;
4215 const RATE_CONTROL *const rc = &cpi->rc;
4216
4217 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4218 int q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4219 top_index, cm->width, cm->height);
4220
4221 int retries = 0;
4222 while (q_regulated > q_high && retries < 10) {
4223 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4224 q_regulated = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4225 top_index, cm->width, cm->height);
4226 retries++;
4227 }
4228 return q_regulated;
4229}
4230
David Turner2f3b5df2019-01-02 14:30:50 +00004231// Called after encode_with_recode_loop() has just encoded a frame and packed
4232// its bitstream. This function works out whether we under- or over-shot
4233// our bitrate target and adjusts q as appropriate. Also decides whether
4234// or not we should do another recode loop, indicated by *loop
4235static void recode_loop_update_q(AV1_COMP *const cpi, int *const loop,
4236 int *const q, int *const q_low,
4237 int *const q_high, const int top_index,
4238 const int bottom_index,
4239 int *const undershoot_seen,
4240 int *const overshoot_seen,
4241 const int loop_at_this_size) {
4242 AV1_COMMON *const cm = &cpi->common;
4243 RATE_CONTROL *const rc = &cpi->rc;
4244
4245 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4246 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4247 &frame_under_shoot_limit,
4248 &frame_over_shoot_limit);
4249 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4250
4251 if ((cm->current_frame.frame_type == KEY_FRAME) &&
4252 rc->this_key_frame_forced &&
4253 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4254 int last_q = *q;
4255 int64_t kf_err;
4256
4257 int64_t high_err_target = cpi->ambient_err;
4258 int64_t low_err_target = cpi->ambient_err >> 1;
4259
4260 if (cm->seq_params.use_highbitdepth) {
4261 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4262 } else {
4263 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4264 }
4265 // Prevent possible divide by zero error below for perfect KF
4266 kf_err += !kf_err;
4267
4268 // The key frame is not good enough or we can afford
4269 // to make it better without undue risk of popping.
4270 if ((kf_err > high_err_target &&
4271 rc->projected_frame_size <= frame_over_shoot_limit) ||
4272 (kf_err > low_err_target &&
4273 rc->projected_frame_size <= frame_under_shoot_limit)) {
4274 // Lower q_high
4275 *q_high = *q > *q_low ? *q - 1 : *q_low;
4276
4277 // Adjust Q
4278 *q = (int)((*q * high_err_target) / kf_err);
4279 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4280 } else if (kf_err < low_err_target &&
4281 rc->projected_frame_size >= frame_under_shoot_limit) {
4282 // The key frame is much better than the previous frame
4283 // Raise q_low
4284 *q_low = *q < *q_high ? *q + 1 : *q_high;
4285
4286 // Adjust Q
4287 *q = (int)((*q * low_err_target) / kf_err);
4288 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4289 }
4290
4291 // Clamp Q to upper and lower limits:
4292 *q = clamp(*q, *q_low, *q_high);
4293
4294 *loop = *q != last_q;
4295 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4296 frame_under_shoot_limit, *q,
4297 AOMMAX(*q_high, top_index), bottom_index)) {
4298 // Is the projected frame size out of range and are we allowed
4299 // to attempt to recode.
4300 int last_q = *q;
David Turner2f3b5df2019-01-02 14:30:50 +00004301
4302 // Frame size out of permitted range:
4303 // Update correction factor & compute new Q to try...
4304 // Frame is too large
4305 if (rc->projected_frame_size > rc->this_frame_target) {
4306 // Special case if the projected size is > the max allowed.
4307 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4308 *q_high = rc->worst_quality;
4309
4310 // Raise Qlow as to at least the current value
4311 *q_low = *q < *q_high ? *q + 1 : *q_high;
4312
Urvang Joshi57643372019-02-21 11:10:57 -08004313 if (*undershoot_seen || loop_at_this_size > 2 ||
4314 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004315 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4316
4317 *q = (*q_high + *q_low + 1) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004318 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4319 const int q_mid = (*q_high + *q_low + 1) / 2;
4320 const int q_regulated = get_regulated_q_overshoot(
4321 cpi, *q_low, *q_high, top_index, bottom_index);
4322 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4323 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4324 *q = (q_mid + q_regulated + 1) / 2;
David Turner2f3b5df2019-01-02 14:30:50 +00004325 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004326 *q = get_regulated_q_overshoot(cpi, *q_low, *q_high, top_index,
4327 bottom_index);
David Turner2f3b5df2019-01-02 14:30:50 +00004328 }
4329
4330 *overshoot_seen = 1;
4331 } else {
4332 // Frame is too small
4333 *q_high = *q > *q_low ? *q - 1 : *q_low;
4334
Urvang Joshi57643372019-02-21 11:10:57 -08004335 if (*overshoot_seen || loop_at_this_size > 2 ||
4336 (loop_at_this_size == 2 && !frame_is_intra_only(cm))) {
David Turner2f3b5df2019-01-02 14:30:50 +00004337 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4338 *q = (*q_high + *q_low) / 2;
Urvang Joshi57643372019-02-21 11:10:57 -08004339 } else if (loop_at_this_size == 2 && frame_is_intra_only(cm)) {
4340 const int q_mid = (*q_high + *q_low) / 2;
4341 const int q_regulated =
4342 get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4343 // Get 'q' in-between 'q_mid' and 'q_regulated' for a smooth
4344 // transition between loop_at_this_size < 2 and loop_at_this_size > 2.
4345 *q = (q_mid + q_regulated) / 2;
4346
4347 // Special case reset for qlow for constrained quality.
4348 // This should only trigger where there is very substantial
4349 // undershoot on a frame and the auto cq level is above
4350 // the user passsed in value.
4351 if (cpi->oxcf.rc_mode == AOM_CQ && q_regulated < *q_low) {
4352 *q_low = *q;
4353 }
David Turner2f3b5df2019-01-02 14:30:50 +00004354 } else {
Urvang Joshi57643372019-02-21 11:10:57 -08004355 *q = get_regulated_q_undershoot(cpi, *q_high, top_index, bottom_index);
4356
David Turner2f3b5df2019-01-02 14:30:50 +00004357 // Special case reset for qlow for constrained quality.
4358 // This should only trigger where there is very substantial
4359 // undershoot on a frame and the auto cq level is above
4360 // the user passsed in value.
4361 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
4362 *q_low = *q;
4363 }
David Turner2f3b5df2019-01-02 14:30:50 +00004364 }
4365
4366 *undershoot_seen = 1;
4367 }
4368
4369 // Clamp Q to upper and lower limits:
4370 *q = clamp(*q, *q_low, *q_high);
4371
4372 *loop = (*q != last_q);
4373 } else {
4374 *loop = 0;
4375 }
4376}
4377
Tom Finegane4099e32018-01-23 12:01:51 -08004378static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004379 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004380 RATE_CONTROL *const rc = &cpi->rc;
David Turner2f3b5df2019-01-02 14:30:50 +00004381 const int allow_recode = cpi->sf.recode_loop != DISALLOW_RECODE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004382
4383 set_size_independent_vars(cpi);
4384
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004385 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004386
David Turnerdedd8ff2019-01-23 13:59:46 +00004387 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004388
David Turner2f3b5df2019-01-02 14:30:50 +00004389 int top_index = 0, bottom_index = 0;
4390 int q = 0, q_low = 0, q_high = 0;
4391 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4392 q_low = bottom_index;
4393 q_high = top_index;
4394
4395 // Loop variables
4396 int loop_count = 0;
4397 int loop_at_this_size = 0;
4398 int loop = 0;
4399 int overshoot_seen = 0;
4400 int undershoot_seen = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004401
4402#if CONFIG_COLLECT_COMPONENT_TIMING
4403 printf("\n Encoding a frame:");
4404#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004405 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004406 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004407
Urvang Joshif1fa6862018-01-08 16:39:33 -08004408 // if frame was scaled calculate global_motion_search again if already
4409 // done
David Turner2f3b5df2019-01-02 14:30:50 +00004410 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004411 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00004412 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004413 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00004414 }
4415 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004416 cpi->source =
4417 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004418 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004419 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4420 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004421 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004422
David Turner2f3b5df2019-01-02 14:30:50 +00004423 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004424 if (loop_count > 0) {
4425 release_scaled_references(cpi);
4426 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004427 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004428 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004429 av1_set_quantizer(cm, q);
Yue Chena7d80482019-01-31 09:48:09 -08004430 av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08004431
4432 av1_set_variance_partition_thresholds(cpi, q, 0);
4433
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004434 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
4435 // cm->current_frame.frame_number, cm->show_frame, q,
4436 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004437
David Turner2f3b5df2019-01-02 14:30:50 +00004438 if (loop_count == 0) {
4439 setup_frame(cpi);
4440 } else if (get_primary_ref_frame_buf(cm) == NULL) {
4441 // Base q-index may have changed, so we need to assign proper default coef
4442 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07004443 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004444 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004445 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004446
Yaowu Xuc27fc142016-08-22 16:08:15 -07004447 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004448 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004449 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004450 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00004451 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
4452 suppress_active_map(cpi);
4453 av1_cyclic_refresh_setup(cpi);
4454 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004455 }
David Turner2f3b5df2019-01-02 14:30:50 +00004456
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004457 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004458 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004459 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004460 } else {
4461 calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004462 }
David Barkercab37552018-03-21 11:56:24 +00004463 } else {
4464 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004465 }
David Barkercab37552018-03-21 11:56:24 +00004466 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004467
David Turner2f3b5df2019-01-02 14:30:50 +00004468 if (allow_recode) save_coding_context(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004469#if CONFIG_COLLECT_COMPONENT_TIMING
4470 start_timing(cpi, av1_encode_frame_time);
4471#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004472 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004473 av1_encode_frame(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004474#if CONFIG_COLLECT_COMPONENT_TIMING
4475 end_timing(cpi, av1_encode_frame_time);
4476#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004477
Yaowu Xuf883b422016-08-30 14:01:10 -07004478 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004479
4480 // Dummy pack of the bitstream using up to date stats to get an
4481 // accurate estimate of output frame size to determine if we need
4482 // to recode.
4483 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004484 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004485
David Turner996b2c12018-12-07 15:52:30 +00004486 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00004487 int largest_tile_id = 0; // Output from bitstream: unused here
4488 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004489 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004490
4491 rc->projected_frame_size = (int)(*size) << 3;
4492 restore_coding_context(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004493 }
4494
David Turner2f3b5df2019-01-02 14:30:50 +00004495 if (allow_recode && cpi->oxcf.rc_mode != AOM_Q) {
4496 // Update q and decide whether to do a recode loop
4497 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
4498 bottom_index, &undershoot_seen, &overshoot_seen,
4499 loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004500 }
4501
4502 // Special case for overlay frame.
4503 if (rc->is_src_frame_alt_ref &&
4504 rc->projected_frame_size < rc->max_frame_bandwidth)
4505 loop = 0;
4506
David Turner2f3b5df2019-01-02 14:30:50 +00004507 if (allow_recode && !cpi->sf.gm_disable_recode &&
4508 recode_loop_test_global_motion(cpi)) {
4509 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004510 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004511
Yaowu Xuc27fc142016-08-22 16:08:15 -07004512 if (loop) {
4513 ++loop_count;
4514 ++loop_at_this_size;
4515
4516#if CONFIG_INTERNAL_STATS
4517 ++cpi->tot_recode_hits;
4518#endif
4519 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004520#if CONFIG_COLLECT_COMPONENT_TIMING
4521 if (loop) printf("\n Recoding:");
4522#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004523 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004524
4525 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004526}
4527
Yaowu Xuc27fc142016-08-22 16:08:15 -07004528#define DUMP_RECON_FRAMES 0
4529
4530#if DUMP_RECON_FRAMES == 1
4531// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004532static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4533 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00004534 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00004535 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004536
Zoe Liub4f31032017-11-03 23:48:35 -07004537 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00004538 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07004539 return;
4540 }
4541
Zoe Liu27deb382018-03-27 15:13:56 -07004542 static const int flag_list[REF_FRAMES] = { 0,
4543 AOM_LAST_FLAG,
4544 AOM_LAST2_FLAG,
4545 AOM_LAST3_FLAG,
4546 AOM_GOLD_FLAG,
4547 AOM_BWD_FLAG,
4548 AOM_ALT2_FLAG,
4549 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004550 printf(
4551 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4552 "show_existing_frame=%d) "
4553 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00004554 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07004555 cm->show_existing_frame);
4556 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004557 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
4558 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
Urvang Joshib6f17672019-03-05 11:51:02 -08004559 printf(" %d(%c)", ref_offset,
4560 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N');
Zoe Liub4f31032017-11-03 23:48:35 -07004561 }
4562 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004563
4564 if (!cm->show_frame) {
4565 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00004566 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004567 return;
4568 }
4569
Zoe Liub4f31032017-11-03 23:48:35 -07004570 int h;
4571 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4572 FILE *f_recon = NULL;
4573
David Turnerd2a592e2018-11-16 14:59:31 +00004574 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004575 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4576 printf("Unable to open file %s to write.\n", file_name);
4577 return;
4578 }
4579 } else {
4580 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4581 printf("Unable to open file %s to append.\n", file_name);
4582 return;
4583 }
4584 }
4585 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004586 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4587 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
4588 "refresh_alt_ref_frame=%d, rf_level=%d, "
4589 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
David Turnerd2a592e2018-11-16 14:59:31 +00004590 current_frame->frame_number, cpi->twopass.gf_group.index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004591 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
David Turnerd2a592e2018-11-16 14:59:31 +00004592 current_frame->order_hint, cm->show_frame, cm->show_existing_frame,
Zoe Liuf40a9572017-10-13 12:37:19 -07004593 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
4594 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4595 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004596#if 0
4597 int ref_frame;
4598 printf("get_ref_frame_map_idx: [");
4599 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00004600 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07004601 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07004602#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004603
4604 // --- Y ---
4605 for (h = 0; h < cm->height; ++h) {
4606 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4607 f_recon);
4608 }
4609 // --- U ---
4610 for (h = 0; h < (cm->height >> 1); ++h) {
4611 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4612 f_recon);
4613 }
4614 // --- V ---
4615 for (h = 0; h < (cm->height >> 1); ++h) {
4616 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4617 f_recon);
4618 }
4619
4620 fclose(f_recon);
4621}
4622#endif // DUMP_RECON_FRAMES
4623
David Turnerbc0993e2019-02-15 14:42:23 +00004624static int get_interp_filter_selected(const AV1_COMMON *const cm,
4625 MV_REFERENCE_FRAME ref,
4626 InterpFilters ifilter) {
4627 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
4628 if (buf == NULL) return 0;
4629 return buf->interp_filter_selected[ifilter];
4630}
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304631
David Turnerbc0993e2019-02-15 14:42:23 +00004632static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
4633 const AV1_COMMON *const cm = &cpi->common;
4634 int ref_total[REF_FRAMES] = { 0 };
4635
4636 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4637 return 0;
4638
4639 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
4640 for (InterpFilters ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4641 ++ifilter) {
4642 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
4643 }
4644 }
4645 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
4646 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
4647 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
4648
4649 int mask = 0;
4650 for (InterpFilters ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4651 ++ifilter) {
4652 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
4653 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
4654 int filter_score =
4655 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
4656 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
4657 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
4658 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
4659 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
4660 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
4661 if (filter_score < ref_total_total) mask |= 1 << ifilter;
4662 }
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304663 }
4664 return mask;
4665}
4666
David Turnerefed6372019-01-11 15:14:11 +00004667static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
4668 const YV12_BUFFER_CONFIG *last_picture,
4669 hash_table *last_hash_table) {
4670 aom_clear_system_state();
4671 // check use hash ME
4672 int k;
4673 uint32_t hash_value_1;
4674 uint32_t hash_value_2;
4675
4676 const int block_size = 8;
4677 const double threshold_current = 0.8;
4678 const double threshold_average = 0.95;
4679 const int max_history_size = 32;
4680 int T = 0; // total block
4681 int C = 0; // match with collocated block
4682 int S = 0; // smooth region but not match with collocated block
4683 int M = 0; // match with other block
4684
4685 const int pic_width = cur_picture->y_width;
4686 const int pic_height = cur_picture->y_height;
4687 for (int i = 0; i + block_size <= pic_height; i += block_size) {
4688 for (int j = 0; j + block_size <= pic_width; j += block_size) {
4689 const int x_pos = j;
4690 const int y_pos = i;
4691 int match = 1;
4692 T++;
4693
4694 // check whether collocated block match with current
4695 uint8_t *p_cur = cur_picture->y_buffer;
4696 uint8_t *p_ref = last_picture->y_buffer;
4697 int stride_cur = cur_picture->y_stride;
4698 int stride_ref = last_picture->y_stride;
4699 p_cur += (y_pos * stride_cur + x_pos);
4700 p_ref += (y_pos * stride_ref + x_pos);
4701
4702 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
4703 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
4704 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
4705 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4706 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4707 if (p16_cur[tmpX] != p16_ref[tmpX]) {
4708 match = 0;
4709 }
4710 }
4711 p16_cur += stride_cur;
4712 p16_ref += stride_ref;
4713 }
4714 } else {
4715 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4716 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4717 if (p_cur[tmpX] != p_ref[tmpX]) {
4718 match = 0;
4719 }
4720 }
4721 p_cur += stride_cur;
4722 p_ref += stride_ref;
4723 }
4724 }
4725
4726 if (match) {
4727 C++;
4728 continue;
4729 }
4730
4731 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
4732 y_pos) ||
4733 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
4734 S++;
4735 continue;
4736 }
4737
4738 av1_get_block_hash_value(
4739 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
4740 block_size, &hash_value_1, &hash_value_2,
4741 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
4742 // Hashing does not work for highbitdepth currently.
4743 // TODO(Roger): Make it work for highbitdepth.
4744 if (av1_use_hash_me(&cpi->common)) {
4745 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
4746 M++;
4747 }
4748 }
4749 }
4750 }
4751
4752 assert(T > 0);
4753 double csm_rate = ((double)(C + S + M)) / ((double)(T));
4754 double m_rate = ((double)(M)) / ((double)(T));
4755
4756 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
4757 cpi->m_rate_array[cpi->rate_index] = m_rate;
4758
4759 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
4760 cpi->rate_size++;
4761 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
4762
4763 if (csm_rate < threshold_current) {
4764 return 0;
4765 }
4766
4767 if (C == T) {
4768 return 1;
4769 }
4770
4771 double csm_average = 0.0;
4772 double m_average = 0.0;
4773
4774 for (k = 0; k < cpi->rate_size; k++) {
4775 csm_average += cpi->csm_rate_array[k];
4776 m_average += cpi->m_rate_array[k];
4777 }
4778 csm_average /= cpi->rate_size;
4779 m_average /= cpi->rate_size;
4780
4781 if (csm_average < threshold_average) {
4782 return 0;
4783 }
4784
4785 if (M > (T - C - S) / 3) {
4786 return 1;
4787 }
4788
4789 if (csm_rate > 0.99 && m_rate > 0.01) {
4790 return 1;
4791 }
4792
4793 if (csm_average + m_average > 1.01) {
4794 return 1;
4795 }
4796
4797 return 0;
4798}
4799
David Turner73245762019-02-11 16:42:34 +00004800// Refresh reference frame buffers according to refresh_frame_flags.
4801static void refresh_reference_frames(AV1_COMP *cpi) {
4802 AV1_COMMON *const cm = &cpi->common;
4803 // All buffers are refreshed for shown keyframes and S-frames.
4804
4805 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
4806 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
4807 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
4808 }
4809 }
4810}
4811
4812static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
4813 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004814 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004815 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00004816 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07004817 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004818 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01004819
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004820#if CONFIG_COLLECT_COMPONENT_TIMING
4821 start_timing(cpi, encode_frame_to_data_rate_time);
4822#endif
4823
Fangwen Fu8d164de2016-12-14 13:40:54 -08004824 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00004825 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07004826
Yunqing Wang9612d552018-05-15 14:58:30 -07004827 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
4828 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07004829
sarahparker21dbca42018-03-30 17:43:44 -07004830 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07004831 // cm->allow_ref_frame_mvs needs to be written into the frame header while
4832 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
4833 // separated from frame_might_allow_ref_frame_mvs().
4834 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
4835
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07004836 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07004837 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004838
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304839 cm->last_frame_type = current_frame->frame_type;
4840 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4841 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4842
Yunqing Wangc3e21ec2019-02-28 16:29:27 -08004843 cpi->two_pass_partition_search = cpi->sf.two_pass_partition_search &&
4844 !cpi->partition_search_skippable_frame;
Yunqing Wangc3e21ec2019-02-28 16:29:27 -08004845
Sarah Parker33005522018-07-27 14:46:25 -07004846 if (encode_show_existing_frame(cm)) {
Jingning Han8f661602017-08-19 08:16:50 -07004847 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07004848
David Turner996b2c12018-12-07 15:52:30 +00004849 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004850 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00004851 int largest_tile_id = 0; // Output from bitstream: unused here
4852 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004853 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004854
David Turner90311862018-11-29 13:34:36 +00004855 if (seq_params->frame_id_numbers_present_flag &&
4856 current_frame->frame_type == KEY_FRAME) {
4857 // Displaying a forward key-frame, so reset the ref buffer IDs
4858 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
4859 for (int i = 0; i < REF_FRAMES; i++)
4860 cm->ref_frame_id[i] = display_frame_id;
4861 }
4862
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004863 cpi->seq_params_locked = 1;
4864
Yaowu Xuc27fc142016-08-22 16:08:15 -07004865#if DUMP_RECON_FRAMES == 1
4866 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4867 dump_filtered_recon_frames(cpi);
4868#endif // DUMP_RECON_FRAMES
4869
David Turner73245762019-02-11 16:42:34 +00004870 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
4871 // for the purpose to verify no mismatch between encoder and decoder.
4872 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004873
David Turner73245762019-02-11 16:42:34 +00004874 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004875
Yaowu Xuc27fc142016-08-22 16:08:15 -07004876 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4877 // to do post-encoding update accordingly.
4878 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004879 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004880 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004881 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004882
David Turnerd2a592e2018-11-16 14:59:31 +00004883 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004884
Tom Finegane4099e32018-01-23 12:01:51 -08004885 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004886 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004887
David Turnerefed6372019-01-11 15:14:11 +00004888 // Work out whether to force_integer_mv this frame
4889 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
4890 !frame_is_intra_only(cm)) {
4891 if (cpi->common.seq_params.force_integer_mv == 2) {
4892 // Adaptive mode: see what previous frame encoded did
4893 if (cpi->unscaled_last_source != NULL) {
4894 cm->cur_frame_force_integer_mv =
4895 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
4896 cpi->previous_hash_table);
4897 } else {
4898 cpi->common.cur_frame_force_integer_mv = 0;
4899 }
4900 } else {
4901 cpi->common.cur_frame_force_integer_mv =
4902 cpi->common.seq_params.force_integer_mv;
4903 }
4904 } else {
4905 cpi->common.cur_frame_force_integer_mv = 0;
4906 }
4907
Yaowu Xuc27fc142016-08-22 16:08:15 -07004908 // Set default state for segment based loop filter update flags.
4909 cm->lf.mode_ref_delta_update = 0;
4910
Yaowu Xuc27fc142016-08-22 16:08:15 -07004911 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05004912 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004913 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004914 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004915
4916 // If segmentation is enabled force a map update for key frames.
4917 if (seg->enabled) {
4918 seg->update_map = 1;
4919 seg->update_data = 1;
4920 }
4921
4922 // The alternate reference frame cannot be active for a key frame.
4923 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004924 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00004925 if (cpi->oxcf.mtu == 0) {
4926 cm->num_tg = cpi->oxcf.num_tile_groups;
4927 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08004928 // Use a default value for the purposes of weighting costs in probability
4929 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00004930 cm->num_tg = DEFAULT_MAX_NUM_TG;
4931 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004932
4933 // For 1 pass CBR, check if we are dropping this frame.
4934 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004935 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00004936 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004937 if (av1_rc_drop_frame(cpi)) {
4938 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00004939 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004940 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004941 }
4942 }
4943
Yaowu Xuf883b422016-08-30 14:01:10 -07004944 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004945
4946#if CONFIG_INTERNAL_STATS
4947 memset(cpi->mode_chosen_counts, 0,
4948 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4949#endif
4950
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004951 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004952 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08004953 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004954 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00004955 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08004956 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07004957 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
4958 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
4959 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004960 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004961 lsb = cpi->source->y_buffer[0] & 0xff;
4962 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004963 }
David Turner760a2f42018-12-07 15:25:36 +00004964 cm->current_frame_id =
4965 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05004966
4967 // S_frame is meant for stitching different streams of different
4968 // resolutions together, so current_frame_id must be the
4969 // same across different streams of the same content current_frame_id
4970 // should be the same and not random. 0x37 is a chosen number as start
4971 // point
4972 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004973 } else {
4974 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00004975 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
4976 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004977 }
4978 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004979
Hui Su483a8452018-02-26 12:28:48 -08004980 switch (cpi->oxcf.cdf_update_mode) {
4981 case 0: // No CDF update for any frames(4~6% compression loss).
4982 cm->disable_cdf_update = 1;
4983 break;
4984 case 1: // Enable CDF update for all frames.
4985 cm->disable_cdf_update = 0;
4986 break;
4987 case 2:
4988 // Strategically determine at which frames to do CDF update.
4989 // Currently only enable CDF update for all-intra and no-show frames(1.5%
4990 // compression loss).
4991 // TODO(huisu@google.com): design schemes for various trade-offs between
4992 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08004993 cm->disable_cdf_update =
4994 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08004995 break;
4996 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004997 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08004998
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004999#if CONFIG_COLLECT_COMPONENT_TIMING
5000 start_timing(cpi, encode_with_recode_loop_time);
5001#endif
David Turner2f3b5df2019-01-02 14:30:50 +00005002 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
5003 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005004#if CONFIG_COLLECT_COMPONENT_TIMING
5005 end_timing(cpi, encode_with_recode_loop_time);
5006#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005007
5008#ifdef OUTPUT_YUV_SKINMAP
David Turnerd2a592e2018-11-16 14:59:31 +00005009 if (cpi->common.current_frame.frame_number > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005010 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005011 }
5012#endif // OUTPUT_YUV_SKINMAP
5013
5014 // Special case code to reduce pulsing when key frames are forced at a
5015 // fixed interval. Note the reconstruction error if it is the frame before
5016 // the force key frame
5017 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005018 if (seq_params->use_highbitdepth) {
Jack Haughtonddb80602018-11-21 16:41:49 +00005019 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005020 } else {
Jack Haughtonddb80602018-11-21 16:41:49 +00005021 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005022 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005023 }
5024
David Turnerc29e1a92018-12-06 14:10:14 +00005025 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5026 cm->cur_frame->buf.transfer_characteristics =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005027 seq_params->transfer_characteristics;
David Turnerc29e1a92018-12-06 14:10:14 +00005028 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5029 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5030 cm->cur_frame->buf.chroma_sample_position =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005031 seq_params->chroma_sample_position;
David Turnerc29e1a92018-12-06 14:10:14 +00005032 cm->cur_frame->buf.color_range = seq_params->color_range;
5033 cm->cur_frame->buf.render_width = cm->render_width;
5034 cm->cur_frame->buf.render_height = cm->render_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005035
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005036 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5037 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005038
5039 // Pick the loop filter level for the frame.
Cheng Chen68dc9142018-05-02 17:46:28 -07005040 if (!cm->allow_intrabc) {
David Barker218556e2018-02-14 14:23:12 +00005041 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08005042 } else {
Hui Su06463e42018-02-23 22:17:36 -08005043 cm->lf.filter_level[0] = 0;
5044 cm->lf.filter_level[1] = 0;
David Turnerebf96f42018-11-14 16:57:57 +00005045 cm->cdef_info.cdef_bits = 0;
5046 cm->cdef_info.cdef_strengths[0] = 0;
5047 cm->cdef_info.nb_cdef_strengths = 1;
5048 cm->cdef_info.cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005049 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5050 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5051 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08005052 }
David Barker218556e2018-02-14 14:23:12 +00005053
5054 // TODO(debargha): Fix mv search range on encoder side
David Turnerc29e1a92018-12-06 14:10:14 +00005055 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5056 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005057
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005058#ifdef OUTPUT_YUV_REC
David Turnerc29e1a92018-12-06 14:10:14 +00005059 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005060#endif
5061
David Turner996b2c12018-12-07 15:52:30 +00005062 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005063 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005064 int largest_tile_id = 0; // Output from pack_bitstream
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005065#if CONFIG_COLLECT_COMPONENT_TIMING
5066 start_timing(cpi, av1_pack_bitstream_final_time);
5067#endif
David Turner35cba132018-12-10 15:48:15 +00005068 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005069 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005070#if CONFIG_COLLECT_COMPONENT_TIMING
5071 end_timing(cpi, av1_pack_bitstream_final_time);
5072#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005073
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005074 cpi->seq_params_locked = 1;
5075
David Turner996b2c12018-12-07 15:52:30 +00005076 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005077 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00005078 for (int i = 0; i < REF_FRAMES; i++) {
5079 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005080 cm->ref_frame_id[i] = cm->current_frame_id;
5081 }
5082 }
5083 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005084
Yaowu Xuc27fc142016-08-22 16:08:15 -07005085#if DUMP_RECON_FRAMES == 1
5086 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005087 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005088#endif // DUMP_RECON_FRAMES
5089
Soo-Chul Han934af352017-10-15 15:21:51 -04005090 if (cm->seg.enabled) {
5091 if (cm->seg.update_map) {
5092 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005093 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00005094 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04005095 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5096 }
5097 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005098
5099 if (frame_is_intra_only(cm) == 0) {
5100 release_scaled_references(cpi);
5101 }
5102
David Turner73245762019-02-11 16:42:34 +00005103 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5104 // for the purpose to verify no mismatch between encoder and decoder.
5105 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
5106
5107 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005108
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005109#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005110 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005111#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005112
Hui Sudc54be62018-03-14 19:14:28 -07005113 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00005114 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07005115 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005116 }
David Turnera4c96252019-01-11 16:36:39 +00005117 if (!cm->large_scale_tile) {
5118 cm->cur_frame->frame_context = *cm->fc;
5119 }
5120#define EXT_TILE_DEBUG 0
5121#if EXT_TILE_DEBUG
5122 if (cm->large_scale_tile && oxcf->pass == 2) {
5123 char fn[20] = "./fc";
5124 fn[4] = current_frame->frame_number / 100 + '0';
5125 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
5126 fn[6] = (current_frame->frame_number % 10) + '0';
5127 fn[7] = '\0';
5128 av1_print_frame_contexts(cm->fc, fn);
5129 }
5130#endif // EXT_TILE_DEBUG
5131#undef EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07005132
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005133#if CONFIG_COLLECT_COMPONENT_TIMING
5134 end_timing(cpi, encode_frame_to_data_rate_time);
5135
5136 // Print out timing information.
5137 int i;
5138 fprintf(stderr, "\n Frame number: %d, Frame type: %s,\n",
5139 cm->current_frame.frame_number,
5140 get_frame_type_enum(cm->current_frame.frame_type));
5141 for (i = 0; i < kTimingComponents; i++) {
5142 cpi->component_time[i] += cpi->frame_component_time[i];
5143 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
5144 get_component_name(i), cpi->frame_component_time[i],
5145 cpi->component_time[i]);
5146 cpi->frame_component_time[i] = 0;
5147 }
5148#endif
5149
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305150 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005151
Yaowu Xuf883b422016-08-30 14:01:10 -07005152 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005153
David Turnerefed6372019-01-11 15:14:11 +00005154 // Store encoded frame's hash table for is_integer_mv() next time
5155 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
5156 cpi->previous_hash_table = &cm->cur_frame->hash_table;
5157 }
5158
Yaowu Xuc27fc142016-08-22 16:08:15 -07005159 // Clear the one shot update flags for segmentation map and mode/ref loop
5160 // filter deltas.
5161 cm->seg.update_map = 0;
5162 cm->seg.update_data = 0;
5163 cm->lf.mode_ref_delta_update = 0;
5164
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005165 // A droppable frame might not be shown but it always
5166 // takes a space in the gf group. Therefore, even when
5167 // it is not shown, we still need update the count down.
5168
Yaowu Xuc27fc142016-08-22 16:08:15 -07005169 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08005170 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5171 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005172 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005173 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005174 // Don't increment frame counters if this was an altref buffer
5175 // update not a real frame
Wei-Ting Lin96ee0eb2018-06-22 15:27:22 -07005176
David Turnerd2a592e2018-11-16 14:59:31 +00005177 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005178 }
5179
Tom Finegane4099e32018-01-23 12:01:51 -08005180 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005181}
5182
David Turner056f7cd2019-01-07 17:48:13 +00005183int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00005184 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00005185 const EncodeFrameParams *const frame_params,
5186 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00005187 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00005188 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005189
David Turnercb5e36f2019-01-17 17:15:25 +00005190 cpi->unscaled_source = frame_input->source;
5191 cpi->source = frame_input->source;
5192 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00005193
David Turner6e8b4d92019-02-18 15:01:33 +00005194 current_frame->refresh_frame_flags = frame_params->refresh_frame_flags;
David Turner07dbd8e2019-01-08 17:16:25 +00005195 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00005196 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00005197 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00005198 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005199 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00005200 cpi->speed = frame_params->speed;
David Turnere86ee0d2019-02-18 17:16:28 +00005201 cm->show_existing_frame = frame_params->show_existing_frame;
5202 cpi->existing_fb_idx_to_show = frame_params->existing_fb_idx_to_show;
David Turner07dbd8e2019-01-08 17:16:25 +00005203
David Turner73245762019-02-11 16:42:34 +00005204 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
5205 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
5206
David Turnerfe3aecb2019-02-06 14:42:42 +00005207 cpi->refresh_last_frame = frame_params->refresh_last_frame;
5208 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
5209 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
5210 cpi->refresh_alt2_ref_frame = frame_params->refresh_alt2_ref_frame;
5211 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
5212
David Turnera7f133c2019-01-22 14:47:16 +00005213 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
5214 current_frame->frame_number = 0;
5215
5216 if (cm->show_existing_frame) {
5217 current_frame->order_hint = cm->cur_frame->order_hint;
5218 } else {
5219 current_frame->order_hint =
5220 current_frame->frame_number + frame_params->order_offset;
5221 current_frame->order_hint %=
5222 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
5223 }
5224
David Turnercb5e36f2019-01-17 17:15:25 +00005225 if (cpi->oxcf.pass == 1) {
5226 av1_first_pass(cpi, frame_input->ts_duration);
5227 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00005228 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
5229 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00005230 return AOM_CODEC_ERROR;
5231 }
5232 } else {
David Turner056f7cd2019-01-07 17:48:13 +00005233 return AOM_CODEC_ERROR;
5234 }
5235
5236 return AOM_CODEC_OK;
5237}
5238
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005239#if CONFIG_DENOISE
5240static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5241 int block_size, float noise_level,
5242 int64_t time_stamp, int64_t end_time) {
5243 AV1_COMMON *const cm = &cpi->common;
5244 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005245 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5246 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005247 if (!cpi->denoise_and_model) {
5248 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5249 "Error allocating denoise and model");
5250 return -1;
5251 }
5252 }
5253 if (!cpi->film_grain_table) {
5254 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5255 if (!cpi->film_grain_table) {
5256 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5257 "Error allocating grain table");
5258 return -1;
5259 }
5260 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5261 }
5262 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5263 &cm->film_grain_params)) {
5264 if (cm->film_grain_params.apply_grain) {
5265 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5266 &cm->film_grain_params);
5267 }
5268 }
5269 return 0;
5270}
5271#endif
5272
James Zern3e2613b2017-03-30 23:14:40 -07005273int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005274 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5275 int64_t end_time) {
5276 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005277 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005278 int res = 0;
5279 const int subsampling_x = sd->subsampling_x;
5280 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005281 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005282
Yaowu Xuc27fc142016-08-22 16:08:15 -07005283 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005284
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005285#if CONFIG_INTERNAL_STATS
5286 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005287 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005288#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005289#if CONFIG_DENOISE
5290 if (cpi->oxcf.noise_level > 0)
5291 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5292 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5293 res = -1;
5294#endif // CONFIG_DENOISE
5295
Yaowu Xuf883b422016-08-30 14:01:10 -07005296 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005297 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005298 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005299#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005300 aom_usec_timer_mark(&timer);
5301 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005302#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005303 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005304 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005305 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005306 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005307 res = -1;
5308 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005309 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005310 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005311 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005312 "Profile 1 requires 4:4:4 color format");
5313 res = -1;
5314 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005315 if ((seq_params->profile == PROFILE_2) &&
5316 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005317 !(subsampling_x == 1 && subsampling_y == 0)) {
5318 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5319 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005320 res = -1;
5321 }
5322
5323 return res;
5324}
5325
Yaowu Xuc27fc142016-08-22 16:08:15 -07005326#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005327extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5328 const unsigned char *img2, int img2_pitch,
5329 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005330
5331static void adjust_image_stat(double y, double u, double v, double all,
5332 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07005333 s->stat[STAT_Y] += y;
5334 s->stat[STAT_U] += u;
5335 s->stat[STAT_V] += v;
5336 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005337 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005338}
5339
Angie Chiang08a22a62017-07-17 17:29:17 -07005340static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005341 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005342 double samples = 0.0;
5343 uint32_t in_bit_depth = 8;
5344 uint32_t bit_depth = 8;
5345
Angie Chiang08a22a62017-07-17 17:29:17 -07005346#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00005347 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005348#endif
5349 cpi->bytes += frame_bytes;
5350
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005351 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005352 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005353 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005354 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005355 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005356 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00005357 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005358 double y, u, v, frame_all;
5359
5360 cpi->count++;
5361 if (cpi->b_calculate_psnr) {
5362 PSNR_STATS psnr;
5363 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005364 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005365 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005366 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005367
5368 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5369 &cpi->psnr);
5370 cpi->total_sq_error += psnr.sse[0];
5371 cpi->total_samples += psnr.samples[0];
5372 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005373 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005374 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005375 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005376 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005377 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005378 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005379
Yaowu Xuf883b422016-08-30 14:01:10 -07005380 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005381 cpi->summed_quality += frame_ssim2 * weight;
5382 cpi->summed_weights += weight;
5383
5384#if 0
5385 {
5386 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005387 double y2 = psnr.psnr[1];
5388 double u2 = psnr.psnr[2];
5389 double v2 = psnr.psnr[3];
5390 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005391 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 +00005392 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005393 frame_psnr2, frame_ssim2);
5394 fclose(f);
5395 }
5396#endif
5397 }
5398 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005399 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005400 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005401 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5402 recon->y_stride, orig->y_width, orig->y_height);
5403 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005404 cpi->total_blockiness += frame_blockiness;
5405 }
5406
5407 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005408 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005409 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005410 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5411 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5412
5413 const double peak = (double)((1 << in_bit_depth) - 1);
5414 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005415 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005416 if (consistency > 0.0)
5417 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005418 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005419 cpi->total_inconsistency += this_inconsistency;
5420 }
5421 }
5422 }
5423
5424 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005425 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005426 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005427 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005428 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5429 }
5430}
5431#endif // CONFIG_INTERNAL_STATS
5432
Andrey Norkin795ba872018-03-06 13:24:14 -08005433int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5434 size_t *size, uint8_t *dest, int64_t *time_stamp,
5435 int64_t *time_end, int flush,
5436 const aom_rational_t *timebase) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005437 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5438 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005439
5440#if CONFIG_BITSTREAM_DEBUG
5441 assert(cpi->oxcf.max_threads == 0 &&
5442 "bitstream debug tool does not support multithreading");
5443 bitstream_queue_record_write();
logangwde8b1982019-02-25 09:47:30 -08005444 bitstream_queue_set_frame_write(cm->current_frame.frame_number * 2 +
David Turnerd2a592e2018-11-16 14:59:31 +00005445 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005446#endif
5447
Sarah Parker740e8392019-01-23 15:47:53 -08005448 // Indicates whether or not to use an adaptive quantize b rather than
5449 // the traditional version
5450 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
5451
Dominic Symesd4929012018-01-31 17:32:01 +01005452 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00005453 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005454#if CONFIG_INTERNAL_STATS
5455 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005456 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005457#endif
RogerZhou3b635242017-09-19 10:06:46 -07005458 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005459
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07005460 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07005461 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
5462 ? REFRESH_FRAME_CONTEXT_DISABLED
5463 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005464 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08005465 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005466
Sarah Parkerb9041612018-05-22 19:06:47 -07005467 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005468 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005469
David Turnerdedd8ff2019-01-23 13:59:46 +00005470 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005471
David Turnerdedd8ff2019-01-23 13:59:46 +00005472 const int result = av1_encode_strategy(cpi, size, dest, frame_flags,
5473 time_stamp, time_end, timebase, flush);
5474 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00005475 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00005476 } else if (result == -1) {
5477 // Returning -1 indicates no frame encoded; more input is required
5478 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005479 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005480#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005481 aom_usec_timer_mark(&cmptimer);
5482 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005483#endif
David Turnerc4bf8c72019-01-15 13:14:37 +00005484 if (cpi->b_calculate_psnr) {
5485 if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
5486 generate_psnr_packet(cpi);
5487 }
5488 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005489
5490#if CONFIG_INTERNAL_STATS
5491 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07005492 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005493 }
5494#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005495#if CONFIG_SPEED_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00005496 if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005497 cpi->tx_search_count += cpi->td.mb.tx_search_count;
5498 cpi->td.mb.tx_search_count = 0;
5499 }
5500#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005501
Yaowu Xuf883b422016-08-30 14:01:10 -07005502 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005503
5504 return 0;
5505}
5506
Yaowu Xuf883b422016-08-30 14:01:10 -07005507int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5508 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005509 if (!cm->show_frame) {
5510 return -1;
5511 } else {
5512 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00005513 if (cm->cur_frame != NULL) {
5514 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005515 dest->y_width = cm->width;
5516 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005517 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
5518 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005519 ret = 0;
5520 } else {
5521 ret = -1;
5522 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005523 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005524 return ret;
5525 }
5526}
5527
Yaowu Xuf883b422016-08-30 14:01:10 -07005528int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00005529 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005530
David Turnere7ebf902018-12-04 14:04:55 +00005531 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005532 return 0;
5533}
5534
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005535static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
5536 const YV12_BUFFER_CONFIG *b) {
5537 return a->y_height == b->y_height && a->y_width == b->y_width &&
5538 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
5539 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
5540 a->border == b->border &&
5541 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
5542 (b->flags & YV12_FLAG_HIGHBITDEPTH);
5543}
5544
Yunqing Wang93b18f32018-06-08 21:08:29 -07005545aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
5546 YV12_BUFFER_CONFIG *new_frame,
5547 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005548 const int num_planes = av1_num_planes(cm);
5549 if (!equal_dimensions_and_border(new_frame, sd))
5550 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
5551 "Incorrect buffer dimensions");
5552 else
5553 aom_yv12_copy_frame(new_frame, sd, num_planes);
5554
5555 return cm->error.error_code;
5556}
5557
Yaowu Xuf883b422016-08-30 14:01:10 -07005558int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
5559 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005560 int hr = 0, hs = 0, vr = 0, vs = 0;
5561
5562 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5563
5564 Scale2Ratio(horiz_mode, &hr, &hs);
5565 Scale2Ratio(vert_mode, &vr, &vs);
5566
5567 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005568 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5569 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005570
5571 return 0;
5572}
5573
Yaowu Xuf883b422016-08-30 14:01:10 -07005574int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005575
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005576int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
5577 size_t output_size = 0;
5578 size_t total_bytes_read = 0;
5579 size_t remaining_size = *frame_size;
5580 uint8_t *buff_ptr = buffer;
5581
5582 // go through each OBUs
5583 while (total_bytes_read < *frame_size) {
5584 uint8_t saved_obu_header[2];
5585 uint64_t obu_payload_size;
5586 size_t length_of_payload_size;
5587 size_t length_of_obu_size;
5588 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
5589 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
5590
5591 // save the obu header (1 or 2 bytes)
5592 memmove(saved_obu_header, buff_ptr, obu_header_size);
5593 // clear the obu_has_size_field
5594 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
5595
5596 // get the payload_size and length of payload_size
5597 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
5598 &obu_payload_size, &length_of_payload_size) != 0) {
5599 return AOM_CODEC_ERROR;
5600 }
5601 obu_bytes_read += length_of_payload_size;
5602
5603 // calculate the length of size of the obu header plus payload
5604 length_of_obu_size =
5605 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
5606
5607 // move the rest of data to new location
5608 memmove(buff_ptr + length_of_obu_size + obu_header_size,
5609 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07005610 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005611
5612 // write the new obu size
5613 const uint64_t obu_size = obu_header_size + obu_payload_size;
5614 size_t coded_obu_size;
5615 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
5616 &coded_obu_size) != 0) {
5617 return AOM_CODEC_ERROR;
5618 }
5619
5620 // write the saved (modified) obu_header following obu size
5621 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
5622
5623 total_bytes_read += obu_bytes_read;
5624 remaining_size -= obu_bytes_read;
5625 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07005626 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005627 }
5628
5629 *frame_size = output_size;
5630 return AOM_CODEC_OK;
5631}
5632
Yaowu Xuf883b422016-08-30 14:01:10 -07005633void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005634 // TODO(yunqingwang): For what references to use, external encoding flags
5635 // should be consistent with internal reference frame selection. Need to
5636 // ensure that there is not conflict between the two. In AV1 encoder, the
5637 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
5638 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
5639 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005640 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005641 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005642 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
5643 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
5644 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005645 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005646 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005647 } else {
5648 int ref = AOM_REFFRAME_ALL;
5649
5650 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
5651 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
5652
5653 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
5654
5655 if (flags & AOM_EFLAG_NO_REF_ARF) {
5656 ref ^= AOM_ALT_FLAG;
5657 ref ^= AOM_BWD_FLAG;
5658 ref ^= AOM_ALT2_FLAG;
5659 } else {
5660 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
5661 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
5662 }
5663
5664 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005665 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005666 }
5667
5668 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005669 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005670 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005671
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005672 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
5673 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005674
Yaowu Xuf883b422016-08-30 14:01:10 -07005675 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005676
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005677 if (flags & AOM_EFLAG_NO_UPD_ARF) {
5678 upd ^= AOM_ALT_FLAG;
5679 upd ^= AOM_BWD_FLAG;
5680 upd ^= AOM_ALT2_FLAG;
5681 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005682
David Turnerce9b5902019-01-23 17:25:47 +00005683 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
5684 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
5685 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
5686 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
5687 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
5688 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00005689 } else {
5690 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005691 }
5692
sarahparker21dbca42018-03-30 17:43:44 -07005693 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
5694 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07005695 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
5696 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07005697 cpi->ext_use_s_frame =
5698 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07005699 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07005700
Yaowu Xuf883b422016-08-30 14:01:10 -07005701 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
5702 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005703 }
5704}
Andrey Norkin795ba872018-03-06 13:24:14 -08005705
Tom Fineganf8d6a162018-08-21 10:47:55 -07005706aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
5707 if (!cpi) return NULL;
5708
5709 uint8_t header_buf[512] = { 0 };
5710 const uint32_t sequence_header_size =
5711 write_sequence_header_obu(cpi, &header_buf[0]);
5712 assert(sequence_header_size <= sizeof(header_buf));
5713 if (sequence_header_size == 0) return NULL;
5714
5715 const size_t obu_header_size = 1;
5716 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
5717 const size_t payload_offset = obu_header_size + size_field_size;
5718
5719 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
5720 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
5721
5722 if (write_obu_header(OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
5723 obu_header_size) {
5724 return NULL;
5725 }
5726
5727 size_t coded_size_field_size = 0;
5728 if (aom_uleb_encode(sequence_header_size, size_field_size,
5729 &header_buf[obu_header_size],
5730 &coded_size_field_size) != 0) {
5731 return NULL;
5732 }
5733 assert(coded_size_field_size == size_field_size);
5734
5735 aom_fixed_buf_t *global_headers =
5736 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
5737 if (!global_headers) return NULL;
5738
5739 const size_t global_header_buf_size =
5740 obu_header_size + size_field_size + sequence_header_size;
5741
5742 global_headers->buf = malloc(global_header_buf_size);
5743 if (!global_headers->buf) {
5744 free(global_headers);
5745 return NULL;
5746 }
5747
5748 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
5749 global_headers->sz = global_header_buf_size;
5750 return global_headers;
5751}