blob: 40ce05b7887692883adba3e41fa51c2d0a482882 [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 Birkbecka2893ab2018-06-08 14:45:13 -0700509 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
510 memset(cpi->film_grain_table, 0, sizeof(aom_film_grain_table_t));
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700511
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700512 aom_film_grain_table_read(cpi->film_grain_table,
Neil Birkbeckeb895ef2018-03-14 17:51:03 -0700513 oxcf->film_grain_table_filename, &cm->error);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800514 } else {
Urvang Joshi8d5a4ba2018-07-19 16:26:34 -0700515 cm->seq_params.film_grain_params_present = 0;
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800516 memset(&cm->film_grain_params, 0, sizeof(cm->film_grain_params));
517 }
518}
Andrey Norkin6f1c2f72018-01-15 20:08:52 -0800519
Yaowu Xuf883b422016-08-30 14:01:10 -0700520static void dealloc_compressor_data(AV1_COMP *cpi) {
521 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000522 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700523
Debargha Mukherjeeccb27262017-09-25 14:19:46 -0700524 dealloc_context_buffers_ext(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700525
Yaowu Xuf883b422016-08-30 14:01:10 -0700526 aom_free(cpi->tile_data);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700527 cpi->tile_data = NULL;
528
529 // Delete sementation map
Yaowu Xuf883b422016-08-30 14:01:10 -0700530 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700531 cpi->segmentation_map = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700532
Yaowu Xuf883b422016-08-30 14:01:10 -0700533 av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700534 cpi->cyclic_refresh = NULL;
535
Yaowu Xuf883b422016-08-30 14:01:10 -0700536 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700537 cpi->active_map.map = NULL;
538
Jingning Hand064cf02017-06-01 10:00:39 -0700539 aom_free(cpi->td.mb.above_pred_buf);
540 cpi->td.mb.above_pred_buf = NULL;
541
542 aom_free(cpi->td.mb.left_pred_buf);
543 cpi->td.mb.left_pred_buf = NULL;
544
545 aom_free(cpi->td.mb.wsrc_buf);
546 cpi->td.mb.wsrc_buf = NULL;
547
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530548 aom_free(cpi->td.mb.inter_modes_info);
549 cpi->td.mb.inter_modes_info = NULL;
Ravi Chaudhary5d970f42018-09-25 11:25:32 +0530550
Ravi Chaudhary783d6a32018-08-28 18:21:02 +0530551 for (int i = 0; i < 2; i++)
552 for (int j = 0; j < 2; j++) {
553 aom_free(cpi->td.mb.hash_value_buffer[i][j]);
554 cpi->td.mb.hash_value_buffer[i][j] = NULL;
555 }
Jingning Hand064cf02017-06-01 10:00:39 -0700556 aom_free(cpi->td.mb.mask_buf);
557 cpi->td.mb.mask_buf = NULL;
Jingning Hand064cf02017-06-01 10:00:39 -0700558
Jingning Han6cc1fd32017-10-13 09:05:36 -0700559 aom_free(cm->tpl_mvs);
560 cm->tpl_mvs = NULL;
Jingning Han6cc1fd32017-10-13 09:05:36 -0700561
Yaowu Xuf883b422016-08-30 14:01:10 -0700562 av1_free_ref_frame_buffers(cm->buffer_pool);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700563 av1_free_txb_buf(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -0700564 av1_free_context_buffers(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700565
Yaowu Xuf883b422016-08-30 14:01:10 -0700566 aom_free_frame_buffer(&cpi->last_frame_uf);
Yaowu Xuf883b422016-08-30 14:01:10 -0700567 av1_free_restoration_buffers(cm);
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800568 aom_free_frame_buffer(&cpi->trial_frame_rst);
Yaowu Xuf883b422016-08-30 14:01:10 -0700569 aom_free_frame_buffer(&cpi->scaled_source);
570 aom_free_frame_buffer(&cpi->scaled_last_source);
571 aom_free_frame_buffer(&cpi->alt_ref_buffer);
572 av1_lookahead_destroy(cpi->lookahead);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700573
Yaowu Xuf883b422016-08-30 14:01:10 -0700574 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700575 cpi->tile_tok[0][0] = 0;
576
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530577 aom_free(cpi->tplist[0][0]);
578 cpi->tplist[0][0] = NULL;
579
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000580 av1_free_pc_tree(&cpi->td, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700581
hui sud9a812b2017-07-06 14:34:37 -0700582 aom_free(cpi->td.mb.palette_buffer);
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700583
Urvang Joshi0a4cfad2018-09-07 11:10:39 -0700584 aom_free(cpi->td.mb.tmp_conv_dst);
585 for (int j = 0; j < 2; ++j) {
586 aom_free(cpi->td.mb.tmp_obmc_bufs[j]);
587 }
588
Neil Birkbecka2893ab2018-06-08 14:45:13 -0700589#if CONFIG_DENOISE
590 if (cpi->denoise_and_model) {
591 aom_denoise_and_model_free(cpi->denoise_and_model);
592 cpi->denoise_and_model = NULL;
593 }
594#endif
595 if (cpi->film_grain_table) {
596 aom_film_grain_table_free(cpi->film_grain_table);
597 cpi->film_grain_table = NULL;
598 }
Yaowu Xuc27fc142016-08-22 16:08:15 -0700599}
600
Yaowu Xuf883b422016-08-30 14:01:10 -0700601static void save_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700602 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700603 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700604
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200605 // Stores a snapshot of key state variables which can subsequently be
606 // restored with a call to av1_restore_coding_context. These functions are
607 // intended for use in a re-code loop in av1_compress_frame where the
608 // quantizer value is adjusted between loop iterations.
Jingning Hanf050fc12018-03-09 14:53:33 -0800609 av1_copy(cc->nmv_vec_cost, cpi->td.mb.nmv_vec_cost);
610 av1_copy(cc->nmv_costs, cpi->nmv_costs);
611 av1_copy(cc->nmv_costs_hp, cpi->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000612
Yaowu Xuc27fc142016-08-22 16:08:15 -0700613 cc->fc = *cm->fc;
614}
615
Yaowu Xuf883b422016-08-30 14:01:10 -0700616static void restore_coding_context(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700617 CODING_CONTEXT *const cc = &cpi->coding_context;
Yaowu Xuf883b422016-08-30 14:01:10 -0700618 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700619
Sebastien Alaiwane140c502017-04-27 09:52:34 +0200620 // Restore key state variables to the snapshot state stored in the
621 // previous call to av1_save_coding_context.
Jingning Hanf050fc12018-03-09 14:53:33 -0800622 av1_copy(cpi->td.mb.nmv_vec_cost, cc->nmv_vec_cost);
623 av1_copy(cpi->nmv_costs, cc->nmv_costs);
624 av1_copy(cpi->nmv_costs_hp, cc->nmv_costs_hp);
James Zern01a9d702017-08-25 19:09:33 +0000625
Yaowu Xuc27fc142016-08-22 16:08:15 -0700626 *cm->fc = cc->fc;
627}
628
Yaowu Xuf883b422016-08-30 14:01:10 -0700629static void configure_static_seg_features(AV1_COMP *cpi) {
630 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700631 const RATE_CONTROL *const rc = &cpi->rc;
632 struct segmentation *const seg = &cm->seg;
633
634 int high_q = (int)(rc->avg_q > 48.0);
635 int qi_delta;
636
637 // Disable and clear down for KF
David Turnerd2a592e2018-11-16 14:59:31 +0000638 if (cm->current_frame.frame_type == KEY_FRAME) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700639 // Clear down the global segmentation map
640 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
641 seg->update_map = 0;
642 seg->update_data = 0;
643 cpi->static_mb_pct = 0;
644
645 // Disable segmentation
Yaowu Xuf883b422016-08-30 14:01:10 -0700646 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700647
648 // Clear down the segment features.
Yaowu Xuf883b422016-08-30 14:01:10 -0700649 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700650 } else if (cpi->refresh_alt_ref_frame) {
651 // If this is an alt ref frame
652 // Clear down the global segmentation map
653 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
654 seg->update_map = 0;
655 seg->update_data = 0;
656 cpi->static_mb_pct = 0;
657
658 // Disable segmentation and individual segment features by default
Yaowu Xuf883b422016-08-30 14:01:10 -0700659 av1_disable_segmentation(seg);
660 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700661
662 // Scan frames from current to arf frame.
663 // This function re-enables segmentation if appropriate.
Yaowu Xuf883b422016-08-30 14:01:10 -0700664 av1_update_mbgraph_stats(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700665
666 // If segmentation was enabled set those features needed for the
667 // arf itself.
668 if (seg->enabled) {
669 seg->update_map = 1;
670 seg->update_data = 1;
671
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700672 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 0.875,
673 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700674 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta - 2);
Cheng Chend8184da2017-09-26 18:15:22 -0700675 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
676 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
677 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
678 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
679
680 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
681 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
682 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
683 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700684
Yaowu Xuf883b422016-08-30 14:01:10 -0700685 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700686 }
687 } else if (seg->enabled) {
688 // All other frames if segmentation has been enabled
689
690 // First normal frame in a valid gf or alt ref group
691 if (rc->frames_since_golden == 0) {
692 // Set up segment features for normal frames in an arf group
693 if (rc->source_alt_ref_active) {
694 seg->update_map = 0;
695 seg->update_data = 1;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700696
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700697 qi_delta = av1_compute_qdelta(rc, rc->avg_q, rc->avg_q * 1.125,
698 cm->seq_params.bit_depth);
Yaowu Xuf883b422016-08-30 14:01:10 -0700699 av1_set_segdata(seg, 1, SEG_LVL_ALT_Q, qi_delta + 2);
700 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_Q);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700701
Cheng Chend8184da2017-09-26 18:15:22 -0700702 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_H, -2);
703 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_Y_V, -2);
704 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_U, -2);
705 av1_set_segdata(seg, 1, SEG_LVL_ALT_LF_V, -2);
706
707 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_H);
708 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_Y_V);
709 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_U);
710 av1_enable_segfeature(seg, 1, SEG_LVL_ALT_LF_V);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700711
712 // Segment coding disabled for compred testing
713 if (high_q || (cpi->static_mb_pct == 100)) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700714 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
715 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
716 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700717 }
718 } else {
719 // Disable segmentation and clear down features if alt ref
720 // is not active for this group
721
Yaowu Xuf883b422016-08-30 14:01:10 -0700722 av1_disable_segmentation(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700723
724 memset(cpi->segmentation_map, 0, cm->mi_rows * cm->mi_cols);
725
726 seg->update_map = 0;
727 seg->update_data = 0;
728
Yaowu Xuf883b422016-08-30 14:01:10 -0700729 av1_clearall_segfeatures(seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700730 }
731 } else if (rc->is_src_frame_alt_ref) {
732 // Special case where we are coding over the top of a previous
733 // alt ref frame.
734 // Segment coding disabled for compred testing
735
736 // Enable ref frame features for segment 0 as well
Yaowu Xuf883b422016-08-30 14:01:10 -0700737 av1_enable_segfeature(seg, 0, SEG_LVL_REF_FRAME);
738 av1_enable_segfeature(seg, 1, SEG_LVL_REF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700739
740 // All mbs should use ALTREF_FRAME
Yaowu Xuf883b422016-08-30 14:01:10 -0700741 av1_clear_segdata(seg, 0, SEG_LVL_REF_FRAME);
742 av1_set_segdata(seg, 0, SEG_LVL_REF_FRAME, ALTREF_FRAME);
743 av1_clear_segdata(seg, 1, SEG_LVL_REF_FRAME);
744 av1_set_segdata(seg, 1, SEG_LVL_REF_FRAME, ALTREF_FRAME);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700745
746 // Skip all MBs if high Q (0,0 mv and skip coeffs)
747 if (high_q) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700748 av1_enable_segfeature(seg, 0, SEG_LVL_SKIP);
749 av1_enable_segfeature(seg, 1, SEG_LVL_SKIP);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700750 }
751 // Enable data update
752 seg->update_data = 1;
753 } else {
754 // All other frames.
755
756 // No updates.. leave things as they are.
757 seg->update_map = 0;
758 seg->update_data = 0;
759 }
760 }
761}
762
Yaowu Xuf883b422016-08-30 14:01:10 -0700763static void update_reference_segmentation_map(AV1_COMP *cpi) {
764 AV1_COMMON *const cm = &cpi->common;
Yushin Choa7f65922018-04-04 16:06:11 -0700765 MB_MODE_INFO **mi_4x4_ptr = cm->mi_grid_visible;
David Turnerb757ce02018-11-12 15:01:28 +0000766 uint8_t *cache_ptr = cm->cur_frame->seg_map;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700767 int row, col;
768
769 for (row = 0; row < cm->mi_rows; row++) {
Yushin Choa7f65922018-04-04 16:06:11 -0700770 MB_MODE_INFO **mi_4x4 = mi_4x4_ptr;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700771 uint8_t *cache = cache_ptr;
Yushin Choa7f65922018-04-04 16:06:11 -0700772 for (col = 0; col < cm->mi_cols; col++, mi_4x4++, cache++)
773 cache[0] = mi_4x4[0]->segment_id;
774 mi_4x4_ptr += cm->mi_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700775 cache_ptr += cm->mi_cols;
776 }
777}
778
Yaowu Xuf883b422016-08-30 14:01:10 -0700779static void alloc_raw_frame_buffers(AV1_COMP *cpi) {
780 AV1_COMMON *cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700781 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuf883b422016-08-30 14:01:10 -0700782 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700783
784 if (!cpi->lookahead)
Satish Kumar Suman29909962019-01-09 10:31:21 +0530785 cpi->lookahead = av1_lookahead_init(
786 oxcf->width, oxcf->height, seq_params->subsampling_x,
787 seq_params->subsampling_y, seq_params->use_highbitdepth,
788 oxcf->lag_in_frames, oxcf->border_in_pixels);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700789 if (!cpi->lookahead)
Yaowu Xuf883b422016-08-30 14:01:10 -0700790 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700791 "Failed to allocate lag buffers");
792
793 // TODO(agrange) Check if ARF is enabled and skip allocation if not.
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700794 if (aom_realloc_frame_buffer(
795 &cpi->alt_ref_buffer, oxcf->width, oxcf->height,
796 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530797 seq_params->use_highbitdepth, oxcf->border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700798 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700799 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700800 "Failed to allocate altref buffer");
801}
802
Yaowu Xuf883b422016-08-30 14:01:10 -0700803static void alloc_util_frame_buffers(AV1_COMP *cpi) {
804 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700805 const SequenceHeader *const seq_params = &cm->seq_params;
806 if (aom_realloc_frame_buffer(
807 &cpi->last_frame_uf, cm->width, cm->height, seq_params->subsampling_x,
808 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530809 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700810 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700811 "Failed to allocate last frame buffer");
812
Fergus Simpson9cd57cf2017-06-12 17:02:03 -0700813 if (aom_realloc_frame_buffer(
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -0800814 &cpi->trial_frame_rst, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700815 cm->superres_upscaled_height, seq_params->subsampling_x,
816 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman3b12c002018-12-19 15:27:20 +0530817 AOM_RESTORATION_FRAME_BORDER, cm->byte_alignment, NULL, NULL, NULL))
Debargha Mukherjee874d36d2016-12-14 16:53:17 -0800818 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Debargha Mukherjee999d2f62016-12-15 13:23:21 -0800819 "Failed to allocate trial restored frame buffer");
Yaowu Xuc27fc142016-08-22 16:08:15 -0700820
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700821 if (aom_realloc_frame_buffer(
822 &cpi->scaled_source, cm->width, cm->height, seq_params->subsampling_x,
823 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530824 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700825 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700826 "Failed to allocate scaled source buffer");
827
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700828 if (aom_realloc_frame_buffer(
829 &cpi->scaled_last_source, cm->width, cm->height,
830 seq_params->subsampling_x, seq_params->subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +0530831 seq_params->use_highbitdepth, cpi->oxcf.border_in_pixels,
Urvang Joshi20cf30e2018-07-19 02:33:58 -0700832 cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -0700833 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -0700834 "Failed to allocate scaled last source buffer");
835}
836
Cheng Chen46f30c72017-09-07 11:13:33 -0700837static void alloc_compressor_data(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -0700838 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000839 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700840
Yaowu Xuf883b422016-08-30 14:01:10 -0700841 av1_alloc_context_buffers(cm, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700842
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530843 int mi_rows_aligned_to_sb =
844 ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
845 int sb_rows = mi_rows_aligned_to_sb >> cm->seq_params.mib_size_log2;
846
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700847 av1_alloc_txb_buf(cpi);
Angie Chiangf0fbf9d2017-03-15 15:01:22 -0700848
Yaowu Xuc27fc142016-08-22 16:08:15 -0700849 alloc_context_buffers_ext(cpi);
850
Yaowu Xuf883b422016-08-30 14:01:10 -0700851 aom_free(cpi->tile_tok[0][0]);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700852
853 {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +0000854 unsigned int tokens =
855 get_token_alloc(cm->mb_rows, cm->mb_cols, MAX_SB_SIZE_LOG2, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700856 CHECK_MEM_ERROR(cm, cpi->tile_tok[0][0],
Yaowu Xuf883b422016-08-30 14:01:10 -0700857 aom_calloc(tokens, sizeof(*cpi->tile_tok[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700858 }
Ravi Chaudhary73cf15b2018-08-30 10:52:51 +0530859 aom_free(cpi->tplist[0][0]);
860
861 CHECK_MEM_ERROR(cm, cpi->tplist[0][0],
862 aom_calloc(sb_rows * MAX_TILE_ROWS * MAX_TILE_COLS,
863 sizeof(*cpi->tplist[0][0])));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700864
Yaowu Xuf883b422016-08-30 14:01:10 -0700865 av1_setup_pc_tree(&cpi->common, &cpi->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700866}
867
Yaowu Xuf883b422016-08-30 14:01:10 -0700868void av1_new_framerate(AV1_COMP *cpi, double framerate) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700869 cpi->framerate = framerate < 0.1 ? 30 : framerate;
Debargha Mukherjee7166f222017-09-05 21:32:42 -0700870 av1_rc_update_framerate(cpi, cpi->common.width, cpi->common.height);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700871}
872
Yunqing Wang75e20e82018-06-16 12:10:48 -0700873static void set_tile_info(AV1_COMP *cpi) {
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200874 AV1_COMMON *const cm = &cpi->common;
Dominic Symesf58f1112017-09-25 12:47:40 +0200875 int i, start_sb;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200876
877 av1_get_tile_limits(cm);
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200878
879 // configure tile columns
Dominic Symes26ad0b22017-10-01 16:35:13 +0200880 if (cpi->oxcf.tile_width_count == 0 || cpi->oxcf.tile_height_count == 0) {
Dominic Symesf58f1112017-09-25 12:47:40 +0200881 cm->uniform_tile_spacing_flag = 1;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200882 cm->log2_tile_cols = AOMMAX(cpi->oxcf.tile_columns, cm->min_log2_tile_cols);
883 cm->log2_tile_cols = AOMMIN(cm->log2_tile_cols, cm->max_log2_tile_cols);
Dominic Symesf58f1112017-09-25 12:47:40 +0200884 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000885 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, cm->seq_params.mib_size_log2);
886 int sb_cols = mi_cols >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200887 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200888 cm->uniform_tile_spacing_flag = 0;
889 for (i = 0, start_sb = 0; start_sb < sb_cols && i < MAX_TILE_COLS; i++) {
890 cm->tile_col_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200891 size_sb = cpi->oxcf.tile_widths[j++];
892 if (j >= cpi->oxcf.tile_width_count) j = 0;
David Barker6cd5a822018-03-05 16:19:28 +0000893 start_sb += AOMMIN(size_sb, cm->max_tile_width_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200894 }
895 cm->tile_cols = i;
896 cm->tile_col_start_sb[i] = sb_cols;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200897 }
898 av1_calculate_tile_cols(cm);
899
900 // configure tile rows
901 if (cm->uniform_tile_spacing_flag) {
902 cm->log2_tile_rows = AOMMAX(cpi->oxcf.tile_rows, cm->min_log2_tile_rows);
903 cm->log2_tile_rows = AOMMIN(cm->log2_tile_rows, cm->max_log2_tile_rows);
Dominic Symesf58f1112017-09-25 12:47:40 +0200904 } else {
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +0000905 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, cm->seq_params.mib_size_log2);
906 int sb_rows = mi_rows >> cm->seq_params.mib_size_log2;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200907 int size_sb, j = 0;
Dominic Symesf58f1112017-09-25 12:47:40 +0200908 for (i = 0, start_sb = 0; start_sb < sb_rows && i < MAX_TILE_ROWS; i++) {
909 cm->tile_row_start_sb[i] = start_sb;
Dominic Symes26ad0b22017-10-01 16:35:13 +0200910 size_sb = cpi->oxcf.tile_heights[j++];
911 if (j >= cpi->oxcf.tile_height_count) j = 0;
912 start_sb += AOMMIN(size_sb, cm->max_tile_height_sb);
Dominic Symesf58f1112017-09-25 12:47:40 +0200913 }
914 cm->tile_rows = i;
915 cm->tile_row_start_sb[i] = sb_rows;
Dominic Symesdb5d66f2017-08-18 18:11:34 +0200916 }
917 av1_calculate_tile_rows(cm);
918}
919
Yaowu Xuf883b422016-08-30 14:01:10 -0700920static void update_frame_size(AV1_COMP *cpi) {
921 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700922 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
923
Yaowu Xuf883b422016-08-30 14:01:10 -0700924 av1_set_mb_mi(cm, cm->width, cm->height);
925 av1_init_context_buffers(cm);
Luc Trudeau1e84af52017-11-25 15:00:28 -0500926 av1_init_macroblockd(cm, xd, NULL);
Yaowu Xuc27fc142016-08-22 16:08:15 -0700927 memset(cpi->mbmi_ext_base, 0,
928 cm->mi_rows * cm->mi_cols * sizeof(*cpi->mbmi_ext_base));
Yaowu Xuc27fc142016-08-22 16:08:15 -0700929 set_tile_info(cpi);
930}
931
Yaowu Xuf883b422016-08-30 14:01:10 -0700932static void init_buffer_indices(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -0700933 int fb_idx;
Zoe Liu5989a722018-03-29 13:37:36 -0700934 for (fb_idx = 0; fb_idx < REF_FRAMES; ++fb_idx)
David Turnera21966b2018-12-05 14:48:49 +0000935 cpi->common.remapped_ref_idx[fb_idx] = fb_idx;
RogerZhou3b635242017-09-19 10:06:46 -0700936 cpi->rate_index = 0;
937 cpi->rate_size = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -0700938}
939
Debargha Mukherjee57498692018-05-11 13:29:31 -0700940static INLINE int does_level_match(int width, int height, double fps,
941 int lvl_width, int lvl_height,
942 double lvl_fps, int lvl_dim_mult) {
943 const int64_t lvl_luma_pels = lvl_width * lvl_height;
944 const double lvl_display_sample_rate = lvl_luma_pels * lvl_fps;
945 const int64_t luma_pels = width * height;
946 const double display_sample_rate = luma_pels * fps;
947 return luma_pels <= lvl_luma_pels &&
948 display_sample_rate <= lvl_display_sample_rate &&
949 width <= lvl_width * lvl_dim_mult &&
950 height <= lvl_height * lvl_dim_mult;
951}
952
Andrey Norkin26495512018-06-20 17:13:11 -0700953static void set_bitstream_level_tier(SequenceHeader *seq, AV1_COMMON *cm,
Andrey Norkinf481d982018-05-15 12:05:31 -0700954 const AV1EncoderConfig *oxcf) {
Debargha Mukherjee57498692018-05-11 13:29:31 -0700955 // TODO(any): This is a placeholder function that only addresses dimensions
956 // and max display sample rates.
957 // Need to add checks for max bit rate, max decoded luma sample rate, header
958 // rate, etc. that are not covered by this function.
Debargha Mukherjeeea675402018-05-10 16:10:41 -0700959 (void)oxcf;
Debargha Mukherjee57498692018-05-11 13:29:31 -0700960 BitstreamLevel bl = { 9, 3 };
961 if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate, 512,
962 288, 30.0, 4)) {
963 bl.major = 2;
964 bl.minor = 0;
965 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
966 704, 396, 30.0, 4)) {
967 bl.major = 2;
968 bl.minor = 1;
969 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
970 1088, 612, 30.0, 4)) {
971 bl.major = 3;
972 bl.minor = 0;
973 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
974 1376, 774, 30.0, 4)) {
975 bl.major = 3;
976 bl.minor = 1;
977 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
978 2048, 1152, 30.0, 3)) {
979 bl.major = 4;
980 bl.minor = 0;
981 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
982 2048, 1152, 60.0, 3)) {
983 bl.major = 4;
984 bl.minor = 1;
985 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
986 4096, 2176, 30.0, 2)) {
987 bl.major = 5;
988 bl.minor = 0;
989 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
990 4096, 2176, 60.0, 2)) {
991 bl.major = 5;
992 bl.minor = 1;
993 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
994 4096, 2176, 120.0, 2)) {
995 bl.major = 5;
996 bl.minor = 2;
997 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
998 8192, 4352, 30.0, 2)) {
999 bl.major = 6;
1000 bl.minor = 0;
1001 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1002 8192, 4352, 60.0, 2)) {
1003 bl.major = 6;
1004 bl.minor = 1;
1005 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1006 8192, 4352, 120.0, 2)) {
1007 bl.major = 6;
1008 bl.minor = 2;
1009 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1010 16384, 8704, 30.0, 2)) {
1011 bl.major = 7;
1012 bl.minor = 0;
1013 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1014 16384, 8704, 60.0, 2)) {
1015 bl.major = 7;
1016 bl.minor = 1;
1017 } else if (does_level_match(oxcf->width, oxcf->height, oxcf->init_framerate,
1018 16384, 8704, 120.0, 2)) {
1019 bl.major = 7;
1020 bl.minor = 2;
1021 }
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001022 for (int i = 0; i < MAX_NUM_OPERATING_POINTS; ++i) {
Debargha Mukherjee57498692018-05-11 13:29:31 -07001023 seq->level[i] = bl;
Andrey Norkinf481d982018-05-15 12:05:31 -07001024 seq->tier[i] = 0; // setting main tier by default
Andrey Norkin26495512018-06-20 17:13:11 -07001025 // Set the maximum parameters for bitrate and buffer size for this profile,
1026 // level, and tier
1027 cm->op_params[i].bitrate = max_level_bitrate(
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001028 cm->seq_params.profile, major_minor_to_seq_level_idx(seq->level[i]),
1029 seq->tier[i]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001030 // Level with seq_level_idx = 31 returns a high "dummy" bitrate to pass the
1031 // check
Andrey Norkin26495512018-06-20 17:13:11 -07001032 if (cm->op_params[i].bitrate == 0)
1033 aom_internal_error(
1034 &cm->error, AOM_CODEC_UNSUP_BITSTREAM,
1035 "AV1 does not support this combination of profile, level, and tier.");
Andrey Norkinc7511de2018-06-22 12:31:06 -07001036 // Buffer size in bits/s is bitrate in bits/s * 1 s
Andrey Norkin26495512018-06-20 17:13:11 -07001037 cm->op_params[i].buffer_size = cm->op_params[i].bitrate;
Debargha Mukherjeeea675402018-05-10 16:10:41 -07001038 }
1039}
1040
Andrey Norkin26495512018-06-20 17:13:11 -07001041static void init_seq_coding_tools(SequenceHeader *seq, AV1_COMMON *cm,
1042 const AV1EncoderConfig *oxcf) {
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001043 seq->still_picture = (oxcf->limit == 1);
1044 seq->reduced_still_picture_hdr = seq->still_picture;
Debargha Mukherjee9713ccb2018-04-08 19:09:17 -07001045 seq->reduced_still_picture_hdr &= !oxcf->full_still_picture_hdr;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001046 seq->force_screen_content_tools = 2;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001047 seq->force_integer_mv = 2;
David Turnerebf96f42018-11-14 16:57:57 +00001048 seq->order_hint_info.enable_order_hint = oxcf->enable_order_hint;
David Turner936235c2018-11-28 13:42:01 +00001049 seq->frame_id_numbers_present_flag =
1050 !(seq->still_picture && seq->reduced_still_picture_hdr) &&
1051 !oxcf->large_scale_tile && oxcf->error_resilient_mode;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001052 if (seq->still_picture && seq->reduced_still_picture_hdr) {
David Turnerebf96f42018-11-14 16:57:57 +00001053 seq->order_hint_info.enable_order_hint = 0;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001054 seq->force_screen_content_tools = 2;
1055 seq->force_integer_mv = 2;
1056 }
David Turnerebf96f42018-11-14 16:57:57 +00001057 seq->order_hint_info.order_hint_bits_minus_1 =
1058 seq->order_hint_info.enable_order_hint
1059 ? DEFAULT_EXPLICIT_ORDER_HINT_BITS - 1
1060 : -1;
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001061
David Turner760a2f42018-12-07 15:25:36 +00001062 seq->max_frame_width =
1063 oxcf->forced_max_frame_width ? oxcf->forced_max_frame_width : oxcf->width;
1064 seq->max_frame_height = oxcf->forced_max_frame_height
1065 ? oxcf->forced_max_frame_height
1066 : oxcf->height;
1067 seq->num_bits_width =
1068 (seq->max_frame_width > 1) ? get_msb(seq->max_frame_width - 1) + 1 : 1;
1069 seq->num_bits_height =
1070 (seq->max_frame_height > 1) ? get_msb(seq->max_frame_height - 1) + 1 : 1;
1071 assert(seq->num_bits_width <= 16);
1072 assert(seq->num_bits_height <= 16);
1073
1074 seq->frame_id_length = FRAME_ID_LENGTH;
1075 seq->delta_frame_id_length = DELTA_FRAME_ID_LENGTH;
1076
Debargha Mukherjeec6f24c22018-04-07 08:43:08 -07001077 seq->enable_dual_filter = oxcf->enable_dual_filter;
Debargha Mukherjee7ac3eb12018-12-12 10:26:50 -08001078 seq->order_hint_info.enable_dist_wtd_comp = oxcf->enable_dist_wtd_comp;
1079 seq->order_hint_info.enable_dist_wtd_comp &=
David Turnerebf96f42018-11-14 16:57:57 +00001080 seq->order_hint_info.enable_order_hint;
1081 seq->order_hint_info.enable_ref_frame_mvs = oxcf->enable_ref_frame_mvs;
1082 seq->order_hint_info.enable_ref_frame_mvs &=
1083 seq->order_hint_info.enable_order_hint;
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001084 seq->enable_superres = oxcf->enable_superres;
1085 seq->enable_cdef = oxcf->enable_cdef;
1086 seq->enable_restoration = oxcf->enable_restoration;
Debargha Mukherjee37df9162018-03-25 12:48:24 -07001087 seq->enable_warped_motion = oxcf->enable_warped_motion;
Debargha Mukherjee16ea6ba2018-12-10 12:01:38 -08001088 seq->enable_interintra_compound = oxcf->enable_interintra_comp;
1089 seq->enable_masked_compound = oxcf->enable_masked_comp;
Debargha Mukherjee03c43ba2018-12-14 13:08:08 -08001090 seq->enable_intra_edge_filter = oxcf->enable_intra_edge_filter;
Yue Chen8f9ca582018-12-12 15:11:47 -08001091 seq->enable_filter_intra = oxcf->enable_filter_intra;
Debargha Mukherjee57498692018-05-11 13:29:31 -07001092
Andrey Norkin26495512018-06-20 17:13:11 -07001093 set_bitstream_level_tier(seq, cm, oxcf);
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001094
1095 if (seq->operating_points_cnt_minus_1 == 0) {
1096 seq->operating_point_idc[0] = 0;
1097 } else {
1098 // Set operating_point_idc[] such that for the i-th operating point the
1099 // first (operating_points_cnt-i) spatial layers and the first temporal
1100 // layer are decoded Note that highest quality operating point should come
1101 // first
1102 for (int i = 0; i < seq->operating_points_cnt_minus_1 + 1; i++)
1103 seq->operating_point_idc[i] =
1104 (~(~0u << (seq->operating_points_cnt_minus_1 + 1 - i)) << 8) | 1;
1105 }
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07001106}
1107
Yaowu Xuf883b422016-08-30 14:01:10 -07001108static void init_config(struct AV1_COMP *cpi, AV1EncoderConfig *oxcf) {
1109 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001110
1111 cpi->oxcf = *oxcf;
1112 cpi->framerate = oxcf->init_framerate;
1113
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001114 cm->seq_params.profile = oxcf->profile;
1115 cm->seq_params.bit_depth = oxcf->bit_depth;
1116 cm->seq_params.use_highbitdepth = oxcf->use_highbitdepth;
1117 cm->seq_params.color_primaries = oxcf->color_primaries;
1118 cm->seq_params.transfer_characteristics = oxcf->transfer_characteristics;
1119 cm->seq_params.matrix_coefficients = oxcf->matrix_coefficients;
Debargha Mukherjeef340fec2018-01-10 18:12:22 -08001120 cm->seq_params.monochrome = oxcf->monochrome;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001121 cm->seq_params.chroma_sample_position = oxcf->chroma_sample_position;
1122 cm->seq_params.color_range = oxcf->color_range;
Andrey Norkin28e9ce22018-01-08 10:11:21 -08001123 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08001124 cm->timing_info.num_units_in_display_tick =
1125 oxcf->timing_info.num_units_in_display_tick;
1126 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
1127 cm->timing_info.equal_picture_interval =
1128 oxcf->timing_info.equal_picture_interval;
1129 cm->timing_info.num_ticks_per_picture =
1130 oxcf->timing_info.num_ticks_per_picture;
1131
Andrey Norkin26495512018-06-20 17:13:11 -07001132 cm->seq_params.display_model_info_present_flag =
1133 oxcf->display_model_info_present_flag;
Adrian Grangec56f6ec2018-05-31 14:19:32 -07001134 cm->seq_params.decoder_model_info_present_flag =
1135 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08001136 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07001137 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08001138 cm->buffer_model.num_units_in_decoding_tick =
1139 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07001140 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08001141 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07001142 set_dec_model_op_parameters(&cm->op_params[0]);
1143 } else if (cm->timing_info_present &&
1144 cm->timing_info.equal_picture_interval &&
1145 !cm->seq_params.decoder_model_info_present_flag) {
1146 // set the decoder model parameters in resource availability mode
1147 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07001148 } else {
1149 cm->op_params[0].initial_display_delay =
1150 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08001151 }
Andrey Norkinc7511de2018-06-22 12:31:06 -07001152
Tom Fineganf8d6a162018-08-21 10:47:55 -07001153 if (cm->seq_params.monochrome) {
1154 cm->seq_params.subsampling_x = 1;
1155 cm->seq_params.subsampling_y = 1;
1156 } else if (cm->seq_params.color_primaries == AOM_CICP_CP_BT_709 &&
1157 cm->seq_params.transfer_characteristics == AOM_CICP_TC_SRGB &&
1158 cm->seq_params.matrix_coefficients == AOM_CICP_MC_IDENTITY) {
1159 cm->seq_params.subsampling_x = 0;
1160 cm->seq_params.subsampling_y = 0;
1161 } else {
1162 if (cm->seq_params.profile == 0) {
1163 cm->seq_params.subsampling_x = 1;
1164 cm->seq_params.subsampling_y = 1;
1165 } else if (cm->seq_params.profile == 1) {
1166 cm->seq_params.subsampling_x = 0;
1167 cm->seq_params.subsampling_y = 0;
1168 } else {
1169 if (cm->seq_params.bit_depth == AOM_BITS_12) {
1170 cm->seq_params.subsampling_x = oxcf->chroma_subsampling_x;
1171 cm->seq_params.subsampling_y = oxcf->chroma_subsampling_y;
1172 } else {
1173 cm->seq_params.subsampling_x = 1;
1174 cm->seq_params.subsampling_y = 0;
1175 }
1176 }
Tom Finegan02b2a842018-08-24 13:50:00 -07001177 }
1178
Yaowu Xuc27fc142016-08-22 16:08:15 -07001179 cm->width = oxcf->width;
1180 cm->height = oxcf->height;
Imdad Sardharwalla4ec84ab2018-02-06 12:20:18 +00001181 set_sb_size(&cm->seq_params,
1182 select_sb_size(cpi)); // set sb size before allocations
Cheng Chen46f30c72017-09-07 11:13:33 -07001183 alloc_compressor_data(cpi);
Yaowu Xuc7119a72018-03-29 09:59:37 -07001184
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08001185 update_film_grain_parameters(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001186
1187 // Single thread case: use counts in common.
Yue Chencc6a6ef2018-05-21 16:21:05 -07001188 cpi->td.counts = &cpi->counts;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001189
1190 // change includes all joint functionality
Yaowu Xuf883b422016-08-30 14:01:10 -07001191 av1_change_config(cpi, oxcf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001192
1193 cpi->static_mb_pct = 0;
1194 cpi->ref_frame_flags = 0;
1195
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07001196 // Reset resize pending flags
1197 cpi->resize_pending_width = 0;
1198 cpi->resize_pending_height = 0;
1199
Yaowu Xuc27fc142016-08-22 16:08:15 -07001200 init_buffer_indices(cpi);
1201}
1202
1203static void set_rc_buffer_sizes(RATE_CONTROL *rc,
Yaowu Xuf883b422016-08-30 14:01:10 -07001204 const AV1EncoderConfig *oxcf) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07001205 const int64_t bandwidth = oxcf->target_bandwidth;
1206 const int64_t starting = oxcf->starting_buffer_level_ms;
1207 const int64_t optimal = oxcf->optimal_buffer_level_ms;
1208 const int64_t maximum = oxcf->maximum_buffer_size_ms;
1209
1210 rc->starting_buffer_level = starting * bandwidth / 1000;
1211 rc->optimal_buffer_level =
1212 (optimal == 0) ? bandwidth / 8 : optimal * bandwidth / 1000;
1213 rc->maximum_buffer_size =
1214 (maximum == 0) ? bandwidth / 8 : maximum * bandwidth / 1000;
1215}
1216
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001217#define HIGHBD_BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
1218 cpi->fn_ptr[BT].sdf = SDF; \
1219 cpi->fn_ptr[BT].sdaf = SDAF; \
1220 cpi->fn_ptr[BT].vf = VF; \
1221 cpi->fn_ptr[BT].svf = SVF; \
1222 cpi->fn_ptr[BT].svaf = SVAF; \
1223 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
1224 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001225 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001226
1227#define MAKE_BFP_SAD_WRAPPER(fnname) \
1228 static unsigned int fnname##_bits8(const uint8_t *src_ptr, \
1229 int source_stride, \
1230 const uint8_t *ref_ptr, int ref_stride) { \
1231 return fnname(src_ptr, source_stride, ref_ptr, ref_stride); \
1232 } \
1233 static unsigned int fnname##_bits10( \
1234 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1235 int ref_stride) { \
1236 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 2; \
1237 } \
1238 static unsigned int fnname##_bits12( \
1239 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1240 int ref_stride) { \
1241 return fnname(src_ptr, source_stride, ref_ptr, ref_stride) >> 4; \
1242 }
1243
1244#define MAKE_BFP_SADAVG_WRAPPER(fnname) \
1245 static unsigned int fnname##_bits8( \
1246 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1247 int ref_stride, const uint8_t *second_pred) { \
1248 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred); \
1249 } \
1250 static unsigned int fnname##_bits10( \
1251 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1252 int ref_stride, const uint8_t *second_pred) { \
1253 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred) >> \
1254 2; \
1255 } \
1256 static unsigned int fnname##_bits12( \
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 4; \
1261 }
1262
Yaowu Xuc27fc142016-08-22 16:08:15 -07001263#define MAKE_BFP_SAD4D_WRAPPER(fnname) \
1264 static void fnname##_bits8(const uint8_t *src_ptr, int source_stride, \
1265 const uint8_t *const ref_ptr[], int ref_stride, \
1266 unsigned int *sad_array) { \
1267 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1268 } \
1269 static void fnname##_bits10(const uint8_t *src_ptr, int source_stride, \
1270 const uint8_t *const ref_ptr[], int ref_stride, \
1271 unsigned int *sad_array) { \
1272 int i; \
1273 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1274 for (i = 0; i < 4; i++) sad_array[i] >>= 2; \
1275 } \
1276 static void fnname##_bits12(const uint8_t *src_ptr, int source_stride, \
1277 const uint8_t *const ref_ptr[], int ref_stride, \
1278 unsigned int *sad_array) { \
1279 int i; \
1280 fnname(src_ptr, source_stride, ref_ptr, ref_stride, sad_array); \
1281 for (i = 0; i < 4; i++) sad_array[i] >>= 4; \
1282 }
1283
Cheng Chenbf3d4962017-11-01 14:48:52 -07001284#define MAKE_BFP_JSADAVG_WRAPPER(fnname) \
1285 static unsigned int fnname##_bits8( \
1286 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1287 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001288 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001289 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1290 jcp_param); \
1291 } \
1292 static unsigned int fnname##_bits10( \
1293 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1294 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001295 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001296 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1297 jcp_param) >> \
1298 2; \
1299 } \
1300 static unsigned int fnname##_bits12( \
1301 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1302 int ref_stride, const uint8_t *second_pred, \
Debargha Mukherjeef90004a2018-12-20 13:35:06 -08001303 const DIST_WTD_COMP_PARAMS *jcp_param) { \
Cheng Chenbf3d4962017-11-01 14:48:52 -07001304 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, second_pred, \
1305 jcp_param) >> \
1306 4; \
1307 }
Cheng Chenbf3d4962017-11-01 14:48:52 -07001308
Yaowu Xuf883b422016-08-30 14:01:10 -07001309MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x128)
1310MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x128_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001311MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x128x4d)
1312MAKE_BFP_SAD_WRAPPER(aom_highbd_sad128x64)
1313MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad128x64_avg)
1314MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad128x64x4d)
1315MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x128)
1316MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x128_avg)
1317MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x128x4d)
Yaowu Xuf883b422016-08-30 14:01:10 -07001318MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x16)
1319MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x16_avg)
1320MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x16x4d)
1321MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x32)
1322MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x32_avg)
1323MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x32x4d)
1324MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x32)
1325MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x32_avg)
1326MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x32x4d)
1327MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x64)
1328MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x64_avg)
1329MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x64x4d)
1330MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x32)
1331MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x32_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001332MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x32x4d)
1333MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x64)
1334MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x64_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001335MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x64x4d)
1336MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x16)
1337MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001338MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x16x4d)
1339MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x8)
1340MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001341MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x8x4d)
1342MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x16)
1343MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x16_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001344MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x16x4d)
1345MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x8)
1346MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001347MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x8x4d)
1348MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x4)
1349MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001350MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x4x4d)
1351MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x8)
1352MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x8_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001353MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x8x4d)
1354MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x4)
1355MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x4_avg)
Yaowu Xuf883b422016-08-30 14:01:10 -07001356MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x4x4d)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001357
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001358MAKE_BFP_SAD_WRAPPER(aom_highbd_sad4x16)
1359MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad4x16_avg)
1360MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad4x16x4d)
1361MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x4)
1362MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x4_avg)
1363MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x4x4d)
1364MAKE_BFP_SAD_WRAPPER(aom_highbd_sad8x32)
1365MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad8x32_avg)
1366MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad8x32x4d)
1367MAKE_BFP_SAD_WRAPPER(aom_highbd_sad32x8)
1368MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad32x8_avg)
1369MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad32x8x4d)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001370MAKE_BFP_SAD_WRAPPER(aom_highbd_sad16x64)
1371MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad16x64_avg)
1372MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad16x64x4d)
1373MAKE_BFP_SAD_WRAPPER(aom_highbd_sad64x16)
1374MAKE_BFP_SADAVG_WRAPPER(aom_highbd_sad64x16_avg)
1375MAKE_BFP_SAD4D_WRAPPER(aom_highbd_sad64x16x4d)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001376
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001377MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x128_avg)
1378MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad128x64_avg)
1379MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x128_avg)
1380MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x16_avg)
1381MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x32_avg)
1382MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x32_avg)
1383MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x64_avg)
1384MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x32_avg)
1385MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x64_avg)
1386MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x16_avg)
1387MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x8_avg)
1388MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x16_avg)
1389MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x8_avg)
1390MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x4_avg)
1391MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x8_avg)
1392MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x4_avg)
1393MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad4x16_avg)
1394MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x4_avg)
1395MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad8x32_avg)
1396MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad32x8_avg)
1397MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad16x64_avg)
1398MAKE_BFP_JSADAVG_WRAPPER(aom_highbd_dist_wtd_sad64x16_avg)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001399
David Barker0f3c94e2017-05-16 15:21:50 +01001400#define HIGHBD_MBFP(BT, MCSDF, MCSVF) \
David Barkerf19f35f2017-05-22 16:33:22 +01001401 cpi->fn_ptr[BT].msdf = MCSDF; \
1402 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07001403
David Barkerc155e012017-05-11 13:54:54 +01001404#define MAKE_MBFP_COMPOUND_SAD_WRAPPER(fnname) \
1405 static unsigned int fnname##_bits8( \
1406 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1407 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1408 int m_stride, int invert_mask) { \
1409 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1410 second_pred_ptr, m, m_stride, invert_mask); \
1411 } \
1412 static unsigned int fnname##_bits10( \
1413 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1414 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1415 int m_stride, int invert_mask) { \
1416 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1417 second_pred_ptr, m, m_stride, invert_mask) >> \
1418 2; \
1419 } \
1420 static unsigned int fnname##_bits12( \
1421 const uint8_t *src_ptr, int source_stride, const uint8_t *ref_ptr, \
1422 int ref_stride, const uint8_t *second_pred_ptr, const uint8_t *m, \
1423 int m_stride, int invert_mask) { \
1424 return fnname(src_ptr, source_stride, ref_ptr, ref_stride, \
1425 second_pred_ptr, m, m_stride, invert_mask) >> \
1426 4; \
1427 }
1428
David Barkerf19f35f2017-05-22 16:33:22 +01001429MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x128)
1430MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad128x64)
1431MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001432MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x64)
1433MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x32)
1434MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x64)
1435MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x32)
1436MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x16)
1437MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x32)
1438MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x16)
1439MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x8)
1440MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x16)
1441MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x8)
1442MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x4)
1443MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x8)
1444MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001445MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad4x16)
1446MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x4)
1447MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad8x32)
1448MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001449MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad16x64)
1450MAKE_MBFP_COMPOUND_SAD_WRAPPER(aom_highbd_masked_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001451
Yaowu Xuc27fc142016-08-22 16:08:15 -07001452#define HIGHBD_OBFP(BT, OSDF, OVF, OSVF) \
1453 cpi->fn_ptr[BT].osdf = OSDF; \
1454 cpi->fn_ptr[BT].ovf = OVF; \
1455 cpi->fn_ptr[BT].osvf = OSVF;
1456
1457#define MAKE_OBFP_SAD_WRAPPER(fnname) \
1458 static unsigned int fnname##_bits8(const uint8_t *ref, int ref_stride, \
1459 const int32_t *wsrc, \
1460 const int32_t *msk) { \
1461 return fnname(ref, ref_stride, wsrc, msk); \
1462 } \
1463 static unsigned int fnname##_bits10(const uint8_t *ref, int ref_stride, \
1464 const int32_t *wsrc, \
1465 const int32_t *msk) { \
1466 return fnname(ref, ref_stride, wsrc, msk) >> 2; \
1467 } \
1468 static unsigned int fnname##_bits12(const uint8_t *ref, int ref_stride, \
1469 const int32_t *wsrc, \
1470 const int32_t *msk) { \
1471 return fnname(ref, ref_stride, wsrc, msk) >> 4; \
1472 }
1473
Yaowu Xuf883b422016-08-30 14:01:10 -07001474MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x128)
1475MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad128x64)
1476MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001477MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x64)
1478MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x32)
1479MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x64)
1480MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x32)
1481MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x16)
1482MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x32)
1483MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x16)
1484MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x8)
1485MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x16)
1486MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x8)
1487MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x4)
1488MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x8)
1489MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001490MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad4x16)
1491MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x4)
1492MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad8x32)
1493MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001494MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad16x64)
1495MAKE_OBFP_SAD_WRAPPER(aom_highbd_obmc_sad64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001496
Yaowu Xuf883b422016-08-30 14:01:10 -07001497static void highbd_set_var_fns(AV1_COMP *const cpi) {
1498 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07001499 if (cm->seq_params.use_highbitdepth) {
1500 switch (cm->seq_params.bit_depth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07001501 case AOM_BITS_8:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001502 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits8,
1503 aom_highbd_sad64x16_avg_bits8, aom_highbd_8_variance64x16,
1504 aom_highbd_8_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001505 aom_highbd_8_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001506 aom_highbd_sad64x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001507 aom_highbd_dist_wtd_sad64x16_avg_bits8,
1508 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001509
1510 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits8,
1511 aom_highbd_sad16x64_avg_bits8, aom_highbd_8_variance16x64,
1512 aom_highbd_8_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001513 aom_highbd_8_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001514 aom_highbd_sad16x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001515 aom_highbd_dist_wtd_sad16x64_avg_bits8,
1516 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001517
1518 HIGHBD_BFP(
1519 BLOCK_32X8, aom_highbd_sad32x8_bits8, aom_highbd_sad32x8_avg_bits8,
1520 aom_highbd_8_variance32x8, aom_highbd_8_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001521 aom_highbd_8_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001522 aom_highbd_sad32x8x4d_bits8, aom_highbd_dist_wtd_sad32x8_avg_bits8,
1523 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001524
1525 HIGHBD_BFP(
1526 BLOCK_8X32, aom_highbd_sad8x32_bits8, aom_highbd_sad8x32_avg_bits8,
1527 aom_highbd_8_variance8x32, aom_highbd_8_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001528 aom_highbd_8_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001529 aom_highbd_sad8x32x4d_bits8, aom_highbd_dist_wtd_sad8x32_avg_bits8,
1530 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001531
1532 HIGHBD_BFP(
1533 BLOCK_16X4, aom_highbd_sad16x4_bits8, aom_highbd_sad16x4_avg_bits8,
1534 aom_highbd_8_variance16x4, aom_highbd_8_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001535 aom_highbd_8_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001536 aom_highbd_sad16x4x4d_bits8, aom_highbd_dist_wtd_sad16x4_avg_bits8,
1537 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001538
1539 HIGHBD_BFP(
1540 BLOCK_4X16, aom_highbd_sad4x16_bits8, aom_highbd_sad4x16_avg_bits8,
1541 aom_highbd_8_variance4x16, aom_highbd_8_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001542 aom_highbd_8_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001543 aom_highbd_sad4x16x4d_bits8, aom_highbd_dist_wtd_sad4x16_avg_bits8,
1544 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001545
1546 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits8,
1547 aom_highbd_sad32x16_avg_bits8, aom_highbd_8_variance32x16,
1548 aom_highbd_8_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001549 aom_highbd_8_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001550 aom_highbd_sad32x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001551 aom_highbd_dist_wtd_sad32x16_avg_bits8,
1552 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001553
1554 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits8,
1555 aom_highbd_sad16x32_avg_bits8, aom_highbd_8_variance16x32,
1556 aom_highbd_8_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001557 aom_highbd_8_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001558 aom_highbd_sad16x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001559 aom_highbd_dist_wtd_sad16x32_avg_bits8,
1560 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001561
1562 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits8,
1563 aom_highbd_sad64x32_avg_bits8, aom_highbd_8_variance64x32,
1564 aom_highbd_8_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001565 aom_highbd_8_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001566 aom_highbd_sad64x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001567 aom_highbd_dist_wtd_sad64x32_avg_bits8,
1568 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001569
1570 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits8,
1571 aom_highbd_sad32x64_avg_bits8, aom_highbd_8_variance32x64,
1572 aom_highbd_8_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001573 aom_highbd_8_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001574 aom_highbd_sad32x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001575 aom_highbd_dist_wtd_sad32x64_avg_bits8,
1576 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001577
1578 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits8,
1579 aom_highbd_sad32x32_avg_bits8, aom_highbd_8_variance32x32,
1580 aom_highbd_8_sub_pixel_variance32x32,
1581 aom_highbd_8_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001582 aom_highbd_sad32x32x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001583 aom_highbd_dist_wtd_sad32x32_avg_bits8,
1584 aom_highbd_8_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001585
1586 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits8,
1587 aom_highbd_sad64x64_avg_bits8, aom_highbd_8_variance64x64,
1588 aom_highbd_8_sub_pixel_variance64x64,
1589 aom_highbd_8_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001590 aom_highbd_sad64x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001591 aom_highbd_dist_wtd_sad64x64_avg_bits8,
1592 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001593
1594 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits8,
1595 aom_highbd_sad16x16_avg_bits8, aom_highbd_8_variance16x16,
1596 aom_highbd_8_sub_pixel_variance16x16,
1597 aom_highbd_8_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001598 aom_highbd_sad16x16x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001599 aom_highbd_dist_wtd_sad16x16_avg_bits8,
1600 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001601
1602 HIGHBD_BFP(
1603 BLOCK_16X8, aom_highbd_sad16x8_bits8, aom_highbd_sad16x8_avg_bits8,
1604 aom_highbd_8_variance16x8, aom_highbd_8_sub_pixel_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001605 aom_highbd_8_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001606 aom_highbd_sad16x8x4d_bits8, aom_highbd_dist_wtd_sad16x8_avg_bits8,
1607 aom_highbd_8_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001608
1609 HIGHBD_BFP(
1610 BLOCK_8X16, aom_highbd_sad8x16_bits8, aom_highbd_sad8x16_avg_bits8,
1611 aom_highbd_8_variance8x16, aom_highbd_8_sub_pixel_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001612 aom_highbd_8_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001613 aom_highbd_sad8x16x4d_bits8, aom_highbd_dist_wtd_sad8x16_avg_bits8,
1614 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001615
Cheng Chenbf3d4962017-11-01 14:48:52 -07001616 HIGHBD_BFP(
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001617 BLOCK_8X8, aom_highbd_sad8x8_bits8, aom_highbd_sad8x8_avg_bits8,
1618 aom_highbd_8_variance8x8, aom_highbd_8_sub_pixel_variance8x8,
1619 aom_highbd_8_sub_pixel_avg_variance8x8, aom_highbd_sad8x8x4d_bits8,
1620 aom_highbd_dist_wtd_sad8x8_avg_bits8,
1621 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x8)
1622
1623 HIGHBD_BFP(
1624 BLOCK_8X4, aom_highbd_sad8x4_bits8, aom_highbd_sad8x4_avg_bits8,
1625 aom_highbd_8_variance8x4, aom_highbd_8_sub_pixel_variance8x4,
1626 aom_highbd_8_sub_pixel_avg_variance8x4, aom_highbd_sad8x4x4d_bits8,
1627 aom_highbd_dist_wtd_sad8x4_avg_bits8,
1628 aom_highbd_8_dist_wtd_sub_pixel_avg_variance8x4)
1629
1630 HIGHBD_BFP(
1631 BLOCK_4X8, aom_highbd_sad4x8_bits8, aom_highbd_sad4x8_avg_bits8,
1632 aom_highbd_8_variance4x8, aom_highbd_8_sub_pixel_variance4x8,
1633 aom_highbd_8_sub_pixel_avg_variance4x8, aom_highbd_sad4x8x4d_bits8,
1634 aom_highbd_dist_wtd_sad4x8_avg_bits8,
1635 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x8)
1636
1637 HIGHBD_BFP(
1638 BLOCK_4X4, aom_highbd_sad4x4_bits8, aom_highbd_sad4x4_avg_bits8,
1639 aom_highbd_8_variance4x4, aom_highbd_8_sub_pixel_variance4x4,
1640 aom_highbd_8_sub_pixel_avg_variance4x4, aom_highbd_sad4x4x4d_bits8,
1641 aom_highbd_dist_wtd_sad4x4_avg_bits8,
1642 aom_highbd_8_dist_wtd_sub_pixel_avg_variance4x4)
1643
1644 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits8,
1645 aom_highbd_sad128x128_avg_bits8,
1646 aom_highbd_8_variance128x128,
1647 aom_highbd_8_sub_pixel_variance128x128,
1648 aom_highbd_8_sub_pixel_avg_variance128x128,
1649 aom_highbd_sad128x128x4d_bits8,
1650 aom_highbd_dist_wtd_sad128x128_avg_bits8,
1651 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001652
1653 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits8,
1654 aom_highbd_sad128x64_avg_bits8, aom_highbd_8_variance128x64,
1655 aom_highbd_8_sub_pixel_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001656 aom_highbd_8_sub_pixel_avg_variance128x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001657 aom_highbd_sad128x64x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001658 aom_highbd_dist_wtd_sad128x64_avg_bits8,
1659 aom_highbd_8_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenbf3d4962017-11-01 14:48:52 -07001660
1661 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits8,
1662 aom_highbd_sad64x128_avg_bits8, aom_highbd_8_variance64x128,
1663 aom_highbd_8_sub_pixel_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001664 aom_highbd_8_sub_pixel_avg_variance64x128,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001665 aom_highbd_sad64x128x4d_bits8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001666 aom_highbd_dist_wtd_sad64x128_avg_bits8,
1667 aom_highbd_8_dist_wtd_sub_pixel_avg_variance64x128)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001668
David Barkerf19f35f2017-05-22 16:33:22 +01001669 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits8,
1670 aom_highbd_8_masked_sub_pixel_variance128x128)
1671 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits8,
1672 aom_highbd_8_masked_sub_pixel_variance128x64)
1673 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits8,
1674 aom_highbd_8_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001675 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits8,
1676 aom_highbd_8_masked_sub_pixel_variance64x64)
1677 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits8,
1678 aom_highbd_8_masked_sub_pixel_variance64x32)
1679 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits8,
1680 aom_highbd_8_masked_sub_pixel_variance32x64)
1681 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits8,
1682 aom_highbd_8_masked_sub_pixel_variance32x32)
1683 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits8,
1684 aom_highbd_8_masked_sub_pixel_variance32x16)
1685 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits8,
1686 aom_highbd_8_masked_sub_pixel_variance16x32)
1687 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits8,
1688 aom_highbd_8_masked_sub_pixel_variance16x16)
1689 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits8,
1690 aom_highbd_8_masked_sub_pixel_variance8x16)
1691 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits8,
1692 aom_highbd_8_masked_sub_pixel_variance16x8)
1693 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits8,
1694 aom_highbd_8_masked_sub_pixel_variance8x8)
1695 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits8,
1696 aom_highbd_8_masked_sub_pixel_variance4x8)
1697 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits8,
1698 aom_highbd_8_masked_sub_pixel_variance8x4)
1699 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits8,
1700 aom_highbd_8_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001701 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits8,
1702 aom_highbd_8_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001703 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits8,
1704 aom_highbd_8_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001705 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits8,
1706 aom_highbd_8_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001707 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits8,
1708 aom_highbd_8_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001709 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits8,
1710 aom_highbd_8_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001711 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits8,
1712 aom_highbd_8_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07001713 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits8,
1714 aom_highbd_obmc_variance128x128,
1715 aom_highbd_obmc_sub_pixel_variance128x128)
1716 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits8,
1717 aom_highbd_obmc_variance128x64,
1718 aom_highbd_obmc_sub_pixel_variance128x64)
1719 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits8,
1720 aom_highbd_obmc_variance64x128,
1721 aom_highbd_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07001722 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits8,
1723 aom_highbd_obmc_variance64x64,
1724 aom_highbd_obmc_sub_pixel_variance64x64)
1725 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits8,
1726 aom_highbd_obmc_variance64x32,
1727 aom_highbd_obmc_sub_pixel_variance64x32)
1728 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits8,
1729 aom_highbd_obmc_variance32x64,
1730 aom_highbd_obmc_sub_pixel_variance32x64)
1731 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits8,
1732 aom_highbd_obmc_variance32x32,
1733 aom_highbd_obmc_sub_pixel_variance32x32)
1734 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits8,
1735 aom_highbd_obmc_variance32x16,
1736 aom_highbd_obmc_sub_pixel_variance32x16)
1737 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits8,
1738 aom_highbd_obmc_variance16x32,
1739 aom_highbd_obmc_sub_pixel_variance16x32)
1740 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits8,
1741 aom_highbd_obmc_variance16x16,
1742 aom_highbd_obmc_sub_pixel_variance16x16)
1743 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits8,
1744 aom_highbd_obmc_variance8x16,
1745 aom_highbd_obmc_sub_pixel_variance8x16)
1746 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits8,
1747 aom_highbd_obmc_variance16x8,
1748 aom_highbd_obmc_sub_pixel_variance16x8)
1749 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits8,
1750 aom_highbd_obmc_variance8x8,
1751 aom_highbd_obmc_sub_pixel_variance8x8)
1752 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits8,
1753 aom_highbd_obmc_variance4x8,
1754 aom_highbd_obmc_sub_pixel_variance4x8)
1755 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits8,
1756 aom_highbd_obmc_variance8x4,
1757 aom_highbd_obmc_sub_pixel_variance8x4)
1758 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits8,
1759 aom_highbd_obmc_variance4x4,
1760 aom_highbd_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001761 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits8,
1762 aom_highbd_obmc_variance64x16,
1763 aom_highbd_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001764 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits8,
1765 aom_highbd_obmc_variance16x64,
1766 aom_highbd_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001767 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits8,
1768 aom_highbd_obmc_variance32x8,
1769 aom_highbd_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001770 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits8,
1771 aom_highbd_obmc_variance8x32,
1772 aom_highbd_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001773 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits8,
1774 aom_highbd_obmc_variance16x4,
1775 aom_highbd_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001776 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits8,
1777 aom_highbd_obmc_variance4x16,
1778 aom_highbd_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07001779 break;
1780
Yaowu Xuf883b422016-08-30 14:01:10 -07001781 case AOM_BITS_10:
Cheng Chenbf3d4962017-11-01 14:48:52 -07001782 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits10,
1783 aom_highbd_sad64x16_avg_bits10, aom_highbd_10_variance64x16,
1784 aom_highbd_10_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001785 aom_highbd_10_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001786 aom_highbd_sad64x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001787 aom_highbd_dist_wtd_sad64x16_avg_bits10,
1788 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001789
1790 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits10,
1791 aom_highbd_sad16x64_avg_bits10, aom_highbd_10_variance16x64,
1792 aom_highbd_10_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001793 aom_highbd_10_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001794 aom_highbd_sad16x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001795 aom_highbd_dist_wtd_sad16x64_avg_bits10,
1796 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001797
1798 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits10,
1799 aom_highbd_sad32x8_avg_bits10, aom_highbd_10_variance32x8,
1800 aom_highbd_10_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001801 aom_highbd_10_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001802 aom_highbd_sad32x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001803 aom_highbd_dist_wtd_sad32x8_avg_bits10,
1804 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001805
1806 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits10,
1807 aom_highbd_sad8x32_avg_bits10, aom_highbd_10_variance8x32,
1808 aom_highbd_10_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001809 aom_highbd_10_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001810 aom_highbd_sad8x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001811 aom_highbd_dist_wtd_sad8x32_avg_bits10,
1812 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001813
1814 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits10,
1815 aom_highbd_sad16x4_avg_bits10, aom_highbd_10_variance16x4,
1816 aom_highbd_10_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001817 aom_highbd_10_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001818 aom_highbd_sad16x4x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001819 aom_highbd_dist_wtd_sad16x4_avg_bits10,
1820 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001821
1822 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits10,
1823 aom_highbd_sad4x16_avg_bits10, aom_highbd_10_variance4x16,
1824 aom_highbd_10_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001825 aom_highbd_10_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001826 aom_highbd_sad4x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001827 aom_highbd_dist_wtd_sad4x16_avg_bits10,
1828 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001829
1830 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits10,
1831 aom_highbd_sad32x16_avg_bits10, aom_highbd_10_variance32x16,
1832 aom_highbd_10_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001833 aom_highbd_10_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001834 aom_highbd_sad32x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001835 aom_highbd_dist_wtd_sad32x16_avg_bits10,
1836 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001837
1838 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits10,
1839 aom_highbd_sad16x32_avg_bits10, aom_highbd_10_variance16x32,
1840 aom_highbd_10_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001841 aom_highbd_10_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001842 aom_highbd_sad16x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001843 aom_highbd_dist_wtd_sad16x32_avg_bits10,
1844 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001845
1846 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits10,
1847 aom_highbd_sad64x32_avg_bits10, aom_highbd_10_variance64x32,
1848 aom_highbd_10_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001849 aom_highbd_10_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001850 aom_highbd_sad64x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001851 aom_highbd_dist_wtd_sad64x32_avg_bits10,
1852 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001853
1854 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits10,
1855 aom_highbd_sad32x64_avg_bits10, aom_highbd_10_variance32x64,
1856 aom_highbd_10_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001857 aom_highbd_10_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001858 aom_highbd_sad32x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001859 aom_highbd_dist_wtd_sad32x64_avg_bits10,
1860 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001861
1862 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits10,
1863 aom_highbd_sad32x32_avg_bits10, aom_highbd_10_variance32x32,
1864 aom_highbd_10_sub_pixel_variance32x32,
1865 aom_highbd_10_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001866 aom_highbd_sad32x32x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001867 aom_highbd_dist_wtd_sad32x32_avg_bits10,
1868 aom_highbd_10_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001869
1870 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits10,
1871 aom_highbd_sad64x64_avg_bits10, aom_highbd_10_variance64x64,
1872 aom_highbd_10_sub_pixel_variance64x64,
1873 aom_highbd_10_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001874 aom_highbd_sad64x64x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001875 aom_highbd_dist_wtd_sad64x64_avg_bits10,
1876 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001877
1878 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits10,
1879 aom_highbd_sad16x16_avg_bits10, aom_highbd_10_variance16x16,
1880 aom_highbd_10_sub_pixel_variance16x16,
1881 aom_highbd_10_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001882 aom_highbd_sad16x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001883 aom_highbd_dist_wtd_sad16x16_avg_bits10,
1884 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001885
1886 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits10,
1887 aom_highbd_sad16x8_avg_bits10, aom_highbd_10_variance16x8,
1888 aom_highbd_10_sub_pixel_variance16x8,
1889 aom_highbd_10_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001890 aom_highbd_sad16x8x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001891 aom_highbd_dist_wtd_sad16x8_avg_bits10,
1892 aom_highbd_10_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001893
1894 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits10,
1895 aom_highbd_sad8x16_avg_bits10, aom_highbd_10_variance8x16,
1896 aom_highbd_10_sub_pixel_variance8x16,
1897 aom_highbd_10_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07001898 aom_highbd_sad8x16x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001899 aom_highbd_dist_wtd_sad8x16_avg_bits10,
1900 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001901
1902 HIGHBD_BFP(
1903 BLOCK_8X8, aom_highbd_sad8x8_bits10, aom_highbd_sad8x8_avg_bits10,
1904 aom_highbd_10_variance8x8, aom_highbd_10_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001905 aom_highbd_10_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001906 aom_highbd_sad8x8x4d_bits10, aom_highbd_dist_wtd_sad8x8_avg_bits10,
1907 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001908
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001909 HIGHBD_BFP(
1910 BLOCK_8X4, aom_highbd_sad8x4_bits10, aom_highbd_sad8x4_avg_bits10,
1911 aom_highbd_10_variance8x4, aom_highbd_10_sub_pixel_variance8x4,
1912 aom_highbd_10_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001913 aom_highbd_sad8x4x4d_bits10, aom_highbd_dist_wtd_sad8x4_avg_bits10,
1914 aom_highbd_10_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001915
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001916 HIGHBD_BFP(
1917 BLOCK_4X8, aom_highbd_sad4x8_bits10, aom_highbd_sad4x8_avg_bits10,
1918 aom_highbd_10_variance4x8, aom_highbd_10_sub_pixel_variance4x8,
1919 aom_highbd_10_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001920 aom_highbd_sad4x8x4d_bits10, aom_highbd_dist_wtd_sad4x8_avg_bits10,
1921 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001922
1923 HIGHBD_BFP(
1924 BLOCK_4X4, aom_highbd_sad4x4_bits10, aom_highbd_sad4x4_avg_bits10,
1925 aom_highbd_10_variance4x4, aom_highbd_10_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001926 aom_highbd_10_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001927 aom_highbd_sad4x4x4d_bits10, aom_highbd_dist_wtd_sad4x4_avg_bits10,
1928 aom_highbd_10_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001929
Kyle Siefringef6e2df2018-04-10 14:51:35 -04001930 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits10,
1931 aom_highbd_sad128x128_avg_bits10,
1932 aom_highbd_10_variance128x128,
1933 aom_highbd_10_sub_pixel_variance128x128,
1934 aom_highbd_10_sub_pixel_avg_variance128x128,
1935 aom_highbd_sad128x128x4d_bits10,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001936 aom_highbd_dist_wtd_sad128x128_avg_bits10,
1937 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001938
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001939 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits10,
1940 aom_highbd_sad128x64_avg_bits10,
1941 aom_highbd_10_variance128x64,
1942 aom_highbd_10_sub_pixel_variance128x64,
1943 aom_highbd_10_sub_pixel_avg_variance128x64,
1944 aom_highbd_sad128x64x4d_bits10,
1945 aom_highbd_dist_wtd_sad128x64_avg_bits10,
1946 aom_highbd_10_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07001947
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08001948 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits10,
1949 aom_highbd_sad64x128_avg_bits10,
1950 aom_highbd_10_variance64x128,
1951 aom_highbd_10_sub_pixel_variance64x128,
1952 aom_highbd_10_sub_pixel_avg_variance64x128,
1953 aom_highbd_sad64x128x4d_bits10,
1954 aom_highbd_dist_wtd_sad64x128_avg_bits10,
1955 aom_highbd_10_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07001956
David Barkerf19f35f2017-05-22 16:33:22 +01001957 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits10,
1958 aom_highbd_10_masked_sub_pixel_variance128x128)
1959 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits10,
1960 aom_highbd_10_masked_sub_pixel_variance128x64)
1961 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits10,
1962 aom_highbd_10_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01001963 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits10,
1964 aom_highbd_10_masked_sub_pixel_variance64x64)
1965 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits10,
1966 aom_highbd_10_masked_sub_pixel_variance64x32)
1967 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits10,
1968 aom_highbd_10_masked_sub_pixel_variance32x64)
1969 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits10,
1970 aom_highbd_10_masked_sub_pixel_variance32x32)
1971 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits10,
1972 aom_highbd_10_masked_sub_pixel_variance32x16)
1973 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits10,
1974 aom_highbd_10_masked_sub_pixel_variance16x32)
1975 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits10,
1976 aom_highbd_10_masked_sub_pixel_variance16x16)
1977 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits10,
1978 aom_highbd_10_masked_sub_pixel_variance8x16)
1979 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits10,
1980 aom_highbd_10_masked_sub_pixel_variance16x8)
1981 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits10,
1982 aom_highbd_10_masked_sub_pixel_variance8x8)
1983 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits10,
1984 aom_highbd_10_masked_sub_pixel_variance4x8)
1985 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits10,
1986 aom_highbd_10_masked_sub_pixel_variance8x4)
1987 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits10,
1988 aom_highbd_10_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001989 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits10,
1990 aom_highbd_10_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01001991 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits10,
1992 aom_highbd_10_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001993 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits10,
1994 aom_highbd_10_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001995 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits10,
1996 aom_highbd_10_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001997 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits10,
1998 aom_highbd_10_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01001999 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits10,
2000 aom_highbd_10_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002001 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits10,
2002 aom_highbd_10_obmc_variance128x128,
2003 aom_highbd_10_obmc_sub_pixel_variance128x128)
2004 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits10,
2005 aom_highbd_10_obmc_variance128x64,
2006 aom_highbd_10_obmc_sub_pixel_variance128x64)
2007 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits10,
2008 aom_highbd_10_obmc_variance64x128,
2009 aom_highbd_10_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002010 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits10,
2011 aom_highbd_10_obmc_variance64x64,
2012 aom_highbd_10_obmc_sub_pixel_variance64x64)
2013 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits10,
2014 aom_highbd_10_obmc_variance64x32,
2015 aom_highbd_10_obmc_sub_pixel_variance64x32)
2016 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits10,
2017 aom_highbd_10_obmc_variance32x64,
2018 aom_highbd_10_obmc_sub_pixel_variance32x64)
2019 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits10,
2020 aom_highbd_10_obmc_variance32x32,
2021 aom_highbd_10_obmc_sub_pixel_variance32x32)
2022 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits10,
2023 aom_highbd_10_obmc_variance32x16,
2024 aom_highbd_10_obmc_sub_pixel_variance32x16)
2025 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits10,
2026 aom_highbd_10_obmc_variance16x32,
2027 aom_highbd_10_obmc_sub_pixel_variance16x32)
2028 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits10,
2029 aom_highbd_10_obmc_variance16x16,
2030 aom_highbd_10_obmc_sub_pixel_variance16x16)
2031 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits10,
2032 aom_highbd_10_obmc_variance8x16,
2033 aom_highbd_10_obmc_sub_pixel_variance8x16)
2034 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits10,
2035 aom_highbd_10_obmc_variance16x8,
2036 aom_highbd_10_obmc_sub_pixel_variance16x8)
2037 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits10,
2038 aom_highbd_10_obmc_variance8x8,
2039 aom_highbd_10_obmc_sub_pixel_variance8x8)
2040 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits10,
2041 aom_highbd_10_obmc_variance4x8,
2042 aom_highbd_10_obmc_sub_pixel_variance4x8)
2043 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits10,
2044 aom_highbd_10_obmc_variance8x4,
2045 aom_highbd_10_obmc_sub_pixel_variance8x4)
2046 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits10,
2047 aom_highbd_10_obmc_variance4x4,
2048 aom_highbd_10_obmc_sub_pixel_variance4x4)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002049
Rupert Swarbrick72678572017-08-02 12:05:26 +01002050 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits10,
2051 aom_highbd_10_obmc_variance64x16,
2052 aom_highbd_10_obmc_sub_pixel_variance64x16)
2053
2054 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits10,
2055 aom_highbd_10_obmc_variance16x64,
2056 aom_highbd_10_obmc_sub_pixel_variance16x64)
2057
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002058 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits10,
2059 aom_highbd_10_obmc_variance32x8,
2060 aom_highbd_10_obmc_sub_pixel_variance32x8)
2061
2062 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits10,
2063 aom_highbd_10_obmc_variance8x32,
2064 aom_highbd_10_obmc_sub_pixel_variance8x32)
2065
2066 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits10,
2067 aom_highbd_10_obmc_variance16x4,
2068 aom_highbd_10_obmc_sub_pixel_variance16x4)
2069
2070 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits10,
2071 aom_highbd_10_obmc_variance4x16,
2072 aom_highbd_10_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002073 break;
2074
Yaowu Xuf883b422016-08-30 14:01:10 -07002075 case AOM_BITS_12:
Cheng Chenbf3d4962017-11-01 14:48:52 -07002076 HIGHBD_BFP(BLOCK_64X16, aom_highbd_sad64x16_bits12,
2077 aom_highbd_sad64x16_avg_bits12, aom_highbd_12_variance64x16,
2078 aom_highbd_12_sub_pixel_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002079 aom_highbd_12_sub_pixel_avg_variance64x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002080 aom_highbd_sad64x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002081 aom_highbd_dist_wtd_sad64x16_avg_bits12,
2082 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002083
2084 HIGHBD_BFP(BLOCK_16X64, aom_highbd_sad16x64_bits12,
2085 aom_highbd_sad16x64_avg_bits12, aom_highbd_12_variance16x64,
2086 aom_highbd_12_sub_pixel_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002087 aom_highbd_12_sub_pixel_avg_variance16x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002088 aom_highbd_sad16x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002089 aom_highbd_dist_wtd_sad16x64_avg_bits12,
2090 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002091
2092 HIGHBD_BFP(BLOCK_32X8, aom_highbd_sad32x8_bits12,
2093 aom_highbd_sad32x8_avg_bits12, aom_highbd_12_variance32x8,
2094 aom_highbd_12_sub_pixel_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002095 aom_highbd_12_sub_pixel_avg_variance32x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002096 aom_highbd_sad32x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002097 aom_highbd_dist_wtd_sad32x8_avg_bits12,
2098 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002099
2100 HIGHBD_BFP(BLOCK_8X32, aom_highbd_sad8x32_bits12,
2101 aom_highbd_sad8x32_avg_bits12, aom_highbd_12_variance8x32,
2102 aom_highbd_12_sub_pixel_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002103 aom_highbd_12_sub_pixel_avg_variance8x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002104 aom_highbd_sad8x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002105 aom_highbd_dist_wtd_sad8x32_avg_bits12,
2106 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002107
2108 HIGHBD_BFP(BLOCK_16X4, aom_highbd_sad16x4_bits12,
2109 aom_highbd_sad16x4_avg_bits12, aom_highbd_12_variance16x4,
2110 aom_highbd_12_sub_pixel_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002111 aom_highbd_12_sub_pixel_avg_variance16x4,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002112 aom_highbd_sad16x4x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002113 aom_highbd_dist_wtd_sad16x4_avg_bits12,
2114 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002115
2116 HIGHBD_BFP(BLOCK_4X16, aom_highbd_sad4x16_bits12,
2117 aom_highbd_sad4x16_avg_bits12, aom_highbd_12_variance4x16,
2118 aom_highbd_12_sub_pixel_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002119 aom_highbd_12_sub_pixel_avg_variance4x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002120 aom_highbd_sad4x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002121 aom_highbd_dist_wtd_sad4x16_avg_bits12,
2122 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002123
2124 HIGHBD_BFP(BLOCK_32X16, aom_highbd_sad32x16_bits12,
2125 aom_highbd_sad32x16_avg_bits12, aom_highbd_12_variance32x16,
2126 aom_highbd_12_sub_pixel_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002127 aom_highbd_12_sub_pixel_avg_variance32x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002128 aom_highbd_sad32x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002129 aom_highbd_dist_wtd_sad32x16_avg_bits12,
2130 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002131
2132 HIGHBD_BFP(BLOCK_16X32, aom_highbd_sad16x32_bits12,
2133 aom_highbd_sad16x32_avg_bits12, aom_highbd_12_variance16x32,
2134 aom_highbd_12_sub_pixel_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002135 aom_highbd_12_sub_pixel_avg_variance16x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002136 aom_highbd_sad16x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002137 aom_highbd_dist_wtd_sad16x32_avg_bits12,
2138 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002139
2140 HIGHBD_BFP(BLOCK_64X32, aom_highbd_sad64x32_bits12,
2141 aom_highbd_sad64x32_avg_bits12, aom_highbd_12_variance64x32,
2142 aom_highbd_12_sub_pixel_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002143 aom_highbd_12_sub_pixel_avg_variance64x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002144 aom_highbd_sad64x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002145 aom_highbd_dist_wtd_sad64x32_avg_bits12,
2146 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002147
2148 HIGHBD_BFP(BLOCK_32X64, aom_highbd_sad32x64_bits12,
2149 aom_highbd_sad32x64_avg_bits12, aom_highbd_12_variance32x64,
2150 aom_highbd_12_sub_pixel_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002151 aom_highbd_12_sub_pixel_avg_variance32x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002152 aom_highbd_sad32x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002153 aom_highbd_dist_wtd_sad32x64_avg_bits12,
2154 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002155
2156 HIGHBD_BFP(BLOCK_32X32, aom_highbd_sad32x32_bits12,
2157 aom_highbd_sad32x32_avg_bits12, aom_highbd_12_variance32x32,
2158 aom_highbd_12_sub_pixel_variance32x32,
2159 aom_highbd_12_sub_pixel_avg_variance32x32,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002160 aom_highbd_sad32x32x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002161 aom_highbd_dist_wtd_sad32x32_avg_bits12,
2162 aom_highbd_12_dist_wtd_sub_pixel_avg_variance32x32);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002163
2164 HIGHBD_BFP(BLOCK_64X64, aom_highbd_sad64x64_bits12,
2165 aom_highbd_sad64x64_avg_bits12, aom_highbd_12_variance64x64,
2166 aom_highbd_12_sub_pixel_variance64x64,
2167 aom_highbd_12_sub_pixel_avg_variance64x64,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002168 aom_highbd_sad64x64x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002169 aom_highbd_dist_wtd_sad64x64_avg_bits12,
2170 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002171
2172 HIGHBD_BFP(BLOCK_16X16, aom_highbd_sad16x16_bits12,
2173 aom_highbd_sad16x16_avg_bits12, aom_highbd_12_variance16x16,
2174 aom_highbd_12_sub_pixel_variance16x16,
2175 aom_highbd_12_sub_pixel_avg_variance16x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002176 aom_highbd_sad16x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002177 aom_highbd_dist_wtd_sad16x16_avg_bits12,
2178 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002179
2180 HIGHBD_BFP(BLOCK_16X8, aom_highbd_sad16x8_bits12,
2181 aom_highbd_sad16x8_avg_bits12, aom_highbd_12_variance16x8,
2182 aom_highbd_12_sub_pixel_variance16x8,
2183 aom_highbd_12_sub_pixel_avg_variance16x8,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002184 aom_highbd_sad16x8x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002185 aom_highbd_dist_wtd_sad16x8_avg_bits12,
2186 aom_highbd_12_dist_wtd_sub_pixel_avg_variance16x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002187
2188 HIGHBD_BFP(BLOCK_8X16, aom_highbd_sad8x16_bits12,
2189 aom_highbd_sad8x16_avg_bits12, aom_highbd_12_variance8x16,
2190 aom_highbd_12_sub_pixel_variance8x16,
2191 aom_highbd_12_sub_pixel_avg_variance8x16,
Cheng Chenbf3d4962017-11-01 14:48:52 -07002192 aom_highbd_sad8x16x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002193 aom_highbd_dist_wtd_sad8x16_avg_bits12,
2194 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x16);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002195
2196 HIGHBD_BFP(
2197 BLOCK_8X8, aom_highbd_sad8x8_bits12, aom_highbd_sad8x8_avg_bits12,
2198 aom_highbd_12_variance8x8, aom_highbd_12_sub_pixel_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002199 aom_highbd_12_sub_pixel_avg_variance8x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002200 aom_highbd_sad8x8x4d_bits12, aom_highbd_dist_wtd_sad8x8_avg_bits12,
2201 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002202
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002203 HIGHBD_BFP(
2204 BLOCK_8X4, aom_highbd_sad8x4_bits12, aom_highbd_sad8x4_avg_bits12,
2205 aom_highbd_12_variance8x4, aom_highbd_12_sub_pixel_variance8x4,
2206 aom_highbd_12_sub_pixel_avg_variance8x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002207 aom_highbd_sad8x4x4d_bits12, aom_highbd_dist_wtd_sad8x4_avg_bits12,
2208 aom_highbd_12_dist_wtd_sub_pixel_avg_variance8x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002209
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002210 HIGHBD_BFP(
2211 BLOCK_4X8, aom_highbd_sad4x8_bits12, aom_highbd_sad4x8_avg_bits12,
2212 aom_highbd_12_variance4x8, aom_highbd_12_sub_pixel_variance4x8,
2213 aom_highbd_12_sub_pixel_avg_variance4x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002214 aom_highbd_sad4x8x4d_bits12, aom_highbd_dist_wtd_sad4x8_avg_bits12,
2215 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x8);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002216
2217 HIGHBD_BFP(
2218 BLOCK_4X4, aom_highbd_sad4x4_bits12, aom_highbd_sad4x4_avg_bits12,
2219 aom_highbd_12_variance4x4, aom_highbd_12_sub_pixel_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002220 aom_highbd_12_sub_pixel_avg_variance4x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002221 aom_highbd_sad4x4x4d_bits12, aom_highbd_dist_wtd_sad4x4_avg_bits12,
2222 aom_highbd_12_dist_wtd_sub_pixel_avg_variance4x4);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002223
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002224 HIGHBD_BFP(BLOCK_128X128, aom_highbd_sad128x128_bits12,
2225 aom_highbd_sad128x128_avg_bits12,
2226 aom_highbd_12_variance128x128,
2227 aom_highbd_12_sub_pixel_variance128x128,
2228 aom_highbd_12_sub_pixel_avg_variance128x128,
2229 aom_highbd_sad128x128x4d_bits12,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002230 aom_highbd_dist_wtd_sad128x128_avg_bits12,
2231 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x128);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002232
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002233 HIGHBD_BFP(BLOCK_128X64, aom_highbd_sad128x64_bits12,
2234 aom_highbd_sad128x64_avg_bits12,
2235 aom_highbd_12_variance128x64,
2236 aom_highbd_12_sub_pixel_variance128x64,
2237 aom_highbd_12_sub_pixel_avg_variance128x64,
2238 aom_highbd_sad128x64x4d_bits12,
2239 aom_highbd_dist_wtd_sad128x64_avg_bits12,
2240 aom_highbd_12_dist_wtd_sub_pixel_avg_variance128x64);
Cheng Chenbf3d4962017-11-01 14:48:52 -07002241
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002242 HIGHBD_BFP(BLOCK_64X128, aom_highbd_sad64x128_bits12,
2243 aom_highbd_sad64x128_avg_bits12,
2244 aom_highbd_12_variance64x128,
2245 aom_highbd_12_sub_pixel_variance64x128,
2246 aom_highbd_12_sub_pixel_avg_variance64x128,
2247 aom_highbd_sad64x128x4d_bits12,
2248 aom_highbd_dist_wtd_sad64x128_avg_bits12,
2249 aom_highbd_12_dist_wtd_sub_pixel_avg_variance64x128);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002250
David Barkerf19f35f2017-05-22 16:33:22 +01002251 HIGHBD_MBFP(BLOCK_128X128, aom_highbd_masked_sad128x128_bits12,
2252 aom_highbd_12_masked_sub_pixel_variance128x128)
2253 HIGHBD_MBFP(BLOCK_128X64, aom_highbd_masked_sad128x64_bits12,
2254 aom_highbd_12_masked_sub_pixel_variance128x64)
2255 HIGHBD_MBFP(BLOCK_64X128, aom_highbd_masked_sad64x128_bits12,
2256 aom_highbd_12_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002257 HIGHBD_MBFP(BLOCK_64X64, aom_highbd_masked_sad64x64_bits12,
2258 aom_highbd_12_masked_sub_pixel_variance64x64)
2259 HIGHBD_MBFP(BLOCK_64X32, aom_highbd_masked_sad64x32_bits12,
2260 aom_highbd_12_masked_sub_pixel_variance64x32)
2261 HIGHBD_MBFP(BLOCK_32X64, aom_highbd_masked_sad32x64_bits12,
2262 aom_highbd_12_masked_sub_pixel_variance32x64)
2263 HIGHBD_MBFP(BLOCK_32X32, aom_highbd_masked_sad32x32_bits12,
2264 aom_highbd_12_masked_sub_pixel_variance32x32)
2265 HIGHBD_MBFP(BLOCK_32X16, aom_highbd_masked_sad32x16_bits12,
2266 aom_highbd_12_masked_sub_pixel_variance32x16)
2267 HIGHBD_MBFP(BLOCK_16X32, aom_highbd_masked_sad16x32_bits12,
2268 aom_highbd_12_masked_sub_pixel_variance16x32)
2269 HIGHBD_MBFP(BLOCK_16X16, aom_highbd_masked_sad16x16_bits12,
2270 aom_highbd_12_masked_sub_pixel_variance16x16)
2271 HIGHBD_MBFP(BLOCK_8X16, aom_highbd_masked_sad8x16_bits12,
2272 aom_highbd_12_masked_sub_pixel_variance8x16)
2273 HIGHBD_MBFP(BLOCK_16X8, aom_highbd_masked_sad16x8_bits12,
2274 aom_highbd_12_masked_sub_pixel_variance16x8)
2275 HIGHBD_MBFP(BLOCK_8X8, aom_highbd_masked_sad8x8_bits12,
2276 aom_highbd_12_masked_sub_pixel_variance8x8)
2277 HIGHBD_MBFP(BLOCK_4X8, aom_highbd_masked_sad4x8_bits12,
2278 aom_highbd_12_masked_sub_pixel_variance4x8)
2279 HIGHBD_MBFP(BLOCK_8X4, aom_highbd_masked_sad8x4_bits12,
2280 aom_highbd_12_masked_sub_pixel_variance8x4)
2281 HIGHBD_MBFP(BLOCK_4X4, aom_highbd_masked_sad4x4_bits12,
2282 aom_highbd_12_masked_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002283 HIGHBD_MBFP(BLOCK_64X16, aom_highbd_masked_sad64x16_bits12,
2284 aom_highbd_12_masked_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002285 HIGHBD_MBFP(BLOCK_16X64, aom_highbd_masked_sad16x64_bits12,
2286 aom_highbd_12_masked_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002287 HIGHBD_MBFP(BLOCK_32X8, aom_highbd_masked_sad32x8_bits12,
2288 aom_highbd_12_masked_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002289 HIGHBD_MBFP(BLOCK_8X32, aom_highbd_masked_sad8x32_bits12,
2290 aom_highbd_12_masked_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002291 HIGHBD_MBFP(BLOCK_16X4, aom_highbd_masked_sad16x4_bits12,
2292 aom_highbd_12_masked_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002293 HIGHBD_MBFP(BLOCK_4X16, aom_highbd_masked_sad4x16_bits12,
2294 aom_highbd_12_masked_sub_pixel_variance4x16)
Yaowu Xuf883b422016-08-30 14:01:10 -07002295 HIGHBD_OBFP(BLOCK_128X128, aom_highbd_obmc_sad128x128_bits12,
2296 aom_highbd_12_obmc_variance128x128,
2297 aom_highbd_12_obmc_sub_pixel_variance128x128)
2298 HIGHBD_OBFP(BLOCK_128X64, aom_highbd_obmc_sad128x64_bits12,
2299 aom_highbd_12_obmc_variance128x64,
2300 aom_highbd_12_obmc_sub_pixel_variance128x64)
2301 HIGHBD_OBFP(BLOCK_64X128, aom_highbd_obmc_sad64x128_bits12,
2302 aom_highbd_12_obmc_variance64x128,
2303 aom_highbd_12_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002304 HIGHBD_OBFP(BLOCK_64X64, aom_highbd_obmc_sad64x64_bits12,
2305 aom_highbd_12_obmc_variance64x64,
2306 aom_highbd_12_obmc_sub_pixel_variance64x64)
2307 HIGHBD_OBFP(BLOCK_64X32, aom_highbd_obmc_sad64x32_bits12,
2308 aom_highbd_12_obmc_variance64x32,
2309 aom_highbd_12_obmc_sub_pixel_variance64x32)
2310 HIGHBD_OBFP(BLOCK_32X64, aom_highbd_obmc_sad32x64_bits12,
2311 aom_highbd_12_obmc_variance32x64,
2312 aom_highbd_12_obmc_sub_pixel_variance32x64)
2313 HIGHBD_OBFP(BLOCK_32X32, aom_highbd_obmc_sad32x32_bits12,
2314 aom_highbd_12_obmc_variance32x32,
2315 aom_highbd_12_obmc_sub_pixel_variance32x32)
2316 HIGHBD_OBFP(BLOCK_32X16, aom_highbd_obmc_sad32x16_bits12,
2317 aom_highbd_12_obmc_variance32x16,
2318 aom_highbd_12_obmc_sub_pixel_variance32x16)
2319 HIGHBD_OBFP(BLOCK_16X32, aom_highbd_obmc_sad16x32_bits12,
2320 aom_highbd_12_obmc_variance16x32,
2321 aom_highbd_12_obmc_sub_pixel_variance16x32)
2322 HIGHBD_OBFP(BLOCK_16X16, aom_highbd_obmc_sad16x16_bits12,
2323 aom_highbd_12_obmc_variance16x16,
2324 aom_highbd_12_obmc_sub_pixel_variance16x16)
2325 HIGHBD_OBFP(BLOCK_8X16, aom_highbd_obmc_sad8x16_bits12,
2326 aom_highbd_12_obmc_variance8x16,
2327 aom_highbd_12_obmc_sub_pixel_variance8x16)
2328 HIGHBD_OBFP(BLOCK_16X8, aom_highbd_obmc_sad16x8_bits12,
2329 aom_highbd_12_obmc_variance16x8,
2330 aom_highbd_12_obmc_sub_pixel_variance16x8)
2331 HIGHBD_OBFP(BLOCK_8X8, aom_highbd_obmc_sad8x8_bits12,
2332 aom_highbd_12_obmc_variance8x8,
2333 aom_highbd_12_obmc_sub_pixel_variance8x8)
2334 HIGHBD_OBFP(BLOCK_4X8, aom_highbd_obmc_sad4x8_bits12,
2335 aom_highbd_12_obmc_variance4x8,
2336 aom_highbd_12_obmc_sub_pixel_variance4x8)
2337 HIGHBD_OBFP(BLOCK_8X4, aom_highbd_obmc_sad8x4_bits12,
2338 aom_highbd_12_obmc_variance8x4,
2339 aom_highbd_12_obmc_sub_pixel_variance8x4)
2340 HIGHBD_OBFP(BLOCK_4X4, aom_highbd_obmc_sad4x4_bits12,
2341 aom_highbd_12_obmc_variance4x4,
2342 aom_highbd_12_obmc_sub_pixel_variance4x4)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002343 HIGHBD_OBFP(BLOCK_64X16, aom_highbd_obmc_sad64x16_bits12,
2344 aom_highbd_12_obmc_variance64x16,
2345 aom_highbd_12_obmc_sub_pixel_variance64x16)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002346 HIGHBD_OBFP(BLOCK_16X64, aom_highbd_obmc_sad16x64_bits12,
2347 aom_highbd_12_obmc_variance16x64,
2348 aom_highbd_12_obmc_sub_pixel_variance16x64)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002349 HIGHBD_OBFP(BLOCK_32X8, aom_highbd_obmc_sad32x8_bits12,
2350 aom_highbd_12_obmc_variance32x8,
2351 aom_highbd_12_obmc_sub_pixel_variance32x8)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002352 HIGHBD_OBFP(BLOCK_8X32, aom_highbd_obmc_sad8x32_bits12,
2353 aom_highbd_12_obmc_variance8x32,
2354 aom_highbd_12_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002355 HIGHBD_OBFP(BLOCK_16X4, aom_highbd_obmc_sad16x4_bits12,
2356 aom_highbd_12_obmc_variance16x4,
2357 aom_highbd_12_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002358 HIGHBD_OBFP(BLOCK_4X16, aom_highbd_obmc_sad4x16_bits12,
2359 aom_highbd_12_obmc_variance4x16,
2360 aom_highbd_12_obmc_sub_pixel_variance4x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002361 break;
2362
2363 default:
2364 assert(0 &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002365 "cm->seq_params.bit_depth should be AOM_BITS_8, "
Yaowu Xuf883b422016-08-30 14:01:10 -07002366 "AOM_BITS_10 or AOM_BITS_12");
Yaowu Xuc27fc142016-08-22 16:08:15 -07002367 }
2368 }
2369}
Yaowu Xuc27fc142016-08-22 16:08:15 -07002370
Yaowu Xuf883b422016-08-30 14:01:10 -07002371static void realloc_segmentation_maps(AV1_COMP *cpi) {
2372 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002373
2374 // Create the encoder segmentation map and set all entries to 0
Yaowu Xuf883b422016-08-30 14:01:10 -07002375 aom_free(cpi->segmentation_map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002376 CHECK_MEM_ERROR(cm, cpi->segmentation_map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002377 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002378
2379 // Create a map used for cyclic background refresh.
Yaowu Xuf883b422016-08-30 14:01:10 -07002380 if (cpi->cyclic_refresh) av1_cyclic_refresh_free(cpi->cyclic_refresh);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002381 CHECK_MEM_ERROR(cm, cpi->cyclic_refresh,
Yaowu Xuf883b422016-08-30 14:01:10 -07002382 av1_cyclic_refresh_alloc(cm->mi_rows, cm->mi_cols));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002383
2384 // Create a map used to mark inactive areas.
Yaowu Xuf883b422016-08-30 14:01:10 -07002385 aom_free(cpi->active_map.map);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002386 CHECK_MEM_ERROR(cm, cpi->active_map.map,
Yaowu Xuf883b422016-08-30 14:01:10 -07002387 aom_calloc(cm->mi_rows * cm->mi_cols, 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002388}
2389
Yaowu Xuf883b422016-08-30 14:01:10 -07002390void av1_change_config(struct AV1_COMP *cpi, const AV1EncoderConfig *oxcf) {
2391 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002392 SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002393 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002394 RATE_CONTROL *const rc = &cpi->rc;
hui sud9a812b2017-07-06 14:34:37 -07002395 MACROBLOCK *const x = &cpi->td.mb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002396
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002397 if (seq_params->profile != oxcf->profile) seq_params->profile = oxcf->profile;
2398 seq_params->bit_depth = oxcf->bit_depth;
2399 seq_params->color_primaries = oxcf->color_primaries;
2400 seq_params->transfer_characteristics = oxcf->transfer_characteristics;
2401 seq_params->matrix_coefficients = oxcf->matrix_coefficients;
2402 seq_params->monochrome = oxcf->monochrome;
2403 seq_params->chroma_sample_position = oxcf->chroma_sample_position;
2404 seq_params->color_range = oxcf->color_range;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002405
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002406 assert(IMPLIES(seq_params->profile <= PROFILE_1,
2407 seq_params->bit_depth <= AOM_BITS_10));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002408
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002409 cm->timing_info_present = oxcf->timing_info_present;
Andrey Norkin795ba872018-03-06 13:24:14 -08002410 cm->timing_info.num_units_in_display_tick =
2411 oxcf->timing_info.num_units_in_display_tick;
2412 cm->timing_info.time_scale = oxcf->timing_info.time_scale;
2413 cm->timing_info.equal_picture_interval =
2414 oxcf->timing_info.equal_picture_interval;
2415 cm->timing_info.num_ticks_per_picture =
2416 oxcf->timing_info.num_ticks_per_picture;
2417
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002418 seq_params->display_model_info_present_flag =
Andrey Norkin26495512018-06-20 17:13:11 -07002419 oxcf->display_model_info_present_flag;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002420 seq_params->decoder_model_info_present_flag =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002421 oxcf->decoder_model_info_present_flag;
Andrey Norkin795ba872018-03-06 13:24:14 -08002422 if (oxcf->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002423 // set the decoder model parameters in schedule mode
Andrey Norkin795ba872018-03-06 13:24:14 -08002424 cm->buffer_model.num_units_in_decoding_tick =
2425 oxcf->buffer_model.num_units_in_decoding_tick;
Wan-Teh Changf64b3bc2018-07-02 09:42:39 -07002426 cm->buffer_removal_time_present = 1;
Andrey Norkin795ba872018-03-06 13:24:14 -08002427 set_aom_dec_model_info(&cm->buffer_model);
Andrey Norkin26495512018-06-20 17:13:11 -07002428 set_dec_model_op_parameters(&cm->op_params[0]);
2429 } else if (cm->timing_info_present &&
2430 cm->timing_info.equal_picture_interval &&
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002431 !seq_params->decoder_model_info_present_flag) {
Andrey Norkin26495512018-06-20 17:13:11 -07002432 // set the decoder model parameters in resource availability mode
2433 set_resource_availability_parameters(&cm->op_params[0]);
Andrey Norkinc7511de2018-06-22 12:31:06 -07002434 } else {
2435 cm->op_params[0].initial_display_delay =
2436 10; // Default value (not signaled)
Andrey Norkin795ba872018-03-06 13:24:14 -08002437 }
Andrey Norkin28e9ce22018-01-08 10:11:21 -08002438
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002439 update_film_grain_parameters(cpi, oxcf);
Andrey Norkin6f1c2f72018-01-15 20:08:52 -08002440
Yaowu Xuc27fc142016-08-22 16:08:15 -07002441 cpi->oxcf = *oxcf;
Maxym Dmytrychenkocc6e0e12018-02-05 16:35:37 +01002442 cpi->common.options = oxcf->cfg;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002443 x->e_mbd.bd = (int)seq_params->bit_depth;
hui sud9a812b2017-07-06 14:34:37 -07002444 x->e_mbd.global_motion = cm->global_motion;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002445
Yaowu Xuf883b422016-08-30 14:01:10 -07002446 if ((oxcf->pass == 0) && (oxcf->rc_mode == AOM_Q)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002447 rc->baseline_gf_interval = FIXED_GF_INTERVAL;
2448 } else {
2449 rc->baseline_gf_interval = (MIN_GF_INTERVAL + MAX_GF_INTERVAL) / 2;
2450 }
2451
2452 cpi->refresh_last_frame = 1;
2453 cpi->refresh_golden_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002454 cpi->refresh_bwd_ref_frame = 0;
Zoe Liue9b15e22017-07-19 15:53:01 -07002455 cpi->refresh_alt2_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002456
Debargha Mukherjee229fdc82018-03-10 07:45:33 -08002457 cm->refresh_frame_context = (oxcf->frame_parallel_decoding_mode)
2458 ? REFRESH_FRAME_CONTEXT_DISABLED
2459 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002460 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08002461 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01002462
Alex Converse74ad0912017-07-18 10:22:58 -07002463 if (x->palette_buffer == NULL) {
hui sud9a812b2017-07-06 14:34:37 -07002464 CHECK_MEM_ERROR(cm, x->palette_buffer,
2465 aom_memalign(16, sizeof(*x->palette_buffer)));
2466 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002467
2468 if (x->tmp_conv_dst == NULL) {
2469 CHECK_MEM_ERROR(
2470 cm, x->tmp_conv_dst,
2471 aom_memalign(32, MAX_SB_SIZE * MAX_SB_SIZE * sizeof(*x->tmp_conv_dst)));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002472 x->e_mbd.tmp_conv_dst = x->tmp_conv_dst;
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002473 }
2474 for (int i = 0; i < 2; ++i) {
2475 if (x->tmp_obmc_bufs[i] == NULL) {
2476 CHECK_MEM_ERROR(cm, x->tmp_obmc_bufs[i],
wenyao.liu22d8ab32018-10-16 09:11:29 +08002477 aom_memalign(32, 2 * MAX_MB_PLANE * MAX_SB_SQUARE *
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002478 sizeof(*x->tmp_obmc_bufs[i])));
Urvang Joshie58f6ec2018-09-10 15:10:12 -07002479 x->e_mbd.tmp_obmc_bufs[i] = x->tmp_obmc_bufs[i];
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07002480 }
2481 }
2482
Yaowu Xuf883b422016-08-30 14:01:10 -07002483 av1_reset_segment_features(cm);
Debargha Mukherjeeb2147752017-11-01 07:00:45 -07002484 set_high_precision_mv(cpi, 1, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002485
Yaowu Xuc27fc142016-08-22 16:08:15 -07002486 set_rc_buffer_sizes(rc, &cpi->oxcf);
2487
2488 // Under a configuration change, where maximum_buffer_size may change,
2489 // keep buffer level clipped to the maximum allowed buffer size.
Yaowu Xuf883b422016-08-30 14:01:10 -07002490 rc->bits_off_target = AOMMIN(rc->bits_off_target, rc->maximum_buffer_size);
2491 rc->buffer_level = AOMMIN(rc->buffer_level, rc->maximum_buffer_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002492
2493 // Set up frame rate and related parameters rate control values.
Yaowu Xuf883b422016-08-30 14:01:10 -07002494 av1_new_framerate(cpi, cpi->framerate);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002495
2496 // Set absolute upper and lower quality limits
2497 rc->worst_quality = cpi->oxcf.worst_allowed_q;
2498 rc->best_quality = cpi->oxcf.best_allowed_q;
2499
Urvang Joshib55cb5e2018-09-12 14:50:21 -07002500 cm->interp_filter = oxcf->large_scale_tile ? EIGHTTAP_REGULAR : SWITCHABLE;
Yue Chen5380cb52018-02-23 15:33:21 -08002501 cm->switchable_motion_mode = 1;
2502
Yaowu Xuc27fc142016-08-22 16:08:15 -07002503 if (cpi->oxcf.render_width > 0 && cpi->oxcf.render_height > 0) {
2504 cm->render_width = cpi->oxcf.render_width;
2505 cm->render_height = cpi->oxcf.render_height;
2506 } else {
2507 cm->render_width = cpi->oxcf.width;
2508 cm->render_height = cpi->oxcf.height;
2509 }
2510 cm->width = cpi->oxcf.width;
2511 cm->height = cpi->oxcf.height;
2512
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002513 int sb_size = seq_params->sb_size;
Urvang Joshie4530f82018-01-09 11:43:37 -08002514 // Superblock size should not be updated after the first key frame.
2515 if (!cpi->seq_params_locked) {
2516 set_sb_size(&cm->seq_params, select_sb_size(cpi));
2517 }
Dominic Symes917d6c02017-10-11 18:00:52 +02002518
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002519 if (cpi->initial_width || sb_size != seq_params->sb_size) {
Dominic Symes917d6c02017-10-11 18:00:52 +02002520 if (cm->width > cpi->initial_width || cm->height > cpi->initial_height ||
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002521 seq_params->sb_size != sb_size) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002522 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002523 av1_free_pc_tree(&cpi->td, num_planes);
Cheng Chen46f30c72017-09-07 11:13:33 -07002524 alloc_compressor_data(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002525 realloc_segmentation_maps(cpi);
2526 cpi->initial_width = cpi->initial_height = 0;
2527 }
2528 }
2529 update_frame_size(cpi);
2530
2531 cpi->alt_ref_source = NULL;
2532 rc->is_src_frame_alt_ref = 0;
2533
Yaowu Xuc27fc142016-08-22 16:08:15 -07002534 rc->is_bwd_ref_frame = 0;
2535 rc->is_last_bipred_frame = 0;
2536 rc->is_bipred_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002537
Yaowu Xuc27fc142016-08-22 16:08:15 -07002538 set_tile_info(cpi);
2539
2540 cpi->ext_refresh_frame_flags_pending = 0;
2541 cpi->ext_refresh_frame_context_pending = 0;
2542
Yaowu Xuc27fc142016-08-22 16:08:15 -07002543 highbd_set_var_fns(cpi);
Imdad Sardharwallabf2cc012018-02-09 17:32:10 +00002544
Debargha Mukherjeeedd77252018-03-25 12:01:38 -07002545 // Init sequence level coding tools
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002546 // This should not be called after the first key frame.
2547 if (!cpi->seq_params_locked) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07002548 seq_params->operating_points_cnt_minus_1 =
Adrian Grangec56f6ec2018-05-31 14:19:32 -07002549 cm->number_spatial_layers > 1 ? cm->number_spatial_layers - 1 : 0;
Andrey Norkin26495512018-06-20 17:13:11 -07002550 init_seq_coding_tools(&cm->seq_params, cm, oxcf);
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002551 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07002552}
2553
Yaowu Xuf883b422016-08-30 14:01:10 -07002554AV1_COMP *av1_create_compressor(AV1EncoderConfig *oxcf,
2555 BufferPool *const pool) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07002556 unsigned int i;
Yaowu Xuf883b422016-08-30 14:01:10 -07002557 AV1_COMP *volatile const cpi = aom_memalign(32, sizeof(AV1_COMP));
2558 AV1_COMMON *volatile const cm = cpi != NULL ? &cpi->common : NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002559
2560 if (!cm) return NULL;
2561
Yaowu Xuf883b422016-08-30 14:01:10 -07002562 av1_zero(*cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002563
Wan-Teh Changa2fad3e2018-07-19 16:55:19 -07002564 // The jmp_buf is valid only for the duration of the function that calls
2565 // setjmp(). Therefore, this function must reset the 'setjmp' field to 0
2566 // before it returns.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002567 if (setjmp(cm->error.jmp)) {
2568 cm->error.setjmp = 0;
Yaowu Xuf883b422016-08-30 14:01:10 -07002569 av1_remove_compressor(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002570 return 0;
2571 }
2572
2573 cm->error.setjmp = 1;
Cheng Chen46f30c72017-09-07 11:13:33 -07002574 cm->alloc_mi = enc_alloc_mi;
2575 cm->free_mi = enc_free_mi;
2576 cm->setup_mi = enc_setup_mi;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002577
Angie Chianga5d96c42016-10-21 16:16:56 -07002578 CHECK_MEM_ERROR(cm, cm->fc,
2579 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->fc)));
David Turner1bcefb32018-11-19 17:54:00 +00002580 CHECK_MEM_ERROR(
2581 cm, cm->default_frame_context,
2582 (FRAME_CONTEXT *)aom_memalign(32, sizeof(*cm->default_frame_context)));
Angie Chianga5d96c42016-10-21 16:16:56 -07002583 memset(cm->fc, 0, sizeof(*cm->fc));
David Turner1bcefb32018-11-19 17:54:00 +00002584 memset(cm->default_frame_context, 0, sizeof(*cm->default_frame_context));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002585
2586 cpi->resize_state = 0;
2587 cpi->resize_avg_qp = 0;
2588 cpi->resize_buffer_underflow = 0;
Fergus Simpsonddc846e2017-04-24 18:09:13 -07002589
Yaowu Xuc27fc142016-08-22 16:08:15 -07002590 cpi->common.buffer_pool = pool;
2591
2592 init_config(cpi, oxcf);
Yaowu Xuf883b422016-08-30 14:01:10 -07002593 av1_rc_init(&cpi->oxcf, oxcf->pass, &cpi->rc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002594
David Turnerd2a592e2018-11-16 14:59:31 +00002595 cm->current_frame.frame_number = 0;
David Turnera4c96252019-01-11 16:36:39 +00002596 cm->current_frame_id = -1;
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07002597 cpi->seq_params_locked = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002598 cpi->partition_search_skippable_frame = 0;
2599 cpi->tile_data = NULL;
David Turnere7ebf902018-12-04 14:04:55 +00002600 cpi->last_show_frame_buf = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002601 realloc_segmentation_maps(cpi);
2602
Jingning Hanf050fc12018-03-09 14:53:33 -08002603 memset(cpi->nmv_costs, 0, sizeof(cpi->nmv_costs));
2604 memset(cpi->nmv_costs_hp, 0, sizeof(cpi->nmv_costs_hp));
James Zern01a9d702017-08-25 19:09:33 +00002605
Yaowu Xuc27fc142016-08-22 16:08:15 -07002606 for (i = 0; i < (sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]));
2607 i++) {
2608 CHECK_MEM_ERROR(
2609 cm, cpi->mbgraph_stats[i].mb_stats,
Yaowu Xuf883b422016-08-30 14:01:10 -07002610 aom_calloc(cm->MBs * sizeof(*cpi->mbgraph_stats[i].mb_stats), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002611 }
2612
2613#if CONFIG_FP_MB_STATS
2614 cpi->use_fp_mb_stats = 0;
2615 if (cpi->use_fp_mb_stats) {
2616 // a place holder used to store the first pass mb stats in the first pass
2617 CHECK_MEM_ERROR(cm, cpi->twopass.frame_mb_stats_buf,
Yaowu Xuf883b422016-08-30 14:01:10 -07002618 aom_calloc(cm->MBs * sizeof(uint8_t), 1));
Yaowu Xuc27fc142016-08-22 16:08:15 -07002619 } else {
2620 cpi->twopass.frame_mb_stats_buf = NULL;
2621 }
2622#endif
2623
2624 cpi->refresh_alt_ref_frame = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002625
2626 cpi->b_calculate_psnr = CONFIG_INTERNAL_STATS;
2627#if CONFIG_INTERNAL_STATS
2628 cpi->b_calculate_blockiness = 1;
2629 cpi->b_calculate_consistency = 1;
2630 cpi->total_inconsistency = 0;
2631 cpi->psnr.worst = 100.0;
2632 cpi->worst_ssim = 100.0;
2633
2634 cpi->count = 0;
2635 cpi->bytes = 0;
Debargha Mukherjee0857e662019-01-04 16:22:09 -08002636#if CONFIG_SPEED_STATS
2637 cpi->tx_search_count = 0;
2638#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002639
2640 if (cpi->b_calculate_psnr) {
2641 cpi->total_sq_error = 0;
2642 cpi->total_samples = 0;
2643 cpi->tot_recode_hits = 0;
2644 cpi->summed_quality = 0;
2645 cpi->summed_weights = 0;
2646 }
2647
2648 cpi->fastssim.worst = 100.0;
2649 cpi->psnrhvs.worst = 100.0;
2650
2651 if (cpi->b_calculate_blockiness) {
2652 cpi->total_blockiness = 0;
2653 cpi->worst_blockiness = 0.0;
2654 }
2655
2656 if (cpi->b_calculate_consistency) {
2657 CHECK_MEM_ERROR(cm, cpi->ssim_vars,
Yaowu Xuf883b422016-08-30 14:01:10 -07002658 aom_malloc(sizeof(*cpi->ssim_vars) * 4 *
Yaowu Xuc27fc142016-08-22 16:08:15 -07002659 cpi->common.mi_rows * cpi->common.mi_cols));
2660 cpi->worst_consistency = 100.0;
2661 }
2662#endif
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002663#if CONFIG_ENTROPY_STATS
2664 av1_zero(aggregate_fc);
2665#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002666
2667 cpi->first_time_stamp_ever = INT64_MAX;
2668
Jingning Hanf050fc12018-03-09 14:53:33 -08002669 cpi->td.mb.nmvcost[0] = &cpi->nmv_costs[0][MV_MAX];
2670 cpi->td.mb.nmvcost[1] = &cpi->nmv_costs[1][MV_MAX];
2671 cpi->td.mb.nmvcost_hp[0] = &cpi->nmv_costs_hp[0][MV_MAX];
2672 cpi->td.mb.nmvcost_hp[1] = &cpi->nmv_costs_hp[1][MV_MAX];
James Zern01a9d702017-08-25 19:09:33 +00002673
Yaowu Xuc27fc142016-08-22 16:08:15 -07002674#ifdef OUTPUT_YUV_SKINMAP
2675 yuv_skinmap_file = fopen("skinmap.yuv", "ab");
2676#endif
2677#ifdef OUTPUT_YUV_REC
2678 yuv_rec_file = fopen("rec.yuv", "wb");
2679#endif
2680
Yaowu Xuc27fc142016-08-22 16:08:15 -07002681 if (oxcf->pass == 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07002682 av1_init_first_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002683 } else if (oxcf->pass == 2) {
2684 const size_t packet_sz = sizeof(FIRSTPASS_STATS);
2685 const int packets = (int)(oxcf->two_pass_stats_in.sz / packet_sz);
2686
2687#if CONFIG_FP_MB_STATS
2688 if (cpi->use_fp_mb_stats) {
2689 const size_t psz = cpi->common.MBs * sizeof(uint8_t);
2690 const int ps = (int)(oxcf->firstpass_mb_stats_in.sz / psz);
2691
2692 cpi->twopass.firstpass_mb_stats.mb_stats_start =
2693 oxcf->firstpass_mb_stats_in.buf;
2694 cpi->twopass.firstpass_mb_stats.mb_stats_end =
2695 cpi->twopass.firstpass_mb_stats.mb_stats_start +
2696 (ps - 1) * cpi->common.MBs * sizeof(uint8_t);
2697 }
2698#endif
2699
2700 cpi->twopass.stats_in_start = oxcf->two_pass_stats_in.buf;
2701 cpi->twopass.stats_in = cpi->twopass.stats_in_start;
2702 cpi->twopass.stats_in_end = &cpi->twopass.stats_in[packets - 1];
2703
Yaowu Xuf883b422016-08-30 14:01:10 -07002704 av1_init_second_pass(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002705 }
2706
Jingning Hand064cf02017-06-01 10:00:39 -07002707 CHECK_MEM_ERROR(
2708 cm, cpi->td.mb.above_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002709 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002710 sizeof(*cpi->td.mb.above_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002711 CHECK_MEM_ERROR(
2712 cm, cpi->td.mb.left_pred_buf,
Yue Chen1a799252018-03-01 16:47:41 -08002713 (uint8_t *)aom_memalign(16, MAX_MB_PLANE * MAX_SB_SQUARE *
Johannb0ef6ff2018-02-08 14:32:21 -08002714 sizeof(*cpi->td.mb.left_pred_buf)));
Jingning Hand064cf02017-06-01 10:00:39 -07002715
2716 CHECK_MEM_ERROR(cm, cpi->td.mb.wsrc_buf,
2717 (int32_t *)aom_memalign(
2718 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.wsrc_buf)));
2719
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302720 CHECK_MEM_ERROR(
2721 cm, cpi->td.mb.inter_modes_info,
2722 (InterModesInfo *)aom_malloc(sizeof(*cpi->td.mb.inter_modes_info)));
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05302723
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05302724 for (int x = 0; x < 2; x++)
2725 for (int y = 0; y < 2; y++)
2726 CHECK_MEM_ERROR(
2727 cm, cpi->td.mb.hash_value_buffer[x][y],
2728 (uint32_t *)aom_malloc(AOM_BUFFER_SIZE_FOR_BLOCK_HASH *
2729 sizeof(*cpi->td.mb.hash_value_buffer[0][0])));
2730
2731 cpi->td.mb.g_crc_initialized = 0;
2732
Jingning Hand064cf02017-06-01 10:00:39 -07002733 CHECK_MEM_ERROR(cm, cpi->td.mb.mask_buf,
2734 (int32_t *)aom_memalign(
2735 16, MAX_SB_SQUARE * sizeof(*cpi->td.mb.mask_buf)));
2736
David Turner04b70d82019-01-24 15:39:19 +00002737 av1_set_speed_features_framesize_independent(cpi, oxcf->speed);
2738 av1_set_speed_features_framesize_dependent(cpi, oxcf->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002739
Yue Chen7cae98f2018-08-24 10:43:16 -07002740 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
2741 int mi_cols = ALIGN_POWER_OF_TWO(cm->mi_cols, MAX_MIB_SIZE_LOG2);
2742 int mi_rows = ALIGN_POWER_OF_TWO(cm->mi_rows, MAX_MIB_SIZE_LOG2);
2743
2744 CHECK_MEM_ERROR(cm, cpi->tpl_stats[frame].tpl_stats_ptr,
2745 aom_calloc(mi_rows * mi_cols,
2746 sizeof(*cpi->tpl_stats[frame].tpl_stats_ptr)));
2747 cpi->tpl_stats[frame].is_valid = 0;
2748 cpi->tpl_stats[frame].width = mi_cols;
2749 cpi->tpl_stats[frame].height = mi_rows;
2750 cpi->tpl_stats[frame].stride = mi_cols;
2751 cpi->tpl_stats[frame].mi_rows = cm->mi_rows;
2752 cpi->tpl_stats[frame].mi_cols = cm->mi_cols;
2753 }
2754
chiyotsai92ed0dd2019-01-25 14:50:14 -08002755#if CONFIG_COLLECT_PARTITION_STATS
2756 av1_zero(cpi->partition_stats);
2757#endif
2758
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002759#define BFP(BT, SDF, SDAF, VF, SVF, SVAF, SDX4DF, JSDAF, JSVAF) \
2760 cpi->fn_ptr[BT].sdf = SDF; \
2761 cpi->fn_ptr[BT].sdaf = SDAF; \
2762 cpi->fn_ptr[BT].vf = VF; \
2763 cpi->fn_ptr[BT].svf = SVF; \
2764 cpi->fn_ptr[BT].svaf = SVAF; \
2765 cpi->fn_ptr[BT].sdx4df = SDX4DF; \
2766 cpi->fn_ptr[BT].jsdaf = JSDAF; \
Cheng Chenf78632e2017-10-20 15:30:51 -07002767 cpi->fn_ptr[BT].jsvaf = JSVAF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002768
Cheng Chenf78632e2017-10-20 15:30:51 -07002769 BFP(BLOCK_4X16, aom_sad4x16, aom_sad4x16_avg, aom_variance4x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002770 aom_sub_pixel_variance4x16, aom_sub_pixel_avg_variance4x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002771 aom_sad4x16x4d, aom_dist_wtd_sad4x16_avg,
2772 aom_dist_wtd_sub_pixel_avg_variance4x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002773
2774 BFP(BLOCK_16X4, aom_sad16x4, aom_sad16x4_avg, aom_variance16x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002775 aom_sub_pixel_variance16x4, aom_sub_pixel_avg_variance16x4,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002776 aom_sad16x4x4d, aom_dist_wtd_sad16x4_avg,
2777 aom_dist_wtd_sub_pixel_avg_variance16x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002778
2779 BFP(BLOCK_8X32, aom_sad8x32, aom_sad8x32_avg, aom_variance8x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002780 aom_sub_pixel_variance8x32, aom_sub_pixel_avg_variance8x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002781 aom_sad8x32x4d, aom_dist_wtd_sad8x32_avg,
2782 aom_dist_wtd_sub_pixel_avg_variance8x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002783
2784 BFP(BLOCK_32X8, aom_sad32x8, aom_sad32x8_avg, aom_variance32x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002785 aom_sub_pixel_variance32x8, aom_sub_pixel_avg_variance32x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002786 aom_sad32x8x4d, aom_dist_wtd_sad32x8_avg,
2787 aom_dist_wtd_sub_pixel_avg_variance32x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002788
2789 BFP(BLOCK_16X64, aom_sad16x64, aom_sad16x64_avg, aom_variance16x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002790 aom_sub_pixel_variance16x64, aom_sub_pixel_avg_variance16x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002791 aom_sad16x64x4d, aom_dist_wtd_sad16x64_avg,
2792 aom_dist_wtd_sub_pixel_avg_variance16x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002793
2794 BFP(BLOCK_64X16, aom_sad64x16, aom_sad64x16_avg, aom_variance64x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002795 aom_sub_pixel_variance64x16, aom_sub_pixel_avg_variance64x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002796 aom_sad64x16x4d, aom_dist_wtd_sad64x16_avg,
2797 aom_dist_wtd_sub_pixel_avg_variance64x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002798
Cheng Chenf78632e2017-10-20 15:30:51 -07002799 BFP(BLOCK_128X128, aom_sad128x128, aom_sad128x128_avg, aom_variance128x128,
2800 aom_sub_pixel_variance128x128, aom_sub_pixel_avg_variance128x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002801 aom_sad128x128x4d, aom_dist_wtd_sad128x128_avg,
2802 aom_dist_wtd_sub_pixel_avg_variance128x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002803
2804 BFP(BLOCK_128X64, aom_sad128x64, aom_sad128x64_avg, aom_variance128x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002805 aom_sub_pixel_variance128x64, aom_sub_pixel_avg_variance128x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002806 aom_sad128x64x4d, aom_dist_wtd_sad128x64_avg,
2807 aom_dist_wtd_sub_pixel_avg_variance128x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002808
2809 BFP(BLOCK_64X128, aom_sad64x128, aom_sad64x128_avg, aom_variance64x128,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002810 aom_sub_pixel_variance64x128, aom_sub_pixel_avg_variance64x128,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002811 aom_sad64x128x4d, aom_dist_wtd_sad64x128_avg,
2812 aom_dist_wtd_sub_pixel_avg_variance64x128)
Cheng Chenf78632e2017-10-20 15:30:51 -07002813
2814 BFP(BLOCK_32X16, aom_sad32x16, aom_sad32x16_avg, aom_variance32x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002815 aom_sub_pixel_variance32x16, aom_sub_pixel_avg_variance32x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002816 aom_sad32x16x4d, aom_dist_wtd_sad32x16_avg,
2817 aom_dist_wtd_sub_pixel_avg_variance32x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002818
2819 BFP(BLOCK_16X32, aom_sad16x32, aom_sad16x32_avg, aom_variance16x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002820 aom_sub_pixel_variance16x32, aom_sub_pixel_avg_variance16x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002821 aom_sad16x32x4d, aom_dist_wtd_sad16x32_avg,
2822 aom_dist_wtd_sub_pixel_avg_variance16x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002823
2824 BFP(BLOCK_64X32, aom_sad64x32, aom_sad64x32_avg, aom_variance64x32,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002825 aom_sub_pixel_variance64x32, aom_sub_pixel_avg_variance64x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002826 aom_sad64x32x4d, aom_dist_wtd_sad64x32_avg,
2827 aom_dist_wtd_sub_pixel_avg_variance64x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002828
2829 BFP(BLOCK_32X64, aom_sad32x64, aom_sad32x64_avg, aom_variance32x64,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002830 aom_sub_pixel_variance32x64, aom_sub_pixel_avg_variance32x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002831 aom_sad32x64x4d, aom_dist_wtd_sad32x64_avg,
2832 aom_dist_wtd_sub_pixel_avg_variance32x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002833
2834 BFP(BLOCK_32X32, aom_sad32x32, aom_sad32x32_avg, aom_variance32x32,
2835 aom_sub_pixel_variance32x32, aom_sub_pixel_avg_variance32x32,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002836 aom_sad32x32x4d, aom_dist_wtd_sad32x32_avg,
2837 aom_dist_wtd_sub_pixel_avg_variance32x32)
Cheng Chenf78632e2017-10-20 15:30:51 -07002838
2839 BFP(BLOCK_64X64, aom_sad64x64, aom_sad64x64_avg, aom_variance64x64,
2840 aom_sub_pixel_variance64x64, aom_sub_pixel_avg_variance64x64,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002841 aom_sad64x64x4d, aom_dist_wtd_sad64x64_avg,
2842 aom_dist_wtd_sub_pixel_avg_variance64x64)
Cheng Chenf78632e2017-10-20 15:30:51 -07002843
2844 BFP(BLOCK_16X16, aom_sad16x16, aom_sad16x16_avg, aom_variance16x16,
2845 aom_sub_pixel_variance16x16, aom_sub_pixel_avg_variance16x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002846 aom_sad16x16x4d, aom_dist_wtd_sad16x16_avg,
2847 aom_dist_wtd_sub_pixel_avg_variance16x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002848
2849 BFP(BLOCK_16X8, aom_sad16x8, aom_sad16x8_avg, aom_variance16x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002850 aom_sub_pixel_variance16x8, aom_sub_pixel_avg_variance16x8,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002851 aom_sad16x8x4d, aom_dist_wtd_sad16x8_avg,
2852 aom_dist_wtd_sub_pixel_avg_variance16x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002853
2854 BFP(BLOCK_8X16, aom_sad8x16, aom_sad8x16_avg, aom_variance8x16,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002855 aom_sub_pixel_variance8x16, aom_sub_pixel_avg_variance8x16,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002856 aom_sad8x16x4d, aom_dist_wtd_sad8x16_avg,
2857 aom_dist_wtd_sub_pixel_avg_variance8x16)
Cheng Chenf78632e2017-10-20 15:30:51 -07002858
2859 BFP(BLOCK_8X8, aom_sad8x8, aom_sad8x8_avg, aom_variance8x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002860 aom_sub_pixel_variance8x8, aom_sub_pixel_avg_variance8x8, aom_sad8x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002861 aom_dist_wtd_sad8x8_avg, aom_dist_wtd_sub_pixel_avg_variance8x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002862
2863 BFP(BLOCK_8X4, aom_sad8x4, aom_sad8x4_avg, aom_variance8x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002864 aom_sub_pixel_variance8x4, aom_sub_pixel_avg_variance8x4, aom_sad8x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002865 aom_dist_wtd_sad8x4_avg, aom_dist_wtd_sub_pixel_avg_variance8x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002866
2867 BFP(BLOCK_4X8, aom_sad4x8, aom_sad4x8_avg, aom_variance4x8,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002868 aom_sub_pixel_variance4x8, aom_sub_pixel_avg_variance4x8, aom_sad4x8x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002869 aom_dist_wtd_sad4x8_avg, aom_dist_wtd_sub_pixel_avg_variance4x8)
Cheng Chenf78632e2017-10-20 15:30:51 -07002870
2871 BFP(BLOCK_4X4, aom_sad4x4, aom_sad4x4_avg, aom_variance4x4,
Kyle Siefringef6e2df2018-04-10 14:51:35 -04002872 aom_sub_pixel_variance4x4, aom_sub_pixel_avg_variance4x4, aom_sad4x4x4d,
Debargha Mukherjee0c96c112018-12-20 16:04:18 -08002873 aom_dist_wtd_sad4x4_avg, aom_dist_wtd_sub_pixel_avg_variance4x4)
Cheng Chenf78632e2017-10-20 15:30:51 -07002874
Yaowu Xuc27fc142016-08-22 16:08:15 -07002875#define OBFP(BT, OSDF, OVF, OSVF) \
2876 cpi->fn_ptr[BT].osdf = OSDF; \
2877 cpi->fn_ptr[BT].ovf = OVF; \
2878 cpi->fn_ptr[BT].osvf = OSVF;
2879
Yaowu Xuf883b422016-08-30 14:01:10 -07002880 OBFP(BLOCK_128X128, aom_obmc_sad128x128, aom_obmc_variance128x128,
2881 aom_obmc_sub_pixel_variance128x128)
2882 OBFP(BLOCK_128X64, aom_obmc_sad128x64, aom_obmc_variance128x64,
2883 aom_obmc_sub_pixel_variance128x64)
2884 OBFP(BLOCK_64X128, aom_obmc_sad64x128, aom_obmc_variance64x128,
2885 aom_obmc_sub_pixel_variance64x128)
Yaowu Xuf883b422016-08-30 14:01:10 -07002886 OBFP(BLOCK_64X64, aom_obmc_sad64x64, aom_obmc_variance64x64,
2887 aom_obmc_sub_pixel_variance64x64)
2888 OBFP(BLOCK_64X32, aom_obmc_sad64x32, aom_obmc_variance64x32,
2889 aom_obmc_sub_pixel_variance64x32)
2890 OBFP(BLOCK_32X64, aom_obmc_sad32x64, aom_obmc_variance32x64,
2891 aom_obmc_sub_pixel_variance32x64)
2892 OBFP(BLOCK_32X32, aom_obmc_sad32x32, aom_obmc_variance32x32,
2893 aom_obmc_sub_pixel_variance32x32)
2894 OBFP(BLOCK_32X16, aom_obmc_sad32x16, aom_obmc_variance32x16,
2895 aom_obmc_sub_pixel_variance32x16)
2896 OBFP(BLOCK_16X32, aom_obmc_sad16x32, aom_obmc_variance16x32,
2897 aom_obmc_sub_pixel_variance16x32)
2898 OBFP(BLOCK_16X16, aom_obmc_sad16x16, aom_obmc_variance16x16,
2899 aom_obmc_sub_pixel_variance16x16)
2900 OBFP(BLOCK_16X8, aom_obmc_sad16x8, aom_obmc_variance16x8,
2901 aom_obmc_sub_pixel_variance16x8)
2902 OBFP(BLOCK_8X16, aom_obmc_sad8x16, aom_obmc_variance8x16,
2903 aom_obmc_sub_pixel_variance8x16)
2904 OBFP(BLOCK_8X8, aom_obmc_sad8x8, aom_obmc_variance8x8,
2905 aom_obmc_sub_pixel_variance8x8)
2906 OBFP(BLOCK_4X8, aom_obmc_sad4x8, aom_obmc_variance4x8,
2907 aom_obmc_sub_pixel_variance4x8)
2908 OBFP(BLOCK_8X4, aom_obmc_sad8x4, aom_obmc_variance8x4,
2909 aom_obmc_sub_pixel_variance8x4)
2910 OBFP(BLOCK_4X4, aom_obmc_sad4x4, aom_obmc_variance4x4,
2911 aom_obmc_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002912 OBFP(BLOCK_4X16, aom_obmc_sad4x16, aom_obmc_variance4x16,
2913 aom_obmc_sub_pixel_variance4x16)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002914 OBFP(BLOCK_16X4, aom_obmc_sad16x4, aom_obmc_variance16x4,
2915 aom_obmc_sub_pixel_variance16x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002916 OBFP(BLOCK_8X32, aom_obmc_sad8x32, aom_obmc_variance8x32,
2917 aom_obmc_sub_pixel_variance8x32)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002918 OBFP(BLOCK_32X8, aom_obmc_sad32x8, aom_obmc_variance32x8,
2919 aom_obmc_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002920 OBFP(BLOCK_16X64, aom_obmc_sad16x64, aom_obmc_variance16x64,
2921 aom_obmc_sub_pixel_variance16x64)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002922 OBFP(BLOCK_64X16, aom_obmc_sad64x16, aom_obmc_variance64x16,
2923 aom_obmc_sub_pixel_variance64x16)
Yaowu Xuc27fc142016-08-22 16:08:15 -07002924
David Barkerf19f35f2017-05-22 16:33:22 +01002925#define MBFP(BT, MCSDF, MCSVF) \
2926 cpi->fn_ptr[BT].msdf = MCSDF; \
2927 cpi->fn_ptr[BT].msvf = MCSVF;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002928
David Barkerf19f35f2017-05-22 16:33:22 +01002929 MBFP(BLOCK_128X128, aom_masked_sad128x128,
2930 aom_masked_sub_pixel_variance128x128)
2931 MBFP(BLOCK_128X64, aom_masked_sad128x64, aom_masked_sub_pixel_variance128x64)
2932 MBFP(BLOCK_64X128, aom_masked_sad64x128, aom_masked_sub_pixel_variance64x128)
David Barkerf19f35f2017-05-22 16:33:22 +01002933 MBFP(BLOCK_64X64, aom_masked_sad64x64, aom_masked_sub_pixel_variance64x64)
2934 MBFP(BLOCK_64X32, aom_masked_sad64x32, aom_masked_sub_pixel_variance64x32)
2935 MBFP(BLOCK_32X64, aom_masked_sad32x64, aom_masked_sub_pixel_variance32x64)
2936 MBFP(BLOCK_32X32, aom_masked_sad32x32, aom_masked_sub_pixel_variance32x32)
2937 MBFP(BLOCK_32X16, aom_masked_sad32x16, aom_masked_sub_pixel_variance32x16)
2938 MBFP(BLOCK_16X32, aom_masked_sad16x32, aom_masked_sub_pixel_variance16x32)
2939 MBFP(BLOCK_16X16, aom_masked_sad16x16, aom_masked_sub_pixel_variance16x16)
2940 MBFP(BLOCK_16X8, aom_masked_sad16x8, aom_masked_sub_pixel_variance16x8)
2941 MBFP(BLOCK_8X16, aom_masked_sad8x16, aom_masked_sub_pixel_variance8x16)
2942 MBFP(BLOCK_8X8, aom_masked_sad8x8, aom_masked_sub_pixel_variance8x8)
2943 MBFP(BLOCK_4X8, aom_masked_sad4x8, aom_masked_sub_pixel_variance4x8)
2944 MBFP(BLOCK_8X4, aom_masked_sad8x4, aom_masked_sub_pixel_variance8x4)
2945 MBFP(BLOCK_4X4, aom_masked_sad4x4, aom_masked_sub_pixel_variance4x4)
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002946
Rupert Swarbrick93c39e92017-07-12 11:11:02 +01002947 MBFP(BLOCK_4X16, aom_masked_sad4x16, aom_masked_sub_pixel_variance4x16)
2948
2949 MBFP(BLOCK_16X4, aom_masked_sad16x4, aom_masked_sub_pixel_variance16x4)
2950
2951 MBFP(BLOCK_8X32, aom_masked_sad8x32, aom_masked_sub_pixel_variance8x32)
2952
2953 MBFP(BLOCK_32X8, aom_masked_sad32x8, aom_masked_sub_pixel_variance32x8)
Rupert Swarbrick72678572017-08-02 12:05:26 +01002954
2955 MBFP(BLOCK_16X64, aom_masked_sad16x64, aom_masked_sub_pixel_variance16x64)
2956
2957 MBFP(BLOCK_64X16, aom_masked_sad64x16, aom_masked_sub_pixel_variance64x16)
Rupert Swarbrick2fa6e1c2017-09-11 12:38:10 +01002958
Yaowu Xuc27fc142016-08-22 16:08:15 -07002959 highbd_set_var_fns(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002960
Yaowu Xuf883b422016-08-30 14:01:10 -07002961 /* av1_init_quantizer() is first called here. Add check in
2962 * av1_frame_init_quantizer() so that av1_init_quantizer is only
Yaowu Xuc27fc142016-08-22 16:08:15 -07002963 * called later when needed. This will avoid unnecessary calls of
Yaowu Xuf883b422016-08-30 14:01:10 -07002964 * av1_init_quantizer() for every frame.
Yaowu Xuc27fc142016-08-22 16:08:15 -07002965 */
Yaowu Xuf883b422016-08-30 14:01:10 -07002966 av1_init_quantizer(cpi);
Zoe Liud902b742018-02-19 17:02:41 -08002967 av1_qm_init(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07002968
Yaowu Xuf883b422016-08-30 14:01:10 -07002969 av1_loop_filter_init(cm);
Urvang Joshide71d142017-10-05 12:12:15 -07002970 cm->superres_scale_denominator = SCALE_NUMERATOR;
Debargha Mukherjee29e40a62017-06-14 09:37:12 -07002971 cm->superres_upscaled_width = oxcf->width;
2972 cm->superres_upscaled_height = oxcf->height;
Yaowu Xuf883b422016-08-30 14:01:10 -07002973 av1_loop_restoration_precal();
Yaowu Xuc27fc142016-08-22 16:08:15 -07002974
2975 cm->error.setjmp = 0;
2976
2977 return cpi;
2978}
2979
Urvang Joshiee2c8112018-05-04 14:53:15 -07002980#if CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002981#define SNPRINT(H, T) snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T))
2982
2983#define SNPRINT2(H, T, V) \
2984 snprintf((H) + strlen(H), sizeof(H) - strlen(H), (T), (V))
Urvang Joshiee2c8112018-05-04 14:53:15 -07002985#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07002986
Yaowu Xuf883b422016-08-30 14:01:10 -07002987void av1_remove_compressor(AV1_COMP *cpi) {
2988 AV1_COMMON *cm;
Yaowu Xuc27fc142016-08-22 16:08:15 -07002989 unsigned int i;
2990 int t;
2991
2992 if (!cpi) return;
2993
2994 cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00002995 const int num_planes = av1_num_planes(cm);
2996
David Turnerd2a592e2018-11-16 14:59:31 +00002997 if (cm->current_frame.frame_number > 0) {
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08002998#if CONFIG_ENTROPY_STATS
2999 if (cpi->oxcf.pass != 1) {
3000 fprintf(stderr, "Writing counts.stt\n");
3001 FILE *f = fopen("counts.stt", "wb");
3002 fwrite(&aggregate_fc, sizeof(aggregate_fc), 1, f);
3003 fclose(f);
3004 }
3005#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003006#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07003007 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07003008
3009 if (cpi->oxcf.pass != 1) {
3010 char headings[512] = { 0 };
3011 char results[512] = { 0 };
3012 FILE *f = fopen("opsnr.stt", "a");
3013 double time_encoded =
3014 (cpi->last_end_time_stamp_seen - cpi->first_time_stamp_ever) /
3015 10000000.000;
3016 double total_encode_time =
3017 (cpi->time_receive_data + cpi->time_compress_data) / 1000.000;
3018 const double dr =
3019 (double)cpi->bytes * (double)8 / (double)1000 / time_encoded;
3020 const double peak = (double)((1 << cpi->oxcf.input_bit_depth) - 1);
3021 const double target_rate = (double)cpi->oxcf.target_bandwidth / 1000;
3022 const double rate_err = ((100.0 * (dr - target_rate)) / target_rate);
3023
3024 if (cpi->b_calculate_psnr) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003025 const double total_psnr = aom_sse_to_psnr(
Yaowu Xuc27fc142016-08-22 16:08:15 -07003026 (double)cpi->total_samples, peak, (double)cpi->total_sq_error);
3027 const double total_ssim =
3028 100 * pow(cpi->summed_quality / cpi->summed_weights, 8.0);
3029 snprintf(headings, sizeof(headings),
Jingning Han87651b22017-11-28 20:02:26 -08003030 "Bitrate\tAVGPsnr\tGLBPsnr\tAVPsnrP\tGLPsnrP\t"
Yaowu Xuf883b422016-08-30 14:01:10 -07003031 "AOMSSIM\tVPSSIMP\tFASTSIM\tPSNRHVS\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003032 "WstPsnr\tWstSsim\tWstFast\tWstHVS\t"
Jingning Han87651b22017-11-28 20:02:26 -08003033 "AVPsrnY\tAPsnrCb\tAPsnrCr");
Yaowu Xuc27fc142016-08-22 16:08:15 -07003034 snprintf(results, sizeof(results),
3035 "%7.2f\t%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
3036 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003037 "%7.3f\t%7.3f\t%7.3f\t%7.3f\t"
Jingning Han87651b22017-11-28 20:02:26 -08003038 "%7.3f\t%7.3f\t%7.3f",
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003039 dr, cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr,
3040 cpi->psnr.stat[STAT_ALL] / cpi->count, total_psnr, total_ssim,
3041 total_ssim, cpi->fastssim.stat[STAT_ALL] / cpi->count,
3042 cpi->psnrhvs.stat[STAT_ALL] / cpi->count, cpi->psnr.worst,
Jingning Hanbe1ae3f2017-11-27 10:27:56 -08003043 cpi->worst_ssim, cpi->fastssim.worst, cpi->psnrhvs.worst,
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07003044 cpi->psnr.stat[STAT_Y] / cpi->count,
3045 cpi->psnr.stat[STAT_U] / cpi->count,
3046 cpi->psnr.stat[STAT_V] / cpi->count);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003047
3048 if (cpi->b_calculate_blockiness) {
3049 SNPRINT(headings, "\t Block\tWstBlck");
3050 SNPRINT2(results, "\t%7.3f", cpi->total_blockiness / cpi->count);
3051 SNPRINT2(results, "\t%7.3f", cpi->worst_blockiness);
3052 }
3053
3054 if (cpi->b_calculate_consistency) {
3055 double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07003056 aom_sse_to_psnr((double)cpi->total_samples, peak,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003057 (double)cpi->total_inconsistency);
3058
3059 SNPRINT(headings, "\tConsist\tWstCons");
3060 SNPRINT2(results, "\t%7.3f", consistency);
3061 SNPRINT2(results, "\t%7.3f", cpi->worst_consistency);
3062 }
Sarah Parkerf97b7862016-08-25 17:42:57 -07003063 fprintf(f, "%s\t Time\tRcErr\tAbsErr\n", headings);
3064 fprintf(f, "%s\t%8.0f\t%7.2f\t%7.2f\n", results, total_encode_time,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003065 rate_err, fabs(rate_err));
3066 }
3067
3068 fclose(f);
3069 }
Urvang Joshiee2c8112018-05-04 14:53:15 -07003070#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08003071#if CONFIG_SPEED_STATS
3072 if (cpi->oxcf.pass != 1) {
3073 fprintf(stdout, "tx_search_count = %d\n", cpi->tx_search_count);
3074 }
3075#endif // CONFIG_SPEED_STATS
chiyotsai92ed0dd2019-01-25 14:50:14 -08003076
3077#if CONFIG_COLLECT_PARTITION_STATS
3078 if (cpi->oxcf.pass != 1) {
3079 av1_print_partition_stats(&cpi->partition_stats);
3080 }
3081#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003082 }
3083
Yue Chen7cae98f2018-08-24 10:43:16 -07003084 for (int frame = 0; frame < MAX_LAG_BUFFERS; ++frame) {
3085 aom_free(cpi->tpl_stats[frame].tpl_stats_ptr);
3086 cpi->tpl_stats[frame].is_valid = 0;
3087 }
3088
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303089 for (t = cpi->num_workers - 1; t >= 0; --t) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003090 AVxWorker *const worker = &cpi->workers[t];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003091 EncWorkerData *const thread_data = &cpi->tile_thr_data[t];
3092
3093 // Deallocate allocated threads.
Yaowu Xuf883b422016-08-30 14:01:10 -07003094 aom_get_worker_interface()->end(worker);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003095
3096 // Deallocate allocated thread data.
Ravi Chaudhary1cf7d162018-10-09 17:00:43 +05303097 if (cpi->row_mt == 1) aom_free(thread_data->td->tctx);
Ravi Chaudhary1f58dd82018-12-07 17:24:15 +05303098 if (t > 0) {
hui sud9a812b2017-07-06 14:34:37 -07003099 aom_free(thread_data->td->palette_buffer);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003100 aom_free(thread_data->td->tmp_conv_dst);
3101 for (int j = 0; j < 2; ++j) {
3102 aom_free(thread_data->td->tmp_obmc_bufs[j]);
3103 }
Jingning Hand064cf02017-06-01 10:00:39 -07003104 aom_free(thread_data->td->above_pred_buf);
3105 aom_free(thread_data->td->left_pred_buf);
3106 aom_free(thread_data->td->wsrc_buf);
wenyao.liu22d8ab32018-10-16 09:11:29 +08003107
Ravi Chaudhary5d970f42018-09-25 11:25:32 +05303108 aom_free(thread_data->td->inter_modes_info);
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003109 for (int x = 0; x < 2; x++) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303110 for (int y = 0; y < 2; y++) {
3111 aom_free(thread_data->td->hash_value_buffer[x][y]);
3112 thread_data->td->hash_value_buffer[x][y] = NULL;
3113 }
Urvang Joshi0a4cfad2018-09-07 11:10:39 -07003114 }
Jingning Hand064cf02017-06-01 10:00:39 -07003115 aom_free(thread_data->td->mask_buf);
Yaowu Xuf883b422016-08-30 14:01:10 -07003116 aom_free(thread_data->td->counts);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003117 av1_free_pc_tree(thread_data->td, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003118 aom_free(thread_data->td);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003119 }
3120 }
Ravi Chaudhary90a15f42018-10-11 18:56:35 +05303121#if CONFIG_MULTITHREAD
3122 if (cpi->row_mt == 1) {
3123 if (cpi->row_mt_mutex_ != NULL) {
3124 pthread_mutex_destroy(cpi->row_mt_mutex_);
3125 aom_free(cpi->row_mt_mutex_);
3126 }
3127 }
3128#endif
Ravi Chaudharyc5e74692018-10-08 16:05:38 +05303129 av1_row_mt_mem_dealloc(cpi);
Yaowu Xuf883b422016-08-30 14:01:10 -07003130 aom_free(cpi->tile_thr_data);
3131 aom_free(cpi->workers);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003132
Deepa K G964e72e2018-05-16 16:56:01 +05303133 if (cpi->num_workers > 1) {
3134 av1_loop_filter_dealloc(&cpi->lf_row_sync);
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05303135 av1_loop_restoration_dealloc(&cpi->lr_row_sync, cpi->num_workers);
Deepa K G964e72e2018-05-16 16:56:01 +05303136 }
3137
Yaowu Xuc27fc142016-08-22 16:08:15 -07003138 dealloc_compressor_data(cpi);
3139
3140 for (i = 0; i < sizeof(cpi->mbgraph_stats) / sizeof(cpi->mbgraph_stats[0]);
3141 ++i) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003142 aom_free(cpi->mbgraph_stats[i].mb_stats);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003143 }
3144
3145#if CONFIG_FP_MB_STATS
3146 if (cpi->use_fp_mb_stats) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003147 aom_free(cpi->twopass.frame_mb_stats_buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003148 cpi->twopass.frame_mb_stats_buf = NULL;
3149 }
3150#endif
Debargha Mukherjee5d157212017-01-10 14:44:47 -08003151#if CONFIG_INTERNAL_STATS
3152 aom_free(cpi->ssim_vars);
3153 cpi->ssim_vars = NULL;
3154#endif // CONFIG_INTERNAL_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07003155
Yaowu Xuf883b422016-08-30 14:01:10 -07003156 av1_remove_common(cm);
RogerZhou80d52342017-11-20 10:56:26 -08003157 for (i = 0; i < FRAME_BUFFERS; ++i) {
3158 av1_hash_table_destroy(&cm->buffer_pool->frame_bufs[i].hash_table);
3159 }
Michelle Findlay-Olynykdea531d2017-12-13 14:10:56 -08003160 if (cpi->sf.use_hash_based_trellis) hbt_destroy();
Yaowu Xuf883b422016-08-30 14:01:10 -07003161 av1_free_ref_frame_buffers(cm->buffer_pool);
3162 aom_free(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003163
3164#ifdef OUTPUT_YUV_SKINMAP
3165 fclose(yuv_skinmap_file);
3166#endif
3167#ifdef OUTPUT_YUV_REC
3168 fclose(yuv_rec_file);
3169#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07003170}
3171
Yaowu Xuf883b422016-08-30 14:01:10 -07003172static void generate_psnr_packet(AV1_COMP *cpi) {
3173 struct aom_codec_cx_pkt pkt;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003174 int i;
3175 PSNR_STATS psnr;
David Turnerc29e1a92018-12-06 14:10:14 +00003176 aom_calc_highbd_psnr(cpi->source, &cpi->common.cur_frame->buf, &psnr,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003177 cpi->td.mb.e_mbd.bd, cpi->oxcf.input_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003178
3179 for (i = 0; i < 4; ++i) {
3180 pkt.data.psnr.samples[i] = psnr.samples[i];
3181 pkt.data.psnr.sse[i] = psnr.sse[i];
3182 pkt.data.psnr.psnr[i] = psnr.psnr[i];
3183 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003184 pkt.kind = AOM_CODEC_PSNR_PKT;
3185 aom_codec_pkt_list_add(cpi->output_pkt_list, &pkt);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003186}
3187
Yaowu Xuf883b422016-08-30 14:01:10 -07003188int av1_use_as_reference(AV1_COMP *cpi, int ref_frame_flags) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003189 if (ref_frame_flags > ((1 << INTER_REFS_PER_FRAME) - 1)) return -1;
3190
Yunqing Wangf2e7a392017-11-08 00:27:21 -08003191 cpi->ext_ref_frame_flags = ref_frame_flags;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003192 return 0;
3193}
3194
Thomas Daede497d1952017-08-08 17:33:06 -07003195int av1_copy_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3196 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003197 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003198 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003199 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003200 aom_yv12_copy_frame(cfg, sd, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003201 return 0;
3202 } else {
3203 return -1;
3204 }
3205}
3206
Thomas Daede497d1952017-08-08 17:33:06 -07003207int av1_set_reference_enc(AV1_COMP *cpi, int idx, YV12_BUFFER_CONFIG *sd) {
3208 AV1_COMMON *const cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003209 const int num_planes = av1_num_planes(cm);
Thomas Daede497d1952017-08-08 17:33:06 -07003210 YV12_BUFFER_CONFIG *cfg = get_ref_frame(cm, idx);
Yaowu Xuf883b422016-08-30 14:01:10 -07003211 if (cfg) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003212 aom_yv12_copy_frame(sd, cfg, num_planes);
Yaowu Xuf883b422016-08-30 14:01:10 -07003213 return 0;
3214 } else {
3215 return -1;
3216 }
3217}
3218
3219int av1_update_entropy(AV1_COMP *cpi, int update) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003220 cpi->ext_refresh_frame_context = update;
3221 cpi->ext_refresh_frame_context_pending = 1;
3222 return 0;
3223}
3224
3225#if defined(OUTPUT_YUV_DENOISED) || defined(OUTPUT_YUV_SKINMAP)
3226// The denoiser buffer is allocated as a YUV 440 buffer. This function writes it
3227// as YUV 420. We simply use the top-left pixels of the UV buffers, since we do
3228// not denoise the UV channels at this time. If ever we implement UV channel
3229// denoising we will have to modify this.
Yaowu Xuf883b422016-08-30 14:01:10 -07003230void aom_write_yuv_frame_420(YV12_BUFFER_CONFIG *s, FILE *f) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003231 uint8_t *src = s->y_buffer;
3232 int h = s->y_height;
3233
3234 do {
3235 fwrite(src, s->y_width, 1, f);
3236 src += s->y_stride;
3237 } while (--h);
3238
3239 src = s->u_buffer;
3240 h = s->uv_height;
3241
3242 do {
3243 fwrite(src, s->uv_width, 1, f);
3244 src += s->uv_stride;
3245 } while (--h);
3246
3247 src = s->v_buffer;
3248 h = s->uv_height;
3249
3250 do {
3251 fwrite(src, s->uv_width, 1, f);
3252 src += s->uv_stride;
3253 } while (--h);
3254}
3255#endif
3256
Yaowu Xuc27fc142016-08-22 16:08:15 -07003257#ifdef OUTPUT_YUV_REC
Yaowu Xuf883b422016-08-30 14:01:10 -07003258void aom_write_one_yuv_frame(AV1_COMMON *cm, YV12_BUFFER_CONFIG *s) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003259 uint8_t *src = s->y_buffer;
3260 int h = cm->height;
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07003261 if (yuv_rec_file == NULL) return;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003262 if (s->flags & YV12_FLAG_HIGHBITDEPTH) {
3263 uint16_t *src16 = CONVERT_TO_SHORTPTR(s->y_buffer);
3264
3265 do {
3266 fwrite(src16, s->y_width, 2, yuv_rec_file);
3267 src16 += s->y_stride;
3268 } while (--h);
3269
3270 src16 = CONVERT_TO_SHORTPTR(s->u_buffer);
3271 h = s->uv_height;
3272
3273 do {
3274 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3275 src16 += s->uv_stride;
3276 } while (--h);
3277
3278 src16 = CONVERT_TO_SHORTPTR(s->v_buffer);
3279 h = s->uv_height;
3280
3281 do {
3282 fwrite(src16, s->uv_width, 2, yuv_rec_file);
3283 src16 += s->uv_stride;
3284 } while (--h);
3285
3286 fflush(yuv_rec_file);
3287 return;
3288 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003289
3290 do {
3291 fwrite(src, s->y_width, 1, yuv_rec_file);
3292 src += s->y_stride;
3293 } while (--h);
3294
3295 src = s->u_buffer;
3296 h = s->uv_height;
3297
3298 do {
3299 fwrite(src, s->uv_width, 1, yuv_rec_file);
3300 src += s->uv_stride;
3301 } while (--h);
3302
3303 src = s->v_buffer;
3304 h = s->uv_height;
3305
3306 do {
3307 fwrite(src, s->uv_width, 1, yuv_rec_file);
3308 src += s->uv_stride;
3309 } while (--h);
3310
3311 fflush(yuv_rec_file);
3312}
3313#endif // OUTPUT_YUV_REC
3314
Debargha Mukherjee11f0e402017-03-29 07:42:40 -07003315#define GM_RECODE_LOOP_NUM4X4_FACTOR 192
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003316static int recode_loop_test_global_motion(AV1_COMP *cpi) {
3317 int i;
3318 int recode = 0;
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003319 RD_COUNTS *const rdc = &cpi->td.rd_counts;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003320 AV1_COMMON *const cm = &cpi->common;
3321 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
3322 if (cm->global_motion[i].wmtype != IDENTITY &&
Debargha Mukherjeea575d232017-04-28 17:46:47 -07003323 rdc->global_motion_used[i] * GM_RECODE_LOOP_NUM4X4_FACTOR <
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003324 cpi->gmparams_cost[i]) {
David Barkerd7c8bd52017-09-25 14:47:29 +01003325 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003326 assert(cm->global_motion[i].wmtype == IDENTITY);
Debargha Mukherjee265db6d2017-03-28 11:15:27 -07003327 cpi->gmparams_cost[i] = 0;
David Barker43479c62016-11-30 10:34:20 +00003328 recode = 1;
Urvang Joshi02aade82017-12-18 17:18:16 -08003329 // TODO(sarahparker): The earlier condition for recoding here was:
3330 // "recode |= (rdc->global_motion_used[i] > 0);". Can we bring something
3331 // similar to that back to speed up global motion?
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003332 }
3333 }
3334 return recode;
3335}
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003336
Yaowu Xuc27fc142016-08-22 16:08:15 -07003337// Function to test for conditions that indicate we should loop
3338// back and recode a frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07003339static int recode_loop_test(AV1_COMP *cpi, int high_limit, int low_limit, int q,
3340 int maxq, int minq) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003341 const RATE_CONTROL *const rc = &cpi->rc;
Yaowu Xuf883b422016-08-30 14:01:10 -07003342 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003343 const int frame_is_kfgfarf = frame_is_kf_gf_arf(cpi);
3344 int force_recode = 0;
3345
3346 if ((rc->projected_frame_size >= rc->max_frame_bandwidth) ||
3347 (cpi->sf.recode_loop == ALLOW_RECODE) ||
3348 (frame_is_kfgfarf && (cpi->sf.recode_loop == ALLOW_RECODE_KFARFGF))) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003349 // TODO(agrange) high_limit could be greater than the scale-down threshold.
3350 if ((rc->projected_frame_size > high_limit && q < maxq) ||
3351 (rc->projected_frame_size < low_limit && q > minq)) {
3352 force_recode = 1;
Yaowu Xuf883b422016-08-30 14:01:10 -07003353 } else if (cpi->oxcf.rc_mode == AOM_CQ) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003354 // Deal with frame undershoot and whether or not we are
3355 // below the automatically set cq level.
3356 if (q > oxcf->cq_level &&
3357 rc->projected_frame_size < ((rc->this_frame_target * 7) >> 3)) {
3358 force_recode = 1;
3359 }
3360 }
3361 }
3362 return force_recode;
3363}
3364
Cheng Chen46f30c72017-09-07 11:13:33 -07003365static void scale_references(AV1_COMP *cpi) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003366 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003367 const int num_planes = av1_num_planes(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003368 MV_REFERENCE_FRAME ref_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07003369 const AOM_REFFRAME ref_mask[INTER_REFS_PER_FRAME] = {
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02003370 AOM_LAST_FLAG, AOM_LAST2_FLAG, AOM_LAST3_FLAG, AOM_GOLD_FLAG,
3371 AOM_BWD_FLAG, AOM_ALT2_FLAG, AOM_ALT_FLAG
Yaowu Xuc27fc142016-08-22 16:08:15 -07003372 };
3373
3374 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003375 // Need to convert from AOM_REFFRAME to index into ref_mask (subtract 1).
Yaowu Xuc27fc142016-08-22 16:08:15 -07003376 if (cpi->ref_frame_flags & ref_mask[ref_frame - 1]) {
3377 BufferPool *const pool = cm->buffer_pool;
3378 const YV12_BUFFER_CONFIG *const ref =
David Turnera21966b2018-12-05 14:48:49 +00003379 get_ref_frame_yv12_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003380
3381 if (ref == NULL) {
David Turnere7ebf902018-12-04 14:04:55 +00003382 cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003383 continue;
3384 }
3385
Yaowu Xuc27fc142016-08-22 16:08:15 -07003386 if (ref->y_crop_width != cm->width || ref->y_crop_height != cm->height) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003387 int force_scaling = 0;
David Turnere7ebf902018-12-04 14:04:55 +00003388 RefCntBuffer *new_fb = cpi->scaled_ref_buf[ref_frame - 1];
3389 if (new_fb == NULL) {
3390 const int new_fb_idx = get_free_fb(cm);
3391 if (new_fb_idx == INVALID_IDX) {
Wan-Teh Chang4a8c0042018-10-05 09:41:52 -07003392 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3393 "Unable to find free frame buffer");
David Turnere7ebf902018-12-04 14:04:55 +00003394 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003395 force_scaling = 1;
David Turnere7ebf902018-12-04 14:04:55 +00003396 new_fb = &pool->frame_bufs[new_fb_idx];
Yaowu Xuc27fc142016-08-22 16:08:15 -07003397 }
David Turnere7ebf902018-12-04 14:04:55 +00003398
3399 if (force_scaling || new_fb->buf.y_crop_width != cm->width ||
3400 new_fb->buf.y_crop_height != cm->height) {
Yaowu Xu671f2bd2016-09-30 15:07:57 -07003401 if (aom_realloc_frame_buffer(
David Turnere7ebf902018-12-04 14:04:55 +00003402 &new_fb->buf, cm->width, cm->height,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003403 cm->seq_params.subsampling_x, cm->seq_params.subsampling_y,
Satish Kumar Suman29909962019-01-09 10:31:21 +05303404 cm->seq_params.use_highbitdepth, cpi->oxcf.border_in_pixels,
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003405 cm->byte_alignment, NULL, NULL, NULL)) {
3406 if (force_scaling) {
3407 // Release the reference acquired in the get_free_fb() call above.
David Turnere7ebf902018-12-04 14:04:55 +00003408 --new_fb->ref_count;
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003409 }
Yaowu Xuf883b422016-08-30 14:01:10 -07003410 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003411 "Failed to allocate frame buffer");
Wan-Teh Chang41d286f2018-10-03 11:43:03 -07003412 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003413 av1_resize_and_extend_frame(
David Turnere7ebf902018-12-04 14:04:55 +00003414 ref, &new_fb->buf, (int)cm->seq_params.bit_depth, num_planes);
3415 cpi->scaled_ref_buf[ref_frame - 1] = new_fb;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003416 alloc_frame_mvs(cm, new_fb);
3417 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003418 } else {
David Turnera21966b2018-12-05 14:48:49 +00003419 RefCntBuffer *buf = get_ref_frame_buf(cm, ref_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003420 buf->buf.y_crop_width = ref->y_crop_width;
3421 buf->buf.y_crop_height = ref->y_crop_height;
David Turnere7ebf902018-12-04 14:04:55 +00003422 cpi->scaled_ref_buf[ref_frame - 1] = buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003423 ++buf->ref_count;
3424 }
3425 } else {
David Turnere7ebf902018-12-04 14:04:55 +00003426 if (cpi->oxcf.pass != 0) cpi->scaled_ref_buf[ref_frame - 1] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003427 }
3428 }
3429}
3430
Yaowu Xuf883b422016-08-30 14:01:10 -07003431static void release_scaled_references(AV1_COMP *cpi) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003432 // TODO(isbs): only refresh the necessary frames, rather than all of them
David Turnere7ebf902018-12-04 14:04:55 +00003433 for (int i = 0; i < INTER_REFS_PER_FRAME; ++i) {
3434 RefCntBuffer *const buf = cpi->scaled_ref_buf[i];
3435 if (buf != NULL) {
Imdad Sardharwalladadaba62018-02-23 12:06:56 +00003436 --buf->ref_count;
David Turnere7ebf902018-12-04 14:04:55 +00003437 cpi->scaled_ref_buf[i] = NULL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003438 }
3439 }
3440}
3441
Yaowu Xuf883b422016-08-30 14:01:10 -07003442static void set_mv_search_params(AV1_COMP *cpi) {
3443 const AV1_COMMON *const cm = &cpi->common;
3444 const unsigned int max_mv_def = AOMMIN(cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003445
3446 // Default based on max resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003447 cpi->mv_step_param = av1_init_search_range(max_mv_def);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003448
3449 if (cpi->sf.mv.auto_mv_step_size) {
3450 if (frame_is_intra_only(cm)) {
3451 // Initialize max_mv_magnitude for use in the first INTER frame
3452 // after a key/intra-only frame.
3453 cpi->max_mv_magnitude = max_mv_def;
3454 } else {
3455 if (cm->show_frame) {
3456 // Allow mv_steps to correspond to twice the max mv magnitude found
3457 // in the previous frame, capped by the default max_mv_magnitude based
3458 // on resolution.
Yaowu Xuf883b422016-08-30 14:01:10 -07003459 cpi->mv_step_param = av1_init_search_range(
3460 AOMMIN(max_mv_def, 2 * cpi->max_mv_magnitude));
Yaowu Xuc27fc142016-08-22 16:08:15 -07003461 }
3462 cpi->max_mv_magnitude = 0;
3463 }
3464 }
3465}
3466
Hui Subdf0c992019-02-14 14:52:41 -08003467static void set_screen_content_options(AV1_COMP *cpi) {
3468 AV1_COMMON *cm = &cpi->common;
3469
3470 if (cm->seq_params.force_screen_content_tools != 2) {
3471 cm->allow_screen_content_tools = cm->allow_intrabc =
3472 cm->seq_params.force_screen_content_tools;
3473 return;
3474 }
3475
3476 if (cpi->oxcf.content == AOM_CONTENT_SCREEN) {
3477 cm->allow_screen_content_tools = cm->allow_intrabc = 1;
3478 return;
3479 }
3480
3481 // Estimate if the source frame is screen content, based on the portion of
3482 // blocks that have few luma colors.
3483 const uint8_t *src = cpi->source->y_buffer;
3484 assert(src != NULL);
3485 const int use_hbd = cpi->source->flags & YV12_FLAG_HIGHBITDEPTH;
3486 const int stride = cpi->source->y_stride;
3487 const int width = cpi->source->y_width;
3488 const int height = cpi->source->y_height;
3489 const int bd = cm->seq_params.bit_depth;
3490 const int blk_w = 16;
3491 const int blk_h = 16;
3492 // These threshold values are selected experimentally.
3493 const int color_thresh = 4;
3494 const unsigned int var_thresh = 0;
3495 // Counts of blocks with no more than color_thresh colors.
3496 int counts_1 = 0;
3497 // Counts of blocks with no more than color_thresh colors and variance larger
3498 // than var_thresh.
3499 int counts_2 = 0;
3500
3501 for (int r = 0; r + blk_h <= height; r += blk_h) {
3502 for (int c = 0; c + blk_w <= width; c += blk_w) {
3503 int count_buf[1 << 12]; // Maximum (1 << 12) color levels.
3504 const uint8_t *const this_src = src + r * stride + c;
3505 const int n_colors =
3506 use_hbd ? av1_count_colors_highbd(this_src, stride, blk_w, blk_h, bd,
3507 count_buf)
3508 : av1_count_colors(this_src, stride, blk_w, blk_h, count_buf);
3509 if (n_colors > 1 && n_colors <= color_thresh) {
3510 ++counts_1;
3511 struct buf_2d buf;
3512 buf.stride = stride;
3513 buf.buf = (uint8_t *)this_src;
3514 const unsigned int var =
3515 use_hbd
3516 ? av1_high_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16, bd)
3517 : av1_get_sby_perpixel_variance(cpi, &buf, BLOCK_16X16);
3518 if (var > var_thresh) ++counts_2;
3519 }
3520 }
3521 }
3522
3523 // The threshold values are selected experimentally.
3524 cm->allow_screen_content_tools =
3525 counts_1 * blk_h * blk_w * 10 > width * height;
3526 // IntraBC would force loop filters off, so we use more strict rules that also
3527 // requires that the block has high variance.
3528 cm->allow_intrabc = cm->allow_screen_content_tools &&
3529 counts_2 * blk_h * blk_w * 15 > width * height;
3530}
3531
Yaowu Xuf883b422016-08-30 14:01:10 -07003532static void set_size_independent_vars(AV1_COMP *cpi) {
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003533 int i;
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003534 AV1_COMMON *cm = &cpi->common;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003535 for (i = LAST_FRAME; i <= ALTREF_FRAME; ++i) {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003536 cm->global_motion[i] = default_warp_params;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08003537 }
3538 cpi->global_motion_search_done = 0;
David Turner04b70d82019-01-24 15:39:19 +00003539 av1_set_speed_features_framesize_independent(cpi, cpi->speed);
Yaowu Xuf883b422016-08-30 14:01:10 -07003540 av1_set_rd_speed_thresholds(cpi);
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003541 cm->interp_filter = SWITCHABLE;
3542 cm->switchable_motion_mode = 1;
3543
Hui Subdf0c992019-02-14 14:52:41 -08003544 if (frame_is_intra_only(cm)) set_screen_content_options(cpi);
Aniket Dhokf6d7ed82019-01-04 14:05:57 +05303545 cpi->is_screen_content_type = (cm->allow_screen_content_tools != 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003546}
3547
Yaowu Xuf883b422016-08-30 14:01:10 -07003548static void set_size_dependent_vars(AV1_COMP *cpi, int *q, int *bottom_index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003549 int *top_index) {
Yaowu Xuf883b422016-08-30 14:01:10 -07003550 AV1_COMMON *const cm = &cpi->common;
3551 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003552
3553 // Setup variables that depend on the dimensions of the frame.
David Turner04b70d82019-01-24 15:39:19 +00003554 av1_set_speed_features_framesize_dependent(cpi, cpi->speed);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003555
Sebastien Alaiwan41cae6a2018-01-12 12:22:29 +01003556 // Decide q and q bounds.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003557 *q = av1_rc_pick_q_and_bounds(cpi, cm->width, cm->height, bottom_index,
3558 top_index);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003559
James Zern01a9d702017-08-25 19:09:33 +00003560 if (!frame_is_intra_only(cm)) {
RogerZhou3b635242017-09-19 10:06:46 -07003561 set_high_precision_mv(cpi, (*q) < HIGH_PRECISION_MV_QTHRESH,
RogerZhou10a03802017-10-26 11:49:48 -07003562 cpi->common.cur_frame_force_integer_mv);
James Zern01a9d702017-08-25 19:09:33 +00003563 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07003564
3565 // Configure experimental use of segmentation for enhanced coding of
3566 // static regions if indicated.
3567 // Only allowed in the second pass of a two pass encode, as it requires
3568 // lagged coding, and if the relevant speed feature flag is set.
3569 if (oxcf->pass == 2 && cpi->sf.static_segmentation)
3570 configure_static_seg_features(cpi);
3571}
3572
Yaowu Xuf883b422016-08-30 14:01:10 -07003573static void init_motion_estimation(AV1_COMP *cpi) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07003574 int y_stride = cpi->scaled_source.y_stride;
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303575 int y_stride_src = (cpi->oxcf.resize_mode || cpi->oxcf.superres_mode)
3576 ? y_stride
3577 : cpi->lookahead->buf->img.y_stride;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003578
3579 if (cpi->sf.mv.search_method == NSTEP) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303580 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3581 av1_init3smotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD], y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003582 } else if (cpi->sf.mv.search_method == DIAMOND) {
Satish Kumar Sumane6d0be52019-02-14 14:33:28 +05303583 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_SRC], y_stride);
3584 av1_init_dsmotion_compensation(&cpi->ss_cfg[SS_CFG_LOOKAHEAD],
3585 y_stride_src);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003586 }
3587}
3588
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003589#define COUPLED_CHROMA_FROM_LUMA_RESTORATION 0
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003590static void set_restoration_unit_size(int width, int height, int sx, int sy,
3591 RestorationInfo *rst) {
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003592 (void)width;
3593 (void)height;
Debargha Mukherjee84f567c2017-06-21 10:53:59 -07003594 (void)sx;
3595 (void)sy;
3596#if COUPLED_CHROMA_FROM_LUMA_RESTORATION
3597 int s = AOMMIN(sx, sy);
3598#else
3599 int s = 0;
3600#endif // !COUPLED_CHROMA_FROM_LUMA_RESTORATION
3601
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003602 if (width * height > 352 * 288)
Urvang Joshi813186b2018-03-08 15:38:46 -08003603 rst[0].restoration_unit_size = RESTORATION_UNITSIZE_MAX;
Debargha Mukherjee5f7f3672017-08-12 10:22:49 -07003604 else
Urvang Joshi813186b2018-03-08 15:38:46 -08003605 rst[0].restoration_unit_size = (RESTORATION_UNITSIZE_MAX >> 1);
Rupert Swarbrickbcb65fe2017-10-25 17:15:28 +01003606 rst[1].restoration_unit_size = rst[0].restoration_unit_size >> s;
3607 rst[2].restoration_unit_size = rst[1].restoration_unit_size;
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003608}
Debargha Mukherjee1008c1e2017-03-06 19:18:43 -08003609
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303610static void init_ref_frame_bufs(AV1_COMP *cpi) {
3611 AV1_COMMON *const cm = &cpi->common;
Cheng Chen46f30c72017-09-07 11:13:33 -07003612 int i;
3613 BufferPool *const pool = cm->buffer_pool;
Jack Haughtonddb80602018-11-21 16:41:49 +00003614 cm->cur_frame = NULL;
Cheng Chen46f30c72017-09-07 11:13:33 -07003615 for (i = 0; i < REF_FRAMES; ++i) {
David Turnere7ebf902018-12-04 14:04:55 +00003616 cm->ref_frame_map[i] = NULL;
Wan-Teh Changd05e0332018-10-03 12:00:43 -07003617 }
3618 for (i = 0; i < FRAME_BUFFERS; ++i) {
Cheng Chen46f30c72017-09-07 11:13:33 -07003619 pool->frame_bufs[i].ref_count = 0;
3620 }
RogerZhou86902d02018-02-28 15:29:16 -08003621 if (cm->seq_params.force_screen_content_tools) {
Hui Su2d5fd742018-02-21 18:10:37 -08003622 for (i = 0; i < FRAME_BUFFERS; ++i) {
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303623 av1_hash_table_init(&pool->frame_bufs[i].hash_table, &cpi->td.mb);
Hui Su2d5fd742018-02-21 18:10:37 -08003624 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003625 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003626}
3627
Yaowu Xud3e7c682017-12-21 14:08:25 -08003628static void check_initial_width(AV1_COMP *cpi, int use_highbitdepth,
Cheng Chen46f30c72017-09-07 11:13:33 -07003629 int subsampling_x, int subsampling_y) {
3630 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003631 SequenceHeader *const seq_params = &cm->seq_params;
Cheng Chen46f30c72017-09-07 11:13:33 -07003632
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003633 if (!cpi->initial_width || seq_params->use_highbitdepth != use_highbitdepth ||
3634 seq_params->subsampling_x != subsampling_x ||
3635 seq_params->subsampling_y != subsampling_y) {
3636 seq_params->subsampling_x = subsampling_x;
3637 seq_params->subsampling_y = subsampling_y;
3638 seq_params->use_highbitdepth = use_highbitdepth;
Cheng Chen46f30c72017-09-07 11:13:33 -07003639
3640 alloc_raw_frame_buffers(cpi);
Ravi Chaudhary783d6a32018-08-28 18:21:02 +05303641 init_ref_frame_bufs(cpi);
Cheng Chen46f30c72017-09-07 11:13:33 -07003642 alloc_util_frame_buffers(cpi);
3643
3644 init_motion_estimation(cpi); // TODO(agrange) This can be removed.
3645
3646 cpi->initial_width = cm->width;
3647 cpi->initial_height = cm->height;
3648 cpi->initial_mbs = cm->MBs;
3649 }
3650}
3651
3652// Returns 1 if the assigned width or height was <= 0.
3653static int set_size_literal(AV1_COMP *cpi, int width, int height) {
3654 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003655 const int num_planes = av1_num_planes(cm);
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003656 check_initial_width(cpi, cm->seq_params.use_highbitdepth,
3657 cm->seq_params.subsampling_x,
3658 cm->seq_params.subsampling_y);
Cheng Chen46f30c72017-09-07 11:13:33 -07003659
3660 if (width <= 0 || height <= 0) return 1;
3661
3662 cm->width = width;
Cheng Chen46f30c72017-09-07 11:13:33 -07003663 cm->height = height;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003664
3665 if (cpi->initial_width && cpi->initial_height &&
3666 (cm->width > cpi->initial_width || cm->height > cpi->initial_height)) {
3667 av1_free_context_buffers(cm);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003668 av1_free_pc_tree(&cpi->td, num_planes);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003669 alloc_compressor_data(cpi);
3670 realloc_segmentation_maps(cpi);
3671 cpi->initial_width = cpi->initial_height = 0;
Cheng Chen46f30c72017-09-07 11:13:33 -07003672 }
Cheng Chen46f30c72017-09-07 11:13:33 -07003673 update_frame_size(cpi);
3674
3675 return 0;
3676}
3677
David Turner475a3132019-01-18 15:17:17 +00003678void av1_set_frame_size(AV1_COMP *cpi, int width, int height) {
Fergus Simpsonbc189932017-05-16 17:02:39 -07003679 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003680 const SequenceHeader *const seq_params = &cm->seq_params;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003681 const int num_planes = av1_num_planes(cm);
Fergus Simpsonbc189932017-05-16 17:02:39 -07003682 MACROBLOCKD *const xd = &cpi->td.mb.e_mbd;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003683 int ref_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07003684
Fergus Simpsonbc189932017-05-16 17:02:39 -07003685 if (width != cm->width || height != cm->height) {
Fergus Simpson3502d082017-04-10 12:25:07 -07003686 // There has been a change in the encoded frame size
Cheng Chen46f30c72017-09-07 11:13:33 -07003687 set_size_literal(cpi, width, height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003688 set_mv_search_params(cpi);
Urvang Joshic8b52d52018-03-23 13:16:51 -07003689 // Recalculate 'all_lossless' in case super-resolution was (un)selected.
Cheng Chen09c83a52018-06-05 12:27:36 -07003690 cm->all_lossless = cm->coded_lossless && !av1_superres_scaled(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003691 }
3692
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003693 if (cpi->oxcf.pass == 2) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003694 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003695 }
3696
David Turnere7ebf902018-12-04 14:04:55 +00003697 alloc_frame_mvs(cm, cm->cur_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003698
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303699 // Allocate above context buffers
Cherma Rajan Af1479082018-05-09 14:26:34 +05303700 if (cm->num_allocated_above_context_planes < av1_num_planes(cm) ||
3701 cm->num_allocated_above_context_mi_col < cm->mi_cols ||
Cherma Rajan A71d20db2018-04-27 11:15:32 +05303702 cm->num_allocated_above_contexts < cm->tile_rows) {
3703 av1_free_above_context_buffers(cm, cm->num_allocated_above_contexts);
3704 if (av1_alloc_above_context_buffers(cm, cm->tile_rows))
3705 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
3706 "Failed to allocate context buffers");
3707 }
3708
Yaowu Xuc27fc142016-08-22 16:08:15 -07003709 // Reset the frame pointers to the current frame size.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003710 if (aom_realloc_frame_buffer(
Jack Haughtonddb80602018-11-21 16:41:49 +00003711 &cm->cur_frame->buf, cm->width, cm->height, seq_params->subsampling_x,
3712 seq_params->subsampling_y, seq_params->use_highbitdepth,
Satish Kumar Suman29909962019-01-09 10:31:21 +05303713 cpi->oxcf.border_in_pixels, cm->byte_alignment, NULL, NULL, NULL))
Yaowu Xuf883b422016-08-30 14:01:10 -07003714 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
Yaowu Xuc27fc142016-08-22 16:08:15 -07003715 "Failed to allocate frame buffer");
3716
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003717 const int frame_width = cm->superres_upscaled_width;
3718 const int frame_height = cm->superres_upscaled_height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07003719 set_restoration_unit_size(frame_width, frame_height,
3720 seq_params->subsampling_x,
3721 seq_params->subsampling_y, cm->rst_info);
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003722 for (int i = 0; i < num_planes; ++i)
Rupert Swarbrick1a96c3f2017-10-24 11:55:00 +01003723 cm->rst_info[i].frame_restoration_type = RESTORE_NONE;
Rupert Swarbrickf88bc042017-10-18 10:45:51 +01003724
3725 av1_alloc_restoration_buffers(cm);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003726 alloc_util_frame_buffers(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003727 init_motion_estimation(cpi);
3728
3729 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00003730 RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
David Turnere7ebf902018-12-04 14:04:55 +00003731 if (buf != NULL) {
David Turnera21966b2018-12-05 14:48:49 +00003732 struct scale_factors *sf = get_ref_scale_factors(cm, ref_frame);
3733 av1_setup_scale_factors_for_frame(sf, buf->buf.y_crop_width,
David Turner1bcefb32018-11-19 17:54:00 +00003734 buf->buf.y_crop_height, cm->width,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003735 cm->height);
David Turnera21966b2018-12-05 14:48:49 +00003736 if (av1_is_scaled(sf)) aom_extend_frame_borders(&buf->buf, num_planes);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003737 }
3738 }
Zoe Liu7b1ec7a2017-05-24 22:28:24 -07003739
Hui Su5ebd8702018-01-08 18:09:20 -08003740 av1_setup_scale_factors_for_frame(&cm->sf_identity, cm->width, cm->height,
Debargha Mukherjeee242a812018-03-07 21:43:09 -08003741 cm->width, cm->height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07003742
3743 set_ref_ptrs(cm, xd, LAST_FRAME, LAST_FRAME);
3744}
3745
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003746static uint8_t calculate_next_resize_scale(const AV1_COMP *cpi) {
3747 // Choose an arbitrary random number
3748 static unsigned int seed = 56789;
3749 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003750 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3751 uint8_t new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003752
Debargha Mukherjee2b7c2b32018-04-10 07:35:28 -07003753 if (cpi->common.seq_params.reduced_still_picture_hdr) return SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003754 switch (oxcf->resize_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003755 case RESIZE_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003756 case RESIZE_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003757 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003758 new_denom = oxcf->resize_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003759 else
Urvang Joshide71d142017-10-05 12:12:15 -07003760 new_denom = oxcf->resize_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003761 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003762 case RESIZE_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003763 default: assert(0);
3764 }
Urvang Joshide71d142017-10-05 12:12:15 -07003765 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003766}
3767
Debargha Mukherjeee3cd5a52018-11-29 11:05:22 -08003768#define ENERGY_BY_Q2_THRESH 0.01
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003769#define ENERGY_BY_AC_THRESH 0.2
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003770
3771static uint8_t get_superres_denom_from_qindex_energy(int qindex, double *energy,
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003772 double threshq,
3773 double threshp) {
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003774 const double q = av1_convert_qindex_to_q(qindex, AOM_BITS_8);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003775 const double tq = threshq * q * q;
3776 const double tp = threshp * energy[1];
3777 const double thresh = AOMMIN(tq, tp);
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003778 int k;
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003779 for (k = 16; k > 8; --k) {
3780 if (energy[k - 1] > thresh) break;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003781 }
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003782 return 3 * SCALE_NUMERATOR - k;
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003783}
3784
3785static uint8_t get_superres_denom_for_qindex(const AV1_COMP *cpi, int qindex) {
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003786 double energy[16];
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003787 analyze_hor_freq(cpi, energy);
Debargha Mukherjee21eb0402018-12-03 12:10:59 -08003788 /*
3789 printf("\nenergy = [");
3790 for (int k = 1; k < 16; ++k) printf("%f, ", energy[k]);
3791 printf("]\n");
3792 */
3793 return get_superres_denom_from_qindex_energy(
3794 qindex, energy, ENERGY_BY_Q2_THRESH, ENERGY_BY_AC_THRESH);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08003795}
3796
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003797static uint8_t calculate_next_superres_scale(AV1_COMP *cpi) {
3798 // Choose an arbitrary random number
3799 static unsigned int seed = 34567;
3800 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Urvang Joshide71d142017-10-05 12:12:15 -07003801 if (oxcf->pass == 1) return SCALE_NUMERATOR;
3802 uint8_t new_denom = SCALE_NUMERATOR;
Urvang Joshi2c92b072018-03-19 17:23:31 -07003803
3804 // Make sure that superres mode of the frame is consistent with the
3805 // sequence-level flag.
3806 assert(IMPLIES(oxcf->superres_mode != SUPERRES_NONE,
3807 cpi->common.seq_params.enable_superres));
3808 assert(IMPLIES(!cpi->common.seq_params.enable_superres,
3809 oxcf->superres_mode == SUPERRES_NONE));
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003810
3811 switch (oxcf->superres_mode) {
Urvang Joshide71d142017-10-05 12:12:15 -07003812 case SUPERRES_NONE: new_denom = SCALE_NUMERATOR; break;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003813 case SUPERRES_FIXED:
David Turnerd2a592e2018-11-16 14:59:31 +00003814 if (cpi->common.current_frame.frame_type == KEY_FRAME)
Urvang Joshide71d142017-10-05 12:12:15 -07003815 new_denom = oxcf->superres_kf_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003816 else
Urvang Joshide71d142017-10-05 12:12:15 -07003817 new_denom = oxcf->superres_scale_denominator;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003818 break;
Urvang Joshide71d142017-10-05 12:12:15 -07003819 case SUPERRES_RANDOM: new_denom = lcg_rand16(&seed) % 9 + 8; break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003820 case SUPERRES_QTHRESH: {
Debargha Mukherjeedf713102018-10-02 12:33:32 -07003821 // Do not use superres when screen content tools are used.
3822 if (cpi->common.allow_screen_content_tools) break;
Debargha Mukherjee2b2c5fd2018-11-14 13:21:24 -08003823 if (oxcf->rc_mode == AOM_VBR || oxcf->rc_mode == AOM_CQ)
3824 av1_set_target_rate(cpi, cpi->oxcf.width, cpi->oxcf.height);
Urvang Joshi2c92b072018-03-19 17:23:31 -07003825 int bottom_index, top_index;
3826 const int q = av1_rc_pick_q_and_bounds(
3827 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08003828
Debargha Mukherjeeacd9b7d2018-11-26 15:15:05 -08003829 const int qthresh = (frame_is_intra_only(&cpi->common))
3830 ? oxcf->superres_kf_qthresh
3831 : oxcf->superres_qthresh;
Urvang Joshib17e0a62019-01-11 16:11:54 -08003832 if (q <= qthresh) {
Urvang Joshide71d142017-10-05 12:12:15 -07003833 new_denom = SCALE_NUMERATOR;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003834 } else {
Debargha Mukherjeee3cd5a52018-11-29 11:05:22 -08003835 new_denom = get_superres_denom_for_qindex(cpi, q);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003836 }
3837 break;
Urvang Joshif1fa6862018-01-08 16:39:33 -08003838 }
Urvang Joshi36a83732019-01-31 15:31:57 -08003839 case SUPERRES_AUTO: {
3840 // Don't use when screen content tools are used.
3841 if (cpi->common.allow_screen_content_tools) break;
3842 // Don't use for inter frames.
3843 if (!frame_is_intra_only(&cpi->common)) break;
3844 // Don't use for keyframes that can be used as references.
3845 if (cpi->rc.frames_to_key != 1) break;
Urvang Joshi36a83732019-01-31 15:31:57 -08003846
3847 // Now decide the use of superres based on 'q'.
3848 int bottom_index, top_index;
3849 const int q = av1_rc_pick_q_and_bounds(
3850 cpi, cpi->oxcf.width, cpi->oxcf.height, &bottom_index, &top_index);
3851
3852 const int qthresh = 128;
3853 if (q <= qthresh) {
3854 new_denom = SCALE_NUMERATOR;
3855 } else {
3856 new_denom = get_superres_denom_for_qindex(cpi, q);
3857 }
3858 break;
3859 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003860 default: assert(0);
3861 }
Urvang Joshide71d142017-10-05 12:12:15 -07003862 return new_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003863}
3864
Urvang Joshide71d142017-10-05 12:12:15 -07003865static int dimension_is_ok(int orig_dim, int resized_dim, int denom) {
3866 return (resized_dim * SCALE_NUMERATOR >= orig_dim * denom / 2);
3867}
3868
3869static int dimensions_are_ok(int owidth, int oheight, size_params_type *rsz) {
Urvang Joshi94ad3702017-12-06 11:38:08 -08003870 // Only need to check the width, as scaling is horizontal only.
3871 (void)oheight;
3872 return dimension_is_ok(owidth, rsz->resize_width, rsz->superres_denom);
Urvang Joshide71d142017-10-05 12:12:15 -07003873}
3874
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003875static int validate_size_scales(RESIZE_MODE resize_mode,
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003876 SUPERRES_MODE superres_mode, int owidth,
3877 int oheight, size_params_type *rsz) {
Urvang Joshide71d142017-10-05 12:12:15 -07003878 if (dimensions_are_ok(owidth, oheight, rsz)) { // Nothing to do.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003879 return 1;
Urvang Joshide71d142017-10-05 12:12:15 -07003880 }
3881
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003882 // Calculate current resize scale.
Urvang Joshide71d142017-10-05 12:12:15 -07003883 int resize_denom =
3884 AOMMAX(DIVIDE_AND_ROUND(owidth * SCALE_NUMERATOR, rsz->resize_width),
3885 DIVIDE_AND_ROUND(oheight * SCALE_NUMERATOR, rsz->resize_height));
3886
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003887 if (resize_mode != RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003888 // Alter superres scale as needed to enforce conformity.
3889 rsz->superres_denom =
3890 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / resize_denom;
3891 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3892 if (rsz->superres_denom > SCALE_NUMERATOR) --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003893 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003894 } else if (resize_mode == RESIZE_RANDOM && superres_mode != SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003895 // Alter resize scale as needed to enforce conformity.
3896 resize_denom =
3897 (2 * SCALE_NUMERATOR * SCALE_NUMERATOR) / rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003898 rsz->resize_width = owidth;
3899 rsz->resize_height = oheight;
3900 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003901 resize_denom);
3902 if (!dimensions_are_ok(owidth, oheight, rsz)) {
3903 if (resize_denom > SCALE_NUMERATOR) {
3904 --resize_denom;
3905 rsz->resize_width = owidth;
3906 rsz->resize_height = oheight;
3907 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
3908 resize_denom);
3909 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003910 }
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003911 } else if (resize_mode == RESIZE_RANDOM && superres_mode == SUPERRES_RANDOM) {
Urvang Joshide71d142017-10-05 12:12:15 -07003912 // Alter both resize and superres scales as needed to enforce conformity.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003913 do {
Urvang Joshide71d142017-10-05 12:12:15 -07003914 if (resize_denom > rsz->superres_denom)
3915 --resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003916 else
Urvang Joshide71d142017-10-05 12:12:15 -07003917 --rsz->superres_denom;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003918 rsz->resize_width = owidth;
3919 rsz->resize_height = oheight;
3920 av1_calculate_scaled_size(&rsz->resize_width, &rsz->resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003921 resize_denom);
3922 } while (!dimensions_are_ok(owidth, oheight, rsz) &&
3923 (resize_denom > SCALE_NUMERATOR ||
3924 rsz->superres_denom > SCALE_NUMERATOR));
Urvang Joshif1fa6862018-01-08 16:39:33 -08003925 } else { // We are allowed to alter neither resize scale nor superres
3926 // scale.
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003927 return 0;
3928 }
Urvang Joshide71d142017-10-05 12:12:15 -07003929 return dimensions_are_ok(owidth, oheight, rsz);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003930}
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003931
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003932// Calculates resize and superres params for next frame
David Turner475a3132019-01-18 15:17:17 +00003933static size_params_type calculate_next_size_params(AV1_COMP *cpi) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003934 const AV1EncoderConfig *oxcf = &cpi->oxcf;
Debargha Mukherjee3a4959f2018-02-26 15:34:03 -08003935 size_params_type rsz = { oxcf->width, oxcf->height, SCALE_NUMERATOR };
Urvang Joshide71d142017-10-05 12:12:15 -07003936 int resize_denom;
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003937 if (oxcf->pass == 1) return rsz;
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003938 if (cpi->resize_pending_width && cpi->resize_pending_height) {
3939 rsz.resize_width = cpi->resize_pending_width;
3940 rsz.resize_height = cpi->resize_pending_height;
3941 cpi->resize_pending_width = cpi->resize_pending_height = 0;
3942 } else {
Urvang Joshide71d142017-10-05 12:12:15 -07003943 resize_denom = calculate_next_resize_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003944 rsz.resize_width = cpi->oxcf.width;
3945 rsz.resize_height = cpi->oxcf.height;
3946 av1_calculate_scaled_size(&rsz.resize_width, &rsz.resize_height,
Urvang Joshide71d142017-10-05 12:12:15 -07003947 resize_denom);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003948 }
Urvang Joshide71d142017-10-05 12:12:15 -07003949 rsz.superres_denom = calculate_next_superres_scale(cpi);
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003950 if (!validate_size_scales(oxcf->resize_mode, oxcf->superres_mode, oxcf->width,
3951 oxcf->height, &rsz))
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003952 assert(0 && "Invalid scale parameters");
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003953 return rsz;
3954}
3955
Urvang Joshi22b150b2019-01-10 14:32:32 -08003956static void setup_frame_size_from_params(AV1_COMP *cpi,
3957 const size_params_type *rsz) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07003958 int encode_width = rsz->resize_width;
3959 int encode_height = rsz->resize_height;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003960
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003961 AV1_COMMON *cm = &cpi->common;
3962 cm->superres_upscaled_width = encode_width;
3963 cm->superres_upscaled_height = encode_height;
Urvang Joshide71d142017-10-05 12:12:15 -07003964 cm->superres_scale_denominator = rsz->superres_denom;
Urvang Joshi69fde2e2017-10-09 15:34:18 -07003965 av1_calculate_scaled_superres_size(&encode_width, &encode_height,
3966 rsz->superres_denom);
David Turner475a3132019-01-18 15:17:17 +00003967 av1_set_frame_size(cpi, encode_width, encode_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003968}
3969
David Turnerdedd8ff2019-01-23 13:59:46 +00003970void av1_setup_frame_size(AV1_COMP *cpi) {
Urvang Joshi22b150b2019-01-10 14:32:32 -08003971 // Reset superres params from previous frame.
3972 cpi->common.superres_scale_denominator = SCALE_NUMERATOR;
David Turner475a3132019-01-18 15:17:17 +00003973 const size_params_type rsz = calculate_next_size_params(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07003974 setup_frame_size_from_params(cpi, &rsz);
3975}
3976
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003977static void superres_post_encode(AV1_COMP *cpi) {
3978 AV1_COMMON *cm = &cpi->common;
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00003979 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003980
Cheng Chen09c83a52018-06-05 12:27:36 -07003981 if (!av1_superres_scaled(cm)) return;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003982
Urvang Joshid6b5d512018-03-20 13:34:38 -07003983 assert(cpi->oxcf.enable_superres);
3984 assert(!is_lossless_requested(&cpi->oxcf));
Urvang Joshic8b52d52018-03-23 13:16:51 -07003985 assert(!cm->all_lossless);
Urvang Joshid6b5d512018-03-20 13:34:38 -07003986
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003987 av1_superres_upscale(cm, NULL);
3988
3989 // If regular resizing is occurring the source will need to be downscaled to
3990 // match the upscaled superres resolution. Otherwise the original source is
3991 // used.
Cheng Chen09c83a52018-06-05 12:27:36 -07003992 if (!av1_resize_scaled(cm)) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07003993 cpi->source = cpi->unscaled_source;
3994 if (cpi->last_source != NULL) cpi->last_source = cpi->unscaled_last_source;
3995 } else {
Fergus Simpsonabd43432017-06-12 15:54:43 -07003996 assert(cpi->unscaled_source->y_crop_width != cm->superres_upscaled_width);
3997 assert(cpi->unscaled_source->y_crop_height != cm->superres_upscaled_height);
Urvang Joshif1fa6862018-01-08 16:39:33 -08003998 // Do downscale. cm->(width|height) has been updated by
3999 // av1_superres_upscale
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004000 if (aom_realloc_frame_buffer(
4001 &cpi->scaled_source, cm->superres_upscaled_width,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004002 cm->superres_upscaled_height, cm->seq_params.subsampling_x,
4003 cm->seq_params.subsampling_y, cm->seq_params.use_highbitdepth,
4004 AOM_BORDER_IN_PIXELS, cm->byte_alignment, NULL, NULL, NULL))
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004005 aom_internal_error(
4006 &cm->error, AOM_CODEC_MEM_ERROR,
4007 "Failed to reallocate scaled source buffer for superres");
4008 assert(cpi->scaled_source.y_crop_width == cm->superres_upscaled_width);
4009 assert(cpi->scaled_source.y_crop_height == cm->superres_upscaled_height);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004010 av1_resize_and_extend_frame(cpi->unscaled_source, &cpi->scaled_source,
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004011 (int)cm->seq_params.bit_depth, num_planes);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004012 cpi->source = &cpi->scaled_source;
4013 }
4014}
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004015
4016static void loopfilter_frame(AV1_COMP *cpi, AV1_COMMON *cm) {
Imdad Sardharwallaaf8e2642018-01-19 11:46:34 +00004017 const int num_planes = av1_num_planes(cm);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004018 MACROBLOCKD *xd = &cpi->td.mb.e_mbd;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004019
Urvang Joshic8b52d52018-03-23 13:16:51 -07004020 assert(IMPLIES(is_lossless_requested(&cpi->oxcf),
4021 cm->coded_lossless && cm->all_lossless));
4022
logangwf95c9162019-02-20 12:02:32 -08004023 const int use_loopfilter = !cm->coded_lossless && !cm->large_scale_tile;
4024 const int use_cdef = cm->seq_params.enable_cdef && !cm->coded_lossless &&
4025 !cm->large_scale_tile;
4026 const int use_restoration = cm->seq_params.enable_restoration &&
4027 !cm->all_lossless && !cm->large_scale_tile;
Urvang Joshi14072aa2018-03-21 17:43:36 -07004028
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004029 struct loopfilter *lf = &cm->lf;
Yunqing Wangeeb08a92017-07-07 21:25:18 -07004030
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004031#if CONFIG_COLLECT_COMPONENT_TIMING
4032 start_timing(cpi, loop_filter_time);
4033#endif
logangwf95c9162019-02-20 12:02:32 -08004034 if (use_loopfilter) {
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004035 aom_clear_system_state();
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004036 av1_pick_filter_level(cpi->source, cpi, cpi->sf.lpf_pick);
logangwf95c9162019-02-20 12:02:32 -08004037 } else {
4038 lf->filter_level[0] = 0;
4039 lf->filter_level[1] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004040 }
4041
Debargha Mukherjee2382b142018-02-26 14:31:32 -08004042 if (lf->filter_level[0] || lf->filter_level[1]) {
Deepa K G964e72e2018-05-16 16:56:01 +05304043 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004044 av1_loop_filter_frame_mt(&cm->cur_frame->buf, cm, xd, 0, num_planes, 0,
Cheng Chene3600cd2018-09-21 18:45:42 -07004045#if LOOP_FILTER_BITMASK
4046 0,
4047#endif
Deepa K G964e72e2018-05-16 16:56:01 +05304048 cpi->workers, cpi->num_workers,
4049 &cpi->lf_row_sync);
4050 else
David Turnerc29e1a92018-12-06 14:10:14 +00004051 av1_loop_filter_frame(&cm->cur_frame->buf, cm, xd,
Cheng Chen84b09932018-08-12 17:35:13 -07004052#if LOOP_FILTER_BITMASK
4053 0,
Cheng Chen8ab1f442018-04-27 18:01:52 -07004054#endif
Cheng Chen84b09932018-08-12 17:35:13 -07004055 0, num_planes, 0);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004056 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004057#if CONFIG_COLLECT_COMPONENT_TIMING
4058 end_timing(cpi, loop_filter_time);
4059#endif
Debargha Mukherjeee168a782017-08-31 12:30:10 -07004060
logangwf95c9162019-02-20 12:02:32 -08004061 if (use_restoration)
David Turnerc29e1a92018-12-06 14:10:14 +00004062 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 0);
Ola Hugosson1e7f2d02017-09-22 21:36:26 +02004063
logangwf95c9162019-02-20 12:02:32 -08004064 if (use_cdef) {
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004065#if CONFIG_COLLECT_COMPONENT_TIMING
4066 start_timing(cpi, cdef_time);
4067#endif
Steinar Midtskogen59782122017-07-20 08:49:43 +02004068 // Find CDEF parameters
David Turnerc29e1a92018-12-06 14:10:14 +00004069 av1_cdef_search(&cm->cur_frame->buf, cpi->source, cm, xd,
Debargha Mukherjeed7338aa2017-11-04 07:34:50 -07004070 cpi->sf.fast_cdef_search);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004071
4072 // Apply the filter
David Turnerc29e1a92018-12-06 14:10:14 +00004073 av1_cdef_frame(&cm->cur_frame->buf, cm, xd);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004074#if CONFIG_COLLECT_COMPONENT_TIMING
4075 end_timing(cpi, cdef_time);
4076#endif
logangwf95c9162019-02-20 12:02:32 -08004077 } else {
4078 cm->cdef_info.cdef_bits = 0;
4079 cm->cdef_info.cdef_strengths[0] = 0;
4080 cm->cdef_info.nb_cdef_strengths = 1;
4081 cm->cdef_info.cdef_uv_strengths[0] = 0;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004082 }
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004083
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004084 superres_post_encode(cpi);
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004085
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004086#if CONFIG_COLLECT_COMPONENT_TIMING
4087 start_timing(cpi, loop_restoration_time);
4088#endif
logangwf95c9162019-02-20 12:02:32 -08004089 if (use_restoration) {
David Turnerc29e1a92018-12-06 14:10:14 +00004090 av1_loop_restoration_save_boundary_lines(&cm->cur_frame->buf, cm, 1);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004091 av1_pick_filter_restoration(cpi->source, cpi);
4092 if (cm->rst_info[0].frame_restoration_type != RESTORE_NONE ||
4093 cm->rst_info[1].frame_restoration_type != RESTORE_NONE ||
4094 cm->rst_info[2].frame_restoration_type != RESTORE_NONE) {
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304095 if (cpi->num_workers > 1)
David Turnerc29e1a92018-12-06 14:10:14 +00004096 av1_loop_restoration_filter_frame_mt(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304097 cpi->workers, cpi->num_workers,
4098 &cpi->lr_row_sync, &cpi->lr_ctxt);
4099 else
David Turnerc29e1a92018-12-06 14:10:14 +00004100 av1_loop_restoration_filter_frame(&cm->cur_frame->buf, cm, 0,
Ravi Chaudharye2aa4012018-06-04 14:20:00 +05304101 &cpi->lr_ctxt);
Yaowu Xu35ee2342017-11-08 11:50:46 -08004102 }
logangwf95c9162019-02-20 12:02:32 -08004103 } else {
4104 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
4105 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
4106 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Fergus Simpsond2bcbb52017-05-22 23:15:05 -07004107 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004108#if CONFIG_COLLECT_COMPONENT_TIMING
4109 end_timing(cpi, loop_restoration_time);
4110#endif
Fergus Simpsonbc189932017-05-16 17:02:39 -07004111}
4112
David Turner996b2c12018-12-07 15:52:30 +00004113static int get_refresh_frame_flags(const AV1_COMP *const cpi) {
4114 const AV1_COMMON *const cm = &cpi->common;
4115
4116 // Switch frames and shown key-frames overwrite all reference slots
4117 if ((cm->current_frame.frame_type == KEY_FRAME && cm->show_frame) ||
4118 frame_is_sframe(cm))
4119 return 0xFF;
4120
David Turner73245762019-02-11 16:42:34 +00004121 // show_existing_frames don't actually send refresh_frame_flags so set the
4122 // flags to 0 to keep things consistent.
4123 if (encode_show_existing_frame(cm)) return 0;
4124
David Turner996b2c12018-12-07 15:52:30 +00004125 int refresh_mask = 0;
4126
4127 // NOTE(zoeliu): When LAST_FRAME is to get refreshed, the decoder will be
4128 // notified to get LAST3_FRAME refreshed and then the virtual indexes for all
4129 // the 3 LAST reference frames will be updated accordingly, i.e.:
4130 // (1) The original virtual index for LAST3_FRAME will become the new virtual
4131 // index for LAST_FRAME; and
4132 // (2) The original virtual indexes for LAST_FRAME and LAST2_FRAME will be
4133 // shifted and become the new virtual indexes for LAST2_FRAME and
4134 // LAST3_FRAME.
4135 refresh_mask |=
4136 (cpi->refresh_last_frame << get_ref_frame_map_idx(cm, LAST3_FRAME));
4137
David Turner996b2c12018-12-07 15:52:30 +00004138 const int bwd_ref_frame =
4139 (cpi->new_bwdref_update_rule == 1) ? EXTREF_FRAME : BWDREF_FRAME;
David Turner996b2c12018-12-07 15:52:30 +00004140 refresh_mask |=
4141 (cpi->refresh_bwd_ref_frame << get_ref_frame_map_idx(cm, bwd_ref_frame));
4142
4143 refresh_mask |=
4144 (cpi->refresh_alt2_ref_frame << get_ref_frame_map_idx(cm, ALTREF2_FRAME));
4145
David Turner73245762019-02-11 16:42:34 +00004146 if (cpi->rc.is_src_frame_alt_ref && !cpi->rc.is_src_frame_ext_arf) {
David Turner996b2c12018-12-07 15:52:30 +00004147 // We have decided to preserve the previously existing golden frame as our
4148 // new ARF frame. However, in the short term we leave it in the GF slot and,
4149 // if we're updating the GF with the current decoded frame, we save it
4150 // instead to the ARF slot.
4151 // Later, in the function av1_encoder.c:av1_update_reference_frames() we
4152 // will swap gld_fb_idx and alt_fb_idx to achieve our objective. We do it
4153 // there so that it can be done outside of the recode loop.
4154 // Note: This is highly specific to the use of ARF as a forward reference,
4155 // and this needs to be generalized as other uses are implemented
4156 // (like RTC/temporal scalability).
4157
4158 if (!cpi->preserve_arf_as_gld) {
4159 refresh_mask |= (cpi->refresh_golden_frame
4160 << get_ref_frame_map_idx(cm, ALTREF_FRAME));
4161 }
4162 } else {
4163 refresh_mask |=
4164 (cpi->refresh_golden_frame << get_ref_frame_map_idx(cm, GOLDEN_FRAME));
4165 refresh_mask |=
4166 (cpi->refresh_alt_ref_frame << get_ref_frame_map_idx(cm, ALTREF_FRAME));
4167 }
4168 return refresh_mask;
4169}
4170
David Turnerf2b334c2018-12-13 13:00:55 +00004171static void fix_interp_filter(InterpFilter *const interp_filter,
4172 const FRAME_COUNTS *const counts) {
4173 if (*interp_filter == SWITCHABLE) {
4174 // Check to see if only one of the filters is actually used
4175 int count[SWITCHABLE_FILTERS] = { 0 };
4176 int num_filters_used = 0;
4177 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4178 for (int j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
4179 count[i] += counts->switchable_interp[j][i];
4180 num_filters_used += (count[i] > 0);
4181 }
4182 if (num_filters_used == 1) {
4183 // Only one filter is used. So set the filter at frame level
4184 for (int i = 0; i < SWITCHABLE_FILTERS; ++i) {
4185 if (count[i]) {
4186 if (i == EIGHTTAP_REGULAR) *interp_filter = i;
4187 break;
4188 }
4189 }
4190 }
4191 }
4192}
4193
David Turner996b2c12018-12-07 15:52:30 +00004194static void finalize_encoded_frame(AV1_COMP *const cpi) {
4195 AV1_COMMON *const cm = &cpi->common;
David Turner99e990e2018-12-10 12:54:26 +00004196 CurrentFrame *const current_frame = &cm->current_frame;
David Turner996b2c12018-12-07 15:52:30 +00004197
4198 // This bitfield indicates which reference frame slots will be overwritten by
4199 // the current frame
David Turner99e990e2018-12-10 12:54:26 +00004200 current_frame->refresh_frame_flags = get_refresh_frame_flags(cpi);
4201
David Turner99e990e2018-12-10 12:54:26 +00004202 if (!cm->seq_params.reduced_still_picture_hdr &&
4203 encode_show_existing_frame(cm)) {
4204 RefCntBuffer *const frame_to_show =
4205 cm->ref_frame_map[cpi->existing_fb_idx_to_show];
4206
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004207 if (frame_to_show == NULL) {
David Turner99e990e2018-12-10 12:54:26 +00004208 aom_internal_error(&cm->error, AOM_CODEC_UNSUP_BITSTREAM,
4209 "Buffer does not contain a reconstructed frame");
4210 }
Wan-Teh Chang88cd1662019-01-14 12:38:41 -08004211 assert(frame_to_show->ref_count > 0);
David Turner99e990e2018-12-10 12:54:26 +00004212 assign_frame_buffer_p(&cm->cur_frame, frame_to_show);
David Turner99e990e2018-12-10 12:54:26 +00004213 }
David Turner08f909c2018-12-18 13:29:14 +00004214
4215 if (!encode_show_existing_frame(cm) &&
4216 cm->seq_params.film_grain_params_present &&
4217 (cm->show_frame || cm->showable_frame)) {
4218 // Copy the current frame's film grain params to the its corresponding
4219 // RefCntBuffer slot.
4220 cm->cur_frame->film_grain_params = cm->film_grain_params;
4221
4222 // We must update the parameters if this is not an INTER_FRAME
4223 if (current_frame->frame_type != INTER_FRAME)
4224 cm->cur_frame->film_grain_params.update_parameters = 1;
4225
4226 // Iterate the random seed for the next frame.
4227 cm->film_grain_params.random_seed += 3381;
4228 if (cm->film_grain_params.random_seed == 0)
4229 cm->film_grain_params.random_seed = 7391;
4230 }
David Turnerf2b334c2018-12-13 13:00:55 +00004231
4232 // Initialise all tiles' contexts from the global frame context
4233 for (int tile_col = 0; tile_col < cm->tile_cols; tile_col++) {
4234 for (int tile_row = 0; tile_row < cm->tile_rows; tile_row++) {
4235 const int tile_idx = tile_row * cm->tile_cols + tile_col;
4236 cpi->tile_data[tile_idx].tctx = *cm->fc;
4237 }
4238 }
4239
4240 fix_interp_filter(&cm->interp_filter, cpi->td.counts);
David Turner996b2c12018-12-07 15:52:30 +00004241}
4242
David Turner2f3b5df2019-01-02 14:30:50 +00004243// Called after encode_with_recode_loop() has just encoded a frame and packed
4244// its bitstream. This function works out whether we under- or over-shot
4245// our bitrate target and adjusts q as appropriate. Also decides whether
4246// or not we should do another recode loop, indicated by *loop
4247static void recode_loop_update_q(AV1_COMP *const cpi, int *const loop,
4248 int *const q, int *const q_low,
4249 int *const q_high, const int top_index,
4250 const int bottom_index,
4251 int *const undershoot_seen,
4252 int *const overshoot_seen,
4253 const int loop_at_this_size) {
4254 AV1_COMMON *const cm = &cpi->common;
4255 RATE_CONTROL *const rc = &cpi->rc;
4256
4257 int frame_over_shoot_limit = 0, frame_under_shoot_limit = 0;
4258 av1_rc_compute_frame_size_bounds(cpi, rc->this_frame_target,
4259 &frame_under_shoot_limit,
4260 &frame_over_shoot_limit);
4261 if (frame_over_shoot_limit == 0) frame_over_shoot_limit = 1;
4262
4263 if ((cm->current_frame.frame_type == KEY_FRAME) &&
4264 rc->this_key_frame_forced &&
4265 (rc->projected_frame_size < rc->max_frame_bandwidth)) {
4266 int last_q = *q;
4267 int64_t kf_err;
4268
4269 int64_t high_err_target = cpi->ambient_err;
4270 int64_t low_err_target = cpi->ambient_err >> 1;
4271
4272 if (cm->seq_params.use_highbitdepth) {
4273 kf_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
4274 } else {
4275 kf_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
4276 }
4277 // Prevent possible divide by zero error below for perfect KF
4278 kf_err += !kf_err;
4279
4280 // The key frame is not good enough or we can afford
4281 // to make it better without undue risk of popping.
4282 if ((kf_err > high_err_target &&
4283 rc->projected_frame_size <= frame_over_shoot_limit) ||
4284 (kf_err > low_err_target &&
4285 rc->projected_frame_size <= frame_under_shoot_limit)) {
4286 // Lower q_high
4287 *q_high = *q > *q_low ? *q - 1 : *q_low;
4288
4289 // Adjust Q
4290 *q = (int)((*q * high_err_target) / kf_err);
4291 *q = AOMMIN(*q, (*q_high + *q_low) >> 1);
4292 } else if (kf_err < low_err_target &&
4293 rc->projected_frame_size >= frame_under_shoot_limit) {
4294 // The key frame is much better than the previous frame
4295 // Raise q_low
4296 *q_low = *q < *q_high ? *q + 1 : *q_high;
4297
4298 // Adjust Q
4299 *q = (int)((*q * low_err_target) / kf_err);
4300 *q = AOMMIN(*q, (*q_high + *q_low + 1) >> 1);
4301 }
4302
4303 // Clamp Q to upper and lower limits:
4304 *q = clamp(*q, *q_low, *q_high);
4305
4306 *loop = *q != last_q;
4307 } else if (recode_loop_test(cpi, frame_over_shoot_limit,
4308 frame_under_shoot_limit, *q,
4309 AOMMAX(*q_high, top_index), bottom_index)) {
4310 // Is the projected frame size out of range and are we allowed
4311 // to attempt to recode.
4312 int last_q = *q;
4313 int retries = 0;
4314
4315 // Frame size out of permitted range:
4316 // Update correction factor & compute new Q to try...
4317 // Frame is too large
4318 if (rc->projected_frame_size > rc->this_frame_target) {
4319 // Special case if the projected size is > the max allowed.
4320 if (rc->projected_frame_size >= rc->max_frame_bandwidth)
4321 *q_high = rc->worst_quality;
4322
4323 // Raise Qlow as to at least the current value
4324 *q_low = *q < *q_high ? *q + 1 : *q_high;
4325
4326 if (*undershoot_seen || loop_at_this_size > 1) {
4327 // Update rate_correction_factor unless
4328 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4329
4330 *q = (*q_high + *q_low + 1) / 2;
4331 } else {
4332 // Update rate_correction_factor unless
4333 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4334
4335 *q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4336 AOMMAX(*q_high, top_index), cm->width,
4337 cm->height);
4338
4339 while (*q < *q_low && retries < 10) {
4340 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4341 *q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4342 AOMMAX(*q_high, top_index), cm->width,
4343 cm->height);
4344 retries++;
4345 }
4346 }
4347
4348 *overshoot_seen = 1;
4349 } else {
4350 // Frame is too small
4351 *q_high = *q > *q_low ? *q - 1 : *q_low;
4352
4353 if (*overshoot_seen || loop_at_this_size > 1) {
4354 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4355 *q = (*q_high + *q_low) / 2;
4356 } else {
4357 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4358 *q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4359 top_index, cm->width, cm->height);
4360 // Special case reset for qlow for constrained quality.
4361 // This should only trigger where there is very substantial
4362 // undershoot on a frame and the auto cq level is above
4363 // the user passsed in value.
4364 if (cpi->oxcf.rc_mode == AOM_CQ && *q < *q_low) {
4365 *q_low = *q;
4366 }
4367
4368 while (*q > *q_high && retries < 10) {
4369 av1_rc_update_rate_correction_factors(cpi, cm->width, cm->height);
4370 *q = av1_rc_regulate_q(cpi, rc->this_frame_target, bottom_index,
4371 top_index, cm->width, cm->height);
4372 retries++;
4373 }
4374 }
4375
4376 *undershoot_seen = 1;
4377 }
4378
4379 // Clamp Q to upper and lower limits:
4380 *q = clamp(*q, *q_low, *q_high);
4381
4382 *loop = (*q != last_q);
4383 } else {
4384 *loop = 0;
4385 }
4386}
4387
Tom Finegane4099e32018-01-23 12:01:51 -08004388static int encode_with_recode_loop(AV1_COMP *cpi, size_t *size, uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004389 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004390 RATE_CONTROL *const rc = &cpi->rc;
David Turner2f3b5df2019-01-02 14:30:50 +00004391 const int allow_recode = cpi->sf.recode_loop != DISALLOW_RECODE;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004392
4393 set_size_independent_vars(cpi);
4394
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004395 cpi->source->buf_8bit_valid = 0;
Yaowu Xu9b0f7032017-07-31 11:01:19 -07004396
David Turnerdedd8ff2019-01-23 13:59:46 +00004397 av1_setup_frame_size(cpi);
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004398
David Turner2f3b5df2019-01-02 14:30:50 +00004399 int top_index = 0, bottom_index = 0;
4400 int q = 0, q_low = 0, q_high = 0;
4401 set_size_dependent_vars(cpi, &q, &bottom_index, &top_index);
4402 q_low = bottom_index;
4403 q_high = top_index;
4404
4405 // Loop variables
4406 int loop_count = 0;
4407 int loop_at_this_size = 0;
4408 int loop = 0;
4409 int overshoot_seen = 0;
4410 int undershoot_seen = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004411
4412#if CONFIG_COLLECT_COMPONENT_TIMING
4413 printf("\n Encoding a frame:");
4414#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004415 do {
Yaowu Xuf883b422016-08-30 14:01:10 -07004416 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004417
Urvang Joshif1fa6862018-01-08 16:39:33 -08004418 // if frame was scaled calculate global_motion_search again if already
4419 // done
David Turner2f3b5df2019-01-02 14:30:50 +00004420 if (loop_count > 0 && cpi->source && cpi->global_motion_search_done) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004421 if (cpi->source->y_crop_width != cm->width ||
David Turner2f3b5df2019-01-02 14:30:50 +00004422 cpi->source->y_crop_height != cm->height) {
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004423 cpi->global_motion_search_done = 0;
David Turner2f3b5df2019-01-02 14:30:50 +00004424 }
4425 }
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07004426 cpi->source =
4427 av1_scale_if_required(cm, cpi->unscaled_source, &cpi->scaled_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004428 if (cpi->unscaled_last_source != NULL) {
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004429 cpi->last_source = av1_scale_if_required(cm, cpi->unscaled_last_source,
4430 &cpi->scaled_last_source);
David Turner2f3b5df2019-01-02 14:30:50 +00004431 }
Debargha Mukherjee17e7b082017-08-13 09:33:03 -07004432
David Turner2f3b5df2019-01-02 14:30:50 +00004433 if (!frame_is_intra_only(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004434 if (loop_count > 0) {
4435 release_scaled_references(cpi);
4436 }
Cheng Chen46f30c72017-09-07 11:13:33 -07004437 scale_references(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004438 }
Yaowu Xuf883b422016-08-30 14:01:10 -07004439 av1_set_quantizer(cm, q);
Yue Chena7d80482019-01-31 09:48:09 -08004440 av1_init_quantizer(cpi);
kyslov7b9d0d62018-12-21 11:12:26 -08004441
4442 av1_set_variance_partition_thresholds(cpi, q, 0);
4443
Debargha Mukherjeef48b0d22018-11-20 12:23:43 -08004444 // printf("Frame %d/%d: q = %d, frame_type = %d superres_denom = %d\n",
4445 // cm->current_frame.frame_number, cm->show_frame, q,
4446 // cm->current_frame.frame_type, cm->superres_scale_denominator);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004447
David Turner2f3b5df2019-01-02 14:30:50 +00004448 if (loop_count == 0) {
4449 setup_frame(cpi);
4450 } else if (get_primary_ref_frame_buf(cm) == NULL) {
4451 // Base q-index may have changed, so we need to assign proper default coef
4452 // probs before every iteration.
Yaowu Xuf883b422016-08-30 14:01:10 -07004453 av1_default_coef_probs(cm);
Hui Su3694c832017-11-10 14:15:58 -08004454 av1_setup_frame_contexts(cm);
David Barkerfc91b392018-03-09 15:32:03 +00004455 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004456
Yaowu Xuc27fc142016-08-22 16:08:15 -07004457 if (cpi->oxcf.aq_mode == VARIANCE_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004458 av1_vaq_frame_setup(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004459 } else if (cpi->oxcf.aq_mode == COMPLEXITY_AQ) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004460 av1_setup_in_frame_q_adj(cpi);
David Turner2f3b5df2019-01-02 14:30:50 +00004461 } else if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ && !allow_recode) {
4462 suppress_active_map(cpi);
4463 av1_cyclic_refresh_setup(cpi);
4464 apply_active_map(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004465 }
David Turner2f3b5df2019-01-02 14:30:50 +00004466
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004467 if (cm->seg.enabled) {
David Barkercab37552018-03-21 11:56:24 +00004468 if (!cm->seg.update_data && cm->prev_frame) {
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004469 segfeatures_copy(&cm->seg, &cm->prev_frame->seg);
David Barker11c93562018-06-05 12:00:07 +01004470 } else {
4471 calculate_segdata(&cm->seg);
Yue Chend90d3432018-03-16 11:28:42 -07004472 }
David Barkercab37552018-03-21 11:56:24 +00004473 } else {
4474 memset(&cm->seg, 0, sizeof(cm->seg));
Rostislav Pehlivanov3a964622018-03-14 18:00:32 +00004475 }
David Barkercab37552018-03-21 11:56:24 +00004476 segfeatures_copy(&cm->cur_frame->seg, &cm->seg);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004477
David Turner2f3b5df2019-01-02 14:30:50 +00004478 if (allow_recode) save_coding_context(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004479#if CONFIG_COLLECT_COMPONENT_TIMING
4480 start_timing(cpi, av1_encode_frame_time);
4481#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004482 // transform / motion compensation build reconstruction frame
Yaowu Xuf883b422016-08-30 14:01:10 -07004483 av1_encode_frame(cpi);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004484#if CONFIG_COLLECT_COMPONENT_TIMING
4485 end_timing(cpi, av1_encode_frame_time);
4486#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004487
David Turner2f3b5df2019-01-02 14:30:50 +00004488 // Update some stats from cyclic refresh, and check if we should not update
4489 // golden reference, for 1 pass CBR.
4490 if (cpi->oxcf.aq_mode == CYCLIC_REFRESH_AQ &&
4491 cm->current_frame.frame_type != KEY_FRAME &&
4492 (cpi->oxcf.pass == 0 && cpi->oxcf.rc_mode == AOM_CBR)) {
4493 av1_cyclic_refresh_check_golden_update(cpi);
4494 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004495
Yaowu Xuf883b422016-08-30 14:01:10 -07004496 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004497
4498 // Dummy pack of the bitstream using up to date stats to get an
4499 // accurate estimate of output frame size to determine if we need
4500 // to recode.
4501 if (cpi->sf.recode_loop >= ALLOW_RECODE_KFARFGF) {
Jingning Han8f661602017-08-19 08:16:50 -07004502 restore_coding_context(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004503
David Turner996b2c12018-12-07 15:52:30 +00004504 finalize_encoded_frame(cpi);
David Turner35cba132018-12-10 15:48:15 +00004505 int largest_tile_id = 0; // Output from bitstream: unused here
4506 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004507 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004508
4509 rc->projected_frame_size = (int)(*size) << 3;
4510 restore_coding_context(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004511 }
4512
David Turner2f3b5df2019-01-02 14:30:50 +00004513 if (allow_recode && cpi->oxcf.rc_mode != AOM_Q) {
4514 // Update q and decide whether to do a recode loop
4515 recode_loop_update_q(cpi, &loop, &q, &q_low, &q_high, top_index,
4516 bottom_index, &undershoot_seen, &overshoot_seen,
4517 loop_at_this_size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004518 }
4519
4520 // Special case for overlay frame.
4521 if (rc->is_src_frame_alt_ref &&
4522 rc->projected_frame_size < rc->max_frame_bandwidth)
4523 loop = 0;
4524
David Turner2f3b5df2019-01-02 14:30:50 +00004525 if (allow_recode && !cpi->sf.gm_disable_recode &&
4526 recode_loop_test_global_motion(cpi)) {
4527 loop = 1;
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004528 }
Debargha Mukherjeeb98a7022016-11-15 16:07:12 -08004529
Yaowu Xuc27fc142016-08-22 16:08:15 -07004530 if (loop) {
4531 ++loop_count;
4532 ++loop_at_this_size;
4533
4534#if CONFIG_INTERNAL_STATS
4535 ++cpi->tot_recode_hits;
4536#endif
4537 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004538#if CONFIG_COLLECT_COMPONENT_TIMING
4539 if (loop) printf("\n Recoding:");
4540#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07004541 } while (loop);
Tom Finegane4099e32018-01-23 12:01:51 -08004542
4543 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004544}
4545
Yaowu Xuc27fc142016-08-22 16:08:15 -07004546#define DUMP_RECON_FRAMES 0
4547
4548#if DUMP_RECON_FRAMES == 1
4549// NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Yaowu Xuf883b422016-08-30 14:01:10 -07004550static void dump_filtered_recon_frames(AV1_COMP *cpi) {
4551 AV1_COMMON *const cm = &cpi->common;
David Turnerd2a592e2018-11-16 14:59:31 +00004552 const CurrentFrame *const current_frame = &cm->current_frame;
David Turnerc29e1a92018-12-06 14:10:14 +00004553 const YV12_BUFFER_CONFIG *recon_buf = &cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004554
Zoe Liub4f31032017-11-03 23:48:35 -07004555 if (recon_buf == NULL) {
David Turnerd2a592e2018-11-16 14:59:31 +00004556 printf("Frame %d is not ready.\n", current_frame->frame_number);
Zoe Liub4f31032017-11-03 23:48:35 -07004557 return;
4558 }
4559
Zoe Liu27deb382018-03-27 15:13:56 -07004560 static const int flag_list[REF_FRAMES] = { 0,
4561 AOM_LAST_FLAG,
4562 AOM_LAST2_FLAG,
4563 AOM_LAST3_FLAG,
4564 AOM_GOLD_FLAG,
4565 AOM_BWD_FLAG,
4566 AOM_ALT2_FLAG,
4567 AOM_ALT_FLAG };
Zoe Liub4f31032017-11-03 23:48:35 -07004568 printf(
4569 "\n***Frame=%d (frame_offset=%d, show_frame=%d, "
4570 "show_existing_frame=%d) "
4571 "[LAST LAST2 LAST3 GOLDEN BWD ALT2 ALT]=[",
David Turnerd2a592e2018-11-16 14:59:31 +00004572 current_frame->frame_number, current_frame->order_hint, cm->show_frame,
Zoe Liub4f31032017-11-03 23:48:35 -07004573 cm->show_existing_frame);
4574 for (int ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame) {
David Turnera21966b2018-12-05 14:48:49 +00004575 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref_frame);
4576 const int ref_offset = buf != NULL ? (int)buf->order_hint : -1;
David Turner1bcefb32018-11-19 17:54:00 +00004577 printf(" %d(%c-%d-%4.2f)", ref_offset,
4578 (cpi->ref_frame_flags & flag_list[ref_frame]) ? 'Y' : 'N',
David Turnera21966b2018-12-05 14:48:49 +00004579 buf ? (int)buf->frame_rf_level : -1,
4580 buf ? rate_factor_deltas[buf->frame_rf_level] : -1);
Zoe Liub4f31032017-11-03 23:48:35 -07004581 }
4582 printf(" ]\n");
Zoe Liub4f31032017-11-03 23:48:35 -07004583
4584 if (!cm->show_frame) {
4585 printf("Frame %d is a no show frame, so no image dump.\n",
David Turnerd2a592e2018-11-16 14:59:31 +00004586 current_frame->frame_number);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004587 return;
4588 }
4589
Zoe Liub4f31032017-11-03 23:48:35 -07004590 int h;
4591 char file_name[256] = "/tmp/enc_filtered_recon.yuv";
4592 FILE *f_recon = NULL;
4593
David Turnerd2a592e2018-11-16 14:59:31 +00004594 if (current_frame->frame_number == 0) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004595 if ((f_recon = fopen(file_name, "wb")) == NULL) {
4596 printf("Unable to open file %s to write.\n", file_name);
4597 return;
4598 }
4599 } else {
4600 if ((f_recon = fopen(file_name, "ab")) == NULL) {
4601 printf("Unable to open file %s to append.\n", file_name);
4602 return;
4603 }
4604 }
4605 printf(
Zoe Liuf40a9572017-10-13 12:37:19 -07004606 "\nFrame=%5d, encode_update_type[%5d]=%1d, frame_offset=%d, "
4607 "show_frame=%d, show_existing_frame=%d, source_alt_ref_active=%d, "
4608 "refresh_alt_ref_frame=%d, rf_level=%d, "
4609 "y_stride=%4d, uv_stride=%4d, cm->width=%4d, cm->height=%4d\n\n",
David Turnerd2a592e2018-11-16 14:59:31 +00004610 current_frame->frame_number, cpi->twopass.gf_group.index,
Yaowu Xuc27fc142016-08-22 16:08:15 -07004611 cpi->twopass.gf_group.update_type[cpi->twopass.gf_group.index],
David Turnerd2a592e2018-11-16 14:59:31 +00004612 current_frame->order_hint, cm->show_frame, cm->show_existing_frame,
Zoe Liuf40a9572017-10-13 12:37:19 -07004613 cpi->rc.source_alt_ref_active, cpi->refresh_alt_ref_frame,
4614 cpi->twopass.gf_group.rf_level[cpi->twopass.gf_group.index],
4615 recon_buf->y_stride, recon_buf->uv_stride, cm->width, cm->height);
Zoe Liue9b15e22017-07-19 15:53:01 -07004616#if 0
4617 int ref_frame;
4618 printf("get_ref_frame_map_idx: [");
4619 for (ref_frame = LAST_FRAME; ref_frame <= ALTREF_FRAME; ++ref_frame)
David Turnera21966b2018-12-05 14:48:49 +00004620 printf(" %d", get_ref_frame_map_idx(cm, ref_frame));
Zoe Liue9b15e22017-07-19 15:53:01 -07004621 printf(" ]\n");
Zoe Liue9b15e22017-07-19 15:53:01 -07004622#endif // 0
Yaowu Xuc27fc142016-08-22 16:08:15 -07004623
4624 // --- Y ---
4625 for (h = 0; h < cm->height; ++h) {
4626 fwrite(&recon_buf->y_buffer[h * recon_buf->y_stride], 1, cm->width,
4627 f_recon);
4628 }
4629 // --- U ---
4630 for (h = 0; h < (cm->height >> 1); ++h) {
4631 fwrite(&recon_buf->u_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4632 f_recon);
4633 }
4634 // --- V ---
4635 for (h = 0; h < (cm->height >> 1); ++h) {
4636 fwrite(&recon_buf->v_buffer[h * recon_buf->uv_stride], 1, (cm->width >> 1),
4637 f_recon);
4638 }
4639
4640 fclose(f_recon);
4641}
4642#endif // DUMP_RECON_FRAMES
4643
David Turnerbc0993e2019-02-15 14:42:23 +00004644static int get_interp_filter_selected(const AV1_COMMON *const cm,
4645 MV_REFERENCE_FRAME ref,
4646 InterpFilters ifilter) {
4647 const RefCntBuffer *const buf = get_ref_frame_buf(cm, ref);
4648 if (buf == NULL) return 0;
4649 return buf->interp_filter_selected[ifilter];
4650}
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304651
David Turnerbc0993e2019-02-15 14:42:23 +00004652static int setup_interp_filter_search_mask(AV1_COMP *cpi) {
4653 const AV1_COMMON *const cm = &cpi->common;
4654 int ref_total[REF_FRAMES] = { 0 };
4655
4656 if (cpi->common.last_frame_type == KEY_FRAME || cpi->refresh_alt_ref_frame)
4657 return 0;
4658
4659 for (MV_REFERENCE_FRAME ref = LAST_FRAME; ref <= ALTREF_FRAME; ++ref) {
4660 for (InterpFilters ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4661 ++ifilter) {
4662 ref_total[ref] += get_interp_filter_selected(cm, ref, ifilter);
4663 }
4664 }
4665 int ref_total_total = (ref_total[LAST2_FRAME] + ref_total[LAST3_FRAME] +
4666 ref_total[GOLDEN_FRAME] + ref_total[BWDREF_FRAME] +
4667 ref_total[ALTREF2_FRAME] + ref_total[ALTREF_FRAME]);
4668
4669 int mask = 0;
4670 for (InterpFilters ifilter = EIGHTTAP_REGULAR; ifilter <= MULTITAP_SHARP;
4671 ++ifilter) {
4672 int last_score = get_interp_filter_selected(cm, LAST_FRAME, ifilter) * 30;
4673 if (ref_total[LAST_FRAME] && last_score <= ref_total[LAST_FRAME]) {
4674 int filter_score =
4675 get_interp_filter_selected(cm, LAST2_FRAME, ifilter) * 20 +
4676 get_interp_filter_selected(cm, LAST3_FRAME, ifilter) * 20 +
4677 get_interp_filter_selected(cm, GOLDEN_FRAME, ifilter) * 20 +
4678 get_interp_filter_selected(cm, BWDREF_FRAME, ifilter) * 10 +
4679 get_interp_filter_selected(cm, ALTREF2_FRAME, ifilter) * 10 +
4680 get_interp_filter_selected(cm, ALTREF_FRAME, ifilter) * 10;
4681 if (filter_score < ref_total_total) mask |= 1 << ifilter;
4682 }
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304683 }
4684 return mask;
4685}
4686
David Turnerefed6372019-01-11 15:14:11 +00004687static int is_integer_mv(AV1_COMP *cpi, const YV12_BUFFER_CONFIG *cur_picture,
4688 const YV12_BUFFER_CONFIG *last_picture,
4689 hash_table *last_hash_table) {
4690 aom_clear_system_state();
4691 // check use hash ME
4692 int k;
4693 uint32_t hash_value_1;
4694 uint32_t hash_value_2;
4695
4696 const int block_size = 8;
4697 const double threshold_current = 0.8;
4698 const double threshold_average = 0.95;
4699 const int max_history_size = 32;
4700 int T = 0; // total block
4701 int C = 0; // match with collocated block
4702 int S = 0; // smooth region but not match with collocated block
4703 int M = 0; // match with other block
4704
4705 const int pic_width = cur_picture->y_width;
4706 const int pic_height = cur_picture->y_height;
4707 for (int i = 0; i + block_size <= pic_height; i += block_size) {
4708 for (int j = 0; j + block_size <= pic_width; j += block_size) {
4709 const int x_pos = j;
4710 const int y_pos = i;
4711 int match = 1;
4712 T++;
4713
4714 // check whether collocated block match with current
4715 uint8_t *p_cur = cur_picture->y_buffer;
4716 uint8_t *p_ref = last_picture->y_buffer;
4717 int stride_cur = cur_picture->y_stride;
4718 int stride_ref = last_picture->y_stride;
4719 p_cur += (y_pos * stride_cur + x_pos);
4720 p_ref += (y_pos * stride_ref + x_pos);
4721
4722 if (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH) {
4723 uint16_t *p16_cur = CONVERT_TO_SHORTPTR(p_cur);
4724 uint16_t *p16_ref = CONVERT_TO_SHORTPTR(p_ref);
4725 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4726 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4727 if (p16_cur[tmpX] != p16_ref[tmpX]) {
4728 match = 0;
4729 }
4730 }
4731 p16_cur += stride_cur;
4732 p16_ref += stride_ref;
4733 }
4734 } else {
4735 for (int tmpY = 0; tmpY < block_size && match; tmpY++) {
4736 for (int tmpX = 0; tmpX < block_size && match; tmpX++) {
4737 if (p_cur[tmpX] != p_ref[tmpX]) {
4738 match = 0;
4739 }
4740 }
4741 p_cur += stride_cur;
4742 p_ref += stride_ref;
4743 }
4744 }
4745
4746 if (match) {
4747 C++;
4748 continue;
4749 }
4750
4751 if (av1_hash_is_horizontal_perfect(cur_picture, block_size, x_pos,
4752 y_pos) ||
4753 av1_hash_is_vertical_perfect(cur_picture, block_size, x_pos, y_pos)) {
4754 S++;
4755 continue;
4756 }
4757
4758 av1_get_block_hash_value(
4759 cur_picture->y_buffer + y_pos * stride_cur + x_pos, stride_cur,
4760 block_size, &hash_value_1, &hash_value_2,
4761 (cur_picture->flags & YV12_FLAG_HIGHBITDEPTH), &cpi->td.mb);
4762 // Hashing does not work for highbitdepth currently.
4763 // TODO(Roger): Make it work for highbitdepth.
4764 if (av1_use_hash_me(&cpi->common)) {
4765 if (av1_has_exact_match(last_hash_table, hash_value_1, hash_value_2)) {
4766 M++;
4767 }
4768 }
4769 }
4770 }
4771
4772 assert(T > 0);
4773 double csm_rate = ((double)(C + S + M)) / ((double)(T));
4774 double m_rate = ((double)(M)) / ((double)(T));
4775
4776 cpi->csm_rate_array[cpi->rate_index] = csm_rate;
4777 cpi->m_rate_array[cpi->rate_index] = m_rate;
4778
4779 cpi->rate_index = (cpi->rate_index + 1) % max_history_size;
4780 cpi->rate_size++;
4781 cpi->rate_size = AOMMIN(cpi->rate_size, max_history_size);
4782
4783 if (csm_rate < threshold_current) {
4784 return 0;
4785 }
4786
4787 if (C == T) {
4788 return 1;
4789 }
4790
4791 double csm_average = 0.0;
4792 double m_average = 0.0;
4793
4794 for (k = 0; k < cpi->rate_size; k++) {
4795 csm_average += cpi->csm_rate_array[k];
4796 m_average += cpi->m_rate_array[k];
4797 }
4798 csm_average /= cpi->rate_size;
4799 m_average /= cpi->rate_size;
4800
4801 if (csm_average < threshold_average) {
4802 return 0;
4803 }
4804
4805 if (M > (T - C - S) / 3) {
4806 return 1;
4807 }
4808
4809 if (csm_rate > 0.99 && m_rate > 0.01) {
4810 return 1;
4811 }
4812
4813 if (csm_average + m_average > 1.01) {
4814 return 1;
4815 }
4816
4817 return 0;
4818}
4819
David Turner73245762019-02-11 16:42:34 +00004820// Refresh reference frame buffers according to refresh_frame_flags.
4821static void refresh_reference_frames(AV1_COMP *cpi) {
4822 AV1_COMMON *const cm = &cpi->common;
4823 // All buffers are refreshed for shown keyframes and S-frames.
4824
4825 for (int ref_frame = 0; ref_frame < REF_FRAMES; ref_frame++) {
4826 if (((cm->current_frame.refresh_frame_flags >> ref_frame) & 1) == 1) {
4827 assign_frame_buffer_p(&cm->ref_frame_map[ref_frame], cm->cur_frame);
4828 }
4829 }
4830}
4831
4832static int encode_frame_to_data_rate(AV1_COMP *cpi, size_t *size,
4833 uint8_t *dest) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004834 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004835 SequenceHeader *const seq_params = &cm->seq_params;
David Turnerd2a592e2018-11-16 14:59:31 +00004836 CurrentFrame *const current_frame = &cm->current_frame;
Yaowu Xuf883b422016-08-30 14:01:10 -07004837 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004838 struct segmentation *const seg = &cm->seg;
Thomas Davies4822e142017-10-10 11:30:36 +01004839
Yunqing Wangd1f32e62019-02-20 10:37:51 -08004840#if CONFIG_COLLECT_COMPONENT_TIMING
4841 start_timing(cpi, encode_frame_to_data_rate_time);
4842#endif
4843
Fangwen Fu8d164de2016-12-14 13:40:54 -08004844 // frame type has been decided outside of this function call
David Turnerd2a592e2018-11-16 14:59:31 +00004845 cm->cur_frame->frame_type = current_frame->frame_type;
Debargha Mukherjee07a7c1f2018-03-21 17:39:13 -07004846
Yunqing Wang9612d552018-05-15 14:58:30 -07004847 cm->large_scale_tile = cpi->oxcf.large_scale_tile;
4848 cm->single_tile_decoding = cpi->oxcf.single_tile_decoding;
Yunqing Wang9612d552018-05-15 14:58:30 -07004849
sarahparker21dbca42018-03-30 17:43:44 -07004850 cm->allow_ref_frame_mvs &= frame_might_allow_ref_frame_mvs(cm);
Yunqing Wangd48fb162018-06-15 10:55:28 -07004851 // cm->allow_ref_frame_mvs needs to be written into the frame header while
4852 // cm->large_scale_tile is 1, therefore, "cm->large_scale_tile=1" case is
4853 // separated from frame_might_allow_ref_frame_mvs().
4854 cm->allow_ref_frame_mvs &= !cm->large_scale_tile;
4855
Debargha Mukherjee1d7217e2018-03-26 13:32:13 -07004856 cm->allow_warped_motion =
Debargha Mukherjeea5b810a2018-03-26 19:19:55 -07004857 cpi->oxcf.allow_warped_motion && frame_might_allow_warped_motion(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004858
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05304859 cm->last_frame_type = current_frame->frame_type;
4860 if (cpi->oxcf.pass == 2 && cpi->sf.adaptive_interp_filter_search)
4861 cpi->sf.interp_filter_search_mask = setup_interp_filter_search_mask(cpi);
4862
Sarah Parker33005522018-07-27 14:46:25 -07004863 if (encode_show_existing_frame(cm)) {
Jingning Han8f661602017-08-19 08:16:50 -07004864 restore_coding_context(cpi);
Zoe Liub4f31032017-11-03 23:48:35 -07004865
David Turner996b2c12018-12-07 15:52:30 +00004866 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004867 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00004868 int largest_tile_id = 0; // Output from bitstream: unused here
4869 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08004870 return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004871
David Turner90311862018-11-29 13:34:36 +00004872 if (seq_params->frame_id_numbers_present_flag &&
4873 current_frame->frame_type == KEY_FRAME) {
4874 // Displaying a forward key-frame, so reset the ref buffer IDs
4875 int display_frame_id = cm->ref_frame_id[cpi->existing_fb_idx_to_show];
4876 for (int i = 0; i < REF_FRAMES; i++)
4877 cm->ref_frame_id[i] = display_frame_id;
4878 }
4879
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07004880 cpi->seq_params_locked = 1;
4881
Yaowu Xuc27fc142016-08-22 16:08:15 -07004882#if DUMP_RECON_FRAMES == 1
4883 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
4884 dump_filtered_recon_frames(cpi);
4885#endif // DUMP_RECON_FRAMES
4886
David Turner73245762019-02-11 16:42:34 +00004887 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
4888 // for the purpose to verify no mismatch between encoder and decoder.
4889 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004890
David Turner73245762019-02-11 16:42:34 +00004891 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004892
Yaowu Xuc27fc142016-08-22 16:08:15 -07004893 // Since we allocate a spot for the OVERLAY frame in the gf group, we need
4894 // to do post-encoding update accordingly.
4895 if (cpi->rc.is_src_frame_alt_ref) {
Debargha Mukherjee7166f222017-09-05 21:32:42 -07004896 av1_set_target_rate(cpi, cm->width, cm->height);
Yaowu Xuf883b422016-08-30 14:01:10 -07004897 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004898 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004899
David Turnerd2a592e2018-11-16 14:59:31 +00004900 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004901
Tom Finegane4099e32018-01-23 12:01:51 -08004902 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004903 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004904
David Turnerefed6372019-01-11 15:14:11 +00004905 // Work out whether to force_integer_mv this frame
4906 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools &&
4907 !frame_is_intra_only(cm)) {
4908 if (cpi->common.seq_params.force_integer_mv == 2) {
4909 // Adaptive mode: see what previous frame encoded did
4910 if (cpi->unscaled_last_source != NULL) {
4911 cm->cur_frame_force_integer_mv =
4912 is_integer_mv(cpi, cpi->source, cpi->unscaled_last_source,
4913 cpi->previous_hash_table);
4914 } else {
4915 cpi->common.cur_frame_force_integer_mv = 0;
4916 }
4917 } else {
4918 cpi->common.cur_frame_force_integer_mv =
4919 cpi->common.seq_params.force_integer_mv;
4920 }
4921 } else {
4922 cpi->common.cur_frame_force_integer_mv = 0;
4923 }
4924
Yaowu Xuc27fc142016-08-22 16:08:15 -07004925 // Set default state for segment based loop filter update flags.
4926 cm->lf.mode_ref_delta_update = 0;
4927
Yaowu Xuc27fc142016-08-22 16:08:15 -07004928 // Set various flags etc to special state if it is a key frame.
Tarek AMARAc9813852018-03-05 18:40:18 -05004929 if (frame_is_intra_only(cm) || frame_is_sframe(cm)) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07004930 // Reset the loop filter deltas and segmentation map.
Yaowu Xuf883b422016-08-30 14:01:10 -07004931 av1_reset_segment_features(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07004932
4933 // If segmentation is enabled force a map update for key frames.
4934 if (seg->enabled) {
4935 seg->update_map = 1;
4936 seg->update_data = 1;
4937 }
4938
4939 // The alternate reference frame cannot be active for a key frame.
4940 cpi->rc.source_alt_ref_active = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004941 }
Thomas Daviesaf6df172016-11-09 14:04:18 +00004942 if (cpi->oxcf.mtu == 0) {
4943 cm->num_tg = cpi->oxcf.num_tile_groups;
4944 } else {
Yaowu Xu859a5272016-11-10 15:32:21 -08004945 // Use a default value for the purposes of weighting costs in probability
4946 // updates
Thomas Daviesaf6df172016-11-09 14:04:18 +00004947 cm->num_tg = DEFAULT_MAX_NUM_TG;
4948 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07004949
4950 // For 1 pass CBR, check if we are dropping this frame.
4951 // Never drop on key frame.
Yaowu Xuf883b422016-08-30 14:01:10 -07004952 if (oxcf->pass == 0 && oxcf->rc_mode == AOM_CBR &&
David Turnerd2a592e2018-11-16 14:59:31 +00004953 current_frame->frame_type != KEY_FRAME) {
Yaowu Xuf883b422016-08-30 14:01:10 -07004954 if (av1_rc_drop_frame(cpi)) {
4955 av1_rc_postencode_update_drop_frame(cpi);
David Turnera4c96252019-01-11 16:36:39 +00004956 release_scaled_references(cpi);
Tom Finegane4099e32018-01-23 12:01:51 -08004957 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07004958 }
4959 }
4960
Yaowu Xuf883b422016-08-30 14:01:10 -07004961 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07004962
4963#if CONFIG_INTERNAL_STATS
4964 memset(cpi->mode_chosen_counts, 0,
4965 MAX_MODES * sizeof(*cpi->mode_chosen_counts));
4966#endif
4967
Urvang Joshi20cf30e2018-07-19 02:33:58 -07004968 if (seq_params->frame_id_numbers_present_flag) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004969 /* Non-normative definition of current_frame_id ("frame counter" with
Johann123e8a62017-12-28 14:40:49 -08004970 * wraparound) */
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004971 if (cm->current_frame_id == -1) {
David Barker49a76562016-12-07 14:50:21 +00004972 int lsb, msb;
Yaowu Xud3e7c682017-12-21 14:08:25 -08004973 /* quasi-random initialization of current_frame_id for a key frame */
Alex Conversef77fd0b2017-04-20 11:00:24 -07004974 if (cpi->source->flags & YV12_FLAG_HIGHBITDEPTH) {
4975 lsb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[0] & 0xff;
4976 msb = CONVERT_TO_SHORTPTR(cpi->source->y_buffer)[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004977 } else {
Alex Conversef77fd0b2017-04-20 11:00:24 -07004978 lsb = cpi->source->y_buffer[0] & 0xff;
4979 msb = cpi->source->y_buffer[1] & 0xff;
David Barker49a76562016-12-07 14:50:21 +00004980 }
David Turner760a2f42018-12-07 15:25:36 +00004981 cm->current_frame_id =
4982 ((msb << 8) + lsb) % (1 << seq_params->frame_id_length);
Tarek AMARAc9813852018-03-05 18:40:18 -05004983
4984 // S_frame is meant for stitching different streams of different
4985 // resolutions together, so current_frame_id must be the
4986 // same across different streams of the same content current_frame_id
4987 // should be the same and not random. 0x37 is a chosen number as start
4988 // point
4989 if (cpi->oxcf.sframe_enabled) cm->current_frame_id = 0x37;
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004990 } else {
4991 cm->current_frame_id =
David Turner760a2f42018-12-07 15:25:36 +00004992 (cm->current_frame_id + 1 + (1 << seq_params->frame_id_length)) %
4993 (1 << seq_params->frame_id_length);
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004994 }
4995 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01004996
Hui Su483a8452018-02-26 12:28:48 -08004997 switch (cpi->oxcf.cdf_update_mode) {
4998 case 0: // No CDF update for any frames(4~6% compression loss).
4999 cm->disable_cdf_update = 1;
5000 break;
5001 case 1: // Enable CDF update for all frames.
5002 cm->disable_cdf_update = 0;
5003 break;
5004 case 2:
5005 // Strategically determine at which frames to do CDF update.
5006 // Currently only enable CDF update for all-intra and no-show frames(1.5%
5007 // compression loss).
5008 // TODO(huisu@google.com): design schemes for various trade-offs between
5009 // compression quality and decoding speed.
Hui Sub1b76b32018-02-27 15:24:48 -08005010 cm->disable_cdf_update =
5011 (frame_is_intra_only(cm) || !cm->show_frame) ? 0 : 1;
Hui Su483a8452018-02-26 12:28:48 -08005012 break;
5013 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005014 cm->timing_info_present &= !seq_params->reduced_still_picture_hdr;
Hui Su483a8452018-02-26 12:28:48 -08005015
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005016#if CONFIG_COLLECT_COMPONENT_TIMING
5017 start_timing(cpi, encode_with_recode_loop_time);
5018#endif
David Turner2f3b5df2019-01-02 14:30:50 +00005019 if (encode_with_recode_loop(cpi, size, dest) != AOM_CODEC_OK)
5020 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005021#if CONFIG_COLLECT_COMPONENT_TIMING
5022 end_timing(cpi, encode_with_recode_loop_time);
5023#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005024
5025#ifdef OUTPUT_YUV_SKINMAP
David Turnerd2a592e2018-11-16 14:59:31 +00005026 if (cpi->common.current_frame.frame_number > 1) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005027 av1_compute_skin_map(cpi, yuv_skinmap_file);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005028 }
5029#endif // OUTPUT_YUV_SKINMAP
5030
5031 // Special case code to reduce pulsing when key frames are forced at a
5032 // fixed interval. Note the reconstruction error if it is the frame before
5033 // the force key frame
5034 if (cpi->rc.next_key_frame_forced && cpi->rc.frames_to_key == 1) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005035 if (seq_params->use_highbitdepth) {
Jack Haughtonddb80602018-11-21 16:41:49 +00005036 cpi->ambient_err = aom_highbd_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005037 } else {
Jack Haughtonddb80602018-11-21 16:41:49 +00005038 cpi->ambient_err = aom_get_y_sse(cpi->source, &cm->cur_frame->buf);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005039 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005040 }
5041
David Turnerc29e1a92018-12-06 14:10:14 +00005042 cm->cur_frame->buf.color_primaries = seq_params->color_primaries;
5043 cm->cur_frame->buf.transfer_characteristics =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005044 seq_params->transfer_characteristics;
David Turnerc29e1a92018-12-06 14:10:14 +00005045 cm->cur_frame->buf.matrix_coefficients = seq_params->matrix_coefficients;
5046 cm->cur_frame->buf.monochrome = seq_params->monochrome;
5047 cm->cur_frame->buf.chroma_sample_position =
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005048 seq_params->chroma_sample_position;
David Turnerc29e1a92018-12-06 14:10:14 +00005049 cm->cur_frame->buf.color_range = seq_params->color_range;
5050 cm->cur_frame->buf.render_width = cm->render_width;
5051 cm->cur_frame->buf.render_height = cm->render_height;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005052
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005053 // TODO(zoeliu): For non-ref frames, loop filtering may need to be turned
5054 // off.
Yaowu Xuc27fc142016-08-22 16:08:15 -07005055
5056 // Pick the loop filter level for the frame.
Cheng Chen68dc9142018-05-02 17:46:28 -07005057 if (!cm->allow_intrabc) {
David Barker218556e2018-02-14 14:23:12 +00005058 loopfilter_frame(cpi, cm);
Hui Su06463e42018-02-23 22:17:36 -08005059 } else {
Hui Su06463e42018-02-23 22:17:36 -08005060 cm->lf.filter_level[0] = 0;
5061 cm->lf.filter_level[1] = 0;
David Turnerebf96f42018-11-14 16:57:57 +00005062 cm->cdef_info.cdef_bits = 0;
5063 cm->cdef_info.cdef_strengths[0] = 0;
5064 cm->cdef_info.nb_cdef_strengths = 1;
5065 cm->cdef_info.cdef_uv_strengths[0] = 0;
Hui Su06463e42018-02-23 22:17:36 -08005066 cm->rst_info[0].frame_restoration_type = RESTORE_NONE;
5067 cm->rst_info[1].frame_restoration_type = RESTORE_NONE;
5068 cm->rst_info[2].frame_restoration_type = RESTORE_NONE;
Hui Su06463e42018-02-23 22:17:36 -08005069 }
David Barker218556e2018-02-14 14:23:12 +00005070
5071 // TODO(debargha): Fix mv search range on encoder side
David Turnerc29e1a92018-12-06 14:10:14 +00005072 // aom_extend_frame_inner_borders(&cm->cur_frame->buf, av1_num_planes(cm));
5073 aom_extend_frame_borders(&cm->cur_frame->buf, av1_num_planes(cm));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005074
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005075#ifdef OUTPUT_YUV_REC
David Turnerc29e1a92018-12-06 14:10:14 +00005076 aom_write_one_yuv_frame(cm, &cm->cur_frame->buf);
Wei-Ting Lin01d4d8f2017-08-03 17:04:12 -07005077#endif
5078
David Turner996b2c12018-12-07 15:52:30 +00005079 finalize_encoded_frame(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005080 // Build the bitstream
David Turner35cba132018-12-10 15:48:15 +00005081 int largest_tile_id = 0; // Output from pack_bitstream
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005082#if CONFIG_COLLECT_COMPONENT_TIMING
5083 start_timing(cpi, av1_pack_bitstream_final_time);
5084#endif
David Turner35cba132018-12-10 15:48:15 +00005085 if (av1_pack_bitstream(cpi, dest, size, &largest_tile_id) != AOM_CODEC_OK)
Tom Finegane4099e32018-01-23 12:01:51 -08005086 return AOM_CODEC_ERROR;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005087#if CONFIG_COLLECT_COMPONENT_TIMING
5088 end_timing(cpi, av1_pack_bitstream_final_time);
5089#endif
Yaowu Xuc27fc142016-08-22 16:08:15 -07005090
Debargha Mukherjeef2e5bb32018-03-26 14:35:24 -07005091 cpi->seq_params_locked = 1;
5092
David Turner996b2c12018-12-07 15:52:30 +00005093 // Update reference frame ids for reference frames this frame will overwrite
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005094 if (seq_params->frame_id_numbers_present_flag) {
David Turner996b2c12018-12-07 15:52:30 +00005095 for (int i = 0; i < REF_FRAMES; i++) {
5096 if ((current_frame->refresh_frame_flags >> i) & 1) {
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005097 cm->ref_frame_id[i] = cm->current_frame_id;
5098 }
5099 }
5100 }
Arild Fuldseth (arilfuld)5114b7b2016-11-09 13:32:54 +01005101
Yaowu Xuc27fc142016-08-22 16:08:15 -07005102#if DUMP_RECON_FRAMES == 1
5103 // NOTE(zoeliu): For debug - Output the filtered reconstructed video.
Zoe Liub4f31032017-11-03 23:48:35 -07005104 dump_filtered_recon_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005105#endif // DUMP_RECON_FRAMES
5106
Soo-Chul Han934af352017-10-15 15:21:51 -04005107 if (cm->seg.enabled) {
5108 if (cm->seg.update_map) {
5109 update_reference_segmentation_map(cpi);
Yue Chend90d3432018-03-16 11:28:42 -07005110 } else if (cm->last_frame_seg_map) {
David Turnerb757ce02018-11-12 15:01:28 +00005111 memcpy(cm->cur_frame->seg_map, cm->last_frame_seg_map,
Soo-Chul Han934af352017-10-15 15:21:51 -04005112 cm->mi_cols * cm->mi_rows * sizeof(uint8_t));
5113 }
5114 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005115
5116 if (frame_is_intra_only(cm) == 0) {
5117 release_scaled_references(cpi);
5118 }
5119
David Turner73245762019-02-11 16:42:34 +00005120 // NOTE: Save the new show frame buffer index for --test-code=warn, i.e.,
5121 // for the purpose to verify no mismatch between encoder and decoder.
5122 if (cm->show_frame) cpi->last_show_frame_buf = cm->cur_frame;
5123
5124 refresh_reference_frames(cpi);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005125
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005126#if CONFIG_ENTROPY_STATS
Yue Chencc6a6ef2018-05-21 16:21:05 -07005127 av1_accumulate_frame_counts(&aggregate_fc, &cpi->counts);
Debargha Mukherjee5802ebe2016-12-21 04:17:24 -08005128#endif // CONFIG_ENTROPY_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005129
Hui Sudc54be62018-03-14 19:14:28 -07005130 if (cm->refresh_frame_context == REFRESH_FRAME_CONTEXT_BACKWARD) {
David Turner35cba132018-12-10 15:48:15 +00005131 *cm->fc = cpi->tile_data[largest_tile_id].tctx;
Hui Sudc54be62018-03-14 19:14:28 -07005132 av1_reset_cdf_symbol_counters(cm->fc);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005133 }
David Turnera4c96252019-01-11 16:36:39 +00005134 if (!cm->large_scale_tile) {
5135 cm->cur_frame->frame_context = *cm->fc;
5136 }
5137#define EXT_TILE_DEBUG 0
5138#if EXT_TILE_DEBUG
5139 if (cm->large_scale_tile && oxcf->pass == 2) {
5140 char fn[20] = "./fc";
5141 fn[4] = current_frame->frame_number / 100 + '0';
5142 fn[5] = (current_frame->frame_number % 100) / 10 + '0';
5143 fn[6] = (current_frame->frame_number % 10) + '0';
5144 fn[7] = '\0';
5145 av1_print_frame_contexts(cm->fc, fn);
5146 }
5147#endif // EXT_TILE_DEBUG
5148#undef EXT_TILE_DEBUG
Yaowu Xuc27fc142016-08-22 16:08:15 -07005149
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005150#if CONFIG_COLLECT_COMPONENT_TIMING
5151 end_timing(cpi, encode_frame_to_data_rate_time);
5152
5153 // Print out timing information.
5154 int i;
5155 fprintf(stderr, "\n Frame number: %d, Frame type: %s,\n",
5156 cm->current_frame.frame_number,
5157 get_frame_type_enum(cm->current_frame.frame_type));
5158 for (i = 0; i < kTimingComponents; i++) {
5159 cpi->component_time[i] += cpi->frame_component_time[i];
5160 fprintf(stderr, " %s: %" PRId64 " us (total: %" PRId64 " us)\n",
5161 get_component_name(i), cpi->frame_component_time[i],
5162 cpi->component_time[i]);
5163 cpi->frame_component_time[i] = 0;
5164 }
5165#endif
5166
Sachin Kumar Gargfd39b232019-01-03 17:41:09 +05305167 cm->last_frame_type = current_frame->frame_type;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005168
Yaowu Xuf883b422016-08-30 14:01:10 -07005169 av1_rc_postencode_update(cpi, *size);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005170
David Turnerefed6372019-01-11 15:14:11 +00005171 // Store encoded frame's hash table for is_integer_mv() next time
5172 if (oxcf->pass != 1 && cpi->common.allow_screen_content_tools) {
5173 cpi->previous_hash_table = &cm->cur_frame->hash_table;
5174 }
5175
Yaowu Xuc27fc142016-08-22 16:08:15 -07005176 // Clear the one shot update flags for segmentation map and mode/ref loop
5177 // filter deltas.
5178 cm->seg.update_map = 0;
5179 cm->seg.update_data = 0;
5180 cm->lf.mode_ref_delta_update = 0;
5181
Wei-Ting Linfb7dc062018-06-28 18:26:13 -07005182 // A droppable frame might not be shown but it always
5183 // takes a space in the gf group. Therefore, even when
5184 // it is not shown, we still need update the count down.
5185
Yaowu Xuc27fc142016-08-22 16:08:15 -07005186 if (cm->show_frame) {
Urvang Joshif1fa6862018-01-08 16:39:33 -08005187 // TODO(zoeliu): We may only swamp mi and prev_mi for those frames that
5188 // are
Sebastien Alaiwan365e6442017-10-16 11:35:00 +02005189 // being used as reference.
Cheng Chen46f30c72017-09-07 11:13:33 -07005190 swap_mi_and_prev_mi(cm);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005191 // Don't increment frame counters if this was an altref buffer
5192 // update not a real frame
Wei-Ting Lin96ee0eb2018-06-22 15:27:22 -07005193
David Turnerd2a592e2018-11-16 14:59:31 +00005194 ++current_frame->frame_number;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005195 }
5196
Tom Finegane4099e32018-01-23 12:01:51 -08005197 return AOM_CODEC_OK;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005198}
5199
David Turner056f7cd2019-01-07 17:48:13 +00005200int av1_encode(AV1_COMP *const cpi, uint8_t *const dest,
David Turnercb5e36f2019-01-17 17:15:25 +00005201 const EncodeFrameInput *const frame_input,
David Turner056f7cd2019-01-07 17:48:13 +00005202 const EncodeFrameParams *const frame_params,
5203 EncodeFrameResults *const frame_results) {
David Turner07dbd8e2019-01-08 17:16:25 +00005204 AV1_COMMON *const cm = &cpi->common;
David Turnera7f133c2019-01-22 14:47:16 +00005205 CurrentFrame *const current_frame = &cm->current_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005206
David Turnercb5e36f2019-01-17 17:15:25 +00005207 cpi->unscaled_source = frame_input->source;
5208 cpi->source = frame_input->source;
5209 cpi->unscaled_last_source = frame_input->last_source;
David Turner056f7cd2019-01-07 17:48:13 +00005210
David Turner07dbd8e2019-01-08 17:16:25 +00005211 cm->error_resilient_mode = frame_params->error_resilient_mode;
David Turnera7f133c2019-01-22 14:47:16 +00005212 cm->primary_ref_frame = frame_params->primary_ref_frame;
David Turner475a3132019-01-18 15:17:17 +00005213 cm->current_frame.frame_type = frame_params->frame_type;
David Turnerdedd8ff2019-01-23 13:59:46 +00005214 cm->show_frame = frame_params->show_frame;
David Turner07dbd8e2019-01-08 17:16:25 +00005215 cpi->ref_frame_flags = frame_params->ref_frame_flags;
David Turner04b70d82019-01-24 15:39:19 +00005216 cpi->speed = frame_params->speed;
David Turner07dbd8e2019-01-08 17:16:25 +00005217
David Turner73245762019-02-11 16:42:34 +00005218 memcpy(cm->remapped_ref_idx, frame_params->remapped_ref_idx,
5219 REF_FRAMES * sizeof(*cm->remapped_ref_idx));
5220
David Turnerfe3aecb2019-02-06 14:42:42 +00005221 cpi->refresh_last_frame = frame_params->refresh_last_frame;
5222 cpi->refresh_golden_frame = frame_params->refresh_golden_frame;
5223 cpi->refresh_bwd_ref_frame = frame_params->refresh_bwd_ref_frame;
5224 cpi->refresh_alt2_ref_frame = frame_params->refresh_alt2_ref_frame;
5225 cpi->refresh_alt_ref_frame = frame_params->refresh_alt_ref_frame;
5226
David Turnera7f133c2019-01-22 14:47:16 +00005227 if (current_frame->frame_type == KEY_FRAME && cm->show_frame)
5228 current_frame->frame_number = 0;
5229
5230 if (cm->show_existing_frame) {
5231 current_frame->order_hint = cm->cur_frame->order_hint;
5232 } else {
5233 current_frame->order_hint =
5234 current_frame->frame_number + frame_params->order_offset;
5235 current_frame->order_hint %=
5236 (1 << (cm->seq_params.order_hint_info.order_hint_bits_minus_1 + 1));
5237 }
5238
David Turnercb5e36f2019-01-17 17:15:25 +00005239 if (cpi->oxcf.pass == 1) {
5240 av1_first_pass(cpi, frame_input->ts_duration);
5241 } else if (cpi->oxcf.pass == 0 || cpi->oxcf.pass == 2) {
David Turner73245762019-02-11 16:42:34 +00005242 if (encode_frame_to_data_rate(cpi, &frame_results->size, dest) !=
5243 AOM_CODEC_OK) {
David Turnercb5e36f2019-01-17 17:15:25 +00005244 return AOM_CODEC_ERROR;
5245 }
5246 } else {
David Turner056f7cd2019-01-07 17:48:13 +00005247 return AOM_CODEC_ERROR;
5248 }
5249
5250 return AOM_CODEC_OK;
5251}
5252
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005253#if CONFIG_DENOISE
5254static int apply_denoise_2d(AV1_COMP *cpi, YV12_BUFFER_CONFIG *sd,
5255 int block_size, float noise_level,
5256 int64_t time_stamp, int64_t end_time) {
5257 AV1_COMMON *const cm = &cpi->common;
5258 if (!cpi->denoise_and_model) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005259 cpi->denoise_and_model = aom_denoise_and_model_alloc(
5260 cm->seq_params.bit_depth, block_size, noise_level);
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005261 if (!cpi->denoise_and_model) {
5262 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5263 "Error allocating denoise and model");
5264 return -1;
5265 }
5266 }
5267 if (!cpi->film_grain_table) {
5268 cpi->film_grain_table = aom_malloc(sizeof(*cpi->film_grain_table));
5269 if (!cpi->film_grain_table) {
5270 aom_internal_error(&cm->error, AOM_CODEC_MEM_ERROR,
5271 "Error allocating grain table");
5272 return -1;
5273 }
5274 memset(cpi->film_grain_table, 0, sizeof(*cpi->film_grain_table));
5275 }
5276 if (aom_denoise_and_model_run(cpi->denoise_and_model, sd,
5277 &cm->film_grain_params)) {
5278 if (cm->film_grain_params.apply_grain) {
5279 aom_film_grain_table_append(cpi->film_grain_table, time_stamp, end_time,
5280 &cm->film_grain_params);
5281 }
5282 }
5283 return 0;
5284}
5285#endif
5286
James Zern3e2613b2017-03-30 23:14:40 -07005287int av1_receive_raw_frame(AV1_COMP *cpi, aom_enc_frame_flags_t frame_flags,
Yaowu Xuf883b422016-08-30 14:01:10 -07005288 YV12_BUFFER_CONFIG *sd, int64_t time_stamp,
5289 int64_t end_time) {
5290 AV1_COMMON *const cm = &cpi->common;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005291 const SequenceHeader *const seq_params = &cm->seq_params;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005292 int res = 0;
5293 const int subsampling_x = sd->subsampling_x;
5294 const int subsampling_y = sd->subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005295 const int use_highbitdepth = (sd->flags & YV12_FLAG_HIGHBITDEPTH) != 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005296
Yaowu Xuc27fc142016-08-22 16:08:15 -07005297 check_initial_width(cpi, use_highbitdepth, subsampling_x, subsampling_y);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005298
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005299#if CONFIG_INTERNAL_STATS
5300 struct aom_usec_timer timer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005301 aom_usec_timer_start(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005302#endif
Neil Birkbecka2893ab2018-06-08 14:45:13 -07005303#if CONFIG_DENOISE
5304 if (cpi->oxcf.noise_level > 0)
5305 if (apply_denoise_2d(cpi, sd, cpi->oxcf.noise_block_size,
5306 cpi->oxcf.noise_level, time_stamp, end_time) < 0)
5307 res = -1;
5308#endif // CONFIG_DENOISE
5309
Yaowu Xuf883b422016-08-30 14:01:10 -07005310 if (av1_lookahead_push(cpi->lookahead, sd, time_stamp, end_time,
Yaowu Xud3e7c682017-12-21 14:08:25 -08005311 use_highbitdepth, frame_flags))
Yaowu Xuc27fc142016-08-22 16:08:15 -07005312 res = -1;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005313#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005314 aom_usec_timer_mark(&timer);
5315 cpi->time_receive_data += aom_usec_timer_elapsed(&timer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005316#endif
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005317 if ((seq_params->profile == PROFILE_0) && !seq_params->monochrome &&
Yaowu Xuc27fc142016-08-22 16:08:15 -07005318 (subsampling_x != 1 || subsampling_y != 1)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005319 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005320 "Non-4:2:0 color format requires profile 1 or 2");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005321 res = -1;
5322 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005323 if ((seq_params->profile == PROFILE_1) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005324 !(subsampling_x == 0 && subsampling_y == 0)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005325 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005326 "Profile 1 requires 4:4:4 color format");
5327 res = -1;
5328 }
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005329 if ((seq_params->profile == PROFILE_2) &&
5330 (seq_params->bit_depth <= AOM_BITS_10) &&
Debargha Mukherjeef9a50ea2018-01-09 22:28:20 -08005331 !(subsampling_x == 1 && subsampling_y == 0)) {
5332 aom_internal_error(&cm->error, AOM_CODEC_INVALID_PARAM,
5333 "Profile 2 bit-depth < 10 requires 4:2:2 color format");
Yaowu Xuc27fc142016-08-22 16:08:15 -07005334 res = -1;
5335 }
5336
5337 return res;
5338}
5339
Yaowu Xuc27fc142016-08-22 16:08:15 -07005340#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005341extern double av1_get_blockiness(const unsigned char *img1, int img1_pitch,
5342 const unsigned char *img2, int img2_pitch,
5343 int width, int height);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005344
5345static void adjust_image_stat(double y, double u, double v, double all,
5346 ImageStat *s) {
Wan-Teh Changc25c92a2018-04-23 15:04:14 -07005347 s->stat[STAT_Y] += y;
5348 s->stat[STAT_U] += u;
5349 s->stat[STAT_V] += v;
5350 s->stat[STAT_ALL] += all;
Yaowu Xuf883b422016-08-30 14:01:10 -07005351 s->worst = AOMMIN(s->worst, all);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005352}
5353
Angie Chiang08a22a62017-07-17 17:29:17 -07005354static void compute_internal_stats(AV1_COMP *cpi, int frame_bytes) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005355 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005356 double samples = 0.0;
5357 uint32_t in_bit_depth = 8;
5358 uint32_t bit_depth = 8;
5359
Angie Chiang08a22a62017-07-17 17:29:17 -07005360#if CONFIG_INTER_STATS_ONLY
David Turnerd2a592e2018-11-16 14:59:31 +00005361 if (cm->current_frame.frame_type == KEY_FRAME) return; // skip key frame
Angie Chiang08a22a62017-07-17 17:29:17 -07005362#endif
5363 cpi->bytes += frame_bytes;
5364
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005365 if (cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005366 in_bit_depth = cpi->oxcf.input_bit_depth;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005367 bit_depth = cm->seq_params.bit_depth;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005368 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005369 if (cm->show_frame) {
Alex Conversef77fd0b2017-04-20 11:00:24 -07005370 const YV12_BUFFER_CONFIG *orig = cpi->source;
David Turnerc29e1a92018-12-06 14:10:14 +00005371 const YV12_BUFFER_CONFIG *recon = &cpi->common.cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005372 double y, u, v, frame_all;
5373
5374 cpi->count++;
5375 if (cpi->b_calculate_psnr) {
5376 PSNR_STATS psnr;
5377 double frame_ssim2 = 0.0, weight = 0.0;
Yaowu Xuf883b422016-08-30 14:01:10 -07005378 aom_clear_system_state();
Yaowu Xud3e7c682017-12-21 14:08:25 -08005379 // TODO(yaowu): unify these two versions into one.
Yaowu Xuf883b422016-08-30 14:01:10 -07005380 aom_calc_highbd_psnr(orig, recon, &psnr, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005381
5382 adjust_image_stat(psnr.psnr[1], psnr.psnr[2], psnr.psnr[3], psnr.psnr[0],
5383 &cpi->psnr);
5384 cpi->total_sq_error += psnr.sse[0];
5385 cpi->total_samples += psnr.samples[0];
5386 samples = psnr.samples[0];
Yaowu Xud3e7c682017-12-21 14:08:25 -08005387 // TODO(yaowu): unify these two versions into one.
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005388 if (cm->seq_params.use_highbitdepth)
Yaowu Xuc27fc142016-08-22 16:08:15 -07005389 frame_ssim2 =
Yaowu Xuf883b422016-08-30 14:01:10 -07005390 aom_highbd_calc_ssim(orig, recon, &weight, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005391 else
Yaowu Xuf883b422016-08-30 14:01:10 -07005392 frame_ssim2 = aom_calc_ssim(orig, recon, &weight);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005393
Yaowu Xuf883b422016-08-30 14:01:10 -07005394 cpi->worst_ssim = AOMMIN(cpi->worst_ssim, frame_ssim2);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005395 cpi->summed_quality += frame_ssim2 * weight;
5396 cpi->summed_weights += weight;
5397
5398#if 0
5399 {
5400 FILE *f = fopen("q_used.stt", "a");
Zoe Liuee202be2017-11-17 12:14:33 -08005401 double y2 = psnr.psnr[1];
5402 double u2 = psnr.psnr[2];
5403 double v2 = psnr.psnr[3];
5404 double frame_psnr2 = psnr.psnr[0];
Yaowu Xuc27fc142016-08-22 16:08:15 -07005405 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 +00005406 cm->current_frame.frame_number, y2, u2, v2,
Yaowu Xuc27fc142016-08-22 16:08:15 -07005407 frame_psnr2, frame_ssim2);
5408 fclose(f);
5409 }
5410#endif
5411 }
5412 if (cpi->b_calculate_blockiness) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005413 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005414 const double frame_blockiness =
Yaowu Xuf883b422016-08-30 14:01:10 -07005415 av1_get_blockiness(orig->y_buffer, orig->y_stride, recon->y_buffer,
5416 recon->y_stride, orig->y_width, orig->y_height);
5417 cpi->worst_blockiness = AOMMAX(cpi->worst_blockiness, frame_blockiness);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005418 cpi->total_blockiness += frame_blockiness;
5419 }
5420
5421 if (cpi->b_calculate_consistency) {
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005422 if (!cm->seq_params.use_highbitdepth) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005423 const double this_inconsistency = aom_get_ssim_metrics(
Yaowu Xuc27fc142016-08-22 16:08:15 -07005424 orig->y_buffer, orig->y_stride, recon->y_buffer, recon->y_stride,
5425 orig->y_width, orig->y_height, cpi->ssim_vars, &cpi->metrics, 1);
5426
5427 const double peak = (double)((1 << in_bit_depth) - 1);
5428 const double consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005429 aom_sse_to_psnr(samples, peak, cpi->total_inconsistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005430 if (consistency > 0.0)
5431 cpi->worst_consistency =
Yaowu Xuf883b422016-08-30 14:01:10 -07005432 AOMMIN(cpi->worst_consistency, consistency);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005433 cpi->total_inconsistency += this_inconsistency;
5434 }
5435 }
5436 }
5437
5438 frame_all =
Yaowu Xuf883b422016-08-30 14:01:10 -07005439 aom_calc_fastssim(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005440 adjust_image_stat(y, u, v, frame_all, &cpi->fastssim);
Yaowu Xuf883b422016-08-30 14:01:10 -07005441 frame_all = aom_psnrhvs(orig, recon, &y, &u, &v, bit_depth, in_bit_depth);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005442 adjust_image_stat(y, u, v, frame_all, &cpi->psnrhvs);
5443 }
5444}
5445#endif // CONFIG_INTERNAL_STATS
5446
Andrey Norkin795ba872018-03-06 13:24:14 -08005447int av1_get_compressed_data(AV1_COMP *cpi, unsigned int *frame_flags,
5448 size_t *size, uint8_t *dest, int64_t *time_stamp,
5449 int64_t *time_end, int flush,
5450 const aom_rational_t *timebase) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005451 const AV1EncoderConfig *const oxcf = &cpi->oxcf;
5452 AV1_COMMON *const cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005453
5454#if CONFIG_BITSTREAM_DEBUG
5455 assert(cpi->oxcf.max_threads == 0 &&
5456 "bitstream debug tool does not support multithreading");
5457 bitstream_queue_record_write();
David Turnerd2a592e2018-11-16 14:59:31 +00005458 bitstream_queue_set_frame_write(current_frame->frame_number * 2 +
5459 cm->show_frame);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005460#endif
5461
Sarah Parker740e8392019-01-23 15:47:53 -08005462 // Indicates whether or not to use an adaptive quantize b rather than
5463 // the traditional version
5464 cm->use_quant_b_adapt = cpi->oxcf.quant_b_adapt;
5465
Dominic Symesd4929012018-01-31 17:32:01 +01005466 cm->showable_frame = 0;
David Turnere43f7fe2019-01-15 14:58:00 +00005467 *size = 0;
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005468#if CONFIG_INTERNAL_STATS
5469 struct aom_usec_timer cmptimer;
Yaowu Xuf883b422016-08-30 14:01:10 -07005470 aom_usec_timer_start(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005471#endif
RogerZhou3b635242017-09-19 10:06:46 -07005472 set_high_precision_mv(cpi, ALTREF_HIGH_PRECISION_MV, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005473
Debargha Mukherjeeba7b8fe2018-03-15 23:10:07 -07005474 // Normal defaults
sarahparker27d686a2018-03-30 17:43:44 -07005475 cm->refresh_frame_context = oxcf->frame_parallel_decoding_mode
5476 ? REFRESH_FRAME_CONTEXT_DISABLED
5477 : REFRESH_FRAME_CONTEXT_BACKWARD;
Rupert Swarbrick84b05ac2017-10-27 18:10:53 +01005478 if (oxcf->large_scale_tile)
James Zernf34dfc82018-02-23 16:53:33 -08005479 cm->refresh_frame_context = REFRESH_FRAME_CONTEXT_DISABLED;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005480
Sarah Parkerb9041612018-05-22 19:06:47 -07005481 // Initialize fields related to forward keyframes
Sarah Parkeraf32a7b2018-06-29 14:59:05 -07005482 cpi->no_show_kf = 0;
Zoe Liub4991202017-12-21 15:31:06 -08005483
David Turnerdedd8ff2019-01-23 13:59:46 +00005484 if (assign_cur_frame_new_fb(cm) == NULL) return AOM_CODEC_ERROR;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005485
David Turnerdedd8ff2019-01-23 13:59:46 +00005486 const int result = av1_encode_strategy(cpi, size, dest, frame_flags,
5487 time_stamp, time_end, timebase, flush);
5488 if (result != AOM_CODEC_OK && result != -1) {
David Turner1539bb02019-01-24 15:28:13 +00005489 return AOM_CODEC_ERROR;
David Turnerdedd8ff2019-01-23 13:59:46 +00005490 } else if (result == -1) {
5491 // Returning -1 indicates no frame encoded; more input is required
5492 return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005493 }
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005494#if CONFIG_INTERNAL_STATS
Yaowu Xuf883b422016-08-30 14:01:10 -07005495 aom_usec_timer_mark(&cmptimer);
5496 cpi->time_compress_data += aom_usec_timer_elapsed(&cmptimer);
Yunqing Wangd1f32e62019-02-20 10:37:51 -08005497#endif
David Turnerc4bf8c72019-01-15 13:14:37 +00005498 if (cpi->b_calculate_psnr) {
5499 if (cm->show_existing_frame || (oxcf->pass != 1 && cm->show_frame)) {
5500 generate_psnr_packet(cpi);
5501 }
5502 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005503
5504#if CONFIG_INTERNAL_STATS
5505 if (oxcf->pass != 1) {
Angie Chiang08a22a62017-07-17 17:29:17 -07005506 compute_internal_stats(cpi, (int)(*size));
Yaowu Xuc27fc142016-08-22 16:08:15 -07005507 }
5508#endif // CONFIG_INTERNAL_STATS
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005509#if CONFIG_SPEED_STATS
David Turnerc4bf8c72019-01-15 13:14:37 +00005510 if (cpi->oxcf.pass != 1 && !cm->show_existing_frame) {
Debargha Mukherjee0857e662019-01-04 16:22:09 -08005511 cpi->tx_search_count += cpi->td.mb.tx_search_count;
5512 cpi->td.mb.tx_search_count = 0;
5513 }
5514#endif // CONFIG_SPEED_STATS
Yaowu Xuc27fc142016-08-22 16:08:15 -07005515
Yaowu Xuf883b422016-08-30 14:01:10 -07005516 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005517
5518 return 0;
5519}
5520
Yaowu Xuf883b422016-08-30 14:01:10 -07005521int av1_get_preview_raw_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *dest) {
5522 AV1_COMMON *cm = &cpi->common;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005523 if (!cm->show_frame) {
5524 return -1;
5525 } else {
5526 int ret;
David Turnerc29e1a92018-12-06 14:10:14 +00005527 if (cm->cur_frame != NULL) {
5528 *dest = cm->cur_frame->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005529 dest->y_width = cm->width;
5530 dest->y_height = cm->height;
Urvang Joshi20cf30e2018-07-19 02:33:58 -07005531 dest->uv_width = cm->width >> cm->seq_params.subsampling_x;
5532 dest->uv_height = cm->height >> cm->seq_params.subsampling_y;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005533 ret = 0;
5534 } else {
5535 ret = -1;
5536 }
Yaowu Xuf883b422016-08-30 14:01:10 -07005537 aom_clear_system_state();
Yaowu Xuc27fc142016-08-22 16:08:15 -07005538 return ret;
5539 }
5540}
5541
Yaowu Xuf883b422016-08-30 14:01:10 -07005542int av1_get_last_show_frame(AV1_COMP *cpi, YV12_BUFFER_CONFIG *frame) {
David Turnere7ebf902018-12-04 14:04:55 +00005543 if (cpi->last_show_frame_buf == NULL) return -1;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005544
David Turnere7ebf902018-12-04 14:04:55 +00005545 *frame = cpi->last_show_frame_buf->buf;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005546 return 0;
5547}
5548
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005549static int equal_dimensions_and_border(const YV12_BUFFER_CONFIG *a,
5550 const YV12_BUFFER_CONFIG *b) {
5551 return a->y_height == b->y_height && a->y_width == b->y_width &&
5552 a->uv_height == b->uv_height && a->uv_width == b->uv_width &&
5553 a->y_stride == b->y_stride && a->uv_stride == b->uv_stride &&
5554 a->border == b->border &&
5555 (a->flags & YV12_FLAG_HIGHBITDEPTH) ==
5556 (b->flags & YV12_FLAG_HIGHBITDEPTH);
5557}
5558
Yunqing Wang93b18f32018-06-08 21:08:29 -07005559aom_codec_err_t av1_copy_new_frame_enc(AV1_COMMON *cm,
5560 YV12_BUFFER_CONFIG *new_frame,
5561 YV12_BUFFER_CONFIG *sd) {
Yunqing Wangff9bfca2018-06-06 11:46:08 -07005562 const int num_planes = av1_num_planes(cm);
5563 if (!equal_dimensions_and_border(new_frame, sd))
5564 aom_internal_error(&cm->error, AOM_CODEC_ERROR,
5565 "Incorrect buffer dimensions");
5566 else
5567 aom_yv12_copy_frame(new_frame, sd, num_planes);
5568
5569 return cm->error.error_code;
5570}
5571
Yaowu Xuf883b422016-08-30 14:01:10 -07005572int av1_set_internal_size(AV1_COMP *cpi, AOM_SCALING horiz_mode,
5573 AOM_SCALING vert_mode) {
Yaowu Xuc27fc142016-08-22 16:08:15 -07005574 int hr = 0, hs = 0, vr = 0, vs = 0;
5575
5576 if (horiz_mode > ONETWO || vert_mode > ONETWO) return -1;
5577
5578 Scale2Ratio(horiz_mode, &hr, &hs);
5579 Scale2Ratio(vert_mode, &vr, &vs);
5580
5581 // always go to the next whole number
Debargha Mukherjeeccb27262017-09-25 14:19:46 -07005582 cpi->resize_pending_width = (hs - 1 + cpi->oxcf.width * hr) / hs;
5583 cpi->resize_pending_height = (vs - 1 + cpi->oxcf.height * vr) / vs;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005584
5585 return 0;
5586}
5587
Yaowu Xuf883b422016-08-30 14:01:10 -07005588int av1_get_quantizer(AV1_COMP *cpi) { return cpi->common.base_qindex; }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005589
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005590int av1_convert_sect5obus_to_annexb(uint8_t *buffer, size_t *frame_size) {
5591 size_t output_size = 0;
5592 size_t total_bytes_read = 0;
5593 size_t remaining_size = *frame_size;
5594 uint8_t *buff_ptr = buffer;
5595
5596 // go through each OBUs
5597 while (total_bytes_read < *frame_size) {
5598 uint8_t saved_obu_header[2];
5599 uint64_t obu_payload_size;
5600 size_t length_of_payload_size;
5601 size_t length_of_obu_size;
5602 uint32_t obu_header_size = (buff_ptr[0] >> 2) & 0x1 ? 2 : 1;
5603 size_t obu_bytes_read = obu_header_size; // bytes read for current obu
5604
5605 // save the obu header (1 or 2 bytes)
5606 memmove(saved_obu_header, buff_ptr, obu_header_size);
5607 // clear the obu_has_size_field
5608 saved_obu_header[0] = saved_obu_header[0] & (~0x2);
5609
5610 // get the payload_size and length of payload_size
5611 if (aom_uleb_decode(buff_ptr + obu_header_size, remaining_size,
5612 &obu_payload_size, &length_of_payload_size) != 0) {
5613 return AOM_CODEC_ERROR;
5614 }
5615 obu_bytes_read += length_of_payload_size;
5616
5617 // calculate the length of size of the obu header plus payload
5618 length_of_obu_size =
5619 aom_uleb_size_in_bytes((uint64_t)(obu_header_size + obu_payload_size));
5620
5621 // move the rest of data to new location
5622 memmove(buff_ptr + length_of_obu_size + obu_header_size,
5623 buff_ptr + obu_bytes_read, remaining_size - obu_bytes_read);
Yaowu Xu9e494202018-04-03 11:19:49 -07005624 obu_bytes_read += (size_t)obu_payload_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005625
5626 // write the new obu size
5627 const uint64_t obu_size = obu_header_size + obu_payload_size;
5628 size_t coded_obu_size;
5629 if (aom_uleb_encode(obu_size, sizeof(obu_size), buff_ptr,
5630 &coded_obu_size) != 0) {
5631 return AOM_CODEC_ERROR;
5632 }
5633
5634 // write the saved (modified) obu_header following obu size
5635 memmove(buff_ptr + length_of_obu_size, saved_obu_header, obu_header_size);
5636
5637 total_bytes_read += obu_bytes_read;
5638 remaining_size -= obu_bytes_read;
5639 buff_ptr += length_of_obu_size + obu_size;
Yaowu Xu9e494202018-04-03 11:19:49 -07005640 output_size += length_of_obu_size + (size_t)obu_size;
Soo-Chul Han29c46fb2018-03-23 16:02:00 -04005641 }
5642
5643 *frame_size = output_size;
5644 return AOM_CODEC_OK;
5645}
5646
Yaowu Xuf883b422016-08-30 14:01:10 -07005647void av1_apply_encoding_flags(AV1_COMP *cpi, aom_enc_frame_flags_t flags) {
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005648 // TODO(yunqingwang): For what references to use, external encoding flags
5649 // should be consistent with internal reference frame selection. Need to
5650 // ensure that there is not conflict between the two. In AV1 encoder, the
5651 // priority rank for 7 reference frames are: LAST, ALTREF, LAST2, LAST3,
5652 // GOLDEN, BWDREF, ALTREF2. If only one reference frame is used, it must be
5653 // LAST.
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005654 cpi->ext_ref_frame_flags = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005655 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005656 (AOM_EFLAG_NO_REF_LAST | AOM_EFLAG_NO_REF_LAST2 | AOM_EFLAG_NO_REF_LAST3 |
5657 AOM_EFLAG_NO_REF_GF | AOM_EFLAG_NO_REF_ARF | AOM_EFLAG_NO_REF_BWD |
5658 AOM_EFLAG_NO_REF_ARF2)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005659 if (flags & AOM_EFLAG_NO_REF_LAST) {
Yunqing Wangf2e7a392017-11-08 00:27:21 -08005660 cpi->ext_ref_frame_flags = 0;
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005661 } else {
5662 int ref = AOM_REFFRAME_ALL;
5663
5664 if (flags & AOM_EFLAG_NO_REF_LAST2) ref ^= AOM_LAST2_FLAG;
5665 if (flags & AOM_EFLAG_NO_REF_LAST3) ref ^= AOM_LAST3_FLAG;
5666
5667 if (flags & AOM_EFLAG_NO_REF_GF) ref ^= AOM_GOLD_FLAG;
5668
5669 if (flags & AOM_EFLAG_NO_REF_ARF) {
5670 ref ^= AOM_ALT_FLAG;
5671 ref ^= AOM_BWD_FLAG;
5672 ref ^= AOM_ALT2_FLAG;
5673 } else {
5674 if (flags & AOM_EFLAG_NO_REF_BWD) ref ^= AOM_BWD_FLAG;
5675 if (flags & AOM_EFLAG_NO_REF_ARF2) ref ^= AOM_ALT2_FLAG;
5676 }
5677
5678 av1_use_as_reference(cpi, ref);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005679 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005680 }
5681
5682 if (flags &
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005683 (AOM_EFLAG_NO_UPD_LAST | AOM_EFLAG_NO_UPD_GF | AOM_EFLAG_NO_UPD_ARF)) {
Yaowu Xuf883b422016-08-30 14:01:10 -07005684 int upd = AOM_REFFRAME_ALL;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005685
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005686 // Refreshing LAST/LAST2/LAST3 is handled by 1 common flag.
5687 if (flags & AOM_EFLAG_NO_UPD_LAST) upd ^= AOM_LAST_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005688
Yaowu Xuf883b422016-08-30 14:01:10 -07005689 if (flags & AOM_EFLAG_NO_UPD_GF) upd ^= AOM_GOLD_FLAG;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005690
Yunqing Wang9a50fec2017-11-02 17:02:00 -07005691 if (flags & AOM_EFLAG_NO_UPD_ARF) {
5692 upd ^= AOM_ALT_FLAG;
5693 upd ^= AOM_BWD_FLAG;
5694 upd ^= AOM_ALT2_FLAG;
5695 }
Yaowu Xuc27fc142016-08-22 16:08:15 -07005696
David Turnerce9b5902019-01-23 17:25:47 +00005697 cpi->ext_refresh_last_frame = (upd & AOM_LAST_FLAG) != 0;
5698 cpi->ext_refresh_golden_frame = (upd & AOM_GOLD_FLAG) != 0;
5699 cpi->ext_refresh_alt_ref_frame = (upd & AOM_ALT_FLAG) != 0;
5700 cpi->ext_refresh_bwd_ref_frame = (upd & AOM_BWD_FLAG) != 0;
5701 cpi->ext_refresh_alt2_ref_frame = (upd & AOM_ALT2_FLAG) != 0;
5702 cpi->ext_refresh_frame_flags_pending = 1;
David Turner4f1f1812019-01-24 17:00:24 +00005703 } else {
5704 cpi->ext_refresh_frame_flags_pending = 0;
Yaowu Xuc27fc142016-08-22 16:08:15 -07005705 }
5706
sarahparker21dbca42018-03-30 17:43:44 -07005707 cpi->ext_use_ref_frame_mvs = cpi->oxcf.allow_ref_frame_mvs &
5708 ((flags & AOM_EFLAG_NO_REF_FRAME_MVS) == 0);
sarahparker27d686a2018-03-30 17:43:44 -07005709 cpi->ext_use_error_resilient = cpi->oxcf.error_resilient_mode |
5710 ((flags & AOM_EFLAG_ERROR_RESILIENT) != 0);
sarahparker9806fed2018-03-30 17:43:44 -07005711 cpi->ext_use_s_frame =
5712 cpi->oxcf.s_frame_mode | ((flags & AOM_EFLAG_SET_S_FRAME) != 0);
Sarah Parker50b6d6e2018-04-11 19:21:54 -07005713 cpi->ext_use_primary_ref_none = (flags & AOM_EFLAG_SET_PRIMARY_REF_NONE) != 0;
sarahparker21dbca42018-03-30 17:43:44 -07005714
Yaowu Xuf883b422016-08-30 14:01:10 -07005715 if (flags & AOM_EFLAG_NO_UPD_ENTROPY) {
5716 av1_update_entropy(cpi, 0);
Yaowu Xuc27fc142016-08-22 16:08:15 -07005717 }
5718}
Andrey Norkin795ba872018-03-06 13:24:14 -08005719
Tom Fineganf8d6a162018-08-21 10:47:55 -07005720aom_fixed_buf_t *av1_get_global_headers(AV1_COMP *cpi) {
5721 if (!cpi) return NULL;
5722
5723 uint8_t header_buf[512] = { 0 };
5724 const uint32_t sequence_header_size =
5725 write_sequence_header_obu(cpi, &header_buf[0]);
5726 assert(sequence_header_size <= sizeof(header_buf));
5727 if (sequence_header_size == 0) return NULL;
5728
5729 const size_t obu_header_size = 1;
5730 const size_t size_field_size = aom_uleb_size_in_bytes(sequence_header_size);
5731 const size_t payload_offset = obu_header_size + size_field_size;
5732
5733 if (payload_offset + sequence_header_size > sizeof(header_buf)) return NULL;
5734 memmove(&header_buf[payload_offset], &header_buf[0], sequence_header_size);
5735
5736 if (write_obu_header(OBU_SEQUENCE_HEADER, 0, &header_buf[0]) !=
5737 obu_header_size) {
5738 return NULL;
5739 }
5740
5741 size_t coded_size_field_size = 0;
5742 if (aom_uleb_encode(sequence_header_size, size_field_size,
5743 &header_buf[obu_header_size],
5744 &coded_size_field_size) != 0) {
5745 return NULL;
5746 }
5747 assert(coded_size_field_size == size_field_size);
5748
5749 aom_fixed_buf_t *global_headers =
5750 (aom_fixed_buf_t *)malloc(sizeof(*global_headers));
5751 if (!global_headers) return NULL;
5752
5753 const size_t global_header_buf_size =
5754 obu_header_size + size_field_size + sequence_header_size;
5755
5756 global_headers->buf = malloc(global_header_buf_size);
5757 if (!global_headers->buf) {
5758 free(global_headers);
5759 return NULL;
5760 }
5761
5762 memcpy(global_headers->buf, &header_buf[0], global_header_buf_size);
5763 global_headers->sz = global_header_buf_size;
5764 return global_headers;
5765}